EntityAvatar.java 13.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.excels.AvatarData;
import emu.grasscutter.data.excels.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;
19
import emu.grasscutter.net.proto.ChangeEnergyReasonOuterClass.ChangeEnergyReason;
Melledy's avatar
Melledy committed
20
import emu.grasscutter.net.proto.ChangeHpReasonOuterClass.ChangeHpReason;
Melledy's avatar
Melledy committed
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.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;
32
import emu.grasscutter.server.event.player.PlayerMoveEvent;
Melledy's avatar
Melledy committed
33
import emu.grasscutter.server.packet.send.PacketAvatarFightPropUpdateNotify;
Melledy's avatar
Melledy committed
34
import emu.grasscutter.server.packet.send.PacketEntityFightPropChangeReasonNotify;
35
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
Melledy's avatar
Melledy committed
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;
AnimeGitB's avatar
AnimeGitB committed
40
import lombok.Getter;
41
import lombok.val;
Melledy's avatar
Melledy committed
42

43
public class EntityAvatar extends GameEntity {
AnimeGitB's avatar
AnimeGitB committed
44
    @Getter private final Avatar avatar;
KingRainbow44's avatar
KingRainbow44 committed
45

AnimeGitB's avatar
AnimeGitB committed
46
47
48
49
50
51
    @Getter private PlayerDieType killedType;
    @Getter private int killedBy;

    public EntityAvatar(Avatar avatar) {
        this(null, avatar);
    }
KingRainbow44's avatar
KingRainbow44 committed
52

github-actions's avatar
github-actions committed
53
54
55
56
    public EntityAvatar(Scene scene, Avatar avatar) {
        super(scene);
        this.avatar = avatar;
        this.avatar.setCurrentEnergy();
AnimeGitB's avatar
AnimeGitB committed
57
58
        if (scene != null)
            this.id = getScene().getWorld().getNextEntityId(EntityIdType.AVATAR);
KingRainbow44's avatar
KingRainbow44 committed
59

github-actions's avatar
github-actions committed
60
61
62
63
64
        GameItem weapon = this.getAvatar().getWeapon();
        if (weapon != null) {
            weapon.setWeaponEntityId(getScene().getWorld().getNextEntityId(EntityIdType.WEAPON));
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
65

AnimeGitB's avatar
AnimeGitB committed
66
    public Player getPlayer() {return this.avatar.getPlayer();}
Melledy's avatar
Melledy committed
67

github-actions's avatar
github-actions committed
68
    @Override
AnimeGitB's avatar
AnimeGitB committed
69
    public Position getPosition() {return getPlayer().getPosition();}
github-actions's avatar
github-actions committed
70
71

    @Override
AnimeGitB's avatar
AnimeGitB committed
72
    public Position getRotation() {return getPlayer().getRotation();}
github-actions's avatar
github-actions committed
73
74
75
76
77
78

    @Override
    public boolean isAlive() {
        return this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) > 0f;
    }

AnimeGitB's avatar
AnimeGitB committed
79
    @Override public Int2FloatMap getFightProperties() {return getAvatar().getFightProperties();}
github-actions's avatar
github-actions committed
80
81
82
83
84
85
86

    public int getWeaponEntityId() {
        if (getAvatar().getWeapon() != null) {
            return getAvatar().getWeapon().getWeaponEntityId();
        }
        return 0;
    }
Melledy's avatar
Melledy committed
87

github-actions's avatar
github-actions committed
88
89
    @Override
    public void onDeath(int killerId) {
90
91
        super.onDeath(killerId); // Invoke super class's onDeath() method.

github-actions's avatar
github-actions committed
92
93
94
95
        this.killedType = PlayerDieType.PLAYER_DIE_TYPE_KILL_BY_MONSTER;
        this.killedBy = killerId;
        clearEnergy(ChangeEnergyReason.CHANGE_ENERGY_REASON_NONE);
    }
96

github-actions's avatar
github-actions committed
97
    public void onDeath(PlayerDieType dieType, int killerId) {
98
99
        super.onDeath(killerId); // Invoke super class's onDeath() method.

github-actions's avatar
github-actions committed
100
101
102
103
        this.killedType = dieType;
        this.killedBy = killerId;
        clearEnergy(ChangeEnergyReason.CHANGE_ENERGY_REASON_NONE);
    }
KingRainbow44's avatar
KingRainbow44 committed
104

github-actions's avatar
github-actions committed
105
106
    @Override
    public float heal(float amount) {
107
108
109
110
        // Do not heal character if they are dead
        if (!this.isAlive()) {
            return 0f;
        }
github-actions's avatar
github-actions committed
111

github-actions's avatar
github-actions committed
112
        float healed = super.heal(amount);
KingRainbow44's avatar
KingRainbow44 committed
113

github-actions's avatar
github-actions committed
114
115
        if (healed > 0f) {
            getScene().broadcastPacket(
github-actions's avatar
github-actions committed
116
                new PacketEntityFightPropChangeReasonNotify(this, FightProperty.FIGHT_PROP_CUR_HP, healed, PropChangeReason.PROP_CHANGE_REASON_ABILITY, ChangeHpReason.CHANGE_HP_REASON_ADD_ABILITY)
github-actions's avatar
github-actions committed
117
118
            );
        }
KingRainbow44's avatar
KingRainbow44 committed
119

github-actions's avatar
github-actions committed
120
121
        return healed;
    }
KingRainbow44's avatar
KingRainbow44 committed
122

github-actions's avatar
github-actions committed
123
124
    public void clearEnergy(ChangeEnergyReason reason) {
        // Fight props.
AnimeGitB's avatar
AnimeGitB committed
125
126
        val curEnergyProp = this.getAvatar().getSkillDepot().getElementType().getCurEnergyProp();
        float curEnergy = this.getFightProperty(curEnergyProp);
127

github-actions's avatar
github-actions committed
128
129
        // Set energy to zero.
        this.avatar.setCurrentEnergy(curEnergyProp, 0);
130

github-actions's avatar
github-actions committed
131
132
        // Send packets.
        this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, curEnergyProp));
133

github-actions's avatar
github-actions committed
134
        if (reason == ChangeEnergyReason.CHANGE_ENERGY_REASON_SKILL_START) {
AnimeGitB's avatar
AnimeGitB committed
135
            this.getScene().broadcastPacket(new PacketEntityFightPropChangeReasonNotify(this, curEnergyProp, -curEnergy, reason));
github-actions's avatar
github-actions committed
136
137
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
138

github-actions's avatar
github-actions committed
139
140
141
142
143
    public void addEnergy(float amount, PropChangeReason reason) {
        this.addEnergy(amount, reason, false);
    }
    public void addEnergy(float amount, PropChangeReason reason, boolean isFlat) {
        // Get current and maximum energy for this avatar.
AnimeGitB's avatar
AnimeGitB committed
144
145
146
        val elementType = this.getAvatar().getSkillDepot().getElementType();
        val curEnergyProp = elementType.getCurEnergyProp();
        val maxEnergyProp = elementType.getMaxEnergyProp();
147

github-actions's avatar
github-actions committed
148
149
        float curEnergy = this.getFightProperty(curEnergyProp);
        float maxEnergy = this.getFightProperty(maxEnergyProp);
KingRainbow44's avatar
KingRainbow44 committed
150

github-actions's avatar
github-actions committed
151
152
        // Scale amount by energy recharge, if the amount is not flat.
        if (!isFlat) {
AnimeGitB's avatar
AnimeGitB committed
153
            amount *= this.getFightProperty(FightProperty.FIGHT_PROP_CHARGE_EFFICIENCY);
github-actions's avatar
github-actions committed
154
        }
155

github-actions's avatar
github-actions committed
156
157
158
159
160
161
162
163
164
165
166
167
168
        // Determine the new energy value.
        float newEnergy = Math.min(curEnergy + amount, maxEnergy);

        // Set energy and notify.
        if (newEnergy != curEnergy) {
            this.avatar.setCurrentEnergy(curEnergyProp, newEnergy);

            this.getScene().broadcastPacket(new PacketAvatarFightPropUpdateNotify(this.getAvatar(), curEnergyProp));
            this.getScene().broadcastPacket(new PacketEntityFightPropChangeReasonNotify(this, curEnergyProp, newEnergy, reason));
        }
    }

    public SceneAvatarInfo getSceneAvatarInfo() {
169
170
        val avatar = this.getAvatar();
        val player = this.getPlayer();
github-actions's avatar
github-actions committed
171
        SceneAvatarInfo.Builder avatarInfo = SceneAvatarInfo.newBuilder()
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                .setUid(player.getUid())
                .setAvatarId(avatar.getAvatarId())
                .setGuid(avatar.getGuid())
                .setPeerId(player.getPeerId())
                .addAllTalentIdList(avatar.getTalentIdList())
                .setCoreProudSkillLevel(avatar.getCoreProudSkillLevel())
                .putAllSkillLevelMap(avatar.getSkillLevelMap())
                .setSkillDepotId(avatar.getSkillDepotId())
                .addAllInherentProudSkillList(avatar.getProudSkillList())
                .putAllProudSkillExtraLevelMap(avatar.getProudSkillBonusMap())
                .addAllTeamResonanceList(player.getTeamManager().getTeamResonances())
                .setWearingFlycloakId(avatar.getFlyCloak())
                .setCostumeId(avatar.getCostume())
                .setBornTime(avatar.getBornTime());
github-actions's avatar
github-actions committed
186
187
188
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
218
219
220
221

        for (GameItem item : avatar.getEquips().values()) {
            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())
                .setEntityType(ProtEntityType.PROT_ENTITY_TYPE_AVATAR)
                .addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
                .setEntityClientData(EntityClientData.newBuilder())
                .setEntityAuthorityInfo(authority)
                .setLastMoveSceneTimeMs(this.getLastMoveSceneTimeMs())
                .setLastMoveReliableSeq(this.getLastMoveReliableSeq())
                .setLifeState(this.getLifeState().getValue());

        if (this.getScene() != null) {
            entityInfo.setMotionInfo(this.getMotionInfo());
        }

AnimeGitB's avatar
AnimeGitB committed
222
        this.addAllFightPropsToEntityInfo(entityInfo);
github-actions's avatar
github-actions committed
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

        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)
                        .setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
                        .build();
                abilityControlBlock.addAbilityEmbryoList(emb);
            }
        }
        // Add default abilities
        for (int id : GameConstants.DEFAULT_ABILITY_HASHES) {
            AbilityEmbryo emb = AbilityEmbryo.newBuilder()
                    .setAbilityId(++embryoId)
                    .setAbilityNameHash(id)
                    .setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
                    .build();
            abilityControlBlock.addAbilityEmbryoList(emb);
        }
        // Add team resonances
        for (int id : this.getPlayer().getTeamManager().getTeamResonancesConfig()) {
            AbilityEmbryo emb = AbilityEmbryo.newBuilder()
                    .setAbilityId(++embryoId)
                    .setAbilityNameHash(id)
                    .setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
                    .build();
            abilityControlBlock.addAbilityEmbryoList(emb);
        }
        // Add skill depot abilities
        AvatarSkillDepotData skillDepot = GameData.getAvatarSkillDepotDataMap().get(this.getAvatar().getSkillDepotId());
        if (skillDepot != null && skillDepot.getAbilities() != null) {
            for (int id : skillDepot.getAbilities()) {
                AbilityEmbryo emb = AbilityEmbryo.newBuilder()
                        .setAbilityId(++embryoId)
                        .setAbilityNameHash(id)
                        .setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
                        .build();
                abilityControlBlock.addAbilityEmbryoList(emb);
            }
        }
        // Add equip abilities
        if (this.getAvatar().getExtraAbilityEmbryos().size() > 0) {
            for (String skill : this.getAvatar().getExtraAbilityEmbryos()) {
                AbilityEmbryo emb = AbilityEmbryo.newBuilder()
                        .setAbilityId(++embryoId)
                        .setAbilityNameHash(Utils.abilityHash(skill))
                        .setAbilityOverrideNameHash(GameConstants.DEFAULT_ABILITY_NAME)
                        .build();
                abilityControlBlock.addAbilityEmbryoList(emb);
            }
        }

        //
        return abilityControlBlock.build();
    }
KingRainbow44's avatar
KingRainbow44 committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

    /**
     * Move this entity to a new position.
     * Additionally invoke player move event.
     * @param newPosition The new position.
     * @param rotation The new rotation.
     */
    @Override public void move(Position newPosition, Position rotation) {
        // Invoke player move event.
        PlayerMoveEvent event = new PlayerMoveEvent(
            this.getPlayer(), PlayerMoveEvent.MoveType.PLAYER,
            this.getPosition(), newPosition
        ); event.call();

        // Set position and rotation.
        super.move(event.getDestination(), rotation);
    }
Melledy's avatar
Melledy committed
313
}