DayNightSpawnManager.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 List<L2Spawn> _dayCreatures;
  33. private List<L2Spawn> _nightCreatures;
  34. private 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,
  76. String SpawnLogInfo)
  77. {
  78. try
  79. {
  80. if (!unSpawnCreatures.isEmpty())
  81. {
  82. int i = 0;
  83. for (L2Spawn spawn: unSpawnCreatures)
  84. {
  85. if (spawn == null)
  86. continue;
  87. spawn.stopRespawn();
  88. L2Npc last = spawn.getLastSpawn();
  89. if (last != null)
  90. {
  91. last.deleteMe();
  92. i++;
  93. }
  94. }
  95. _log.info("DayNightSpawnManager: Removed " + i + " " + UnspawnLogInfo + " creatures");
  96. }
  97. int i = 0;
  98. for (L2Spawn spawnDat : spawnCreatures)
  99. {
  100. if (spawnDat == null)
  101. continue;
  102. spawnDat.startRespawn();
  103. spawnDat.doSpawn();
  104. i++;
  105. }
  106. _log.info("DayNightSpawnManager: Spawned " + i + " " + SpawnLogInfo + " creatures");
  107. }
  108. catch (Exception e)
  109. {
  110. _log.log(Level.WARNING, "Error while spawning creatures: " + e.getMessage(), e);
  111. }
  112. }
  113. private void changeMode(int mode)
  114. {
  115. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty())
  116. return;
  117. switch (mode)
  118. {
  119. case 0:
  120. spawnDayCreatures();
  121. specialNightBoss(0);
  122. break;
  123. case 1:
  124. spawnNightCreatures();
  125. specialNightBoss(1);
  126. break;
  127. default:
  128. _log.warning("DayNightSpawnManager: Wrong mode sent");
  129. break;
  130. }
  131. }
  132. public DayNightSpawnManager trim()
  133. {
  134. ((ArrayList<?>)_nightCreatures).trimToSize();
  135. ((ArrayList<?>)_dayCreatures).trimToSize();
  136. return this;
  137. }
  138. public void notifyChangeMode()
  139. {
  140. try
  141. {
  142. if (GameTimeController.getInstance().isNowNight())
  143. changeMode(1);
  144. else
  145. changeMode(0);
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, "Error while notifyChangeMode(): " + e.getMessage(), e);
  150. }
  151. }
  152. public void cleanUp()
  153. {
  154. _nightCreatures.clear();
  155. _dayCreatures.clear();
  156. _bosses.clear();
  157. }
  158. private void specialNightBoss(int mode)
  159. {
  160. try
  161. {
  162. L2RaidBossInstance boss;
  163. for (L2Spawn spawn : _bosses.keySet())
  164. {
  165. boss = _bosses.get(spawn);
  166. if ((boss == null) && (mode == 1))
  167. {
  168. boss = (L2RaidBossInstance) spawn.doSpawn();
  169. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  170. _bosses.remove(spawn);
  171. _bosses.put(spawn, boss);
  172. continue;
  173. }
  174. if ((boss == null) && (mode == 0))
  175. {
  176. continue;
  177. }
  178. if ((boss != null) && (boss.getNpcId() == 25328) && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE))
  179. handleHellmans(boss, mode);
  180. return;
  181. }
  182. }
  183. catch (Exception e)
  184. {
  185. _log.log(Level.WARNING, "Error while specialNoghtBoss(): " + e.getMessage(), e);
  186. }
  187. }
  188. private void handleHellmans(L2RaidBossInstance boss, int mode)
  189. {
  190. switch (mode)
  191. {
  192. case 0:
  193. boss.deleteMe();
  194. _log.info("DayNightSpawnManager: Deleting Hellman raidboss");
  195. break;
  196. case 1:
  197. boss.spawnMe();
  198. _log.info("DayNightSpawnManager: Spawning Hellman raidboss");
  199. break;
  200. }
  201. }
  202. public L2RaidBossInstance handleBoss(L2Spawn spawnDat)
  203. {
  204. if (_bosses.containsKey(spawnDat))
  205. return _bosses.get(spawnDat);
  206. if (GameTimeController.getInstance().isNowNight())
  207. {
  208. L2RaidBossInstance raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  209. _bosses.put(spawnDat, raidboss);
  210. return raidboss;
  211. }
  212. _bosses.put(spawnDat, null);
  213. return null;
  214. }
  215. private static class SingletonHolder
  216. {
  217. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  218. }
  219. }