DayNightSpawnManager.java 7.0 KB

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