-
Notifications
You must be signed in to change notification settings - Fork 497
/
AIRobotBreak.java
160 lines (130 loc) · 4.5 KB
/
AIRobotBreak.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
/**
* 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.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import buildcraft.api.blueprints.BuilderAPI;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.utils.BlockUtils;
import buildcraft.core.proxy.CoreProxy;
public class AIRobotBreak extends AIRobot {
private BlockIndex blockToBreak;
private float blockDamage = 0;
private Block block;
private int meta;
private float hardness;
private float speed;
public AIRobotBreak(EntityRobotBase iRobot) {
super(iRobot);
}
public AIRobotBreak(EntityRobotBase iRobot, BlockIndex iBlockToBreak) {
this(iRobot);
blockToBreak = iBlockToBreak;
}
@Override
public void start() {
robot.aimItemAt(blockToBreak.x, blockToBreak.y, blockToBreak.z);
robot.setItemActive(true);
block = robot.worldObj.getBlock(blockToBreak.x, blockToBreak.y, blockToBreak.z);
meta = robot.worldObj.getBlockMetadata(blockToBreak.x, blockToBreak.y, blockToBreak.z);
hardness = block.getBlockHardness(robot.worldObj, blockToBreak.x, blockToBreak.y, blockToBreak.z);
speed = getBreakSpeed(robot, robot.getHeldItem(), block, meta);
}
@Override
public void update() {
if (block == null || block.isAir(robot.worldObj, blockToBreak.x, blockToBreak.y, blockToBreak.z)) {
terminate();
}
if (hardness != 0) {
blockDamage += speed / hardness / 30F;
} else {
// Instantly break the block
blockDamage = 1.1F;
}
if (blockDamage > 1.0F) {
robot.worldObj.destroyBlockInWorldPartially(robot.getEntityId(), blockToBreak.x,
blockToBreak.y, blockToBreak.z, -1);
blockDamage = 0;
if (robot.getHeldItem() != null) {
robot.getHeldItem().getItem()
.onBlockStartBreak(robot.getHeldItem(), blockToBreak.x, blockToBreak.y, blockToBreak.z,
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) robot.worldObj).get());
}
if (BlockUtils.breakBlock((WorldServer) robot.worldObj, blockToBreak.x, blockToBreak.y, blockToBreak.z, 6000)) {
robot.worldObj.playAuxSFXAtEntity(null, 2001,
blockToBreak.x, blockToBreak.y, blockToBreak.z,
Block.getIdFromBlock(block) + (meta << 12));
if (robot.getHeldItem() != null) {
robot.getHeldItem().getItem()
.onBlockDestroyed(robot.getHeldItem(), robot.worldObj, block, blockToBreak.x,
blockToBreak.y, blockToBreak.z, robot);
if (robot.getHeldItem().getItemDamage() >= robot.getHeldItem().getMaxDamage()) {
robot.setItemInUse(null);
}
}
} else {
setSuccess(false);
}
terminate();
} else {
robot.worldObj.destroyBlockInWorldPartially(robot.getEntityId(), blockToBreak.x,
blockToBreak.y, blockToBreak.z, (int) (blockDamage * 10.0F) - 1);
}
}
@Override
public void end() {
robot.setItemActive(false);
robot.worldObj.destroyBlockInWorldPartially(robot.getEntityId(), blockToBreak.x,
blockToBreak.y, blockToBreak.z, -1);
}
private float getBreakSpeed(EntityRobotBase robot, ItemStack usingItem, Block block, int meta) {
ItemStack stack = usingItem;
float f = stack == null ? 1.0F : stack.getItem().getDigSpeed(stack, block, meta);
if (f > 1.0F) {
int i = EnchantmentHelper.getEfficiencyModifier(robot);
ItemStack itemstack = usingItem;
if (i > 0 && itemstack != null) {
float f1 = i * i + 1;
boolean canHarvest = ForgeHooks.canToolHarvestBlock(block, meta, itemstack);
if (!canHarvest && f <= 1.0F) {
f += f1 * 0.08F;
} else {
f += f1;
}
}
}
return f;
}
@Override
public int getEnergyCost() {
return (int) Math.ceil((float) BuilderAPI.BREAK_ENERGY * 2 / 30.0F);
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockToBreak != null) {
NBTTagCompound sub = new NBTTagCompound();
blockToBreak.writeTo(sub);
nbt.setTag("blockToBreak", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockToBreak")) {
blockToBreak = new BlockIndex(nbt.getCompoundTag("blockToBreak"));
}
}
}