-
Notifications
You must be signed in to change notification settings - Fork 497
/
EntityRobot.java
316 lines (229 loc) · 6.75 KB
/
EntityRobot.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Copyright (c) SpaceToad, 2011-2012
* 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.ArrayList;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import buildcraft.BuildCraftCore;
import buildcraft.api.blueprints.BptSlotInfo;
import buildcraft.api.core.Position;
import buildcraft.core.blueprints.BptBuilderBase;
import buildcraft.core.blueprints.BptContext;
import buildcraft.core.blueprints.BptSlot;
import buildcraft.core.blueprints.BptSlot.Mode;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtil;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
public class EntityRobot extends Entity implements IEntityAdditionalSpawnData {
private Box box;
private int destX, destY, destZ;
EntityEnergyLaser laser;
public LinkedList<Action> targets = new LinkedList<Action>();
public static int MAX_TARGETS = 20;
public int wait = 0;
private class Action {
public Action(BptSlot slot, BptContext context) {
this.slot = slot;
this.context = context;
}
public Action(BptBuilderBase builder) {
this.builder = builder;
}
BptSlot slot;
BptBuilderBase builder;
BptContext context;
}
public EntityRobot(World world) {
super(world);
}
public EntityRobot(World world, Box box) {
super(world);
this.box = box;
init();
}
protected void init() {
destX = (int) box.centerX();
destY = (int) box.centerY();
destZ = (int) box.centerZ();
motionX = 0;
motionY = 0;
motionZ = 0;
setLocationAndAngles(destX, destY, destZ, 0, 0);
laser = new EntityEnergyLaser(worldObj, new Position(posX, posY, posZ), new Position(posX, posY, posZ));
worldObj.spawnEntityInWorld(laser);
}
@Override
public void writeSpawnData(ByteArrayDataOutput data) {
if (box == null) {
box = new Box();
}
data.writeInt(box.xMin);
data.writeInt(box.yMin);
data.writeInt(box.zMin);
data.writeInt(box.xMax);
data.writeInt(box.yMax);
data.writeInt(box.zMax);
}
@Override
public void readSpawnData(ByteArrayDataInput data) {
box = new Box();
box.xMin = data.readInt();
box.yMin = data.readInt();
box.zMin = data.readInt();
box.xMax = data.readInt();
box.yMax = data.readInt();
box.zMax = data.readInt();
init();
}
@Override
protected void entityInit() {
}
@Override
protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {
}
@Override
protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {
}
@Override
public void onUpdate() {
move();
build();
updateLaser();
}
protected void move() {
setPosition(posX + motionX, posY + motionY, posZ + motionZ);
if (reachedDesination()) {
BlockIndex newDesination = getNewDestination();
if (newDesination != null) {
setDestination(newDesination.x, newDesination.y, newDesination.z);
}
}
}
protected BlockIndex getNewDestination() {
Box movementBoundary = new Box();
movementBoundary.initialize(box);
movementBoundary.expand(1);
Box moveArea = new Box();
moveArea.initialize(destX, destY, destZ, 1);
List<BlockIndex> potentialDestinations = new ArrayList<BlockIndex>();
for (BlockIndex blockIndex : moveArea.getBlocksInArea()) {
if (BlockUtil.isSoftBlock(worldObj, blockIndex.x, blockIndex.y, blockIndex.z) && movementBoundary.contains(blockIndex)) {
potentialDestinations.add(blockIndex);
}
}
if (!potentialDestinations.isEmpty()) {
int i = worldObj.rand.nextInt(potentialDestinations.size());
return potentialDestinations.get(i);
}
return null;
}
protected void setDestination(int x, int y, int z) {
destX = x;
destY = y;
destZ = z;
motionX = (destX - posX) / 75 * (laser.getPowerAverage() / 2 + 1);
motionY = (destY - posY) / 75 * (laser.getPowerAverage() / 2 + 1);
motionZ = (destZ - posZ) / 75 * (laser.getPowerAverage() / 2 + 1);
}
protected boolean reachedDesination() {
if (getDistance(destX, destY, destZ) <= .2)
return true;
return false;
}
protected void build() {
updateWait();
if (targets.size() > 0) {
Action a = targets.getFirst();
if (a.slot != null) {
BptSlot target = a.slot;
//System.out.printf("RobotChanging %d %d %d %s\n",target.x, target.y, target.z, target.mode);
if (wait <= 0 && BlockUtil.canChangeBlock(worldObj, target.x, target.y, target.z)) {
if (!CoreProxy.proxy.isRenderWorld(worldObj)) {
if (target.mode == Mode.ClearIfInvalid) {
if (!target.isValid(a.context)) {
worldObj.setBlockToAir(target.x, target.y, target.z);
}
} else if (target.stackToUse != null) {
worldObj.setBlockToAir(target.x, target.y, target.z);
throw new RuntimeException("NOT IMPLEMENTED");
// target.stackToUse.getItem().onItemUse(target.stackToUse,
// CoreProxy.getBuildCraftPlayer(worldObj), worldObj, target.x, target.y - 1,
// target.z, 1);
} else {
try {
target.buildBlock(a.context);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
BuildCraftCore.bcLog.throwing("EntityRobot", "update", t);
}
}
}
targets.pop();
}
} else if (a.builder != null) {
a.builder.postProcessing(worldObj);
targets.pop();
}
}
}
public void updateWait() {
if (targets.size() > 0)
if (wait == 0) {
wait = MAX_TARGETS - targets.size() + 2;
} else {
wait--;
}
}
private void updateLaser() {
if (laser == null)
return;
if (targets.size() > 0) {
Action a = targets.getFirst();
BptSlotInfo target = a.slot;
if (target != null) {
laser.setPositions(new Position(posX, posY, posZ), new Position(target.x + 0.5, target.y + 0.5, target.z + 0.5));
laser.show();
}
} else {
laser.hide();
}
laser.pushPower(((float) targets.size()) / ((float) MAX_TARGETS) * 4F);
}
public void scheduleContruction(BptSlot slot, BptContext context) {
if (slot != null) {
targets.add(new Action(slot, context));
}
}
public void markEndOfBlueprint(BptBuilderBase builder) {
targets.add(new Action(builder));
}
public boolean readyToBuild() {
return targets.size() < MAX_TARGETS;
}
public boolean done() {
return targets.isEmpty();
}
public void setBox(Box box) {
this.box = box;
setDestination((int) box.centerX(), (int) box.centerY(), (int) box.centerZ());
}
@Override
public void setDead() {
if (laser != null) {
laser.setDead();
}
super.setDead();
}
}