L2NpcTemplate.java 15 KB

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