SpawnTable.java
上传用户:zgh98012
上传日期:2017-06-22
资源大小:1073k
文件大小:7k
源码类别:

Java编程

开发平台:

Java

  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 l1j.server.server.datatables;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import l1j.server.Config;
  29. import l1j.server.L1DatabaseFactory;
  30. import l1j.server.server.model.L1Spawn;
  31. import l1j.server.server.model.Instance.L1PcInstance;
  32. import l1j.server.server.templates.L1Npc;
  33. import l1j.server.server.utils.NumberUtil;
  34. import l1j.server.server.utils.PerformanceTimer;
  35. import l1j.server.server.utils.SQLUtil;
  36. public class SpawnTable {
  37. private static l1j.eric.EricLogger _log =  l1j.eric.EricLogger.getLogger2(SpawnTable.class.getName());
  38. private static SpawnTable _instance;
  39. private Map<Integer, L1Spawn> _spawntable = new HashMap<Integer, L1Spawn>();
  40. private int _highestId;
  41. public static SpawnTable getInstance() {
  42. if (_instance == null) {
  43. _instance = new SpawnTable();
  44. }
  45. return _instance;
  46. }
  47. private SpawnTable() {
  48. PerformanceTimer timer = new PerformanceTimer();
  49. l1j.eric.gui.J_Main.getInstance().addConsolNoLR("》》正在載入 spawn mob 設定...");
  50. fillSpawnTable();
  51. _log.config("モンスター配置リスト " + _spawntable.size() + "件");
  52. l1j.eric.gui.J_Main.getInstance().addConsolPost("完成! " + timer.get() + " ms");
  53. }
  54. private void fillSpawnTable() {
  55. int spawnCount = 0;
  56. java.sql.Connection con = null;
  57. PreparedStatement pstm = null;
  58. ResultSet rs = null;
  59. try {
  60. con = L1DatabaseFactory.getInstance().getConnection();
  61. pstm = con.prepareStatement("SELECT * FROM spawnlist");
  62. rs = pstm.executeQuery();
  63. L1Spawn spawnDat;
  64. L1Npc template1;
  65. while (rs.next()) {
  66. if (Config.ALT_HALLOWEENIVENT == false) {
  67. int npcid = rs.getInt("id");
  68. if (npcid >= 26656 && npcid <= 26734) {
  69. continue;
  70. }
  71. }
  72. int npcTemplateId = rs.getInt("npc_templateid");
  73. template1 = NpcTable.getInstance().getTemplate(npcTemplateId);
  74. int count;
  75. if (template1 == null) {
  76. _log.warning("mob data for id:" + npcTemplateId
  77. + " missing in npc table");
  78. spawnDat = null;
  79. } else {
  80. if (rs.getInt("count") == 0) {
  81. continue;
  82. }
  83. double amount_rate = MapsTable.getInstance()
  84. .getMonsterAmount(rs.getShort("mapid"));
  85. count = calcCount(template1, rs.getInt("count"),
  86. amount_rate);
  87. if (count == 0) {
  88. continue;
  89. }
  90. spawnDat = new L1Spawn(template1);
  91. spawnDat.setId(rs.getInt("id"));
  92. spawnDat.setAmount(count);
  93. spawnDat.setGroupId(rs.getInt("group_id"));
  94. spawnDat.setLocX(rs.getInt("locx"));
  95. spawnDat.setLocY(rs.getInt("locy"));
  96. spawnDat.setRandomx(rs.getInt("randomx"));
  97. spawnDat.setRandomy(rs.getInt("randomy"));
  98. spawnDat.setLocX1(rs.getInt("locx1"));
  99. spawnDat.setLocY1(rs.getInt("locy1"));
  100. spawnDat.setLocX2(rs.getInt("locx2"));
  101. spawnDat.setLocY2(rs.getInt("locy2"));
  102. spawnDat.setHeading(rs.getInt("heading"));
  103. spawnDat.setMinRespawnDelay(rs.getInt("min_respawn_delay"));
  104. spawnDat.setMaxRespawnDelay(rs.getInt("max_respawn_delay"));
  105. spawnDat.setMapId(rs.getShort("mapid"));
  106. spawnDat.setRespawnScreen(rs.getBoolean("respawn_screen"));
  107. spawnDat
  108. .setMovementDistance(rs.getInt("movement_distance"));
  109. spawnDat.setRest(rs.getBoolean("rest"));
  110. spawnDat.setSpawnType(rs.getInt("near_spawn"));
  111. spawnDat.setTime(SpawnTimeTable.getInstance().get(
  112. spawnDat.getId()));
  113. spawnDat.setName(template1.get_name());
  114. if (count > 1 && spawnDat.getLocX1() == 0) {
  115. // 複数かつ固定spawnの場合は、個体数 * 6 の範囲spawnに変える。
  116. // ただし範囲が30を超えないようにする
  117. int range = Math.min(count * 6, 30);
  118. spawnDat.setLocX1(spawnDat.getLocX() - range);
  119. spawnDat.setLocY1(spawnDat.getLocY() - range);
  120. spawnDat.setLocX2(spawnDat.getLocX() + range);
  121. spawnDat.setLocY2(spawnDat.getLocY() + range);
  122. }
  123. // start the spawning
  124. spawnDat.init();
  125. spawnCount += spawnDat.getAmount();
  126. }
  127. _spawntable.put(new Integer(spawnDat.getId()), spawnDat);
  128. if (spawnDat.getId() > _highestId) {
  129. _highestId = spawnDat.getId();
  130. }
  131. }
  132. } catch (SQLException e) {
  133. _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
  134. } finally {
  135. SQLUtil.close(rs);
  136. SQLUtil.close(pstm);
  137. SQLUtil.close(con);
  138. }
  139. _log.fine("総モンスター数 " + spawnCount + "匹");
  140. }
  141. public L1Spawn getTemplate(int Id) {
  142. return _spawntable.get(new Integer(Id));
  143. }
  144. public void addNewSpawn(L1Spawn spawn) {
  145. _highestId++;
  146. spawn.setId(_highestId);
  147. _spawntable.put(new Integer(spawn.getId()), spawn);
  148. }
  149. public static void storeSpawn(L1PcInstance pc, L1Npc npc) {
  150. Connection con = null;
  151. PreparedStatement pstm = null;
  152. try {
  153. int count = 1;
  154. int randomXY = 12;
  155. int minRespawnDelay = 60;
  156. int maxRespawnDelay = 120;
  157. String note = npc.get_name();
  158. con = L1DatabaseFactory.getInstance().getConnection();
  159. pstm = con
  160. .prepareStatement("INSERT INTO spawnlist SET location=?,count=?,npc_templateid=?,group_id=?,locx=?,locy=?,randomx=?,randomy=?,heading=?,min_respawn_delay=?,max_respawn_delay=?,mapid=?");
  161. pstm.setString(1, note);
  162. pstm.setInt(2, count);
  163. pstm.setInt(3, npc.get_npcId());
  164. pstm.setInt(4, 0);
  165. pstm.setInt(5, pc.getX());
  166. pstm.setInt(6, pc.getY());
  167. pstm.setInt(7, randomXY);
  168. pstm.setInt(8, randomXY);
  169. pstm.setInt(9, pc.getHeading());
  170. pstm.setInt(10, minRespawnDelay);
  171. pstm.setInt(11, maxRespawnDelay);
  172. pstm.setInt(12, pc.getMapId());
  173. pstm.execute();
  174. } catch (Exception e) {
  175. NpcTable._log.log(Level.SEVERE, e.getLocalizedMessage(), e);
  176. } finally {
  177. SQLUtil.close(pstm);
  178. SQLUtil.close(con);
  179. }
  180. }
  181. private static int calcCount(L1Npc npc, int count, double rate) {
  182. if (rate == 0) {
  183. return 0;
  184. }
  185. if (rate == 1 || npc.isAmountFixed()) {
  186. return count;
  187. } else {
  188. return NumberUtil.randomRound((count * rate));
  189. }
  190. }
  191. }