Position.java 4.23 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
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
AnimeGitB's avatar
AnimeGitB committed
9
10
import lombok.Getter;
import lombok.Setter;
Melledy's avatar
Melledy committed
11

12
@Entity
Melledy's avatar
Melledy committed
13
public class Position implements Serializable {
github-actions's avatar
github-actions committed
14
15
16
    private static final long serialVersionUID = -2001232313615923575L;

    @SerializedName(value="x", alternate={"_x", "X"})
AnimeGitB's avatar
AnimeGitB committed
17
    @Getter @Setter private float x;
github-actions's avatar
github-actions committed
18
19

    @SerializedName(value="y", alternate={"_y", "Y"})
AnimeGitB's avatar
AnimeGitB committed
20
    @Getter @Setter private float y;
github-actions's avatar
github-actions committed
21
22

    @SerializedName(value="z", alternate={"_z", "Z"})
AnimeGitB's avatar
AnimeGitB committed
23
    @Getter @Setter private float z;
github-actions's avatar
github-actions committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    public Position() {

    }

    public Position(float x, float y) {
        set(x, y);
    }

    public Position(float x, float y, float z) {
        set(x, y, z);
    }

    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]);
        }
    }

    public Position(Vector vector) {
        this.set(vector);
    }

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

    public Position set(float x, float y) {
        this.x = x;
        this.y = y;
        return this;
    }

    // Deep copy
    public Position set(Position pos) {
        return this.set(pos.getX(), pos.getY(), pos.getZ());
    }

    public Position set(Vector pos) {
        return this.set(pos.getX(), pos.getY(), pos.getZ());
    }

    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;
    }

    public Position addX(float d) {
        this.x += d;
        return this;
    }

    public Position addY(float d) {
        this.y += d;
        return this;
    }

    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;
    }

    /** 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) {
        // 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);
    }

AnimeGitB's avatar
AnimeGitB committed
131
    public Position nearby2d(float range) {
赵怡然's avatar
赵怡然 committed
132
        Position position = clone();
AnimeGitB's avatar
AnimeGitB committed
133
134
        position.z += Utils.randomFloatRange(-range, range);
        position.x += Utils.randomFloatRange(-range, range);
赵怡然's avatar
赵怡然 committed
135
136
        return position;
    }
github-actions's avatar
github-actions committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    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;
    }

    @Override
    public Position clone() {
        return new Position(x, y, z);
    }

    @Override
    public String toString() {
        return "(" + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")";
    }

    public Vector toProto() {
        return Vector.newBuilder()
            .setX(this.getX())
            .setY(this.getY())
            .setZ(this.getZ())
            .build();
    }
    public Point toPoint() {
        return Point.create(x,y,z);
    }

    /**
     * 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};
    }
Melledy's avatar
Melledy committed
177
}