TeamManager.java 17.8 KB
Newer Older
Melledy's avatar
Melledy committed
1
package emu.grasscutter.game.player;
Melledy's avatar
Melledy committed
2

Akka's avatar
Akka committed
3
import java.util.*;
4

5
import dev.morphia.annotations.Entity;
Melledy's avatar
Melledy committed
6
import dev.morphia.annotations.Transient;
7
import emu.grasscutter.GameConstants;
8
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.data.def.AvatarSkillDepotData;
10
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.entity.EntityAvatar;
12
import emu.grasscutter.game.entity.EntityBaseGadget;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
14
15
16
import emu.grasscutter.game.props.ElementType;
import emu.grasscutter.game.props.EnterReason;
import emu.grasscutter.game.props.FightProperty;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.world.World;
18
import emu.grasscutter.net.packet.BasePacket;
Melledy's avatar
Melledy committed
19
20
21
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.EnterTypeOuterClass.EnterType;
import emu.grasscutter.net.proto.MotionStateOuterClass.MotionState;
22
import emu.grasscutter.net.proto.PlayerDieTypeOuterClass.PlayerDieType;
Melledy's avatar
Melledy committed
23
24
25
import emu.grasscutter.server.packet.send.PacketAvatarDieAnimationEndRsp;
import emu.grasscutter.server.packet.send.PacketAvatarFightPropUpdateNotify;
import emu.grasscutter.server.packet.send.PacketAvatarLifeStateChangeNotify;
26
import emu.grasscutter.server.packet.send.PacketAvatarSkillInfoNotify;
Melledy's avatar
Melledy committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import emu.grasscutter.server.packet.send.PacketAvatarTeamUpdateNotify;
import emu.grasscutter.server.packet.send.PacketChangeAvatarRsp;
import emu.grasscutter.server.packet.send.PacketChangeMpTeamAvatarRsp;
import emu.grasscutter.server.packet.send.PacketChangeTeamNameRsp;
import emu.grasscutter.server.packet.send.PacketChooseCurAvatarTeamRsp;
import emu.grasscutter.server.packet.send.PacketPlayerEnterSceneNotify;
import emu.grasscutter.server.packet.send.PacketSceneTeamUpdateNotify;
import emu.grasscutter.server.packet.send.PacketSetUpAvatarTeamRsp;
import emu.grasscutter.server.packet.send.PacketWorldPlayerDieNotify;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;

43
@Entity
Melledy's avatar
Melledy committed
44
public class TeamManager {
45
	@Transient private Player player;
Melledy's avatar
Melledy committed
46
47
48
49
50
51
52
53
	
	private Map<Integer, TeamInfo> teams;
	private int currentTeamIndex;
	private int currentCharacterIndex;
	
	@Transient private TeamInfo mpTeam;
	@Transient private int entityId;
	@Transient private final List<EntityAvatar> avatars;
54
	@Transient private final Set<EntityBaseGadget> gadgets;
Melledy's avatar
Melledy committed
55
56
	@Transient private final IntSet teamResonances;
	@Transient private final IntSet teamResonancesConfig;
Akka's avatar
Akka committed
57

Akka's avatar
Akka committed
58
	@Transient private int useTemporarilyTeamIndex = -1;
Akka's avatar
Akka committed
59
60
61
	/**
	 * Temporary Team for tower
	 */
Akka's avatar
Akka committed
62
	@Transient private List<TeamInfo> temporaryTeam;
Akka's avatar
Akka committed
63

Melledy's avatar
Melledy committed
64
65
66
	public TeamManager() {
		this.mpTeam = new TeamInfo();
		this.avatars = new ArrayList<>();
67
		this.gadgets = new HashSet<>();
Melledy's avatar
Melledy committed
68
69
70
71
		this.teamResonances = new IntOpenHashSet();
		this.teamResonancesConfig = new IntOpenHashSet();
	}
	
72
	public TeamManager(Player player) {
Melledy's avatar
Melledy committed
73
74
75
76
77
		this();
		this.player = player;
		
		this.teams = new HashMap<>();
		this.currentTeamIndex = 1;
78
		for (int i = 1; i <= GameConstants.MAX_TEAMS; i++) {
Melledy's avatar
Melledy committed
79
80
81
82
			this.teams.put(i, new TeamInfo());
		}
	}
	
83
	public Player getPlayer() {
Melledy's avatar
Melledy committed
84
85
86
87
88
89
90
		return player;
	}
	
	public World getWorld() {
		return player.getWorld();
	}

91
	public void setPlayer(Player player) {
Melledy's avatar
Melledy committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
		this.player = player;
	}
	
	public Map<Integer, TeamInfo> getTeams() {
		return this.teams;
	}

	public TeamInfo getMpTeam() {
		return mpTeam;
	}

	public void setMpTeam(TeamInfo mpTeam) {
		this.mpTeam = mpTeam;
	}

	public int getCurrentTeamId() {
		// Starts from 1
		return currentTeamIndex;
	}

	private void setCurrentTeamId(int currentTeamIndex) {
		this.currentTeamIndex = currentTeamIndex;
	}

	public int getCurrentCharacterIndex() {
		return currentCharacterIndex;
	}

	public void setCurrentCharacterIndex(int currentCharacterIndex) {
		this.currentCharacterIndex = currentCharacterIndex;
	}

	public long getCurrentCharacterGuid() {
		return getCurrentAvatarEntity().getAvatar().getGuid();
	}
	
	public TeamInfo getCurrentTeamInfo() {
Akka's avatar
Akka committed
129
130
131
132
		if (useTemporarilyTeamIndex >= 0 &&
				useTemporarilyTeamIndex < temporaryTeam.size()){
			return temporaryTeam.get(useTemporarilyTeamIndex);
		}
Melledy's avatar
Melledy committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		if (this.getPlayer().isInMultiplayer()) {
			return this.getMpTeam();
		}
		return this.getTeams().get(this.currentTeamIndex);
	}
	
	public TeamInfo getCurrentSinglePlayerTeamInfo() {
		return this.getTeams().get(this.currentTeamIndex);
	}

	public int getEntityId() {
		return entityId;
	}

	public void setEntityId(int entityId) {
		this.entityId = entityId;
	}

151
	public Set<EntityBaseGadget> getGadgets() {
152
153
154
		return gadgets;
	}

Melledy's avatar
Melledy committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	public IntSet getTeamResonances() {
		return teamResonances;
	}

	public IntSet getTeamResonancesConfig() {
		return teamResonancesConfig;
	}

	public List<EntityAvatar> getActiveTeam() {
		return avatars;
	}
	
	public EntityAvatar getCurrentAvatarEntity() {
		return getActiveTeam().get(currentCharacterIndex);
	}
	
	public boolean isSpawned() {
172
		return getPlayer().getScene() != null && getPlayer().getScene().getEntities().containsKey(getCurrentAvatarEntity().getId());
Melledy's avatar
Melledy committed
173
174
175
176
	}
	
	public int getMaxTeamSize() {
		if (getPlayer().isInMultiplayer()) {
177
			int max = Grasscutter.getConfig().getGameServerOptions().MaxAvatarsInTeamMultiplayer;
Melledy's avatar
Melledy committed
178
			if (getPlayer().getWorld().getHost() == this.getPlayer()) {
179
				return Math.max(1, (int) Math.ceil(max / (double) getWorld().getPlayerCount()));
Melledy's avatar
Melledy committed
180
			}
181
			return Math.max(1, (int) Math.floor(max / (double) getWorld().getPlayerCount()));
Melledy's avatar
Melledy committed
182
		}
183
		return Grasscutter.getConfig().getGameServerOptions().MaxAvatarsInTeam;
Melledy's avatar
Melledy committed
184
185
186
187
	}
	
	// Methods
	
188
	private void updateTeamResonances() {
Melledy's avatar
Melledy committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
		Int2IntOpenHashMap map = new Int2IntOpenHashMap();
		
		this.getTeamResonances().clear();
		this.getTeamResonancesConfig().clear();
		
		for (EntityAvatar entity : getActiveTeam()) {
			AvatarSkillDepotData skillData = entity.getAvatar().getAvatarData().getSkillDepot();
			if (skillData != null) {
				map.addTo(skillData.getElementType().getValue(), 1);
			}
		}
		
		for (Int2IntMap.Entry e : map.int2IntEntrySet()) {
			if (e.getIntValue() >= 2) {
				ElementType element = ElementType.getTypeByValue(e.getIntKey());
				if (element.getTeamResonanceId() != 0) {
					this.getTeamResonances().add(element.getTeamResonanceId());
					this.getTeamResonancesConfig().add(element.getConfigHash());
				}
			}
		}
		
		// No resonances
		if (this.getTeamResonances().size() == 0) {
			this.getTeamResonances().add(ElementType.Default.getTeamResonanceId());
			this.getTeamResonancesConfig().add(ElementType.Default.getTeamResonanceId());
		}
	}
	
218
	public void updateTeamEntities(BasePacket responsePacket) {
Melledy's avatar
Melledy committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
		// Sanity check - Should never happen
		if (this.getCurrentTeamInfo().getAvatars().size() <= 0) {
			return;
		}
		
		// If current team has changed
		EntityAvatar currentEntity = this.getCurrentAvatarEntity();
		Int2ObjectMap<EntityAvatar> existingAvatars = new Int2ObjectOpenHashMap<>();
		int prevSelectedAvatarIndex = -1;
		
		for (EntityAvatar entity : getActiveTeam()) {
			existingAvatars.put(entity.getAvatar().getAvatarId(), entity);
		}
		
		// Clear active team entity list
		this.getActiveTeam().clear();
		
		// Add back entities into team
		for (int i = 0; i < this.getCurrentTeamInfo().getAvatars().size(); i++) {
			int avatarId = this.getCurrentTeamInfo().getAvatars().get(i);
			EntityAvatar entity = null;
			
			if (existingAvatars.containsKey(avatarId)) {
				entity = existingAvatars.get(avatarId);
				existingAvatars.remove(avatarId);
				if (entity == currentEntity) {
					prevSelectedAvatarIndex = i;
				}
			} else {
248
				entity = new EntityAvatar(getPlayer().getScene(), getPlayer().getAvatars().getAvatarById(avatarId));
Melledy's avatar
Melledy committed
249
250
251
252
253
254
255
			}
			
			this.getActiveTeam().add(entity);
		}
		
		// Unload removed entities
		for (EntityAvatar entity : existingAvatars.values()) {
256
			getPlayer().getScene().removeEntity(entity);
Melledy's avatar
Melledy committed
257
258
259
260
261
262
			entity.getAvatar().save();
		}
		
		// Set new selected character index
		if (prevSelectedAvatarIndex == -1) {
			// Previous selected avatar is not in the same spot, we will select the current one in the prev slot
263
			prevSelectedAvatarIndex = Math.min(this.currentCharacterIndex, this.getActiveTeam().size() - 1);
Melledy's avatar
Melledy committed
264
265
266
267
268
269
270
		}
		this.currentCharacterIndex = prevSelectedAvatarIndex;
		
		// Update team resonances
		updateTeamResonances();
		
		// Packets
271
		getPlayer().getWorld().broadcastPacket(new PacketSceneTeamUpdateNotify(getPlayer()));
Melledy's avatar
Melledy committed
272
		
273
274
275
276
277
278
279
		// Skill charges packet - Yes, this is official server behavior as of 2.6.0
		for (EntityAvatar entity : getActiveTeam()) {
			if (entity.getAvatar().getSkillExtraChargeMap().size() > 0) {
				getPlayer().sendPacket(new PacketAvatarSkillInfoNotify(entity.getAvatar()));
			}
		}
		
Melledy's avatar
Melledy committed
280
281
282
283
284
285
286
287
		// Run callback
		if (responsePacket != null) {
			getPlayer().sendPacket(responsePacket);
		}

		// Check if character changed
		if (currentEntity != getCurrentAvatarEntity()) {
			// Remove and Add
288
			getPlayer().getScene().replaceEntity(currentEntity, getCurrentAvatarEntity());
Melledy's avatar
Melledy committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
		}
	}
	
	public synchronized void setupAvatarTeam(int teamId, List<Long> list) {
		// Sanity checks
		if (list.size() == 0 || list.size() > getMaxTeamSize() || getPlayer().isInMultiplayer()) {
			return;
		}
		
		// Get team
		TeamInfo teamInfo = this.getTeams().get(teamId);
		if (teamInfo == null) {
			return;
		}
		
		// Set team data
305
		LinkedHashSet<Avatar> newTeam = new LinkedHashSet<>();
Melledy's avatar
Melledy committed
306
		for (int i = 0; i < list.size(); i++) {
307
			Avatar avatar = getPlayer().getAvatars().getAvatarByGuid(list.get(i));
Melledy's avatar
Melledy committed
308
309
310
311
312
313
314
315
316
			if (avatar == null || newTeam.contains(avatar)) {
				// Should never happen
				return;
			}
			newTeam.add(avatar);
		}
		
		// Clear current team info and add avatars from our new team
		teamInfo.getAvatars().clear();
317
		for (Avatar avatar : newTeam) {
Melledy's avatar
Melledy committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
			teamInfo.addAvatar(avatar);
		}
		
		// Update packet
		getPlayer().sendPacket(new PacketAvatarTeamUpdateNotify(getPlayer()));
		
		// Update entites
		if (teamId == this.getCurrentTeamId()) {
			this.updateTeamEntities(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, teamInfo));
		} else {
			getPlayer().sendPacket(new PacketSetUpAvatarTeamRsp(getPlayer(), teamId, teamInfo));
		}
	}
	
	public void setupMpTeam(List<Long> list) {
		// Sanity checks
		if (list.size() == 0 || list.size() > getMaxTeamSize() || !getPlayer().isInMultiplayer()) {
			return;
		}

		TeamInfo teamInfo = this.getMpTeam();
		
		// Set team data
341
		LinkedHashSet<Avatar> newTeam = new LinkedHashSet<>();
Melledy's avatar
Melledy committed
342
		for (int i = 0; i < list.size(); i++) {
343
			Avatar avatar = getPlayer().getAvatars().getAvatarByGuid(list.get(i));
Melledy's avatar
Melledy committed
344
345
346
347
348
349
350
351
352
			if (avatar == null || newTeam.contains(avatar)) {
				// Should never happen
				return;
			}
			newTeam.add(avatar);
		}
		
		// Clear current team info and add avatars from our new team
		teamInfo.getAvatars().clear();
353
		for (Avatar avatar : newTeam) {
Melledy's avatar
Melledy committed
354
355
356
357
358
359
			teamInfo.addAvatar(avatar);
		}
		
		// Packet
		this.updateTeamEntities(new PacketChangeMpTeamAvatarRsp(getPlayer(), teamInfo));
	}
Akka's avatar
Akka committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

	public void setupTemporaryTeam(List<List<Long>> guidList) {
		var team = guidList.stream().map(list -> {
					// Sanity checks
					if (list.size() == 0 || list.size() > getMaxTeamSize()) {
						return null;
					}

					// Set team data
					LinkedHashSet<Avatar> newTeam = new LinkedHashSet<>();
					for (int i = 0; i < list.size(); i++) {
						Avatar avatar = getPlayer().getAvatars().getAvatarByGuid(list.get(i));
						if (avatar == null || newTeam.contains(avatar)) {
							// Should never happen
							return null;
						}
						newTeam.add(avatar);
					}

					// convert to avatar ids
					return newTeam.stream()
							.map(Avatar::getAvatarId)
							.toList();
				})
				.filter(Objects::nonNull)
				.map(TeamInfo::new)
				.toList();
		this.temporaryTeam = team;
	}

	public void useTemporaryTeam(int index) {
		this.useTemporarilyTeamIndex = index;
		updateTeamEntities(null);
	}

	public void cleanTemporaryTeam() {
		// check if using temporary team
		if(useTemporarilyTeamIndex < 0){
			return;
		}

		this.useTemporarilyTeamIndex = -1;
		this.temporaryTeam = null;
		updateTeamEntities(null);
	}
Melledy's avatar
Melledy committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
	public synchronized void setCurrentTeam(int teamId) {
		// 
		if (getPlayer().isInMultiplayer()) {
			return;
		}
		
		// Get team
		TeamInfo teamInfo = this.getTeams().get(teamId);
		if (teamInfo == null || teamInfo.getAvatars().size() == 0) {
			return;
		}
		
		// Set
		this.setCurrentTeamId(teamId);
		this.updateTeamEntities(new PacketChooseCurAvatarTeamRsp(teamId));
	}

	public synchronized void setTeamName(int teamId, String teamName) {
		// Get team
		TeamInfo teamInfo = this.getTeams().get(teamId);
		if (teamInfo == null) {
			return;
		}
		
		teamInfo.setName(teamName);
		
		// Packet
		getPlayer().sendPacket(new PacketChangeTeamNameRsp(teamId, teamName));
	}

	public synchronized void changeAvatar(long guid) {
		EntityAvatar oldEntity = this.getCurrentAvatarEntity();
		
		if (guid == oldEntity.getAvatar().getGuid()) {
			return;
		}

		EntityAvatar newEntity = null;
		int index = -1;
		for (int i = 0; i < getActiveTeam().size(); i++) {
			if (guid == getActiveTeam().get(i).getAvatar().getGuid()) {
				index = i;
				newEntity = getActiveTeam().get(i);
			}
		}
		
		if (index < 0 || newEntity == oldEntity) {
			return;
		}
		
		// Set index
		this.setCurrentCharacterIndex(index);
		
		// Old entity motion state
459
		oldEntity.setMotionState(MotionState.MOTION_STANDBY);
Melledy's avatar
Melledy committed
460
461

		// Remove and Add
462
		getPlayer().getScene().replaceEntity(oldEntity, newEntity);
Melledy's avatar
Melledy committed
463
464
465
466
467
468
469
470
471
		getPlayer().sendPacket(new PacketChangeAvatarRsp(guid));
	}
	
	public void onAvatarDie(long dieGuid) {
		EntityAvatar deadAvatar = this.getCurrentAvatarEntity();
		
		if (deadAvatar.isAlive() || deadAvatar.getId() != dieGuid) {
			return;
		}
472
473
474
475
476
477
478
479
480

		PlayerDieType dieType = deadAvatar.getKilledType();
		int killedBy = deadAvatar.getKilledBy();

		if (dieType == PlayerDieType.PLAYER_DIE_DRAWN) {
			// Died in water. Do not replace
			// The official server has skipped this notify and will just respawn the team immediately after the animation.
			// TODO: Perhaps find a way to get vanilla experience?
			getPlayer().sendPacket(new PacketWorldPlayerDieNotify(dieType, killedBy));
Melledy's avatar
Melledy committed
481
		} else {
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
			// Replacement avatar
			EntityAvatar replacement = null;
			int replaceIndex = -1;

			for (int i = 0; i < this.getActiveTeam().size(); i++) {
				EntityAvatar entity = this.getActiveTeam().get(i);
				if (entity.isAlive()) {
					replaceIndex = i;
					replacement = entity;
					break;
				}
			}

			if (replacement == null) {
				// No more living team members...
				getPlayer().sendPacket(new PacketWorldPlayerDieNotify(dieType, killedBy));
			} else {
				// Set index and spawn replacement member
				this.setCurrentCharacterIndex(replaceIndex);
				getPlayer().getScene().addEntity(replacement);
			}
Melledy's avatar
Melledy committed
503
504
505
506
507
508
		}

		// Response packet
		getPlayer().sendPacket(new PacketAvatarDieAnimationEndRsp(deadAvatar.getId(), 0));
	}
	
509
	public boolean reviveAvatar(Avatar avatar) {
Melledy's avatar
Melledy committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
		for (EntityAvatar entity : getActiveTeam()) {
			if (entity.getAvatar() == avatar) {
				if (entity.isAlive()) {
					return false;
				}
				
				entity.setFightProperty(
						FightProperty.FIGHT_PROP_CUR_HP, 
						entity.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP) * .1f
				);
				getPlayer().sendPacket(new PacketAvatarFightPropUpdateNotify(entity.getAvatar(), FightProperty.FIGHT_PROP_CUR_HP));
				getPlayer().sendPacket(new PacketAvatarLifeStateChangeNotify(entity.getAvatar()));
				return true;
			}
		}
		
		return false;
	}
xtaodada's avatar
xtaodada committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

	public boolean healAvatar(Avatar avatar, int healRate, int healAmount) {
		for (EntityAvatar entity : getActiveTeam()) {
			if (entity.getAvatar() == avatar) {
				if (!entity.isAlive()) {
					return false;
				}

				entity.setFightProperty(
						FightProperty.FIGHT_PROP_CUR_HP,
						(float) Math.min(
								(entity.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) +
										entity.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP) * (float) healRate / 100.0 +
										(float) healAmount / 100.0),
								entity.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP)
						)
				);
				getPlayer().sendPacket(new PacketAvatarFightPropUpdateNotify(entity.getAvatar(), FightProperty.FIGHT_PROP_CUR_HP));
				getPlayer().sendPacket(new PacketAvatarLifeStateChangeNotify(entity.getAvatar()));
				return true;
			}
		}
		return false;
	}

Melledy's avatar
Melledy committed
553
554
	public void respawnTeam() {
		// Make sure all team members are dead
555
556
557
558
559
560
		// Drowning needs revive when there may be other team members still alive.
		//  for (EntityAvatar entity : getActiveTeam()) {
		//      if (entity.isAlive()) {
		//		     return;
		//		}
		//	}
561
		player.getStaminaManager().stopSustainedStaminaHandler(); // prevent drowning immediately after respawn
Melledy's avatar
Melledy committed
562
563
564
565
566
567
568
569
570
571
572
573
		
		// Revive all team members
		for (EntityAvatar entity : getActiveTeam()) {
			entity.setFightProperty(
					FightProperty.FIGHT_PROP_CUR_HP, 
					entity.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP) * .4f
			);
			getPlayer().sendPacket(new PacketAvatarFightPropUpdateNotify(entity.getAvatar(), FightProperty.FIGHT_PROP_CUR_HP));
			getPlayer().sendPacket(new PacketAvatarLifeStateChangeNotify(entity.getAvatar()));
		}
		
		// Teleport player
574
		getPlayer().sendPacket(new PacketPlayerEnterSceneNotify(getPlayer(), EnterType.ENTER_SELF, EnterReason.Revival, 3, GameConstants.START_POSITION));
Melledy's avatar
Melledy committed
575
576
577
		
		// Set player position
		player.setSceneId(3);
578
		player.getPos().set(GameConstants.START_POSITION);
Melledy's avatar
Melledy committed
579
580

		// Packets
581
		getPlayer().sendPacket(new BasePacket(PacketOpcodes.WorldPlayerReviveRsp));
Melledy's avatar
Melledy committed
582
	}
Melledy's avatar
Melledy committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
	
	public synchronized void addEnergyToTeam(GameItem energyBall) {
		// TODO
		float baseEnergy = 2;
		
		for (int i = 0; i < getActiveTeam().size(); i++) {
			EntityAvatar entity = getActiveTeam().get(i);
			
			float energyGain = baseEnergy;
			
			// Active character gets full hp
			if (getCurrentCharacterIndex() != i) {
				energyGain *= Math.max(1.0 - (getActiveTeam().size() * .1f), .6f);
			}

			entity.addEnergy(energyGain);
		}
	}
Melledy's avatar
Melledy committed
601
602
603
604
605
606
607
608

	public void saveAvatars() {
		// Save all avatars from active team
		for (EntityAvatar entity : getActiveTeam()) {
			entity.getAvatar().save();
		}
	}
}