2
0

GameTimeController.java 6.4 KB

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