EntityGadget.java 7.47 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
41
42
43
44
public class EntityGadget extends EntityBaseGadget {
	private final GadgetData data;
	private final Position pos;
	private final Position rot;
	private int gadgetId;
45

46
	private int state;
Melledy's avatar
Melledy committed
47
48
	private int pointType;
	private GadgetContent content;
49
	private Int2FloatOpenHashMap fightProp;
Akka's avatar
Akka committed
50
	private SceneGadget metaGadget;
51

52
	public EntityGadget(Scene scene, int gadgetId, Position pos, Position rot) {
53
		super(scene);
54
55
56
57
		this.data = GameData.getGadgetDataMap().get(gadgetId);
		this.id = getScene().getWorld().getNextEntityId(EntityIdType.GADGET);
		this.gadgetId = gadgetId;
		this.pos = pos.clone();
58
		this.rot = rot != null ? rot.clone() : new Position();
Melledy's avatar
Melledy committed
59
	}
60
61
62
63
64
65
66

	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);
Melledy's avatar
Melledy committed
67
68
		this.content = content;
	}
69

70
71
72
73
74
75
76
77
78
79
80
81
82
	public GadgetData getGadgetData() {
		return data;
	}

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

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

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	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;
	}
99

100
101
102
	public void updateState(int state){
		this.setState(state);
		this.getScene().broadcastPacket(new PacketGadgetStateNotify(this, state));
Akka's avatar
Akka committed
103
		getScene().getScriptManager().callEvent(EventType.EVENT_GADGET_STATE_CHANGE, new ScriptArgs(state, this.getConfigId()));
104
	}
105

Melledy's avatar
Melledy committed
106
107
	public int getPointType() {
		return pointType;
108
	}
Melledy's avatar
Melledy committed
109
110
111

	public void setPointType(int pointType) {
		this.pointType = pointType;
112
	}
Melledy's avatar
Melledy committed
113
114
115
116
117
118
119
120

	public GadgetContent getContent() {
		return content;
	}

	@Deprecated // Dont use!
	public void setContent(GadgetContent content) {
		this.content = this.content == null ? content : this.content;
121
	}
Akka's avatar
Akka committed
122
123
124
125
126
127
128
129
130

	public SceneGadget getMetaGadget() {
		return metaGadget;
	}

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

Melledy's avatar
Melledy committed
131
132
133
	// TODO refactor
	public void buildContent() {
		if (getContent() != null || getGadgetData() == null || getGadgetData().getType() == null) {
134
135
			return;
		}
136

Melledy's avatar
Melledy committed
137
138
139
		EntityType type = getGadgetData().getType();
		GadgetContent content = switch (type) {
			case GatherPoint -> new GadgetGatherPoint(this);
140
			case GatherObject -> new GadgetGatherObject(this);
Melledy's avatar
Melledy committed
141
142
			case Worktop -> new GadgetWorktop(this);
			case RewardStatue -> new GadgetRewardStatue(this);
143
			case Chest -> new GadgetChest(this);
144
            case Gadget -> new GadgetObject(this);
Melledy's avatar
Melledy committed
145
146
			default -> null;
		};
147

Melledy's avatar
Melledy committed
148
		this.content = content;
149
150
151
152
	}

	@Override
	public Int2FloatOpenHashMap getFightProperties() {
153
154
		if (this.fightProp == null) this.fightProp = new Int2FloatOpenHashMap();
		return this.fightProp;
155
	}
Melledy's avatar
Melledy committed
156
157
158
159
160
161
162
163
164
165
166
167
168
	
	@Override
    public void onInteract(Player player, GadgetInteractReq interactReq) {
	    if (this.getContent() == null) {
            return;
        }

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

        if (shouldDelete) {
            this.getScene().killEntity(this);
        }
	}
169

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

Melledy's avatar
Melledy committed
176
177
	@Override
	public void onDeath(int killerId) {
178
179
180
181
		if (this.getSpawnEntry() != null) {
			this.getScene().getDeadSpawnedEntities().add(getSpawnEntry());
		}
		if (getScene().getChallenge() != null) {
Akka's avatar
Akka committed
182
183
184
			getScene().getChallenge().onGadgetDeath(this);
		}
		getScene().getScriptManager().callEvent(EventType.EVENT_ANY_GADGET_DIE, new ScriptArgs(this.getConfigId()));
Melledy's avatar
Melledy committed
185
	}
186

187
188
189
190
191
192
193
194
	@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();
195

196
197
		SceneEntityInfo.Builder entityInfo = SceneEntityInfo.newBuilder()
				.setEntityId(getId())
198
				.setEntityType(ProtEntityType.PROT_ENTITY_TYPE_GADGET)
199
200
201
202
203
				.setMotionInfo(MotionInfo.newBuilder().setPos(getPosition().toProto()).setRot(getRotation().toProto()).setSpeed(Vector.newBuilder()))
				.addAnimatorParaList(AnimatorParameterValueInfoPair.newBuilder())
				.setEntityClientData(EntityClientData.newBuilder())
				.setEntityAuthorityInfo(authority)
				.setLifeState(1);
204

205
206
207
208
209
		PropPair pair = PropPair.newBuilder()
				.setType(PlayerProperty.PROP_LEVEL.getId())
				.setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, 1))
				.build();
		entityInfo.addPropList(pair);
210

211
212
213
214
215
		// 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);
		}

216
217
218
219
220
221
222
		SceneGadgetInfo.Builder gadgetInfo = SceneGadgetInfo.newBuilder()
				.setGadgetId(this.getGadgetId())
				.setGroupId(this.getGroupId())
				.setConfigId(this.getConfigId())
				.setGadgetState(this.getState())
				.setIsEnableInteract(true)
				.setAuthorityPeerId(this.getScene().getWorld().getHostPeerId());
223

Melledy's avatar
Melledy committed
224
225
		if (this.getContent() != null) {
			this.getContent().onBuildProto(gadgetInfo);
226
227
228
		}

		entityInfo.setGadget(gadgetInfo);
229

230
231
		return entityInfo.build();
	}
Melledy's avatar
Melledy committed
232
}