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

3
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
4
import emu.grasscutter.data.excels.GadgetData;
Akka's avatar
Akka committed
5
import emu.grasscutter.game.entity.gadget.*;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.player.Player;
7
8
import emu.grasscutter.game.props.EntityIdType;
import emu.grasscutter.game.props.EntityType;
Akka's avatar
Akka committed
9
import emu.grasscutter.game.props.LifeState;
10
import emu.grasscutter.game.props.PlayerProperty;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.world.Scene;
12
import emu.grasscutter.game.world.SpawnDataEntry;
13
14
15
16
17
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;
18
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
20
21
22
23
24
25
26
import emu.grasscutter.net.proto.MotionInfoOuterClass.MotionInfo;
import emu.grasscutter.net.proto.PropPairOuterClass.PropPair;
import emu.grasscutter.net.proto.ProtEntityTypeOuterClass.ProtEntityType;
import emu.grasscutter.net.proto.SceneEntityAiInfoOuterClass.SceneEntityAiInfo;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.SceneGadgetInfoOuterClass.SceneGadgetInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
Melledy's avatar
Melledy committed
27
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
28
import emu.grasscutter.scripts.constants.EventType;
Akka's avatar
Akka committed
29
import emu.grasscutter.scripts.data.SceneGadget;
30
import emu.grasscutter.scripts.data.ScriptArgs;
31
import emu.grasscutter.server.packet.send.PacketGadgetStateNotify;
Akka's avatar
Akka committed
32
import emu.grasscutter.server.packet.send.PacketLifeStateChangeNotify;
33
34
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.ProtoHelper;
35
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
36
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
Akka's avatar
Akka committed
37
import lombok.ToString;
Melledy's avatar
Melledy committed
38

Akka's avatar
Akka committed
39
@ToString(callSuper = true)
40
public class EntityGadget extends EntityBaseGadget {
github-actions's avatar
github-actions committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    private final GadgetData data;
    private final Position pos;
    private final Position rot;
    private int gadgetId;

    private int state;
    private int pointType;
    private GadgetContent content;
    private Int2FloatOpenHashMap fightProp;
    private SceneGadget metaGadget;

    public EntityGadget(Scene scene, int gadgetId, Position pos, Position rot) {
        super(scene);
        this.data = GameData.getGadgetDataMap().get(gadgetId);
        this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
        this.gadgetId = gadgetId;
        this.pos = pos.clone();
        this.rot = rot != null ? rot.clone() : new Position();
    }

    public EntityGadget(Scene scene, int gadgetId, Position pos) {
        this(scene, gadgetId, pos, new Position());
    }

    public EntityGadget(Scene scene, int gadgetId, Position pos, Position rot, GadgetContent content) {
        this(scene, gadgetId, pos, rot);
        this.content = content;
    }

    public GadgetData getGadgetData() {
        return data;
    }

    @Override
    public Position getPosition() {
        return this.pos;
    }

    @Override
    public Position getRotation() {
        return this.rot;
    }

    public int getGadgetId() {
        return gadgetId;
    }

    public void setGadgetId(int gadgetId) {
        this.gadgetId = gadgetId;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void updateState(int state) {
        this.setState(state);
        this.getScene().broadcastPacket(new PacketGadgetStateNotify(this, state));
        getScene().getScriptManager().callEvent(EventType.EVENT_GADGET_STATE_CHANGE, new ScriptArgs(state, this.getConfigId()));
    }

    public int getPointType() {
        return pointType;
    }

    public void setPointType(int pointType) {
        this.pointType = pointType;
    }

    public GadgetContent getContent() {
        return content;
    }

    @Deprecated // Dont use!
    public void setContent(GadgetContent content) {
        this.content = this.content == null ? content : this.content;
    }

    public SceneGadget getMetaGadget() {
        return metaGadget;
    }

    public void setMetaGadget(SceneGadget metaGadget) {
        this.metaGadget = metaGadget;
    }

    // TODO refactor
    public void buildContent() {
        if (getContent() != null || getGadgetData() == null || getGadgetData().getType() == null) {
            return;
        }

        EntityType type = getGadgetData().getType();
        GadgetContent content = switch (type) {
            case GatherPoint -> new GadgetGatherPoint(this);
            case GatherObject -> new GadgetGatherObject(this);
            case Worktop -> new GadgetWorktop(this);
            case RewardStatue -> new GadgetRewardStatue(this);
            case Chest -> new GadgetChest(this);
144
            case Gadget -> new GadgetObject(this);
github-actions's avatar
github-actions committed
145
146
147
148
149
150
151
152
153
154
155
156
157
            default -> null;
        };

        this.content = content;
    }

    @Override
    public Int2FloatOpenHashMap getFightProperties() {
        if (this.fightProp == null) this.fightProp = new Int2FloatOpenHashMap();
        return this.fightProp;
    }

    @Override
Melledy's avatar
Melledy committed
158
    public void onInteract(Player player, GadgetInteractReq interactReq) {
github-actions's avatar
github-actions committed
159
        if (this.getContent() == null) {
Melledy's avatar
Melledy committed
160
161
162
163
164
165
166
167
            return;
        }

        boolean shouldDelete = this.getContent().onInteract(player, interactReq);

        if (shouldDelete) {
            this.getScene().killEntity(this);
        }
github-actions's avatar
github-actions committed
168
169
170
171
172
173
174
175
176
177
    }

    @Override
    public void onCreate() {
        // Lua event
        getScene().getScriptManager().callEvent(EventType.EVENT_GADGET_CREATE, new ScriptArgs(this.getConfigId()));
    }

    @Override
    public void onDeath(int killerId) {
178
179
        super.onDeath(killerId); // Invoke super class's onDeath() method.

github-actions's avatar
github-actions committed
180
181
182
183
184
185
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
222
223
224
225
        if (this.getSpawnEntry() != null) {
            this.getScene().getDeadSpawnedEntities().add(getSpawnEntry());
        }
        if (getScene().getChallenge() != null) {
            getScene().getChallenge().onGadgetDeath(this);
        }
        getScene().getScriptManager().callEvent(EventType.EVENT_ANY_GADGET_DIE, new ScriptArgs(this.getConfigId()));
    }

    @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_GADGET)
                .setMotionInfo(MotionInfo.newBuilder().setPos(getPosition().toProto()).setRot(getRotation().toProto()).setSpeed(Vector.newBuilder()))
                .addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
                .setEntityClientData(EntityClientData.newBuilder())
                .setEntityAuthorityInfo(authority)
                .setLifeState(1);

        PropPair pair = PropPair.newBuilder()
                .setType(PlayerProperty.PROP_LEVEL.getId())
                .setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, 1))
                .build();
        entityInfo.addPropList(pair);

        // We do not use the getter to null check because the getter will create a fight prop map if it is null
        if (this.fightProp != null) {
            this.addAllFightPropsToEntityInfo(entityInfo);
        }

        SceneGadgetInfo.Builder gadgetInfo = SceneGadgetInfo.newBuilder()
                .setGadgetId(this.getGadgetId())
                .setGroupId(this.getGroupId())
                .setConfigId(this.getConfigId())
                .setGadgetState(this.getState())
                .setIsEnableInteract(true)
                .setAuthorityPeerId(this.getScene().getWorld().getHostPeerId());

226
227
228
229
        if(this.metaGadget != null) {
            gadgetInfo.setDraftId(this.metaGadget.draft_id);
        }

github-actions's avatar
github-actions committed
230
231
232
233
234
235
236
237
        if (this.getContent() != null) {
            this.getContent().onBuildProto(gadgetInfo);
        }

        entityInfo.setGadget(gadgetInfo);

        return entityInfo.build();
    }
Melledy's avatar
Melledy committed
238
}