SceneGroup.java 4.56 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;
35

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;
47
48
49
50

	public boolean isLoaded() {
		return loaded;
	}
Melledy's avatar
Melledy committed
51
	
Akka's avatar
Akka committed
52
53
54
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
Melledy's avatar
Melledy committed
55
56
57
58
59
60
61
62
	
	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
63
64
65
66
67

	public CompiledScript getScript() {
		return script;
	}

Melledy's avatar
Melledy committed
68
69
70
	public SceneSuite getSuiteByIndex(int index) {
		return suites.get(index - 1);
	}
Akka's avatar
Akka committed
71

72
73
74
75
76
	public Bindings getBindings() {
		return bindings;
	}

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

83
84
		this.bindings = ScriptLoader.getEngine().createBindings();

Akka's avatar
Akka committed
85
		CompiledScript cs = ScriptLoader.getScriptByPath(
86
				SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType()));
Akka's avatar
Akka committed
87
88
89
90
91
92

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

		this.script = cs;
Melledy's avatar
Melledy committed
93
		
Akka's avatar
Akka committed
94
95
96
97
98
99
100
		// 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
101
102
103
104
105
106
			monsters.values().forEach(m -> m.groupId = id);

			gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets")).stream()
					.collect(Collectors.toMap(x -> x.config_id, y -> y));
			gadgets.values().forEach(m -> m.groupId = id);

Akka's avatar
Akka committed
107
108
109
			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
110

Akka's avatar
Akka committed
111
112
113
			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"));
114
115
116
117
118
119
120
121
122
123
			
			// 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
124
125
126
			// Add variables to suite
			variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));

Melledy's avatar
Melledy committed
127
			// Add monsters and gadgets to suite
Akka's avatar
Akka committed
128
			for (SceneSuite suite : suites) {
Akka's avatar
Akka committed
129
130
131
132
133
134
135
136
137
138
139
140
141
				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
142
143
144
145
146
147
148

				suite.sceneTriggers = new ArrayList<>(
						suite.triggers.stream()
								.filter(triggers::containsKey)
								.map(triggers::get)
								.toList()
				);
Akka's avatar
Akka committed
149
150
151
152
153
			}

		} catch (ScriptException e) {
			Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
		}
Melledy's avatar
Melledy committed
154
		
Akka's avatar
Akka committed
155
156
157
		Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
		return this;
	}
158
}