L2WorldRegion.java 14 KB

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