ScriptLib.java 9.48 KB
Newer Older
1
2
3
4
5
6
7
package emu.grasscutter.scripts;

import emu.grasscutter.game.dungeons.DungeonChallenge;
import emu.grasscutter.game.entity.EntityGadget;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.entity.GameEntity;
import emu.grasscutter.scripts.data.SceneGroup;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.scripts.data.SceneRegion;
9
import emu.grasscutter.server.packet.send.PacketCanUseSkillNotify;
10
11
import emu.grasscutter.server.packet.send.PacketGadgetStateNotify;
import emu.grasscutter.server.packet.send.PacketWorktopOptionNotify;
12
13
14
15
16
17
18
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
19
20

public class ScriptLib {
21
	public static final Logger logger = LoggerFactory.getLogger(ScriptLib.class);
22
23
24
25
26
27
28
29
30
31
32
	private final SceneScriptManager sceneScriptManager;
	
	public ScriptLib(SceneScriptManager sceneScriptManager) {
		this.sceneScriptManager = sceneScriptManager;
	}

	public SceneScriptManager getSceneScriptManager() {
		return sceneScriptManager;
	}
	
	public int SetGadgetStateByConfigId(int configId, int gadgetState) {
33
34
		logger.debug("[LUA] Call SetGadgetStateByConfigId with {},{}",
				configId,gadgetState);
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
		Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(e -> e.getConfigId() == configId).findFirst();

		if (entity.isEmpty()) {
			return 1;
		}
		
		if (!(entity.get() instanceof EntityGadget)) {
			return 1;
		}
		
		EntityGadget gadget = (EntityGadget) entity.get();
		gadget.setState(gadgetState);
		
		getSceneScriptManager().getScene().broadcastPacket(new PacketGadgetStateNotify(gadget, gadgetState));
		return 0;
	}

	public int SetGroupGadgetStateByConfigId(int groupId, int configId, int gadgetState) {
54
55
		logger.debug("[LUA] Call SetGroupGadgetStateByConfigId with {},{},{}",
				groupId,configId,gadgetState);
56
57
58
59
		List<GameEntity> list = getSceneScriptManager().getScene().getEntities().values().stream()
												.filter(e -> e.getGroupId() == groupId).toList();
		
		for (GameEntity entity : list) {
Melledy's avatar
Melledy committed
60
61
62
63
			if (!(entity instanceof EntityGadget)) {
				continue;
			}
			
64
65
66
67
68
69
70
71
72
73
			EntityGadget gadget = (EntityGadget) entity;
			gadget.setState(gadgetState);
			
			getSceneScriptManager().getScene().broadcastPacket(new PacketGadgetStateNotify(gadget, gadgetState));
		}
		
		return 0;
	}
	
	public int SetWorktopOptionsByGroupId(int groupId, int configId, int[] options) {
74
75
		logger.debug("[LUA] Call SetWorktopOptionsByGroupId with {},{},{}",
				groupId,configId,options);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();

		if (entity.isEmpty()) {
			return 1;
		}
		
		if (!(entity.get() instanceof EntityGadget)) {
			return 1;
		}
		
		EntityGadget gadget = (EntityGadget) entity.get();
		gadget.addWorktopOptions(options);

		getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
		return 0;
	}
	
	public int DelWorktopOptionByGroupId(int groupId, int configId, int option) {
95
96
		logger.debug("[LUA] Call DelWorktopOptionByGroupId with {},{},{}",groupId,configId,option);

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
		Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();

		if (entity.isEmpty()) {
			return 1;
		}
		
		if (!(entity.get() instanceof EntityGadget)) {
			return 1;
		}
		
		EntityGadget gadget = (EntityGadget) entity.get();
		gadget.removeWorktopOption(option);
		
		getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
		return 0;
	}
	
	// Some fields are guessed
116
117
118
119
	public int AutoMonsterTide(int challengeIndex, int groupId, Integer[] ordersConfigId, int tideCount, int sceneLimit, int param6) {
		logger.debug("[LUA] Call AutoMonsterTide with {},{},{},{},{},{}",
				challengeIndex,groupId,ordersConfigId,tideCount,sceneLimit,param6);

120
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
121

122
123
124
		if (group == null || group.monsters == null) {
			return 1;
		}
125
126

		this.getSceneScriptManager().spawnMonstersInGroup(group, ordersConfigId, tideCount, sceneLimit);
127
128
129
130
		
		return 0;
	}
	
131
	public int AddExtraGroupSuite(int groupId, int suite) {
132
133
		logger.debug("[LUA] Call AddExtraGroupSuite with {},{}",
				groupId,suite);
Melledy's avatar
Melledy committed
134
135
136
137
138
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
		
		if (group == null || group.monsters == null) {
			return 1;
		}
139

Melledy's avatar
Melledy committed
140
		this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
Melledy's avatar
Melledy committed
141
		
Melledy's avatar
Melledy committed
142
143
144
		return 0;
	}
	
Melledy's avatar
Melledy committed
145
	// param3 (probably time limit for timed dungeons)
146
147
148
149
	public int ActiveChallenge(int challengeId, int challengeIndex, int timeLimitOrGroupId, int groupId, int objectiveKills, int param5) {
		logger.debug("[LUA] Call ActiveChallenge with {},{},{},{},{},{}",
				challengeId,challengeIndex,timeLimitOrGroupId,groupId,objectiveKills,param5);

150
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
151
152
153
154
155
156
		var objective = objectiveKills;

		if(group == null){
			group = getSceneScriptManager().getGroupById(timeLimitOrGroupId);
			objective = groupId;
		}
157
158
159
160
		
		if (group == null || group.monsters == null) {
			return 1;
		}
161
162
163
164
165
166
167

		if(getSceneScriptManager().getScene().getChallenge() != null &&
				getSceneScriptManager().getScene().getChallenge().inProgress())
		{
			return 0;
		}

168
169
170
		DungeonChallenge challenge = new DungeonChallenge(getSceneScriptManager().getScene(), group);
		challenge.setChallengeId(challengeId);
		challenge.setChallengeIndex(challengeIndex);
171
		challenge.setObjective(objective);
172
173
174
175
176
177
178
		
		getSceneScriptManager().getScene().setChallenge(challenge);
		
		challenge.start();
		return 0;
	}
	
179
	public int GetGroupMonsterCountByGroupId(int groupId) {
180
181
		logger.debug("[LUA] Call GetGroupMonsterCountByGroupId with {}",
				groupId);
182
183
184
185
186
		return (int) getSceneScriptManager().getScene().getEntities().values().stream()
								.filter(e -> e instanceof EntityMonster && e.getGroupId() == groupId)
								.count();
	}
	
Melledy's avatar
Melledy committed
187
	public int GetGroupVariableValue(String var) {
188
189
		logger.debug("[LUA] Call GetGroupVariableValue with {}",
				var);
Melledy's avatar
Melledy committed
190
		return getSceneScriptManager().getVariables().getOrDefault(var, 0);
191
192
	}
	
193
	public int SetGroupVariableValue(String var, int value) {
194
195
		logger.debug("[LUA] Call SetGroupVariableValue with {},{}",
				var, value);
196
		getSceneScriptManager().getVariables().put(var, value);
197
198
199
200
		return 0;
	}
	
	public LuaValue ChangeGroupVariableValue(String var, int value) {
201
202
203
		logger.debug("[LUA] Call ChangeGroupVariableValue with {},{}",
				var, value);

204
		getSceneScriptManager().getVariables().put(var, getSceneScriptManager().getVariables().get(var) + value);
205
206
207
		return LuaValue.ZERO;
	}
	
208
	public int RefreshGroup(LuaTable table) {
209
210
		logger.debug("[LUA] Call RefreshGroup with {}",
				table);
211
212
213
214
215
216
217
218
219
220
		// Kill and Respawn?
		int groupId = table.get("group_id").toint();
		int suite = table.get("suite").toint();
		
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
		
		if (group == null || group.monsters == null) {
			return 1;
		}
		
Melledy's avatar
Melledy committed
221
		this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
222
		this.getSceneScriptManager().spawnGadgetsInGroup(group, suite);
223
		
224
225
226
		return 0;
	}
	
Melledy's avatar
Melledy committed
227
	public int GetRegionEntityCount(LuaTable table) {
228
229
		logger.debug("[LUA] Call GetRegionEntityCount with {}",
				table);
Melledy's avatar
Melledy committed
230
231
232
233
234
235
236
237
238
239
240
241
		int regionId = table.get("region_eid").toint();
		int entityType = table.get("entity_type").toint();

		SceneRegion region = this.getSceneScriptManager().getRegionById(regionId);
		
		if (region == null) {
			return 0;
		}

		return (int) region.getEntities().intStream().filter(e -> e >> 24 == entityType).count();
	}
	
242
	public void PrintContextLog(String msg) {
243
		logger.info("[LUA] " + msg);
244
	}
Akka's avatar
Akka committed
245

246
247
248
249
	public int TowerCountTimeStatus(int isDone, int var2){
		logger.debug("[LUA] Call TowerCountTimeStatus with {},{}",
				isDone,var2);
		// TODO record time
Akka's avatar
Akka committed
250
251
252
		return 0;
	}
	public int GetGroupMonsterCount(int var1){
253
254
255
256
		logger.debug("[LUA] Call GetGroupMonsterCount with {}",
				var1);

		return (int) getSceneScriptManager().getScene().getEntities().values().stream()
257
				.filter(e -> e instanceof EntityMonster && e.getGroupId() == getSceneScriptManager().getCurrentGroup().id)
258
				.count();
Akka's avatar
Akka committed
259
260
	}
	public int SetMonsterBattleByGroup(int var1, int var2, int var3){
261
262
263
		logger.debug("[LUA] Call SetMonsterBattleByGroup with {},{},{}",
				var1,var2,var3);

Akka's avatar
Akka committed
264
265
266
267
		return 0;
	}

	public int CauseDungeonFail(int var1){
268
269
270
271
272
273
		logger.debug("[LUA] Call CauseDungeonFail with {}",
				var1);

		return 0;
	}
	// 8-1
274
275
276
	public int GetGroupVariableValueByGroup(String name, int groupId){
		logger.debug("[LUA] Call GetGroupVariableValueByGroup with {},{}",
				name,groupId);
277

278
		return getSceneScriptManager().getVariables().getOrDefault(name, 0);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	}

	public int SetIsAllowUseSkill(int canUse, int var2){
		logger.debug("[LUA] Call SetIsAllowUseSkill with {},{}",
				canUse,var2);

		getSceneScriptManager().getScene().broadcastPacket(new PacketCanUseSkillNotify(canUse == 1));
		return 0;
	}

	public int KillEntityByConfigId(LuaTable table){
		logger.debug("[LUA] Call KillEntityByConfigId with {}",
				table);
		var configId = table.get("config_id");
		if(configId == LuaValue.NIL){
			return 1;
		}

		var entity = getSceneScriptManager().getScene().getEntityByConfigId(configId.toint());
		if(entity == null){
			return 1;
		}
		getSceneScriptManager().getScene().killEntity(entity, 0);
Akka's avatar
Akka committed
302
303
		return 0;
	}
304

305
306
307
308
309
310
311
	public int SetGroupVariableValueByGroup(String key, int value, int groupId){
		logger.debug("[LUA] Call SetGroupVariableValueByGroup with {},{},{}",
				key,value,groupId);

		return 0;
	}

312
}