/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
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.l2emuproject.gameserver.manager;
-
- import javolution.util.FastMap;
- import net.l2emuproject.gameserver.system.time.GameTimeController;
- import net.l2emuproject.gameserver.world.object.L2Npc;
- import net.l2emuproject.gameserver.world.spawn.L2Spawn;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- /**
- * @author godson
- */
- public class DayNightSpawnManager
- {
- private final static Log _log = LogFactory.getLog(DayNightSpawnManager.class);
-
- private static final class SingletonHolder
- {
- private static final DayNightSpawnManager INSTANCE = new DayNightSpawnManager();
- }
-
- public static DayNightSpawnManager getInstance()
- {
- return SingletonHolder.INSTANCE;
- }
-
- private final FastMap<L2Spawn, L2Npc> _dayCreatures;
- private final FastMap<L2Spawn, L2Npc> _nightCreatures;
-
- private DayNightSpawnManager()
- {
- _dayCreatures = new FastMap<L2Spawn, L2Npc>();
- _nightCreatures = new FastMap<L2Spawn, L2Npc>();
-
- _log.info(getClass().getSimpleName() + " : Day/Night handler initialized.");
- }
-
- public void addDayCreature(L2Spawn spawnDat)
- {
- if (_dayCreatures.containsKey(spawnDat))
- {
- _log.warn("DayNightSpawnManager: Spawn already added into day map");
- return;
- }
- _dayCreatures.put(spawnDat, null);
- }
-
- public void addNightCreature(L2Spawn spawnDat)
- {
- if (_nightCreatures.containsKey(spawnDat))
- {
- _log.warn("DayNightSpawnManager: Spawn already added into night map");
- return;
- }
- _nightCreatures.put(spawnDat, null);
- }
-
- /*
- * Spawn Day Creatures, and Unspawn Night Creatures
- */
- public void spawnDayCreatures()
- {
- spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
- }
-
- /*
- * Spawn Night Creatures, and Unspawn Day Creatures
- */
- public void spawnNightCreatures()
- {
- spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
- }
-
- /*
- * Manage Spawn/Respawn
- * Arg 1 : Map with L2Npc must be unspawned
- * Arg 2 : Map with L2Npc must be spawned
- * Arg 3 : String for log info for unspawned L2Npc
- * Arg 4 : String for log info for spawned L2Npc
- */
- private void spawnCreatures(FastMap<L2Spawn, L2Npc> UnSpawnCreatures, FastMap<L2Spawn, L2Npc> SpawnCreatures, String UnspawnLogInfo,
- String SpawnLogInfo)
- {
- try
- {
- if (!UnSpawnCreatures.isEmpty())
- {
- int i = 0;
- for (L2Npc dayCreature : UnSpawnCreatures.values())
- {
- if (dayCreature == null)
- continue;
-
- dayCreature.getSpawn().stopRespawn();
- dayCreature.deleteMe();
- i++;
- }
- if (_log.isDebugEnabled())
- _log.info("DayNightSpawnManager: Deleted " + i + " " + UnspawnLogInfo + " creatures");
- }
-
- int i = 0;
- L2Npc creature = null;
- for (L2Spawn spawnDat : SpawnCreatures.keySet())
- {
- if (SpawnCreatures.get(spawnDat) == null)
- {
- creature = spawnDat.doSpawn();
- if (creature == null)
- continue;
-
- SpawnCreatures.remove(spawnDat);
- SpawnCreatures.put(spawnDat, creature);
- creature.getStatus().setCurrentHp(creature.getMaxHp());
- creature.getStatus().setCurrentMp(creature.getMaxMp());
- creature.getSpawn().startRespawn();
- if (creature.isDecayed())
- creature.setDecayed(false);
- if (creature.isDead())
- creature.doRevive();
- }
- else
- {
- creature = SpawnCreatures.get(spawnDat);
- if (creature == null)
- continue;
-
- creature.getSpawn().startRespawn();
- if (creature.isDecayed())
- creature.setDecayed(false);
- if (creature.isDead())
- creature.doRevive();
- creature.getStatus().setCurrentHp(creature.getMaxHp());
- creature.getStatus().setCurrentMp(creature.getMaxMp());
- creature.spawnMe();
- }
-
- i++;
- }
- if (_log.isDebugEnabled())
- _log.info("DayNightSpawnManager: Spawning " + i + " " + SpawnLogInfo + " creatures");
- }
- catch (Exception e)
- {
- _log.error(e.getMessage(), e);
- }
- }
-
- private void changeMode(int mode)
- {
- if (_nightCreatures.size() == 0 && _dayCreatures.size() == 0)
- return;
-
- switch (mode)
- {
- case 0:
- spawnDayCreatures();
- specialNightBoss(0);
- break;
- case 1:
- spawnNightCreatures();
- specialNightBoss(1);
- break;
- default:
- _log.warn("DayNightSpawnManager: Wrong mode sent");
- break;
- }
- }
-
- public void notifyChangeMode()
- {
- try
- {
- if (GameTimeController.getInstance().isNowNight())
- changeMode(1);
- else
- changeMode(0);
- }
- catch (Exception e)
- {
- _log.error(e.getMessage(), e);
- }
- }
-
- public void cleanUp()
- {
- _nightCreatures.clear();
- _dayCreatures.clear();
- }
-
- private void specialNightBoss(int mode)
- {
-
- }
- }