SceneGroup.java 5.24 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
import java.util.Optional;
Akka's avatar
Akka committed
20
21
import java.util.stream.Collectors;

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

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

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

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

	public boolean isLoaded() {
		return loaded;
	}
Melledy's avatar
Melledy committed
57
	
Akka's avatar
Akka committed
58
59
60
	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}
Melledy's avatar
Melledy committed
61
62
63
64
65
66
67
68
	
	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
69
70
71
72
73

	public CompiledScript getScript() {
		return script;
	}

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

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

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

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

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

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

		this.script = cs;
Melledy's avatar
Melledy committed
99
		
Akka's avatar
Akka committed
100
101
102
103
104
105
106
		// 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
107
			monsters.values().forEach(m -> m.group = this);
Akka's avatar
Akka committed
108
109
110

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

Akka's avatar
Akka committed
113
114
115
			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
116

Akka's avatar
Akka committed
117
118
119
			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"));
120
121
122
123
124
125
126
			
			// 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
127
					garbages.gadgets.forEach(m -> m.group = this);
128
129
130
				}
			}
			
Akka's avatar
Akka committed
131
132
			// Add variables to suite
			variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));
Akka's avatar
Akka committed
133
134
135
136
			// 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
137

Melledy's avatar
Melledy committed
138
			// Add monsters and gadgets to suite
Akka's avatar
Akka committed
139
			for (SceneSuite suite : suites) {
Akka's avatar
Akka committed
140
141
142
143
144
145
146
147
148
149
150
151
152
				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
153
154
155
156
157
158
159

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

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

	public Optional<SceneBossChest> searchBossChestInGroup() {
		return gadgets.values().stream()
				.filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0)
				.map(g -> g.boss_chest)
				.findFirst();
	}

177
}