finished resurection of item / energy consumption, fix #1503

This commit is contained in:
SpaceToad 2014-04-05 11:28:58 +02:00
parent 49ef1c9c29
commit 115a340e80
7 changed files with 159 additions and 172 deletions

View file

@ -49,7 +49,7 @@ public class SchematicBlock extends Schematic implements Comparable<SchematicBl
}
/**
* This is called each time an item matches a reqquirement, that is: (req id
* This is called each time an item matches a requirement, that is: (req id
* == stack id) for damageable items (req id == stack id && req dmg == stack
* dmg) for other items by default, it will increase damage of damageable
* items by the amount of damage of the requirement, and remove the intended
@ -58,16 +58,17 @@ public class SchematicBlock extends Schematic implements Comparable<SchematicBl
* Client may override this behavior for default items. Note that this
* subprogram may be called twice with the same parameters, once with a copy
* of requirements and stack to check if the entire requirements can be
* fullfilled, and once with the real inventory. Implementer is responsible
* fulfilled, and once with the real inventory. Implementer is responsible
* for updating req (with the remaining requirements if any) and stack
* (after usage)
*
* returns: what was used (similer to req, but created from stack, so that
* returns: what was used (similar to req, but created from stack, so that
* any NBT based differences are drawn from the correct source)
*/
@Override
public ItemStack useItem(IBuilderContext context, ItemStack req, ItemStack stack) {
ItemStack result = stack.copy();
if (stack.isItemStackDamageable()) {
if (req.getItemDamage() + stack.getItemDamage() <= stack.getMaxDamage()) {
stack.setItemDamage(req.getItemDamage() + stack.getItemDamage());
@ -96,6 +97,7 @@ public class SchematicBlock extends Schematic implements Comparable<SchematicBl
stack.stackSize = 1;
stack.setItemDamage(0);
}
return result;
}

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.LinkedList;
import net.minecraft.inventory.IInventory;
import buildcraft.api.mj.MjBattery;
import buildcraft.core.LaserData;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.network.NetworkData;
@ -21,6 +22,12 @@ import buildcraft.core.network.RPCSide;
public abstract class TileAbstractBuilder extends TileBuildCraft implements IInventory {
public static double BREAK_ENERGY = 10;
public static double BUILD_ENERGY = 20;
@MjBattery(maxReceivedPerCycle = 100, maxCapacity = 1000, minimumConsumption = 1)
protected double mjStored = 0;
@NetworkData
public LinkedList<LaserData> pathLasers = new LinkedList<LaserData> ();
@ -65,4 +72,13 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements IInv
public abstract boolean isBuildingMaterialSlot(int i);
public final double energyAvailable() {
return mjStored;
}
public final void consumeEnergy(double quantity) {
mjStored -= quantity;
}
}

View file

@ -18,12 +18,12 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.core.Position;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.gates.IAction;
import buildcraft.api.mj.MjBattery;
import buildcraft.core.BlockIndex;
import buildcraft.core.Box;
import buildcraft.core.Box.Kind;
@ -40,11 +40,12 @@ import buildcraft.core.network.NetworkData;
import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCSide;
import buildcraft.core.robots.EntityRobot;
import buildcraft.core.utils.Utils;
public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxProvider {
private static int POWER_ACTIVATION = 50;
private final ItemStack items[] = new ItemStack[28];
private BptBuilderBase bluePrintBuilder;
@ -54,14 +55,9 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
private LinkedList<BlockIndex> path;
private EntityRobot builderRobot;
private LinkedList <ItemStack> requiredToBuild;
private SafeTimeTracker debugBuildTracker = new SafeTimeTracker(5);
@MjBattery (maxReceivedPerCycle = 25)
private double mjStored = 0;
private SafeTimeTracker buildTracker = new SafeTimeTracker(5);
private class PathIterator {
@ -301,11 +297,6 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
bluePrintBuilder = null;
}
if (builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
}
if (box.isInitialized()) {
box.reset();
}
@ -325,10 +316,6 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
currentPathIterator = new PathIterator(start, it);
}
if (bluePrintBuilder != null && builderRobot != null) {
//builderRobot.markEndOfBlueprint(bluePrintBuilder);
}
bluePrintBuilder = currentPathIterator.next();
if (bluePrintBuilder != null) {
@ -337,10 +324,6 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
sendNetworkUpdate();
}
if (builderRobot != null) {
//builderRobot.setBox(box);
}
if (bluePrintBuilder == null) {
currentPathIterator = currentPathIterator.iterate();
}
@ -350,10 +333,6 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
}
} else {
if (bluePrintBuilder != null && bluePrintBuilder.isDone(this)) {
if (builderRobot != null) {
//builderRobot.markEndOfBlueprint(bluePrintBuilder);
}
done = true;
bluePrintBuilder = null;
} else {
@ -497,14 +476,6 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
destroy();
}
@Override
public void destroy() {
if (builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
}
}
@Override
public void openInventory() {
}
@ -526,10 +497,7 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
}
if ((bluePrintBuilder == null || bluePrintBuilder.isDone(this))
&& box.isInitialized()
//&& (builderRobot == null || builderRobot.done())
) {
&& box.isInitialized()) {
box.reset();
sendNetworkUpdate();
@ -537,41 +505,22 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
return;
}
if (!box.isInitialized() && bluePrintBuilder == null && builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
iterateBpt();
if (getWorld().getWorldInfo().getGameType() == GameType.CREATIVE) {
build();
} else {
if (mjStored > POWER_ACTIVATION) {
build();
}
}
iterateBpt();
debugForceBlueprintCompletion();
if (done) {
return;
//}// else if (builderRobot != null && !builderRobot.readyToBuild()) {
// return;
} else if (mjStored < 25) {
return;
}
/* Temp fix to make Builders impotent as the World Destroyers they are
if (bluePrintBuilder != null && !bluePrintBuilder.done) {
if (!box.isInitialized()) {
box.initialize(bluePrintBuilder);
}
if (builderRobot == null) {
builderRobot = new EntityRobot(worldObj, box);
worldObj.spawnEntityInWorld(builderRobot);
}
box.createLasers(worldObj, LaserKind.Stripes);
builderRobot.scheduleContruction(bluePrintBuilder.getNextBlock(worldObj, new SurroundingInventory(worldObj, xCoord, yCoord, zCoord)),
bluePrintBuilder.getContext());
}
*/
mjStored = 0;
}
@Override
@ -661,8 +610,8 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxPr
return renderBox.expand(50).getBoundingBox();
}
public void debugForceBlueprintCompletion () {
if (!debugBuildTracker.markTimeIfDelay(worldObj)) {
public void build () {
if (!buildTracker.markTimeIfDelay(worldObj)) {
return;
}

View file

@ -22,7 +22,6 @@ import buildcraft.api.filler.FillerManager;
import buildcraft.api.filler.IFillerPattern;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.mj.MjBattery;
import buildcraft.builders.filler.pattern.PatternFill;
import buildcraft.builders.triggers.ActionFiller;
import buildcraft.core.Box;
@ -48,15 +47,12 @@ public class TileFiller extends TileAbstractBuilder implements IMachine, IAction
private BptBuilderTemplate currentTemplate;
private BptContext context;
private static int POWER_USAGE = 25;
private static int POWER_ACTIVATION = 50;
private final Box box = new Box();
private boolean done = false;
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private SimpleInventory inv = new SimpleInventory(27, "Filler", 64);
@MjBattery (maxReceivedPerCycle = 25)
private double mjStored = 0;
public TileFiller() {
inv.addListener(this);
box.kind = Kind.STRIPES;
@ -103,9 +99,7 @@ public class TileFiller extends TileAbstractBuilder implements IMachine, IAction
return;
}
if (mjStored > POWER_USAGE) {
mjStored -= POWER_USAGE;
} else {
if (mjStored < POWER_ACTIVATION) {
return;
}

View file

@ -9,9 +9,13 @@
package buildcraft.core.blueprints;
import java.util.ArrayList;
import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.IBuilderContext;
import buildcraft.api.core.IAreaProvider;
import buildcraft.api.core.Position;
import buildcraft.builders.BuildingItem;
@ -118,4 +122,27 @@ public abstract class BptBuilderBase implements IAreaProvider {
public boolean isDone (TileAbstractBuilder builder) {
return done && builder.getBuilders().size() == 0;
}
protected boolean setupForDestroy (TileAbstractBuilder builder, IBuilderContext context, BuildingSlotBlock slot) {
LinkedList<ItemStack> result = new LinkedList<ItemStack>();
int hardness = (int) context
.world()
.getBlock(slot.x, slot.y, slot.z)
.getBlockHardness(context.world(), slot.x, slot.y,
slot.z);
if (builder.energyAvailable() < hardness * TileAbstractBuilder.BREAK_ENERGY) {
return false;
} else {
builder.consumeEnergy(hardness * TileAbstractBuilder.BREAK_ENERGY);
for (int i = 0; i < hardness; ++i) {
slot.addStackConsumed(new ItemStack(
BuildCraftBuilders.buildToolBlock));
}
return true;
}
}
}

View file

@ -31,11 +31,12 @@ import buildcraft.core.utils.BlockUtil;
public class BptBuilderBlueprint extends BptBuilderBase {
private LinkedList<BuildingSlotBlock> clearList = new LinkedList<BuildingSlotBlock>();
private LinkedList<BuildingSlotBlock> blockList = new LinkedList<BuildingSlotBlock>();
private LinkedList<BuildingSlotBlock> buildList = new LinkedList<BuildingSlotBlock>();
private LinkedList<BuildingSlotEntity> entityList = new LinkedList<BuildingSlotEntity>();
private LinkedList<BuildingSlot> postProcessing = new LinkedList<BuildingSlot>();
private BuildingSlotIterator iterator;
public LinkedList <ItemStack> neededItems = new LinkedList <ItemStack> ();
public BptBuilderBlueprint(Blueprint bluePrint, World world, int x, int y, int z) {
@ -63,7 +64,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
b.z = zCoord;
b.mode = Mode.ClearIfInvalid;
clearList.add(b);
buildList.add(b);
}
}
@ -91,12 +92,14 @@ public class BptBuilderBlueprint extends BptBuilderBase {
b.z = zCoord;
b.mode = Mode.Build;
blockList.add (b);
buildList.add (b);
}
}
}
Collections.sort(blockList);
Collections.sort(buildList);
iterator = new BuildingSlotIterator(buildList);
CoordTransformation transform = new CoordTransformation();
@ -118,8 +121,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
private void checkDone() {
recomputeNeededItems();
if (clearList.size() == 0 && blockList.size() == 0
&& entityList.size() == 0) {
if (buildList.size() == 0 && entityList.size() == 0) {
done = true;
} else {
done = false;
@ -128,17 +130,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
@Override
public BuildingSlot getNextBlock(World world, TileAbstractBuilder inv) {
if (clearList.size() != 0) {
BuildingSlot slot = internalGetNextBlock(world, inv, clearList);
checkDone();
if (slot != null) {
return slot;
}
}
if (blockList.size() != 0) {
BuildingSlot slot = internalGetNextBlock(world, inv, blockList);
if (buildList.size() != 0) {
BuildingSlot slot = internalGetNextBlock(world, inv, buildList);
checkDone();
if (slot != null) {
@ -160,20 +153,18 @@ public class BptBuilderBlueprint extends BptBuilderBase {
return null;
}
private BuildingSlot internalGetNextBlock(World world, TileAbstractBuilder inv, LinkedList<BuildingSlotBlock> list) {
LinkedList<BuildingSlotBlock> failSlots = new LinkedList<BuildingSlotBlock>();
private BuildingSlot internalGetNextBlock(World world, TileAbstractBuilder builder, LinkedList<BuildingSlotBlock> list) {
iterator.startIteration();
BuildingSlot result = null;
while (list.size() > 0) {
BuildingSlotBlock slot = list.removeFirst();
postProcessing.add(slot);
while (iterator.hasNext()) {
BuildingSlotBlock slot = iterator.next();
boolean getNext = false;
try {
getNext = !slot.schematic.isValid(context, slot.x, slot.y,
slot.z) && !slot.schematic.ignoreBuilding();
getNext = !slot.schematic.ignoreBuilding()
&& !slot.schematic.isValid(context, slot.x, slot.y,
slot.z);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
@ -184,36 +175,40 @@ public class BptBuilderBlueprint extends BptBuilderBase {
if (getNext) {
if (slot.mode == Mode.ClearIfInvalid) {
if (!BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)) {
result = slot;
break;
if (setupForDestroy(builder, context, slot)) {
iterator.remove();
postProcessing.add(slot);
return slot;
}
}
} else if (world.getWorldInfo().getGameType() == GameType.CREATIVE) {
// In creative, we don't use blocks given in the builder
// In creative, we don't use blocks or energy from the builder
result = slot;
iterator.remove();
postProcessing.add(slot);
return slot;
} else if (checkRequirements(builder, (SchematicBlock) slot.schematic)) {
useRequirements(builder, slot);
break;
} else if (checkRequirements(inv, (SchematicBlock) slot.schematic)) {
useRequirements(inv, (SchematicBlock) slot.schematic);
result = slot;
break;
} else {
failSlots.add(slot);
iterator.remove();
postProcessing.add(slot);
return slot;
}
} else {
iterator.remove();
}
}
list.addAll(failSlots);
return result;
return null;
}
public boolean checkRequirements(TileAbstractBuilder inv, SchematicBlock slot) {
public boolean checkRequirements(TileAbstractBuilder builder, SchematicBlock slot) {
if (slot.block == null) {
return true;
}
double energyRequired = 0;
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
LinkedList<ItemStack> tmpInv = new LinkedList<ItemStack>();
@ -221,6 +216,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
}
}
} catch (Throwable t) {
@ -229,27 +225,25 @@ public class BptBuilderBlueprint extends BptBuilderBase {
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
}
int size = inv.getSizeInventory();
if (builder.energyAvailable() < energyRequired) {
return false;
}
int size = builder.getSizeInventory();
for (int i = 0; i < size; ++i) {
if (!inv.isBuildingMaterialSlot(i)) {
if (!builder.isBuildingMaterialSlot(i)) {
continue;
}
if (inv.getStackInSlot(i) != null) {
tmpInv.add(inv.getStackInSlot(i).copy());
if (builder.getStackInSlot(i) != null) {
tmpInv.add(builder.getStackInSlot(i).copy());
}
}
/*for (ItemStack reqStk : tmpReq) {
for (ItemStack reqStk : tmpReq) {
for (ItemStack invStk : tmpInv) {
if (invStk != null && reqStk.itemID == invStk.itemID && invStk.stackSize > 0) {
if (!invStk.isItemStackDamageable() && (reqStk.getItemDamage() != invStk.getItemDamage())) {
continue; // it doesn't match, try again
}
if (invStk != null && invStk.stackSize > 0 && isEqual (reqStk, invStk)) {
try {
slot.useItem(context, reqStk, invStk);
} catch (Throwable t) {
// Defensive code against errors in implementers
@ -263,24 +257,24 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
}
if (reqStk.stackSize != 0)
if (reqStk.stackSize != 0) {
return false;
}*/
}
}
return true;
}
public void useRequirements(TileAbstractBuilder inv, SchematicBlock slot) {
if (slot.block == null) {
return;
}
public void useRequirements(TileAbstractBuilder builder, BuildingSlotBlock slot) {
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
double energyRequired = 0;
try {
for (ItemStack stk : slot.getRequirements(context)) {
if (stk != null) {
tmpReq.add(stk.copy());
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
}
}
} catch (Throwable t) {
@ -290,28 +284,26 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
builder.consumeEnergy(energyRequired);
ListIterator<ItemStack> itr = tmpReq.listIterator();
while (itr.hasNext()) {
ItemStack reqStk = itr.next();
boolean smallStack = reqStk.stackSize == 1;
ItemStack usedStack = reqStk;
int size = inv.getSizeInventory();
int size = builder.getSizeInventory();
for (int i = 0; i < size; ++i) {
if (!inv.isBuildingMaterialSlot(i)) {
if (!builder.isBuildingMaterialSlot(i)) {
}
ItemStack invStk = inv.getStackInSlot(i);
/*if (invStk != null && reqStk.itemID == invStk.itemID && invStk.stackSize > 0) {
if (!invStk.isItemStackDamageable() && (reqStk.getItemDamage() != invStk.getItemDamage())) {
continue;
}
ItemStack invStk = builder.getStackInSlot(i);
if (invStk != null && invStk.stackSize > 0 && isEqual (reqStk, invStk)) {
try {
usedStack = slot.useItem(context, reqStk, invStk);
usedStack = slot.getSchematic().useItem(context, reqStk, invStk);
slot.addStackConsumed (usedStack);
} catch (Throwable t) {
// Defensive code against errors in implementers
t.printStackTrace();
@ -319,15 +311,15 @@ public class BptBuilderBlueprint extends BptBuilderBase {
}
if (invStk.stackSize == 0) {
inv.setInventorySlotContents(i, null);
builder.setInventorySlotContents(i, null);
} else {
inv.setInventorySlotContents(i, invStk);
builder.setInventorySlotContents(i, invStk);
}
if (reqStk.stackSize == 0) {
break;
}
}*/
}
}
if (reqStk.stackSize != 0) {
@ -341,12 +333,29 @@ public class BptBuilderBlueprint extends BptBuilderBase {
return;
}
private boolean isEqual(ItemStack reqStk, ItemStack invStk) {
if (reqStk.getItem() != invStk.getItem()) {
return false;
} else if (!invStk.isItemStackDamageable() && (reqStk.getItemDamage() != invStk.getItemDamage())) {
return false;
} else if (reqStk.stackTagCompound != null && invStk.stackTagCompound == null) {
return false;
} else if (reqStk.stackTagCompound == null && invStk.stackTagCompound != null) {
return false;
} else if (reqStk.stackTagCompound != null && !reqStk.stackTagCompound.equals(invStk.stackTagCompound)) {
return false;
} else {
return true;
}
}
public void recomputeNeededItems() {
neededItems.clear();
HashMap <StackKey, Integer> computeStacks = new HashMap <StackKey, Integer> ();
for (BuildingSlot slot : blockList) {
for (BuildingSlot slot : buildList) {
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
try {

View file

@ -13,7 +13,6 @@ import java.util.LinkedList;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.Schematic;
import buildcraft.builders.TileAbstractBuilder;
import buildcraft.core.blueprints.BuildingSlotBlock.Mode;
@ -53,8 +52,6 @@ public class BptBuilderTemplate extends BptBuilderBase {
}
}
buildList.add(null);
for (int j = 0; j < bluePrint.sizeY; ++j) {
for (int i = 0; i < bluePrint.sizeX; ++i) {
for (int k = 0; k < bluePrint.sizeZ; ++k) {
@ -107,12 +104,12 @@ public class BptBuilderTemplate extends BptBuilderBase {
return null;
}
public BuildingSlotBlock internalGetNextBlock(World world, TileAbstractBuilder inv, LinkedList<BuildingSlotBlock> list) {
public BuildingSlotBlock internalGetNextBlock(World world, TileAbstractBuilder builder, LinkedList<BuildingSlotBlock> list) {
BuildingSlotBlock result = null;
IInvSlot firstSlotToConsume = null;
for (IInvSlot invSlot : InventoryIterator.getIterable(inv, ForgeDirection.UNKNOWN)) {
for (IInvSlot invSlot : InventoryIterator.getIterable(builder, ForgeDirection.UNKNOWN)) {
ItemStack stack = invSlot.getStackInSlot();
if (stack != null && stack.stackSize > 0) {
@ -132,27 +129,20 @@ public class BptBuilderTemplate extends BptBuilderBase {
if (BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)) {
iterator.remove();
} else {
int hardness = (int) context
.world()
.getBlock(slot.x, slot.y, slot.z)
.getBlockHardness(context.world(), slot.x, slot.y,
slot.z) + 1;
if (setupForDestroy(builder, context, slot)) {
result = slot;
iterator.remove();
for (int i = 0; i < hardness; ++i) {
slot.addStackConsumed(new ItemStack(
BuildCraftBuilders.buildToolBlock));
break;
}
result = slot;
iterator.remove();
break;
}
} else if (slot.mode == Mode.Build) {
if (!BlockUtil.isSoftBlock(world, slot.x, slot.y, slot.z)) {
iterator.remove();
} else {
if (firstSlotToConsume != null) {
if (builder.energyAvailable() > TileAbstractBuilder.BUILD_ENERGY && firstSlotToConsume != null) {
builder.consumeEnergy(TileAbstractBuilder.BUILD_ENERGY);
slot.addStackConsumed(firstSlotToConsume
.decreaseStackInSlot());
result = slot;