DayNightSpawnManager.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.gameserver.GameTimeController;
  27. import com.l2jserver.gameserver.model.L2Spawn;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  30. /**
  31. * @author godson
  32. */
  33. public class DayNightSpawnManager
  34. {
  35. private static Logger _log = Logger.getLogger(DayNightSpawnManager.class.getName());
  36. private final List<L2Spawn> _dayCreatures;
  37. private final List<L2Spawn> _nightCreatures;
  38. private final Map<L2Spawn, L2RaidBossInstance> _bosses;
  39. // private static int _currentState; // 0 = Day, 1 = Night
  40. public static DayNightSpawnManager getInstance()
  41. {
  42. return SingletonHolder._instance;
  43. }
  44. protected DayNightSpawnManager()
  45. {
  46. _dayCreatures = new ArrayList<>();
  47. _nightCreatures = new ArrayList<>();
  48. _bosses = new FastMap<>();
  49. }
  50. public void addDayCreature(L2Spawn spawnDat)
  51. {
  52. _dayCreatures.add(spawnDat);
  53. }
  54. public void addNightCreature(L2Spawn spawnDat)
  55. {
  56. _nightCreatures.add(spawnDat);
  57. }
  58. /**
  59. * Spawn Day Creatures, and Unspawn Night Creatures
  60. */
  61. public void spawnDayCreatures()
  62. {
  63. spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
  64. }
  65. /**
  66. * Spawn Night Creatures, and Unspawn Day Creatures
  67. */
  68. public void spawnNightCreatures()
  69. {
  70. spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
  71. }
  72. /**
  73. * Manage Spawn/Respawn
  74. * @param unSpawnCreatures List with spawns must be unspawned
  75. * @param spawnCreatures List with spawns must be spawned
  76. * @param UnspawnLogInfo String for log info for unspawned L2NpcInstance
  77. * @param SpawnLogInfo String for log info for spawned L2NpcInstance
  78. */
  79. private void spawnCreatures(List<L2Spawn> unSpawnCreatures, List<L2Spawn> spawnCreatures, String UnspawnLogInfo, String SpawnLogInfo)
  80. {
  81. try
  82. {
  83. if (!unSpawnCreatures.isEmpty())
  84. {
  85. int i = 0;
  86. for (L2Spawn spawn : unSpawnCreatures)
  87. {
  88. if (spawn == null)
  89. {
  90. continue;
  91. }
  92. spawn.stopRespawn();
  93. L2Npc last = spawn.getLastSpawn();
  94. if (last != null)
  95. {
  96. last.deleteMe();
  97. i++;
  98. }
  99. }
  100. _log.info("DayNightSpawnManager: Removed " + i + " " + UnspawnLogInfo + " creatures");
  101. }
  102. int i = 0;
  103. for (L2Spawn spawnDat : spawnCreatures)
  104. {
  105. if (spawnDat == null)
  106. {
  107. continue;
  108. }
  109. spawnDat.startRespawn();
  110. spawnDat.doSpawn();
  111. i++;
  112. }
  113. _log.info("DayNightSpawnManager: Spawned " + i + " " + SpawnLogInfo + " creatures");
  114. }
  115. catch (Exception e)
  116. {
  117. _log.log(Level.WARNING, "Error while spawning creatures: " + e.getMessage(), e);
  118. }
  119. }
  120. private void changeMode(int mode)
  121. {
  122. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty())
  123. {
  124. return;
  125. }
  126. switch (mode)
  127. {
  128. case 0:
  129. spawnDayCreatures();
  130. specialNightBoss(0);
  131. break;
  132. case 1:
  133. spawnNightCreatures();
  134. specialNightBoss(1);
  135. break;
  136. default:
  137. _log.warning("DayNightSpawnManager: Wrong mode sent");
  138. break;
  139. }
  140. }
  141. public DayNightSpawnManager trim()
  142. {
  143. ((ArrayList<?>) _nightCreatures).trimToSize();
  144. ((ArrayList<?>) _dayCreatures).trimToSize();
  145. return this;
  146. }
  147. public void notifyChangeMode()
  148. {
  149. try
  150. {
  151. if (GameTimeController.getInstance().isNight())
  152. {
  153. changeMode(1);
  154. }
  155. else
  156. {
  157. changeMode(0);
  158. }
  159. }
  160. catch (Exception e)
  161. {
  162. _log.log(Level.WARNING, "Error while notifyChangeMode(): " + e.getMessage(), e);
  163. }
  164. }
  165. public void cleanUp()
  166. {
  167. _nightCreatures.clear();
  168. _dayCreatures.clear();
  169. _bosses.clear();
  170. }
  171. private void specialNightBoss(int mode)
  172. {
  173. try
  174. {
  175. L2RaidBossInstance boss;
  176. for (L2Spawn spawn : _bosses.keySet())
  177. {
  178. boss = _bosses.get(spawn);
  179. if ((boss == null) && (mode == 1))
  180. {
  181. boss = (L2RaidBossInstance) spawn.doSpawn();
  182. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  183. _bosses.remove(spawn);
  184. _bosses.put(spawn, boss);
  185. continue;
  186. }
  187. if ((boss == null) && (mode == 0))
  188. {
  189. continue;
  190. }
  191. if ((boss != null) && (boss.getNpcId() == 25328) && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE))
  192. {
  193. handleHellmans(boss, mode);
  194. }
  195. return;
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. _log.log(Level.WARNING, "Error while specialNoghtBoss(): " + e.getMessage(), e);
  201. }
  202. }
  203. private void handleHellmans(L2RaidBossInstance boss, int mode)
  204. {
  205. switch (mode)
  206. {
  207. case 0:
  208. boss.deleteMe();
  209. _log.info(getClass().getSimpleName() + ": Deleting Hellman raidboss");
  210. break;
  211. case 1:
  212. if (!boss.isVisible())
  213. {
  214. boss.spawnMe();
  215. }
  216. _log.info(getClass().getSimpleName() + ": Spawning Hellman raidboss");
  217. break;
  218. }
  219. }
  220. public L2RaidBossInstance handleBoss(L2Spawn spawnDat)
  221. {
  222. if (_bosses.containsKey(spawnDat))
  223. {
  224. return _bosses.get(spawnDat);
  225. }
  226. if (GameTimeController.getInstance().isNight())
  227. {
  228. L2RaidBossInstance raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  229. _bosses.put(spawnDat, raidboss);
  230. return raidboss;
  231. }
  232. _bosses.put(spawnDat, null);
  233. return null;
  234. }
  235. private static class SingletonHolder
  236. {
  237. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  238. }
  239. }