L2NpcTemplate.java 15 KB

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