SceneGroup.java 4.06 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
12
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptException;
import java.util.ArrayList;
Akka's avatar
Akka committed
13
import java.util.Collection;
14
15
import java.util.List;
import java.util.Map;
Akka's avatar
Akka committed
16
17
import java.util.stream.Collectors;

18
import static emu.grasscutter.Configuration.*;
19

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

Melledy's avatar
Melledy committed
29
30
	public Map<Integer,SceneMonster> monsters; // <ConfigId, Monster>
	public Map<Integer, SceneGadget> gadgets; // <ConfigId, Gadgets>
Akka's avatar
Akka committed
31
	public Map<String, SceneTrigger> triggers;
32

Melledy's avatar
Melledy committed
33
	public List<SceneRegion> regions;
34
	public List<SceneSuite> suites;
Melledy's avatar
Melledy committed
35
36
37
38
	public List<SceneVar> variables;
	
	public SceneBusiness business;
	public SceneGarbage garbages;
39
	public SceneInitConfig init_config;
Akka's avatar
Akka committed
40
41

	private transient boolean loaded; // Not an actual variable in the scripts either
Melledy's avatar
Melledy committed
42
	private transient CompiledScript script;
43
44
45
46

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

	public CompiledScript getScript() {
		return script;
	}

Melledy's avatar
Melledy committed
64
65
66
	public SceneSuite getSuiteByIndex(int index) {
		return suites.get(index - 1);
	}
Akka's avatar
Akka committed
67
68
69
70
71
72
73
74
75

	public SceneGroup load(int sceneId, Bindings bindings){
		if(loaded){
			return this;
		}
		// Set flag here so if there is no script, we dont call this function over and over again.
		setLoaded(true);

		CompiledScript cs = ScriptLoader.getScriptByPath(
76
				SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType()));
Akka's avatar
Akka committed
77
78
79
80
81
82

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

		this.script = cs;
Melledy's avatar
Melledy committed
83
		
Akka's avatar
Akka committed
84
85
86
87
88
89
90
		// 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
91
92
93
94
95
96
			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
97
98
99
			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
100

Akka's avatar
Akka committed
101
102
			suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
			regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions"));
Melledy's avatar
Melledy committed
103
			garbages = ScriptLoader.getSerializer().toObject(SceneGarbage.class, bindings.get("garbages"));
Akka's avatar
Akka committed
104
105
106
107
108
			init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config"));

			// Add variables to suite
			variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));

Melledy's avatar
Melledy committed
109
			// Add monsters and gadgets to suite
Akka's avatar
Akka committed
110
			for (SceneSuite suite : suites) {
Akka's avatar
Akka committed
111
112
113
114
115
116
117
118
119
120
121
122
123
				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
124
125
126
127
128
129
130

				suite.sceneTriggers = new ArrayList<>(
						suite.triggers.stream()
								.filter(triggers::containsKey)
								.map(triggers::get)
								.toList()
				);
Akka's avatar
Akka committed
131
132
133
134
135
			}

		} catch (ScriptException e) {
			Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
		}
Melledy's avatar
Melledy committed
136
		
Akka's avatar
Akka committed
137
138
139
		Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
		return this;
	}
140
}