SceneBlock.java 2.38 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;
16
17
18

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

Akka's avatar
Akka committed
19
20
import java.util.Map;
import java.util.stream.Collectors;
21

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

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

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

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

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

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

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

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

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

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

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

70
71
72
73
            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
74
		}
75
		Grasscutter.getLogger().debug("Successfully loaded block {} in scene {}.", this.id, sceneId);
Akka's avatar
Akka committed
76
77
		return this;
	}
78
79

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