KnownListUpdateTaskManager.java 5.1 KB

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