Advertisement
Deedlit

GameTimeController performance improvement

Dec 20th, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.87 KB | None | 0 0
  1. Index: java/com/l2jserver/gameserver/GameTimeController.java
  2. IDEA additional info:
  3. Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
  4. <+>UTF-8
  5. ===================================================================
  6. --- java/com/l2jserver/gameserver/GameTimeController.java   (revision 6306)
  7. +++ java/com/l2jserver/gameserver/GameTimeController.java   (revision )
  8. @@ -1,36 +1,36 @@
  9.  /*
  10.   * Copyright (C) 2004-2013 L2J Server
  11. - *
  12. + *
  13.   * This file is part of L2J Server.
  14. - *
  15. + *
  16.   * L2J Server is free software: you can redistribute it and/or modify
  17.   * it under the terms of the GNU General Public License as published by
  18.   * the Free Software Foundation, either version 3 of the License, or
  19.   * (at your option) any later version.
  20. - *
  21. + *
  22.   * L2J Server is distributed in the hope that it will be useful,
  23.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25.   * General Public License for more details.
  26. - *
  27. + *
  28.   * You should have received a copy of the GNU General Public License
  29.   * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30.   */
  31.  package com.l2jserver.gameserver;
  32.  
  33. -import java.util.Calendar;
  34. -import java.util.logging.Level;
  35. -import java.util.logging.Logger;
  36. -
  37. -import javolution.util.FastMap;
  38. -
  39.  import com.l2jserver.Config;
  40.  import com.l2jserver.gameserver.ai.CtrlEvent;
  41.  import com.l2jserver.gameserver.ai.L2CharacterAI;
  42.  import com.l2jserver.gameserver.instancemanager.DayNightSpawnManager;
  43.  import com.l2jserver.gameserver.model.actor.L2Character;
  44.  import com.l2jserver.util.StackTrace;
  45. +import javolution.util.FastMap;
  46.  
  47. +import java.util.Calendar;
  48. +import java.util.Iterator;
  49. +import java.util.logging.Level;
  50. +import java.util.logging.Logger;
  51. +
  52.  /**
  53.   * Game Time controller class.
  54.   * @author Unknown, Forsaiken
  55. @@ -38,7 +38,7 @@
  56.  public final class GameTimeController extends Thread
  57.  {
  58.     private static final Logger _log = Logger.getLogger(GameTimeController.class.getName());
  59. -  
  60. +
  61.     public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code
  62.     public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;
  63.     public static final int IG_DAYS_PER_DAY = 6;
  64. @@ -47,53 +47,53 @@
  65.     public static final int MINUTES_PER_IG_DAY = SECONDS_PER_IG_DAY / 60;
  66.     public static final int TICKS_PER_IG_DAY = SECONDS_PER_IG_DAY * TICKS_PER_SECOND;
  67.     public static final int TICKS_SUN_STATE_CHANGE = TICKS_PER_IG_DAY / 4;
  68. -  
  69. +
  70.     private static GameTimeController _instance;
  71. -  
  72. +
  73. -   private final FastMap<Integer, L2Character> _movingObjects = new FastMap<Integer, L2Character>().shared();
  74. +   private final FastMap<L2Character, Integer> _movingObjects = new FastMap<L2Character, Integer>().shared();
  75.     private final long _referenceTime;
  76. -  
  77. +
  78.     private GameTimeController()
  79.     {
  80.         super("GameTimeController");
  81.         super.setDaemon(true);
  82.         super.setPriority(MAX_PRIORITY);
  83. -      
  84. +
  85.         final Calendar c = Calendar.getInstance();
  86.         c.set(Calendar.HOUR_OF_DAY, 0);
  87.         c.set(Calendar.MINUTE, 0);
  88.         c.set(Calendar.SECOND, 0);
  89.         c.set(Calendar.MILLISECOND, 0);
  90.         _referenceTime = c.getTimeInMillis();
  91. -      
  92. +
  93.         super.start();
  94.     }
  95. -  
  96. +
  97.     public static final void init()
  98.     {
  99.         _instance = new GameTimeController();
  100.     }
  101. -  
  102. +
  103.     public final int getGameTime()
  104.     {
  105.         return (getGameTicks() % TICKS_PER_IG_DAY) / MILLIS_IN_TICK;
  106.     }
  107. -  
  108. +
  109.     public final int getGameHour()
  110.     {
  111.         return getGameTime() / 60;
  112.     }
  113. -  
  114. +
  115.     public final int getGameMinute()
  116.     {
  117.         return getGameTime() % 60;
  118.     }
  119. -  
  120. +
  121.     public final boolean isNight()
  122.     {
  123.         return getGameHour() < 6;
  124.     }
  125. -  
  126. +
  127.     /**
  128.      * The true GameTime tick. Directly taken from current time. This represents the tick of the time.
  129.      * @return
  130. @@ -102,7 +102,7 @@
  131.     {
  132.         return (int) ((System.currentTimeMillis() - _referenceTime) / MILLIS_IN_TICK);
  133.     }
  134. -  
  135. +
  136.     /**
  137.      * Add a L2Character to movingObjects of GameTimeController.
  138.      * @param cha The L2Character to add to movingObjects of GameTimeController
  139. @@ -113,10 +113,11 @@
  140.         {
  141.             return;
  142.         }
  143. -      
  144. +
  145. -       _movingObjects.putIfAbsent(cha.getObjectId(), cha);
  146. +       _movingObjects.put(cha, 0); // NOTE: Deedlit - probably no noticeable performance improvement here...
  147. +//     _movingObjects.putIfAbsent(cha, 0);
  148.     }
  149. -  
  150. +
  151.     /**
  152.      * Move all L2Characters contained in movingObjects of GameTimeController.<BR>
  153.      * <B><U> Concept</U> :</B><BR>
  154. @@ -130,20 +131,32 @@
  155.      */
  156.     private final void moveObjects()
  157.     {
  158. -       L2Character character;
  159. -       for (FastMap.Entry<Integer, L2Character> e = _movingObjects.head(), tail = _movingObjects.tail(); (e = e.getNext()) != tail;)
  160. +       final Iterator<L2Character> i = _movingObjects.keySet().iterator();
  161. +       L2Character cha;
  162. +       while (i.hasNext())
  163.         {
  164. -           character = e.getValue();
  165. -          
  166. -           if (character.updatePosition(getGameTicks()))
  167. +           cha = i.next();
  168. +           if (cha == null)
  169.             {
  170. -               // Destination reached. Remove from map and execute arrive event.
  171. -               _movingObjects.remove(e.getKey());
  172. -               fireCharacterArrived(character);
  173. +               i.remove();
  174. +               continue;
  175. +           }
  176. +           // If movement is finished, the L2Character is removed from movingObjects
  177. +           if (cha.updatePosition(getGameTicks()))
  178. +           {
  179. +               /**
  180. +                * NOTE: Deedlit - 2nd big performance improvement hint:
  181. +                * 1) Determine in {@link com.l2jserver.gameserver.model.actor.L2Character#moveToLocation(int, int, int, int)} if character is already moving (if so, do not call registerMovingObject())
  182. +                * 2) Remove registerMovingObject from {@link com.l2jserver.gameserver.model.actor.L2Character#moveToNextRoutePoint()}
  183. +                * 3) Place ai.notifyEvent(CtrlEvent.EVT_ARRIVED) in {@link com.l2jserver.gameserver.model.actor.L2Character#updatePosition(int)} and remove from {@link com.l2jserver.gameserver.GameTimeController#fireCharacterArrived(com.l2jserver.gameserver.model.actor.L2Character)}
  184. +                * 4) Determine if moving object should be removed from map, by checking {@link com.l2jserver.gameserver.model.actor.L2Character#isMoving()} (_move is always null when last move was completed or char stopped)
  185. +                */
  186. +               i.remove();
  187. +               fireCharacterArrived(cha);
  188.             }
  189.         }
  190.     }
  191. -  
  192. +
  193.     private final void fireCharacterArrived(final L2Character character)
  194.     {
  195.         final L2CharacterAI ai = character.getAI();
  196. @@ -151,7 +164,7 @@
  197.         {
  198.             return;
  199.         }
  200. -      
  201. +
  202.         ThreadPoolManager.getInstance().executeAi(new Runnable()
  203.         {
  204.             @Override
  205. @@ -163,7 +176,7 @@
  206.                     {
  207.                         character.getKnownList().findObjects();
  208.                     }
  209. -                  
  210. +
  211.                     ai.notifyEvent(CtrlEvent.EVT_ARRIVED);
  212.                 }
  213.                 catch (final Throwable e)
  214. @@ -173,21 +186,21 @@
  215.             }
  216.         });
  217.     }
  218. -  
  219. +
  220.     public final void stopTimer()
  221.     {
  222.         super.interrupt();
  223.         _log.log(Level.INFO, "Stopping " + getClass().getSimpleName());
  224.     }
  225. -  
  226. +
  227.     @Override
  228.     public final void run()
  229.     {
  230.         _log.log(Level.CONFIG, getClass().getSimpleName() + ": Started.");
  231. -      
  232. +
  233.         long nextTickTime, sleepTime;
  234.         boolean isNight = isNight();
  235. -      
  236. +
  237.         if (isNight)
  238.         {
  239.             ThreadPoolManager.getInstance().executeAi(new Runnable()
  240. @@ -199,11 +212,11 @@
  241.                 }
  242.             });
  243.         }
  244. -      
  245. +
  246.         while (true)
  247.         {
  248.             nextTickTime = ((System.currentTimeMillis() / MILLIS_IN_TICK) * MILLIS_IN_TICK) + 100;
  249. -          
  250. +
  251.             try
  252.             {
  253.                 moveObjects();
  254. @@ -212,7 +225,7 @@
  255.             {
  256.                 StackTrace.displayStackTraceInformation(e);
  257.             }
  258. -          
  259. +
  260.             sleepTime = nextTickTime - System.currentTimeMillis();
  261.             if (sleepTime > 0)
  262.             {
  263. @@ -222,14 +235,14 @@
  264.                 }
  265.                 catch (final InterruptedException e)
  266.                 {
  267. -                  
  268. +
  269.                 }
  270.             }
  271. -          
  272. +
  273.             if (isNight() != isNight)
  274.             {
  275.                 isNight = !isNight;
  276. -              
  277. +
  278.                 ThreadPoolManager.getInstance().executeAi(new Runnable()
  279.                 {
  280.                     @Override
  281. @@ -241,7 +254,7 @@
  282.             }
  283.         }
  284.     }
  285. -  
  286. +
  287.     public static final GameTimeController getInstance()
  288.     {
  289.         return _instance;
  290. \ No newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement