L2NpcTemplate.java 15 KB

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