DayNightSpawnManager.java 8.3 KB

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