L2WorldRegion.java 12 KB

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