NpcTable.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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.datatables;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import net.sf.l2j.Config;
  27. import net.sf.l2j.L2DatabaseFactory;
  28. import net.sf.l2j.gameserver.model.L2DropCategory;
  29. import net.sf.l2j.gameserver.model.L2DropData;
  30. import net.sf.l2j.gameserver.model.L2MinionData;
  31. import net.sf.l2j.gameserver.model.L2Skill;
  32. import net.sf.l2j.gameserver.model.base.ClassId;
  33. import net.sf.l2j.gameserver.skills.Stats;
  34. import net.sf.l2j.gameserver.templates.L2NpcTemplate;
  35. import net.sf.l2j.gameserver.templates.StatsSet;
  36. /**
  37. * This class ...
  38. *
  39. * @version $Revision: 1.8.2.6.2.9 $ $Date: 2005/04/06 16:13:25 $
  40. */
  41. public class NpcTable
  42. {
  43. private static Logger _log = Logger.getLogger(NpcTable.class.getName());
  44. private static NpcTable _instance;
  45. private Map<Integer, L2NpcTemplate> _npcs;
  46. private boolean _initialized = false;
  47. public static NpcTable getInstance()
  48. {
  49. if (_instance == null)
  50. _instance = new NpcTable();
  51. return _instance;
  52. }
  53. private NpcTable()
  54. {
  55. _npcs = new FastMap<Integer, L2NpcTemplate>();
  56. restoreNpcData();
  57. }
  58. private void restoreNpcData()
  59. {
  60. java.sql.Connection con = null;
  61. try
  62. {
  63. try
  64. {
  65. con = L2DatabaseFactory.getInstance().getConnection();
  66. PreparedStatement statement;
  67. statement = con.prepareStatement("SELECT "
  68. + L2DatabaseFactory.getInstance().safetyString(new String[]
  69. {
  70. "id", "idTemplate", "name", "serverSideName", "title", "serverSideTitle", "class", "collision_radius", "collision_height", "level", "sex", "type", "attackrange", "hp", "mp", "hpreg", "mpreg", "str", "con",
  71. "dex", "int", "wit", "men", "exp", "sp", "patk", "pdef", "matk", "mdef", "atkspd", "aggro", "matkspd", "rhand", "lhand", "armor", "walkspd", "runspd", "faction_id", "faction_range", "isUndead", "absorb_level",
  72. "absorb_type", "ss", "bss", "ss_rate", "AI", "drop_herbs"
  73. }) + " FROM npc");
  74. ResultSet npcdata = statement.executeQuery();
  75. fillNpcTable(npcdata);
  76. npcdata.close();
  77. statement.close();
  78. }
  79. catch (Exception e)
  80. {
  81. _log.log(Level.SEVERE, "NPCTable: Error creating NPC table.", e);
  82. }
  83. if (Config.CUSTOM_NPC_TABLE)
  84. {
  85. try
  86. {
  87. con = L2DatabaseFactory.getInstance().getConnection();
  88. PreparedStatement statement;
  89. statement = con.prepareStatement("SELECT "
  90. + L2DatabaseFactory.getInstance().safetyString(new String[]
  91. {
  92. "id", "idTemplate", "name", "serverSideName", "title", "serverSideTitle", "class", "collision_radius", "collision_height", "level", "sex", "type", "attackrange", "hp", "mp", "hpreg", "mpreg", "str", "con",
  93. "dex", "int", "wit", "men", "exp", "sp", "patk", "pdef", "matk", "mdef", "atkspd", "aggro", "matkspd", "rhand", "lhand", "armor", "walkspd", "runspd", "faction_id", "faction_range", "isUndead",
  94. "absorb_level", "absorb_type", "ss", "bss", "ss_rate", "AI", "drop_herbs"
  95. }) + " FROM custom_npc");
  96. ResultSet npcdata = statement.executeQuery();
  97. fillNpcTable(npcdata);
  98. npcdata.close();
  99. statement.close();
  100. }
  101. catch (Exception e)
  102. {
  103. _log.log(Level.SEVERE, "NPCTable: Error creating custom NPC table.", e);
  104. }
  105. }
  106. try
  107. {
  108. con = L2DatabaseFactory.getInstance().getConnection();
  109. PreparedStatement statement = con.prepareStatement("SELECT npcid, skillid, level FROM npcskills");
  110. ResultSet npcskills = statement.executeQuery();
  111. L2NpcTemplate npcDat = null;
  112. L2Skill npcSkill = null;
  113. while (npcskills.next())
  114. {
  115. int mobId = npcskills.getInt("npcid");
  116. npcDat = _npcs.get(mobId);
  117. if (npcDat == null)
  118. continue;
  119. int skillId = npcskills.getInt("skillid");
  120. int level = npcskills.getInt("level");
  121. if (npcDat.race == null && skillId == 4416)
  122. {
  123. npcDat.setRace(level);
  124. continue;
  125. }
  126. npcSkill = SkillTable.getInstance().getInfo(skillId, level);
  127. if (npcSkill == null)
  128. continue;
  129. npcDat.addSkill(npcSkill);
  130. }
  131. npcskills.close();
  132. statement.close();
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.SEVERE, "NPCTable: Error reading NPC skills table.", e);
  137. }
  138. try
  139. {
  140. PreparedStatement statement2 = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
  141. {
  142. "mobId", "itemId", "min", "max", "category", "chance"
  143. }) + " FROM droplist ORDER BY mobId, chance DESC");
  144. ResultSet dropData = statement2.executeQuery();
  145. L2DropData dropDat = null;
  146. L2NpcTemplate npcDat = null;
  147. while (dropData.next())
  148. {
  149. int mobId = dropData.getInt("mobId");
  150. npcDat = _npcs.get(mobId);
  151. if (npcDat == null)
  152. {
  153. _log.warning("NPCTable: Drop data for undefined NPC. npcId: " + mobId);
  154. continue;
  155. }
  156. dropDat = new L2DropData();
  157. dropDat.setItemId(dropData.getInt("itemId"));
  158. dropDat.setMinDrop(dropData.getInt("min"));
  159. dropDat.setMaxDrop(dropData.getInt("max"));
  160. dropDat.setChance(dropData.getInt("chance"));
  161. int category = dropData.getInt("category");
  162. npcDat.addDropData(dropDat, category);
  163. }
  164. dropData.close();
  165. statement2.close();
  166. }
  167. catch (Exception e)
  168. {
  169. _log.log(Level.SEVERE, "NPCTable: Error reading NPC dropdata. ", e);
  170. }
  171. if (Config.CUSTOM_DROPLIST_TABLE)
  172. {
  173. try
  174. {
  175. PreparedStatement statement2 = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
  176. {
  177. "mobId", "itemId", "min", "max", "category", "chance"
  178. }) + " FROM custom_droplist ORDER BY mobId, chance DESC");
  179. ResultSet dropData = statement2.executeQuery();
  180. L2DropData dropDat = null;
  181. L2NpcTemplate npcDat = null;
  182. int cCount = 0;
  183. while (dropData.next())
  184. {
  185. int mobId = dropData.getInt("mobId");
  186. npcDat = _npcs.get(mobId);
  187. if (npcDat == null)
  188. {
  189. _log.warning("NPCTable: CUSTOM DROPLIST: Drop data for undefined NPC. npcId: " + mobId);
  190. continue;
  191. }
  192. dropDat = new L2DropData();
  193. dropDat.setItemId(dropData.getInt("itemId"));
  194. dropDat.setMinDrop(dropData.getInt("min"));
  195. dropDat.setMaxDrop(dropData.getInt("max"));
  196. dropDat.setChance(dropData.getInt("chance"));
  197. int category = dropData.getInt("category");
  198. npcDat.addDropData(dropDat, category);
  199. cCount++;
  200. }
  201. dropData.close();
  202. statement2.close();
  203. _log.info("CustomDropList: Added " + cCount + " custom droplist.");
  204. }
  205. catch (Exception e)
  206. {
  207. _log.log(Level.SEVERE, "NPCTable: Error reading NPC custom dropdata.", e);
  208. }
  209. }
  210. try
  211. {
  212. PreparedStatement statement3 = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
  213. {
  214. "npc_id", "class_id"
  215. }) + " FROM skill_learn");
  216. ResultSet learndata = statement3.executeQuery();
  217. while (learndata.next())
  218. {
  219. int npcId = learndata.getInt("npc_id");
  220. int classId = learndata.getInt("class_id");
  221. L2NpcTemplate npc = getTemplate(npcId);
  222. if (npc == null)
  223. {
  224. _log.warning("NPCTable: Error getting NPC template ID " + npcId + " while trying to load skill trainer data.");
  225. continue;
  226. }
  227. npc.addTeachInfo(ClassId.values()[classId]);
  228. }
  229. learndata.close();
  230. statement3.close();
  231. }
  232. catch (Exception e)
  233. {
  234. _log.log(Level.SEVERE, "NPCTable: Error reading NPC trainer data.", e);
  235. }
  236. try
  237. {
  238. PreparedStatement statement4 = con.prepareStatement("SELECT " + L2DatabaseFactory.getInstance().safetyString(new String[]
  239. {
  240. "boss_id", "minion_id", "amount_min", "amount_max"
  241. }) + " FROM minions");
  242. ResultSet minionData = statement4.executeQuery();
  243. L2MinionData minionDat = null;
  244. L2NpcTemplate npcDat = null;
  245. int cnt = 0;
  246. while (minionData.next())
  247. {
  248. int raidId = minionData.getInt("boss_id");
  249. npcDat = _npcs.get(raidId);
  250. if (npcDat == null)
  251. {
  252. _log.warning("Minion references undefined boss NPC. Boss NpcId: " + raidId);
  253. continue;
  254. }
  255. minionDat = new L2MinionData();
  256. minionDat.setMinionId(minionData.getInt("minion_id"));
  257. minionDat.setAmountMin(minionData.getInt("amount_min"));
  258. minionDat.setAmountMax(minionData.getInt("amount_max"));
  259. npcDat.addRaidData(minionDat);
  260. cnt++;
  261. }
  262. minionData.close();
  263. statement4.close();
  264. _log.config("NpcTable: Loaded " + cnt + " Minions.");
  265. }
  266. catch (Exception e)
  267. {
  268. _log.log(Level.SEVERE, "NPCTable: Error loading minion data.", e);
  269. }
  270. }
  271. finally
  272. {
  273. try
  274. {
  275. con.close();
  276. }
  277. catch (SQLException e)
  278. {
  279. // nothing
  280. }
  281. }
  282. _initialized = true;
  283. }
  284. private void fillNpcTable(ResultSet NpcData) throws Exception
  285. {
  286. while (NpcData.next())
  287. {
  288. StatsSet npcDat = new StatsSet();
  289. int id = NpcData.getInt("id");
  290. if (Config.ASSERT)
  291. assert id < 1000000;
  292. npcDat.set("npcId", id);
  293. npcDat.set("idTemplate", NpcData.getInt("idTemplate"));
  294. int level = NpcData.getInt("level");
  295. npcDat.set("level", level);
  296. npcDat.set("jClass", NpcData.getString("class"));
  297. npcDat.set("baseShldDef", 0);
  298. npcDat.set("baseShldRate", 0);
  299. npcDat.set("baseCritRate", 38);
  300. npcDat.set("name", NpcData.getString("name"));
  301. npcDat.set("serverSideName", NpcData.getBoolean("serverSideName"));
  302. //npcDat.set("name", "");
  303. npcDat.set("title", NpcData.getString("title"));
  304. npcDat.set("serverSideTitle", NpcData.getBoolean("serverSideTitle"));
  305. npcDat.set("collision_radius", NpcData.getDouble("collision_radius"));
  306. npcDat.set("collision_height", NpcData.getDouble("collision_height"));
  307. npcDat.set("sex", NpcData.getString("sex"));
  308. npcDat.set("type", NpcData.getString("type"));
  309. npcDat.set("baseAtkRange", NpcData.getInt("attackrange"));
  310. npcDat.set("rewardExp", NpcData.getInt("exp"));
  311. npcDat.set("rewardSp", NpcData.getInt("sp"));
  312. npcDat.set("basePAtkSpd", NpcData.getInt("atkspd"));
  313. npcDat.set("baseMAtkSpd", NpcData.getInt("matkspd"));
  314. npcDat.set("aggroRange", NpcData.getInt("aggro"));
  315. npcDat.set("rhand", NpcData.getInt("rhand"));
  316. npcDat.set("lhand", NpcData.getInt("lhand"));
  317. npcDat.set("armor", NpcData.getInt("armor"));
  318. npcDat.set("baseWalkSpd", NpcData.getInt("walkspd"));
  319. npcDat.set("baseRunSpd", NpcData.getInt("runspd"));
  320. // constants, until we have stats in DB
  321. npcDat.set("baseSTR", NpcData.getInt("str"));
  322. npcDat.set("baseCON", NpcData.getInt("con"));
  323. npcDat.set("baseDEX", NpcData.getInt("dex"));
  324. npcDat.set("baseINT", NpcData.getInt("int"));
  325. npcDat.set("baseWIT", NpcData.getInt("wit"));
  326. npcDat.set("baseMEN", NpcData.getInt("men"));
  327. npcDat.set("baseHpMax", NpcData.getInt("hp"));
  328. npcDat.set("baseCpMax", 0);
  329. npcDat.set("baseMpMax", NpcData.getInt("mp"));
  330. npcDat.set("baseHpReg", NpcData.getFloat("hpreg") > 0 ? NpcData.getFloat("hpreg") : 1.5 + ((level - 1) / 10.0));
  331. npcDat.set("baseMpReg", NpcData.getFloat("mpreg") > 0 ? NpcData.getFloat("mpreg") : 0.9 + 0.3 * ((level - 1) / 10.0));
  332. npcDat.set("basePAtk", NpcData.getInt("patk"));
  333. npcDat.set("basePDef", NpcData.getInt("pdef"));
  334. npcDat.set("baseMAtk", NpcData.getInt("matk"));
  335. npcDat.set("baseMDef", NpcData.getInt("mdef"));
  336. npcDat.set("factionId", NpcData.getString("faction_id"));
  337. npcDat.set("factionRange", NpcData.getInt("faction_range"));
  338. npcDat.set("isUndead", NpcData.getString("isUndead"));
  339. npcDat.set("absorb_level", NpcData.getString("absorb_level"));
  340. npcDat.set("absorb_type", NpcData.getString("absorb_type"));
  341. npcDat.set("ss", NpcData.getInt("ss"));
  342. npcDat.set("bss", NpcData.getInt("bss"));
  343. npcDat.set("ssRate", NpcData.getInt("ss_rate"));
  344. npcDat.set("AI", NpcData.getString("AI"));
  345. npcDat.set("drop_herbs", Boolean.valueOf(NpcData.getString("drop_herbs")));
  346. L2NpcTemplate template = new L2NpcTemplate(npcDat);
  347. template.addVulnerability(Stats.BOW_WPN_VULN, 1);
  348. template.addVulnerability(Stats.CROSSBOW_WPN_VULN, 1);
  349. template.addVulnerability(Stats.BLUNT_WPN_VULN, 1);
  350. template.addVulnerability(Stats.DAGGER_WPN_VULN, 1);
  351. _npcs.put(id, template);
  352. }
  353. _log.config("NpcTable: Loaded " + _npcs.size() + " Npc Templates.");
  354. }
  355. public void reloadNpc(int id)
  356. {
  357. java.sql.Connection con = null;
  358. try
  359. {
  360. // save a copy of the old data
  361. L2NpcTemplate old = getTemplate(id);
  362. Map<Integer, L2Skill> skills = new FastMap<Integer, L2Skill>();
  363. if (old.getSkills() != null)
  364. skills.putAll(old.getSkills());
  365. FastList<L2DropCategory> categories = new FastList<L2DropCategory>();
  366. if (old.getDropData() != null)
  367. categories.addAll(old.getDropData());
  368. ClassId[] classIds = null;
  369. if (old.getTeachInfo() != null)
  370. classIds = old.getTeachInfo().clone();
  371. List<L2MinionData> minions = new FastList<L2MinionData>();
  372. if (old.getMinionData() != null)
  373. minions.addAll(old.getMinionData());
  374. // reload the NPC base data
  375. con = L2DatabaseFactory.getInstance().getConnection();
  376. PreparedStatement st = con.prepareStatement("SELECT "
  377. + L2DatabaseFactory.getInstance().safetyString(new String[]
  378. {
  379. "id", "idTemplate", "name", "serverSideName", "title", "serverSideTitle", "class", "collision_radius", "collision_height", "level", "sex", "type", "attackrange", "hp", "mp", "hpreg", "mpreg", "str", "con", "dex",
  380. "int", "wit", "men", "exp", "sp", "patk", "pdef", "matk", "mdef", "atkspd", "aggro", "matkspd", "rhand", "lhand", "armor", "walkspd", "runspd", "faction_id", "faction_range", "isUndead", "absorb_level",
  381. "absorb_type", "ss", "bss", "ss_rate", "AI", "drop_herbs"
  382. }) + " FROM npc WHERE id=?");
  383. st.setInt(1, id);
  384. ResultSet rs = st.executeQuery();
  385. fillNpcTable(rs);
  386. rs.close();
  387. st.close();
  388. // restore additional data from saved copy
  389. L2NpcTemplate created = getTemplate(id);
  390. for (L2Skill skill : skills.values())
  391. created.addSkill(skill);
  392. if (classIds != null)
  393. for (ClassId classId : classIds)
  394. created.addTeachInfo(classId);
  395. for (L2MinionData minion : minions)
  396. created.addRaidData(minion);
  397. }
  398. catch (Exception e)
  399. {
  400. _log.warning("NPCTable: Could not reload data for NPC " + id + ": " + e);
  401. }
  402. finally
  403. {
  404. try
  405. {
  406. con.close();
  407. }
  408. catch (Exception e)
  409. {
  410. }
  411. }
  412. }
  413. // just wrapper
  414. public void reloadAllNpc()
  415. {
  416. restoreNpcData();
  417. }
  418. public void saveNpc(StatsSet npc)
  419. {
  420. java.sql.Connection con = null;
  421. String query = "";
  422. try
  423. {
  424. con = L2DatabaseFactory.getInstance().getConnection();
  425. Map<String, Object> set = npc.getSet();
  426. String name = "";
  427. String values = "";
  428. for (Object obj : set.keySet())
  429. {
  430. name = (String) obj;
  431. if (!name.equalsIgnoreCase("npcId"))
  432. {
  433. if (values != "")
  434. values += ", ";
  435. values += name + " = '" + set.get(name) + "'";
  436. }
  437. }
  438. query = "UPDATE npc SET " + values + " WHERE id = ?";
  439. PreparedStatement statement = con.prepareStatement(query);
  440. statement.setInt(1, npc.getInteger("npcId"));
  441. statement.execute();
  442. statement.close();
  443. }
  444. catch (Exception e)
  445. {
  446. _log.warning("NPCTable: Could not store new NPC data in database: " + e);
  447. }
  448. finally
  449. {
  450. try
  451. {
  452. con.close();
  453. }
  454. catch (Exception e)
  455. {
  456. }
  457. }
  458. }
  459. public boolean isInitialized()
  460. {
  461. return _initialized;
  462. }
  463. public void replaceTemplate(L2NpcTemplate npc)
  464. {
  465. _npcs.put(npc.npcId, npc);
  466. }
  467. public L2NpcTemplate getTemplate(int id)
  468. {
  469. return _npcs.get(id);
  470. }
  471. public L2NpcTemplate getTemplateByName(String name)
  472. {
  473. for (L2NpcTemplate npcTemplate : _npcs.values())
  474. if (npcTemplate.name.equalsIgnoreCase(name))
  475. return npcTemplate;
  476. return null;
  477. }
  478. public L2NpcTemplate[] getAllOfLevel(int lvl)
  479. {
  480. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  481. for (L2NpcTemplate t : _npcs.values())
  482. if (t.level == lvl)
  483. list.add(t);
  484. return list.toArray(new L2NpcTemplate[list.size()]);
  485. }
  486. public L2NpcTemplate[] getAllMonstersOfLevel(int lvl)
  487. {
  488. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  489. for (L2NpcTemplate t : _npcs.values())
  490. if (t.level == lvl && "L2Monster".equals(t.type))
  491. list.add(t);
  492. return list.toArray(new L2NpcTemplate[list.size()]);
  493. }
  494. public L2NpcTemplate[] getAllNpcStartingWith(String letter)
  495. {
  496. List<L2NpcTemplate> list = new FastList<L2NpcTemplate>();
  497. for (L2NpcTemplate t : _npcs.values())
  498. if (t.name.startsWith(letter) && "L2Npc".equals(t.type))
  499. list.add(t);
  500. return list.toArray(new L2NpcTemplate[list.size()]);
  501. }
  502. /**
  503. * @param classType
  504. * @return
  505. */
  506. public Set<Integer> getAllNpcOfClassType(String classType)
  507. {
  508. return null;
  509. }
  510. /**
  511. * @param class1
  512. * @return
  513. */
  514. public Set<Integer> getAllNpcOfL2jClass(Class<?> clazz)
  515. {
  516. return null;
  517. }
  518. /**
  519. * @param aiType
  520. * @return
  521. */
  522. public Set<Integer> getAllNpcOfAiType(String aiType)
  523. {
  524. return null;
  525. }
  526. }