Position.java 3.82 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.utils;

import java.io.Serializable;

Melledy's avatar
Melledy committed
5
import com.google.gson.annotations.SerializedName;
6
import com.github.davidmoten.rtreemulti.geometry.Point;
7
import dev.morphia.annotations.Entity;
Melledy's avatar
Melledy committed
8
9
import emu.grasscutter.net.proto.VectorOuterClass.Vector;

10
@Entity
Melledy's avatar
Melledy committed
11
12
public class Position implements Serializable {
	private static final long serialVersionUID = -2001232313615923575L;
赵怡然's avatar
赵怡然 committed
13

Melledy's avatar
Melledy committed
14
	@SerializedName(value="x", alternate={"_x", "X"})
Melledy's avatar
Melledy committed
15
	private float x;
赵怡然's avatar
赵怡然 committed
16

Melledy's avatar
Melledy committed
17
	@SerializedName(value="y", alternate={"_y", "Y"})
Melledy's avatar
Melledy committed
18
	private float y;
赵怡然's avatar
赵怡然 committed
19

Melledy's avatar
Melledy committed
20
	@SerializedName(value="z", alternate={"_z", "Z"})
Melledy's avatar
Melledy committed
21
	private float z;
赵怡然's avatar
赵怡然 committed
22

Melledy's avatar
Melledy committed
23
24
25
	public Position() {

	}
赵怡然's avatar
赵怡然 committed
26

Melledy's avatar
Melledy committed
27
28
29
	public Position(float x, float y) {
		set(x, y);
	}
赵怡然's avatar
赵怡然 committed
30

Melledy's avatar
Melledy committed
31
32
33
	public Position(float x, float y, float z) {
		set(x, y, z);
	}
赵怡然's avatar
赵怡然 committed
34

Melledy's avatar
Melledy committed
35
36
37
38
39
40
41
42
43
44
	public Position(String p) {
		String[] split = p.split(",");
		if (split.length >= 2) {
			this.x = Float.parseFloat(split[0]);
			this.y = Float.parseFloat(split[1]);
		}
		if (split.length >= 3) {
			this.z = Float.parseFloat(split[2]);
		}
	}
赵怡然's avatar
赵怡然 committed
45

Melledy's avatar
Melledy committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	public Position(Vector vector) {
		this.set(vector);
	}

	public Position(Position pos) {
		this.set(pos);
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getZ() {
		return z;
	}

	public void setZ(float z) {
		this.z = z;
	}
赵怡然's avatar
赵怡然 committed
69

Melledy's avatar
Melledy committed
70
71
72
73
74
75
76
	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}
赵怡然's avatar
赵怡然 committed
77

Melledy's avatar
Melledy committed
78
79
80
81
82
	public Position set(float x, float y) {
		this.x = x;
		this.y = y;
		return this;
	}
赵怡然's avatar
赵怡然 committed
83

Melledy's avatar
Melledy committed
84
85
86
87
	// Deep copy
	public Position set(Position pos) {
		return this.set(pos.getX(), pos.getY(), pos.getZ());
	}
赵怡然's avatar
赵怡然 committed
88

Melledy's avatar
Melledy committed
89
90
91
	public Position set(Vector pos) {
		return this.set(pos.getX(), pos.getY(), pos.getZ());
	}
赵怡然's avatar
赵怡然 committed
92

Melledy's avatar
Melledy committed
93
94
95
96
97
98
99
100
101
102
103
104
105
	public Position set(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
		return this;
	}

	public Position add(Position add) {
		this.x += add.getX();
		this.y += add.getY();
		this.z += add.getZ();
		return this;
	}
赵怡然's avatar
赵怡然 committed
106

Melledy's avatar
Melledy committed
107
108
109
110
	public Position addX(float d) {
		this.x += d;
		return this;
	}
赵怡然's avatar
赵怡然 committed
111

Melledy's avatar
Melledy committed
112
113
114
115
	public Position addY(float d) {
		this.y += d;
		return this;
	}
赵怡然's avatar
赵怡然 committed
116

Melledy's avatar
Melledy committed
117
118
119
120
121
122
123
124
125
126
127
	public Position addZ(float d) {
		this.z += d;
		return this;
	}

	public Position subtract(Position sub) {
		this.x -= sub.getX();
		this.y -= sub.getY();
		this.z -= sub.getZ();
		return this;
	}
赵怡然's avatar
赵怡然 committed
128

Melledy's avatar
Melledy committed
129
130
131
132
133
134
135
136
137
	/** In radians
	 * */
	public Position translate(float dist, float angle) {
		this.x += dist * Math.sin(angle);
		this.y += dist * Math.cos(angle);
		return this;
	}

	public boolean equal2d(Position other) {
138
139
140
141
142
143
144
145
146
147
148
149
150
		// Y is height
		return getX() == other.getX() && getZ() == other.getZ();
	}

	public boolean equal3d(Position other) {
		return getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ();
	}

	public double computeDistance(Position b){
		double detX = getX()-b.getX();
		double detY = getY()-b.getY();
		double detZ = getZ()-b.getZ();
		return Math.sqrt(detX*detX+detY*detY+detZ*detZ);
Melledy's avatar
Melledy committed
151
	}
赵怡然's avatar
赵怡然 committed
152
153
154
155
156
157
158

    public Position nearby2d(int range){
        Position position = clone();
        position.z += (float)Utils.randomRange(-range,range)/10;
        position.x += (float)Utils.randomRange(-range,range)/10;
        return position;
    }
Melledy's avatar
Melledy committed
159
160
161
162
163
164
	public Position translateWithDegrees(float dist, float angle) {
		angle = (float) Math.toRadians(angle);
		this.x += dist * Math.sin(angle);
		this.y += -dist * Math.cos(angle);
		return this;
	}
赵怡然's avatar
赵怡然 committed
165

Melledy's avatar
Melledy committed
166
167
168
169
170
171
172
173
174
	@Override
	public Position clone() {
		return new Position(x, y, z);
	}

	@Override
	public String toString() {
		return "(" + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")";
	}
赵怡然's avatar
赵怡然 committed
175

Melledy's avatar
Melledy committed
176
177
178
179
180
181
182
	public Vector toProto() {
		return Vector.newBuilder()
			.setX(this.getX())
			.setY(this.getY())
			.setZ(this.getZ())
			.build();
	}
183
184
	public Point toPoint(){
		return Point.create(x,y,z);
Akka's avatar
Akka committed
185
	}
186
187
188
189
190
191
192
193
194
195
196
197

	/**
	 * To XYZ array for Spatial Index
	 */
	public double[] toDoubleArray(){
		return new double[]{ x, y, z};
	}
	/**
	 * To XZ array for Spatial Index (Blocks)
	 */
	public double[] toXZDoubleArray(){
		return new double[]{x, z};
Akka's avatar
Akka committed
198
	}
Melledy's avatar
Melledy committed
199
}