EntityAvatar.java 11.1 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.entity;

3
4
import emu.grasscutter.GameConstants;
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
5
6
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.AvatarSkillDepotData;
7
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.inventory.EquipType;
9
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
11
12
13
import emu.grasscutter.game.props.EntityIdType;
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.PlayerProperty;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
15
16
17
18
import emu.grasscutter.net.proto.AbilityControlBlockOuterClass.AbilityControlBlock;
import emu.grasscutter.net.proto.AbilityEmbryoOuterClass.AbilityEmbryo;
import emu.grasscutter.net.proto.AbilitySyncStateInfoOuterClass.AbilitySyncStateInfo;
import emu.grasscutter.net.proto.AnimatorParameterValueInfoPairOuterClass.AnimatorParameterValueInfoPair;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.net.proto.ChangeHpReasonOuterClass.ChangeHpReason;
Melledy's avatar
Melledy committed
20
21
22
23
24
import emu.grasscutter.net.proto.EntityAuthorityInfoOuterClass.EntityAuthorityInfo;
import emu.grasscutter.net.proto.EntityClientDataOuterClass.EntityClientData;
import emu.grasscutter.net.proto.EntityRendererChangedInfoOuterClass.EntityRendererChangedInfo;
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
import emu.grasscutter.net.proto.PlayerDieTypeOuterClass.PlayerDieType;
Melledy's avatar
Melledy committed
25
import emu.grasscutter.net.proto.PropChangeReasonOuterClass.PropChangeReason;
Melledy's avatar
Melledy committed
26
27
28
29
30
31
import emu.grasscutter.net.proto.PropPairOuterClass.PropPair;
import emu.grasscutter.net.proto.ProtEntityTypeOuterClass.ProtEntityType;
import emu.grasscutter.net.proto.SceneAvatarInfoOuterClass.SceneAvatarInfo;
import emu.grasscutter.net.proto.SceneEntityAiInfoOuterClass.SceneEntityAiInfo;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
Melledy's avatar
Melledy committed
32
import emu.grasscutter.server.packet.send.PacketAvatarFightPropUpdateNotify;
Melledy's avatar
Melledy committed
33
import emu.grasscutter.server.packet.send.PacketEntityFightPropChangeReasonNotify;
Melledy's avatar
Melledy committed
34
35
36
37
38
39
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.ProtoHelper;
import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;

40
41
public class EntityAvatar extends GameEntity {
	private final Avatar avatar;
Melledy's avatar
Melledy committed
42
43
44
45
	
	private PlayerDieType killedType;
	private int killedBy;
	
46
	public EntityAvatar(Scene scene, Avatar avatar) {
47
		super(scene);
Melledy's avatar
Melledy committed
48
		this.avatar = avatar;
logictc's avatar
logictc committed
49
		this.avatar.setCurrentEnergy();
50
		this.id = getScene().getWorld().getNextEntityId(EntityIdType.AVATAR);
Melledy's avatar
Melledy committed
51
		
52
		GameItem weapon = this.getAvatar().getWeapon();
Melledy's avatar
Melledy committed
53
		if (weapon != null) {
54
			weapon.setWeaponEntityId(getScene().getWorld().getNextEntityId(EntityIdType.WEAPON));
Melledy's avatar
Melledy committed
55
56
57
		}
	}
	
58
	public EntityAvatar(Avatar avatar) {
Melledy's avatar
Melledy committed
59
60
		super(null);
		this.avatar = avatar;
logictc's avatar
logictc committed
61
		this.avatar.setCurrentEnergy();
Melledy's avatar
Melledy committed
62
63
	}

64
	public Player getPlayer() {
Melledy's avatar
Melledy committed
65
66
67
68
69
70
71
72
73
74
75
76
77
		return avatar.getPlayer();
	}

	@Override
	public Position getPosition() {
		return getPlayer().getPos();
	}
	
	@Override
	public Position getRotation() {
		return getPlayer().getRotation();
	}

78
	public Avatar getAvatar() {
Melledy's avatar
Melledy committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
		return avatar;
	}
	
	public int getKilledBy() {
		return killedBy;
	}
	
	public PlayerDieType getKilledType() {
		return killedType;
	}

	@Override
	public boolean isAlive() {
		return this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) > 0f;
	}
	
	@Override
	public Int2FloatOpenHashMap getFightProperties() {
		return getAvatar().getFightProperties();
	}
	
	public int getWeaponEntityId() {
		if (getAvatar().getWeapon() != null) {
			return getAvatar().getWeapon().getWeaponEntityId();
		}
		return 0;
	}

	@Override
	public void onDeath(int killerId) {
109
		this.killedType = PlayerDieType.PLAYER_DIE_TYPE_KILL_BY_MONSTER;
Melledy's avatar
Melledy committed
110
		this.killedBy = killerId;
111
		clearEnergy(PropChangeReason.PROP_CHANGE_REASON_STATUE_RECOVER);
Melledy's avatar
Melledy committed
112
	}
113
114
115
116

	public void onDeath(PlayerDieType dieType, int killerId) {
		this.killedType = dieType;
		this.killedBy = killerId;
117
		clearEnergy(PropChangeReason.PROP_CHANGE_REASON_STATUE_RECOVER);
118
	}
Melledy's avatar
Melledy committed
119
	
Melledy's avatar
Melledy committed
120
121
122
123
124
125
	@Override
	public float heal(float amount) {
		float healed = super.heal(amount);
		
		if (healed > 0f) {
			getScene().broadcastPacket(
126
				new PacketEntityFightPropChangeReasonNotify(this, FightProperty.FIGHT_PROP_CUR_HP, healed, PropChangeReason.PROP_CHANGE_REASON_ABILITY, ChangeHpReason.CHANGE_HP_REASON_CHANGE_HP_ADD_ABILITY)
Melledy's avatar
Melledy committed
127
128
129
130
131
132
			);
		}
		
		return healed;
	}
	
133
134
	public void clearEnergy(PropChangeReason reason) {
		FightProperty curEnergyProp = this.getAvatar().getSkillDepot().getElementType().getCurEnergyProp();
logictc's avatar
logictc committed
135
		this.avatar.setCurrentEnergy(curEnergyProp, 0);
136
137
138
139
140
141
142
			
		this.getScene().broadcastPacket(new PacketAvatarFightPropUpdateNotify(this.getAvatar(), curEnergyProp));
		this.getScene().broadcastPacket(new PacketEntityFightPropChangeReasonNotify(this, curEnergyProp, 0f, reason));
	}

	public void addEnergy(float amount, PropChangeReason reason) {
		this.addEnergy(amount, reason, false);
143
	}
144
	public void addEnergy(float amount, PropChangeReason reason, boolean isFlat) {
145
		// Get current and maximum energy for this avatar.
146
147
148
149
150
		FightProperty curEnergyProp = this.getAvatar().getSkillDepot().getElementType().getCurEnergyProp();
		FightProperty maxEnergyProp = this.getAvatar().getSkillDepot().getElementType().getMaxEnergyProp();

		float curEnergy = this.getFightProperty(curEnergyProp);
		float maxEnergy = this.getFightProperty(maxEnergyProp);
Melledy's avatar
Melledy committed
151
		
152
153
154
155
156
157
158
159
160
		// Get energy recharge.
		float energyRecharge = this.getFightProperty(FightProperty.FIGHT_PROP_CHARGE_EFFICIENCY);

		// Scale amount by energy recharge, if the amount is not flat.
		if (!isFlat) {
			amount *= energyRecharge;
		}

		// Determine the new energy value.
Melledy's avatar
Melledy committed
161
162
		float newEnergy = Math.min(curEnergy + amount, maxEnergy);
		
163
		// Set energy and notify.
Melledy's avatar
Melledy committed
164
		if (newEnergy != curEnergy) {
logictc's avatar
logictc committed
165
			this.avatar.setCurrentEnergy(curEnergyProp, newEnergy);
Melledy's avatar
Melledy committed
166
			
167
168
			this.getScene().broadcastPacket(new PacketAvatarFightPropUpdateNotify(this.getAvatar(), curEnergyProp));
			this.getScene().broadcastPacket(new PacketEntityFightPropChangeReasonNotify(this, curEnergyProp, newEnergy, reason));
Melledy's avatar
Melledy committed
169
170
171
		}
	} 
	
Melledy's avatar
Melledy committed
172
173
	public SceneAvatarInfo getSceneAvatarInfo() {
		SceneAvatarInfo.Builder avatarInfo = SceneAvatarInfo.newBuilder()
174
				.setUid(this.getPlayer().getUid())
Melledy's avatar
Melledy committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
				.setAvatarId(this.getAvatar().getAvatarId())
				.setGuid(this.getAvatar().getGuid())
				.setPeerId(this.getPlayer().getPeerId())
				.addAllTalentIdList(this.getAvatar().getTalentIdList())
				.setCoreProudSkillLevel(this.getAvatar().getCoreProudSkillLevel())
				.putAllSkillLevelMap(this.getAvatar().getSkillLevelMap())
				.setSkillDepotId(this.getAvatar().getSkillDepotId())
				.addAllInherentProudSkillList(this.getAvatar().getProudSkillList())
				.putAllProudSkillExtraLevelMap(this.getAvatar().getProudSkillBonusMap())
				.addAllTeamResonanceList(this.getAvatar().getPlayer().getTeamManager().getTeamResonances())
				.setWearingFlycloakId(this.getAvatar().getFlyCloak())
				.setCostumeId(this.getAvatar().getCostume())
				.setBornTime(this.getAvatar().getBornTime());
		
189
		for (GameItem item : avatar.getEquips().values()) {
Melledy's avatar
Melledy committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			if (item.getItemData().getEquipType() == EquipType.EQUIP_WEAPON) {
				avatarInfo.setWeapon(item.createSceneWeaponInfo());
			} else {
				avatarInfo.addReliquaryList(item.createSceneReliquaryInfo());
			}
			avatarInfo.addEquipIdList(item.getItemId());
		}
		
		return avatarInfo.build();
	}

	@Override
	public SceneEntityInfo toProto() {
		EntityAuthorityInfo authority = EntityAuthorityInfo.newBuilder()
				.setAbilityInfo(AbilitySyncStateInfo.newBuilder())
				.setRendererChangedInfo(EntityRendererChangedInfo.newBuilder())
				.setAiInfo(SceneEntityAiInfo.newBuilder().setIsAiOpen(true).setBornPos(Vector.newBuilder()))
				.setBornPos(Vector.newBuilder())
				.build();
		
		SceneEntityInfo.Builder entityInfo = SceneEntityInfo.newBuilder()
				.setEntityId(getId())
212
				.setEntityType(ProtEntityType.PROT_ENTITY_TYPE_AVATAR)
Melledy's avatar
Melledy committed
213
214
215
216
217
218
219
				.addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
				.setEntityClientData(EntityClientData.newBuilder())
				.setEntityAuthorityInfo(authority)
				.setLastMoveSceneTimeMs(this.getLastMoveSceneTimeMs())
				.setLastMoveReliableSeq(this.getLastMoveReliableSeq())
				.setLifeState(this.getLifeState().getValue());
		
220
		if (this.getScene() != null) {
Melledy's avatar
Melledy committed
221
222
223
224
225
226
227
			entityInfo.setMotionInfo(this.getMotionInfo());
		}
		
		for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) {
			if (entry.getIntKey() == 0) {
				continue;
			}
228
			FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
Melledy's avatar
Melledy committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
			entityInfo.addFightPropList(fightProp);
		}
		
		PropPair pair = PropPair.newBuilder()
				.setType(PlayerProperty.PROP_LEVEL.getId())
				.setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, getAvatar().getLevel()))
				.build();
		entityInfo.addPropList(pair);
		
		entityInfo.setAvatar(this.getSceneAvatarInfo());
		
		return entityInfo.build();
	}
		
	public AbilityControlBlock getAbilityControlBlock() {
		AvatarData data = this.getAvatar().getAvatarData();
		AbilityControlBlock.Builder abilityControlBlock = AbilityControlBlock.newBuilder();
		int embryoId = 0;
		
		// Add avatar abilities
		if (data.getAbilities() != null) {
			for (int id : data.getAbilities()) {
				AbilityEmbryo emb = AbilityEmbryo.newBuilder()
						.setAbilityId(++embryoId)
						.setAbilityNameHash(id)
254
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
255
256
257
258
259
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		// Add default abilities 
260
		for (int id : GameConstants.DEFAULT_ABILITY_HASHES) {
Melledy's avatar
Melledy committed
261
262
263
			AbilityEmbryo emb = AbilityEmbryo.newBuilder()
					.setAbilityId(++embryoId)
					.setAbilityNameHash(id)
264
					.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
265
266
267
268
269
270
271
272
					.build();
			abilityControlBlock.addAbilityEmbryoList(emb);
		}
		// Add team resonances 
		for (int id : this.getPlayer().getTeamManager().getTeamResonancesConfig()) {
			AbilityEmbryo emb = AbilityEmbryo.newBuilder()
					.setAbilityId(++embryoId)
					.setAbilityNameHash(id)
273
					.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
274
275
276
277
					.build();
			abilityControlBlock.addAbilityEmbryoList(emb);
		}
		// Add skill depot abilities
278
		AvatarSkillDepotData skillDepot = GameData.getAvatarSkillDepotDataMap().get(this.getAvatar().getSkillDepotId());
Melledy's avatar
Melledy committed
279
280
281
282
283
		if (skillDepot != null && skillDepot.getAbilities() != null) {
			for (int id : skillDepot.getAbilities()) {
				AbilityEmbryo emb = AbilityEmbryo.newBuilder()
						.setAbilityId(++embryoId)
						.setAbilityNameHash(id)
284
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
285
286
287
288
289
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		// Add equip abilities
290
291
		if (this.getAvatar().getExtraAbilityEmbryos().size() > 0) {
			for (String skill : this.getAvatar().getExtraAbilityEmbryos()) {
Melledy's avatar
Melledy committed
292
293
294
				AbilityEmbryo emb = AbilityEmbryo.newBuilder()
						.setAbilityId(++embryoId)
						.setAbilityNameHash(Utils.abilityHash(skill))
295
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
296
297
298
299
300
301
302
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		
		//
		return abilityControlBlock.build();
Melledy's avatar
Melledy committed
303
	}
Melledy's avatar
Melledy committed
304
}