/L2EmuProject-Game/src/main/java/net/l2emuproject/gameserver/manager/DayNightSpawnManager.java

http://l2emu.googlecode.com/ · Java · 210 lines · 156 code · 24 blank · 30 comment · 25 complexity · d91fee4e115173419232d4c446a619b7 MD5 · raw file

  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.l2emuproject.gameserver.manager;
  16. import javolution.util.FastMap;
  17. import net.l2emuproject.gameserver.system.time.GameTimeController;
  18. import net.l2emuproject.gameserver.world.object.L2Npc;
  19. import net.l2emuproject.gameserver.world.spawn.L2Spawn;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. /**
  23. * @author godson
  24. */
  25. public class DayNightSpawnManager
  26. {
  27. private final static Log _log = LogFactory.getLog(DayNightSpawnManager.class);
  28. private static final class SingletonHolder
  29. {
  30. private static final DayNightSpawnManager INSTANCE = new DayNightSpawnManager();
  31. }
  32. public static DayNightSpawnManager getInstance()
  33. {
  34. return SingletonHolder.INSTANCE;
  35. }
  36. private final FastMap<L2Spawn, L2Npc> _dayCreatures;
  37. private final FastMap<L2Spawn, L2Npc> _nightCreatures;
  38. private DayNightSpawnManager()
  39. {
  40. _dayCreatures = new FastMap<L2Spawn, L2Npc>();
  41. _nightCreatures = new FastMap<L2Spawn, L2Npc>();
  42. _log.info(getClass().getSimpleName() + " : Day/Night handler initialized.");
  43. }
  44. public void addDayCreature(L2Spawn spawnDat)
  45. {
  46. if (_dayCreatures.containsKey(spawnDat))
  47. {
  48. _log.warn("DayNightSpawnManager: Spawn already added into day map");
  49. return;
  50. }
  51. _dayCreatures.put(spawnDat, null);
  52. }
  53. public void addNightCreature(L2Spawn spawnDat)
  54. {
  55. if (_nightCreatures.containsKey(spawnDat))
  56. {
  57. _log.warn("DayNightSpawnManager: Spawn already added into night map");
  58. return;
  59. }
  60. _nightCreatures.put(spawnDat, null);
  61. }
  62. /*
  63. * Spawn Day Creatures, and Unspawn Night Creatures
  64. */
  65. public void spawnDayCreatures()
  66. {
  67. spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
  68. }
  69. /*
  70. * Spawn Night Creatures, and Unspawn Day Creatures
  71. */
  72. public void spawnNightCreatures()
  73. {
  74. spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
  75. }
  76. /*
  77. * Manage Spawn/Respawn
  78. * Arg 1 : Map with L2Npc must be unspawned
  79. * Arg 2 : Map with L2Npc must be spawned
  80. * Arg 3 : String for log info for unspawned L2Npc
  81. * Arg 4 : String for log info for spawned L2Npc
  82. */
  83. private void spawnCreatures(FastMap<L2Spawn, L2Npc> UnSpawnCreatures, FastMap<L2Spawn, L2Npc> SpawnCreatures, String UnspawnLogInfo,
  84. String SpawnLogInfo)
  85. {
  86. try
  87. {
  88. if (!UnSpawnCreatures.isEmpty())
  89. {
  90. int i = 0;
  91. for (L2Npc dayCreature : UnSpawnCreatures.values())
  92. {
  93. if (dayCreature == null)
  94. continue;
  95. dayCreature.getSpawn().stopRespawn();
  96. dayCreature.deleteMe();
  97. i++;
  98. }
  99. if (_log.isDebugEnabled())
  100. _log.info("DayNightSpawnManager: Deleted " + i + " " + UnspawnLogInfo + " creatures");
  101. }
  102. int i = 0;
  103. L2Npc creature = null;
  104. for (L2Spawn spawnDat : SpawnCreatures.keySet())
  105. {
  106. if (SpawnCreatures.get(spawnDat) == null)
  107. {
  108. creature = spawnDat.doSpawn();
  109. if (creature == null)
  110. continue;
  111. SpawnCreatures.remove(spawnDat);
  112. SpawnCreatures.put(spawnDat, creature);
  113. creature.getStatus().setCurrentHp(creature.getMaxHp());
  114. creature.getStatus().setCurrentMp(creature.getMaxMp());
  115. creature.getSpawn().startRespawn();
  116. if (creature.isDecayed())
  117. creature.setDecayed(false);
  118. if (creature.isDead())
  119. creature.doRevive();
  120. }
  121. else
  122. {
  123. creature = SpawnCreatures.get(spawnDat);
  124. if (creature == null)
  125. continue;
  126. creature.getSpawn().startRespawn();
  127. if (creature.isDecayed())
  128. creature.setDecayed(false);
  129. if (creature.isDead())
  130. creature.doRevive();
  131. creature.getStatus().setCurrentHp(creature.getMaxHp());
  132. creature.getStatus().setCurrentMp(creature.getMaxMp());
  133. creature.spawnMe();
  134. }
  135. i++;
  136. }
  137. if (_log.isDebugEnabled())
  138. _log.info("DayNightSpawnManager: Spawning " + i + " " + SpawnLogInfo + " creatures");
  139. }
  140. catch (Exception e)
  141. {
  142. _log.error(e.getMessage(), e);
  143. }
  144. }
  145. private void changeMode(int mode)
  146. {
  147. if (_nightCreatures.size() == 0 && _dayCreatures.size() == 0)
  148. return;
  149. switch (mode)
  150. {
  151. case 0:
  152. spawnDayCreatures();
  153. specialNightBoss(0);
  154. break;
  155. case 1:
  156. spawnNightCreatures();
  157. specialNightBoss(1);
  158. break;
  159. default:
  160. _log.warn("DayNightSpawnManager: Wrong mode sent");
  161. break;
  162. }
  163. }
  164. public void notifyChangeMode()
  165. {
  166. try
  167. {
  168. if (GameTimeController.getInstance().isNowNight())
  169. changeMode(1);
  170. else
  171. changeMode(0);
  172. }
  173. catch (Exception e)
  174. {
  175. _log.error(e.getMessage(), e);
  176. }
  177. }
  178. public void cleanUp()
  179. {
  180. _nightCreatures.clear();
  181. _dayCreatures.clear();
  182. }
  183. private void specialNightBoss(int mode)
  184. {
  185. }
  186. }