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

Melledy's avatar
Melledy committed
3
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
4
5
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.LifeState;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.world.SpawnDataEntry;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.world.World;
9
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
Melledy's avatar
Melledy committed
11
12
13
14
import emu.grasscutter.net.proto.MotionInfoOuterClass.MotionInfo;
import emu.grasscutter.net.proto.MotionStateOuterClass.MotionState;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
15
import emu.grasscutter.server.event.entity.EntityDamageEvent;
16
import emu.grasscutter.server.event.entity.EntityDeathEvent;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
Melledy's avatar
Melledy committed
18
import emu.grasscutter.utils.Position;
19
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
Melledy's avatar
Melledy committed
20
21
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
AnimeGitB's avatar
AnimeGitB committed
22
23
24
25
import it.unimi.dsi.fastutil.objects.Object2FloatMap;
import it.unimi.dsi.fastutil.objects.Object2FloatOpenHashMap;
import lombok.Getter;
import lombok.Setter;
Melledy's avatar
Melledy committed
26

27
public abstract class GameEntity {
AnimeGitB's avatar
AnimeGitB committed
28
29
30
    @Getter protected int id;
    @Getter private final Scene scene;
    @Getter @Setter private SpawnDataEntry spawnEntry;
github-actions's avatar
github-actions committed
31

AnimeGitB's avatar
AnimeGitB committed
32
33
34
    @Getter @Setter private int blockId;
    @Getter @Setter private int configId;
    @Getter @Setter private int groupId;
github-actions's avatar
github-actions committed
35
36

    private MotionState moveState;
AnimeGitB's avatar
AnimeGitB committed
37
38
    @Getter @Setter private int lastMoveSceneTimeMs;
    @Getter @Setter private int lastMoveReliableSeq;
github-actions's avatar
github-actions committed
39

40
41
    @Getter @Setter private boolean lockHP;

github-actions's avatar
github-actions committed
42
    // Abilities
AnimeGitB's avatar
AnimeGitB committed
43
    private Object2FloatMap<String> metaOverrideMap;
github-actions's avatar
github-actions committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    private Int2ObjectMap<String> metaModifiers;

    public GameEntity(Scene scene) {
        this.scene = scene;
        this.moveState = MotionState.MOTION_STATE_NONE;
    }

    public int getEntityType() {
        return this.getId() >> 24;
    }

    public World getWorld() {
        return this.getScene().getWorld();
    }

    public boolean isAlive() {
        return true;
    }

    public LifeState getLifeState() {
        return this.isAlive() ? LifeState.LIFE_ALIVE : LifeState.LIFE_DEAD;
    }

AnimeGitB's avatar
AnimeGitB committed
67
    public Object2FloatMap<String> getMetaOverrideMap() {
github-actions's avatar
github-actions committed
68
        if (this.metaOverrideMap == null) {
AnimeGitB's avatar
AnimeGitB committed
69
            this.metaOverrideMap = new Object2FloatOpenHashMap<>();
github-actions's avatar
github-actions committed
70
71
72
73
74
75
76
77
78
79
        }
        return this.metaOverrideMap;
    }

    public Int2ObjectMap<String> getMetaModifiers() {
        if (this.metaModifiers == null) {
            this.metaModifiers = new Int2ObjectOpenHashMap<>();
        }
        return this.metaModifiers;
    }
Melledy's avatar
Melledy committed
80

AnimeGitB's avatar
AnimeGitB committed
81
    public abstract Int2FloatMap getFightProperties();
KingRainbow44's avatar
KingRainbow44 committed
82

github-actions's avatar
github-actions committed
83
    public abstract Position getPosition();
KingRainbow44's avatar
KingRainbow44 committed
84

github-actions's avatar
github-actions committed
85
    public abstract Position getRotation();
KingRainbow44's avatar
KingRainbow44 committed
86

github-actions's avatar
github-actions committed
87
88
89
    public MotionState getMotionState() {
        return moveState;
    }
Melledy's avatar
Melledy committed
90

github-actions's avatar
github-actions committed
91
92
93
    public void setMotionState(MotionState moveState) {
        this.moveState = moveState;
    }
KingRainbow44's avatar
KingRainbow44 committed
94

github-actions's avatar
github-actions committed
95
96
97
    public void setFightProperty(FightProperty prop, float value) {
        this.getFightProperties().put(prop.getId(), value);
    }
KingRainbow44's avatar
KingRainbow44 committed
98

AnimeGitB's avatar
AnimeGitB committed
99
    public void setFightProperty(int id, float value) {
github-actions's avatar
github-actions committed
100
101
        this.getFightProperties().put(id, value);
    }
102

github-actions's avatar
github-actions committed
103
    public void addFightProperty(FightProperty prop, float value) {
104
        this.getFightProperties().put(prop.getId(), this.getFightProperty(prop) + value);
github-actions's avatar
github-actions committed
105
    }
KingRainbow44's avatar
KingRainbow44 committed
106

github-actions's avatar
github-actions committed
107
    public float getFightProperty(FightProperty prop) {
108
        return this.getFightProperties().getOrDefault(prop.getId(), 0f);
github-actions's avatar
github-actions committed
109
    }
110

111
112
113
114
    public boolean hasFightProperty(FightProperty prop) {
        return this.getFightProperties().containsKey(prop.getId());
    }

github-actions's avatar
github-actions committed
115
    public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) {
116
        for (Int2FloatMap.Entry entry : this.getFightProperties().int2FloatEntrySet()) {
github-actions's avatar
github-actions committed
117
118
119
120
121
122
123
            if (entry.getIntKey() == 0) {
                continue;
            }
            FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
            entityInfo.addFightPropList(fightProp);
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
124

github-actions's avatar
github-actions committed
125
126
    protected MotionInfo getMotionInfo() {
        MotionInfo proto = MotionInfo.newBuilder()
127
128
                .setPos(this.getPosition().toProto())
                .setRot(this.getRotation().toProto())
github-actions's avatar
github-actions committed
129
130
131
                .setSpeed(Vector.newBuilder())
                .setState(this.getMotionState())
                .build();
Melledy's avatar
Melledy committed
132

github-actions's avatar
github-actions committed
133
134
        return proto;
    }
KingRainbow44's avatar
KingRainbow44 committed
135

github-actions's avatar
github-actions committed
136
137
138
139
    public float heal(float amount) {
        if (this.getFightProperties() == null) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
140

141
142
        float curHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
        float maxHp = this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
KingRainbow44's avatar
KingRainbow44 committed
143

github-actions's avatar
github-actions committed
144
145
146
        if (curHp >= maxHp) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
147

github-actions's avatar
github-actions committed
148
149
        float healed = Math.min(maxHp - curHp, amount);
        this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, healed);
KingRainbow44's avatar
KingRainbow44 committed
150

151
        this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
KingRainbow44's avatar
KingRainbow44 committed
152

github-actions's avatar
github-actions committed
153
154
        return healed;
    }
KingRainbow44's avatar
KingRainbow44 committed
155

github-actions's avatar
github-actions committed
156
    public void damage(float amount) {
157
        this.damage(amount, 0);
github-actions's avatar
github-actions committed
158
    }
KingRainbow44's avatar
KingRainbow44 committed
159

github-actions's avatar
github-actions committed
160
    public void damage(float amount, int killerId) {
161
        // Check if the entity has properties.
162
        if (this.getFightProperties() == null || !hasFightProperty(FightProperty.FIGHT_PROP_CUR_HP)) {
github-actions's avatar
github-actions committed
163
164
165
            return;
        }

166
167
        // Invoke entity damage event.
        EntityDamageEvent event = new EntityDamageEvent(this, amount, this.getScene().getEntityById(killerId));
github-actions's avatar
github-actions committed
168
        event.call();
赵怡然's avatar
赵怡然 committed
169
        if (event.isCanceled()) {
170
171
            return; // If the event is canceled, do not damage the entity.
        }
github-actions's avatar
github-actions committed
172

173
174
175
176
        float curHp = getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
        if (curHp != Float.POSITIVE_INFINITY && !lockHP || lockHP && curHp <= event.getDamage()) {
            // Add negative HP to the current HP property.
            this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -(event.getDamage()));
赵怡然's avatar
赵怡然 committed
177
        }
github-actions's avatar
github-actions committed
178
179
180

        // Check if dead
        boolean isDead = false;
181
182
        if (this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
            this.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
github-actions's avatar
github-actions committed
183
184
185
186
187
188
            isDead = true;
        }

        // Packets
        this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));

189
        // Check if dead.
github-actions's avatar
github-actions committed
190
        if (isDead) {
191
            this.getScene().killEntity(this, killerId);
github-actions's avatar
github-actions committed
192
193
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
194
195
196
197
198
199
200
201
202
203
204
205

    /**
     * Move this entity to a new position.
     * @param position The new position.
     * @param rotation The new rotation.
     */
    public void move(Position position, Position rotation) {
        // Set the position and rotation.
        this.getPosition().set(position);
        this.getRotation().set(rotation);
    }

github-actions's avatar
github-actions committed
206
    /**
Melledy's avatar
Melledy committed
207
208
     * Called when a player interacts with this entity
     * @param player Player that is interacting with this entity
github-actions's avatar
github-actions committed
209
     * @param interactReq Interact request protobuf data
Melledy's avatar
Melledy committed
210
211
     */
    public void onInteract(Player player, GadgetInteractReq interactReq) {
github-actions's avatar
github-actions committed
212

Melledy's avatar
Melledy committed
213
    }
github-actions's avatar
github-actions committed
214

215
216
217
    /**
     * Called when this entity is added to the world
     */
github-actions's avatar
github-actions committed
218
    public void onCreate() {
KingRainbow44's avatar
KingRainbow44 committed
219

github-actions's avatar
github-actions committed
220
    }
KingRainbow44's avatar
KingRainbow44 committed
221

222
223
224
225
    public void onRemoved() {

    }

github-actions's avatar
github-actions committed
226
    /**
227
228
229
     * Called when this entity dies
     * @param killerId Entity id of the entity that killed this entity
     */
github-actions's avatar
github-actions committed
230
    public void onDeath(int killerId) {
231
232
233
        // Invoke entity death event.
        EntityDeathEvent event = new EntityDeathEvent(this, killerId);
        event.call();
github-actions's avatar
github-actions committed
234
    }
KingRainbow44's avatar
KingRainbow44 committed
235

github-actions's avatar
github-actions committed
236
    public abstract SceneEntityInfo toProto();
Melledy's avatar
Melledy committed
237
}