root/trunk/AE-go_GameServer/src/com/aionemu/gameserver/world/WorldMap.java

14941514
17
package com.aionemu.gameserver.world;
17
package com.aionemu.gameserver.world;
18
18
19
import java.util.HashMap;
19
import java.util.HashMap;
20
import java.util.Iterator;
20
import java.util.Map;
21
import java.util.Map;
21
22
22
import org.apache.log4j.Logger;
23
24
import com.aionemu.gameserver.model.templates.WorldMapTemplate;
23
import com.aionemu.gameserver.model.templates.WorldMapTemplate;
25
24
26
/**
25
/**
...
...
31
 */
30
 */
32
public class WorldMap
31
public class WorldMap
33
{
32
{
34
	private static Logger log = Logger.getLogger(WorldMap.class);
35
	
36
	private WorldMapTemplate				worldMapTemplate;
33
	private WorldMapTemplate				worldMapTemplate;
37
34
38
	private int nextInstanceId = 1;
35
	private int nextInstanceId = 1;
...
...
84
		int twinCount = worldMapTemplate.getTwinCount();
81
		int twinCount = worldMapTemplate.getTwinCount();
85
		return twinCount > 0 ? twinCount : 1;
82
		return twinCount > 0 ? twinCount : 1;
86
	}
83
	}
87
	
88
	/**
89
	 *  Will create new instance if there are not free yet and spawn according to xml data
90
	 *  //TODO limit
91
	 * @return WorldMapInstance
92
	 */
93
	public synchronized WorldMapInstance getNextFreeInstance()
94
	{	
95
		if (!worldMapTemplate.isInstance())
96
		{
97
			for(WorldMapInstance instance : instances.values())
98
			{
99
				if(!instance.isInUse())
100
					return instance;
101
			}
102
		}
103
		log.info("Creating new instance: " + worldMapTemplate.getMapId() + " " + nextInstanceId );
104
		if (worldMapTemplate.isInstance())
105
			addInstance(nextInstanceId, new WorldMapScriptInstance(this, nextInstanceId));
106
		else
107
			addInstance(nextInstanceId, new WorldMapInstance(this, nextInstanceId));
108
		world.getSpawnEngine().spawnInstance(worldMapTemplate.getMapId(), nextInstanceId-1);
109
		
110
		return instances.get(nextInstanceId-1);
111
	}
112
84
113
	/**
85
	/**
114
	 * Return a WorldMapInstance - depends on map configuration one map may have twins instances to balance player. This
86
	 * Return a WorldMapInstance - depends on map configuration one map may have twins instances to balance player. This
115
	 * method will return WorldMapInstance by server chose.
87
	 * method will return WorldMapInstance by server chose.
116
	 * 
88
	 *  
89
	 *  
117
	 * @return WorldMapInstance.
90
	 * @return WorldMapInstance.
118
	 */
91
	 */
119
	public WorldMapInstance getWorldMapInstance()
92
	public WorldMapInstance getWorldMapInstance()
120
	{
93
	{
121
		/**
94
		//TODO Balance players into instances.
122
		 * twin map - balance players
95
		return getWorldMapInstance(1);
123
		 */
124
		if(worldMapTemplate.getTwinCount() != 0)
125
		{
126
			/**
127
			 * Balance players into instances.
128
			 */
129
			int i = 1;
130
			while(i <= worldMapTemplate.getTwinCount())
131
			{		
132
				WorldMapInstance inst = getWorldMapInstance(i);
133
				i++;
134
				// TODO! user count etc..
135
				return inst;
136
			}
137
			// TODO! whats now?
138
			return getWorldMapInstance(worldMapTemplate.getTwinCount());
139
		}
140
		else
141
			return getWorldMapInstance(1);
142
	}
96
	}
143
	
97
	
144
	/**
98
	/**
...
...
174
	 * Remove WorldMapInstance by instanceId.
128
	 * Remove WorldMapInstance by instanceId.
175
	 * 
129
	 * 
176
	 * @param instanceId
130
	 * @param instanceId
177
	 * @return WorldMapInstance/
178
	 */
131
	 */
179
	public WorldMapInstance removeWorldMapInstance(int instanceId)
132
	public void removeWorldMapInstance(int instanceId)
180
	{
133
	{
181
		WorldMapInstance instance = instances.get(instanceId);
134
		instances.remove(instanceId);
182
		if (instance != null)
183
		{
184
			instance.destroyInstance();
185
			return instances.remove(instanceId);
186
		}
187
		return null;
188
	}
135
	}
189
136
	
190
	public int getWorldMapScriptInstanceIdByPlyerObjId(int objId)
137
	/**
138
	 *  Add instance to map and increases pointer to nextInstanceId
139
	 *  
140
	 * @param instanceId
141
	 * @param instance
142
	 */
143
	public void addInstance(int instanceId, WorldMapInstance instance)
191
	{
144
	{
192
		for (WorldMapInstance instance : instances.values())
193
		{
194
			if (instance.isInInstance(objId))
195
				return instance.getInstanceId();
196
		}
197
		return -1;
198
	}
199
200
	private void addInstance(int instanceId, WorldMapInstance instance)
201
	{
202
		instances.put(instanceId, instance);
145
		instances.put(instanceId, instance);
203
		nextInstanceId++;
146
		nextInstanceId++;
204
	}
147
	}
...
...
209
	{
152
	{
210
		return world;
153
		return world;
211
	}
154
	}
155
156
	/**
157
	 * @return the nextInstanceId
158
	 */
159
	public int getNextInstanceId()
160
	{
161
		return nextInstanceId;
162
	}
163
164
	/**
165
	 *  Whether this world map is instance type
166
	 *  
167
	 * @return
168
	 */
169
	public boolean isInstanceType()
170
	{
171
		return worldMapTemplate.isInstance();
172
	}
173
	
174
	/**
175
	 * @return
176
	 */
177
	public Iterator<WorldMapInstance> iterator()
178
	{
179
		return instances.values().iterator();
180
	}
212
}
181
}