L2World.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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.Arrays;
  22. import java.util.Collection;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.gameserver.datatables.AdminTable;
  31. import com.l2jserver.gameserver.datatables.CharNameTable;
  32. import com.l2jserver.gameserver.model.actor.L2Playable;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  35. import com.l2jserver.util.StringUtil;
  36. public final class L2World
  37. {
  38. private static final Logger _log = Logger.getLogger(L2World.class.getName());
  39. /** Gracia border Flying objects not allowed to the east of it. */
  40. public static final int GRACIA_MAX_X = -166168;
  41. public static final int GRACIA_MAX_Z = 6105;
  42. public static final int GRACIA_MIN_Z = -895;
  43. /** Biteshift, defines number of regions note, shifting by 15 will result in regions corresponding to map tiles shifting by 12 divides one tile to 8x8 regions. */
  44. public static final int SHIFT_BY = 12;
  45. private static final int TILE_SIZE = 32768;
  46. /** Map dimensions */
  47. public static final int TILE_X_MIN = 11;
  48. public static final int TILE_Y_MIN = 10;
  49. public static final int TILE_X_MAX = 26;
  50. public static final int TILE_Y_MAX = 26;
  51. public static final int TILE_ZERO_COORD_X = 20;
  52. public static final int TILE_ZERO_COORD_Y = 18;
  53. public static final int MAP_MIN_X = (TILE_X_MIN - TILE_ZERO_COORD_X) * TILE_SIZE;
  54. public static final int MAP_MIN_Y = (TILE_Y_MIN - TILE_ZERO_COORD_Y) * TILE_SIZE;
  55. public static final int MAP_MAX_X = ((TILE_X_MAX - TILE_ZERO_COORD_X) + 1) * TILE_SIZE;
  56. public static final int MAP_MAX_Y = ((TILE_Y_MAX - TILE_ZERO_COORD_Y) + 1) * TILE_SIZE;
  57. /** calculated offset used so top left region is 0,0 */
  58. public static final int OFFSET_X = Math.abs(MAP_MIN_X >> SHIFT_BY);
  59. public static final int OFFSET_Y = Math.abs(MAP_MIN_Y >> SHIFT_BY);
  60. /** number of regions */
  61. private static final int REGIONS_X = (MAP_MAX_X >> SHIFT_BY) + OFFSET_X;
  62. private static final int REGIONS_Y = (MAP_MAX_Y >> SHIFT_BY) + OFFSET_Y;
  63. /** Map containing all the players in game. */
  64. private final Map<Integer, L2PcInstance> _allPlayers = new ConcurrentHashMap<>();
  65. /** Map containing all visible objects. */
  66. private final Map<Integer, L2Object> _allObjects = new ConcurrentHashMap<>();
  67. /** Map used for debug. */
  68. private final Map<Integer, String> _allObjectsDebug = new ConcurrentHashMap<>();
  69. /** Map with the pets instances and their owner ID. */
  70. private final Map<Integer, L2PetInstance> _petsInstance = new ConcurrentHashMap<>();
  71. private L2WorldRegion[][] _worldRegions;
  72. /** Constructor of L2World. */
  73. protected L2World()
  74. {
  75. initRegions();
  76. }
  77. /**
  78. * Adds an object to the world.<br>
  79. * <B><U>Example of use</U>:</B>
  80. * <ul>
  81. * <li>Withdraw an item from the warehouse, create an item</li>
  82. * <li>Spawn a L2Character (PC, NPC, Pet)</li>
  83. * </ul>
  84. * @param object
  85. */
  86. public void storeObject(L2Object object)
  87. {
  88. if (_allObjects.containsKey(object.getObjectId()))
  89. {
  90. _log.log(Level.WARNING, getClass().getSimpleName() + ": Current object: " + object + " already exist in OID map!");
  91. _log.log(Level.WARNING, StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  92. _log.log(Level.WARNING, getClass().getSimpleName() + ": Previous object: " + _allObjects.get(object.getObjectId()) + " already exist in OID map!");
  93. _log.log(Level.WARNING, _allObjectsDebug.get(object.getObjectId()));
  94. _log.log(Level.WARNING, "---------------------- End ---------------------");
  95. return;
  96. }
  97. _allObjects.put(object.getObjectId(), object);
  98. _allObjectsDebug.put(object.getObjectId(), StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  99. }
  100. /**
  101. * Removes an object from the world.<br>
  102. * <B><U>Example of use</U>:</B>
  103. * <ul>
  104. * <li>Delete item from inventory, transfer Item from inventory to warehouse</li>
  105. * <li>Crystallize item</li>
  106. * <li>Remove NPC/PC/Pet from the world</li>
  107. * </ul>
  108. * @param object the object to remove
  109. */
  110. public void removeObject(L2Object object)
  111. {
  112. _allObjects.remove(object.getObjectId());
  113. _allObjectsDebug.remove(object.getObjectId());
  114. }
  115. /**
  116. * <B><U> Example of use</U>:</B>
  117. * <ul>
  118. * <li>Client packets : Action, AttackRequest, RequestJoinParty, RequestJoinPledge...</li>
  119. * </ul>
  120. * @param objectId Identifier of the L2Object
  121. * @return the L2Object object that belongs to an ID or null if no object found.
  122. */
  123. public L2Object findObject(int objectId)
  124. {
  125. return _allObjects.get(objectId);
  126. }
  127. public Collection<L2Object> getVisibleObjects()
  128. {
  129. return _allObjects.values();
  130. }
  131. /**
  132. * Get the count of all visible objects in world.
  133. * @return count off all L2World objects
  134. */
  135. public int getVisibleObjectsCount()
  136. {
  137. return _allObjects.size();
  138. }
  139. public List<L2PcInstance> getAllGMs()
  140. {
  141. return AdminTable.getInstance().getAllGms(true);
  142. }
  143. public Collection<L2PcInstance> getPlayers()
  144. {
  145. return _allPlayers.values();
  146. }
  147. /**
  148. * Gets all players sorted by the given comparator.
  149. * @param comparator the comparator
  150. * @return the players sorted by the comparator
  151. */
  152. public L2PcInstance[] getPlayersSortedBy(Comparator<L2PcInstance> comparator)
  153. {
  154. final L2PcInstance[] players = _allPlayers.values().toArray(new L2PcInstance[_allPlayers.values().size()]);
  155. Arrays.sort(players, comparator);
  156. return players;
  157. }
  158. /**
  159. * Return how many players are online.
  160. * @return number of online players.
  161. */
  162. public int getAllPlayersCount()
  163. {
  164. return _allPlayers.size();
  165. }
  166. /**
  167. * <B>If you have access to player objectId use {@link #getPlayer(int playerObjId)}</B>
  168. * @param name Name of the player to get Instance
  169. * @return the player instance corresponding to the given name.
  170. */
  171. public L2PcInstance getPlayer(String name)
  172. {
  173. return getPlayer(CharNameTable.getInstance().getIdByName(name));
  174. }
  175. /**
  176. * @param objectId of the player to get Instance
  177. * @return the player instance corresponding to the given object ID.
  178. */
  179. public L2PcInstance getPlayer(int objectId)
  180. {
  181. return _allPlayers.get(objectId);
  182. }
  183. /**
  184. * @param ownerId ID of the owner
  185. * @return the pet instance from the given ownerId.
  186. */
  187. public L2PetInstance getPet(int ownerId)
  188. {
  189. return _petsInstance.get(ownerId);
  190. }
  191. /**
  192. * Add the given pet instance from the given ownerId.
  193. * @param ownerId ID of the owner
  194. * @param pet L2PetInstance of the pet
  195. * @return
  196. */
  197. public L2PetInstance addPet(int ownerId, L2PetInstance pet)
  198. {
  199. return _petsInstance.put(ownerId, pet);
  200. }
  201. /**
  202. * Remove the given pet instance.
  203. * @param ownerId ID of the owner
  204. */
  205. public void removePet(int ownerId)
  206. {
  207. _petsInstance.remove(ownerId);
  208. }
  209. /**
  210. * Remove the given pet instance.
  211. * @param pet the pet to remove
  212. */
  213. public void removePet(L2PetInstance pet)
  214. {
  215. _petsInstance.remove(pet.getOwner().getObjectId());
  216. }
  217. /**
  218. * Add a L2Object in the world. <B><U> Concept</U> :</B> L2Object (including L2PcInstance) are identified in <B>_visibleObjects</B> of his current L2WorldRegion and in <B>_knownObjects</B> of other surrounding L2Characters <BR>
  219. * L2PcInstance are identified in <B>_allPlayers</B> of L2World, in <B>_allPlayers</B> of his current L2WorldRegion and in <B>_knownPlayer</B> of other surrounding L2Characters <B><U> Actions</U> :</B> <li>Add the L2Object object in _allPlayers* of L2World</li> <li>Add the L2Object object in
  220. * _gmList** of GmListTable</li> <li>Add object in _knownObjects and _knownPlayer* of all surrounding L2WorldRegion L2Characters</li><BR>
  221. * <li>If object is a L2Character, add all surrounding L2Object in its _knownObjects and all surrounding L2PcInstance in its _knownPlayer</li><BR>
  222. * <I>* only if object is a L2PcInstance</I><BR>
  223. * <I>** only if object is a GM L2PcInstance</I> <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object in _visibleObjects and _allPlayers* of L2WorldRegion (need synchronisation)</B></FONT><BR>
  224. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object to _allObjects and _allPlayers* of L2World (need synchronisation)</B></FONT> <B><U> Example of use </U> :</B> <li>Drop an Item</li> <li>Spawn a L2Character</li> <li>Apply Death Penalty of a L2PcInstance</li>
  225. * @param object L2object to add in the world
  226. * @param newRegion L2WorldRegion in wich the object will be add (not used)
  227. */
  228. public void addVisibleObject(L2Object object, L2WorldRegion newRegion)
  229. {
  230. // TODO: this code should be obsoleted by protection in putObject func...
  231. if (object.isPlayer())
  232. {
  233. L2PcInstance player = object.getActingPlayer();
  234. if (!player.isTeleporting())
  235. {
  236. final L2PcInstance old = getPlayer(player.getObjectId());
  237. if (old != null)
  238. {
  239. _log.warning("Duplicate character!? Closing both characters (" + player.getName() + ")");
  240. player.logout();
  241. old.logout();
  242. return;
  243. }
  244. addPlayerToWorld(player);
  245. }
  246. }
  247. if (!newRegion.isActive())
  248. {
  249. return;
  250. }
  251. // Get all visible objects contained in the _visibleObjects of L2WorldRegions
  252. // in a circular area of 2000 units
  253. List<L2Object> visibles = getVisibleObjects(object, 2000);
  254. if (Config.DEBUG)
  255. {
  256. _log.finest("objects in range:" + visibles.size());
  257. }
  258. // tell the player about the surroundings
  259. // Go through the visible objects contained in the circular area
  260. for (L2Object visible : visibles)
  261. {
  262. if (visible == null)
  263. {
  264. continue;
  265. }
  266. // Add the object in L2ObjectHashSet(L2Object) _knownObjects of the visible L2Character according to conditions :
  267. // - L2Character is visible
  268. // - object is not already known
  269. // - object is in the watch distance
  270. // If L2Object is a L2PcInstance, add L2Object in L2ObjectHashSet(L2PcInstance) _knownPlayer of the visible L2Character
  271. visible.getKnownList().addKnownObject(object);
  272. // Add the visible L2Object in L2ObjectHashSet(L2Object) _knownObjects of the object according to conditions
  273. // If visible L2Object is a L2PcInstance, add visible L2Object in L2ObjectHashSet(L2PcInstance) _knownPlayer of the object
  274. object.getKnownList().addKnownObject(visible);
  275. }
  276. }
  277. /**
  278. * Adds the player to the world.
  279. * @param player the player to add
  280. */
  281. public void addPlayerToWorld(L2PcInstance player)
  282. {
  283. _allPlayers.put(player.getObjectId(), player);
  284. }
  285. /**
  286. * Remove the player from the world.
  287. * @param player the player to remove
  288. */
  289. public void removeFromAllPlayers(L2PcInstance player)
  290. {
  291. _allPlayers.remove(player.getObjectId());
  292. }
  293. /**
  294. * Remove a L2Object from the world. <B><U> Concept</U> :</B> L2Object (including L2PcInstance) are identified in <B>_visibleObjects</B> of his current L2WorldRegion and in <B>_knownObjects</B> of other surrounding L2Characters <BR>
  295. * L2PcInstance are identified in <B>_allPlayers</B> of L2World, in <B>_allPlayers</B> of his current L2WorldRegion and in <B>_knownPlayer</B> of other surrounding L2Characters <B><U> Actions</U> :</B> <li>Remove the L2Object object from _allPlayers* of L2World</li> <li>Remove the L2Object
  296. * object from _visibleObjects and _allPlayers* of L2WorldRegion</li> <li>Remove the L2Object object from _gmList** of GmListTable</li> <li>Remove object from _knownObjects and _knownPlayer* of all surrounding L2WorldRegion L2Characters</li><BR>
  297. * <li>If object is a L2Character, remove all L2Object from its _knownObjects and all L2PcInstance from its _knownPlayer</li> <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World</B></FONT> <I>* only if object is a L2PcInstance</I><BR>
  298. * <I>** only if object is a GM L2PcInstance</I> <B><U> Example of use </U> :</B> <li>Pickup an Item</li> <li>Decay a L2Character</li>
  299. * @param object L2object to remove from the world
  300. * @param oldRegion L2WorldRegion in which the object was before removing
  301. */
  302. public void removeVisibleObject(L2Object object, L2WorldRegion oldRegion)
  303. {
  304. if (object == null)
  305. {
  306. return;
  307. }
  308. if (oldRegion != null)
  309. {
  310. // Remove the object from the L2ObjectHashSet(L2Object) _visibleObjects of L2WorldRegion
  311. // If object is a L2PcInstance, remove it from the L2ObjectHashSet(L2PcInstance) _allPlayers of this L2WorldRegion
  312. oldRegion.removeVisibleObject(object);
  313. // Go through all surrounding L2WorldRegion L2Characters
  314. for (L2WorldRegion reg : oldRegion.getSurroundingRegions())
  315. {
  316. final Collection<L2Object> vObj = reg.getVisibleObjects().values();
  317. for (L2Object obj : vObj)
  318. {
  319. if (obj != null)
  320. {
  321. obj.getKnownList().removeKnownObject(object);
  322. }
  323. }
  324. }
  325. // If object is a L2Character :
  326. // Remove all L2Object from L2ObjectHashSet(L2Object) containing all L2Object detected by the L2Character
  327. // Remove all L2PcInstance from L2ObjectHashSet(L2PcInstance) containing all player ingame detected by the L2Character
  328. object.getKnownList().removeAllKnownObjects();
  329. // If selected L2Object is a L2PcIntance, remove it from L2ObjectHashSet(L2PcInstance) _allPlayers of L2World
  330. if (object.isPlayer())
  331. {
  332. final L2PcInstance player = object.getActingPlayer();
  333. if (!player.isTeleporting())
  334. {
  335. removeFromAllPlayers(player);
  336. }
  337. }
  338. }
  339. }
  340. /**
  341. * Return all visible objects of the L2WorldRegion object's and of its surrounding L2WorldRegion. <B><U> Concept</U> :</B> All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  342. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object <B><U> Example of use </U> :</B> <li>Find Close Objects for L2Character</li><BR>
  343. * @param object L2object that determine the current L2WorldRegion
  344. * @return
  345. */
  346. public List<L2Object> getVisibleObjects(L2Object object)
  347. {
  348. L2WorldRegion reg = object.getWorldRegion();
  349. if (reg == null)
  350. {
  351. return null;
  352. }
  353. // Create an FastList in order to contain all visible L2Object
  354. List<L2Object> result = new ArrayList<>();
  355. // Go through the FastList of region
  356. for (L2WorldRegion regi : reg.getSurroundingRegions())
  357. {
  358. // Go through visible objects of the selected region
  359. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  360. for (L2Object _object : vObj)
  361. {
  362. if ((_object == null) || _object.equals(object))
  363. {
  364. continue; // skip our own character
  365. }
  366. else if (!_object.isVisible())
  367. {
  368. continue; // skip dying objects
  369. }
  370. result.add(_object);
  371. }
  372. }
  373. return result;
  374. }
  375. /**
  376. * Return all visible objects of the L2WorldRegions in the circular area (radius) centered on the object. <B><U> Concept</U> :</B> All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  377. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object <B><U> Example of use </U> :</B> <li>Define the aggrolist of monster</li> <li>Define visible objects of a L2Object</li> <li>Skill :
  378. * Confusion...</li><BR>
  379. * @param object L2object that determine the center of the circular area
  380. * @param radius Radius of the circular area
  381. * @return
  382. */
  383. public List<L2Object> getVisibleObjects(L2Object object, int radius)
  384. {
  385. if ((object == null) || !object.isVisible())
  386. {
  387. return new ArrayList<>();
  388. }
  389. final int sqRadius = radius * radius;
  390. // Create an FastList in order to contain all visible L2Object
  391. List<L2Object> result = new ArrayList<>();
  392. // Go through the FastList of region
  393. for (L2WorldRegion regi : object.getWorldRegion().getSurroundingRegions())
  394. {
  395. // Go through visible objects of the selected region
  396. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  397. for (L2Object _object : vObj)
  398. {
  399. if ((_object == null) || _object.equals(object))
  400. {
  401. continue; // skip our own character
  402. }
  403. if (sqRadius > object.calculateDistance(_object, false, true))
  404. {
  405. result.add(_object);
  406. }
  407. }
  408. }
  409. return result;
  410. }
  411. /**
  412. * Return all visible objects of the L2WorldRegions in the spheric area (radius) centered on the object. <B><U> Concept</U> :</B> All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  413. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object <B><U> Example of use </U> :</B> <li>Define the target list of a skill</li> <li>Define the target list of a polearme attack</li>
  414. * @param object L2object that determine the center of the circular area
  415. * @param radius Radius of the spheric area
  416. * @return
  417. */
  418. public List<L2Object> getVisibleObjects3D(L2Object object, int radius)
  419. {
  420. if ((object == null) || !object.isVisible())
  421. {
  422. return new ArrayList<>();
  423. }
  424. final int sqRadius = radius * radius;
  425. // Create an FastList in order to contain all visible L2Object
  426. List<L2Object> result = new ArrayList<>();
  427. // Go through visible object of the selected region
  428. for (L2WorldRegion regi : object.getWorldRegion().getSurroundingRegions())
  429. {
  430. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  431. for (L2Object _object : vObj)
  432. {
  433. if ((_object == null) || _object.equals(object))
  434. {
  435. continue; // skip our own character
  436. }
  437. if (sqRadius > object.calculateDistance(_object, true, true))
  438. {
  439. result.add(_object);
  440. }
  441. }
  442. }
  443. return result;
  444. }
  445. /**
  446. * <B><U> Concept</U> :</B> All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  447. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object <B><U> Example of use </U> :</B> <li>Find Close Objects for L2Character</li><BR>
  448. * @param object L2object that determine the current L2WorldRegion
  449. * @return all visible players of the L2WorldRegion object's and of its surrounding L2WorldRegion.
  450. */
  451. public List<L2Playable> getVisiblePlayable(L2Object object)
  452. {
  453. L2WorldRegion reg = object.getWorldRegion();
  454. if (reg == null)
  455. {
  456. return null;
  457. }
  458. // Create an FastList in order to contain all visible L2Object
  459. List<L2Playable> result = new ArrayList<>();
  460. // Go through the FastList of region
  461. for (L2WorldRegion regi : reg.getSurroundingRegions())
  462. {
  463. // Create an Iterator to go through the visible L2Object of the L2WorldRegion
  464. Map<Integer, L2Playable> _allpls = regi.getVisiblePlayable();
  465. Collection<L2Playable> _playables = _allpls.values();
  466. // Go through visible object of the selected region
  467. for (L2Playable _object : _playables)
  468. {
  469. if ((_object == null) || _object.equals(object))
  470. {
  471. continue; // skip our own character
  472. }
  473. if (!_object.isVisible())
  474. {
  475. continue; // skip dying objects
  476. }
  477. result.add(_object);
  478. }
  479. }
  480. return result;
  481. }
  482. /**
  483. * Calculate the current L2WorldRegions of the object according to its position (x,y). <B><U> Example of use </U> :</B> <li>Set position of a new L2Object (drop, spawn...)</li> <li>Update position of a L2Object after a movement</li><BR>
  484. * @param point position of the object
  485. * @return
  486. */
  487. public L2WorldRegion getRegion(Location point)
  488. {
  489. return _worldRegions[(point.getX() >> SHIFT_BY) + OFFSET_X][(point.getY() >> SHIFT_BY) + OFFSET_Y];
  490. }
  491. public L2WorldRegion getRegion(int x, int y)
  492. {
  493. return _worldRegions[(x >> SHIFT_BY) + OFFSET_X][(y >> SHIFT_BY) + OFFSET_Y];
  494. }
  495. /**
  496. * Returns the whole 2d array containing the world regions used by ZoneData.java to setup zones inside the world regions
  497. * @return
  498. */
  499. public L2WorldRegion[][] getWorldRegions()
  500. {
  501. return _worldRegions;
  502. }
  503. /**
  504. * Check if the current L2WorldRegions of the object is valid according to its position (x,y). <B><U> Example of use </U> :</B> <li>Init L2WorldRegions</li><BR>
  505. * @param x X position of the object
  506. * @param y Y position of the object
  507. * @return True if the L2WorldRegion is valid
  508. */
  509. private boolean validRegion(int x, int y)
  510. {
  511. return ((x >= 0) && (x <= REGIONS_X) && (y >= 0) && (y <= REGIONS_Y));
  512. }
  513. /**
  514. * Initialize the world regions.
  515. */
  516. private void initRegions()
  517. {
  518. _worldRegions = new L2WorldRegion[REGIONS_X + 1][REGIONS_Y + 1];
  519. for (int i = 0; i <= REGIONS_X; i++)
  520. {
  521. for (int j = 0; j <= REGIONS_Y; j++)
  522. {
  523. _worldRegions[i][j] = new L2WorldRegion(i, j);
  524. }
  525. }
  526. for (int x = 0; x <= REGIONS_X; x++)
  527. {
  528. for (int y = 0; y <= REGIONS_Y; y++)
  529. {
  530. for (int a = -1; a <= 1; a++)
  531. {
  532. for (int b = -1; b <= 1; b++)
  533. {
  534. if (validRegion(x + a, y + b))
  535. {
  536. _worldRegions[x + a][y + b].addSurroundingRegion(_worldRegions[x][y]);
  537. }
  538. }
  539. }
  540. }
  541. }
  542. _log.info("L2World: (" + REGIONS_X + " by " + REGIONS_Y + ") World Region Grid set up.");
  543. }
  544. /**
  545. * Deleted all spawns in the world.
  546. */
  547. public void deleteVisibleNpcSpawns()
  548. {
  549. _log.info("Deleting all visible NPC's.");
  550. for (int i = 0; i <= REGIONS_X; i++)
  551. {
  552. for (int j = 0; j <= REGIONS_Y; j++)
  553. {
  554. _worldRegions[i][j].deleteVisibleNpcSpawns();
  555. }
  556. }
  557. _log.info("All visible NPC's deleted.");
  558. }
  559. /**
  560. * @return the current instance of L2World
  561. */
  562. public static L2World getInstance()
  563. {
  564. return SingletonHolder._instance;
  565. }
  566. private static class SingletonHolder
  567. {
  568. protected static final L2World _instance = new L2World();
  569. }
  570. }