EntityAvatar.java 8.57 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
15
import emu.grasscutter.game.world.Scene;
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
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;
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;
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;

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

59
	public Player getPlayer() {
Melledy's avatar
Melledy committed
60
61
62
63
64
65
66
67
68
69
70
71
72
		return avatar.getPlayer();
	}

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

73
	public Avatar getAvatar() {
Melledy's avatar
Melledy committed
74
75
76
77
78
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
		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) {
104
		this.killedType = PlayerDieType.PLAYER_DIE_KILL_BY_MONSTER;
Melledy's avatar
Melledy committed
105
106
		this.killedBy = killerId;
	}
107
108
109
110
111

	public void onDeath(PlayerDieType dieType, int killerId) {
		this.killedType = dieType;
		this.killedBy = killerId;
	}
Melledy's avatar
Melledy committed
112
113
114
	
	public SceneAvatarInfo getSceneAvatarInfo() {
		SceneAvatarInfo.Builder avatarInfo = SceneAvatarInfo.newBuilder()
115
				.setUid(this.getPlayer().getUid())
Melledy's avatar
Melledy committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
				.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());
		
130
		for (GameItem item : avatar.getEquips().values()) {
Melledy's avatar
Melledy committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
			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())
153
				.setEntityType(ProtEntityType.PROT_ENTITY_AVATAR)
Melledy's avatar
Melledy committed
154
155
156
157
158
159
160
				.addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
				.setEntityClientData(EntityClientData.newBuilder())
				.setEntityAuthorityInfo(authority)
				.setLastMoveSceneTimeMs(this.getLastMoveSceneTimeMs())
				.setLastMoveReliableSeq(this.getLastMoveReliableSeq())
				.setLifeState(this.getLifeState().getValue());
		
161
		if (this.getScene() != null) {
Melledy's avatar
Melledy committed
162
163
164
165
166
167
168
			entityInfo.setMotionInfo(this.getMotionInfo());
		}
		
		for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) {
			if (entry.getIntKey() == 0) {
				continue;
			}
169
			FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
Melledy's avatar
Melledy committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
			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)
195
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
196
197
198
199
200
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		// Add default abilities 
201
		for (int id : GameConstants.DEFAULT_ABILITY_HASHES) {
Melledy's avatar
Melledy committed
202
203
204
			AbilityEmbryo emb = AbilityEmbryo.newBuilder()
					.setAbilityId(++embryoId)
					.setAbilityNameHash(id)
205
					.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
206
207
208
209
210
211
212
213
					.build();
			abilityControlBlock.addAbilityEmbryoList(emb);
		}
		// Add team resonances 
		for (int id : this.getPlayer().getTeamManager().getTeamResonancesConfig()) {
			AbilityEmbryo emb = AbilityEmbryo.newBuilder()
					.setAbilityId(++embryoId)
					.setAbilityNameHash(id)
214
					.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
215
216
217
218
					.build();
			abilityControlBlock.addAbilityEmbryoList(emb);
		}
		// Add skill depot abilities
219
		AvatarSkillDepotData skillDepot = GameData.getAvatarSkillDepotDataMap().get(this.getAvatar().getSkillDepotId());
Melledy's avatar
Melledy committed
220
221
222
223
224
		if (skillDepot != null && skillDepot.getAbilities() != null) {
			for (int id : skillDepot.getAbilities()) {
				AbilityEmbryo emb = AbilityEmbryo.newBuilder()
						.setAbilityId(++embryoId)
						.setAbilityNameHash(id)
225
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
226
227
228
229
230
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		// Add equip abilities
231
232
		if (this.getAvatar().getExtraAbilityEmbryos().size() > 0) {
			for (String skill : this.getAvatar().getExtraAbilityEmbryos()) {
Melledy's avatar
Melledy committed
233
234
235
				AbilityEmbryo emb = AbilityEmbryo.newBuilder()
						.setAbilityId(++embryoId)
						.setAbilityNameHash(Utils.abilityHash(skill))
236
						.setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
Melledy's avatar
Melledy committed
237
238
239
240
241
242
243
244
245
						.build();
				abilityControlBlock.addAbilityEmbryoList(emb);
			}
		}
		
		//
		return abilityControlBlock.build();
	} 
}