GameTimeController.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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]
  27. *
  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. private static final TIntObjectHashMap<L2Character> _movingObjects = new TIntObjectHashMap<L2Character>();
  40. private static final ReentrantLock _lock = new ReentrantLock();
  41. protected static TimerThread _timer;
  42. /**
  43. * one ingame day is 240 real minutes
  44. * @return
  45. */
  46. public static GameTimeController getInstance()
  47. {
  48. return SingletonHolder._instance;
  49. }
  50. private GameTimeController()
  51. {
  52. _gameStartTime = System.currentTimeMillis() - 3600000; // offset so that the server starts a day begin
  53. _gameTicks = 3600000 / MILLIS_IN_TICK; // offset so that the server starts a day begin
  54. _timer = new TimerThread();
  55. _timer.start();
  56. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
  57. }
  58. public boolean isNowNight()
  59. {
  60. return _isNight;
  61. }
  62. public int getGameTime()
  63. {
  64. return (_gameTicks / (TICKS_PER_SECOND * 10));
  65. }
  66. public static int getGameTicks()
  67. {
  68. return _gameTicks;
  69. }
  70. /**
  71. * Add a L2Character to movingObjects of GameTimeController.<BR><BR>
  72. *
  73. * <B><U> Concept</U> :</B><BR><BR>
  74. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  75. *
  76. * @param cha The L2Character to add to movingObjects of GameTimeController
  77. *
  78. */
  79. public void registerMovingObject(L2Character cha)
  80. {
  81. if (cha == null)
  82. return;
  83. _lock.lock();
  84. try
  85. {
  86. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  87. }
  88. finally
  89. {
  90. _lock.unlock();
  91. }
  92. }
  93. /**
  94. * Move all L2Characters contained in movingObjects of GameTimeController.<BR><BR>
  95. *
  96. * <B><U> Concept</U> :</B><BR><BR>
  97. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  98. *
  99. * <B><U> Actions</U> :</B><BR><BR>
  100. * <li>Update the position of each L2Character </li>
  101. * <li>If movement is finished, the L2Character is removed from movingObjects </li>
  102. * <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><BR><BR>
  103. *
  104. */
  105. protected void moveObjects()
  106. {
  107. _lock.lock();
  108. try
  109. {
  110. _movingObjects.forEachValue(new MoveObjects());
  111. }
  112. finally
  113. {
  114. _lock.unlock();
  115. }
  116. }
  117. private final class MoveObjects implements TObjectProcedure<L2Character>
  118. {
  119. @Override
  120. public final boolean execute(final L2Character ch)
  121. {
  122. if (ch.updatePosition(_gameTicks))
  123. {
  124. // If movement is finished, the L2Character is removed from
  125. // movingObjects and added to the ArrayList ended
  126. _movingObjects.remove(ch.getObjectId());
  127. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
  128. }
  129. return true;
  130. }
  131. }
  132. public void stopTimer()
  133. {
  134. _interruptRequest = true;
  135. _timer.interrupt();
  136. }
  137. class TimerThread extends Thread
  138. {
  139. public TimerThread()
  140. {
  141. super("GameTimeController");
  142. setDaemon(true);
  143. setPriority(MAX_PRIORITY);
  144. }
  145. @Override
  146. public void run()
  147. {
  148. int oldTicks;
  149. long runtime;
  150. int sleepTime;
  151. for(;;)
  152. {
  153. try
  154. {
  155. oldTicks = _gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  156. runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  157. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  158. if (oldTicks != _gameTicks)
  159. moveObjects(); // Runs possibly too often
  160. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  161. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  162. sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
  163. //_log.finest("TICK: "+_gameTicks);
  164. if (sleepTime > 0)
  165. Thread.sleep(sleepTime);
  166. }
  167. catch (InterruptedException ie)
  168. {
  169. if (_interruptRequest)
  170. return;
  171. _log.log(Level.WARNING, "", ie);
  172. }
  173. catch (Exception e)
  174. {
  175. _log.log(Level.WARNING, "", e);
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * Update the _knownObject and _knowPlayers of each L2Character that finished its movement and of their already known L2Object then notify AI with EVT_ARRIVED.<BR><BR>
  182. */
  183. private static class MovingObjectArrived implements Runnable
  184. {
  185. private final L2Character _ended;
  186. MovingObjectArrived(L2Character ended)
  187. {
  188. _ended = ended;
  189. }
  190. @Override
  191. public void run()
  192. {
  193. try
  194. {
  195. if (_ended.hasAI()) // AI could be just disabled due to region turn off
  196. {
  197. if (Config.MOVE_BASED_KNOWNLIST)
  198. _ended.getKnownList().findObjects();
  199. _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  200. }
  201. }
  202. catch (NullPointerException e)
  203. {
  204. _log.log(Level.WARNING, "", e);
  205. }
  206. }
  207. }
  208. class BroadcastSunState implements Runnable
  209. {
  210. int h;
  211. boolean tempIsNight;
  212. @Override
  213. public void run()
  214. {
  215. h = ((getGameTime() + 29) / 60) % 24; // Time in hour (+ 29 is to round 60)
  216. tempIsNight = (h < 6);
  217. if (tempIsNight != _isNight)
  218. { // If diff day/night state
  219. _isNight = tempIsNight; // Set current day/night varible to value of temp varible
  220. DayNightSpawnManager.getInstance().notifyChangeMode();
  221. }
  222. }
  223. }
  224. @SuppressWarnings("synthetic-access")
  225. private static class SingletonHolder
  226. {
  227. protected static final GameTimeController _instance = new GameTimeController();
  228. }
  229. }