SceneGroup.java 5.99 KB
Newer Older
1
2
package emu.grasscutter.scripts.data;

Akka's avatar
Akka committed
3
4
import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.ScriptLoader;
5
import emu.grasscutter.utils.Position;
Akka's avatar
Akka committed
6
7
import lombok.Setter;
import lombok.ToString;
8

Akka's avatar
Akka committed
9
10
11
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptException;
12
13
14
15

import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

Akka's avatar
Akka committed
16
import java.util.ArrayList;
17
18
import java.util.List;
import java.util.Map;
Akka's avatar
Akka committed
19
import java.util.Optional;
Akka's avatar
Akka committed
20
21
import java.util.stream.Collectors;

22
import static emu.grasscutter.Configuration.SCRIPT;
23

Akka's avatar
Akka committed
24
25
@ToString
@Setter
26
public class SceneGroup {
27
	public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference
28

29
30
31
	public int id;
	public int refresh_id;
	public Position pos;
32

Melledy's avatar
Melledy committed
33
34
	public Map<Integer,SceneMonster> monsters; // <ConfigId, Monster>
	public Map<Integer, SceneGadget> gadgets; // <ConfigId, Gadgets>
Akka's avatar
Akka committed
35
	public Map<String, SceneTrigger> triggers;
Akka's avatar
Akka committed
36
	public Map<Integer, SceneNPC> npc; // <NpcId, NPC>
Akka's avatar
Akka committed
37
    public Map<Integer, SceneRegion> regions;
38
	public List<SceneSuite> suites;
Melledy's avatar
Melledy committed
39
	public List<SceneVar> variables;
Akka's avatar
Akka committed
40

Melledy's avatar
Melledy committed
41
42
	public SceneBusiness business;
	public SceneGarbage garbages;
43
	public SceneInitConfig init_config;
Akka's avatar
Akka committed
44
45

	private transient boolean loaded; // Not an actual variable in the scripts either
Melledy's avatar
Melledy committed
46
	private transient CompiledScript script;
47
	private transient Bindings bindings;
Akka's avatar
Akka committed
48
49
50
51
52
	public static SceneGroup of(int groupId) {
		var group = new SceneGroup();
		group.id = groupId;
		return group;
	}
53
54

	public boolean isLoaded() {
55
		return this.loaded;
56
	}
57

Akka's avatar
Akka committed
58
59
60
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
61

Melledy's avatar
Melledy committed
62
63
64
	public int getBusinessType() {
		return this.business == null ? 0 : this.business.type;
	}
65

Melledy's avatar
Melledy committed
66
67
68
	public List<SceneGadget> getGarbageGadgets() {
		return this.garbages == null ? null : this.garbages.gadgets;
	}
Akka's avatar
Akka committed
69
70

	public CompiledScript getScript() {
71
		return this.script;
Akka's avatar
Akka committed
72
73
	}

Melledy's avatar
Melledy committed
74
	public SceneSuite getSuiteByIndex(int index) {
75
		return this.suites.get(index - 1);
Melledy's avatar
Melledy committed
76
	}
Akka's avatar
Akka committed
77

78
	public Bindings getBindings() {
79
		return this.bindings;
80
81
	}

Akka's avatar
Akka committed
82
	public synchronized SceneGroup load(int sceneId){
83
		if(this.loaded){
Akka's avatar
Akka committed
84
85
			return this;
		}
86
87
		// Set flag here so if there is no script, we don't call this function over and over again.
        this.setLoaded(true);
Akka's avatar
Akka committed
88

89
90
		this.bindings = ScriptLoader.getEngine().createBindings();

Akka's avatar
Akka committed
91
		CompiledScript cs = ScriptLoader.getScriptByPath(
92
				SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + this.id + "." + ScriptLoader.getScriptType()));
Akka's avatar
Akka committed
93
94
95
96
97
98

		if (cs == null) {
			return this;
		}

		this.script = cs;
99

Akka's avatar
Akka committed
100
101
		// Eval script
		try {
102
			cs.eval(this.bindings);
Akka's avatar
Akka committed
103
104

			// Set
105
            this.monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, this.bindings.get("monsters")).stream()
Akka's avatar
Akka committed
106
					.collect(Collectors.toMap(x -> x.config_id, y -> y));
107
            this.monsters.values().forEach(m -> m.group = this);
Akka's avatar
Akka committed
108

109
            this.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, this.bindings.get("gadgets")).stream()
Akka's avatar
Akka committed
110
					.collect(Collectors.toMap(x -> x.config_id, y -> y));
111
            this.gadgets.values().forEach(m -> m.group = this);
Akka's avatar
Akka committed
112

113
            this.triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, this.bindings.get("triggers")).stream()
Akka's avatar
Akka committed
114
					.collect(Collectors.toMap(x -> x.name, y -> y));
115
            this.triggers.values().forEach(t -> t.currentGroup = this);
Akka's avatar
Akka committed
116

117
118
            this.suites = ScriptLoader.getSerializer().toList(SceneSuite.class, this.bindings.get("suites"));
            this.regions = ScriptLoader.getSerializer().toList(SceneRegion.class, this.bindings.get("regions")).stream()
Akka's avatar
Akka committed
119
                .collect(Collectors.toMap(x -> x.config_id, y -> y));
120
            this.regions.values().forEach(m -> m.group = this);
Akka's avatar
Akka committed
121

122
            this.init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, this.bindings.get("init_config"));
Akka's avatar
Akka committed
123

124
125
126
127
			// Garbages // TODO: fix properly later
			Object garbagesValue = this.bindings.get("garbages");
			if (garbagesValue instanceof LuaValue garbagesTable) {
                this.garbages = new SceneGarbage();
128
				if (garbagesTable.checktable().get("gadgets") != LuaValue.NIL) {
129
130
                    this.garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable());
                    this.garbages.gadgets.forEach(m -> m.group = this);
131
132
				}
			}
133

Akka's avatar
Akka committed
134
			// Add variables to suite
135
            this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables"));
Akka's avatar
Akka committed
136
			// NPC in groups
137
            this.npc = ScriptLoader.getSerializer().toList(SceneNPC.class, this.bindings.get("npcs")).stream()
Akka's avatar
Akka committed
138
					.collect(Collectors.toMap(x -> x.npc_id, y -> y));
139
            this.npc.values().forEach(n -> n.group = this);
Akka's avatar
Akka committed
140

Melledy's avatar
Melledy committed
141
			// Add monsters and gadgets to suite
142
			for (SceneSuite suite : this.suites) {
Akka's avatar
Akka committed
143
144
				suite.sceneMonsters = new ArrayList<>(
						suite.monsters.stream()
145
146
						.filter(this.monsters::containsKey)
						.map(this.monsters::get)
Akka's avatar
Akka committed
147
148
149
150
151
						.toList()
				);

				suite.sceneGadgets = new ArrayList<>(
						suite.gadgets.stream()
152
153
								.filter(this.gadgets::containsKey)
								.map(this.gadgets::get)
Akka's avatar
Akka committed
154
155
								.toList()
				);
Akka's avatar
Akka committed
156
157
158

				suite.sceneTriggers = new ArrayList<>(
						suite.triggers.stream()
159
160
								.filter(this.triggers::containsKey)
								.map(this.triggers::get)
Akka's avatar
Akka committed
161
162
								.toList()
				);
Akka's avatar
Akka committed
163
164
165

                suite.sceneRegions = new ArrayList<>(
                    suite.regions.stream()
166
167
                        .filter(this.regions::containsKey)
                        .map(this.regions::get)
Akka's avatar
Akka committed
168
169
                        .toList()
                );
Akka's avatar
Akka committed
170
171
172
			}

		} catch (ScriptException e) {
173
			Grasscutter.getLogger().error("An error occurred while loading group " + this.id + " in scene " + sceneId + ".", e);
Akka's avatar
Akka committed
174
		}
Akka's avatar
Akka committed
175

176
		Grasscutter.getLogger().debug("Successfully loaded group {} in scene {}.", this.id, sceneId);
Akka's avatar
Akka committed
177
178
		return this;
	}
Akka's avatar
Akka committed
179
180

	public Optional<SceneBossChest> searchBossChestInGroup() {
181
		return this.gadgets.values().stream()
Akka's avatar
Akka committed
182
183
184
185
186
				.filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0)
				.map(g -> g.boss_chest)
				.findFirst();
	}

187
}