GameEntity.java 7.23 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

    // Abilities
AnimeGitB's avatar
AnimeGitB committed
41
    private Object2FloatMap<String> metaOverrideMap;
github-actions's avatar
github-actions committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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
65
    public Object2FloatMap<String> getMetaOverrideMap() {
github-actions's avatar
github-actions committed
66
        if (this.metaOverrideMap == null) {
AnimeGitB's avatar
AnimeGitB committed
67
            this.metaOverrideMap = new Object2FloatOpenHashMap<>();
github-actions's avatar
github-actions committed
68
69
70
71
72
73
74
75
76
77
        }
        return this.metaOverrideMap;
    }

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

AnimeGitB's avatar
AnimeGitB committed
79
    public abstract Int2FloatMap getFightProperties();
KingRainbow44's avatar
KingRainbow44 committed
80

github-actions's avatar
github-actions committed
81
    public abstract Position getPosition();
KingRainbow44's avatar
KingRainbow44 committed
82

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

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

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

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

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

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

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

github-actions's avatar
github-actions committed
109
    public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) {
110
        for (Int2FloatMap.Entry entry : this.getFightProperties().int2FloatEntrySet()) {
github-actions's avatar
github-actions committed
111
112
113
114
115
116
117
            if (entry.getIntKey() == 0) {
                continue;
            }
            FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
            entityInfo.addFightPropList(fightProp);
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
118

github-actions's avatar
github-actions committed
119
120
    protected MotionInfo getMotionInfo() {
        MotionInfo proto = MotionInfo.newBuilder()
121
122
                .setPos(this.getPosition().toProto())
                .setRot(this.getRotation().toProto())
github-actions's avatar
github-actions committed
123
124
125
                .setSpeed(Vector.newBuilder())
                .setState(this.getMotionState())
                .build();
Melledy's avatar
Melledy committed
126

github-actions's avatar
github-actions committed
127
128
        return proto;
    }
KingRainbow44's avatar
KingRainbow44 committed
129

github-actions's avatar
github-actions committed
130
131
132
133
    public float heal(float amount) {
        if (this.getFightProperties() == null) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
134

135
136
        float curHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
        float maxHp = this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
KingRainbow44's avatar
KingRainbow44 committed
137

github-actions's avatar
github-actions committed
138
139
140
        if (curHp >= maxHp) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
141

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

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

github-actions's avatar
github-actions committed
147
148
        return healed;
    }
KingRainbow44's avatar
KingRainbow44 committed
149

github-actions's avatar
github-actions committed
150
    public void damage(float amount) {
151
        this.damage(amount, 0);
github-actions's avatar
github-actions committed
152
    }
KingRainbow44's avatar
KingRainbow44 committed
153

github-actions's avatar
github-actions committed
154
    public void damage(float amount, int killerId) {
155
156
        // Check if the entity has properties.
        if (this.getFightProperties() == null) {
github-actions's avatar
github-actions committed
157
158
159
            return;
        }

160
161
        // Invoke entity damage event.
        EntityDamageEvent event = new EntityDamageEvent(this, amount, this.getScene().getEntityById(killerId));
github-actions's avatar
github-actions committed
162
        event.call();
赵怡然's avatar
赵怡然 committed
163
        if (event.isCanceled()) {
164
165
            return; // If the event is canceled, do not damage the entity.
        }
github-actions's avatar
github-actions committed
166
167

        if (getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) != Float.POSITIVE_INFINITY) {
赵怡然's avatar
赵怡然 committed
168
169
170
          // Add negative HP to the current HP property.
          this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -(event.getDamage()));
        }
github-actions's avatar
github-actions committed
171
172
173

        // Check if dead
        boolean isDead = false;
174
175
        if (this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
            this.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
github-actions's avatar
github-actions committed
176
177
178
179
180
181
            isDead = true;
        }

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

182
        // Check if dead.
github-actions's avatar
github-actions committed
183
        if (isDead) {
184
            this.getScene().killEntity(this, killerId);
github-actions's avatar
github-actions committed
185
186
        }
    }
KingRainbow44's avatar
KingRainbow44 committed
187
188
189
190
191
192
193
194
195
196
197
198

    /**
     * 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
199
    /**
Melledy's avatar
Melledy committed
200
201
     * Called when a player interacts with this entity
     * @param player Player that is interacting with this entity
github-actions's avatar
github-actions committed
202
     * @param interactReq Interact request protobuf data
Melledy's avatar
Melledy committed
203
204
     */
    public void onInteract(Player player, GadgetInteractReq interactReq) {
github-actions's avatar
github-actions committed
205

Melledy's avatar
Melledy committed
206
    }
github-actions's avatar
github-actions committed
207

208
209
210
    /**
     * Called when this entity is added to the world
     */
github-actions's avatar
github-actions committed
211
    public void onCreate() {
KingRainbow44's avatar
KingRainbow44 committed
212

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

github-actions's avatar
github-actions committed
215
    /**
216
217
218
     * Called when this entity dies
     * @param killerId Entity id of the entity that killed this entity
     */
github-actions's avatar
github-actions committed
219
    public void onDeath(int killerId) {
220
221
222
        // Invoke entity death event.
        EntityDeathEvent event = new EntityDeathEvent(this, killerId);
        event.call();
github-actions's avatar
github-actions committed
223
    }
KingRainbow44's avatar
KingRainbow44 committed
224

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