View difference between Paste ID: au1jutn8 and s5T1tfQh
SHOW: | | - or go back to the newest paste.
1
package net.forge.content.world.node;
2
3
/**
4
 * | RuneForge 317 |
5
 * Location.java
6
 * @version 1.0.0
7
 * @author SiniSoul ([email protected])
8
 */
9
public final class Location {
10
    
11
    /**
12
     * The Tile X and Y coordinates.
13
     */
14
    private int tilex = 0, 
15
                tiley = 0;
16
    
17
    /**
18
     * The Height of the location.
19
     */
20
    private int height = 0;
21
    
22
    /**
23
     * The asynchronous Chunk X and Y coordinates; used in region updating. 
24
     */
25
    private int chunkx = 0,
26
                chunky = 0;
27
    
28
    /**
29
     * Sets the Tile X value.
30
     * @param tilex The Tile X value.
31
     */
32
    public void setTileX(int tilex) {
33
        this.tilex = (tilex & 0xFFFF);
34
    }
35
    
36
    /**
37
     * Gets The Tile X, also known as the absolute X.
38
     * @return The Tile X value.
39
     */
40
    public int getTileX() {
41
        return tilex;
42
    }
43
    
44
    /**
45
     * Sets the Tile Y value.
46
     * @param tilex The Tile Y value.
47
     */
48
    public void setTileY(int tiley) {
49
        this.tiley = (tiley & 0xFFFF);
50
    }
51
    
52
    /**
53
     * Gets the Tile Y, also known as the absolute Y.
54
     * @return The Tile Y.
55
     */
56
    public int getTileY() {
57
        return tiley;
58
    }
59
    
60
    /**
61
     * Calculates the Chunk X the location is within.
62
     * @param formated If the chunk is formatted for map positioning or
63
     *                 other formatted chunk comparison.    
64
     * @return 
65
     */
66
    public int calculateChunkX(boolean formated) {
67
        return formated ? (getTileX() >> 3) - 6 : (getTileX() >> 3);
68
    }
69
    
70
    /**
71
     * Calculates the Chunk Y the location is within.
72
     * @param formated If the chunk is formatted for map positioning or
73
     *                 other formatted chunk comparison.                
74
     * @return 
75
     */
76
    public int calculateChunkY(boolean formated) {
77
        return formated ? (getTileY() >> 3) - 6 : (getTileY() >> 3);
78
    }
79
    
80
    /**
81
     * Updates the Chunk X used for walking/region updating.
82
     * This chunk is formatted. 
83
     */
84
    public void updateChunkX() {
85
        this.chunkx = calculateChunkX(true);
86
    }
87
    
88
    /**
89
     * Updates the Chunk Y used for walking/region updating.
90
     * This chunk is formatted.
91
     */
92
    public void updateChunkY() {
93
        this.chunkx = calculateChunkY(true);
94
    }
95
    
96
    /**
97
     * Gets the asynchronous Chunk X.
98
     * @return This Chunk should only be used in walking/region update.
99
     */
100
    public int getChunkX() {
101
        return chunkx;
102
    }
103
    
104
    /**
105
     * Gets the Tile X within the map.
106
     * @return The calculation should never be greater or equal to 104 or
107
     *         less than or equal to 0.
108
     */
109
    public int getMapLocalX() {
110
        return getTileX() - (getChunkX() << 3);
111
    }
112
    
113
    /**
114
     * Gets the asynchronous Chunk Y.
115
     * @return This Chunk should only be used in walking/region update.
116
     */
117
    public int getChunkY() {
118
        return chunky;
119
    }
120
    
121
   /**
122
     * Gets the Tile Y within the map.
123
     * @return The calculation should never be greater or equal to 104 or
124
     *         less than or equal to 0.
125
     */
126
    public int getMapLocalY() {
127
        return getTileX() - (getChunkY() << 3);
128
    }
129
    
130
    /**
131
     * Sets the Height of the location.
132
     * @param height Since the height is never larger than 2 bits,
133
     *               it is masked to never be greater than 3.
134
     */
135
    public void setHeight(int height) {
136
        this.height = (height & 0x3);
137
    }
138
    
139
    /**
140
     * Gets the Height of the location.
141
     * @return The Height.
142
     */
143
    public int getHeight() {
144
        return height;
145
    }
146
    
147
    /**
148
     * Gets/Calculates the Region X.
149
     * @return The calculation uses the non-formatted chunk.
150
     */
151
    public int getRegionX() {
152
        return calculateChunkX(false) >> 3;
153
    }
154
    
155
    /**
156
     * Gets the Tile X within the region, should never be larger than 6 bits.
157
     * Range is 0 - 63 according; corresponding to the face a region is 64 x 64.
158
     * @return The result is automatically masked to be smaller than 63. 
159
     */
160
    public int getRegionLocalX() {
161
        return (getTileX() - (getRegionX() << 6)) & 0x3F;
162
    }
163
    
164
    /**
165
     * Gets/Calculates the Region Y
166
     * @return The calculation uses the non-formatted chunk.
167
     */
168
    public int getRegionY() {
169
        return calculateChunkY(false) >> 3;
170
    }
171
    
172
    /**
173
     * Gets the Tile Y within the region, should never be larger than 6 bits.
174
     * Range is 0 - 63 according; corresponding to the face a region is 64 x 64.
175
     * @return The result is automatically masked to be smaller than 63. 
176
     */
177
    public int getRegionLocalY() {
178
        return (getTileY() - (getRegionY() << 6)) & 0x3F;
179
    }
180
    
181
    /**
182
     * Sets the Tile X Y coordinates and the height of the location.
183
     * 
184
     * @param tilex  The X coordinate of the Tile to set the location at.
185
     * @param tiley  The Y coordinate of the Tile to set the location at.
186
     * @param height The Height of the location.
187
     */
188
    public void set(int tilex, int tiley, int height) {
189
        setTileX(tilex);
190
        setTileY(tiley);
191
        setHeight(height);
192
    }
193
    
194
    /**
195
     *  
196
     * @param tilex  The X coordinate of the Tile to set the location at.
197
     * @param tiley  The Y coordinate of the Tile to set the location at.
198
     * @param height The Height of the location.
199
     * 
200
     * Updates the Chunk X and Y coordinates also.
201
     */
202
    public Location(int tilex, int tiley, int height) {
203
        set(tilex, tiley, height);
204
        updateChunkX();
205
        updateChunkY();
206
    }
207
}