2
0

L2World.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 gnu.trove.map.hash.TIntObjectHashMap;
  17. import gnu.trove.procedure.TObjectProcedure;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.GmListTable;
  27. import com.l2jserver.gameserver.datatables.CharNameTable;
  28. import com.l2jserver.gameserver.model.actor.L2Playable;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  31. import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
  32. import com.l2jserver.gameserver.util.Point3D;
  33. import com.l2jserver.util.StringUtil;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.21.2.5.2.7 $ $Date: 2005/03/27 15:29:32 $
  38. */
  39. public final class L2World
  40. {
  41. private static Logger _log = Logger.getLogger(L2World.class.getName());
  42. /**
  43. * Gracia border
  44. * Flying objects not allowed to the east of it.
  45. */
  46. public static final int GRACIA_MAX_X = -166168;
  47. public static final int GRACIA_MAX_Z = 6105;
  48. public static final int GRACIA_MIN_Z = -895;
  49. /*
  50. * biteshift, defines number of regions
  51. * note, shifting by 15 will result in regions corresponding to map tiles
  52. * shifting by 12 divides one tile to 8x8 regions
  53. */
  54. public static final int SHIFT_BY = 12;
  55. private static final int TILE_SIZE = 32768;
  56. /** Map dimensions */
  57. public static final int MAP_MIN_X = (Config.WORLD_X_MIN - 20) * TILE_SIZE;
  58. public static final int MAP_MAX_X = (Config.WORLD_X_MAX - 19) * TILE_SIZE;
  59. public static final int MAP_MIN_Y = (Config.WORLD_Y_MIN - 18) * TILE_SIZE;
  60. public static final int MAP_MAX_Y = (Config.WORLD_Y_MAX - 17) * TILE_SIZE;
  61. /** calculated offset used so top left region is 0,0 */
  62. public static final int OFFSET_X = Math.abs(MAP_MIN_X >> SHIFT_BY);
  63. public static final int OFFSET_Y = Math.abs(MAP_MIN_Y >> SHIFT_BY);
  64. /** number of regions */
  65. private static final int REGIONS_X = (MAP_MAX_X >> SHIFT_BY) + OFFSET_X;
  66. private static final int REGIONS_Y = (MAP_MAX_Y >> SHIFT_BY) + OFFSET_Y;
  67. //private FastMap<String, L2PcInstance> _allGms;
  68. /** HashMap(Integer Player id, L2PcInstance) containing all the players in game */
  69. private final L2TIntObjectHashMap<L2PcInstance> _allPlayers;
  70. /** L2ObjectHashMap(L2Object) containing all visible objects */
  71. private final L2TIntObjectHashMap<L2Object> _allObjects;
  72. private final L2TIntObjectHashMap<String> _allObjectsDebug;
  73. /** List with the pets instances and their owner id */
  74. private final L2TIntObjectHashMap<L2PetInstance> _petsInstance;
  75. private L2WorldRegion[][] _worldRegions;
  76. /**
  77. * Constructor of L2World.<BR><BR>
  78. */
  79. private L2World()
  80. {
  81. _allPlayers = new L2TIntObjectHashMap<L2PcInstance>();
  82. _allObjects = new L2TIntObjectHashMap<L2Object>();
  83. _allObjectsDebug = new L2TIntObjectHashMap<String>();
  84. _petsInstance = new L2TIntObjectHashMap<L2PetInstance>();
  85. initRegions();
  86. }
  87. /**
  88. * @return the current instance of L2World.
  89. */
  90. public static L2World getInstance()
  91. {
  92. return SingletonHolder._instance;
  93. }
  94. /**
  95. * Add L2Object object in _allObjects.<BR><BR>
  96. *
  97. * <B><U> Example of use </U> :</B><BR><BR>
  98. * <li> Withdraw an item from the warehouse, create an item</li>
  99. * <li> Spawn a L2Character (PC, NPC, Pet)</li><BR>
  100. * @param object
  101. */
  102. public void storeObject(L2Object object)
  103. {
  104. if (_allObjects.containsKey(object.getObjectId()))
  105. {
  106. if (Config.DEBUG)
  107. {
  108. _log.log(Level.WARNING, "--------[L2World] object: " + object + " already exist in OID map!--------");
  109. _log.log(Level.WARNING, "New object: " + StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  110. _log.log(Level.WARNING, "----------------- Previous Put -----------------");
  111. _log.log(Level.WARNING, "Previous: " + _allObjectsDebug.get(object.getObjectId()));
  112. _log.log(Level.WARNING, "---------------------- End ---------------------");
  113. return;
  114. }
  115. }
  116. _allObjects.put(object.getObjectId(), object);
  117. _allObjectsDebug.put(object.getObjectId(), StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  118. }
  119. public long timeStoreObject(L2Object object)
  120. {
  121. long time = System.nanoTime();
  122. _allObjects.put(object.getObjectId(), object);
  123. _allObjectsDebug.put(object.getObjectId(), StringUtil.getTraceString(Thread.currentThread().getStackTrace()));
  124. time = System.nanoTime() - time;
  125. return time;
  126. }
  127. /**
  128. * Remove L2Object object from _allObjects of L2World.<BR><BR>
  129. *
  130. * <B><U> Example of use </U> :</B><BR><BR>
  131. * <li> Delete item from inventory, tranfer Item from inventory to warehouse</li>
  132. * <li> Crystallize item</li>
  133. * <li> Remove NPC/PC/Pet from the world</li><BR>
  134. *
  135. * @param object L2Object to remove from _allObjects of L2World
  136. *
  137. */
  138. public void removeObject(L2Object object)
  139. {
  140. _allObjects.remove(object.getObjectId()); // suggestion by whatev
  141. //IdFactory.getInstance().releaseId(object.getObjectId());
  142. }
  143. public void removeObjects(List<L2Object> list)
  144. {
  145. for (L2Object o : list)
  146. {
  147. if (o != null)
  148. _allObjects.remove(o.getObjectId()); // suggestion by whatev
  149. }
  150. //IdFactory.getInstance().releaseId(object.getObjectId());
  151. }
  152. public void removeObjects(L2Object[] objects)
  153. {
  154. for (L2Object o : objects)
  155. _allObjects.remove(o.getObjectId()); // suggestion by whatev
  156. //IdFactory.getInstance().releaseId(object.getObjectId());
  157. }
  158. public long timeRemoveObject(L2Object object)
  159. {
  160. long time = System.nanoTime();
  161. _allObjects.remove(object.getObjectId());
  162. time = System.nanoTime() - time;
  163. return time;
  164. }
  165. /**
  166. * <B><U> Example of use </U> :</B><BR><BR>
  167. * <li> Client packets : Action, AttackRequest, RequestJoinParty, RequestJoinPledge...</li><BR>
  168. *
  169. * @param oID Identifier of the L2Object
  170. * @return the L2Object object that belongs to an ID or null if no object found.
  171. */
  172. public L2Object findObject(int oID)
  173. {
  174. return _allObjects.get(oID);
  175. }
  176. public long timeFindObject(int objectID)
  177. {
  178. long time = System.nanoTime();
  179. _allObjects.get(objectID);
  180. time = System.nanoTime() - time;
  181. return time;
  182. }
  183. /**
  184. * Added by Tempy - 08 Aug 05
  185. * @return retrieval of all visible objects in world.
  186. * @deprecated do not use that function, its unsafe!
  187. */
  188. @Deprecated
  189. public final TIntObjectHashMap<L2Object> getAllVisibleObjects()
  190. {
  191. return _allObjects;
  192. }
  193. public final L2Object[] getAllVisibleObjectsArray()
  194. {
  195. return _allObjects.values(new L2Object[0]);
  196. }
  197. public final boolean forEachObject(final TObjectProcedure<L2Object> proc)
  198. {
  199. return _allObjects.forEachValue(proc);
  200. }
  201. /**
  202. * Get the count of all visible objects in world.<br><br>
  203. *
  204. * @return count off all L2World objects
  205. */
  206. public final int getAllVisibleObjectsCount()
  207. {
  208. return _allObjects.size();
  209. }
  210. /**
  211. * @return a table containing all GMs.
  212. */
  213. public FastList<L2PcInstance> getAllGMs()
  214. {
  215. return GmListTable.getInstance().getAllGms(true);
  216. }
  217. public L2TIntObjectHashMap<L2PcInstance> getAllPlayers()
  218. {
  219. return _allPlayers;
  220. }
  221. public final L2PcInstance[] getAllPlayersArray()
  222. {
  223. return _allPlayers.values(new L2PcInstance[0]);
  224. }
  225. public final boolean forEachPlayer(final TObjectProcedure<L2PcInstance> proc)
  226. {
  227. return _allPlayers.forEachValue(proc);
  228. }
  229. /**
  230. * Return how many players are online.<BR><BR>
  231. *
  232. * @return number of online players.
  233. */
  234. public int getAllPlayersCount()
  235. {
  236. return _allPlayers.size();
  237. }
  238. /**
  239. * <B>If you have access to player objectId use {@link #getPlayer(int playerObjId)}</B>
  240. * @param name Name of the player to get Instance
  241. * @return the player instance corresponding to the given name.
  242. */
  243. public L2PcInstance getPlayer(String name)
  244. {
  245. return getPlayer(CharNameTable.getInstance().getIdByName(name));
  246. }
  247. /**
  248. * @param playerObjId Object ID of the player to get Instance
  249. * @return the player instance corresponding to the given object ID.
  250. */
  251. public L2PcInstance getPlayer(int playerObjId)
  252. {
  253. return _allPlayers.get(playerObjId);
  254. }
  255. /**
  256. * @param ownerId ID of the owner
  257. * @return the pet instance from the given ownerId.
  258. */
  259. public L2PetInstance getPet(int ownerId)
  260. {
  261. return _petsInstance.get(ownerId);
  262. }
  263. /**
  264. * Add the given pet instance from the given ownerId.<BR><BR>
  265. *
  266. * @param ownerId ID of the owner
  267. * @param pet L2PetInstance of the pet
  268. * @return
  269. */
  270. public L2PetInstance addPet(int ownerId, L2PetInstance pet)
  271. {
  272. return _petsInstance.put(ownerId, pet);
  273. }
  274. /**
  275. * Remove the given pet instance.<BR><BR>
  276. *
  277. * @param ownerId ID of the owner
  278. */
  279. public void removePet(int ownerId)
  280. {
  281. _petsInstance.remove(ownerId);
  282. }
  283. /**
  284. * Remove the given pet instance.<BR><BR>
  285. *
  286. * @param pet the pet to remove
  287. */
  288. public void removePet(L2PetInstance pet)
  289. {
  290. _petsInstance.remove(pet.getOwner().getObjectId());
  291. }
  292. /**
  293. * Add a L2Object in the world.<BR><BR>
  294. *
  295. * <B><U> Concept</U> :</B><BR><BR>
  296. * L2Object (including L2PcInstance) are identified in <B>_visibleObjects</B> of his current L2WorldRegion and in <B>_knownObjects</B> of other surrounding L2Characters <BR>
  297. * 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 <BR><BR>
  298. *
  299. * <B><U> Actions</U> :</B><BR><BR>
  300. * <li>Add the L2Object object in _allPlayers* of L2World </li>
  301. * <li>Add the L2Object object in _gmList** of GmListTable </li>
  302. * <li>Add object in _knownObjects and _knownPlayer* of all surrounding L2WorldRegion L2Characters </li><BR>
  303. *
  304. * <li>If object is a L2Character, add all surrounding L2Object in its _knownObjects and all surrounding L2PcInstance in its _knownPlayer </li><BR>
  305. *
  306. * <I>* only if object is a L2PcInstance</I><BR>
  307. * <I>** only if object is a GM L2PcInstance</I><BR><BR>
  308. *
  309. * <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>
  310. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T ADD the object to _allObjects and _allPlayers* of L2World (need synchronisation)</B></FONT><BR><BR>
  311. *
  312. * <B><U> Example of use </U> :</B><BR><BR>
  313. * <li> Drop an Item </li>
  314. * <li> Spawn a L2Character</li>
  315. * <li> Apply Death Penalty of a L2PcInstance </li><BR><BR>
  316. *
  317. * @param object L2object to add in the world
  318. * @param newRegion L2WorldRegion in wich the object will be add (not used)
  319. */
  320. public void addVisibleObject(L2Object object, L2WorldRegion newRegion)
  321. {
  322. // If selected L2Object is a L2PcIntance, add it in L2ObjectHashSet(L2PcInstance) _allPlayers of L2World
  323. // XXX TODO: this code should be obsoleted by protection in putObject func...
  324. if (object instanceof L2PcInstance)
  325. {
  326. L2PcInstance player = (L2PcInstance) object;
  327. if (!player.isTeleporting())
  328. {
  329. L2PcInstance tmp = getPlayer(player.getObjectId());
  330. if (tmp != null)
  331. {
  332. _log.warning("Duplicate character!? Closing both characters (" + player.getName() + ")");
  333. player.logout();
  334. tmp.logout();
  335. return;
  336. }
  337. addToAllPlayers(player);
  338. }
  339. }
  340. if (!newRegion.isActive())
  341. return;
  342. // Get all visible objects contained in the _visibleObjects of L2WorldRegions
  343. // in a circular area of 2000 units
  344. List<L2Object> visibles = getVisibleObjects(object, 2000);
  345. if (Config.DEBUG)
  346. _log.finest("objects in range:" + visibles.size());
  347. // tell the player about the surroundings
  348. // Go through the visible objects contained in the circular area
  349. for (L2Object visible : visibles)
  350. {
  351. if (visible == null)
  352. continue;
  353. // Add the object in L2ObjectHashSet(L2Object) _knownObjects of the visible L2Character according to conditions :
  354. // - L2Character is visible
  355. // - object is not already known
  356. // - object is in the watch distance
  357. // If L2Object is a L2PcInstance, add L2Object in L2ObjectHashSet(L2PcInstance) _knownPlayer of the visible L2Character
  358. visible.getKnownList().addKnownObject(object);
  359. // Add the visible L2Object in L2ObjectHashSet(L2Object) _knownObjects of the object according to conditions
  360. // If visible L2Object is a L2PcInstance, add visible L2Object in L2ObjectHashSet(L2PcInstance) _knownPlayer of the object
  361. object.getKnownList().addKnownObject(visible);
  362. }
  363. }
  364. /**
  365. * Add the L2PcInstance to _allPlayers of L2World.<BR><BR>
  366. * @param cha
  367. */
  368. public void addToAllPlayers(L2PcInstance cha)
  369. {
  370. _allPlayers.put(cha.getObjectId(), cha);
  371. }
  372. /**
  373. * Remove the L2PcInstance from _allPlayers of L2World.<BR><BR>
  374. *
  375. * <B><U> Example of use </U> :</B><BR><BR>
  376. * <li> Remove a player from the visible objects </li><BR>
  377. * @param cha
  378. */
  379. public void removeFromAllPlayers(L2PcInstance cha)
  380. {
  381. _allPlayers.remove(cha.getObjectId());
  382. }
  383. /**
  384. * Remove a L2Object from the world.<BR><BR>
  385. *
  386. * <B><U> Concept</U> :</B><BR><BR>
  387. * L2Object (including L2PcInstance) are identified in <B>_visibleObjects</B> of his current L2WorldRegion and in <B>_knownObjects</B> of other surrounding L2Characters <BR>
  388. * 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 <BR><BR>
  389. *
  390. * <B><U> Actions</U> :</B><BR><BR>
  391. * <li>Remove the L2Object object from _allPlayers* of L2World </li>
  392. * <li>Remove the L2Object object from _visibleObjects and _allPlayers* of L2WorldRegion </li>
  393. * <li>Remove the L2Object object from _gmList** of GmListTable </li>
  394. * <li>Remove object from _knownObjects and _knownPlayer* of all surrounding L2WorldRegion L2Characters </li><BR>
  395. *
  396. * <li>If object is a L2Character, remove all L2Object from its _knownObjects and all L2PcInstance from its _knownPlayer </li><BR><BR>
  397. *
  398. * <FONT COLOR=#FF0000><B> <U>Caution</U> : This method DOESN'T REMOVE the object from _allObjects of L2World</B></FONT><BR><BR>
  399. *
  400. * <I>* only if object is a L2PcInstance</I><BR>
  401. * <I>** only if object is a GM L2PcInstance</I><BR><BR>
  402. *
  403. * <B><U> Example of use </U> :</B><BR><BR>
  404. * <li> Pickup an Item </li>
  405. * <li> Decay a L2Character</li><BR><BR>
  406. *
  407. * @param object L2object to remove from the world
  408. * @param oldRegion L2WorldRegion in which the object was before removing
  409. *
  410. */
  411. public void removeVisibleObject(L2Object object, L2WorldRegion oldRegion)
  412. {
  413. if (object == null)
  414. return;
  415. //removeObject(object);
  416. if (oldRegion != null)
  417. {
  418. // Remove the object from the L2ObjectHashSet(L2Object) _visibleObjects of L2WorldRegion
  419. // If object is a L2PcInstance, remove it from the L2ObjectHashSet(L2PcInstance) _allPlayers of this L2WorldRegion
  420. oldRegion.removeVisibleObject(object);
  421. // Go through all surrounding L2WorldRegion L2Characters
  422. for (L2WorldRegion reg : oldRegion.getSurroundingRegions())
  423. {
  424. Collection<L2Object> vObj = reg.getVisibleObjects().values();
  425. for (L2Object obj : vObj)
  426. {
  427. if (obj != null)
  428. {
  429. obj.getKnownList().removeKnownObject(object);
  430. object.getKnownList().removeKnownObject(obj);
  431. }
  432. }
  433. }
  434. // If object is a L2Character :
  435. // Remove all L2Object from L2ObjectHashSet(L2Object) containing all L2Object detected by the L2Character
  436. // Remove all L2PcInstance from L2ObjectHashSet(L2PcInstance) containing all player ingame detected by the L2Character
  437. object.getKnownList().removeAllKnownObjects();
  438. // If selected L2Object is a L2PcIntance, remove it from L2ObjectHashSet(L2PcInstance) _allPlayers of L2World
  439. if (object instanceof L2PcInstance)
  440. {
  441. if (!((L2PcInstance) object).isTeleporting())
  442. removeFromAllPlayers((L2PcInstance) object);
  443. // If selected L2Object is a GM L2PcInstance, remove it from Set(L2PcInstance) _gmList of GmListTable
  444. //if (((L2PcInstance)object).isGM())
  445. //GmListTable.getInstance().deleteGm((L2PcInstance)object);
  446. }
  447. }
  448. }
  449. /**
  450. * Return all visible objects of the L2WorldRegion object's and of its surrounding L2WorldRegion.<BR><BR>
  451. *
  452. * <B><U> Concept</U> :</B><BR><BR>
  453. * All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  454. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object<BR><BR>
  455. *
  456. * <B><U> Example of use </U> :</B><BR><BR>
  457. * <li> Find Close Objects for L2Character </li><BR>
  458. *
  459. * @param object L2object that determine the current L2WorldRegion
  460. * @return
  461. */
  462. public List<L2Object> getVisibleObjects(L2Object object)
  463. {
  464. L2WorldRegion reg = object.getWorldRegion();
  465. if (reg == null)
  466. return null;
  467. // Create an FastList in order to contain all visible L2Object
  468. List<L2Object> result = new ArrayList<L2Object>();
  469. // Go through the FastList of region
  470. for (L2WorldRegion regi : reg.getSurroundingRegions())
  471. {
  472. // Go through visible objects of the selected region
  473. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  474. for (L2Object _object : vObj)
  475. {
  476. if (_object == null || _object.equals(object))
  477. continue; // skip our own character
  478. else if (!_object.isVisible())
  479. continue; // skip dying objects
  480. result.add(_object);
  481. }
  482. }
  483. return result;
  484. }
  485. /**
  486. * Return all visible objects of the L2WorldRegions in the circular area (radius) centered on the object.<BR><BR>
  487. *
  488. * <B><U> Concept</U> :</B><BR><BR>
  489. * All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  490. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object<BR><BR>
  491. *
  492. * <B><U> Example of use </U> :</B><BR><BR>
  493. * <li> Define the aggrolist of monster </li>
  494. * <li> Define visible objects of a L2Object </li>
  495. * <li> Skill : Confusion... </li><BR>
  496. *
  497. * @param object L2object that determine the center of the circular area
  498. * @param radius Radius of the circular area
  499. * @return
  500. */
  501. public List<L2Object> getVisibleObjects(L2Object object, int radius)
  502. {
  503. if (object == null || !object.isVisible())
  504. return new ArrayList<L2Object>();
  505. int x = object.getX();
  506. int y = object.getY();
  507. int sqRadius = radius * radius;
  508. // Create an FastList in order to contain all visible L2Object
  509. List<L2Object> result = new ArrayList<L2Object>();
  510. // Go through the FastList of region
  511. for (L2WorldRegion regi : object.getWorldRegion().getSurroundingRegions())
  512. {
  513. // Go through visible objects of the selected region
  514. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  515. for (L2Object _object : vObj)
  516. {
  517. if (_object == null || _object.equals(object))
  518. continue; // skip our own character
  519. int x1 = _object.getX();
  520. int y1 = _object.getY();
  521. double dx = x1 - x;
  522. double dy = y1 - y;
  523. if (dx * dx + dy * dy < sqRadius)
  524. result.add(_object);
  525. }
  526. }
  527. return result;
  528. }
  529. /**
  530. * Return all visible objects of the L2WorldRegions in the spheric area (radius) centered on the object.<BR><BR>
  531. *
  532. * <B><U> Concept</U> :</B><BR><BR>
  533. * All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  534. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object<BR><BR>
  535. *
  536. * <B><U> Example of use </U> :</B><BR><BR>
  537. * <li> Define the target list of a skill </li>
  538. * <li> Define the target list of a polearme attack </li><BR><BR>
  539. *
  540. * @param object L2object that determine the center of the circular area
  541. * @param radius Radius of the spheric area
  542. * @return
  543. */
  544. public List<L2Object> getVisibleObjects3D(L2Object object, int radius)
  545. {
  546. if (object == null || !object.isVisible())
  547. return new ArrayList<L2Object>();
  548. int x = object.getX();
  549. int y = object.getY();
  550. int z = object.getZ();
  551. int sqRadius = radius * radius;
  552. // Create an FastList in order to contain all visible L2Object
  553. List<L2Object> result = new ArrayList<L2Object>();
  554. // Go through visible object of the selected region
  555. for (L2WorldRegion regi : object.getWorldRegion().getSurroundingRegions())
  556. {
  557. Collection<L2Object> vObj = regi.getVisibleObjects().values();
  558. for (L2Object _object : vObj)
  559. {
  560. if (_object == null || _object.equals(object))
  561. continue; // skip our own character
  562. int x1 = _object.getX();
  563. int y1 = _object.getY();
  564. int z1 = _object.getZ();
  565. long dx = x1 - x;
  566. long dy = y1 - y;
  567. long dz = z1 - z;
  568. if (dx * dx + dy * dy + dz * dz < sqRadius)
  569. result.add(_object);
  570. }
  571. }
  572. return result;
  573. }
  574. /**
  575. * <B><U> Concept</U> :</B><BR><BR>
  576. * All visible object are identified in <B>_visibleObjects</B> of their current L2WorldRegion <BR>
  577. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object<BR><BR>
  578. *
  579. * <B><U> Example of use </U> :</B><BR><BR>
  580. * <li> Find Close Objects for L2Character </li><BR>
  581. *
  582. * @param object L2object that determine the current L2WorldRegion
  583. * @return all visible players of the L2WorldRegion object's and of its surrounding L2WorldRegion.
  584. */
  585. public List<L2Playable> getVisiblePlayable(L2Object object)
  586. {
  587. L2WorldRegion reg = object.getWorldRegion();
  588. if (reg == null)
  589. return null;
  590. // Create an FastList in order to contain all visible L2Object
  591. List<L2Playable> result = new ArrayList<L2Playable>();
  592. // Go through the FastList of region
  593. for (L2WorldRegion regi : reg.getSurroundingRegions())
  594. {
  595. // Create an Iterator to go through the visible L2Object of the L2WorldRegion
  596. Map<Integer, L2Playable> _allpls = regi.getVisiblePlayable();
  597. Collection<L2Playable> _playables = _allpls.values();
  598. // Go through visible object of the selected region
  599. for (L2Playable _object : _playables)
  600. {
  601. if (_object == null || _object.equals(object))
  602. continue; // skip our own character
  603. if (!_object.isVisible()) // GM invisible is different than this...
  604. continue; // skip dying objects
  605. result.add(_object);
  606. }
  607. }
  608. return result;
  609. }
  610. /**
  611. * Calculate the current L2WorldRegions of the object according to its position (x,y).<BR><BR>
  612. *
  613. * <B><U> Example of use </U> :</B><BR><BR>
  614. * <li> Set position of a new L2Object (drop, spawn...) </li>
  615. * <li> Update position of a L2Object after a movement </li><BR>
  616. *
  617. * @param point position of the object
  618. * @return
  619. */
  620. public L2WorldRegion getRegion(Point3D point)
  621. {
  622. return _worldRegions[(point.getX() >> SHIFT_BY) + OFFSET_X][(point.getY() >> SHIFT_BY) + OFFSET_Y];
  623. }
  624. public L2WorldRegion getRegion(int x, int y)
  625. {
  626. return _worldRegions[(x >> SHIFT_BY) + OFFSET_X][(y >> SHIFT_BY) + OFFSET_Y];
  627. }
  628. /**
  629. * Returns the whole 2d array containing the world regions
  630. * used by ZoneData.java to setup zones inside the world regions
  631. * @return
  632. */
  633. public L2WorldRegion[][] getAllWorldRegions()
  634. {
  635. return _worldRegions;
  636. }
  637. /**
  638. * Check if the current L2WorldRegions of the object is valid according to its position (x,y).<BR><BR>
  639. *
  640. * <B><U> Example of use </U> :</B><BR><BR>
  641. * <li> Init L2WorldRegions </li><BR>
  642. *
  643. * @param x X position of the object
  644. * @param y Y position of the object
  645. *
  646. * @return True if the L2WorldRegion is valid
  647. */
  648. private boolean validRegion(int x, int y)
  649. {
  650. return (x >= 0 && x <= REGIONS_X && y >= 0 && y <= REGIONS_Y);
  651. }
  652. /**
  653. * Init each L2WorldRegion and their surrounding table.<BR><BR>
  654. *
  655. * <B><U> Concept</U> :</B><BR><BR>
  656. * All surrounding L2WorldRegion are identified in <B>_surroundingRegions</B> of the selected L2WorldRegion in order to scan a large area around a L2Object<BR><BR>
  657. *
  658. * <B><U> Example of use </U> :</B><BR><BR>
  659. * <li> Constructor of L2World </li><BR>
  660. *
  661. */
  662. private void initRegions()
  663. {
  664. _worldRegions = new L2WorldRegion[REGIONS_X + 1][REGIONS_Y + 1];
  665. for (int i = 0; i <= REGIONS_X; i++)
  666. {
  667. for (int j = 0; j <= REGIONS_Y; j++)
  668. {
  669. _worldRegions[i][j] = new L2WorldRegion(i, j);
  670. }
  671. }
  672. for (int x = 0; x <= REGIONS_X; x++)
  673. {
  674. for (int y = 0; y <= REGIONS_Y; y++)
  675. {
  676. for (int a = -1; a <= 1; a++)
  677. {
  678. for (int b = -1; b <= 1; b++)
  679. {
  680. if (validRegion(x + a, y + b))
  681. {
  682. _worldRegions[x + a][y + b].addSurroundingRegion(_worldRegions[x][y]);
  683. }
  684. }
  685. }
  686. }
  687. }
  688. _log.info("L2World: (" + REGIONS_X + " by " + REGIONS_Y + ") World Region Grid set up.");
  689. }
  690. /**
  691. * Deleted all spawns in the world.
  692. */
  693. public void deleteVisibleNpcSpawns()
  694. {
  695. _log.info("Deleting all visible NPC's.");
  696. for (int i = 0; i <= REGIONS_X; i++)
  697. {
  698. for (int j = 0; j <= REGIONS_Y; j++)
  699. {
  700. _worldRegions[i][j].deleteVisibleNpcSpawns();
  701. }
  702. }
  703. _log.info("All visible NPC's deleted.");
  704. }
  705. @SuppressWarnings("synthetic-access")
  706. private static class SingletonHolder
  707. {
  708. protected static final L2World _instance = new L2World();
  709. }
  710. }