-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotGotoBlock.java
executable file
·206 lines (167 loc) · 4.92 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* 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 java.util.LinkedList;
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.BuildCraftAPI;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.utils.IterableAlgorithmRunner;
import buildcraft.core.lib.utils.PathFinding;
public class AIRobotGotoBlock extends AIRobotGoto {
private PathFinding pathSearch;
private IterableAlgorithmRunner pathSearchJob;
private LinkedList<BlockIndex> path;
private double prevDistance = Double.MAX_VALUE;
private float finalX, finalY, finalZ;
private double maxDistance = 0;
private BlockIndex lastBlockInPath;
private boolean loadedFromNBT;
public AIRobotGotoBlock(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotGotoBlock(EntityRobotBase robot, int x, int y, int z) {
this(robot);
finalX = x;
finalY = y;
finalZ = z;
}
public AIRobotGotoBlock(EntityRobotBase robot, int x, int y, int z, double iMaxDistance) {
this(robot, x, y, z);
maxDistance = iMaxDistance;
}
public AIRobotGotoBlock(EntityRobotBase robot, LinkedList<BlockIndex> iPath) {
this(robot);
path = iPath;
finalX = path.getLast().x;
finalY = path.getLast().y;
finalZ = path.getLast().z;
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 BlockIndex((int) Math.floor(robot.posX),
(int) Math.floor(robot.posY), (int) Math.floor(robot.posZ)), new BlockIndex(
(int) Math.floor(finalX), (int) Math.floor(finalY), (int) Math.floor(finalZ)), maxDistance, 96);
pathSearchJob = new IterableAlgorithmRunner(pathSearch, 50);
pathSearchJob.start();
} else if (path != null) {
double distance = robot.getDistance(nextX, nextY, nextZ);
if (!robot.isMoving() || distance > prevDistance) {
if (path.size() > 0) {
path.removeFirst();
}
setNextInPath();
} else {
prevDistance = robot.getDistance(nextX, nextY, nextZ);
}
} 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.x + 0.5F;
robot.posY = lastBlockInPath.y + 0.5F;
robot.posZ = lastBlockInPath.z + 0.5F;
}
terminate();
}
}
private void setNextInPath() {
if (path.size() > 0) {
boolean isFirst = prevDistance == Double.MAX_VALUE;
BlockIndex next = path.getFirst();
prevDistance = Double.MAX_VALUE;
if (isFirst || BuildCraftAPI.isSoftBlock(robot.worldObj, next.x, next.y, next.z)) {
setDestination(robot, next.x + 0.5F, next.y + 0.5F, next.z + 0.5F);
robot.aimItemAt(next.x, next.y, next.z);
} 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.setFloat("finalX", finalX);
nbt.setFloat("finalY", finalY);
nbt.setFloat("finalZ", finalZ);
nbt.setDouble("maxDistance", maxDistance);
if (path != null) {
NBTTagList pathList = new NBTTagList();
for (BlockIndex i : path) {
NBTTagCompound subNBT = new NBTTagCompound();
i.writeTo(subNBT);
pathList.appendTag(subNBT);
}
nbt.setTag("path", pathList);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
finalX = nbt.getFloat("finalX");
finalY = nbt.getFloat("finalY");
finalZ = nbt.getFloat("finalZ");
maxDistance = nbt.getDouble("maxDistance");
if (nbt.hasKey("path")) {
NBTTagList pathList = nbt.getTagList("path", Constants.NBT.TAG_COMPOUND);
path = new LinkedList<BlockIndex>();
for (int i = 0; i < pathList.tagCount(); ++i) {
path.add(new BlockIndex(pathList.getCompoundTagAt(i)));
}
}
loadedFromNBT = true;
}
}