-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotCraftAssemblyTable.java
executable file
·270 lines (217 loc) · 7.15 KB
/
AIRobotCraftAssemblyTable.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright (c) 2011-2014, 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.robots.ai;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.recipes.CraftingResult;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.inventory.InventoryCopy;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.inventory.filters.ArrayStackFilter;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.robots.DockingStation;
import buildcraft.robots.IStationFilter;
import buildcraft.robots.ResourceIdAssemblyTable;
import buildcraft.robots.ResourceIdBlock;
import buildcraft.robots.RobotRegistry;
import buildcraft.silicon.BlockLaserTable;
import buildcraft.silicon.TileAssemblyTable;
import buildcraft.robots.statements.ActionRobotFilter;
import buildcraft.robots.statements.ActionStationAllowCraft;
public class AIRobotCraftAssemblyTable extends AIRobotCraftGeneric {
private CraftingResult<ItemStack> expectedResult;
private DockingStation stationFound;
private TileAssemblyTable table;
private boolean craftStarted = false;
private ArrayList<ArrayStackFilter> requirements;
private int waitedTime = 0;
public AIRobotCraftAssemblyTable(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotCraftAssemblyTable(EntityRobotBase iRobot, CraftingResult craftingResult) {
super(iRobot);
expectedResult = craftingResult;
}
@Override
public void start() {
requirements = tryCraft();
startDelegateAI(new AIRobotSearchStation(robot, new StationAssemblyTableFilter(), robot.getZoneToWork()));
}
@Override
public void update() {
if (table != null) {
if (!craftStarted) {
if (requirements.size() != 0) {
startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ReqStackFilter(), robot.getZoneToWork()));
return;
}
if (robot.getDockingStation() != stationFound) {
startDelegateAI(new AIRobotGotoStation(robot, stationFound));
return;
}
ITransactor trans = Transactor.getTransactorFor(table);
for (IInvSlot s : InventoryIterator.getIterable(robot)) {
if (s.getStackInSlot() != null) {
ItemStack added = trans.add(s.getStackInSlot(), ForgeDirection.UNKNOWN, true);
if (added.stackSize == 0) {
terminate();
} else if (added.stackSize == s.getStackInSlot().stackSize) {
s.setStackInSlot(null);
} else {
s.getStackInSlot().stackSize -= added.stackSize;
}
}
}
RobotRegistry.getRegistry(robot.worldObj).take(new ResourceIdAssemblyTable(table), robot);
table.planOutput(expectedResult.recipe);
// TODO: How to make sure this output is not crafted more than
// once??
craftStarted = true;
} else {
waitedTime++;
if (InvUtils.getItem(robot, new ArrayStackFilter(expectedResult.crafted)) != null) {
crafted = true;
terminate();
} else if (waitedTime > 120 * 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;
table = getUsableAssemblyTable(new BlockIndex(stationFound.x(), stationFound.y(), stationFound.z()));
if (table == null) {
terminate();
return;
}
BlockIndex index = new BlockIndex(table);
if (!robot.getRegistry().take(new ResourceIdBlock(index), robot)) {
terminate();
}
if (!stationFound.take(robot)) {
terminate();
}
}
} else if (ai instanceof AIRobotGotoStationAndLoad) {
if (!ai.success()) {
terminate();
} else {
requirements = tryCraft();
}
}
}
@Override
public ItemStack receiveItem(ItemStack stack) {
if (StackHelper.isMatchingItem(stack, expectedResult.crafted)) {
ITransactor robotTransactor = Transactor.getTransactorFor(robot);
ItemStack added = robotTransactor.add(stack, ForgeDirection.UNKNOWN, true);
stack.stackSize -= added.stackSize;
return stack;
} else {
return stack;
}
}
protected ArrayList<ArrayStackFilter> tryCraft() {
Object[] items = expectedResult.usedItems.toArray();
ArrayList<ArrayStackFilter> result = new ArrayList<ArrayStackFilter>();
IInventory inv = new InventoryCopy(robot);
for (Object tmp : items) {
if (tmp == null) {
continue;
}
int qty = 0;
ArrayStackFilter filter;
if (tmp instanceof ItemStack) {
ItemStack stack = (ItemStack) tmp;
qty = stack.stackSize;
filter = new ArrayStackFilter(stack);
} else {
ArrayList<ItemStack> stacks = (ArrayList<ItemStack>) tmp;
qty = stacks.get(0).stackSize;
filter = new ArrayStackFilter(stacks.toArray(new ItemStack[stacks.size()]));
}
for (IInvSlot s : InventoryIterator.getIterable(inv)) {
if (filter.matches(s.getStackInSlot())) {
ItemStack removed = s.decreaseStackInSlot(qty);
qty = qty - removed.stackSize;
if (removed.stackSize == 0) {
break;
}
}
}
if (qty > 0) {
result.add(filter);
}
}
return result;
}
private class StationAssemblyTableFilter implements IStationFilter {
@Override
public boolean matches(DockingStation station) {
if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackFilter(expectedResult.crafted),
ActionStationAllowCraft.class)) {
return false;
}
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if (getUsableAssemblyTable(new BlockIndex(station.x(), station.y(), station.z())) != null) {
return true;
}
}
return false;
}
}
private TileAssemblyTable getUsableAssemblyTable(BlockIndex b) {
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);
int nearbyMeta = robot.worldObj.getBlockMetadata(index.x, index.y, index.z);
if (nearbyBlock instanceof BlockLaserTable && nearbyMeta == 0) {
TileAssemblyTable f = (TileAssemblyTable) robot.worldObj.getTileEntity(index.x, index.y, index.z);
// TODO: check if assembly table has some empty slots
return f;
}
}
return null;
}
private class ReqStackFilter implements IStackFilter {
@Override
public boolean matches(ItemStack stack) {
for (ArrayStackFilter s : requirements) {
if (s.matches(stack)) {
return true;
}
}
return false;
}
}
}