GameTimeController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 net.sf.l2j.gameserver;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.Map;
  19. import java.util.concurrent.ScheduledFuture;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import javolution.util.FastMap;
  23. import net.sf.l2j.gameserver.ai.CtrlEvent;
  24. import net.sf.l2j.gameserver.instancemanager.DayNightSpawnManager;
  25. import net.sf.l2j.gameserver.model.L2Character;
  26. /**
  27. * This class ...
  28. *
  29. * @version $Revision: 1.1.4.8 $ $Date: 2005/04/06 16:13:24 $
  30. */
  31. public class GameTimeController
  32. {
  33. static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  34. public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  35. public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  36. private static GameTimeController _instance = new GameTimeController();
  37. protected static int _gameTicks;
  38. protected static long _gameStartTime;
  39. protected static boolean _isNight = false;
  40. private static Map<Integer,L2Character> _movingObjects = new FastMap<Integer,L2Character>().setShared(true);
  41. protected static TimerThread _timer;
  42. private ScheduledFuture<?> _timerWatcher;
  43. /**
  44. * one ingame day is 240 real minutes
  45. */
  46. public static GameTimeController getInstance()
  47. {
  48. return _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. _timerWatcher = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new TimerWatcher(), 0, 1000);
  57. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
  58. }
  59. public boolean isNowNight()
  60. {
  61. return _isNight;
  62. }
  63. public int getGameTime()
  64. {
  65. return (_gameTicks / (TICKS_PER_SECOND * 10));
  66. }
  67. public static int getGameTicks()
  68. {
  69. return _gameTicks;
  70. }
  71. /**
  72. * Add a L2Character to movingObjects of GameTimeController.<BR><BR>
  73. *
  74. * <B><U> Concept</U> :</B><BR><BR>
  75. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  76. *
  77. * @param cha The L2Character to add to movingObjects of GameTimeController
  78. *
  79. */
  80. public void registerMovingObject(L2Character cha)
  81. {
  82. if(cha == null) return;
  83. if (!_movingObjects.containsKey(cha.getObjectId())) _movingObjects.put(cha.getObjectId(),cha);
  84. }
  85. /**
  86. * Move all L2Characters contained in movingObjects of GameTimeController.<BR><BR>
  87. *
  88. * <B><U> Concept</U> :</B><BR><BR>
  89. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  90. *
  91. * <B><U> Actions</U> :</B><BR><BR>
  92. * <li>Update the position of each L2Character </li>
  93. * <li>If movement is finished, the L2Character is removed from movingObjects </li>
  94. * <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>
  95. *
  96. */
  97. protected void moveObjects()
  98. {
  99. // Create an FastList to contain all L2Character that are arrived to
  100. // destination
  101. FastList<L2Character> ended = null;
  102. // Go throw the table containing L2Character in movement
  103. for (L2Character ch : _movingObjects.values())
  104. {
  105. // If movement is finished, the L2Character is removed from
  106. // movingObjects and added to the ArrayList ended
  107. if (ch != null)
  108. if (ch.updatePosition(_gameTicks))
  109. {
  110. if (ended == null)
  111. ended = new FastList<L2Character>();
  112. ended.add(ch);
  113. }
  114. }
  115. if (ended != null)
  116. {
  117. _movingObjects.values().removeAll(ended);
  118. for (L2Character ch : ended)
  119. if (ch != null) // Disconnected?
  120. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
  121. ended.clear();
  122. }
  123. }
  124. public void stopTimer()
  125. {
  126. _timerWatcher.cancel(true);
  127. _timer.interrupt();
  128. }
  129. class TimerThread extends Thread
  130. {
  131. protected Exception _error;
  132. public TimerThread()
  133. {
  134. super("GameTimeController");
  135. setDaemon(true);
  136. setPriority(MAX_PRIORITY);
  137. _error = null;
  138. }
  139. @Override
  140. public void run()
  141. {
  142. try
  143. {
  144. for (;;)
  145. {
  146. int _oldTicks=_gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  147. long runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  148. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  149. if (_oldTicks != _gameTicks) moveObjects(); // XXX: if this makes objects go slower, remove it
  150. // but I think it can't make that effect. is it better to call moveObjects() twice in same
  151. // tick to make-up for missed tick ? or is it better to ignore missed tick ?
  152. // (will happen very rarely but it will happen ... on garbage collection definitely)
  153. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  154. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  155. int sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
  156. //_log.finest("TICK: "+_gameTicks);
  157. sleep(sleepTime); // hope other threads will have much more cpu time available now
  158. // SelectorThread most of all
  159. }
  160. }
  161. catch (Exception e)
  162. {
  163. _error = e;
  164. }
  165. }
  166. }
  167. class TimerWatcher implements Runnable
  168. {
  169. public void run()
  170. {
  171. if (!_timer.isAlive())
  172. {
  173. String time = (new SimpleDateFormat("HH:mm:ss")).format(new Date());
  174. _log.warning(time + " TimerThread stop with following error. restart it.");
  175. if (_timer._error != null) _timer._error.printStackTrace();
  176. _timer = new TimerThread();
  177. _timer.start();
  178. }
  179. }
  180. }
  181. /**
  182. * 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>
  183. */
  184. class MovingObjectArrived implements Runnable
  185. {
  186. private final L2Character _ended;
  187. MovingObjectArrived(L2Character ended)
  188. {
  189. _ended = ended;
  190. }
  191. public void run()
  192. {
  193. try
  194. {
  195. _ended.getKnownList().updateKnownObjects();
  196. _ended.getAI().notifyEvent(CtrlEvent.EVT_ARRIVED);
  197. }
  198. catch (NullPointerException e)
  199. {
  200. e.printStackTrace();
  201. }
  202. }
  203. }
  204. /**
  205. * @param rise
  206. */
  207. class BroadcastSunState implements Runnable
  208. {
  209. public void run()
  210. {
  211. int h = (getGameTime() / 60) % 24; // Time in hour
  212. boolean tempIsNight = (h < 6);
  213. if (tempIsNight != _isNight) { // If diff day/night state
  214. _isNight = tempIsNight; // Set current day/night varible to value of temp varible
  215. DayNightSpawnManager.getInstance().notifyChangeMode();
  216. }
  217. }
  218. }
  219. }