GameTimeController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.logging.Level;
  17. import java.util.logging.Logger;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.ai.CtrlEvent;
  20. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  21. import com.l2jserver.gameserver.model.actor.L2Character;
  22. import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
  23. /**
  24. * Removed TimerThread watcher [DrHouse]
  25. *
  26. * @version $Date: 2010/02/02 22:43:00 $
  27. */
  28. public class GameTimeController
  29. {
  30. protected static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  31. public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  32. public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  33. protected static int _gameTicks;
  34. protected static long _gameStartTime;
  35. protected static boolean _isNight = false;
  36. protected static boolean _interruptRequest = false;
  37. private static final L2TIntObjectHashMap<L2Character> _movingObjects = new L2TIntObjectHashMap<L2Character>();
  38. protected static TimerThread _timer;
  39. /**
  40. * one ingame day is 240 real minutes
  41. */
  42. public static GameTimeController getInstance()
  43. {
  44. return SingletonHolder._instance;
  45. }
  46. private GameTimeController()
  47. {
  48. _gameStartTime = System.currentTimeMillis() - 3600000; // offset so that the server starts a day begin
  49. _gameTicks = 3600000 / MILLIS_IN_TICK; // offset so that the server starts a day begin
  50. _timer = new TimerThread();
  51. _timer.start();
  52. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
  53. }
  54. public boolean isNowNight()
  55. {
  56. return _isNight;
  57. }
  58. public int getGameTime()
  59. {
  60. return (_gameTicks / (TICKS_PER_SECOND * 10));
  61. }
  62. public static int getGameTicks()
  63. {
  64. return _gameTicks;
  65. }
  66. /**
  67. * Add a L2Character to movingObjects of GameTimeController.<BR><BR>
  68. *
  69. * <B><U> Concept</U> :</B><BR><BR>
  70. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  71. *
  72. * @param cha The L2Character to add to movingObjects of GameTimeController
  73. *
  74. */
  75. public void registerMovingObject(L2Character cha)
  76. {
  77. if (cha == null)
  78. return;
  79. _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  80. }
  81. /**
  82. * Move all L2Characters contained in movingObjects of GameTimeController.<BR><BR>
  83. *
  84. * <B><U> Concept</U> :</B><BR><BR>
  85. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  86. *
  87. * <B><U> Actions</U> :</B><BR><BR>
  88. * <li>Update the position of each L2Character </li>
  89. * <li>If movement is finished, the L2Character is removed from movingObjects </li>
  90. * <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>
  91. *
  92. */
  93. protected void moveObjects()
  94. {
  95. // Go throw the table containing L2Character in movement
  96. for (Object obj : _movingObjects.getValues())
  97. {
  98. // If movement is finished, the L2Character is removed from
  99. // movingObjects and added to the ArrayList ended
  100. if (((L2Character) obj).updatePosition(_gameTicks))
  101. {
  102. _movingObjects.remove(((L2Character) obj).getObjectId());
  103. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(((L2Character) obj)));
  104. }
  105. }
  106. }
  107. public void stopTimer()
  108. {
  109. _interruptRequest = true;
  110. _timer.interrupt();
  111. }
  112. class TimerThread extends Thread
  113. {
  114. public TimerThread()
  115. {
  116. super("GameTimeController");
  117. setDaemon(true);
  118. setPriority(MAX_PRIORITY);
  119. }
  120. @Override
  121. public void run()
  122. {
  123. int oldTicks;
  124. long runtime;
  125. int sleepTime;
  126. for(;;)
  127. {
  128. try
  129. {
  130. oldTicks = _gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  131. runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  132. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  133. if (oldTicks != _gameTicks)
  134. moveObjects(); // Runs possibly too often
  135. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  136. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  137. sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
  138. //_log.finest("TICK: "+_gameTicks);
  139. if (sleepTime > 0)
  140. Thread.sleep(sleepTime);
  141. }
  142. catch (InterruptedException ie)
  143. {
  144. if (_interruptRequest)
  145. return;
  146. _log.log(Level.WARNING, "", ie);
  147. }
  148. catch (Exception e)
  149. {
  150. _log.log(Level.WARNING, "", e);
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * 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>
  157. */
  158. private static class MovingObjectArrived implements Runnable
  159. {
  160. private final L2Character _ended;
  161. MovingObjectArrived(L2Character ended)
  162. {
  163. _ended = ended;
  164. }
  165. public void run()
  166. {
  167. try
  168. {
  169. if (_ended.hasAI()) // AI could be just disabled due to region turn off
  170. {
  171. if (Config.MOVE_BASED_KNOWNLIST)
  172. _ended.getKnownList().findObjects();
  173. _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  174. }
  175. }
  176. catch (NullPointerException e)
  177. {
  178. _log.log(Level.WARNING, "", e);
  179. }
  180. }
  181. }
  182. /**
  183. * @param rise
  184. */
  185. class BroadcastSunState implements Runnable
  186. {
  187. int h;
  188. boolean tempIsNight;
  189. public void run()
  190. {
  191. h = (getGameTime() / 60) % 24; // Time in hour
  192. tempIsNight = (h < 6);
  193. if (tempIsNight != _isNight)
  194. { // If diff day/night state
  195. _isNight = tempIsNight; // Set current day/night varible to value of temp varible
  196. DayNightSpawnManager.getInstance().notifyChangeMode();
  197. }
  198. }
  199. }
  200. @SuppressWarnings("synthetic-access")
  201. private static class SingletonHolder
  202. {
  203. protected static final GameTimeController _instance = new GameTimeController();
  204. }
  205. }