DayNightSpawnManager.java 6.1 KB

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