-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotSearchBlock.java
128 lines (108 loc) · 3.07 KB
/
AIRobotSearchBlock.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
package buildcraft.robotics.ai;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IZone;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.utils.BlockScannerExpanding;
import buildcraft.core.lib.utils.BlockScannerRandom;
import buildcraft.core.lib.utils.BlockScannerZoneRandom;
import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.core.lib.utils.IterableAlgorithmRunner;
import buildcraft.core.lib.utils.PathFindingSearch;
public class AIRobotSearchBlock extends AIRobot {
public BlockIndex blockFound;
public LinkedList<BlockIndex> path;
private PathFindingSearch blockScanner = null;
private IterableAlgorithmRunner blockScannerJob;
private IBlockFilter pathFound;
private Iterator<BlockIndex> blockIter;
private IZone zone;
public AIRobotSearchBlock(EntityRobotBase iRobot, boolean random, IBlockFilter iPathFound) {
super(iRobot);
pathFound = iPathFound;
zone = iRobot.getZoneToWork();
if (!random) {
blockIter = new BlockScannerExpanding().iterator();
} else {
if (zone != null) {
BlockIndex pos = new BlockIndex(iRobot);
blockIter = new BlockScannerZoneRandom(pos.x, pos.y, pos.z, iRobot.worldObj.rand, zone)
.iterator();
} else {
blockIter = new BlockScannerRandom(iRobot.worldObj.rand, 64).iterator();
}
}
blockFound = null;
path = null;
}
@Override
public void start() {
blockScanner = new PathFindingSearch(robot.worldObj, new BlockIndex(
robot), blockIter, pathFound, 96, zone);
blockScannerJob = new IterableAlgorithmRunner(blockScanner);
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;
}
}