-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotCraftFurnace.java
executable file
·220 lines (174 loc) · 6.11 KB
/
AIRobotCraftFurnace.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* 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.robotics.ai;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFurnace;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.inventory.ITransactor;
import buildcraft.core.lib.inventory.InvUtils;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.lib.inventory.Transactor;
import buildcraft.core.lib.inventory.filters.ArrayStackFilter;
import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.robotics.DockingStation;
import buildcraft.robotics.IStationFilter;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.robotics.statements.ActionRobotFilter;
import buildcraft.robotics.statements.ActionStationAllowCraft;
public class AIRobotCraftFurnace extends AIRobotCraftGeneric {
private static final int INPUT_SLOT = 0;
private static final int FUEL_SLOT = 1;
private static final int OUTPUT_SLOT = 2;
private ItemStack input;
private DockingStation stationFound;
private TileEntityFurnace furnace;
private boolean craftStarted = false;
private int waitedTime = 0;
private ItemStack expectedOutput;
public AIRobotCraftFurnace(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotCraftFurnace(EntityRobotBase iRobot, ItemStack iInput, ItemStack iOutput) {
super(iRobot);
input = iInput;
expectedOutput = iOutput;
}
@Override
public void start() {
startDelegateAI(new AIRobotSearchStation(robot, new StationFurnaceFilter(), robot.getZoneToWork()));
}
@Override
public void update() {
if (furnace != null) {
if (!craftStarted) {
if (furnace.getStackInSlot(FUEL_SLOT) == null && InvUtils.getItem(robot, new FuelFilter()) == null) {
startDelegateAI(new AIRobotGotoStationAndLoad(robot, new FuelFilter(), robot.getZoneToWork()));
return;
}
if (InvUtils.getItem(robot, new ArrayStackFilter(input)) == null) {
startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ArrayStackFilter(input),
robot.getZoneToWork()));
return;
}
if (robot.getDockingStation() != stationFound) {
startDelegateAI(new AIRobotGotoStation(robot, stationFound));
return;
}
if (furnace.getStackInSlot(FUEL_SLOT) == null) {
IInvSlot s = InvUtils.getItem(robot, new FuelFilter());
furnace.setInventorySlotContents(FUEL_SLOT, s.decreaseStackInSlot(1));
}
if (furnace.getStackInSlot(INPUT_SLOT) == null) {
IInvSlot s = InvUtils.getItem(robot, new ArrayStackFilter(input));
furnace.setInventorySlotContents(INPUT_SLOT, s.decreaseStackInSlot(1));
}
craftStarted = true;
if (!robot.hasFreeSlot()) {
startDelegateAI(new AIRobotGotoStationAndUnload(robot));
}
} else {
waitedTime++;
if (waitedTime > 40 && furnace.getStackInSlot(OUTPUT_SLOT) != null) {
if (robot.hasFreeSlot()) {
ItemStack stack = furnace.decrStackSize(OUTPUT_SLOT, 1);
if (stack != null) {
ITransactor transactor = Transactor.getTransactorFor(robot);
transactor.add(stack, ForgeDirection.UNKNOWN, true);
crafted = true;
}
}
terminate();
} else if (waitedTime > 20 * 60) {
terminate();
}
}
} else {
terminate();
}
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchStation) {
if (!ai.success()) {
crafted = false;
terminate();
} else {
stationFound = ((AIRobotSearchStation) ai).targetStation;
furnace = getUsableFurnace(new BlockIndex(stationFound.x(), stationFound.y(), stationFound.z()));
if (furnace == null) {
terminate();
return;
}
BlockIndex index = new BlockIndex(furnace);
if (!robot.getRegistry().take(new ResourceIdBlock(index), robot)) {
terminate();
}
if (!stationFound.take(robot)) {
terminate();
}
}
} else if (ai instanceof AIRobotGotoStationAndLoad) {
}
}
// How to operate furnaces
// [1] identify a furnace
// [2] verify that proper item is in. If empty, and slot out empty or
// contains order get proper item, otherwise skip
// [3] bring proper item and put in
// [4] as soon as output contains expected item, get it and place it
// somewhere
private class StationFurnaceFilter implements IStationFilter {
@Override
public boolean matches(DockingStation station) {
if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackFilter(expectedOutput),
ActionStationAllowCraft.class)) {
return false;
}
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if (getUsableFurnace(new BlockIndex(station.x(), station.y(), station.z())) != null) {
return true;
}
}
return false;
}
}
private class FuelFilter implements IStackFilter {
@Override
public boolean matches(ItemStack stack) {
return TileEntityFurnace.getItemBurnTime(stack) > 0 && !StackHelper.isMatchingItem(stack, input);
}
}
private TileEntityFurnace getUsableFurnace(BlockIndex b) {
// reserve that furnace if found from the block reserve system
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
BlockIndex index = new BlockIndex (b.x + dir.offsetX, b.y
+ dir.offsetY, b.z
+ dir.offsetZ);
if (robot.getRegistry().isTaken(new ResourceIdBlock(index))) {
continue;
}
Block nearbyBlock = robot.worldObj.getBlock(index.x, index.y, index.z);
if (nearbyBlock instanceof BlockFurnace) {
TileEntityFurnace f = (TileEntityFurnace) robot.worldObj.getTileEntity(index.x, index.y, index.z);
if (f.getStackInSlot(INPUT_SLOT) != null
&& !StackHelper.isMatchingItem(input, f.getStackInSlot(INPUT_SLOT))) {
continue;
}
return f;
}
}
return null;
}
}