From 115a340e80eebdb0cea2ddb2ffdaca15b48c59ae Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Sat, 5 Apr 2014 11:28:58 +0200 Subject: [PATCH] finished resurection of item / energy consumption, fix #1503 --- .../api/blueprints/SchematicBlock.java | 8 +- .../builders/TileAbstractBuilder.java | 16 ++ common/buildcraft/builders/TileBuilder.java | 81 ++------- common/buildcraft/builders/TileFiller.java | 10 +- .../core/blueprints/BptBuilderBase.java | 27 +++ .../core/blueprints/BptBuilderBlueprint.java | 161 +++++++++--------- .../core/blueprints/BptBuilderTemplate.java | 28 +-- 7 files changed, 159 insertions(+), 172 deletions(-) diff --git a/common/buildcraft/api/blueprints/SchematicBlock.java b/common/buildcraft/api/blueprints/SchematicBlock.java index f05a4007..419944f9 100755 --- a/common/buildcraft/api/blueprints/SchematicBlock.java +++ b/common/buildcraft/api/blueprints/SchematicBlock.java @@ -49,7 +49,7 @@ public class SchematicBlock extends Schematic implements Comparable pathLasers = new LinkedList (); @@ -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; + } + + } diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index edb77739..3465375d 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -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 path; - private EntityRobot builderRobot; - private LinkedList 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; } diff --git a/common/buildcraft/builders/TileFiller.java b/common/buildcraft/builders/TileFiller.java index 399dd83f..a0a4637b 100644 --- a/common/buildcraft/builders/TileFiller.java +++ b/common/buildcraft/builders/TileFiller.java @@ -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; } diff --git a/common/buildcraft/core/blueprints/BptBuilderBase.java b/common/buildcraft/core/blueprints/BptBuilderBase.java index 35461b6f..204eeb0b 100644 --- a/common/buildcraft/core/blueprints/BptBuilderBase.java +++ b/common/buildcraft/core/blueprints/BptBuilderBase.java @@ -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 result = new LinkedList(); + + 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; + } + } } diff --git a/common/buildcraft/core/blueprints/BptBuilderBlueprint.java b/common/buildcraft/core/blueprints/BptBuilderBlueprint.java index 3f2073ec..34d586b9 100644 --- a/common/buildcraft/core/blueprints/BptBuilderBlueprint.java +++ b/common/buildcraft/core/blueprints/BptBuilderBlueprint.java @@ -31,11 +31,12 @@ import buildcraft.core.utils.BlockUtil; public class BptBuilderBlueprint extends BptBuilderBase { - private LinkedList clearList = new LinkedList(); - private LinkedList blockList = new LinkedList(); + private LinkedList buildList = new LinkedList(); private LinkedList entityList = new LinkedList(); private LinkedList postProcessing = new LinkedList(); + private BuildingSlotIterator iterator; + public LinkedList neededItems = new LinkedList (); 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 list) { - LinkedList failSlots = new LinkedList(); + private BuildingSlot internalGetNextBlock(World world, TileAbstractBuilder builder, LinkedList 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 tmpReq = new LinkedList(); LinkedList tmpInv = new LinkedList(); @@ -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 tmpReq = new LinkedList(); + 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 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 computeStacks = new HashMap (); - for (BuildingSlot slot : blockList) { + for (BuildingSlot slot : buildList) { LinkedList stacks = new LinkedList(); try { diff --git a/common/buildcraft/core/blueprints/BptBuilderTemplate.java b/common/buildcraft/core/blueprints/BptBuilderTemplate.java index aca612f7..c25d4860 100644 --- a/common/buildcraft/core/blueprints/BptBuilderTemplate.java +++ b/common/buildcraft/core/blueprints/BptBuilderTemplate.java @@ -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 list) { + public BuildingSlotBlock internalGetNextBlock(World world, TileAbstractBuilder builder, LinkedList 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;