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

AnimeGitB's avatar
AnimeGitB committed
36
    @Getter @Setter private MotionState motionState;
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
    private Int2ObjectMap<String> metaModifiers;

    public GameEntity(Scene scene) {
        this.scene = scene;
AnimeGitB's avatar
AnimeGitB committed
48
        this.motionState = MotionState.MOTION_STATE_NONE;
github-actions's avatar
github-actions committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    }

    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 void setFightProperty(FightProperty prop, float value) {
        this.getFightProperties().put(prop.getId(), value);
    }
KingRainbow44's avatar
KingRainbow44 committed
90

AnimeGitB's avatar
AnimeGitB committed
91
    public void setFightProperty(int id, float value) {
github-actions's avatar
github-actions committed
92
93
        this.getFightProperties().put(id, value);
    }
94

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

github-actions's avatar
github-actions committed
99
    public float getFightProperty(FightProperty prop) {
100
        return this.getFightProperties().getOrDefault(prop.getId(), 0f);
github-actions's avatar
github-actions committed
101
    }
102

103
104
105
106
    public boolean hasFightProperty(FightProperty prop) {
        return this.getFightProperties().containsKey(prop.getId());
    }

github-actions's avatar
github-actions committed
107
    public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) {
AnimeGitB's avatar
AnimeGitB committed
108
109
110
111
        this.getFightProperties().forEach((key, value) -> {
            if (key == 0) return;
            entityInfo.addFightPropList(FightPropPair.newBuilder().setPropType(key).setPropValue(value).build());
        });
github-actions's avatar
github-actions committed
112
    }
KingRainbow44's avatar
KingRainbow44 committed
113

github-actions's avatar
github-actions committed
114
115
    protected MotionInfo getMotionInfo() {
        MotionInfo proto = MotionInfo.newBuilder()
116
117
                .setPos(this.getPosition().toProto())
                .setRot(this.getRotation().toProto())
github-actions's avatar
github-actions committed
118
119
120
                .setSpeed(Vector.newBuilder())
                .setState(this.getMotionState())
                .build();
Melledy's avatar
Melledy committed
121

github-actions's avatar
github-actions committed
122
123
        return proto;
    }
KingRainbow44's avatar
KingRainbow44 committed
124

github-actions's avatar
github-actions committed
125
126
127
128
    public float heal(float amount) {
        if (this.getFightProperties() == null) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
129

130
131
        float curHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
        float maxHp = this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
KingRainbow44's avatar
KingRainbow44 committed
132

github-actions's avatar
github-actions committed
133
134
135
        if (curHp >= maxHp) {
            return 0f;
        }
KingRainbow44's avatar
KingRainbow44 committed
136

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

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

github-actions's avatar
github-actions committed
142
143
        return healed;
    }
KingRainbow44's avatar
KingRainbow44 committed
144

github-actions's avatar
github-actions committed
145
    public void damage(float amount) {
146
        this.damage(amount, 0);
github-actions's avatar
github-actions committed
147
    }
KingRainbow44's avatar
KingRainbow44 committed
148

github-actions's avatar
github-actions committed
149
    public void damage(float amount, int killerId) {
150
        // Check if the entity has properties.
151
        if (this.getFightProperties() == null || !hasFightProperty(FightProperty.FIGHT_PROP_CUR_HP)) {
github-actions's avatar
github-actions committed
152
153
154
            return;
        }

155
156
        // Invoke entity damage event.
        EntityDamageEvent event = new EntityDamageEvent(this, amount, this.getScene().getEntityById(killerId));
github-actions's avatar
github-actions committed
157
        event.call();
赵怡然's avatar
赵怡然 committed
158
        if (event.isCanceled()) {
159
160
            return; // If the event is canceled, do not damage the entity.
        }
github-actions's avatar
github-actions committed
161

162
163
164
165
        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
166
        }
github-actions's avatar
github-actions committed
167
168
169

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

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

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

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

Melledy's avatar
Melledy committed
202
    }
github-actions's avatar
github-actions committed
203

204
205
206
    /**
     * Called when this entity is added to the world
     */
github-actions's avatar
github-actions committed
207
    public void onCreate() {
KingRainbow44's avatar
KingRainbow44 committed
208

github-actions's avatar
github-actions committed
209
    }
KingRainbow44's avatar
KingRainbow44 committed
210

211
212
213
214
    public void onRemoved() {

    }

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
}