GameTimeController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 gnu.trove.map.hash.TIntObjectHashMap;
  17. import gnu.trove.procedure.TObjectProcedure;
  18. import java.util.concurrent.locks.ReentrantLock;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.ai.CtrlEvent;
  23. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  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. return;
  94. _lock.lock();
  95. try
  96. {
  97. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  98. }
  99. finally
  100. {
  101. _lock.unlock();
  102. }
  103. }
  104. /**
  105. * Move all characters contained in movingObjects of GameTimeController.<br>
  106. * All characters in movement are identified in <b>movingObjects</b> of GameTimeController.<br>
  107. * <b><u> Actions</u> :</b><br>
  108. * <li>Update the position of each L2Character </li>
  109. * <li>If movement is finished, the L2Character is removed from movingObjects </li>
  110. * <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>
  111. */
  112. protected void moveObjects()
  113. {
  114. _lock.lock();
  115. try
  116. {
  117. _movingObjects.forEachValue(new MoveObjects());
  118. }
  119. finally
  120. {
  121. _lock.unlock();
  122. }
  123. }
  124. protected final class MoveObjects implements TObjectProcedure<L2Character>
  125. {
  126. @Override
  127. public final boolean execute(final L2Character ch)
  128. {
  129. if (ch.updatePosition(_gameTicks))
  130. {
  131. // If movement is finished, the L2Character is removed from
  132. // movingObjects and added to the ArrayList ended
  133. _movingObjects.remove(ch.getObjectId());
  134. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
  135. }
  136. return true;
  137. }
  138. }
  139. /**
  140. * Stop timer.
  141. */
  142. public void stopTimer()
  143. {
  144. _interruptRequest = true;
  145. _timer.interrupt();
  146. }
  147. class TimerThread extends Thread
  148. {
  149. /**
  150. * Instantiates a new timer thread.
  151. */
  152. public TimerThread()
  153. {
  154. super("GameTimeController");
  155. setDaemon(true);
  156. setPriority(MAX_PRIORITY);
  157. }
  158. @Override
  159. public void run()
  160. {
  161. int oldTicks;
  162. long runtime;
  163. int sleepTime;
  164. for(;;)
  165. {
  166. try
  167. {
  168. oldTicks = _gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  169. runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  170. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  171. if (oldTicks != _gameTicks)
  172. moveObjects(); // Runs possibly too often
  173. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  174. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  175. sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
  176. //_log.finest("TICK: "+_gameTicks);
  177. if (sleepTime > 0)
  178. Thread.sleep(sleepTime);
  179. }
  180. catch (InterruptedException ie)
  181. {
  182. if (_interruptRequest)
  183. return;
  184. _log.log(Level.WARNING, "", ie);
  185. }
  186. catch (Exception e)
  187. {
  188. _log.log(Level.WARNING, "", e);
  189. }
  190. }
  191. }
  192. }
  193. /**
  194. * Update the _knownObject and _knowPlayers of each L2Character that finished its movement and of their already known L2Object then notify AI with EVT_ARRIVED.
  195. */
  196. private static class MovingObjectArrived implements Runnable
  197. {
  198. private final L2Character _ended;
  199. /**
  200. * Instantiates a new moving object arrived.
  201. * @param ended the ended
  202. */
  203. MovingObjectArrived(L2Character ended)
  204. {
  205. _ended = ended;
  206. }
  207. @Override
  208. public void run()
  209. {
  210. try
  211. {
  212. if (_ended.hasAI()) // AI could be just disabled due to region turn off
  213. {
  214. if (Config.MOVE_BASED_KNOWNLIST)
  215. _ended.getKnownList().findObjects();
  216. _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  217. }
  218. }
  219. catch (NullPointerException e)
  220. {
  221. _log.log(Level.WARNING, "", e);
  222. }
  223. }
  224. }
  225. class BroadcastSunState implements Runnable
  226. {
  227. int h;
  228. boolean tempIsNight;
  229. @Override
  230. public void run()
  231. {
  232. h = ((getGameTime() + 29) / 60) % 24; // Time in hour (+ 29 is to round 60)
  233. tempIsNight = (h < 6);
  234. if (tempIsNight != _isNight)
  235. {
  236. // If diff day/night state
  237. _isNight = tempIsNight; // Set current day/night variable to value of temp variable
  238. DayNightSpawnManager.getInstance().notifyChangeMode();
  239. }
  240. }
  241. }
  242. private static class SingletonHolder
  243. {
  244. protected static final GameTimeController _instance = new GameTimeController();
  245. }
  246. }