-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileZonePlan.java
executable file
·287 lines (227 loc) · 6.72 KB
/
TileZonePlan.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Copyright (c) 2011-2014, 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.commander;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.chunk.Chunk;
import buildcraft.api.core.NetworkData;
import buildcraft.core.ItemMapLocation;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.ZonePlan;
import buildcraft.core.inventory.SimpleInventory;
public class TileZonePlan extends TileBuildCraft implements IInventory {
public static final int RESOLUTION = 2048;
public static final int CRAFT_TIME = 120;
private static int RESOLUTION_CHUNKS = RESOLUTION >> 4;
public int chunkStartX, chunkStartZ;
public byte[] colors = new byte[RESOLUTION * RESOLUTION];
@NetworkData
public int progress = 0;
private boolean scan = false;
private int chunkIt = 0;
private ZonePlan[] selectedAreas = new ZonePlan[16];
private int currentSelectedArea = 0;
private SimpleInventory inv = new SimpleInventory(2, "inv", 64);
@Override
public void initialize() {
super.initialize();
chunkStartX = (xCoord >> 4) - RESOLUTION_CHUNKS / 2;
chunkStartZ = (zCoord >> 4) - RESOLUTION_CHUNKS / 2;
if (!scan) {
chunkIt = 0;
scan = true;
}
}
private int[] getCoords() {
int chunkCenterX = xCoord >> 4;
int chunkCenterZ = zCoord >> 4;
if (chunkIt == 0) {
return new int[] {chunkCenterX, chunkCenterZ};
}
int radius = 1;
int left = chunkIt;
while (radius < RESOLUTION_CHUNKS / 2) {
int lineLength = radius * 2;
int perimeter = lineLength * 4;
if (left <= perimeter) {
int chunkX = 0, chunkZ = 0;
int remained = (left - 1) % lineLength;
if ((left - 1) / lineLength == 0) {
chunkX = chunkCenterX + radius;
chunkZ = chunkCenterZ - lineLength / 2 + remained;
} else if ((left - 1) / lineLength == 1) {
chunkX = chunkCenterX - radius;
chunkZ = chunkCenterZ - lineLength / 2 + remained + 1;
} else if ((left - 1) / lineLength == 2) {
chunkX = chunkCenterX - lineLength / 2 + remained + 1;
chunkZ = chunkCenterZ + radius;
} else {
chunkX = chunkCenterX - lineLength / 2 + remained;
chunkZ = chunkCenterZ - radius;
}
return new int[] {chunkX, chunkZ};
} else {
left -= perimeter;
}
radius += 1;
}
return new int[] {chunkCenterX, chunkCenterZ};
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (scan) {
int[] coords = getCoords();
Chunk chunk = worldObj.getChunkFromChunkCoords(coords[0], coords[1]);
loadChunk(chunk);
if (chunkIt > RESOLUTION_CHUNKS * RESOLUTION_CHUNKS) {
scan = false;
chunkIt = 0;
} else {
chunkIt++;
}
}
if (inv.getStackInSlot(0) != null
&& inv.getStackInSlot(1) == null
&& inv.getStackInSlot(0).getItem() instanceof ItemMapLocation) {
if (progress < CRAFT_TIME) {
progress++;
if (worldObj.getTotalWorldTime() % 5 == 0) {
sendNetworkUpdate();
}
} else {
ItemStack stack = inv.decrStackSize(0, 1);
if (selectedAreas[currentSelectedArea] != null) {
ItemMapLocation.setZone(stack, selectedAreas[currentSelectedArea]);
}
inv.setInventorySlotContents(1, stack);
}
} else if (progress != 0) {
progress = 0;
sendNetworkUpdate();
}
}
private void loadChunk(Chunk chunk) {
for (int cx = 0; cx < 16; ++cx) {
for (int cz = 0; cz < 16; ++cz) {
int x = (chunk.xPosition << 4) + cx;
int z = (chunk.zPosition << 4) + cz;
int color = 0;
for (int y = getWorld().getHeight() - 1; y >= 0; --y) {
if (!chunk.getBlock(cx, y, cz).isAir(worldObj, x, y, z)) {
color = chunk.getBlock(cx, y, cz).getMapColor(0).colorIndex;
break;
}
}
int ix = x - chunkStartX * 16;
int iz = z - chunkStartZ * 16;
colors[ix + iz * RESOLUTION] = (byte) color;
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setBoolean("scan", scan);
nbt.setInteger("chunkIt", chunkIt);
nbt.setByteArray("colors", colors);
NBTTagCompound invNBT = new NBTTagCompound();
inv.writeToNBT(invNBT);
nbt.setTag("inv", invNBT);
for (int i = 0; i < selectedAreas.length; ++i) {
if (selectedAreas[i] != null) {
NBTTagCompound subNBT = new NBTTagCompound();
selectedAreas[i].writeToNBT(subNBT);
nbt.setTag("selectedArea[" + i + "]", subNBT);
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
scan = nbt.getBoolean("scan");
chunkIt = nbt.getInteger("chunkIt");
colors = nbt.getByteArray("colors");
if (colors.length != RESOLUTION * RESOLUTION || chunkIt >= colors.length) {
colors = new byte[RESOLUTION * RESOLUTION];
scan = true;
chunkIt = 0;
}
inv.readFromNBT(nbt.getCompoundTag("inv"));
for (int i = 0; i < selectedAreas.length; ++i) {
if (nbt.hasKey("selectedArea[" + i + "]")) {
selectedAreas[i] = new ZonePlan();
selectedAreas[i].readFromNBT(nbt.getCompoundTag("selectedArea[" + i + "]"));
}
}
}
public Object selectArea(int index) {
if (selectedAreas[index] == null) {
selectedAreas[index] = new ZonePlan();
}
currentSelectedArea = index;
return selectedAreas[index];
}
public void setArea(int index, ZonePlan area) {
selectedAreas[index] = area;
}
@Override
public int getSizeInventory() {
return inv.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slotId) {
return inv.getStackInSlot(slotId);
}
@Override
public ItemStack decrStackSize(int slotId, int count) {
return inv.decrStackSize(slotId, count);
}
@Override
public ItemStack getStackInSlotOnClosing(int slotId) {
return inv.getStackInSlotOnClosing(slotId);
}
@Override
public void setInventorySlotContents(int slotId, ItemStack itemstack) {
inv.setInventorySlotContents(slotId, itemstack);
}
@Override
public String getInventoryName() {
return inv.getInventoryName();
}
@Override
public boolean hasCustomInventoryName() {
return inv.hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit() {
return inv.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return inv.isUseableByPlayer(entityplayer);
}
@Override
public void openInventory() {
inv.openInventory();
}
@Override
public void closeInventory() {
inv.closeInventory();
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
return inv.isItemValidForSlot(i, itemstack);
}
}