DS Axe now uses the same algorithm as the farm station for cutting down trees (including removing leaves)

This commit is contained in:
CrazyPants committed 2014-09-17 18:58:08 +08:00
1 parent 96679f3099
commit 59fd28f0b7
6 files changed
+233 -110

No files matched your search

@@ -175,7 +175,7 @@ public final class Config {
public static int darkSteelAxePowerUsePerDamagePoint = 750;
public static int darkSteelAxePowerUsePerDamagePointMultiHarvest = 1500;
public static float darkSteelAxeEffeciencyBoostWhenPowered = 2;
public static float darkSteelAxeSpeedPenaltyMultiHarvest = 8;
public static float darkSteelAxeSpeedPenaltyMultiHarvest = 4;
public static int darkSteelUpgradeVibrantCost = 10;
public static int darkSteelUpgradePowerOneCost = 10;
@@ -9,10 +9,13 @@ import java.util.List;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.block.BlockNewLeaf;
import net.minecraft.block.BlockOldLeaf;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
@@ -33,6 +36,8 @@ import crazypants.enderio.EnderIO;
import crazypants.enderio.EnderIOTab;
import crazypants.enderio.config.Config;
import crazypants.enderio.gui.IAdvancedTooltipProvider;
import crazypants.enderio.machine.farm.farmers.HarvestResult;
import crazypants.enderio.machine.farm.farmers.TreeHarvestUtil;
import crazypants.util.BlockCoord;
import crazypants.util.ItemUtil;
import crazypants.util.Lang;
@@ -109,71 +114,52 @@ public class ItemDarkSteelAxe extends ItemAxe implements IEnergyContainerItem, I
}
@SubscribeEvent
public void onBreakEvent(BlockEvent.BreakEvent evt) {
public void onBreakEvent(BlockEvent.BreakEvent evt) {
if(evt.getPlayer().isSneaking() && isEquipped(evt.getPlayer()) && isLog(evt.block, evt.blockMetadata)) {
int powerStored = getStoredPower(evt.getPlayer());
int maxBlocks = 50;
Set<BlockCoord> toBreak = new HashSet<BlockCoord>();
TreeHarvestUtil harvester = new TreeHarvestUtil();
HarvestResult res = new HarvestResult();
BlockCoord bc = new BlockCoord(evt.x, evt.y, evt.z);
getConnectedLogs(evt.world, bc, evt.block, evt.blockMetadata, toBreak, maxBlocks);
toBreak.remove(bc); //handled automatically
harvester.harvest(evt.getPlayer().worldObj, bc, res);
List<BlockCoord> sortedTargets = new ArrayList<BlockCoord>(toBreak);
List<BlockCoord> sortedTargets = new ArrayList<BlockCoord>(res.getHarvestedBlocks());
harvestComparator.refPoint = bc;
Collections.sort(sortedTargets, harvestComparator);
maxBlocks = powerStored / Config.darkSteelAxePowerUsePerDamagePointMultiHarvest;
for(int i=0;i<maxBlocks && i < sortedTargets.size();i++) {
doMultiHarvest(evt.getPlayer(), evt.getPlayer().worldObj, sortedTargets.get(i), evt.block, evt.blockMetadata % 4);
int maxBlocks = powerStored / Config.darkSteelAxePowerUsePerDamagePointMultiHarvest;
int numUsedPower = 0;
for(int i=0;numUsedPower<maxBlocks && i < sortedTargets.size();i++) {
if(doMultiHarvest(evt.getPlayer(), evt.getPlayer().worldObj, sortedTargets.get(i), evt.block, evt.blockMetadata % 4)) {
numUsedPower++;
}
}
}
}
private void doMultiHarvest(EntityPlayer player, World worldObj, BlockCoord bc, Block block, int meta) {
private boolean doMultiHarvest(EntityPlayer player, World worldObj, BlockCoord bc, Block refBlock, int refMeta) {
Block block = worldObj.getBlock(bc.x, bc.y, bc.z);
int meta = worldObj.getBlockMetadata(bc.x, bc.y, bc.z);
ArrayList<ItemStack> itemDrops = block.getDrops(worldObj, bc.x, bc.y, bc.z, meta, 0);
worldObj.setBlockToAir(bc.x, bc.y, bc.z);
boolean removed = false;
boolean usedPower = false;
if(itemDrops != null) {
for (ItemStack stack : itemDrops) {
worldObj.spawnEntityInWorld(new EntityItem(worldObj, bc.x + 0.5, bc.y + 0.5, bc.z + 0.5, stack.copy()));
applyDamage(player, player.getCurrentEquippedItem(), 1, true);
worldObj.spawnEntityInWorld(new EntityItem(worldObj, bc.x + 0.5, bc.y + 0.5, bc.z + 0.5, stack.copy()));
if(TreeHarvestUtil.canDropApples(block, meta)) {
if(worldObj.rand.nextInt(200) == 0) {
worldObj.spawnEntityInWorld(new EntityItem(worldObj, bc.x + 0.5, bc.y + 0.5, bc.z + 0.5, new ItemStack(Items.apple)));
}
} else if(block == refBlock) { //other wise leaves
applyDamage(player, player.getCurrentEquippedItem(), 1, true);
usedPower = true;
}
}
}
}
private void getConnectedLogs(World world, BlockCoord bc, Block block, int blockMetadata, Set<BlockCoord> results, int maxBlocks) {
if(results.size() >= maxBlocks || results.contains(bc)) {
return;
}
Block targetBlock = world.getBlock(bc.x, bc.y, bc.z);
int targetMeta = world.getBlockMetadata(bc.x, bc.y, bc.z) % 4;
boolean isValidTarget = targetBlock == block && targetMeta == blockMetadata;
if(isValidTarget) {
results.add(bc);
getConnectedNeighbours(world, bc, blockMetadata, results, maxBlocks, targetBlock);
bc = bc.getLocation(ForgeDirection.UP);
getConnectedLogs(world, bc, targetBlock, blockMetadata, results, maxBlocks);
getConnectedNeighbours(world, bc, blockMetadata, results, maxBlocks, targetBlock);
}
}
private void getConnectedNeighbours(World world, BlockCoord bc, int blockMetadata, Set<BlockCoord> results, int maxBlocks, Block targetBlock) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir.offsetY == 0) { //logs next to us
getConnectedLogs(world, bc.getLocation(dir), targetBlock, blockMetadata, results, maxBlocks);
}
}
for(Point p : DIAGINALS) {
BlockCoord diagBc = new BlockCoord(bc.x + p.x, bc.y, bc.z + p.y);
getConnectedLogs(world, diagBc, targetBlock, blockMetadata, results, maxBlocks);
}
return usedPower;
}
@SubscribeEvent
@@ -287,6 +287,9 @@ public class TileFarmStation extends AbstractPoweredTaskEntity {
setNotification("noPower");
return false;
}
if("noPower".equals(notification)) {
clearNotification();
}
BlockCoord bc = getNextCoord();
if(bc != null && bc.equals(getLocation())) { //don't try and harvest ourselves
@@ -27,6 +27,8 @@ public class TreeFarmer implements IFarmerJoe {
protected Block sapling;
protected ItemStack saplingItem;
protected Block[] woods;
protected TreeHarvestUtil harvester = new TreeHarvestUtil();
public TreeFarmer(Block sapling, Block... wood) {
this.sapling = sapling;
@@ -98,7 +100,8 @@ public class TreeFarmer implements IFarmerJoe {
@Override
public IHarvestResult harvestBlock(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
HarvestResult res = new HarvestResult();
harvestUp(farm, bc, res);
//harvestUp(farm, bc, res);
harvester.harvest(farm, this, bc, res);
Collections.sort(res.harvestedBlocks, comp);
List<BlockCoord> actualHarvests = new ArrayList<BlockCoord>();
@@ -117,11 +120,7 @@ public class TreeFarmer implements IFarmerJoe {
if(!isWood(blk)) { //leaves
isWood = Config.farmAxeDamageOnLeafBreak;
int leaveMeta = farm.getBlockMeta(coord);
boolean canDropApple =
(blk instanceof BlockOldLeaf && (leaveMeta == 0 || leaveMeta == 8)) || //oak
(blk instanceof BlockNewLeaf && (leaveMeta == 1 || leaveMeta == 9)); //giant oak
if(canDropApple) {
if(TreeHarvestUtil.canDropApples(blk, leaveMeta)) {
if(farm.getWorldObj().rand.nextInt(200) == 0) {
res.drops.add(new EntityItem(farm.getWorldObj(), bc.x + 0.5, bc.y + 0.5, bc.z + 0.5, new ItemStack(Items.apple)));
}
@@ -145,64 +144,64 @@ public class TreeFarmer implements IFarmerJoe {
return res;
}
protected void harvestUp(TileFarmStation farm, BlockCoord bc, HarvestResult res) {
if(!isInHarvestBounds(farm, bc) || res.harvestedBlocks.contains(bc)) {
return;
}
Block blk = farm.getBlock(bc);
boolean isLeaves = blk instanceof BlockLeaves;
if(isWood(blk) || isLeaves) {
res.harvestedBlocks.add(bc);
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir != ForgeDirection.DOWN) {
harvestUp(farm, bc.getLocation(dir), res);
}
}
} else {
// check the sides for connected wood
harvestAdjacentWood(farm, bc, res);
//and another check for large oaks, where wood can be surrounded by leaves
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir.offsetY == 0) {
Block targetBlock = farm.getBlock(bc.getLocation(dir));
if(targetBlock instanceof BlockLeaves) {
harvestAdjacentWood(farm, bc, res);
}
}
}
}
}
private void harvestAdjacentWood(TileFarmStation farm, BlockCoord bc, HarvestResult res) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir.offsetY == 0) {
Block targetBlock = farm.getBlock(bc.getLocation(dir));
if(isWood(targetBlock)) {
harvestUp(farm, bc.getLocation(dir), res);
}
}
}
}
private boolean isInHarvestBounds(TileFarmStation farm, BlockCoord bc) {
BlockCoord fLoc = farm.getLocation();
int dist = Math.abs(fLoc.x - bc.x);
if(dist > farm.getFarmSize() + 7) {
return false;
}
dist = Math.abs(fLoc.z - bc.z);
if(dist > farm.getFarmSize() + 7) {
return false;
}
dist = Math.abs(bc.y - fLoc.y);
if(dist > 30) {
return false;
}
return true;
}
// protected void harvestUp(TileFarmStation farm, BlockCoord bc, HarvestResult res) {
//
// if(!isInHarvestBounds(farm, bc) || res.harvestedBlocks.contains(bc)) {
// return;
// }
//
// Block blk = farm.getBlock(bc);
// boolean isLeaves = blk instanceof BlockLeaves;
// if(isWood(blk) || isLeaves) {
// res.harvestedBlocks.add(bc);
// for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
// if(dir != ForgeDirection.DOWN) {
// harvestUp(farm, bc.getLocation(dir), res);
// }
// }
// } else {
// // check the sides for connected wood
// harvestAdjacentWood(farm, bc, res);
// //and another check for large oaks, where wood can be surrounded by leaves
// for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
// if(dir.offsetY == 0) {
// Block targetBlock = farm.getBlock(bc.getLocation(dir));
// if(targetBlock instanceof BlockLeaves) {
// harvestAdjacentWood(farm, bc, res);
// }
// }
// }
// }
//
// }
//
// private void harvestAdjacentWood(TileFarmStation farm, BlockCoord bc, HarvestResult res) {
// for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
// if(dir.offsetY == 0) {
// Block targetBlock = farm.getBlock(bc.getLocation(dir));
// if(isWood(targetBlock)) {
// harvestUp(farm, bc.getLocation(dir), res);
// }
// }
// }
// }
//
// private boolean isInHarvestBounds(TileFarmStation farm, BlockCoord bc) {
// BlockCoord fLoc = farm.getLocation();
// int dist = Math.abs(fLoc.x - bc.x);
// if(dist > farm.getFarmSize() + 7) {
// return false;
// }
// dist = Math.abs(fLoc.z - bc.z);
// if(dist > farm.getFarmSize() + 7) {
// return false;
// }
// dist = Math.abs(bc.y - fLoc.y);
// if(dist > 30) {
// return false;
// }
// return true;
// }
private static class HeightComparator implements Comparator<BlockCoord> {
@@ -0,0 +1,129 @@
package crazypants.enderio.machine.farm.farmers;
import java.util.ArrayList;
import java.util.List;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.BlockNewLeaf;
import net.minecraft.block.BlockOldLeaf;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import crazypants.enderio.machine.farm.TileFarmStation;
import crazypants.util.BlockCoord;
public class TreeHarvestUtil {
public static boolean canDropApples(Block block, int meta) {
return
(block instanceof BlockOldLeaf && (meta == 0 || meta == 8)) || //oak
(block instanceof BlockNewLeaf && (meta == 1 || meta == 9)); //giant oak
}
private int horizontalRange;
private int verticalRange;
protected Block[] woods;
private BlockCoord origin;
// private List<Block> extraLeaves = new ArrayList<Block>();
public TreeHarvestUtil() {
//Dont get saplings if we do this
// Block blk = GameRegistry.findBlock("Thaumcraft", "blockMagicalLeaves");
// if(blk != null) {
// extraLeaves.add(blk);
// }
}
public void harvest(TileFarmStation farm, TreeFarmer farmer, BlockCoord bc, HarvestResult res) {
woods = farmer.woods;
horizontalRange = farm.getFarmSize() + 7;
verticalRange = 30;
harvest(farm.getWorldObj(), farm.getLocation(), bc, res);
}
public void harvest(World world, BlockCoord bc, HarvestResult res) {
horizontalRange = 12;
verticalRange = 30;
origin = new BlockCoord(bc);
woods = new Block[] {world.getBlock(bc.x, bc.y, bc.z)};
harvestUp(world, bc, res);
}
private void harvest(World farm, BlockCoord origin, BlockCoord bc, HarvestResult res) {
this.origin = new BlockCoord(origin);
harvestUp(farm, bc, res);
}
protected void harvestUp(World world, BlockCoord bc, HarvestResult res) {
if(!isInHarvestBounds(bc) || res.harvestedBlocks.contains(bc)) {
return;
}
Block blk = world.getBlock(bc.x, bc.y,bc.z);
boolean isLeaves = blk instanceof BlockLeaves;//|| extraLeaves.contains(blk);
if(isWood(blk) || isLeaves) {
res.harvestedBlocks.add(bc);
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir != ForgeDirection.DOWN) {
harvestUp(world, bc.getLocation(dir), res);
}
}
} else {
// check the sides for connected wood
harvestAdjacentWood(world, bc, res);
//and another check for large oaks, where wood can be surrounded by leaves
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir.offsetY == 0) {
BlockCoord loc = bc.getLocation(dir);
Block targetBlock = world.getBlock(loc.x,loc.y,loc.z);
if(targetBlock instanceof BlockLeaves) {
harvestAdjacentWood(world, bc, res);
}
}
}
}
}
private void harvestAdjacentWood(World world, BlockCoord bc, HarvestResult res) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
if(dir.offsetY == 0) {
BlockCoord loc = bc.getLocation(dir);
Block targetBlock = world.getBlock(loc.x, loc.y, loc.z);
if(isWood(targetBlock)) {
harvestUp(world, bc.getLocation(dir), res);
}
}
}
}
private boolean isInHarvestBounds(BlockCoord bc) {
int dist = Math.abs(origin.x - bc.x);
if(dist > horizontalRange) {
return false;
}
dist = Math.abs(origin.z - bc.z);
if(dist > horizontalRange) {
return false;
}
dist = Math.abs(bc.y - horizontalRange);
if(dist > verticalRange) {
return false;
}
return true;
}
protected boolean isWood(Block block) {
for(Block wood : woods) {
if(block == wood) {
return true;
}
}
return false;
}
}
@@ -19,6 +19,12 @@ public final class BlockCoord {
this(tile.xCoord, tile.yCoord, tile.zCoord);
}
public BlockCoord(BlockCoord bc) {
x = bc.x;
y = bc.y;
z = bc.z;
}
public BlockCoord getLocation(ForgeDirection dir) {
return new BlockCoord(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ);
}