-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotSearchAndGotoBlock.java
117 lines (96 loc) · 2.69 KB
/
AIRobotSearchAndGotoBlock.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
* <p/>
* 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.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.utils.IBlockFilter;
public class AIRobotSearchAndGotoBlock extends AIRobot {
private BlockIndex blockFound;
private IBlockFilter filter;
private boolean random;
private double maxDistanceToEnd;
public AIRobotSearchAndGotoBlock(EntityRobotBase iRobot) {
super(iRobot);
blockFound = null;
random = false;
filter = null;
}
public AIRobotSearchAndGotoBlock(EntityRobotBase iRobot, boolean iRandom,
IBlockFilter iPathFound) {
this(iRobot, iRandom, iPathFound, 0);
}
public AIRobotSearchAndGotoBlock(EntityRobotBase iRobot, boolean iRandom,
IBlockFilter iPathFound, double iMaxDistanceToEnd) {
this(iRobot);
random = iRandom;
filter = iPathFound;
maxDistanceToEnd = iMaxDistanceToEnd;
}
@Override
public void start() {
startDelegateAI(new AIRobotSearchBlock(robot, random, filter, maxDistanceToEnd));
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) {
if (ai.success()) {
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
terminate();
}
} else {
terminate();
}
} else if (ai instanceof AIRobotGotoBlock) {
if (!ai.success()) {
releaseBlockFound();
}
terminate();
}
}
@Override
public boolean success() {
return blockFound != null;
}
private void releaseBlockFound() {
if (blockFound != null) {
robot.getRegistry().release(new ResourceIdBlock(blockFound));
blockFound = null;
}
}
public BlockIndex getBlockFound() {
return blockFound;
}
@Override
public boolean canLoadFromNBT() {
return true;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("indexStored", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("indexStored")) {
blockFound = new BlockIndex(nbt.getCompoundTag("indexStored"));
}
}
}