GameTimeController.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver;
  16. import java.util.concurrent.locks.ReentrantLock;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.ai.CtrlEvent;
  21. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import gnu.trove.map.hash.TIntObjectHashMap;
  24. import gnu.trove.procedure.TObjectProcedure;
  25. /**
  26. * Removed TimerThread watcher [DrHouse]<br>
  27. * One in-game day is 240 real minutes.
  28. * @version $Date: 2010/02/02 22:43:00 $
  29. */
  30. public class GameTimeController
  31. {
  32. protected static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  33. public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  34. public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  35. protected static int _gameTicks;
  36. protected static long _gameStartTime;
  37. protected static boolean _isNight = false;
  38. protected static boolean _interruptRequest = false;
  39. protected static final TIntObjectHashMap<L2Character> _movingObjects = new TIntObjectHashMap<>();
  40. private static final ReentrantLock _lock = new ReentrantLock();
  41. protected static TimerThread _timer;
  42. /**
  43. * Gets the single instance of GameTimeController.
  44. * @return single instance of GameTimeController
  45. */
  46. public static GameTimeController getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. /**
  51. * Instantiates a new game time controller.
  52. */
  53. protected GameTimeController()
  54. {
  55. _gameStartTime = System.currentTimeMillis() - 3600000; // offset so that the server starts a day begin
  56. _gameTicks = 3600000 / MILLIS_IN_TICK; // offset so that the server starts a day begin
  57. _timer = new TimerThread();
  58. _timer.start();
  59. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
  60. }
  61. /**
  62. * Checks if is now night.
  63. * @return true, if is now night
  64. */
  65. public boolean isNowNight()
  66. {
  67. return _isNight;
  68. }
  69. /**
  70. * Gets the game time.
  71. * @return the game time
  72. */
  73. public int getGameTime()
  74. {
  75. return (_gameTicks / (TICKS_PER_SECOND * 10));
  76. }
  77. /**
  78. * Gets the game ticks.
  79. * @return the game ticks
  80. */
  81. public static int getGameTicks()
  82. {
  83. return _gameTicks;
  84. }
  85. /**
  86. * Add a L2Character to movingObjects of GameTimeController.<br>
  87. * All characters in movement are identified in <b>movingObjects</b> of GameTimeController.
  88. * @param cha the character to add to movingObjects of GameTimeController
  89. */
  90. public void registerMovingObject(L2Character cha)
  91. {
  92. if (cha == null)
  93. {
  94. return;
  95. }
  96. _lock.lock();
  97. try
  98. {
  99. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  100. }
  101. finally
  102. {
  103. _lock.unlock();
  104. }
  105. }
  106. /**
  107. * Move all characters contained in movingObjects of GameTimeController.<br>
  108. * All characters in movement are identified in <b>movingObjects</b> of GameTimeController.<br>
  109. * <b><u> Actions</u> :</b><br>
  110. * <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
  111. * EVT_ARRIVED</li>
  112. */
  113. protected void moveObjects()
  114. {
  115. _lock.lock();
  116. try
  117. {
  118. _movingObjects.forEachValue(new MoveObjects());
  119. }
  120. finally
  121. {
  122. _lock.unlock();
  123. }
  124. }
  125. protected final class MoveObjects implements TObjectProcedure<L2Character>
  126. {
  127. @Override
  128. public final boolean execute(final L2Character ch)
  129. {
  130. if (ch.updatePosition(_gameTicks))
  131. {
  132. // If movement is finished, the L2Character is removed from
  133. // movingObjects and added to the ArrayList ended
  134. _movingObjects.remove(ch.getObjectId());
  135. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
  136. }
  137. return true;
  138. }
  139. }
  140. /**
  141. * Stop timer.
  142. */
  143. public void stopTimer()
  144. {
  145. _interruptRequest = true;
  146. _timer.interrupt();
  147. }
  148. class TimerThread extends Thread
  149. {
  150. /**
  151. * Instantiates a new timer thread.
  152. */
  153. public TimerThread()
  154. {
  155. super("GameTimeController");
  156. setDaemon(true);
  157. setPriority(MAX_PRIORITY);
  158. }
  159. @Override
  160. public void run()
  161. {
  162. int oldTicks;
  163. long runtime;
  164. int sleepTime;
  165. for (;;)
  166. {
  167. try
  168. {
  169. oldTicks = _gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  170. runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  171. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  172. if (oldTicks != _gameTicks)
  173. {
  174. moveObjects(); // Runs possibly too often
  175. }
  176. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  177. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  178. sleepTime = (1 + MILLIS_IN_TICK) - (((int) runtime) % MILLIS_IN_TICK);
  179. // _log.finest("TICK: "+_gameTicks);
  180. if (sleepTime > 0)
  181. {
  182. Thread.sleep(sleepTime);
  183. }
  184. }
  185. catch (InterruptedException ie)
  186. {
  187. if (_interruptRequest)
  188. {
  189. return;
  190. }
  191. _log.log(Level.WARNING, "", ie);
  192. }
  193. catch (Exception e)
  194. {
  195. _log.log(Level.WARNING, "", e);
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * Update the _knownObject and _knowPlayers of each L2Character that finished its movement and of their already known L2Object then notify AI with EVT_ARRIVED.
  202. */
  203. private static class MovingObjectArrived implements Runnable
  204. {
  205. private final L2Character _ended;
  206. /**
  207. * Instantiates a new moving object arrived.
  208. * @param ended the ended
  209. */
  210. MovingObjectArrived(L2Character ended)
  211. {
  212. _ended = ended;
  213. }
  214. @Override
  215. public void run()
  216. {
  217. try
  218. {
  219. if (_ended.hasAI()) // AI could be just disabled due to region turn off
  220. {
  221. if (Config.MOVE_BASED_KNOWNLIST)
  222. {
  223. _ended.getKnownList().findObjects();
  224. }
  225. _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  226. }
  227. }
  228. catch (NullPointerException e)
  229. {
  230. _log.log(Level.WARNING, "", e);
  231. }
  232. }
  233. }
  234. class BroadcastSunState implements Runnable
  235. {
  236. int h;
  237. boolean tempIsNight;
  238. @Override
  239. public void run()
  240. {
  241. h = ((getGameTime() + 29) / 60) % 24; // Time in hour (+ 29 is to round 60)
  242. tempIsNight = (h < 6);
  243. if (tempIsNight != _isNight)
  244. {
  245. // If diff day/night state
  246. _isNight = tempIsNight; // Set current day/night variable to value of temp variable
  247. DayNightSpawnManager.getInstance().notifyChangeMode();
  248. }
  249. }
  250. }
  251. private static class SingletonHolder
  252. {
  253. protected static final GameTimeController _instance = new GameTimeController();
  254. }
  255. }