-
Notifications
You must be signed in to change notification settings - Fork 497
/
ZonePlan.java
executable file
·171 lines (135 loc) · 4.03 KB
/
ZonePlan.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
131
132
133
134
135
136
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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.ISerializable;
import buildcraft.api.core.IZone;
public class ZonePlan implements IZone, ISerializable {
private HashMap<ChunkIndex, ZoneChunk> chunkMapping = new HashMap<ChunkIndex, ZoneChunk>();
public boolean get(int x, int z) {
int xChunk = x >> 4;
int zChunk = z >> 4;
ChunkIndex chunkId = new ChunkIndex(xChunk, zChunk);
ZoneChunk property;
if (!chunkMapping.containsKey(chunkId)) {
return false;
} else {
property = chunkMapping.get(chunkId);
return property.get(x & 0xF, z & 0xF);
}
}
public void set(int x, int z, boolean val) {
int xChunk = x >> 4;
int zChunk = z >> 4;
ChunkIndex chunkId = new ChunkIndex(xChunk, zChunk);
ZoneChunk property;
if (!chunkMapping.containsKey(chunkId)) {
if (val) {
property = new ZoneChunk();
chunkMapping.put(chunkId, property);
} else {
return;
}
} else {
property = chunkMapping.get(chunkId);
}
property.set(x & 0xF, z & 0xF, val);
if (property.isEmpty()) {
chunkMapping.remove(chunkId);
}
}
public void writeToNBT(NBTTagCompound nbt) {
NBTTagList list = new NBTTagList();
for (Map.Entry<ChunkIndex, ZoneChunk> e : chunkMapping.entrySet()) {
NBTTagCompound subNBT = new NBTTagCompound();
e.getKey().writeToNBT(subNBT);
e.getValue().writeToNBT(subNBT);
list.appendTag(subNBT);
}
nbt.setTag("chunkMapping", list);
}
public void readFromNBT(NBTTagCompound nbt) {
NBTTagList list = nbt.getTagList("chunkMapping", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound subNBT = list.getCompoundTagAt(i);
ChunkIndex id = new ChunkIndex();
id.readFromNBT(subNBT);
ZoneChunk chunk = new ZoneChunk();
chunk.readFromNBT(subNBT);
chunkMapping.put(id, chunk);
}
}
@Override
public double distanceTo(BlockIndex index) {
return Math.sqrt(distanceToSquared(index));
}
@Override
public double distanceToSquared(BlockIndex index) {
double maxSqrDistance = Double.MAX_VALUE;
for (Map.Entry<ChunkIndex, ZoneChunk> e : chunkMapping.entrySet()) {
double dx = (e.getKey().x << 4 + 8) - index.x;
double dz = (e.getKey().x << 4 + 8) - index.z;
double sqrDistance = dx * dx + dz * dz;
if (sqrDistance < maxSqrDistance) {
maxSqrDistance = sqrDistance;
}
}
return maxSqrDistance;
}
@Override
public boolean contains(double x, double y, double z) {
int xBlock = (int) Math.floor(x);
int zBlock = (int) Math.floor(z);
return get(xBlock, zBlock);
}
@Override
public BlockIndex getRandomBlockIndex(Random rand) {
if (chunkMapping.size() == 0) {
return null;
}
int chunkId = rand.nextInt(chunkMapping.size());
for (Map.Entry<ChunkIndex, ZoneChunk> e : chunkMapping.entrySet()) {
if (chunkId == 0) {
BlockIndex i = e.getValue().getRandomBlockIndex(rand);
i.x = (e.getKey().x << 4) + i.x;
i.z = (e.getKey().z << 4) + i.z;
return i;
}
chunkId--;
}
return null;
}
@Override
public void readData(ByteBuf stream) {
chunkMapping.clear();
int size = stream.readInt();
for (int i = 0; i < size; i++) {
ChunkIndex key = new ChunkIndex();
ZoneChunk value = new ZoneChunk();
key.readData(stream);
value.readData(stream);
chunkMapping.put(key, value);
}
}
@Override
public void writeData(ByteBuf stream) {
stream.writeInt(chunkMapping.size());
for (Map.Entry<ChunkIndex, ZoneChunk> e : chunkMapping.entrySet()) {
e.getKey().writeData(stream);
e.getValue().writeData(stream);
}
}
}