ScriptLib.java 12.1 KB
Newer Older
1
2
3
4
5
6
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;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.entity.gadget.GadgetWorktop;
8
import emu.grasscutter.scripts.data.SceneGroup;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.scripts.data.SceneRegion;
10
import emu.grasscutter.server.packet.send.PacketCanUseSkillNotify;
11
12
import emu.grasscutter.server.packet.send.PacketGadgetStateNotify;
import emu.grasscutter.server.packet.send.PacketWorktopOptionNotify;
13
14
15
16
17
18
19
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;
20
21

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

	public SceneScriptManager getSceneScriptManager() {
		return sceneScriptManager;
	}
32
33
34
35
36
37
38
39
40
41
42

	private String printTable(LuaTable table){
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		for(var meta : table.keys()){
			sb.append(meta).append(":").append(table.get(meta)).append(",");
		}
		sb.append("}");
		return sb.toString();
	}

43
	public int SetGadgetStateByConfigId(int configId, int gadgetState) {
44
45
		logger.debug("[LUA] Call SetGadgetStateByConfigId with {},{}",
				configId,gadgetState);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		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) {
65
66
		logger.debug("[LUA] Call SetGroupGadgetStateByConfigId with {},{},{}",
				groupId,configId,gadgetState);
67
68
69
70
		List<GameEntity> list = getSceneScriptManager().getScene().getEntities().values().stream()
												.filter(e -> e.getGroupId() == groupId).toList();
		
		for (GameEntity entity : list) {
Melledy's avatar
Melledy committed
71
72
73
74
			if (!(entity instanceof EntityGadget)) {
				continue;
			}
			
75
76
77
78
79
80
81
82
83
84
			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) {
85
86
		logger.debug("[LUA] Call SetWorktopOptionsByGroupId with {},{},{}",
				groupId,configId,options);
Melledy's avatar
Melledy committed
87
		
88
89
90
		Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();

Melledy's avatar
Melledy committed
91
92

		if (entity.isEmpty() || !(entity.get() instanceof EntityGadget gadget)) {
93
94
			return 1;
		}
Melledy's avatar
Melledy committed
95
96

		if (!(gadget.getContent() instanceof GadgetWorktop worktop)) {
97
98
99
			return 1;
		}
		
Melledy's avatar
Melledy committed
100
		worktop.addWorktopOptions(options);
101
		getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
Melledy's avatar
Melledy committed
102
		
103
104
105
106
		return 0;
	}
	
	public int DelWorktopOptionByGroupId(int groupId, int configId, int option) {
107
108
		logger.debug("[LUA] Call DelWorktopOptionByGroupId with {},{},{}",groupId,configId,option);

109
110
111
		Optional<GameEntity> entity = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(e -> e.getConfigId() == configId && e.getGroupId() == groupId).findFirst();

Melledy's avatar
Melledy committed
112
		if (entity.isEmpty() || !(entity.get() instanceof EntityGadget gadget)) {
113
114
			return 1;
		}
Melledy's avatar
Melledy committed
115
116

		if (!(gadget.getContent() instanceof GadgetWorktop worktop)) {
117
118
119
			return 1;
		}
		
Melledy's avatar
Melledy committed
120
		worktop.removeWorktopOption(option);
121
		getSceneScriptManager().getScene().broadcastPacket(new PacketWorktopOptionNotify(gadget));
Melledy's avatar
Melledy committed
122
		
123
124
125
126
		return 0;
	}
	
	// Some fields are guessed
127
128
129
130
	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);

131
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
132

133
134
135
		if (group == null || group.monsters == null) {
			return 1;
		}
136

137
		this.getSceneScriptManager().startMonsterTideInGroup(group, ordersConfigId, tideCount, sceneLimit);
138
139
140
141
		
		return 0;
	}
	
142
	public int AddExtraGroupSuite(int groupId, int suite) {
143
144
		logger.debug("[LUA] Call AddExtraGroupSuite with {},{}",
				groupId,suite);
Melledy's avatar
Melledy committed
145
146
147
148
149
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
		
		if (group == null || group.monsters == null) {
			return 1;
		}
150

151
152
153
154
155
156
		// avoid spawn wrong monster
		if(getSceneScriptManager().getScene().getChallenge() != null)
			if(!getSceneScriptManager().getScene().getChallenge().inProgress() ||
					getSceneScriptManager().getScene().getChallenge().getGroup().id != groupId){
			return 0;
		}
Melledy's avatar
Melledy committed
157
		this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
Melledy's avatar
Melledy committed
158
		
Melledy's avatar
Melledy committed
159
160
161
		return 0;
	}
	
Melledy's avatar
Melledy committed
162
	// param3 (probably time limit for timed dungeons)
163
164
165
166
	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);

167
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
168
169
170
171
172
173
		var objective = objectiveKills;

		if(group == null){
			group = getSceneScriptManager().getGroupById(timeLimitOrGroupId);
			objective = groupId;
		}
174
175
176
177
		
		if (group == null || group.monsters == null) {
			return 1;
		}
178
179
180
181
182
183
184

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

185
186
187
188
189
		DungeonChallenge challenge = new DungeonChallenge(getSceneScriptManager().getScene(),
				group, challengeId, challengeIndex, objective);
		// set if tower first stage (6-1)
		challenge.setStage(getSceneScriptManager().getVariables().getOrDefault("stage", -1) == 0);

190
		getSceneScriptManager().getScene().setChallenge(challenge);
191

192
193
194
195
		challenge.start();
		return 0;
	}
	
196
	public int GetGroupMonsterCountByGroupId(int groupId) {
197
198
		logger.debug("[LUA] Call GetGroupMonsterCountByGroupId with {}",
				groupId);
199
200
201
202
203
		return (int) getSceneScriptManager().getScene().getEntities().values().stream()
								.filter(e -> e instanceof EntityMonster && e.getGroupId() == groupId)
								.count();
	}
	
Melledy's avatar
Melledy committed
204
	public int GetGroupVariableValue(String var) {
205
206
		logger.debug("[LUA] Call GetGroupVariableValue with {}",
				var);
Melledy's avatar
Melledy committed
207
		return getSceneScriptManager().getVariables().getOrDefault(var, 0);
208
209
	}
	
210
	public int SetGroupVariableValue(String var, int value) {
211
212
		logger.debug("[LUA] Call SetGroupVariableValue with {},{}",
				var, value);
213
		getSceneScriptManager().getVariables().put(var, value);
214
215
216
217
		return 0;
	}
	
	public LuaValue ChangeGroupVariableValue(String var, int value) {
218
219
220
		logger.debug("[LUA] Call ChangeGroupVariableValue with {},{}",
				var, value);

221
		getSceneScriptManager().getVariables().put(var, getSceneScriptManager().getVariables().get(var) + value);
222
223
		return LuaValue.ZERO;
	}
224
225
226
227

	/**
	 * Set the actions and triggers to designated group
	 */
228
	public int RefreshGroup(LuaTable table) {
229
		logger.debug("[LUA] Call RefreshGroup with {}",
230
				printTable(table));
231
232
233
234
235
236
237
238
239
240
		// 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;
		}
		
241
		getSceneScriptManager().refreshGroup(group, suite);
242
		
243
244
245
		return 0;
	}
	
Melledy's avatar
Melledy committed
246
	public int GetRegionEntityCount(LuaTable table) {
247
248
		logger.debug("[LUA] Call GetRegionEntityCount with {}",
				table);
Melledy's avatar
Melledy committed
249
250
251
252
253
254
255
256
257
258
259
260
		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();
	}
	
261
	public void PrintContextLog(String msg) {
262
		logger.info("[LUA] " + msg);
263
	}
Akka's avatar
Akka committed
264

265
266
267
268
	public int TowerCountTimeStatus(int isDone, int var2){
		logger.debug("[LUA] Call TowerCountTimeStatus with {},{}",
				isDone,var2);
		// TODO record time
Akka's avatar
Akka committed
269
270
271
		return 0;
	}
	public int GetGroupMonsterCount(int var1){
272
273
274
275
		logger.debug("[LUA] Call GetGroupMonsterCount with {}",
				var1);

		return (int) getSceneScriptManager().getScene().getEntities().values().stream()
276
				.filter(e -> e instanceof EntityMonster && e.getGroupId() == getSceneScriptManager().getCurrentGroup().id)
277
				.count();
Akka's avatar
Akka committed
278
279
	}
	public int SetMonsterBattleByGroup(int var1, int var2, int var3){
280
281
		logger.debug("[LUA] Call SetMonsterBattleByGroup with {},{},{}",
				var1,var2,var3);
282
		// TODO
Akka's avatar
Akka committed
283
284
285
286
		return 0;
	}

	public int CauseDungeonFail(int var1){
287
288
289
290
291
		logger.debug("[LUA] Call CauseDungeonFail with {}",
				var1);

		return 0;
	}
292

293
294
295
	public int GetGroupVariableValueByGroup(String name, int groupId){
		logger.debug("[LUA] Call GetGroupVariableValueByGroup with {},{}",
				name,groupId);
296

297
		return getSceneScriptManager().getVariables().getOrDefault(name, 0);
298
299
300
301
302
303
304
305
306
307
308
309
	}

	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 {}",
310
				printTable(table));
311
312
313
314
315
316
317
318
319
320
		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
321
322
		return 0;
	}
323

324
325
326
327
	public int SetGroupVariableValueByGroup(String key, int value, int groupId){
		logger.debug("[LUA] Call SetGroupVariableValueByGroup with {},{},{}",
				key,value,groupId);

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		getSceneScriptManager().getVariables().put(key, value);
		return 0;
	}

	public int CreateMonster(LuaTable table){
		logger.debug("[LUA] Call CreateMonster with {}",
				printTable(table));
		var configId = table.get("config_id").toint();
		var delayTime = table.get("delay_time").toint();

		getSceneScriptManager().spawnMonstersByConfigId(configId, delayTime);
		return 0;
	}

	public int TowerMirrorTeamSetUp(int team, int var1) {
		logger.debug("[LUA] Call TowerMirrorTeamSetUp with {},{}",
				team,var1);

346
		getSceneScriptManager().unloadCurrentMonsterTide();
347
348
		getSceneScriptManager().getScene().getPlayers().get(0).getTowerManager().mirrorTeamSetUp(team-1);

349
350
351
		return 0;
	}

352
353
354
355
356
357
358
	public int CreateGadget(LuaTable table){
		logger.debug("[LUA] Call CreateGadget with {}",
				printTable(table));
		var configId = table.get("config_id").toint();

		//TODO

Akka's avatar
Akka committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
		return 0;
	}
	public int CheckRemainGadgetCountByGroupId(LuaTable table){
		logger.debug("[LUA] Call CheckRemainGadgetCountByGroupId with {}",
				printTable(table));
		var groupId = table.get("group_id").toint();

		var count = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(g -> g instanceof EntityGadget entityGadget && entityGadget.getGroupId() == groupId)
				.count();
		return (int)count;
	}

	public int GetGadgetStateByConfigId(int groupId, int configId){
		logger.debug("[LUA] Call GetGadgetStateByConfigId with {},{}",
				groupId, configId);
		var gadget = getSceneScriptManager().getScene().getEntities().values().stream()
				.filter(g -> g instanceof EntityGadget entityGadget && entityGadget.getGroupId() == groupId)
				.filter(g -> g.getConfigId() == configId)
				.findFirst();
		if(gadget.isEmpty()){
			return 0;
		}
		var stat = ((EntityGadget)gadget.get()).getState();
		return stat;
	}
	public int SetGadgetStateByConfigId(int configId, LuaTable gadgetState){
		logger.debug("[LUA] Call SetGadgetStateByConfigId with {},{}",
				configId, printTable(gadgetState));

389
390
		return 0;
	}
391
}