-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotSearchBlockBase.java
116 lines (94 loc) · 2.59 KB
/
AIRobotSearchBlockBase.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
package buildcraft.robots.ai;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceId;
import buildcraft.core.utils.IBlockFilter;
import buildcraft.core.utils.concurrency.IterableAlgorithmRunner;
import buildcraft.core.utils.concurrency.PathFindingSearch;
import buildcraft.robots.ResourceIdBlock;
public class AIRobotSearchBlockBase extends AIRobot {
public BlockIndex blockFound;
public LinkedList<BlockIndex> path;
private PathFindingSearch blockScanner = null;
private IterableAlgorithmRunner blockScannerJob;
private IBlockFilter pathFound;
private Iterator<BlockIndex> blockIter;
public AIRobotSearchBlockBase(EntityRobotBase iRobot, IBlockFilter iPathFound, Iterator<BlockIndex> iBlockIter) {
super(iRobot);
pathFound = iPathFound;
blockIter = iBlockIter;
blockFound = null;
path = null;
}
@Override
public void start() {
blockScanner = new PathFindingSearch(robot.worldObj, new BlockIndex(
robot), blockIter, pathFound, 96, robot
.getZoneToWork());
blockScannerJob = new IterableAlgorithmRunner(blockScanner, 40000);
blockScannerJob.start();
}
@Override
public void update() {
if (blockScannerJob == null) {
// This is probably due to a load from NBT. Abort the ai in
// that case, since there's no filter to analyze either.
abort();
return;
}
if (blockScannerJob.isDone()) {
path = blockScanner.getResult();
if (path != null && path.size() > 0) {
blockFound = path.removeLast();
} else {
path = null;
}
terminate();
}
}
@Override
public void end() {
if (blockScannerJob != null) {
blockScannerJob.terminate();
}
}
@Override
public boolean success() {
return blockFound != null;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("blockFound", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex(nbt.getCompoundTag("blockFound"));
}
}
public boolean takeResource() {
boolean taken = false;
if (robot.getRegistry().take(new ResourceIdBlock(blockFound), robot)) {
taken = true;
}
unreserve();
return taken;
}
public void unreserve() {
blockScanner.unreserve(blockFound);
}
@Override
public int getEnergyCost() {
return 2;
}
}