L2NpcTemplate.java 14 KB

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