GameTimeController.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.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 javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.gameserver.ai.CtrlEvent;
  27. import net.sf.l2j.gameserver.instancemanager.DayNightSpawnManager;
  28. import net.sf.l2j.gameserver.model.actor.L2Character;
  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. private static GameTimeController _instance = new GameTimeController();
  40. protected static int _gameTicks;
  41. protected static long _gameStartTime;
  42. protected static boolean _isNight = false;
  43. private static Map<Integer,L2Character> _movingObjects = new FastMap<Integer,L2Character>().setShared(true);
  44. protected static TimerThread _timer;
  45. private ScheduledFuture<?> _timerWatcher;
  46. /**
  47. * one ingame day is 240 real minutes
  48. */
  49. public static GameTimeController getInstance()
  50. {
  51. return _instance;
  52. }
  53. private GameTimeController()
  54. {
  55. _gameStartTime = System.currentTimeMillis() - 3600000; // offset so that the server starts a day begin
  56. _gameTicks = 3600000 / MILLIS_IN_TICK; // offset so that the server starts a day begin
  57. _timer = new TimerThread();
  58. _timer.start();
  59. _timerWatcher = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new TimerWatcher(), 0, 1000);
  60. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new BroadcastSunState(), 0, 600000);
  61. }
  62. public boolean isNowNight()
  63. {
  64. return _isNight;
  65. }
  66. public int getGameTime()
  67. {
  68. return (_gameTicks / (TICKS_PER_SECOND * 10));
  69. }
  70. public static int getGameTicks()
  71. {
  72. return _gameTicks;
  73. }
  74. /**
  75. * Add a L2Character to movingObjects of GameTimeController.<BR><BR>
  76. *
  77. * <B><U> Concept</U> :</B><BR><BR>
  78. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  79. *
  80. * @param cha The L2Character to add to movingObjects of GameTimeController
  81. *
  82. */
  83. public void registerMovingObject(L2Character cha)
  84. {
  85. if(cha == null) return;
  86. if (!_movingObjects.containsKey(cha.getObjectId())) _movingObjects.put(cha.getObjectId(),cha);
  87. }
  88. /**
  89. * Move all L2Characters contained in movingObjects of GameTimeController.<BR><BR>
  90. *
  91. * <B><U> Concept</U> :</B><BR><BR>
  92. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR><BR>
  93. *
  94. * <B><U> Actions</U> :</B><BR><BR>
  95. * <li>Update the position of each L2Character </li>
  96. * <li>If movement is finished, the L2Character is removed from movingObjects </li>
  97. * <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>
  98. *
  99. */
  100. protected void moveObjects()
  101. {
  102. // Create an FastList to contain all L2Character that are arrived to
  103. // destination
  104. List<L2Character> ended = null;
  105. // Go throw the table containing L2Character in movement
  106. Collection<L2Character> mObjs = _movingObjects.values();
  107. //synchronized (_movingObjects)
  108. {
  109. for (L2Character ch : mObjs)
  110. {
  111. // If movement is finished, the L2Character is removed from
  112. // movingObjects and added to the ArrayList ended
  113. if (ch.updatePosition(_gameTicks))
  114. {
  115. if (ended == null)
  116. ended = new ArrayList<L2Character>();
  117. ended.add(ch);
  118. }
  119. }
  120. if (ended != null)
  121. {
  122. _movingObjects.values().removeAll(ended);
  123. for (L2Character ch : ended)
  124. ThreadPoolManager.getInstance().executeTask(new MovingObjectArrived(ch));
  125. ended.clear();
  126. }
  127. }
  128. }
  129. public void stopTimer()
  130. {
  131. _timerWatcher.cancel(true);
  132. _timer.interrupt();
  133. }
  134. class TimerThread extends Thread
  135. {
  136. protected Exception _error;
  137. public TimerThread()
  138. {
  139. super("GameTimeController");
  140. setDaemon(true);
  141. setPriority(MAX_PRIORITY);
  142. _error = null;
  143. }
  144. @Override
  145. public void run()
  146. {
  147. try
  148. {
  149. for (;;)
  150. {
  151. int _oldTicks=_gameTicks; // save old ticks value to avoid moving objects 2x in same tick
  152. long runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now
  153. _gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)
  154. if (_oldTicks != _gameTicks) moveObjects(); // Runs possibly too often
  155. runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;
  156. // calculate sleep time... time needed to next tick minus time it takes to call moveObjects()
  157. int sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
  158. //_log.finest("TICK: "+_gameTicks);
  159. sleep(sleepTime);
  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. }