KnownListUpdateTaskManager.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.taskmanager;
  16. import java.util.Collection;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastSet;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.ThreadPoolManager;
  21. import com.l2jserver.gameserver.model.L2Object;
  22. import com.l2jserver.gameserver.model.L2World;
  23. import com.l2jserver.gameserver.model.L2WorldRegion;
  24. import com.l2jserver.gameserver.model.actor.L2Attackable;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Playable;
  27. import com.l2jserver.gameserver.model.actor.instance.L2GuardInstance;
  28. public class KnownListUpdateTaskManager
  29. {
  30. protected static final Logger _log = Logger.getLogger(KnownListUpdateTaskManager.class.getName());
  31. private final static int FULL_UPDATE_TIMER = 100;
  32. public static boolean updatePass = true;
  33. // Do full update every FULL_UPDATE_TIMER * KNOWNLIST_UPDATE_INTERVAL
  34. public static int _fullUpdateTimer = FULL_UPDATE_TIMER;
  35. private static final FastSet<L2WorldRegion> _failedRegions = new FastSet<L2WorldRegion>(1);
  36. private KnownListUpdateTaskManager()
  37. {
  38. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new KnownListUpdate(), 1000, Config.KNOWNLIST_UPDATE_INTERVAL);
  39. }
  40. public static KnownListUpdateTaskManager getInstance()
  41. {
  42. return SingletonHolder._instance;
  43. }
  44. private class KnownListUpdate implements Runnable
  45. {
  46. public KnownListUpdate()
  47. {
  48. }
  49. public void run()
  50. {
  51. try
  52. {
  53. boolean failed;
  54. for (L2WorldRegion regions[] : L2World.getInstance().getAllWorldRegions())
  55. {
  56. for (L2WorldRegion r : regions) // go through all world regions
  57. {
  58. // avoid stopping update if something went wrong in updateRegion()
  59. try
  60. {
  61. failed = _failedRegions.contains(r); // failed on last pass
  62. if (r.isActive()) // and check only if the region is active
  63. {
  64. updateRegion(r, (_fullUpdateTimer == FULL_UPDATE_TIMER || failed), updatePass);
  65. }
  66. if (failed)
  67. _failedRegions.remove(r); // if all ok, remove
  68. }
  69. catch (Exception e)
  70. {
  71. _log.info("KnownListUpdateTaskManager: updateRegion(" + _fullUpdateTimer + "," + updatePass + ") failed for region " + r.getName() + ". Full update scheduled. " + e);
  72. if (Config.DEBUG)
  73. e.printStackTrace();
  74. _failedRegions.add(r);
  75. }
  76. }
  77. }
  78. updatePass = !updatePass;
  79. if (_fullUpdateTimer > 0)
  80. _fullUpdateTimer--;
  81. else
  82. _fullUpdateTimer = FULL_UPDATE_TIMER;
  83. }
  84. catch (Exception e)
  85. {
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90. public void updateRegion(L2WorldRegion region, boolean fullUpdate, boolean forgetObjects)
  91. {
  92. // synchronized (syncObject)
  93. {
  94. Collection<L2Object> vObj = region.getVisibleObjects().values();
  95. // synchronized (region.getVisibleObjects())
  96. {
  97. for (L2Object object : vObj) // and for all members in region
  98. {
  99. if (object == null || !object.isVisible())
  100. continue; // skip dying objects
  101. // Some mobs need faster knownlist update
  102. final boolean aggro = (Config.GUARD_ATTACK_AGGRO_MOB && object instanceof L2GuardInstance)
  103. || (object instanceof L2Attackable && ((L2Attackable)object).getEnemyClan() != null);
  104. if (forgetObjects)
  105. {
  106. object.getKnownList().forgetObjects(aggro || fullUpdate);
  107. continue;
  108. }
  109. for (L2WorldRegion regi : region.getSurroundingRegions())
  110. {
  111. if (object instanceof L2Playable
  112. || (aggro && regi.isActive())
  113. || fullUpdate)
  114. {
  115. Collection<L2Object> inrObj = regi.getVisibleObjects().values();
  116. // synchronized (regi.getVisibleObjects())
  117. {
  118. for (L2Object _object : inrObj)
  119. if (_object != object)
  120. object.getKnownList().addKnownObject(_object);
  121. }
  122. }
  123. else if (object instanceof L2Character)
  124. {
  125. if (regi.isActive())
  126. {
  127. Collection<L2Playable> inrPls = regi.getVisiblePlayable().values();
  128. // synchronized (regi.getVisiblePlayable())
  129. {
  130. for (L2Object _object : inrPls)
  131. if (_object != object)
  132. object.getKnownList().addKnownObject(_object);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. @SuppressWarnings("synthetic-access")
  142. private static class SingletonHolder
  143. {
  144. protected static final KnownListUpdateTaskManager _instance = new KnownListUpdateTaskManager();
  145. }
  146. }