protect clean up TE intergration code, add into TCon lib a mathutil class for simple math helper functions

This commit is contained in:
Progwml6 committed 2013-11-11 12:07:28 -05:00
1 parent 2734753aad
commit 56873dcf17
5 files changed
+94 -263

No files matched your search

-216
View File
@@ -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;
}
}
+70 -41
View File
@@ -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)
+11 -5
View File
@@ -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
@@ -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;
}
}
-1
View File
@@ -1,7 +1,6 @@
package tconstruct.modifiers;
import cofh.api.energy.IEnergyContainerItem;
import cofh.util.MathHelper;
import java.util.ArrayList;