SceneGroup.java 4.88 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
import org.luaj.vm2.LuaValue;
9

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

import static emu.grasscutter.config.Configuration.SCRIPT;

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

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

26
27
28
	public int id;
	public int refresh_id;
	public Position pos;
29

Melledy's avatar
Melledy committed
30
31
	public Map<Integer,SceneMonster> monsters; // <ConfigId, Monster>
	public Map<Integer, SceneGadget> gadgets; // <ConfigId, Gadgets>
Akka's avatar
Akka committed
32
	public Map<String, SceneTrigger> triggers;
Akka's avatar
Akka committed
33
    public Map<Integer, SceneRegion> regions;
34
	public List<SceneSuite> suites;
Melledy's avatar
Melledy committed
35
	public List<SceneVar> variables;
Akka's avatar
Akka committed
36

Melledy's avatar
Melledy committed
37
38
	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
	private transient Bindings bindings;
Akka's avatar
Akka committed
44
45
46
47
48
	public static SceneGroup of(int groupId) {
		var group = new SceneGroup();
		group.id = groupId;
		return group;
	}
49
50

	public boolean isLoaded() {
51
		return this.loaded;
52
	}
53

Akka's avatar
Akka committed
54
55
56
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
57

Melledy's avatar
Melledy committed
58
59
60
	public int getBusinessType() {
		return this.business == null ? 0 : this.business.type;
	}
61

Melledy's avatar
Melledy committed
62
63
64
	public List<SceneGadget> getGarbageGadgets() {
		return this.garbages == null ? null : this.garbages.gadgets;
	}
Akka's avatar
Akka committed
65
66

	public CompiledScript getScript() {
67
		return this.script;
Akka's avatar
Akka committed
68
69
	}

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

74
	public Bindings getBindings() {
75
		return this.bindings;
76
77
	}

Akka's avatar
Akka committed
78
	public synchronized SceneGroup load(int sceneId){
79
		if(this.loaded){
Akka's avatar
Akka committed
80
81
			return this;
		}
82
83
		// 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
84

85
86
		this.bindings = ScriptLoader.getEngine().createBindings();

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

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

		this.script = cs;
95

Akka's avatar
Akka committed
96
97
		// Eval script
		try {
98
			cs.eval(this.bindings);
Akka's avatar
Akka committed
99
100

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

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

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

113
114
            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
115
                .collect(Collectors.toMap(x -> x.config_id, y -> y));
116
            this.regions.values().forEach(m -> m.group = this);
Akka's avatar
Akka committed
117

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

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

Akka's avatar
Akka committed
130
			// Add variables to suite
131
			this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables"));
Akka's avatar
Akka committed
132

Melledy's avatar
Melledy committed
133
			// Add monsters and gadgets to suite
134
            this.suites.forEach(i -> i.init(this));
Akka's avatar
Akka committed
135
136

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

140
		Grasscutter.getLogger().debug("Successfully loaded group {} in scene {}.", this.id, sceneId);
Akka's avatar
Akka committed
141
142
		return this;
	}
Akka's avatar
Akka committed
143
144

	public Optional<SceneBossChest> searchBossChestInGroup() {
145
		return this.gadgets.values().stream()
Akka's avatar
Akka committed
146
147
148
149
150
				.filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0)
				.map(g -> g.boss_chest)
				.findFirst();
	}

151
}