L2WorldRegion.java 14 KB

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