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

import static emu.grasscutter.Configuration.SCRIPTS_FOLDER;
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
29
30
31
32

	/**
	 * ConfigId - Monster
	 */
	public Map<Integer,SceneMonster> monsters;
Akka's avatar
Akka committed
33
34
35
36
	/**
	 * ConfigId - Gadget
	 */
	public Map<Integer, SceneGadget> gadgets;
37
	public List<SceneTrigger> triggers;
Melledy's avatar
Melledy committed
38
	public List<SceneRegion> regions;
39
40
	public List<SceneSuite> suites;
	public SceneInitConfig init_config;
Akka's avatar
Akka committed
41
42
43

	public List<SceneVar> variables;
	private transient boolean loaded; // Not an actual variable in the scripts either
44
45
46
47

	public boolean isLoaded() {
		return loaded;
	}
Melledy's avatar
Melledy committed
48
	
Akka's avatar
Akka committed
49
50
51
52
53
54
55
56
57
58
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}

	private transient CompiledScript script; // Not an actual variable in the scripts either

	public CompiledScript getScript() {
		return script;
	}

Melledy's avatar
Melledy committed
59
60
61
	public SceneSuite getSuiteByIndex(int index) {
		return suites.get(index - 1);
	}
Akka's avatar
Akka committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

	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(
				SCRIPTS_FOLDER + "Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType());

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

		this.script = cs;
		// 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
85
86
87
88
89
90
			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
91
			triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers"));
Akka's avatar
Akka committed
92
93
			triggers.forEach(t -> t.currentGroup = this);

Akka's avatar
Akka committed
94
95
96
97
98
99
100
			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"));

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

Akka's avatar
Akka committed
101
			// Add monsters to suite
Akka's avatar
Akka committed
102
			for (SceneSuite suite : suites) {
Akka's avatar
Akka committed
103
104
105
106
107
108
109
110
111
112
113
114
115
				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
116
117
118
119
120
121
122
123
			}

		} catch (ScriptException e) {
			Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
		}
		Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
		return this;
	}
124
}