L2NpcTemplate.java 12 KB

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