L2WorldRegion.java 12 KB

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