From 56873dcf174eedcb5d84dce4d689d9ba288d5799 Mon Sep 17 00:00:00 2001 From: Progwml6 Date: Mon, 11 Nov 2013 12:07:28 -0500 Subject: [PATCH] protect clean up TE intergration code, add into TCon lib a mathutil class for simple math helper functions --- src/cofh/util/MathHelper.java | 216 ------------------ .../library/tools/AbilityHelper.java | 111 +++++---- src/tconstruct/library/tools/ToolCore.java | 16 +- src/tconstruct/library/util/MathUtils.java | 13 ++ src/tconstruct/modifiers/ModFlux.java | 1 - 5 files changed, 94 insertions(+), 263 deletions(-) delete mode 100644 src/cofh/util/MathHelper.java create mode 100644 src/tconstruct/library/util/MathUtils.java diff --git a/src/cofh/util/MathHelper.java b/src/cofh/util/MathHelper.java deleted file mode 100644 index 48b639e4..00000000 --- a/src/cofh/util/MathHelper.java +++ /dev/null @@ -1,216 +0,0 @@ -package cofh.util; - -/** - * Contains various math-related helper functions. Often faster than conventional implementations. - * - * @author King Lemming - * - */ -public final class MathHelper -{ - - private MathHelper() - { - - } - - public static final double PHI = 1.618034; - public static final double[] SIN_TABLE = new double[65536]; - - static - { - for (int i = 0; i < 65536; i++) - { - SIN_TABLE[i] = Math.sin(i / 65536D * 2 * Math.PI); - } - SIN_TABLE[0] = 0; - SIN_TABLE[16384] = 1; - SIN_TABLE[32768] = 0; - SIN_TABLE[49152] = 1; - } - - public static double sin (double d) - { - - return SIN_TABLE[(int) ((float) d * 10430.378F) & 65535]; - } - - public static double cos (double d) - { - - return SIN_TABLE[(int) ((float) d * 10430.378F + 16384.0F) & 65535]; - } - - public static float approachLinear (float a, float b, float max) - { - - return a > b ? a - b < max ? b : a - max : b - a < max ? b : a + max; - } - - public static double approachLinear (double a, double b, double max) - { - - return a > b ? a - b < max ? b : a - max : b - a < max ? b : a + max; - } - - public static float interpolate (float a, float b, float d) - { - - return a + (b - a) * d; - } - - public static double interpolate (double a, double b, double d) - { - - return a + (b - a) * d; - } - - public static double approachExp (double a, double b, double ratio) - { - - return a + (b - a) * ratio; - } - - public static double approachExp (double a, double b, double ratio, double cap) - { - - double d = (b - a) * ratio; - - if (Math.abs(d) > cap) - { - d = Math.signum(d) * cap; - } - return a + d; - } - - public static double retreatExp (double a, double b, double c, double ratio, double kick) - { - - double d = (Math.abs(c - a) + kick) * ratio; - - if (d > Math.abs(b - a)) - { - return b; - } - return a + Math.signum(b - a) * d; - } - - public static double clip (double value, double min, double max) - { - - if (value > max) - { - value = max; - } - else if (value < min) - { - value = min; - } - return value; - } - - public static boolean between (double a, double x, double b) - { - - return a <= x && x <= b; - } - - public static int approachExpI (int a, int b, double ratio) - { - - int r = (int) Math.round(approachExp(a, b, ratio)); - return r == a ? b : r; - } - - public static int retreatExpI (int a, int b, int c, double ratio, int kick) - { - - int r = (int) Math.round(retreatExp(a, b, c, ratio, kick)); - return r == a ? b : r; - } - - public static int floor_double (double d) - { - - return net.minecraft.util.MathHelper.floor_double(d); - } - - /** - * Unchecked implementation to round a number. Parameter should be known to be valid in advance. - */ - public static int round (double d) - { - - return (int) (d + 0.5D); - } - - /** - * Unchecked implementation to round a number up. Parameter should be known to be valid in advance. - */ - public static int ceil (double d) - { - - return (int) (d + 0.9999D); - } - - /** - * Unchecked implementation to determine the smaller of two Floats. Parameters should be known to be valid in advance. - */ - public static float minF (float a, float b) - { - - return a < b ? a : b; - } - - public static float minF (int a, float b) - { - - return a < b ? a : b; - } - - public static float minF (float a, int b) - { - - return a < b ? a : b; - } - - /** - * Unchecked implementation to determine the larger of two Floats. Parameters should be known to be valid in advance. - */ - public static float maxF (float a, float b) - { - - return a > b ? a : b; - } - - public static float maxF (int a, float b) - { - - return a > b ? a : b; - } - - public static float maxF (float a, int b) - { - - return a > b ? a : b; - } - - /** - * Unchecked implementation to determine the smaller of two Integers. Parameters should be known to be valid in advance. - */ - public static int minI (int a, int b) - { - - return a < b ? a : b; - } - - /** - * Unchecked implementation to determine the larger of two Integers. Parameters should be known to be valid in advance. - */ - public static int maxI (int a, int b) - { - - return a > b ? a : b; - } - -} diff --git a/src/tconstruct/library/tools/AbilityHelper.java b/src/tconstruct/library/tools/AbilityHelper.java index 95cf9961..cdef201f 100644 --- a/src/tconstruct/library/tools/AbilityHelper.java +++ b/src/tconstruct/library/tools/AbilityHelper.java @@ -389,54 +389,83 @@ public class AbilityHelper public static boolean damageElectricTool (ItemStack stack, NBTTagCompound tags, Entity entity) { - if (!tags.hasKey("charge")) - return false; - - NBTTagCompound toolTag = stack.getTagCompound().getCompoundTag("InfiTool"); - int charge = tags.getInteger("charge"); - - int durability = toolTag.getInteger("Damage"); - float shoddy = toolTag.getFloat("Shoddy"); - - float mineSpeed = toolTag.getInteger("MiningSpeed"); - int heads = 1; - if (toolTag.hasKey("MiningSpeed2")) + if (tags.hasKey("charge") || tags.hasKey("Energy")) { - mineSpeed += toolTag.getInteger("MiningSpeed2"); - heads++; - } - if (toolTag.hasKey("MiningSpeedHandle")) - { - mineSpeed += toolTag.getInteger("MiningSpeedHandle"); - heads++; - } + NBTTagCompound toolTag = stack.getTagCompound().getCompoundTag("InfiTool"); + int charge = -1; + int energy = -1; + if (tags.hasKey("charge")) + { + charge = tags.getInteger("charge"); + } + if (tags.hasKey("Energy")) + { + energy = tags.getInteger("Energy"); + } + int durability = toolTag.getInteger("Damage"); + float shoddy = toolTag.getFloat("Shoddy"); - if (toolTag.hasKey("MiningSpeedExtra")) - { - mineSpeed += toolTag.getInteger("MiningSpeedExtra"); - heads++; - } - float trueSpeed = mineSpeed / (heads * 100f); - float stonebound = toolTag.getFloat("Shoddy"); - float bonusLog = (float) Math.log(durability / 72f + 1) * 2 * stonebound; - trueSpeed += bonusLog; - trueSpeed *= 6; + float mineSpeed = toolTag.getInteger("MiningSpeed"); + int heads = 1; + if (toolTag.hasKey("MiningSpeed2")) + { + mineSpeed += toolTag.getInteger("MiningSpeed2"); + heads++; + } - if (charge < trueSpeed) - { + if (toolTag.hasKey("MiningSpeedHandle")) + { + mineSpeed += toolTag.getInteger("MiningSpeedHandle"); + heads++; + } + + if (toolTag.hasKey("MiningSpeedExtra")) + { + mineSpeed += toolTag.getInteger("MiningSpeedExtra"); + heads++; + } + float trueSpeed = mineSpeed / (heads * 100f); + float stonebound = toolTag.getFloat("Shoddy"); + float bonusLog = (float) Math.log(durability / 72f + 1) * 2 * stonebound; + trueSpeed += bonusLog; + trueSpeed *= 6; if (charge > 0) - tags.setInteger("charge", 0); + { + if (charge < trueSpeed) + { + if (charge > 0) + tags.setInteger("charge", 0); + return false; + } + + charge -= trueSpeed; + ToolCore tool = (ToolCore) stack.getItem(); + stack.setItemDamage(1 + (tool.getMaxCharge(stack) - charge) * (stack.getMaxDamage() - 1) / tool.getMaxCharge(stack)); + tags.setInteger("charge", charge); + if (entity instanceof EntityPlayer) + chargeFromArmor(stack, (EntityPlayer) entity); + } + if (energy > 0) + { + if (energy < trueSpeed) + { + if (energy > 0) + tags.setInteger("Energy", 0); + return false; + } + + energy -= trueSpeed; + ToolCore tool = (ToolCore) stack.getItem(); + stack.setItemDamage(1 + (tool.getMaxEnergyStored(stack) - energy) * (stack.getMaxDamage() - 1) / tool.getMaxEnergyStored(stack)); + tags.setInteger("Energy", energy); + } + return true; + } + else + { return false; } - - charge -= trueSpeed; - ToolCore tool = (ToolCore) stack.getItem(); - stack.setItemDamage(1 + (tool.getMaxCharge(stack) - charge) * (stack.getMaxDamage() - 1) / tool.getMaxCharge(stack)); - tags.setInteger("charge", charge); - if (entity instanceof EntityPlayer) - chargeFromArmor(stack, (EntityPlayer) entity); - return true; } static void chargeFromArmor (ItemStack stack, EntityPlayer player) diff --git a/src/tconstruct/library/tools/ToolCore.java b/src/tconstruct/library/tools/ToolCore.java index 2f67428e..1fcfbda8 100644 --- a/src/tconstruct/library/tools/ToolCore.java +++ b/src/tconstruct/library/tools/ToolCore.java @@ -10,8 +10,6 @@ import java.util.Map; import java.util.Random; import cofh.api.energy.IEnergyContainerItem; -import cofh.util.MathHelper; - import appeng.api.IAEItemStack; import appeng.api.me.items.IAEChargeableItem; import appeng.api.me.items.IStorageCell; @@ -35,6 +33,7 @@ import tconstruct.common.TContent; import tconstruct.library.ActiveToolMod; import tconstruct.library.TConstructRegistry; import tconstruct.library.crafting.ToolBuilder; +import tconstruct.library.util.MathUtils; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -379,7 +378,7 @@ public abstract class ToolCore extends Item implements IEnergyContainerItem, ICu color = "\u00a76"; } - String energy = new StringBuilder().append(color).append(tags.getInteger("energy")).append("/").append(getMaxEnergyStored(stack)).append(" RF").toString(); + String energy = new StringBuilder().append(color).append(tags.getInteger("Energy")).append("/").append(getMaxEnergyStored(stack)).append(" RF").toString(); list.add(energy); } if (tags.hasKey("InfiTool")) @@ -1104,11 +1103,13 @@ public abstract class ToolCore extends Item implements IEnergyContainerItem, ICu if (tags == null || !tags.hasKey("Energy")) return 0; int energy = tags.getInteger("Energy"); - int energyReceived = MathHelper.minI(capacity - energy, MathHelper.minI(this.maxReceive, maxReceive)); + int energyReceived = MathUtils.minInt(capacity - energy, MathUtils.minInt(this.maxReceive, maxReceive)); if (!simulate) { energy += energyReceived; tags.setInteger("Energy", energy); + container.setItemDamage(1 + (getMaxEnergyStored(container) - energy) * (container.getMaxDamage() - 2) / getMaxEnergyStored(container)); + } return energyReceived; } @@ -1122,11 +1123,13 @@ public abstract class ToolCore extends Item implements IEnergyContainerItem, ICu return 0; } int energy = tags.getInteger("Energy"); - int energyExtracted = MathHelper.minI(energy, MathHelper.minI(this.maxExtract, maxExtract)); + int energyExtracted = MathUtils.minInt(energy, MathUtils.minInt(this.maxExtract, maxExtract)); if (!simulate) { energy -= energyExtracted; tags.setInteger("Energy", energy); + container.setItemDamage(1 + (getMaxEnergyStored(container) - energy) * (container.getMaxDamage() - 1) / getMaxEnergyStored(container)); + } return energyExtracted; } @@ -1145,6 +1148,9 @@ public abstract class ToolCore extends Item implements IEnergyContainerItem, ICu @Override public int getMaxEnergyStored (ItemStack container) { + NBTTagCompound tags =container.getTagCompound(); + if (tags == null || !tags.hasKey("Energy")) + return 0; return capacity; } //end of TE support section diff --git a/src/tconstruct/library/util/MathUtils.java b/src/tconstruct/library/util/MathUtils.java new file mode 100644 index 00000000..c52d481b --- /dev/null +++ b/src/tconstruct/library/util/MathUtils.java @@ -0,0 +1,13 @@ +package tconstruct.library.util; + +public class MathUtils +{ + /** + * finds smaller of 2 valid int's + */ + public static int minInt (int a, int b) + { + + return a < b ? a : b; + } +} diff --git a/src/tconstruct/modifiers/ModFlux.java b/src/tconstruct/modifiers/ModFlux.java index 6f5dc80c..eab2f50c 100644 --- a/src/tconstruct/modifiers/ModFlux.java +++ b/src/tconstruct/modifiers/ModFlux.java @@ -1,7 +1,6 @@ package tconstruct.modifiers; import cofh.api.energy.IEnergyContainerItem; -import cofh.util.MathHelper; import java.util.ArrayList;