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

10351514
16
 */
16
 */
17
package com.aionemu.gameserver.world;
17
package com.aionemu.gameserver.world;
18
18
19
import java.util.Collections;
19
import java.util.HashMap;
20
import java.util.HashMap;
21
import java.util.Iterator;
20
import java.util.Map;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.concurrent.ConcurrentHashMap;
25
import java.util.concurrent.Future;
21
26
27
import com.aionemu.gameserver.model.gameobjects.AionObject;
22
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
28
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
23
import com.aionemu.gameserver.model.gameobjects.player.Player;
29
import com.aionemu.gameserver.model.gameobjects.player.Player;
30
import com.aionemu.gameserver.world.exceptions.DuplicateAionObjectException;
24
31
25
/**
32
/**
26
 * World map instance object.
33
 * World map instance object.
...
...
33
	/**
40
	/**
34
	 * Size of region
41
	 * Size of region
35
	 */
42
	 */
36
	public static final int				regionSize		= 500;
43
	public static final int						regionSize			= 500;
37
	/**
44
	/**
38
	 * Max world size - actually it must be some value bigger than world size. Used only for id generation.
45
	 * Max world size - actually it must be some value bigger than world size. Used only for id generation.
39
	 */
46
	 */
40
	private static final int				maxWorldSize	= 10000;
47
	private static final int					maxWorldSize		= 10000;
41
	/**
48
	/**
42
	 * WorldMap witch is parent of this instance.
49
	 * WorldMap witch is parent of this instance.
43
	 */
50
	 */
44
	private final WorldMap					parent;
51
	private final WorldMap						parent;
45
	/**
52
	/**
46
	 * List of active regions.
53
	 * Map of active regions.
47
	 */
54
	 */
48
	private final Map<Integer, MapRegion>	regions			= new HashMap<Integer, MapRegion>();
55
	private final Map<Integer, MapRegion>		regions				= new HashMap<Integer, MapRegion>();
49
	
56
50
	/**
57
	/**
51
	 * Current player count in this instance
58
	 * All objects spawned in this world map instance
52
	 */
59
	 */
53
	private int 							currentPlayerCount;
60
	private final Map<Integer, VisibleObject>	worldMapObjects		= new ConcurrentHashMap<Integer, VisibleObject>();
54
	
61
55
	private int								instanceId;
56
	/**
62
	/**
63
	 * All players spawned in this world map instance
64
	 */
65
	private final Map<Integer, Player>			worldMapPlayers		= new ConcurrentHashMap<Integer, Player>();
66
67
	private final Set<Integer>					registeredObjects	= Collections
68
																		.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
69
70
	/**
71
	 * Id of this instance (channel)
72
	 */
73
	private int									instanceId;
74
	/**
75
	 * Destroy task of this instance
76
	 */
77
	private Future<?>							destroyTask;
78
	/**
57
	 * Constructor.
79
	 * Constructor.
58
	 *
80
	 *
59
	 * @param parent
81
	 * @param parent
60
	 */
82
	 */
61
	WorldMapInstance(WorldMap parent, int instanceId)
83
	public WorldMapInstance(WorldMap parent, int instanceId)
62
	{
84
	{
63
		this.parent = parent;
85
		this.parent = parent;
64
		this.instanceId = instanceId;
86
		this.instanceId = instanceId;
...
...
174
	{
196
	{
175
		return getParent().getWorld();
197
		return getParent().getWorld();
176
	}
198
	}
177
199
	
178
	/**
200
	/**
179
	 * @return the currentPlayerCount
201
	 * 
202
	 * @param object
180
	 */
203
	 */
181
	public int getCurrentPlayerCount()
204
	public void addObject(VisibleObject object)
182
	{
205
	{
183
		return currentPlayerCount;
206
		if(worldMapObjects.put(object.getObjectId(), object) != null)
207
			throw new DuplicateAionObjectException();
208
209
		if(object instanceof Player)
210
			worldMapPlayers.put(object.getObjectId(), (Player) object);
184
	}
211
	}
185
	
212
	
186
	public void onEnter(VisibleObject object)
213
	/**
214
	 * 
215
	 * @param object
216
	 */
217
	public void removeObject(AionObject object)
187
	{
218
	{
188
		if (object instanceof Player)
219
		worldMapObjects.remove(object.getObjectId());
189
			currentPlayerCount++;
220
		if(object instanceof Player)
221
			worldMapPlayers.remove(object.getObjectId());
190
	}
222
	}
191
	
192
	public void onLeave(VisibleObject object)
193
	{
194
		if (object instanceof Player)
195
			currentPlayerCount--;
196
	}
197
	
198
	public boolean isInUse()
199
	{
200
		return currentPlayerCount > 0;
201
	}
202
223
203
	/**
224
	/**
204
	 * @return the instanceIndex
225
	 * @return the instanceIndex
...
...
208
		return instanceId;
229
		return instanceId;
209
	}
230
	}
210
	
231
	
232
	/**
233
	 *  Check player is in instance
234
	 *  
235
	 * @param objId
236
	 * @return
237
	 */
211
	public boolean isInInstance(int objId)
238
	public boolean isInInstance(int objId)
212
	{
239
	{
213
		return false;
240
		return worldMapPlayers.containsKey(objId);
214
	}
241
	}
215
242
216
	public void setDestroyTime(int sec)
243
	/**
217
	{	
244
	 * @return the destroyTask
245
	 */
246
	public Future<?> getDestroyTask()
247
	{
248
		return destroyTask;
218
	}
249
	}
219
250
220
	public void destroyInstance()
251
	/**
252
	 * @param destroyTask the destroyTask to set
253
	 */
254
	public void setDestroyTask(Future<?> destroyTask)
221
	{
255
	{
256
		this.destroyTask = destroyTask;
222
	}
257
	}
223
258
	
224
	public void addPlayer(int objId)
259
	/**
260
	 * @return
261
	 */
262
	public Iterator<VisibleObject> objectIterator()
225
	{
263
	{
264
		return worldMapObjects.values().iterator();
226
	}
265
	}
266
	
267
	/**
268
	 * @return
269
	 */
270
	public Iterator<Player> playerIterator()
271
	{
272
		return worldMapPlayers.values().iterator();
273
	}
274
	
275
	/**
276
	 * @param objectId
277
	 */
278
	public void register(int objectId)
279
	{
280
		registeredObjects.add(objectId);
281
	}
227
282
228
	public void removePlayer(int objId)
283
	/**
284
	 * @param objectId
285
	 * @return
286
	 */
287
	public boolean isRegistered(int objectId)
229
	{
288
	{
289
		return registeredObjects.contains(objectId);
230
	}
290
	}
231
}
291
}