-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotFetchItem.java
executable file
·146 lines (120 loc) · 3.64 KB
/
AIRobotFetchItem.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
/**
* 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.core.robots;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.IBox;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.inventory.TransactorSimple;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.core.robots.boards.BoardRobotPicker;
public class AIRobotFetchItem extends AIRobot {
public EntityItem target;
public boolean noItemPicked = false;
private float maxRange;
private IStackFilter stackFilter;
private int pickTime = -1;
private IBox box;
public AIRobotFetchItem(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotFetchItem(EntityRobotBase iRobot, float iMaxRange, IStackFilter iStackFilter, IBox iBox) {
super(iRobot);
maxRange = iMaxRange;
stackFilter = iStackFilter;
box = iBox;
}
@Override
public void preempt(AIRobot ai) {
if (target != null && target.isDead) {
noItemPicked = true;
terminate();
}
}
@Override
public void update() {
if (target == null) {
scanForItem();
} else {
pickTime++;
if (pickTime > 5) {
TransactorSimple inventoryInsert = new TransactorSimple(robot);
target.getEntityItem().stackSize -= inventoryInsert.inject(
target.getEntityItem(), ForgeDirection.UNKNOWN,
true);
if (target.getEntityItem().stackSize <= 0) {
target.setDead();
}
terminate();
}
}
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoBlock) {
if (((AIRobotGotoBlock) ai).unreachable) {
robot.unreachableEntityDetected(target);
noItemPicked = true;
terminate();
}
}
}
@Override
public void end() {
if (target != null) {
BoardRobotPicker.targettedItems.remove(target.getEntityId());
}
}
private void scanForItem() {
double previousDistance = Double.MAX_VALUE;
TransactorSimple inventoryInsert = new TransactorSimple(robot);
for (Object o : robot.worldObj.loadedEntityList) {
Entity e = (Entity) o;
if (!e.isDead && e instanceof EntityItem
&& !BoardRobotPicker.targettedItems.contains(e.getEntityId())
&& !robot.isKnownUnreachable(e)
&& (box == null || box.contains(e.posX, e.posY, e.posZ))) {
double dx = e.posX - robot.posX;
double dy = e.posY - robot.posY;
double dz = e.posZ - robot.posZ;
double sqrDistance = dx * dx + dy * dy + dz * dz;
double maxDistance = maxRange * maxRange;
if (sqrDistance >= maxDistance) {
continue;
} else if (stackFilter != null && !stackFilter.matches(((EntityItem) e).getEntityItem())) {
continue;
} else {
EntityItem item = (EntityItem) e;
if (inventoryInsert.inject(item.getEntityItem(), ForgeDirection.UNKNOWN, false) > 0) {
if (target == null) {
previousDistance = sqrDistance;
target = item;
} else {
if (sqrDistance < previousDistance) {
previousDistance = sqrDistance;
target = item;
}
}
}
}
}
}
if (target != null) {
BoardRobotPicker.targettedItems.add(target.getEntityId());
startDelegateAI(new AIRobotGotoBlock(robot, (int) Math.floor(target.posX),
(int) Math.floor(target.posY), (int) Math.floor(target.posZ)));
} else {
// No item was found, terminate this AI
noItemPicked = true;
terminate();
}
}
}