2
0

GameTimeController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.util.Calendar;
  21. import java.util.Set;
  22. import java.util.concurrent.ConcurrentHashMap;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. /**
  28. * Game Time controller class.
  29. * @author Forsaiken
  30. */
  31. public final class GameTimeController extends Thread
  32. {
  33. private static final Logger _log = LoggerFactory.getLogger(GameTimeController.class);
  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. public static final int IG_DAYS_PER_DAY = 6;
  37. public static final int MILLIS_PER_IG_DAY = (3600000 * 24) / IG_DAYS_PER_DAY;
  38. public static final int SECONDS_PER_IG_DAY = MILLIS_PER_IG_DAY / 1000;
  39. public static final int MINUTES_PER_IG_DAY = SECONDS_PER_IG_DAY / 60;
  40. public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
  41. public static final int TICKS_SUN_STATE_CHANGE = TICKS_PER_IG_DAY / 4;
  42. private static GameTimeController _instance;
  43. private final Set<L2Character> _movingObjects = ConcurrentHashMap.newKeySet();
  44. private final long _referenceTime;
  45. private GameTimeController()
  46. {
  47. super("GameTimeController");
  48. super.setDaemon(true);
  49. super.setPriority(MAX_PRIORITY);
  50. final Calendar c = Calendar.getInstance();
  51. c.set(Calendar.HOUR_OF_DAY, 0);
  52. c.set(Calendar.MINUTE, 0);
  53. c.set(Calendar.SECOND, 0);
  54. c.set(Calendar.MILLISECOND, 0);
  55. _referenceTime = c.getTimeInMillis();
  56. super.start();
  57. }
  58. public static final void init()
  59. {
  60. _instance = new GameTimeController();
  61. }
  62. public final int getGameTime()
  63. {
  64. return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
  65. }
  66. public final int getGameHour()
  67. {
  68. return getGameTime() / 60;
  69. }
  70. public final int getGameMinute()
  71. {
  72. return getGameTime() % 60;
  73. }
  74. public final boolean isNight()
  75. {
  76. return getGameHour() < 6;
  77. }
  78. /**
  79. * The true GameTime tick. Directly taken from current time. This represents the tick of the time.
  80. * @return
  81. */
  82. public final int getGameTicks()
  83. {
  84. return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
  85. }
  86. /**
  87. * Add a L2Character to movingObjects of GameTimeController.
  88. * @param cha The L2Character to add to movingObjects of GameTimeController
  89. */
  90. public final void registerMovingObject(final L2Character cha)
  91. {
  92. if (cha == null)
  93. {
  94. return;
  95. }
  96. _movingObjects.add(cha);
  97. }
  98. /**
  99. * Move all L2Characters contained in movingObjects of GameTimeController.<BR>
  100. * <B><U> Concept</U> :</B><BR>
  101. * All L2Character in movement are identified in <B>movingObjects</B> of GameTimeController.<BR>
  102. * <B><U> Actions</U> :</B><BR>
  103. * <ul>
  104. * <li>Update the position of each L2Character</li>
  105. * <li>If movement is finished, the L2Character is removed from movingObjects</li>
  106. * <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>
  107. * </ul>
  108. */
  109. private final void moveObjects()
  110. {
  111. _movingObjects.removeIf(L2Character::updatePosition);
  112. }
  113. public final void stopTimer()
  114. {
  115. super.interrupt();
  116. _log.info("Stopping {}", getClass().getSimpleName());
  117. }
  118. @Override
  119. public final void run()
  120. {
  121. _log.debug("{}: Started.", getClass().getSimpleName());
  122. long nextTickTime, sleepTime;
  123. boolean isNight = isNight();
  124. if (isNight)
  125. {
  126. ThreadPoolManager.getInstance().executeAi(() -> DayNightSpawnManager.getInstance().notifyChangeMode());
  127. }
  128. while (true)
  129. {
  130. nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
  131. try
  132. {
  133. moveObjects();
  134. }
  135. catch (final Throwable e)
  136. {
  137. _log.warn("Unable to move objects!", e);
  138. }
  139. sleepTime = nextTickTime - System.currentTimeMillis();
  140. if (sleepTime > 0)
  141. {
  142. try
  143. {
  144. Thread.sleep(sleepTime);
  145. }
  146. catch (final InterruptedException e)
  147. {
  148. }
  149. }
  150. if (isNight() != isNight)
  151. {
  152. isNight = !isNight;
  153. ThreadPoolManager.getInstance().executeAi(() -> DayNightSpawnManager.getInstance().notifyChangeMode());
  154. }
  155. }
  156. }
  157. public static final GameTimeController getInstance()
  158. {
  159. return _instance;
  160. }
  161. }