GameTimeController.java 6.8 KB

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