L2World.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
net.sf.l2j.gameserver.model |
![]() |
![]() |
L2J |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* $Header: /cvsroot/l2j/L2_Gameserver/java/net/sf/l2j/gameserver/model/L2World.java,v 1.21 2004/11/10 11:00:46 nuocnam Exp $
*
* $Author: nuocnam $
* $Date: 2004/11/10 11:00:46 $
* $Revision: 1.21 $
* $Log: L2World.java,v $
* Revision 1.21 2004/11/10 11:00:46 nuocnam
* removed unused imports (whatev) (taken from database patch)
*
* Revision 1.20 2004/11/02 22:51:37 nuocnam
* critical bugfix (case23)
*
* Revision 1.19 2004/11/02 14:33:16 nuocnam
* introduced world regions
*
* Revision 1.18 2004/10/23 21:06:53 l2chef
* character names are stored lowercase to make the player search non-casesensitive
*
* Revision 1.17 2004/09/20 00:23:33 whatev66
* mobs now appear to you when they spawn without you having to move.
*
* Revision 1.16 2004/09/19 00:30:09 whatev66
* *** empty log message ***
*
* Revision 1.15 2004/09/18 01:41:39 whatev66
* added private store buy/sell
*
* Revision 1.14 2004/08/11 22:22:14 l2chef
* using threadsafe collection classes
*
* Revision 1.13 2004/08/08 00:48:31 l2chef
* world.csv handling removed
*
* Revision 1.12 2004/08/02 00:08:32 l2chef
* better solution to drop item problem
*
* Revision 1.11 2004/07/28 23:57:24 l2chef
* loglevels changed
* client crash fixed when picking up items twice
*
* Revision 1.10 2004/07/23 01:51:47 l2chef
* all object spawn and delete is now handeld in L2PcInstance
*
* Revision 1.9 2004/07/18 17:39:35 l2chef
* aggressive monsters scan for targets in range (FTPW)
*
* Revision 1.8 2004/07/17 23:12:00 l2chef
* attack range is set for monsters
*
* Revision 1.7 2004/07/13 22:59:27 l2chef
* world init removed
*
* Revision 1.6 2004/07/11 11:48:54 l2chef
* additional npc data is used when populating the world
*
* Revision 1.5 2004/07/04 11:13:27 l2chef
* logging is used instead of system.out
*
* Revision 1.4 2004/06/29 22:55:35 l2chef
* tell and say works now. say is actually a server broadcast now
*
* Revision 1.3 2004/06/27 20:50:03 l2chef
* better error message when data file is missing. skipping of comment lines
*
* Revision 1.2 2004/06/27 08:51:42 jeichhorn
* Added copyright notice
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package net.sf.l2j.gameserver.model;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Logger;
import net.sf.l2j.gameserver.IdFactory;
import net.sf.l2j.util.ConcurrentHashMap;
/**
* This class ...
*
* @version $Revision: 1.21 $ $Date: 2004/11/10 11:00:46 $
*/
public class L2World
{
private static Logger _log = Logger.getLogger(L2World.class.getName());
/*
* 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
*/
private static final int SHIFT_BY = 12;
//map dimensions
private static final int MAP_MIN_X = -131072;
private static final int MAP_MAX_X = 196608;
private static final int MAP_MIN_Y = -393216;
private static final int MAP_MAX_Y = 262144;
//calculated offset used so top left region is 0,0
private static final int OFFSET_X = Math.abs(MAP_MIN_X >> SHIFT_BY);
private static final int OFFSET_Y = Math.abs(MAP_MIN_Y >> SHIFT_BY);
//number of regions
private static final int REGIONS_X = (MAP_MAX_X >> SHIFT_BY) + OFFSET_X;
private static final int REGIONS_Y = (MAP_MAX_Y >> SHIFT_BY) + OFFSET_Y;
private Map _allPlayers;
private Map _allObjects;
private IdFactory _idFactory;
private static L2World _instance;
private L2WorldRegion[][] _worldRegions;
private L2World()
{
_allPlayers = new ConcurrentHashMap();
_allObjects = new ConcurrentHashMap();
_idFactory = IdFactory.getInstance();
initRegions();
}
public static L2World getInstance()
{
if (_instance == null)
{
_instance = new L2World();
}
return _instance;
}
/*
* Old methods
*/
public void storeObject(L2Object temp)
{
_allObjects.put(new Integer(temp.getObjectId()), temp);
}
public void removeObject(L2Object object)
{
_allObjects.remove(new Integer(object.getObjectId())); // suggestion by whatev
}
/**
* find the object that belongs to an ID
* @param oID
* @return null if no object was found.
*/
public L2Object findObject(int oID)
{
return (L2Object) _allObjects.get(new Integer(oID));
}
public L2PcInstance[] getAllPlayers()
{
return (L2PcInstance[]) _allPlayers.values().toArray(new L2PcInstance[_allPlayers.size()]);
}
public L2PcInstance getPlayer(String name)
{
return (L2PcInstance) _allPlayers.get(name.toLowerCase());
}
/*
* New L2WorldRegion stuff
*/
public void addVisibleObject(L2Object object)
{
//update region info for object
object.updateCurrentWorldRegion();
if (object instanceof L2PcInstance)
{
//old stuff to remember all players
_allPlayers.put(((L2PcInstance)object).getName().toLowerCase(), object);
//get all visible objects around
L2Object[] visible = getVisibleObjects(object, 2000);
_log.finest("objects in range:"+visible.length);
// tell the player about the surroundings
for (int i = 0; i < visible.length; i++)
{
object.addKnownObject(visible[i]);
if (object instanceof L2ItemInstance && visible[i].getKnownObjects().contains(object))
{
// special case for droped items. they are already known when they appear in the world
}
else
{
visible[i].addKnownObject(object);
}
}
}
else if (!(object instanceof L2PetInstance)&& !(object instanceof L2ItemInstance))
{//for npcs to be seen by players if they pop up in world.
int x = object.getX();
int y = object.getY();
int sqRadius = 2000*2000;
//get all regions around
ArrayList _regions = object.getCurrentWorldRegion().getSurroundingRegions();
for (int i = 0; i < _regions.size(); i++) {
//update info for each player in surrounding regions if needed
L2PcInstance[] _players = ((L2WorldRegion)_regions.get(i)).getAllPlayers();
for (int j = 0; j < _players.length; j++)
{
int x1 = _players[j].getX();
int y1 = _players[j].getY();
long dx = x1 - x;
long dy = y1 - y;
long sqDist = dx*dx + dy*dy;
if (sqDist < sqRadius)
{
_players[j].addKnownObject(object);
object.addKnownObject(_players[j]);
}
}
}
}
}
public void removeVisibleObject(L2Object object)
{
//update known objects
Object[] temp = object.getKnownObjects().toArray();
for (int i = 0; i < temp.length; i++)
{
L2Object temp1 = (L2Object) temp[i];
temp1.removeKnownObject(object);
object.removeKnownObject(temp1);
}
//old stuff to rember all players
if (object instanceof L2PcInstance)
{
_allPlayers.remove(((L2PcInstance)object).getName().toLowerCase());
}
//remove visible object from it's region
object.getCurrentWorldRegion().removeVisibleObject(object);
}
/**
* returns all visible objects in range
* @param object
* @param radius
* @return
*/
public L2Object[] getVisibleObjects(L2Object object, int radius)
{
int x = object.getX();
int y = object.getY();
int sqRadius = radius*radius;
ArrayList result = new ArrayList();
//get all regions around
ArrayList _regions = object.getCurrentWorldRegion().getSurroundingRegions();
for (int i = 0; i < _regions.size(); i++) {
//get visible objects in region & do stuff if needed
L2Object[] _objects = ((L2WorldRegion)_regions.get(i)).getVisibleObjects();
for (int j = 0; j < _objects.length; j++)
{
if (_objects[j].equals(object)) continue; // skip our own character
int x1 = _objects[j].getX();
int y1 = _objects[j].getY();
long dx = x1 - x;
long dy = y1 - y;
long sqDist = dx*dx + dy*dy;
if (sqDist < sqRadius)
{
result.add(_objects[j]);
}
}
}
return (L2Object[]) result.toArray(new L2Object[result.size()]);
}
public L2WorldRegion getRegion(int x, int y) {
return _worldRegions[(x >> SHIFT_BY) + OFFSET_X][(y >> SHIFT_BY) + OFFSET_Y];
}
private void initRegions() {
_log.config("setting up world regions ...");
_worldRegions = new L2WorldRegion[REGIONS_X+1][REGIONS_Y+1];
for (int i = 0; i <= REGIONS_X; i++) {
for (int j = 0; j <= REGIONS_Y; j++) {
_worldRegions[i][j] = new L2WorldRegion();
}
}
//init regions in center
for (int i = 1; i < REGIONS_X - 1; i++) {
for (int j = 1; j < REGIONS_Y - 1; j++) {
for (int a = -1; a < 1; a++) {
for (int b = -1; b < 1; b++) {
_worldRegions[i][j].addSurroundingRegion(_worldRegions[i+a][j+b]);
}
}
}
}
//init up & down regions on Y axis
for (int i = 1; i < REGIONS_X - 1; i++) {
for (int a = -1; a < 1; a++) {
_worldRegions[i][0].addSurroundingRegion(_worldRegions[i+a][0]);
_worldRegions[i][0].addSurroundingRegion(_worldRegions[i+a][1]);
_worldRegions[i][REGIONS_Y].addSurroundingRegion(_worldRegions[i+a][REGIONS_Y]);
_worldRegions[i][REGIONS_Y].addSurroundingRegion(_worldRegions[i+a][REGIONS_Y - 1]);
}
}
//init left & right regions on X axis
for (int j = 1; j < REGIONS_Y - 1; j++) {
for (int b = -1; b < 1; b++) {
_worldRegions[0][j].addSurroundingRegion(_worldRegions[0][j+b]);
_worldRegions[0][j].addSurroundingRegion(_worldRegions[1][j+b]);
_worldRegions[REGIONS_X][j].addSurroundingRegion(_worldRegions[REGIONS_X][j+b]);
_worldRegions[REGIONS_X][j].addSurroundingRegion(_worldRegions[REGIONS_X - 1][j+b]);
}
}
//init regions in corners
//top left
_worldRegions[0][0].addSurroundingRegion(_worldRegions[0][0]);
_worldRegions[0][0].addSurroundingRegion(_worldRegions[0][1]);
_worldRegions[0][0].addSurroundingRegion(_worldRegions[1][0]);
_worldRegions[0][0].addSurroundingRegion(_worldRegions[1][1]);
//down left
_worldRegions[0][REGIONS_Y].addSurroundingRegion(_worldRegions[0][REGIONS_Y]);
_worldRegions[0][REGIONS_Y].addSurroundingRegion(_worldRegions[0][REGIONS_Y-1]);
_worldRegions[0][REGIONS_Y].addSurroundingRegion(_worldRegions[1][REGIONS_Y]);
_worldRegions[0][REGIONS_Y].addSurroundingRegion(_worldRegions[1][REGIONS_Y-1]);
//top right
_worldRegions[REGIONS_X][0].addSurroundingRegion(_worldRegions[REGIONS_X][0]);
_worldRegions[REGIONS_X][0].addSurroundingRegion(_worldRegions[REGIONS_X][1]);
_worldRegions[REGIONS_X][0].addSurroundingRegion(_worldRegions[REGIONS_X-1][0]);
_worldRegions[REGIONS_X][0].addSurroundingRegion(_worldRegions[REGIONS_X-1][1]);
//down right
_worldRegions[REGIONS_X][REGIONS_Y].addSurroundingRegion(_worldRegions[REGIONS_X][REGIONS_Y]);
_worldRegions[REGIONS_X][REGIONS_Y].addSurroundingRegion(_worldRegions[REGIONS_X][REGIONS_Y-1]);
_worldRegions[REGIONS_X][REGIONS_Y].addSurroundingRegion(_worldRegions[REGIONS_X-1][REGIONS_Y]);
_worldRegions[REGIONS_X][REGIONS_Y].addSurroundingRegion(_worldRegions[REGIONS_X-1][REGIONS_Y-1]);
_log.config("... done ("+REGIONS_X+"x"+REGIONS_Y+" set up)");
}
}
The table below shows all metrics for L2World.java.




