L2World.java 23 KB

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