GameTimeController.java 7.5 KB

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