L2WorldRegion.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 net.sf.l2j.gameserver.model;
  16. import java.util.Iterator;
  17. import java.util.concurrent.ScheduledFuture;
  18. import java.util.logging.Logger;
  19. import javolution.util.FastList;
  20. import net.sf.l2j.Config;
  21. import net.sf.l2j.gameserver.ThreadPoolManager;
  22. import net.sf.l2j.gameserver.ai.L2AttackableAI;
  23. import net.sf.l2j.gameserver.datatables.SpawnTable;
  24. import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
  26. import net.sf.l2j.gameserver.model.zone.L2ZoneManager;
  27. import net.sf.l2j.gameserver.model.zone.L2ZoneType;
  28. import net.sf.l2j.gameserver.model.zone.type.L2DerbyTrackZone;
  29. import net.sf.l2j.gameserver.model.zone.type.L2TownZone;
  30. import net.sf.l2j.gameserver.taskmanager.KnownListUpdateTaskManager;
  31. import net.sf.l2j.util.L2ObjectSet;
  32. /**
  33. * This class ...
  34. *
  35. * @version $Revision: 1.3.4.4 $ $Date: 2005/03/27 15:29:33 $
  36. */
  37. public final class L2WorldRegion
  38. {
  39. private static Logger _log = Logger.getLogger(L2WorldRegion.class.getName());
  40. /** L2ObjectHashSet(L2PlayableInstance) containing L2PlayableInstance of all player & summon in game in this L2WorldRegion */
  41. private L2ObjectSet<L2PlayableInstance> _allPlayable;
  42. /** L2ObjectHashSet(L2Object) containing L2Object visible in this L2WorldRegion */
  43. private L2ObjectSet<L2Object> _visibleObjects;
  44. private FastList<L2WorldRegion> _surroundingRegions;
  45. private int _tileX, _tileY;
  46. private Boolean _active = false;
  47. private ScheduledFuture<?> _neighborsTask = null;
  48. private L2ZoneManager _zoneManager;
  49. public L2WorldRegion(int pTileX, int pTileY)
  50. {
  51. _allPlayable = L2ObjectSet.createL2PlayerSet(); //new L2ObjectHashSet<L2PcInstance>();
  52. _visibleObjects = L2ObjectSet.createL2ObjectSet(); // new L2ObjectHashSet<L2Object>();
  53. _surroundingRegions = new FastList<L2WorldRegion>();
  54. //_surroundingRegions.add(this); //done in L2World.initRegions()
  55. _tileX = pTileX;
  56. _tileY = pTileY;
  57. // default a newly initialized region to inactive, unless always on is specified
  58. if (Config.GRIDS_ALWAYS_ON)
  59. _active = true;
  60. else
  61. _active = false;
  62. }
  63. public void addZone(L2ZoneType zone)
  64. {
  65. if (_zoneManager == null)
  66. {
  67. _zoneManager = new L2ZoneManager();
  68. }
  69. _zoneManager.registerNewZone(zone);
  70. }
  71. public void removeZone(L2ZoneType zone)
  72. {
  73. if (_zoneManager == null)
  74. return;
  75. _zoneManager.unregisterZone(zone);
  76. }
  77. public void revalidateZones(L2Character character)
  78. {
  79. if (_zoneManager == null) return;
  80. // do NOT update the world region while the character is still in the process of teleporting
  81. // Once the teleport is COMPLETED, revalidation occurs safely, at that time.
  82. if (character.isTeleporting())
  83. return;
  84. if (_zoneManager != null)
  85. {
  86. _zoneManager.revalidateZones(character);
  87. }
  88. }
  89. public void removeFromZones(L2Character character)
  90. {
  91. if (_zoneManager == null) return;
  92. if (_zoneManager != null)
  93. {
  94. _zoneManager.removeCharacter(character);
  95. }
  96. }
  97. public boolean containsZone(int zoneId)
  98. {
  99. if (_zoneManager != null)
  100. {
  101. for (L2ZoneType e : _zoneManager.getZones())
  102. {
  103. if (e != null && e.getId() == zoneId)
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. return false;
  111. }
  112. public boolean checkEffectRangeInsidePeaceZone(L2Skill skill, final int x, final int y, final int z)
  113. {
  114. if (_zoneManager != null)
  115. {
  116. final int range = skill.getEffectRange();
  117. final int up = y + range;
  118. final int down = y - range;
  119. final int left = x + range;
  120. final int right = x - range;
  121. for (L2ZoneType e : _zoneManager.getZones())
  122. {
  123. if ((e instanceof L2TownZone && ((L2TownZone)e).isPeaceZone()) || (e instanceof L2DerbyTrackZone))
  124. {
  125. if (e.isInsideZone(x, up, z))
  126. return false;
  127. if (e.isInsideZone(x, down, z))
  128. return false;
  129. if (e.isInsideZone(left, y, z))
  130. return false;
  131. if (e.isInsideZone(right, y, z))
  132. return false;
  133. if (e.isInsideZone(x, y, z))
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. return true;
  140. }
  141. public void onDeath(L2Character character)
  142. {
  143. if (_zoneManager == null) return;
  144. if (_zoneManager != null)
  145. {
  146. _zoneManager.onDeath(character);
  147. }
  148. }
  149. public void onRevive(L2Character character)
  150. {
  151. if (_zoneManager == null) return;
  152. if (_zoneManager != null)
  153. {
  154. _zoneManager.onRevive(character);
  155. }
  156. }
  157. /** Task of AI notification */
  158. public class NeighborsTask implements Runnable
  159. {
  160. private boolean _isActivating;
  161. public NeighborsTask(boolean isActivating)
  162. {
  163. _isActivating = isActivating;
  164. }
  165. public void run()
  166. {
  167. if (_isActivating)
  168. {
  169. // for each neighbor, if it's not active, activate.
  170. for (L2WorldRegion neighbor: getSurroundingRegions())
  171. neighbor.setActive(true);
  172. }
  173. else
  174. {
  175. if(areNeighborsEmpty())
  176. setActive(false);
  177. // check and deactivate
  178. for (L2WorldRegion neighbor: getSurroundingRegions())
  179. if(neighbor.areNeighborsEmpty())
  180. neighbor.setActive(false);
  181. }
  182. }
  183. }
  184. private void switchAI(Boolean isOn)
  185. {
  186. int c = 0;
  187. if (!isOn)
  188. {
  189. for(L2Object o: _visibleObjects)
  190. {
  191. if (o instanceof L2Attackable)
  192. {
  193. c++;
  194. L2Attackable mob = (L2Attackable)o;
  195. // Set target to null and cancel Attack or Cast
  196. mob.setTarget(null);
  197. // Stop movement
  198. mob.stopMove(null);
  199. // Stop all active skills effects in progress on the L2Character
  200. mob.stopAllEffects();
  201. mob.clearAggroList();
  202. mob.getKnownList().removeAllKnownObjects();
  203. mob.getAI().setIntention(net.sf.l2j.gameserver.ai.CtrlIntention.AI_INTENTION_IDLE);
  204. // stop the ai tasks
  205. ((L2AttackableAI) mob.getAI()).stopAITask();
  206. // Stop HP/MP/CP Regeneration task
  207. // try this: allow regen, but only until mob is 100% full...then stop
  208. // it until the grid is made active.
  209. //mob.getStatus().stopHpMpRegeneration();
  210. }
  211. }
  212. _log.fine(c+ " mobs were turned off");
  213. }
  214. else
  215. {
  216. for(L2Object o: _visibleObjects)
  217. {
  218. if (o instanceof L2Attackable)
  219. {
  220. c++;
  221. // Start HP/MP/CP Regeneration task
  222. ((L2Attackable)o).getStatus().startHpMpRegeneration();
  223. // start the ai
  224. //((L2AttackableAI) mob.getAI()).startAITask();
  225. }
  226. else if (o instanceof L2NpcInstance)
  227. {
  228. // Create a RandomAnimation Task that will be launched after the calculated delay if the server allow it
  229. // L2Monsterinstance/L2Attackable socials are handled by AI (TODO: check the instances)
  230. ((L2NpcInstance)o).startRandomAnimationTimer();
  231. }
  232. }
  233. KnownListUpdateTaskManager.getInstance().updateRegion(this, true, false);
  234. _log.fine(c+ " mobs were turned on");
  235. }
  236. }
  237. public Boolean isActive()
  238. {
  239. return _active;
  240. }
  241. // check if all 9 neighbors (including self) are inactive or active but with no players.
  242. // returns true if the above condition is met.
  243. public Boolean areNeighborsEmpty()
  244. {
  245. // if this region is occupied, return false.
  246. if (isActive() && (_allPlayable.size() > 0 ))
  247. return false;
  248. // if any one of the neighbors is occupied, return false
  249. for (L2WorldRegion neighbor: _surroundingRegions)
  250. if (neighbor.isActive() && (neighbor._allPlayable.size() > 0))
  251. return false;
  252. // in all other cases, return true.
  253. return true;
  254. }
  255. /**
  256. * this function turns this region's AI and geodata on or off
  257. * @param value
  258. */
  259. public void setActive(boolean value)
  260. {
  261. if (_active == value)
  262. return;
  263. _active = value;
  264. // turn the AI on or off to match the region's activation.
  265. switchAI(value);
  266. // TODO
  267. // turn the geodata on or off to match the region's activation.
  268. if(value)
  269. _log.fine("Starting Grid " + _tileX + ","+ _tileY);
  270. else
  271. _log.fine("Stoping Grid " + _tileX + ","+ _tileY);
  272. }
  273. /** Immediately sets self as active and starts a timer to set neighbors as active
  274. * this timer is to avoid turning on neighbors in the case when a person just
  275. * teleported into a region and then teleported out immediately...there is no
  276. * reason to activate all the neighbors in that case.
  277. */
  278. private void startActivation()
  279. {
  280. // first set self to active and do self-tasks...
  281. setActive(true);
  282. // if the timer to deactivate neighbors is running, cancel it.
  283. if(_neighborsTask !=null)
  284. {
  285. _neighborsTask.cancel(true);
  286. _neighborsTask = null;
  287. }
  288. // then, set a timer to activate the neighbors
  289. _neighborsTask = ThreadPoolManager.getInstance().scheduleGeneral(new NeighborsTask(true), 1000*Config.GRID_NEIGHBOR_TURNON_TIME);
  290. }
  291. /** starts a timer to set neighbors (including self) as inactive
  292. * this timer is to avoid turning off neighbors in the case when a person just
  293. * moved out of a region that he may very soon return to. There is no reason
  294. * to turn self & neighbors off in that case.
  295. */
  296. private void startDeactivation()
  297. {
  298. // if the timer to activate neighbors is running, cancel it.
  299. if(_neighborsTask !=null)
  300. {
  301. _neighborsTask.cancel(true);
  302. _neighborsTask = null;
  303. }
  304. // start a timer to "suggest" a deactivate to self and neighbors.
  305. // suggest means: first check if a neighbor has L2PcInstances in it. If not, deactivate.
  306. _neighborsTask = ThreadPoolManager.getInstance().scheduleGeneral(new NeighborsTask(false), 1000*Config.GRID_NEIGHBOR_TURNOFF_TIME);
  307. }
  308. /**
  309. * Add the L2Object in the L2ObjectHashSet(L2Object) _visibleObjects containing L2Object visible in this L2WorldRegion <BR>
  310. * If L2Object is a L2PcInstance, Add the L2PcInstance in the L2ObjectHashSet(L2PcInstance) _allPlayable
  311. * containing L2PcInstance of all player in game in this L2WorldRegion <BR>
  312. * Assert : object.getCurrentWorldRegion() == this
  313. */
  314. public void addVisibleObject(L2Object object)
  315. {
  316. if (Config.ASSERT) assert object.getWorldRegion() == this;
  317. if (object == null) return;
  318. _visibleObjects.put(object);
  319. if (object instanceof L2PlayableInstance)
  320. {
  321. _allPlayable.put((L2PlayableInstance) object);
  322. // if this is the first player to enter the region, activate self & neighbors
  323. if ((_allPlayable.size() == 1) && (!Config.GRIDS_ALWAYS_ON))
  324. startActivation();
  325. }
  326. }
  327. /**
  328. * Remove the L2Object from the L2ObjectHashSet(L2Object) _visibleObjects in this L2WorldRegion <BR><BR>
  329. *
  330. * If L2Object is a L2PcInstance, remove it from the L2ObjectHashSet(L2PcInstance) _allPlayable of this L2WorldRegion <BR>
  331. * Assert : object.getCurrentWorldRegion() == this || object.getCurrentWorldRegion() == null
  332. */
  333. public void removeVisibleObject(L2Object object)
  334. {
  335. if (Config.ASSERT) assert object.getWorldRegion() == this || object.getWorldRegion() == null;
  336. if (object == null) return;
  337. _visibleObjects.remove(object);
  338. if (object instanceof L2PlayableInstance)
  339. {
  340. _allPlayable.remove((L2PlayableInstance) object);
  341. if ((_allPlayable.size() == 0 ) && (!Config.GRIDS_ALWAYS_ON))
  342. startDeactivation();
  343. }
  344. }
  345. public void addSurroundingRegion(L2WorldRegion region)
  346. {
  347. _surroundingRegions.add(region);
  348. }
  349. /**
  350. * Return the FastList _surroundingRegions containing all L2WorldRegion around the current L2WorldRegion
  351. */
  352. public FastList<L2WorldRegion> getSurroundingRegions()
  353. {
  354. //change to return L2WorldRegion[] ?
  355. //this should not change after initialization, so maybe changes are not necessary
  356. return _surroundingRegions;
  357. }
  358. public Iterator<L2PlayableInstance> iterateAllPlayers()
  359. {
  360. return _allPlayable.iterator();
  361. }
  362. public L2ObjectSet<L2Object> getVisibleObjects()
  363. {
  364. return _visibleObjects;
  365. }
  366. public L2ObjectSet<L2PlayableInstance> getVisiblePlayable()
  367. {
  368. return _allPlayable;
  369. }
  370. public String getName()
  371. {
  372. return "(" + _tileX + ", " + _tileY + ")";
  373. }
  374. /**
  375. * Deleted all spawns in the world.
  376. */
  377. public synchronized void deleteVisibleNpcSpawns()
  378. {
  379. _log.fine("Deleting all visible NPC's in Region: " + getName());
  380. for (L2Object obj : _visibleObjects)
  381. {
  382. if (obj instanceof L2NpcInstance)
  383. {
  384. L2NpcInstance target = (L2NpcInstance) obj;
  385. target.deleteMe();
  386. L2Spawn spawn = target.getSpawn();
  387. if (spawn != null)
  388. {
  389. spawn.stopRespawn();
  390. SpawnTable.getInstance().deleteSpawn(spawn, false);
  391. }
  392. _log.finest("Removed NPC " + target.getObjectId());
  393. }
  394. }
  395. _log.info("All visible NPC's deleted in Region: " + getName());
  396. }
  397. }