DayNightSpawnManager.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 com.l2jserver.gameserver.instancemanager;
  16. import java.util.Map;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.GameTimeController;
  21. import com.l2jserver.gameserver.model.L2Spawn;
  22. import com.l2jserver.gameserver.model.actor.L2Npc;
  23. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  24. import javolution.util.FastMap;
  25. /**
  26. * This class ...
  27. *
  28. * @version $Revision: $ $Date: $
  29. * @author godson
  30. */
  31. public class DayNightSpawnManager
  32. {
  33. private static Logger _log = Logger.getLogger(DayNightSpawnManager.class.getName());
  34. private static Map<L2Spawn, L2Npc> _dayCreatures;
  35. private static Map<L2Spawn, L2Npc> _nightCreatures;
  36. private static Map<L2Spawn, L2RaidBossInstance> _bosses;
  37. //private static int _currentState; // 0 = Day, 1 = Night
  38. public static DayNightSpawnManager getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. private DayNightSpawnManager()
  43. {
  44. _dayCreatures = new FastMap<L2Spawn, L2Npc>();
  45. _nightCreatures = new FastMap<L2Spawn, L2Npc>();
  46. _bosses = new FastMap<L2Spawn, L2RaidBossInstance>();
  47. _log.info("DayNightSpawnManager: Day/Night handler initialized");
  48. }
  49. public void addDayCreature(L2Spawn spawnDat)
  50. {
  51. if (_dayCreatures.containsKey(spawnDat))
  52. {
  53. _log.warning("DayNightSpawnManager: Spawn already added into day map");
  54. return;
  55. }
  56. else
  57. _dayCreatures.put(spawnDat, null);
  58. }
  59. public void addNightCreature(L2Spawn spawnDat)
  60. {
  61. if (_nightCreatures.containsKey(spawnDat))
  62. {
  63. _log.warning("DayNightSpawnManager: Spawn already added into night map");
  64. return;
  65. }
  66. else
  67. _nightCreatures.put(spawnDat, null);
  68. }
  69. /*
  70. * Spawn Day Creatures, and Unspawn Night Creatures
  71. */
  72. public void spawnDayCreatures()
  73. {
  74. spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
  75. }
  76. /*
  77. * Spawn Night Creatures, and Unspawn Day Creatures
  78. */
  79. public void spawnNightCreatures()
  80. {
  81. spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
  82. }
  83. /*
  84. * Manage Spawn/Respawn
  85. * Arg 1 : Map with L2NpcInstance must be unspawned
  86. * Arg 2 : Map with L2NpcInstance must be spawned
  87. * Arg 3 : String for log info for unspawned L2NpcInstance
  88. * Arg 4 : String for log info for spawned L2NpcInstance
  89. */
  90. private void spawnCreatures(Map<L2Spawn, L2Npc> UnSpawnCreatures, Map<L2Spawn, L2Npc> SpawnCreatures, String UnspawnLogInfo,
  91. String SpawnLogInfo)
  92. {
  93. try
  94. {
  95. if (!UnSpawnCreatures.isEmpty())
  96. {
  97. int i = 0;
  98. for (L2Npc dayCreature : UnSpawnCreatures.values())
  99. {
  100. if (dayCreature == null)
  101. continue;
  102. dayCreature.getSpawn().stopRespawn();
  103. dayCreature.deleteMe();
  104. i++;
  105. }
  106. if (Config.DEBUG)
  107. _log.info("DayNightSpawnManager: Deleted " + i + " " + UnspawnLogInfo + " creatures");
  108. }
  109. int i = 0;
  110. L2Npc creature = null;
  111. for (L2Spawn spawnDat : SpawnCreatures.keySet())
  112. {
  113. if (SpawnCreatures.get(spawnDat) == null)
  114. {
  115. creature = spawnDat.doSpawn();
  116. if (creature == null)
  117. continue;
  118. SpawnCreatures.remove(spawnDat);
  119. SpawnCreatures.put(spawnDat, creature);
  120. creature.setCurrentHp(creature.getMaxHp());
  121. creature.setCurrentMp(creature.getMaxMp());
  122. creature.getSpawn().startRespawn();
  123. if (creature.isDecayed())
  124. creature.setDecayed(false);
  125. if (creature.isDead())
  126. creature.doRevive();
  127. }
  128. else
  129. {
  130. creature = SpawnCreatures.get(spawnDat);
  131. if (creature == null)
  132. continue;
  133. creature.getSpawn().startRespawn();
  134. if (creature.isDecayed())
  135. creature.setDecayed(false);
  136. if (creature.isDead())
  137. creature.doRevive();
  138. creature.setCurrentHp(creature.getMaxHp());
  139. creature.setCurrentMp(creature.getMaxMp());
  140. creature.spawnMe();
  141. }
  142. i++;
  143. }
  144. if (Config.DEBUG)
  145. _log.info("DayNightSpawnManager: Spawning " + i + " " + SpawnLogInfo + " creatures");
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, "Error while spawning creatures: " + e.getMessage(), e);
  150. }
  151. }
  152. private void changeMode(int mode)
  153. {
  154. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty())
  155. return;
  156. switch (mode)
  157. {
  158. case 0:
  159. spawnDayCreatures();
  160. specialNightBoss(0);
  161. break;
  162. case 1:
  163. spawnNightCreatures();
  164. specialNightBoss(1);
  165. break;
  166. default:
  167. _log.warning("DayNightSpawnManager: Wrong mode sent");
  168. break;
  169. }
  170. }
  171. public void notifyChangeMode()
  172. {
  173. try
  174. {
  175. if (GameTimeController.getInstance().isNowNight())
  176. changeMode(1);
  177. else
  178. changeMode(0);
  179. }
  180. catch (Exception e)
  181. {
  182. _log.log(Level.WARNING, "Error while notifyChangeMode(): " + e.getMessage(), e);
  183. }
  184. }
  185. public void cleanUp()
  186. {
  187. _nightCreatures.clear();
  188. _dayCreatures.clear();
  189. _bosses.clear();
  190. }
  191. private void specialNightBoss(int mode)
  192. {
  193. try
  194. {
  195. for (L2Spawn spawn : _bosses.keySet())
  196. {
  197. L2RaidBossInstance boss = _bosses.get(spawn);
  198. if (boss == null && mode == 1)
  199. {
  200. boss = (L2RaidBossInstance) spawn.doSpawn();
  201. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  202. _bosses.remove(spawn);
  203. _bosses.put(spawn, boss);
  204. continue;
  205. }
  206. if (boss == null && mode == 0)
  207. continue;
  208. if (boss.getNpcId() == 25328 && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE))
  209. handleHellmans(boss, mode);
  210. return;
  211. }
  212. }
  213. catch (Exception e)
  214. {
  215. _log.log(Level.WARNING, "Error while specialNoghtBoss(): " + e.getMessage(), e);
  216. }
  217. }
  218. private void handleHellmans(L2RaidBossInstance boss, int mode)
  219. {
  220. switch (mode)
  221. {
  222. case 0:
  223. boss.deleteMe();
  224. _log.info("DayNightSpawnManager: Deleting Hellman raidboss");
  225. break;
  226. case 1:
  227. boss.spawnMe();
  228. _log.info("DayNightSpawnManager: Spawning Hellman raidboss");
  229. break;
  230. }
  231. }
  232. public L2RaidBossInstance handleBoss(L2Spawn spawnDat)
  233. {
  234. if (_bosses.containsKey(spawnDat))
  235. return _bosses.get(spawnDat);
  236. if (GameTimeController.getInstance().isNowNight())
  237. {
  238. L2RaidBossInstance raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  239. _bosses.put(spawnDat, raidboss);
  240. return raidboss;
  241. }
  242. else
  243. _bosses.put(spawnDat, null);
  244. return null;
  245. }
  246. @SuppressWarnings("synthetic-access")
  247. private static class SingletonHolder
  248. {
  249. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  250. }
  251. }