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

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.luaj.vm2.LuaTable;
8
import org.luaj.vm2.LuaValue;
9
10
11
12
13
14
15
16

import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.MonsterData;
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
17
import emu.grasscutter.scripts.constants.EventType;
18
19
import emu.grasscutter.scripts.data.SceneGroup;
import emu.grasscutter.scripts.data.SceneMonster;
Melledy's avatar
Melledy committed
20
import emu.grasscutter.scripts.data.SceneRegion;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.scripts.data.ScriptArgs;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import emu.grasscutter.server.packet.send.PacketGadgetStateNotify;
import emu.grasscutter.server.packet.send.PacketWorktopOptionNotify;

public class ScriptLib {
	private final SceneScriptManager sceneScriptManager;
	
	public ScriptLib(SceneScriptManager sceneScriptManager) {
		this.sceneScriptManager = sceneScriptManager;
	}

	public SceneScriptManager getSceneScriptManager() {
		return sceneScriptManager;
	}
	
	public int SetGadgetStateByConfigId(int configId, int gadgetState) {
		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) {
		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
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
			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) {
		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) {
		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
	public int AutoMonsterTide(int challengeIndex, int groupId, int[] config_ids, int param4, int param5, int param6) {
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
				
		if (group == null || group.monsters == null) {
			return 1;
		}
		
		// TODO just spawn all from group for now
Melledy's avatar
Melledy committed
120
		this.getSceneScriptManager().spawnMonstersInGroup(group);
121
122
123
124
		
		return 0;
	}
	
125
	public int AddExtraGroupSuite(int groupId, int suite) {
Melledy's avatar
Melledy committed
126
127
128
129
130
131
132
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
		
		if (group == null || group.monsters == null) {
			return 1;
		}
		
		// TODO just spawn all from group for now
Melledy's avatar
Melledy committed
133
		this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
Melledy's avatar
Melledy committed
134
		
Melledy's avatar
Melledy committed
135
136
137
		return 0;
	}
	
Melledy's avatar
Melledy committed
138
	// param3 (probably time limit for timed dungeons)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	public int ActiveChallenge(int challengeId, int challengeIndex, int param3, int groupId, int param4, int param5) {
		SceneGroup group = getSceneScriptManager().getGroupById(groupId);
		
		if (group == null || group.monsters == null) {
			return 1;
		}
		
		DungeonChallenge challenge = new DungeonChallenge(getSceneScriptManager().getScene(), group);
		challenge.setChallengeId(challengeId);
		challenge.setChallengeIndex(challengeIndex);
		
		getSceneScriptManager().getScene().setChallenge(challenge);
		
		challenge.start();
		return 0;
	}
	
156
157
158
159
160
161
	public int GetGroupMonsterCountByGroupId(int groupId) {
		return (int) getSceneScriptManager().getScene().getEntities().values().stream()
								.filter(e -> e instanceof EntityMonster && e.getGroupId() == groupId)
								.count();
	}
	
Melledy's avatar
Melledy committed
162
163
	public int GetGroupVariableValue(String var) {
		return getSceneScriptManager().getVariables().getOrDefault(var, 0);
164
165
	}
	
Melledy's avatar
Melledy committed
166
	public LuaValue ChangeGroupVariableValue(String var, int value) {
167
168
169
170
		getSceneScriptManager().getVariables().put(var, value);
		return LuaValue.ZERO;
	}
	
171
	public int RefreshGroup(LuaTable table) {
172
173
174
175
176
177
178
179
180
181
182
		// 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;
		}
		
		// TODO just spawn all from group for now
Melledy's avatar
Melledy committed
183
		this.getSceneScriptManager().spawnMonstersInGroup(group, suite);
184
		
185
186
187
		return 0;
	}
	
Melledy's avatar
Melledy committed
188
189
190
191
192
193
194
195
196
197
198
199
200
	public int GetRegionEntityCount(LuaTable table) {
		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();
	}
	
201
202
203
204
	public void PrintContextLog(String msg) {
		Grasscutter.getLogger().info("[LUA] " + msg);
	}
}