L2WorldRegion.java 12 KB

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