-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotGotoBlock.java
executable file
·190 lines (154 loc) · 5.66 KB
/
AIRobotGotoBlock.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
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* 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.robotics.ai;
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.utils.IterableAlgorithmRunner;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.core.lib.utils.PathFinding;
import buildcraft.core.lib.utils.Utils;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.BlockPos;
import net.minecraftforge.common.util.Constants;
import java.util.LinkedList;
public class AIRobotGotoBlock extends AIRobotGoto {
private PathFinding pathSearch;
private IterableAlgorithmRunner pathSearchJob;
private LinkedList<BlockPos> path;
private double prevDistance = Double.MAX_VALUE;
private BlockPos finalPos;
private double maxDistance = 0;
private BlockPos lastBlockInPath;
private boolean loadedFromNBT;
public AIRobotGotoBlock(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotGotoBlock(EntityRobotBase robot, BlockPos pos) {
this(robot);
finalPos = pos;
}
public AIRobotGotoBlock(EntityRobotBase robot, BlockPos pos, double iMaxDistance) {
this(robot, pos);
maxDistance = iMaxDistance;
}
public AIRobotGotoBlock(EntityRobotBase robot, LinkedList<BlockPos> iPath) {
this(robot);
path = iPath;
finalPos = path.getLast();
setNextInPath();
}
@Override
public void start() {
robot.undock();
}
@Override
public void update() {
if (loadedFromNBT) {
// Prevent a race condition with terminate() being called in
// setNextInPath.
setNextInPath();
loadedFromNBT = false;
}
if (path == null && pathSearch == null) {
pathSearch = new PathFinding(robot.worldObj, new BlockPos((int) Math.floor(robot.posX), (int) Math.floor(robot.posY), (int) Math.floor(
robot.posZ)), finalPos, maxDistance);
pathSearchJob = new IterableAlgorithmRunner(pathSearch, 50);
pathSearchJob.start();
} else if (path != null && next != null) {
double distance = robot.getDistance(next.xCoord, next.yCoord, next.zCoord);
if (!robot.isMoving() || distance > prevDistance) {
if (path.size() > 0) {
path.removeFirst();
}
setNextInPath();
} else {
prevDistance = robot.getDistance(next.xCoord, next.yCoord, next.zCoord);
}
} else {
if (pathSearchJob.isDone()) {
path = pathSearch.getResult();
if (path.size() == 0) {
setSuccess(false);
terminate();
return;
}
lastBlockInPath = path.getLast();
setNextInPath();
}
}
if (path != null && path.size() == 0) {
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;
if (lastBlockInPath != null) {
robot.posX = lastBlockInPath.getX() + 0.5F;
robot.posY = lastBlockInPath.getY() + 0.5F;
robot.posZ = lastBlockInPath.getZ() + 0.5F;
}
terminate();
}
}
private void setNextInPath() {
if (path != null && path.size() > 0) {
boolean isFirst = prevDistance == Double.MAX_VALUE;
BlockPos next = path.getFirst();
if (isFirst || BuildCraftAPI.isSoftBlock(robot.worldObj, next)) {
setDestination(robot, Utils.convertMiddle(next));
prevDistance = Double.MAX_VALUE;
robot.aimItemAt(next);
} else {
// Path invalid!
path = null;
if (pathSearchJob != null) {
pathSearchJob.terminate();
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;
}
}
}
}
@Override
public void end() {
if (pathSearchJob != null) {
pathSearchJob.terminate();
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;
}
}
@Override
public boolean canLoadFromNBT() {
return true;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
nbt.setTag("finalPos", NBTUtils.writeBlockPos(finalPos));
nbt.setDouble("maxDistance", maxDistance);
if (path != null) {
NBTTagList pathList = new NBTTagList();
for (BlockPos i : path) {
pathList.appendTag(NBTUtils.writeBlockPos(i));
}
nbt.setTag("path", pathList);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
finalPos = NBTUtils.readBlockPos(nbt.getTag("finalPos"));
maxDistance = nbt.getDouble("maxDistance");
if (nbt.hasKey("path")) {
NBTTagList pathList = nbt.getTagList("path", Constants.NBT.TAG_COMPOUND);
path = new LinkedList<BlockPos>();
for (int i = 0; i < pathList.tagCount(); ++i) {
path.add(NBTUtils.readBlockPos(pathList.get(i)));
}
}
loadedFromNBT = true;
}
}