GameTimeController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.util.Calendar;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastMap;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.ai.CtrlEvent;
  26. import com.l2jserver.gameserver.ai.L2CharacterAI;
  27. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  28. import com.l2jserver.gameserver.model.actor.L2Character;
  29. import com.l2jserver.util.StackTrace;
  30. /**
  31. * Game Time controller class.
  32. * @author Unknown, Forsaiken
  33. */
  34. public final class GameTimeController extends Thread
  35. {
  36. private static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  37. public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  38. public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  39. public static final int IG_DAYS_PER_DAY = 6;
  40. public static final int MILLIS_PER_IG_DAY = (3600000 * 24) / IG_DAYS_PER_DAY;
  41. public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000;
  42. public static final int MINUTES_PER_IG_DAY = SECONDS_PER_IG_DAY / 60;
  43. public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
  44. public static final int TICKS_SUN_STATE_CHANGE = TICKS_PER_IG_DAY / 4;
  45. private static GameTimeController _instance;
  46. private final FastMap<Integer, L2Character> _movingObjects = new FastMap<Integer, L2Character>().shared();
  47. private final long _referenceTime;
  48. private GameTimeController()
  49. {
  50. super("GameTimeController");
  51. super.setDaemon(true);
  52. super.setPriority(MAX_PRIORITY);
  53. final Calendar c = Calendar.getInstance();
  54. c.set(Calendar.HOUR_OF_DAY, 0);
  55. c.set(Calendar.MINUTE, 0);
  56. c.set(Calendar.SECOND, 0);
  57. c.set(Calendar.MILLISECOND, 0);
  58. _referenceTime = c.getTimeInMillis();
  59. super.start();
  60. }
  61. public static final void init()
  62. {
  63. _instance = new GameTimeController();
  64. }
  65. public final int getGameTime()
  66. {
  67. return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
  68. }
  69. public final int getGameHour()
  70. {
  71. return getGameTime() / 60;
  72. }
  73. public final int getGameMinute()
  74. {
  75. return getGameTime() % 60;
  76. }
  77. public final boolean isNight()
  78. {
  79. return getGameHour() < 6;
  80. }
  81. /**
  82. * The true GameTime tick. Directly taken from current time. This represents the tick of the time.
  83. * @return
  84. */
  85. public final int getGameTicks()
  86. {
  87. return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
  88. }
  89. /**
  90. * Add a L2Character to movingObjects of GameTimeController.
  91. * @param cha The L2Character to add to movingObjects of GameTimeController
  92. */
  93. public final void registerMovingObject(final L2Character cha)
  94. {
  95. if (cha == null)
  96. {
  97. return;
  98. }
  99. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  100. }
  101. /**
  102. * Move all L2Characters contained in movingObjects of GameTimeController.<BR>
  103. * <B><U> Concept</U> :</B><BR>
  104. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
  105. * <B><U> Actions</U> :</B><BR>
  106. * <ul>
  107. * <li>Update the position of each L2Character</li>
  108. * <li>If movement is finished, the L2Character is removed from movingObjects</li>
  109. * <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>
  110. * </ul>
  111. */
  112. private final void moveObjects()
  113. {
  114. L2Character character;
  115. for (FastMap.Entry<Integer, L2Character> e = _movingObjects.head(), tail = _movingObjects.tail(); (e = e.getNext()) != tail;)
  116. {
  117. character = e.getValue();
  118. if (character.updatePosition(getGameTicks()))
  119. {
  120. // Destination reached. Remove from map and execute arrive event.
  121. _movingObjects.remove(e.getKey());
  122. fireCharacterArrived(character);
  123. }
  124. }
  125. }
  126. private final void fireCharacterArrived(final L2Character character)
  127. {
  128. final L2CharacterAI ai = character.getAI();
  129. if (ai == null)
  130. {
  131. return;
  132. }
  133. ThreadPoolManager.getInstance().executeAi(new Runnable()
  134. {
  135. @Override
  136. public final void run()
  137. {
  138. try
  139. {
  140. if (Config.MOVE_BASED_KNOWNLIST)
  141. {
  142. character.getKnownList().findObjects();
  143. }
  144. ai.notifyEvent(CtrlEvent.EVT_ARRIVED);
  145. }
  146. catch (final Throwable e)
  147. {
  148. StackTrace.displayStackTraceInformation(e);
  149. }
  150. }
  151. });
  152. }
  153. public final void stopTimer()
  154. {
  155. super.interrupt();
  156. _log.log(Level.INFO, "Stopping " + getClass().getSimpleName());
  157. }
  158. @Override
  159. public final void run()
  160. {
  161. _log.log(Level.CONFIG, getClass().getSimpleName() + ": Started.");
  162. long nextTickTime, sleepTime;
  163. boolean isNight = isNight();
  164. if (isNight)
  165. {
  166. ThreadPoolManager.getInstance().executeAi(new Runnable()
  167. {
  168. @Override
  169. public final void run()
  170. {
  171. DayNightSpawnManager.getInstance().notifyChangeMode();
  172. }
  173. });
  174. }
  175. while (true)
  176. {
  177. nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
  178. try
  179. {
  180. moveObjects();
  181. }
  182. catch (final Throwable e)
  183. {
  184. StackTrace.displayStackTraceInformation(e);
  185. }
  186. sleepTime = nextTickTime - System.currentTimeMillis();
  187. if (sleepTime > 0)
  188. {
  189. try
  190. {
  191. Thread.sleep(sleepTime);
  192. }
  193. catch (final InterruptedException e)
  194. {
  195. }
  196. }
  197. if (isNight() != isNight)
  198. {
  199. isNight = !isNight;
  200. ThreadPoolManager.getInstance().executeAi(new Runnable()
  201. {
  202. @Override
  203. public final void run()
  204. {
  205. DayNightSpawnManager.getInstance().notifyChangeMode();
  206. }
  207. });
  208. }
  209. }
  210. }
  211. public static final GameTimeController getInstance()
  212. {
  213. return _instance;
  214. }
  215. }