|
@@ -18,280 +18,240 @@
|
|
|
*/
|
|
|
package com.l2jserver.gameserver;
|
|
|
|
|
|
-import java.util.concurrent.locks.ReentrantLock;
|
|
|
+import java.util.Calendar;
|
|
|
import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
+import javolution.util.FastMap;
|
|
|
+
|
|
|
import com.l2jserver.Config;
|
|
|
import com.l2jserver.gameserver.ai.CtrlEvent;
|
|
|
+import com.l2jserver.gameserver.ai.L2CharacterAI;
|
|
|
import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
|
|
|
import com.l2jserver.gameserver.model.actor.L2Character;
|
|
|
-
|
|
|
-import gnu.trove.map.hash.TIntObjectHashMap;
|
|
|
-import gnu.trove.procedure.TObjectProcedure;
|
|
|
+import com.l2jserver.util.StackTrace;
|
|
|
|
|
|
/**
|
|
|
- * Removed TimerThread watcher [DrHouse]<br>
|
|
|
- * One in-game day is 240 real minutes.
|
|
|
- * @version $Date: 2010/02/02 22:43:00 $
|
|
|
+ * @author Unknown, Forsaiken
|
|
|
*/
|
|
|
-public class GameTimeController
|
|
|
+public final class GameTimeController extends Thread
|
|
|
{
|
|
|
- protected static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
|
|
|
+ static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
|
|
|
|
|
|
public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
|
|
|
public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
|
|
|
+ public static final int IG_DAYS_PER_DAY = 6;
|
|
|
+ public static final int MILLIS_PER_IG_DAY = (1000 * 60 * 60 * 24) / IG_DAYS_PER_DAY;
|
|
|
+ public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000;
|
|
|
+ public static final int MINUTES_PER_IG_DAY = SECONDS_PER_IG_DAY / 60;
|
|
|
+ public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
|
|
|
+ public static final int TICKS_SUN_STATE_CHANGE = TICKS_PER_IG_DAY / 4;
|
|
|
|
|
|
- protected static int _gameTicks;
|
|
|
- protected static long _gameStartTime;
|
|
|
- protected static boolean _isNight = false;
|
|
|
- protected static boolean _interruptRequest = false;
|
|
|
-
|
|
|
- protected static final TIntObjectHashMap<L2Character> _movingObjects = new TIntObjectHashMap<>();
|
|
|
- private static final ReentrantLock _lock = new ReentrantLock();
|
|
|
+ private static GameTimeController _instance;
|
|
|
|
|
|
- protected static TimerThread _timer;
|
|
|
+ public static final void init()
|
|
|
+ {
|
|
|
+ _instance = new GameTimeController();
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Gets the single instance of GameTimeController.
|
|
|
- * @return single instance of GameTimeController
|
|
|
- */
|
|
|
- public static GameTimeController getInstance()
|
|
|
+ public static final GameTimeController getInstance()
|
|
|
{
|
|
|
- return SingletonHolder._instance;
|
|
|
+ return _instance;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Instantiates a new game time controller.
|
|
|
- */
|
|
|
- protected GameTimeController()
|
|
|
+ private final FastMap<Integer, L2Character> _movingObjects;
|
|
|
+ private final long _referenceTime;
|
|
|
+
|
|
|
+ private GameTimeController()
|
|
|
{
|
|
|
- _gameStartTime = System.currentTimeMillis() - 3600000; // offset so that the server starts a day begin
|
|
|
- _gameTicks = 3600000 / MILLIS_IN_TICK; // offset so that the server starts a day begin
|
|
|
+ super("GameTimeController");
|
|
|
+ super.setDaemon(true);
|
|
|
+ super.setPriority(MAX_PRIORITY);
|
|
|
|
|
|
- _timer = new TimerThread();
|
|
|
- _timer.start();
|
|
|
+ _movingObjects = new FastMap<Integer, L2Character>().shared();
|
|
|
|
|
|
- ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
|
|
|
+ final Calendar c = Calendar.getInstance();
|
|
|
+ c.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ c.set(Calendar.MINUTE, 0);
|
|
|
+ c.set(Calendar.SECOND, 0);
|
|
|
+ c.set(Calendar.MILLISECOND, 0);
|
|
|
+ _referenceTime = c.getTimeInMillis();
|
|
|
|
|
|
+ super.start();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Checks if is now night.
|
|
|
- * @return true, if is now night
|
|
|
- */
|
|
|
- public boolean isNowNight()
|
|
|
+ public final int getGameTime()
|
|
|
{
|
|
|
- return _isNight;
|
|
|
+ return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Gets the game time.
|
|
|
- * @return the game time
|
|
|
- */
|
|
|
- public int getGameTime()
|
|
|
+ public final int getGameHour()
|
|
|
+ {
|
|
|
+ return getGameTime() / 60;
|
|
|
+ }
|
|
|
+
|
|
|
+ public final int getGameMinute()
|
|
|
{
|
|
|
- return (_gameTicks / (TICKS_PER_SECOND * 10));
|
|
|
+ return getGameTime() % 60;
|
|
|
+ }
|
|
|
+
|
|
|
+ public final boolean isNight()
|
|
|
+ {
|
|
|
+ return getGameHour() < 6;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Gets the game ticks.
|
|
|
- * @return the game ticks
|
|
|
+ * The true GameTime tick. Directly taken from current time. This represents the tick of the time.
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public static int getGameTicks()
|
|
|
+ public final int getGameTicks()
|
|
|
{
|
|
|
- return _gameTicks;
|
|
|
+ return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Add a L2Character to movingObjects of GameTimeController.<br>
|
|
|
- * All characters in movement are identified in <b>movingObjects</b> of GameTimeController.
|
|
|
- * @param cha the character to add to movingObjects of GameTimeController
|
|
|
+ * Add a L2Character to movingObjects of GameTimeController.<BR>
|
|
|
+ * <BR>
|
|
|
+ * <B><U> Concept</U> :</B><BR>
|
|
|
+ * <BR>
|
|
|
+ * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
|
|
|
+ * <BR>
|
|
|
+ * @param cha The L2Character to add to movingObjects of GameTimeController
|
|
|
*/
|
|
|
- public void registerMovingObject(L2Character cha)
|
|
|
+ public final void registerMovingObject(final L2Character cha)
|
|
|
{
|
|
|
if (cha == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- _lock.lock();
|
|
|
- try
|
|
|
- {
|
|
|
- _movingObjects.putIfAbsent(cha.getObjectId(), cha);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- _lock.unlock();
|
|
|
- }
|
|
|
+ _movingObjects.putIfAbsent(cha.getObjectId(), cha);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Move all characters contained in movingObjects of GameTimeController.<br>
|
|
|
- * All characters in movement are identified in <b>movingObjects</b> of GameTimeController.<br>
|
|
|
- * <b><u> Actions</u> :</b><br>
|
|
|
+ * Move all L2Characters contained in movingObjects of GameTimeController.<BR>
|
|
|
+ * <BR>
|
|
|
+ * <B><U> Concept</U> :</B><BR>
|
|
|
+ * <BR>
|
|
|
+ * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
|
|
|
+ * <BR>
|
|
|
+ * <B><U> Actions</U> :</B><BR>
|
|
|
+ * <BR>
|
|
|
* <li>Update the position of each L2Character</li> <li>If movement is finished, the L2Character is removed from movingObjects</li> <li>Create a task to update the _knownObject and _knowPlayers of each L2Character that finished its movement and of their already known L2Object then notify AI with
|
|
|
- * EVT_ARRIVED</li>
|
|
|
+ * EVT_ARRIVED</li><BR>
|
|
|
+ * <BR>
|
|
|
*/
|
|
|
- protected void moveObjects()
|
|
|
+ private final void moveObjects()
|
|
|
{
|
|
|
- _lock.lock();
|
|
|
- try
|
|
|
- {
|
|
|
- _movingObjects.forEachValue(new MoveObjects());
|
|
|
- }
|
|
|
- finally
|
|
|
+ L2Character character;
|
|
|
+ for (FastMap.Entry<Integer, L2Character> e = _movingObjects.head(), tail = _movingObjects.tail(); (e = e.getNext()) != tail;)
|
|
|
{
|
|
|
- _lock.unlock();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- protected final class MoveObjects implements TObjectProcedure<L2Character>
|
|
|
- {
|
|
|
- @Override
|
|
|
- public final boolean execute(final L2Character ch)
|
|
|
- {
|
|
|
- if (ch.updatePosition(_gameTicks))
|
|
|
+ character = e.getValue();
|
|
|
+
|
|
|
+ if (character.updatePosition(getGameTicks()))
|
|
|
{
|
|
|
- // If movement is finished, the L2Character is removed from
|
|
|
- // movingObjects and added to the ArrayList ended
|
|
|
- _movingObjects.remove(ch.getObjectId());
|
|
|
- ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
|
|
|
+ // Destination reached. Remove from map and execute arrive event.
|
|
|
+ _movingObjects.remove(e.getKey());
|
|
|
+ fireCharacterArrived(character);
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Stop timer.
|
|
|
- */
|
|
|
- public void stopTimer()
|
|
|
- {
|
|
|
- _interruptRequest = true;
|
|
|
- _timer.interrupt();
|
|
|
- }
|
|
|
-
|
|
|
- class TimerThread extends Thread
|
|
|
+ private final void fireCharacterArrived(final L2Character character)
|
|
|
{
|
|
|
- /**
|
|
|
- * Instantiates a new timer thread.
|
|
|
- */
|
|
|
- public TimerThread()
|
|
|
+ final L2CharacterAI ai = character.getAI();
|
|
|
+ if (ai == null)
|
|
|
{
|
|
|
- super("GameTimeController");
|
|
|
- setDaemon(true);
|
|
|
- setPriority(MAX_PRIORITY);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void run()
|
|
|
+ ThreadPoolManager.getInstance().executeTask(new Runnable()
|
|
|
{
|
|
|
- int oldTicks;
|
|
|
- long runtime;
|
|
|
- int sleepTime;
|
|
|
-
|
|
|
- for (;;)
|
|
|
+ @Override
|
|
|
+ public final void run()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- oldTicks = _gameTicks; // save old ticks value to avoid moving objects 2x in same tick
|
|
|
- runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
|
|
|
-
|
|
|
- _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
|
|
|
-
|
|
|
- if (oldTicks != _gameTicks)
|
|
|
- {
|
|
|
- moveObjects(); // Runs possibly too often
|
|
|
- }
|
|
|
-
|
|
|
- runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
|
|
|
-
|
|
|
- // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
|
|
|
- sleepTime = (1 + MILLIS_IN_TICK) - (((int) runtime) % MILLIS_IN_TICK);
|
|
|
-
|
|
|
- // _log.finest("TICK: "+_gameTicks);
|
|
|
-
|
|
|
- if (sleepTime > 0)
|
|
|
- {
|
|
|
- Thread.sleep(sleepTime);
|
|
|
- }
|
|
|
- }
|
|
|
- catch (InterruptedException ie)
|
|
|
- {
|
|
|
- if (_interruptRequest)
|
|
|
+ if (Config.MOVE_BASED_KNOWNLIST)
|
|
|
{
|
|
|
- return;
|
|
|
+ character.getKnownList().findObjects();
|
|
|
}
|
|
|
|
|
|
- _log.log(Level.WARNING, "", ie);
|
|
|
+ ai.notifyEvent(CtrlEvent.EVT_ARRIVED);
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
+ catch (final Throwable e)
|
|
|
{
|
|
|
- _log.log(Level.WARNING, "", e);
|
|
|
+ StackTrace.displayStackTraceInformation(e);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Update the _knownObject and _knowPlayers of each L2Character that finished its movement and of their already known L2Object then notify AI with EVT_ARRIVED.
|
|
|
- */
|
|
|
- private static class MovingObjectArrived implements Runnable
|
|
|
+ public final void stopTimer()
|
|
|
+ {
|
|
|
+ super.interrupt();
|
|
|
+ System.out.println("Stopping GameTimeController.");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public final void run()
|
|
|
{
|
|
|
- private final L2Character _ended;
|
|
|
+ _log.log(Level.CONFIG, "GameClientController: Started.");
|
|
|
+
|
|
|
+ long nextTickTime, sleepTime;
|
|
|
+ boolean isNight = isNight();
|
|
|
|
|
|
- /**
|
|
|
- * Instantiates a new moving object arrived.
|
|
|
- * @param ended the ended
|
|
|
- */
|
|
|
- MovingObjectArrived(L2Character ended)
|
|
|
+ if (isNight)
|
|
|
{
|
|
|
- _ended = ended;
|
|
|
+ ThreadPoolManager.getInstance().executeTask(new Runnable()
|
|
|
+ {
|
|
|
+ @Override
|
|
|
+ public final void run()
|
|
|
+ {
|
|
|
+ DayNightSpawnManager.getInstance().notifyChangeMode();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void run()
|
|
|
+ while (true)
|
|
|
{
|
|
|
+ nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
- if (_ended.hasAI()) // AI could be just disabled due to region turn off
|
|
|
- {
|
|
|
- if (Config.MOVE_BASED_KNOWNLIST)
|
|
|
- {
|
|
|
- _ended.getKnownList().findObjects();
|
|
|
- }
|
|
|
- _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
|
|
|
- }
|
|
|
+ moveObjects();
|
|
|
}
|
|
|
- catch (NullPointerException e)
|
|
|
+ catch (final Throwable e)
|
|
|
{
|
|
|
- _log.log(Level.WARNING, "", e);
|
|
|
+ StackTrace.displayStackTraceInformation(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ sleepTime = nextTickTime - System.currentTimeMillis();
|
|
|
+ if (sleepTime > 0)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Thread.sleep(sleepTime);
|
|
|
+ }
|
|
|
+ catch (final InterruptedException e)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- class BroadcastSunState implements Runnable
|
|
|
- {
|
|
|
- int h;
|
|
|
- boolean tempIsNight;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run()
|
|
|
- {
|
|
|
- h = ((getGameTime() + 29) / 60) % 24; // Time in hour (+ 29 is to round 60)
|
|
|
- tempIsNight = (h < 6);
|
|
|
|
|
|
- if (tempIsNight != _isNight)
|
|
|
+ if (isNight() != isNight)
|
|
|
{
|
|
|
- // If diff day/night state
|
|
|
- _isNight = tempIsNight; // Set current day/night variable to value of temp variable
|
|
|
- DayNightSpawnManager.getInstance().notifyChangeMode();
|
|
|
+ isNight = !isNight;
|
|
|
+
|
|
|
+ ThreadPoolManager.getInstance().executeTask(new Runnable()
|
|
|
+ {
|
|
|
+ @Override
|
|
|
+ public final void run()
|
|
|
+ {
|
|
|
+ DayNightSpawnManager.getInstance().notifyChangeMode();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private static class SingletonHolder
|
|
|
- {
|
|
|
- protected static final GameTimeController _instance = new GameTimeController();
|
|
|
- }
|
|
|
-}
|
|
|
+}
|