SceneBlock.java 2.37 KB
Newer Older
1
2
package emu.grasscutter.scripts.data;

3
4
5
import com.github.davidmoten.rtreemulti.RTree;
import com.github.davidmoten.rtreemulti.geometry.Geometry;
import com.github.davidmoten.rtreemulti.geometry.Rectangle;
Akka's avatar
Akka committed
6
7
8
9
import emu.grasscutter.Grasscutter;
import emu.grasscutter.scripts.SceneIndexManager;
import emu.grasscutter.scripts.ScriptLoader;
import emu.grasscutter.utils.Position;
Akka's avatar
Akka committed
10
11
import lombok.Setter;
import lombok.ToString;
Akka's avatar
Akka committed
12
13
14
15

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptException;
Akka's avatar
Akka committed
16
17
import java.util.Map;
import java.util.stream.Collectors;
18

Akka's avatar
Akka committed
19
import static emu.grasscutter.Configuration.SCRIPT;
20

Akka's avatar
Akka committed
21
22
@ToString
@Setter
23
24
25
26
public class SceneBlock {
	public int id;
	public Position max;
	public Position min;
Akka's avatar
Akka committed
27
28

	public int sceneId;
Akka's avatar
Akka committed
29
	public Map<Integer,SceneGroup> groups;
30
	public RTree<SceneGroup, Geometry> sceneGroupIndex;
Akka's avatar
Akka committed
31
32
33
34

	private transient boolean loaded; // Not an actual variable in the scripts either

	public boolean isLoaded() {
35
		return this.loaded;
Akka's avatar
Akka committed
36
37
38
39
40
41
	}

	public void setLoaded(boolean loaded) {
		this.loaded = loaded;
	}

42
	public boolean contains(Position pos) {
43
44
		return 	pos.getX() <= this.max.getX() && pos.getX() >= this.min.getX() &&
				pos.getZ() <= this.max.getZ() && pos.getZ() >= this.min.getZ();
45
	}
Akka's avatar
Akka committed
46
47

	public SceneBlock load(int sceneId, Bindings bindings){
48
		if(this.loaded){
Akka's avatar
Akka committed
49
50
51
			return this;
		}
		this.sceneId = sceneId;
52
        this.setLoaded(true);
Akka's avatar
Akka committed
53
54

		CompiledScript cs = ScriptLoader.getScriptByPath(
55
				SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + this.id + "." + ScriptLoader.getScriptType()));
Akka's avatar
Akka committed
56
57
58
59
60
61
62
63
64
65

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

		// Eval script
		try {
			cs.eval(bindings);

			// Set groups
66
            this.groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")).stream()
Akka's avatar
Akka committed
67
68
					.collect(Collectors.toMap(x -> x.id, y -> y));

69
70
71
72
            this.groups.values().forEach(g -> g.block_id = this.id);
			this.sceneGroupIndex = SceneIndexManager.buildIndex(3, this.groups.values(), g -> g.pos.toPoint());
		} catch (ScriptException exception) {
            Grasscutter.getLogger().error("An error occurred while loading block " + this.id + " in scene " + sceneId, exception);
Akka's avatar
Akka committed
73
		}
74
		Grasscutter.getLogger().debug("Successfully loaded block {} in scene {}.", this.id, sceneId);
Akka's avatar
Akka committed
75
76
		return this;
	}
77
78

	public Rectangle toRectangle() {
79
		return Rectangle.create(this.min.toXZDoubleArray(), this.max.toXZDoubleArray());
80
	}
Akka's avatar
Akka committed
81
}