L2NpcTemplate.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.templates;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.logging.Logger;
  19. import javolution.util.FastList;
  20. import javolution.util.FastMap;
  21. import net.sf.l2j.gameserver.model.L2DropCategory;
  22. import net.sf.l2j.gameserver.model.L2DropData;
  23. import net.sf.l2j.gameserver.model.L2MinionData;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.model.base.ClassId;
  26. import net.sf.l2j.gameserver.model.quest.Quest;
  27. import net.sf.l2j.gameserver.skills.Stats;
  28. /**
  29. * This cl contains all generic data of a L2Spawn object.<BR><BR>
  30. *
  31. * <B><U> Data</U> :</B><BR><BR>
  32. * <li>npcId, type, name, sex</li>
  33. * <li>rewardExp, rewardSp</li>
  34. * <li>aggroRange, factionId, factionRange</li>
  35. * <li>rhand, lhand, armor</li>
  36. * <li>isUndead</li>
  37. * <li>_drops</li>
  38. * <li>_minions</li>
  39. * <li>_teachInfo</li>
  40. * <li>_skills</li>
  41. * <li>_questsStart</li><BR><BR>
  42. *
  43. * @version $Revision: 1.1.2.4 $ $Date: 2005/04/02 15:57:51 $
  44. */
  45. public final class L2NpcTemplate extends L2CharTemplate
  46. {
  47. protected static final Logger _log = Logger.getLogger(Quest.class.getName());
  48. public final int npcId;
  49. public final int idTemplate;
  50. public final String type;
  51. public final String name;
  52. public final boolean serverSideName;
  53. public final String title;
  54. public final boolean serverSideTitle;
  55. public final String sex;
  56. public final byte level;
  57. public final int rewardExp;
  58. public final int rewardSp;
  59. public final int aggroRange;
  60. public final int rhand;
  61. public final int lhand;
  62. public final int armor;
  63. public final String factionId;
  64. public final int factionRange;
  65. public final int absorbLevel;
  66. public final AbsorbCrystalType absorbType;
  67. public final short ss;
  68. public final short bss;
  69. public final short ssRate;
  70. public Race race;
  71. public final String jClass;
  72. public final AIType AI;
  73. public final boolean dropherb;
  74. public boolean isQuestMonster; // doesn't include all mobs that are involved in
  75. // quests, just plain quest monsters for preventing champion spawn
  76. public static enum AbsorbCrystalType
  77. {
  78. LAST_HIT,
  79. FULL_PARTY,
  80. PARTY_ONE_RANDOM
  81. }
  82. public static enum AIType
  83. {
  84. FIGHTER,
  85. ARCHER,
  86. BALANCED,
  87. MAGE
  88. }
  89. public static enum Race
  90. {
  91. UNDEAD,
  92. MAGICCREATURE,
  93. BEAST,
  94. ANIMAL,
  95. PLANT,
  96. HUMANOID,
  97. SPIRIT,
  98. ANGEL,
  99. DEMON,
  100. DRAGON,
  101. GIANT,
  102. BUG,
  103. FAIRIE,
  104. HUMAN,
  105. ELVE,
  106. DARKELVE,
  107. ORC,
  108. DWARVE,
  109. OTHER,
  110. NONLIVING,
  111. SIEGEWEAPON,
  112. DEFENDINGARMY,
  113. MERCENARIE,
  114. UNKNOWN,
  115. KAMAEL,
  116. NONE
  117. }
  118. //private final StatsSet _npcStatsSet;
  119. /** The table containing all Item that can be dropped by L2NpcInstance using this L2NpcTemplate*/
  120. private FastList<L2DropCategory> _categories = null;
  121. /** The table containing all Minions that must be spawn with the L2NpcInstance using this L2NpcTemplate*/
  122. private List<L2MinionData> _minions = null;
  123. private List<ClassId> _teachInfo;
  124. private Map<Integer, L2Skill> _skills;
  125. private Map<Stats, Double> _vulnerabilities;
  126. // contains a list of quests for each event type (questStart, questAttack, questKill, etc)
  127. private Map<Quest.QuestEventType, Quest[]> _questEvents;
  128. /**
  129. * Constructor of L2Character.<BR><BR>
  130. *
  131. * @param set The StatsSet object to transfer data to the method
  132. *
  133. */
  134. public L2NpcTemplate(StatsSet set)
  135. {
  136. super(set);
  137. npcId = set.getInteger("npcId");
  138. idTemplate = set.getInteger("idTemplate");
  139. type = set.getString("type");
  140. name = set.getString("name");
  141. serverSideName = set.getBool("serverSideName");
  142. title = set.getString("title");
  143. if (title.equalsIgnoreCase("Quest Monster"))
  144. isQuestMonster = true;
  145. else
  146. isQuestMonster = false;
  147. serverSideTitle = set.getBool("serverSideTitle");
  148. sex = set.getString("sex");
  149. level = set.getByte("level");
  150. rewardExp = set.getInteger("rewardExp");
  151. rewardSp = set.getInteger("rewardSp");
  152. aggroRange= set.getInteger("aggroRange");
  153. rhand = set.getInteger("rhand");
  154. lhand = set.getInteger("lhand");
  155. armor = set.getInteger("armor");
  156. String f = set.getString("factionId", null);
  157. if (f == null)
  158. factionId = null;
  159. else
  160. factionId = f.intern();
  161. factionRange = set.getInteger("factionRange");
  162. absorbLevel = set.getInteger("absorb_level", 0);
  163. absorbType = AbsorbCrystalType.valueOf(set.getString("absorb_type"));
  164. ss = (short)set.getInteger("ss", 0);
  165. bss = (short)set.getInteger("bss", 0);
  166. ssRate = (short)set.getInteger("ssRate", 0);
  167. race = null;
  168. dropherb = set.getBool("drop_herbs", false);
  169. //_npcStatsSet = set;
  170. _teachInfo = null;
  171. jClass = set.getString("jClass");
  172. String ai = set.getString("AI", "fighter");
  173. if (ai.equalsIgnoreCase("archer")) AI = AIType.ARCHER;
  174. else if (ai.equalsIgnoreCase("balanced")) AI = AIType.BALANCED;
  175. else if (ai.equalsIgnoreCase("mage")) AI = AIType.MAGE;
  176. else AI = AIType.FIGHTER;
  177. }
  178. public void addTeachInfo(ClassId classId)
  179. {
  180. if (_teachInfo == null)
  181. _teachInfo = new FastList<ClassId>();
  182. _teachInfo.add(classId);
  183. }
  184. public ClassId[] getTeachInfo()
  185. {
  186. if (_teachInfo == null)
  187. return null;
  188. return _teachInfo.toArray(new ClassId[_teachInfo.size()]);
  189. }
  190. public boolean canTeach(ClassId classId)
  191. {
  192. if (_teachInfo == null)
  193. return false;
  194. // If the player is on a third class, fetch the class teacher
  195. // information for its parent class.
  196. if (classId.level() == 3)
  197. return _teachInfo.contains(classId.getParent());
  198. return _teachInfo.contains(classId);
  199. }
  200. // add a drop to a given category. If the category does not exist, create it.
  201. public void addDropData(L2DropData drop, int categoryType)
  202. {
  203. if (drop.isQuestDrop()) {
  204. // if (_questDrops == null)
  205. // _questDrops = new FastList<L2DropData>(0);
  206. // _questDrops.add(drop);
  207. } else {
  208. if (_categories == null) _categories = new FastList<L2DropCategory>();
  209. // if the category doesn't already exist, create it first
  210. synchronized (_categories)
  211. {
  212. boolean catExists = false;
  213. for(L2DropCategory cat:_categories)
  214. // if the category exists, add the drop to this category.
  215. if (cat.getCategoryType() == categoryType)
  216. {
  217. cat.addDropData(drop, type.equalsIgnoreCase("L2RaidBoss") || type.equalsIgnoreCase("L2GrandBoss"));
  218. catExists = true;
  219. break;
  220. }
  221. // if the category doesn't exit, create it and add the drop
  222. if (!catExists)
  223. {
  224. L2DropCategory cat = new L2DropCategory(categoryType);
  225. cat.addDropData(drop, type.equalsIgnoreCase("L2RaidBoss") || type.equalsIgnoreCase("L2GrandBoss"));
  226. _categories.add(cat);
  227. }
  228. }
  229. }
  230. }
  231. public void addRaidData(L2MinionData minion)
  232. {
  233. if (_minions == null) _minions = new FastList<L2MinionData>();
  234. _minions.add(minion);
  235. }
  236. public void addSkill(L2Skill skill)
  237. {
  238. if (_skills == null)
  239. _skills = new FastMap<Integer, L2Skill>();
  240. _skills.put(skill.getId(), skill);
  241. }
  242. public void addVulnerability(Stats id, double vuln)
  243. {
  244. if (_vulnerabilities == null)
  245. _vulnerabilities = new FastMap<Stats, Double>();
  246. _vulnerabilities.put(id, new Double(vuln));
  247. }
  248. public double getVulnerability(Stats id)
  249. {
  250. if(_vulnerabilities == null || _vulnerabilities.get(id) == null)
  251. return 1;
  252. return _vulnerabilities.get(id);
  253. }
  254. public double removeVulnerability(Stats id)
  255. {
  256. return _vulnerabilities.remove(id);
  257. }
  258. /**
  259. * Return the list of all possible UNCATEGORIZED drops of this L2NpcTemplate.<BR><BR>
  260. */
  261. public FastList<L2DropCategory> getDropData()
  262. {
  263. return _categories;
  264. }
  265. /**
  266. * Return the list of all possible item drops of this L2NpcTemplate.<BR>
  267. * (ie full drops and part drops, mats, miscellaneous & UNCATEGORIZED)<BR><BR>
  268. */
  269. public List<L2DropData> getAllDropData()
  270. {
  271. if (_categories == null) return null;
  272. List<L2DropData> lst = new FastList<L2DropData>();
  273. for (L2DropCategory tmp:_categories)
  274. {
  275. lst.addAll(tmp.getAllDrops());
  276. }
  277. return lst;
  278. }
  279. /**
  280. * Empty all possible drops of this L2NpcTemplate.<BR><BR>
  281. */
  282. public synchronized void clearAllDropData()
  283. {
  284. if (_categories == null) return;
  285. while (_categories.size() > 0)
  286. {
  287. _categories.getFirst().clearAllDrops();
  288. _categories.removeFirst();
  289. }
  290. _categories.clear();
  291. }
  292. /**
  293. * Return the list of all Minions that must be spawn with the L2NpcInstance using this L2NpcTemplate.<BR><BR>
  294. */
  295. public List<L2MinionData> getMinionData()
  296. {
  297. return _minions;
  298. }
  299. public Map<Integer, L2Skill> getSkills()
  300. {
  301. return _skills;
  302. }
  303. public void addQuestEvent(Quest.QuestEventType EventType, Quest q)
  304. {
  305. if (_questEvents == null)
  306. _questEvents = new FastMap<Quest.QuestEventType, Quest[]>();
  307. if (_questEvents.get(EventType) == null) {
  308. _questEvents.put(EventType, new Quest[]{q});
  309. }
  310. else
  311. {
  312. Quest[] _quests = _questEvents.get(EventType);
  313. int len = _quests.length;
  314. // if only one registration per npc is allowed for this event type
  315. // then only register this NPC if not already registered for the specified event.
  316. // if a quest allows multiple registrations, then register regardless of count
  317. // In all cases, check if this new registration is replacing an older copy of the SAME quest
  318. // Finally, check quest class hierarchy: a parent class should never replace a child class.
  319. // a child class should always replace a parent class.
  320. if (!EventType.isMultipleRegistrationAllowed())
  321. {
  322. // if it is the same quest (i.e. reload) or the existing is a superclass of the new one, replace the existing.
  323. if ( _quests[0].getName().equals(q.getName()) || L2NpcTemplate.isAssignableTo(q, _quests[0].getClass()))
  324. {
  325. _quests[0] = q;
  326. }
  327. else
  328. {
  329. _log.warning("Quest event not allowed in multiple quests. Skipped addition of Event Type \""+EventType+"\" for NPC \""+name +"\" and quest \""+q.getName()+"\".");
  330. }
  331. }
  332. else
  333. {
  334. // be ready to add a new quest to a new copy of the list, with larger size than previously.
  335. Quest[] tmp = new Quest[len+1];
  336. // loop through the existing quests and copy them to the new list. While doing so, also
  337. // check if this new quest happens to be just a replacement for a previously loaded quest.
  338. // Replace existing if the new quest is the same (reload) or a child of the existing quest.
  339. // Do nothing if the new quest is a superclass of an existing quest.
  340. // Add the new quest in the end of the list otherwise.
  341. for (int i=0; i < len; i++)
  342. {
  343. if (_quests[i].getName().equals(q.getName()) || L2NpcTemplate.isAssignableTo(q, _quests[i].getClass()))
  344. {
  345. _quests[i] = q;
  346. return;
  347. }
  348. else if (L2NpcTemplate.isAssignableTo(_quests[i], q.getClass()))
  349. {
  350. return;
  351. }
  352. tmp[i] = _quests[i];
  353. }
  354. tmp[len] = q;
  355. _questEvents.put(EventType, tmp);
  356. }
  357. }
  358. }
  359. /**
  360. * Checks if obj can be assigned to the Class represented by clazz.<br>
  361. * This is true if, and only if, obj is the same class represented by clazz,
  362. * or a subclass of it or obj implements the interface represented by clazz.
  363. *
  364. *
  365. * @param obj
  366. * @param clazz
  367. * @return
  368. */
  369. public static boolean isAssignableTo(Object obj, Class<?> clazz)
  370. {
  371. return L2NpcTemplate.isAssignableTo(obj.getClass(), clazz);
  372. }
  373. public static boolean isAssignableTo(Class<?> sub, Class<?> clazz)
  374. {
  375. // if clazz represents an interface
  376. if (clazz.isInterface())
  377. {
  378. // check if obj implements the clazz interface
  379. Class<?>[] interfaces = sub.getInterfaces();
  380. for (int i = 0; i < interfaces.length; i++)
  381. {
  382. if (clazz.getName().equals(interfaces[i].getName()))
  383. {
  384. return true;
  385. }
  386. }
  387. }
  388. else
  389. {
  390. do
  391. {
  392. if (sub.getName().equals(clazz.getName()))
  393. {
  394. return true;
  395. }
  396. sub = sub.getSuperclass();
  397. }
  398. while (sub != null);
  399. }
  400. return false;
  401. }
  402. public Quest[] getEventQuests(Quest.QuestEventType EventType)
  403. {
  404. if (_questEvents == null)
  405. {
  406. return null;
  407. }
  408. return _questEvents.get(EventType);
  409. }
  410. public void setRace(int raceId)
  411. {
  412. switch (raceId)
  413. {
  414. case 1:
  415. race = L2NpcTemplate.Race.UNDEAD;
  416. break;
  417. case 2:
  418. race = L2NpcTemplate.Race.MAGICCREATURE;
  419. break;
  420. case 3:
  421. race = L2NpcTemplate.Race.BEAST;
  422. break;
  423. case 4:
  424. race = L2NpcTemplate.Race.ANIMAL;
  425. break;
  426. case 5:
  427. race = L2NpcTemplate.Race.PLANT;
  428. break;
  429. case 6:
  430. race = L2NpcTemplate.Race.HUMANOID;
  431. break;
  432. case 7:
  433. race = L2NpcTemplate.Race.SPIRIT;
  434. break;
  435. case 8:
  436. race = L2NpcTemplate.Race.ANGEL;
  437. break;
  438. case 9:
  439. race = L2NpcTemplate.Race.DEMON;
  440. break;
  441. case 10:
  442. race = L2NpcTemplate.Race.DRAGON;
  443. break;
  444. case 11:
  445. race = L2NpcTemplate.Race.GIANT;
  446. break;
  447. case 12:
  448. race = L2NpcTemplate.Race.BUG;
  449. break;
  450. case 13:
  451. race = L2NpcTemplate.Race.FAIRIE;
  452. break;
  453. case 14:
  454. race = L2NpcTemplate.Race.HUMAN;
  455. break;
  456. case 15:
  457. race = L2NpcTemplate.Race.ELVE;
  458. break;
  459. case 16:
  460. race = L2NpcTemplate.Race.DARKELVE;
  461. break;
  462. case 17:
  463. race = L2NpcTemplate.Race.ORC;
  464. break;
  465. case 18:
  466. race = L2NpcTemplate.Race.DWARVE;
  467. break;
  468. case 19:
  469. race = L2NpcTemplate.Race.OTHER;
  470. break;
  471. case 20:
  472. race = L2NpcTemplate.Race.NONLIVING;
  473. break;
  474. case 21:
  475. race = L2NpcTemplate.Race.SIEGEWEAPON;
  476. break;
  477. case 22:
  478. race = L2NpcTemplate.Race.DEFENDINGARMY;
  479. break;
  480. case 23:
  481. race = L2NpcTemplate.Race.MERCENARIE;
  482. break;
  483. case 24:
  484. race = L2NpcTemplate.Race.UNKNOWN;
  485. break;
  486. case 25:
  487. race = L2NpcTemplate.Race.KAMAEL;
  488. break;
  489. default:
  490. race = L2NpcTemplate.Race.NONE;
  491. break;
  492. }
  493. }
  494. public L2NpcTemplate.Race getRace()
  495. {
  496. if (race == null)
  497. race = L2NpcTemplate.Race.NONE;
  498. return race;
  499. }
  500. public boolean isCustom()
  501. {
  502. return npcId != idTemplate;
  503. }
  504. /**
  505. * @return name
  506. */
  507. public String getName()
  508. {
  509. return name;
  510. }
  511. }