GameTimeController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2004-2013 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. * @author Unknown, Forsaiken
  32. */
  33. public final class GameTimeController extends Thread
  34. {
  35. static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  36. public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  37. public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  38. public static final int IG_DAYS_PER_DAY = 6;
  39. public static final int MILLIS_PER_IG_DAY = (1000 * 60 * 60 * 24) / IG_DAYS_PER_DAY;
  40. public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000;
  41. public static final int MINUTES_PER_IG_DAY = SECONDS_PER_IG_DAY / 60;
  42. public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
  43. public static final int TICKS_SUN_STATE_CHANGE = TICKS_PER_IG_DAY / 4;
  44. private static GameTimeController _instance;
  45. public static final void init()
  46. {
  47. _instance = new GameTimeController();
  48. }
  49. public static final GameTimeController getInstance()
  50. {
  51. return _instance;
  52. }
  53. private final FastMap<Integer, L2Character> _movingObjects;
  54. private final long _referenceTime;
  55. private GameTimeController()
  56. {
  57. super("GameTimeController");
  58. super.setDaemon(true);
  59. super.setPriority(MAX_PRIORITY);
  60. _movingObjects = new FastMap<Integer, L2Character>().shared();
  61. final Calendar c = Calendar.getInstance();
  62. c.set(Calendar.HOUR_OF_DAY, 0);
  63. c.set(Calendar.MINUTE, 0);
  64. c.set(Calendar.SECOND, 0);
  65. c.set(Calendar.MILLISECOND, 0);
  66. _referenceTime = c.getTimeInMillis();
  67. super.start();
  68. }
  69. public final int getGameTime()
  70. {
  71. return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
  72. }
  73. public final int getGameHour()
  74. {
  75. return getGameTime() / 60;
  76. }
  77. public final int getGameMinute()
  78. {
  79. return getGameTime() % 60;
  80. }
  81. public final boolean isNight()
  82. {
  83. return getGameHour() < 6;
  84. }
  85. /**
  86. * The true GameTime tick. Directly taken from current time. This represents the tick of the time.
  87. * @return
  88. */
  89. public final int getGameTicks()
  90. {
  91. return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
  92. }
  93. /**
  94. * Add a L2Character to movingObjects of GameTimeController.<BR>
  95. * <BR>
  96. * <B><U> Concept</U> :</B><BR>
  97. * <BR>
  98. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
  99. * <BR>
  100. * @param cha The L2Character to add to movingObjects of GameTimeController
  101. */
  102. public final void registerMovingObject(final L2Character cha)
  103. {
  104. if (cha == null)
  105. {
  106. return;
  107. }
  108. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  109. }
  110. /**
  111. * Move all L2Characters contained in movingObjects of GameTimeController.<BR>
  112. * <BR>
  113. * <B><U> Concept</U> :</B><BR>
  114. * <BR>
  115. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
  116. * <BR>
  117. * <B><U> Actions</U> :</B><BR>
  118. * <BR>
  119. * <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
  120. * EVT_ARRIVED</li><BR>
  121. * <BR>
  122. */
  123. private final void moveObjects()
  124. {
  125. L2Character character;
  126. for (FastMap.Entry<Integer, L2Character> e = _movingObjects.head(), tail = _movingObjects.tail(); (e = e.getNext()) != tail;)
  127. {
  128. character = e.getValue();
  129. if (character.updatePosition(getGameTicks()))
  130. {
  131. // Destination reached. Remove from map and execute arrive event.
  132. _movingObjects.remove(e.getKey());
  133. fireCharacterArrived(character);
  134. }
  135. }
  136. }
  137. private final void fireCharacterArrived(final L2Character character)
  138. {
  139. final L2CharacterAI ai = character.getAI();
  140. if (ai == null)
  141. {
  142. return;
  143. }
  144. ThreadPoolManager.getInstance().executeTask(new Runnable()
  145. {
  146. @Override
  147. public final void run()
  148. {
  149. try
  150. {
  151. if (Config.MOVE_BASED_KNOWNLIST)
  152. {
  153. character.getKnownList().findObjects();
  154. }
  155. ai.notifyEvent(CtrlEvent.EVT_ARRIVED);
  156. }
  157. catch (final Throwable e)
  158. {
  159. StackTrace.displayStackTraceInformation(e);
  160. }
  161. }
  162. });
  163. }
  164. public final void stopTimer()
  165. {
  166. super.interrupt();
  167. System.out.println("Stopping GameTimeController.");
  168. }
  169. @Override
  170. public final void run()
  171. {
  172. _log.log(Level.CONFIG, "GameClientController: Started.");
  173. long nextTickTime, sleepTime;
  174. boolean isNight = isNight();
  175. if (isNight)
  176. {
  177. ThreadPoolManager.getInstance().executeTask(new Runnable()
  178. {
  179. @Override
  180. public final void run()
  181. {
  182. DayNightSpawnManager.getInstance().notifyChangeMode();
  183. }
  184. });
  185. }
  186. while (true)
  187. {
  188. nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
  189. try
  190. {
  191. moveObjects();
  192. }
  193. catch (final Throwable e)
  194. {
  195. StackTrace.displayStackTraceInformation(e);
  196. }
  197. sleepTime = nextTickTime - System.currentTimeMillis();
  198. if (sleepTime > 0)
  199. {
  200. try
  201. {
  202. Thread.sleep(sleepTime);
  203. }
  204. catch (final InterruptedException e)
  205. {
  206. }
  207. }
  208. if (isNight() != isNight)
  209. {
  210. isNight = !isNight;
  211. ThreadPoolManager.getInstance().executeTask(new Runnable()
  212. {
  213. @Override
  214. public final void run()
  215. {
  216. DayNightSpawnManager.getInstance().notifyChangeMode();
  217. }
  218. });
  219. }
  220. }
  221. }
  222. }