SceneGroup.java 5 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
20
import java.util.stream.Collectors;

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

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

Melledy's avatar
Melledy committed
32
33
	public Map<Integer,SceneMonster> monsters; // <ConfigId, Monster>
	public Map<Integer, SceneGadget> gadgets; // <ConfigId, Gadgets>
Akka's avatar
Akka committed
34
	public Map<String, SceneTrigger> triggers;
Akka's avatar
Akka committed
35
	public Map<Integer, SceneNPC> npc; // <NpcId, NPC>
Melledy's avatar
Melledy committed
36
	public List<SceneRegion> regions;
37
	public List<SceneSuite> suites;
Melledy's avatar
Melledy committed
38
39
40
41
	public List<SceneVar> variables;
	
	public SceneBusiness business;
	public SceneGarbage garbages;
42
	public SceneInitConfig init_config;
Akka's avatar
Akka committed
43
44

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

	public boolean isLoaded() {
		return loaded;
	}
Melledy's avatar
Melledy committed
56
	
Akka's avatar
Akka committed
57
58
59
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
Melledy's avatar
Melledy committed
60
61
62
63
64
65
66
67
	
	public int getBusinessType() {
		return this.business == null ? 0 : this.business.type;
	}
	
	public List<SceneGadget> getGarbageGadgets() {
		return this.garbages == null ? null : this.garbages.gadgets;
	}
Akka's avatar
Akka committed
68
69
70
71
72

	public CompiledScript getScript() {
		return script;
	}

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

77
78
79
80
	public Bindings getBindings() {
		return bindings;
	}

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

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

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

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

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

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

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

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

Akka's avatar
Akka committed
116
117
118
			suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
			regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions"));
			init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config"));
119
120
121
122
123
124
125
			
			// Garbages TODO fix properly later
			Object garbagesValue = bindings.get("garbages");
			if (garbagesValue != null && garbagesValue instanceof LuaValue garbagesTable) {
				garbages = new SceneGarbage();
				if (garbagesTable.checktable().get("gadgets") != LuaValue.NIL) {
					garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable());
Akka's avatar
Akka committed
126
					garbages.gadgets.forEach(m -> m.group = this);
127
128
129
				}
			}
			
Akka's avatar
Akka committed
130
131
			// Add variables to suite
			variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));
Akka's avatar
Akka committed
132
133
134
135
			// NPC in groups
			npc = ScriptLoader.getSerializer().toList(SceneNPC.class, bindings.get("npcs")).stream()
					.collect(Collectors.toMap(x -> x.npc_id, y -> y));
			npc.values().forEach(n -> n.group = this);
Akka's avatar
Akka committed
136

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

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

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

		} catch (ScriptException e) {
			Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
		}
Melledy's avatar
Melledy committed
164
		
Akka's avatar
Akka committed
165
166
167
		Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
		return this;
	}
168
}