KnownListUpdateTaskManager.java 4.9 KB

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