GameTimeController.java 7.8 KB

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