Skip to content

Commit

Permalink
Unify tool speed calculation in one accessible place, instead of havi…
Browse files Browse the repository at this point in the history
…ng the same code floating around 3 times.
  • Loading branch information
bonii-xx committed Aug 28, 2014
1 parent 7ca3aca commit 2dd426f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 89 deletions.
41 changes: 41 additions & 0 deletions src/main/java/tconstruct/library/tools/AbilityHelper.java
Expand Up @@ -680,4 +680,45 @@ public static MovingObjectPosition raytraceFromEntity (World world, Entity playe
return world.func_147447_a(vec3, vec31, par3, !par3, par3);
}

public static float calcToolSpeed(ToolCore tool, NBTTagCompound tags)
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;
if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}

if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}

if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}
float speedMod = 1f;
if(tool instanceof HarvestTool)
speedMod = ((HarvestTool) tool).breakSpeedModifier();

float trueSpeed = mineSpeed / (heads * 100f) * speedMod;
trueSpeed += calcStoneboundBonus(tool, tags);

return trueSpeed;
}

public static float calcStoneboundBonus(ToolCore tool, NBTTagCompound tags)
{
int durability = tags.getInteger("Damage");
float stonebound = tags.getFloat("Shoddy");
float stoneboundMod = 72f;
if(tool instanceof HarvestTool)
stoneboundMod = ((HarvestTool) tool).stoneboundModifier();

return (float) Math.log(durability / stoneboundMod + 1) * 2 * stonebound;
}
}
32 changes: 4 additions & 28 deletions src/main/java/tconstruct/library/tools/HarvestTool.java
Expand Up @@ -141,38 +141,14 @@ public float getDigSpeed (ItemStack stack, Block block, int meta)

public float calculateStrength (NBTTagCompound tags, Block block, int meta)
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;
if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}

if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}

if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}
float trueSpeed = mineSpeed / (heads * 100f) * breakSpeedModifier();
int hlvl = block.getHarvestLevel(meta);
int durability = tags.getInteger("Damage");

float stonebound = tags.getFloat("Shoddy");
float bonusLog = (float) Math.log(durability / stoneboundModifier() + 1) * 2 * stonebound;
trueSpeed += bonusLog;
if (hlvl > tags.getInteger("HarvestLevel"))
return 0.1f;

if (hlvl <= tags.getInteger("HarvestLevel"))
return trueSpeed;
return 0.1f;
return AbilityHelper.calcToolSpeed(this, tags);
}

public float breakSpeedModifier()
{
return 1.0f;
Expand Down
33 changes: 3 additions & 30 deletions src/main/java/tconstruct/tools/gui/CraftingStationGui.java
Expand Up @@ -17,6 +17,7 @@
import org.lwjgl.opengl.GL11;

import tconstruct.library.armor.ArmorCore;
import tconstruct.library.tools.AbilityHelper;
import tconstruct.library.tools.HarvestTool;
import tconstruct.library.tools.ToolCore;
import tconstruct.tools.logic.CraftingStationLogic;
Expand Down Expand Up @@ -315,36 +316,8 @@ void drawModularToolStats (ItemStack stack, ToolCore tool, NBTTagCompound tags)
}
else if (categories.contains("harvest"))
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;

if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}

if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}

if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}

float trueSpeed = mineSpeed / (heads * 100f);
if(tool instanceof HarvestTool)
trueSpeed *= ((HarvestTool) tool).breakSpeedModifier();


float localStonebound = 72f;
if (tool instanceof HarvestTool)
localStonebound = ((HarvestTool) tool).stoneboundModifier();
float stoneboundSpeed = (float) Math.log(durability / localStonebound + 1) * 2 * stonebound;
float trueSpeed = AbilityHelper.calcToolSpeed(tool, tags);
float stoneboundSpeed = AbilityHelper.calcToolSpeed(tool, tags);

DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
Expand Down
35 changes: 4 additions & 31 deletions src/main/java/tconstruct/tools/gui/ToolStationGui.java
Expand Up @@ -21,6 +21,7 @@
import tconstruct.TConstruct;
import tconstruct.library.client.TConstructClientRegistry;
import tconstruct.library.client.ToolGuiElement;
import tconstruct.library.tools.AbilityHelper;
import tconstruct.library.tools.HarvestTool;
import tconstruct.library.tools.ToolCore;
import tconstruct.library.util.HarvestLevels;
Expand Down Expand Up @@ -324,36 +325,8 @@ void drawModularToolStats (ItemStack stack, ToolCore tool, NBTTagCompound tags)
}
else if (categories.contains("harvest"))
{
float mineSpeed = tags.getInteger("MiningSpeed");
int heads = 1;

if (tags.hasKey("MiningSpeed2"))
{
mineSpeed += tags.getInteger("MiningSpeed2");
heads++;
}

if (tags.hasKey("MiningSpeedHandle"))
{
mineSpeed += tags.getInteger("MiningSpeedHandle");
heads++;
}

if (tags.hasKey("MiningSpeedExtra"))
{
mineSpeed += tags.getInteger("MiningSpeedExtra");
heads++;
}

float trueSpeed = mineSpeed / (heads * 100f);
if(tool instanceof HarvestTool)
trueSpeed *= ((HarvestTool) tool).breakSpeedModifier();


float localStonebound = 72f;
if (tool instanceof HarvestTool)
localStonebound = ((HarvestTool) tool).stoneboundModifier();
float stoneboundSpeed = (float) Math.log(durability / localStonebound + 1) * 2 * stonebound;
float trueSpeed = AbilityHelper.calcToolSpeed(tool, tags);
float stoneboundSpeed = AbilityHelper.calcToolSpeed(tool, tags);

DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
Expand All @@ -362,7 +335,7 @@ else if (categories.contains("harvest"))
trueSpeed = 0;
fontRendererObj.drawString(StatCollector.translateToLocal("gui.toolstation14") + df.format(trueSpeed), 294, base + offset * 10, 0xffffff);
offset++;
if (stoneboundSpeed != 0)
if (stoneboundSpeed != 0 && !Float.isNaN(stoneboundSpeed))
{
String bloss = stoneboundSpeed > 0 ? StatCollector.translateToLocal("gui.toolstation4") : StatCollector.translateToLocal("gui.toolstation5");
fontRendererObj.drawString(bloss + df.format(stoneboundSpeed), 294, base + offset * 10, 0xffffff);
Expand Down

0 comments on commit 2dd426f

Please sign in to comment.