KnownListUpdateTaskManager.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 com.l2jserver.Config;
  19. import com.l2jserver.gameserver.ThreadPoolManager;
  20. import com.l2jserver.gameserver.model.L2Object;
  21. import com.l2jserver.gameserver.model.L2World;
  22. import com.l2jserver.gameserver.model.L2WorldRegion;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.actor.L2Playable;
  25. import com.l2jserver.gameserver.model.actor.instance.L2GuardInstance;
  26. public class KnownListUpdateTaskManager
  27. {
  28. protected static final Logger _log = Logger.getLogger(KnownListUpdateTaskManager.class.getName());
  29. private final static int FULL_UPDATE_TIMER = 100;
  30. private Object syncObject = new Object();
  31. public static boolean updatePass = true;
  32. // Do full update every FULL_UPDATE_TIMER * KNOWNLIST_UPDATE_INTERVAL
  33. public static int _fullUpdateTimer = FULL_UPDATE_TIMER;
  34. private KnownListUpdateTaskManager()
  35. {
  36. ThreadPoolManager.getInstance().scheduleAi(new KnownListUpdate(), 1000);
  37. }
  38. public static KnownListUpdateTaskManager getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. public Object getSync()
  43. {
  44. return syncObject;
  45. }
  46. private class KnownListUpdate implements Runnable
  47. {
  48. public KnownListUpdate()
  49. {
  50. }
  51. public void run()
  52. {
  53. try
  54. {
  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. if (r.isActive()) // and check only if the region is active
  63. {
  64. updateRegion(r, (_fullUpdateTimer == FULL_UPDATE_TIMER), updatePass);
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. _log.warning(e.toString());
  77. }
  78. updatePass = !updatePass;
  79. if (_fullUpdateTimer > 0)
  80. _fullUpdateTimer--;
  81. else
  82. _fullUpdateTimer = FULL_UPDATE_TIMER;
  83. ThreadPoolManager.getInstance().scheduleAi(new KnownListUpdate(), Config.KNOWNLIST_UPDATE_INTERVAL);
  84. }
  85. }
  86. public void updateRegion(L2WorldRegion region, boolean fullUpdate, boolean forgetObjects)
  87. {
  88. // synchronized (syncObject)
  89. {
  90. Collection<L2Object> vObj = region.getVisibleObjects().values();
  91. // synchronized (region.getVisibleObjects())
  92. {
  93. for (L2Object object : vObj) // and for all members in region
  94. {
  95. if (object == null || !object.isVisible())
  96. continue; // skip dying objects
  97. if (forgetObjects)
  98. {
  99. object.getKnownList().forgetObjects((object instanceof L2Playable
  100. || (Config.GUARD_ATTACK_AGGRO_MOB && object instanceof L2GuardInstance) || fullUpdate));
  101. continue;
  102. }
  103. if (object instanceof L2Playable || (Config.GUARD_ATTACK_AGGRO_MOB && object instanceof L2GuardInstance) || fullUpdate)
  104. {
  105. for (L2WorldRegion regi : region.getSurroundingRegions())
  106. {
  107. Collection<L2Object> inrObj = regi.getVisibleObjects().values();
  108. // synchronized (regi.getVisibleObjects())
  109. {
  110. for (L2Object _object : inrObj)
  111. if (_object != object)
  112. object.getKnownList().addKnownObject(_object);
  113. }
  114. }
  115. }
  116. else if (object instanceof L2Character)
  117. {
  118. for (L2WorldRegion regi : region.getSurroundingRegions())
  119. {
  120. Collection<L2Playable> inrPls = regi.getVisiblePlayable().values();
  121. // synchronized (regi.getVisiblePlayable())
  122. {
  123. if (regi.isActive())
  124. for (L2Object _object : inrPls)
  125. if (_object != object)
  126. object.getKnownList().addKnownObject(_object);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. @SuppressWarnings("synthetic-access")
  135. private static class SingletonHolder
  136. {
  137. protected static final KnownListUpdateTaskManager _instance = new KnownListUpdateTaskManager();
  138. }
  139. }