From f1595ff3917766bf528a30eec7807f55fc1ceaf6 Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Thu, 7 May 2015 21:21:18 -0300 Subject: [PATCH 1/4] add rotation to the robot model and support ItemArmor.getArmorModel hook for robot wearables --- common/buildcraft/robotics/EntityRobot.java | 23 ++++++++++--------- .../robotics/render/RenderRobot.java | 14 +++++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/common/buildcraft/robotics/EntityRobot.java b/common/buildcraft/robotics/EntityRobot.java index 6bb9e880..37ad8e02 100644 --- a/common/buildcraft/robotics/EntityRobot.java +++ b/common/buildcraft/robotics/EntityRobot.java @@ -34,9 +34,11 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import net.minecraft.util.Vec3; import net.minecraft.world.World; + import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; + import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.Fluid; @@ -831,30 +833,29 @@ public class EntityRobot extends EntityRobotBase implements @Override public void aimItemAt(int x, int y, int z) { - itemAngle1 = (float) Math.atan2(z - Math.floor(posZ), - x - Math.floor(posX)); + int robotX = (int) Math.floor(posX); + int robotY = (int) Math.floor(posY); + int robotZ = (int) Math.floor(posZ); + if (z != robotZ || x != robotX) + itemAngle1 = (float) Math.atan2(z - robotZ, x - robotX); itemAngle2 = 0; - if (Math.floor(posY) < y) { + if (robotY < y) { itemAngle2 = (float) -Math.PI / 4; - if (Math.floor(posX) == x && Math.floor(posZ) == z) { + if (robotX == x && robotZ == z) { itemAngle2 -= (float) Math.PI / 4; } - } else if (Math.floor(posY) > y) { + } else if (robotY > y) { itemAngle2 = (float) Math.PI / 2; - if (Math.floor(posX) == x && Math.floor(posZ) == z) { + if (robotX == x && robotZ == z) { itemAngle2 += (float) Math.PI / 4; } } - int xComp = (int) Math.floor(posX); - int yComp = (int) Math.floor(posY); - int zComp = (int) Math.floor(posZ); - - setSteamDirection(xComp - x, yComp - y, zComp - z); + setSteamDirection(robotX - x, robotY - y, robotZ - z); updateDataServer(); } diff --git a/common/buildcraft/robotics/render/RenderRobot.java b/common/buildcraft/robotics/render/RenderRobot.java index 49dfd284..1c1c1dbb 100644 --- a/common/buildcraft/robotics/render/RenderRobot.java +++ b/common/buildcraft/robotics/render/RenderRobot.java @@ -13,6 +13,7 @@ import java.util.Date; import org.lwjgl.opengl.GL11; import net.minecraft.client.model.ModelBase; +import net.minecraft.client.model.ModelBiped; import net.minecraft.client.model.ModelRenderer; import net.minecraft.client.renderer.OpenGlHelper; import net.minecraft.client.renderer.entity.Render; @@ -25,6 +26,8 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.item.ItemArmor; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; + +import net.minecraftforge.client.ForgeHooksClient; import net.minecraftforge.client.IItemRenderer; import buildcraft.BuildCraftRobotics; @@ -158,6 +161,7 @@ public class RenderRobot extends Render implements IItemRenderer { if (robot.getTexture() != null) { renderManager.renderEngine.bindTexture(robot.getTexture()); float storagePercent = (float) robot.getBattery().getEnergyStored() / (float) robot.getBattery().getMaxEnergyStored(); + box.rotateAngleY = -robot.itemAngle1; doRenderRobot(1F / 16F, renderManager.renderEngine, storagePercent, robot.isActive()); } @@ -221,14 +225,20 @@ public class RenderRobot extends Render implements IItemRenderer { } private void doRenderWearable(EntityRobot entity, TextureManager textureManager, ItemStack wearable) { + float robotYaw = 90f + entity.itemAngle1 * 180f / (float)Math.PI; if (wearable.getItem() instanceof IRobotOverlayItem) { ((IRobotOverlayItem) wearable.getItem()).renderRobotOverlay(wearable, textureManager); } else if (wearable.getItem() instanceof ItemArmor) { GL11.glPushMatrix(); - GL11.glScalef(1.125F, 1.125F, 1.125F); + GL11.glScalef(1.0125F, 1.0125F, 1.0125F); + GL11.glTranslatef(0.0f, -0.25f, 0.0f); GL11.glRotatef(180F, 0, 0, 1); textureManager.bindTexture(RenderBiped.getArmorResource(entity, wearable, 0, null)); - helmetBox.render(1 / 16F); + ModelBiped model = ForgeHooksClient.getArmorModel(entity, wearable, 0, null); + if (model != null) + model.render(entity, 0, 0, 0, robotYaw, 0, 1 / 16F); + else + helmetBox.render(1 / 16F); GL11.glPopMatrix(); } } From cd88af1feffbcbd8044ad3b9b4aaeef75e17c01c Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Thu, 7 May 2015 22:20:53 -0300 Subject: [PATCH 2/4] smooth robots animations --- common/buildcraft/robotics/EntityRobot.java | 4 +- .../robotics/render/RenderRobot.java | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/common/buildcraft/robotics/EntityRobot.java b/common/buildcraft/robotics/EntityRobot.java index 37ad8e02..a39f4cf5 100644 --- a/common/buildcraft/robotics/EntityRobot.java +++ b/common/buildcraft/robotics/EntityRobot.java @@ -100,6 +100,7 @@ public class EntityRobot extends EntityRobotBase implements public ItemStack itemInUse; public float itemAngle1 = 0; + public float renderItemAngle1 = 0; public float itemAngle2 = 0; public boolean itemActive = false; public float itemActiveStage = 0; @@ -837,8 +838,9 @@ public class EntityRobot extends EntityRobotBase implements int robotY = (int) Math.floor(posY); int robotZ = (int) Math.floor(posZ); - if (z != robotZ || x != robotX) + if (z != robotZ || x != robotX) { itemAngle1 = (float) Math.atan2(z - robotZ, x - robotX); + } itemAngle2 = 0; if (robotY < y) { diff --git a/common/buildcraft/robotics/render/RenderRobot.java b/common/buildcraft/robotics/render/RenderRobot.java index 1c1c1dbb..2444f53e 100644 --- a/common/buildcraft/robotics/render/RenderRobot.java +++ b/common/buildcraft/robotics/render/RenderRobot.java @@ -85,6 +85,9 @@ public class RenderRobot extends Render implements IItemRenderer { GL11.glPushMatrix(); GL11.glTranslated(x, y, z); + float robotYaw = getSmoothYaw(robot); + GL11.glRotatef((-robotYaw / (2f * (float) Math.PI) * 360f) + 180f, 0.0f, 1.0f, 0.0f); + if (robot.getStackInSlot(0) != null) { GL11.glPushMatrix(); GL11.glTranslatef(-0.125F, 0, -0.125F); @@ -120,7 +123,6 @@ public class RenderRobot extends Render implements IItemRenderer { if (robot.itemInUse != null) { GL11.glPushMatrix(); - GL11.glRotatef((float) (-robot.itemAngle1 / (2 * Math.PI) * 360) + 180, 0, 1, 0); GL11.glRotatef((float) (robot.itemAngle2 / (2 * Math.PI) * 360), 0, 0, 1); if (robot.itemActive) { @@ -161,7 +163,6 @@ public class RenderRobot extends Render implements IItemRenderer { if (robot.getTexture() != null) { renderManager.renderEngine.bindTexture(robot.getTexture()); float storagePercent = (float) robot.getBattery().getEnergyStored() / (float) robot.getBattery().getMaxEnergyStored(); - box.rotateAngleY = -robot.itemAngle1; doRenderRobot(1F / 16F, renderManager.renderEngine, storagePercent, robot.isActive()); } @@ -172,6 +173,35 @@ public class RenderRobot extends Render implements IItemRenderer { GL11.glPopMatrix(); } + private float getSmoothYaw(EntityRobot robot) { + if (robot.itemAngle1 == robot.renderItemAngle1) { + return robot.itemAngle1; + } + + float step = 0; + if (robot.itemAngle1 < robot.renderItemAngle1) { + step = (robot.itemAngle1 - robot.renderItemAngle1 <= Math.PI) ? -0.25f : 0.25f; + } else { + step = (robot.renderItemAngle1 - robot.itemAngle1 < Math.PI) ? -0.25f : 0.25f; + } + robot.renderItemAngle1 += step; + if (robot.renderItemAngle1 >= Math.PI) { + step *= -1; + robot.renderItemAngle1 -= (float) Math.PI; + } + if (robot.renderItemAngle1 <= -Math.PI) { + step *= -1; + robot.renderItemAngle1 += (float) Math.PI; + } + + if ((step < 0 && robot.renderItemAngle1 < robot.itemAngle1) || + (step > 0 && robot.itemAngle1 < robot.renderItemAngle1)) { + robot.renderItemAngle1 = robot.itemAngle1; + } + + return robot.renderItemAngle1; + } + @Override protected ResourceLocation getEntityTexture(Entity entity) { return ((EntityRobot) entity).getTexture(); @@ -225,7 +255,6 @@ public class RenderRobot extends Render implements IItemRenderer { } private void doRenderWearable(EntityRobot entity, TextureManager textureManager, ItemStack wearable) { - float robotYaw = 90f + entity.itemAngle1 * 180f / (float)Math.PI; if (wearable.getItem() instanceof IRobotOverlayItem) { ((IRobotOverlayItem) wearable.getItem()).renderRobotOverlay(wearable, textureManager); } else if (wearable.getItem() instanceof ItemArmor) { @@ -234,11 +263,12 @@ public class RenderRobot extends Render implements IItemRenderer { GL11.glTranslatef(0.0f, -0.25f, 0.0f); GL11.glRotatef(180F, 0, 0, 1); textureManager.bindTexture(RenderBiped.getArmorResource(entity, wearable, 0, null)); - ModelBiped model = ForgeHooksClient.getArmorModel(entity, wearable, 0, null); - if (model != null) - model.render(entity, 0, 0, 0, robotYaw, 0, 1 / 16F); - else + ModelBiped armorModel = ForgeHooksClient.getArmorModel(entity, wearable, 0, null); + if (armorModel != null) { + armorModel.render(entity, 0, 0, 0, -90f, 0, 1 / 16F); + } else { helmetBox.render(1 / 16F); + } GL11.glPopMatrix(); } } From b8675dc414f5f9b05e8a612085ac287f628fd5fd Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Mon, 11 May 2015 21:30:03 -0300 Subject: [PATCH 3/4] move robot rotation logic from the render loop to the game loop --- .../api/robots/EntityRobotBase.java | 8 ++ common/buildcraft/robotics/EntityRobot.java | 95 ++++++++++++++----- .../robotics/ai/AIRobotGotoBlock.java | 3 +- .../robotics/ai/AIRobotGotoStation.java | 8 ++ .../robotics/render/RenderRobot.java | 53 ++++------- 5 files changed, 108 insertions(+), 59 deletions(-) diff --git a/api/buildcraft/api/robots/EntityRobotBase.java b/api/buildcraft/api/robots/EntityRobotBase.java index 3d18af54..1febc7be 100755 --- a/api/buildcraft/api/robots/EntityRobotBase.java +++ b/api/buildcraft/api/robots/EntityRobotBase.java @@ -14,9 +14,11 @@ import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; + import net.minecraftforge.fluids.IFluidHandler; import cofh.api.energy.IEnergyStorage; + import buildcraft.api.boards.RedstoneBoardRobot; import buildcraft.api.core.IZone; @@ -41,8 +43,14 @@ public abstract class EntityRobotBase extends EntityLiving implements IInventory public abstract RedstoneBoardRobot getBoard(); + public abstract void aimItemAt(float yaw, float pitch); + public abstract void aimItemAt(int x, int y, int z); + public abstract float getAimYaw(); + + public abstract float getAimPitch(); + public abstract int getEnergy(); public abstract IEnergyStorage getBattery(); diff --git a/common/buildcraft/robotics/EntityRobot.java b/common/buildcraft/robotics/EntityRobot.java index a39f4cf5..1a3fd323 100644 --- a/common/buildcraft/robotics/EntityRobot.java +++ b/common/buildcraft/robotics/EntityRobot.java @@ -30,6 +30,7 @@ import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.DamageSource; import net.minecraft.util.EntityDamageSource; import net.minecraft.util.IIcon; +import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import net.minecraft.util.Vec3; @@ -293,6 +294,7 @@ public class EntityRobot extends EntityRobotBase implements if (worldObj.isRemote) { updateDataClient(); + updateRotationYaw(60.0f); updateEnergyFX(); } @@ -346,6 +348,10 @@ public class EntityRobot extends EntityRobotBase implements this.worldObj.theProfiler.endSection(); } + @Override + protected void updateEntityActionState() { + } + @SideOnly(Side.CLIENT) private void updateEnergyFX() { energyFX += energySpendPerCycle; @@ -833,35 +839,78 @@ public class EntityRobot extends EntityRobotBase implements } @Override - public void aimItemAt(int x, int y, int z) { - int robotX = (int) Math.floor(posX); - int robotY = (int) Math.floor(posY); - int robotZ = (int) Math.floor(posZ); + public float getAimYaw() { + return itemAngle1; + } - if (z != robotZ || x != robotX) { - itemAngle1 = (float) Math.atan2(z - robotZ, x - robotX); - } - itemAngle2 = 0; + @Override + public float getAimPitch() { + return itemAngle2; + } - if (robotY < y) { - itemAngle2 = (float) -Math.PI / 4; - - if (robotX == x && robotZ == z) { - itemAngle2 -= (float) Math.PI / 4; - } - } else if (robotY > y) { - itemAngle2 = (float) Math.PI / 2; - - if (robotX == x && robotZ == z) { - itemAngle2 += (float) Math.PI / 4; - } - } - - setSteamDirection(robotX - x, robotY - y, robotZ - z); + @Override + public void aimItemAt(float yaw, float pitch) { + itemAngle1 = yaw; + itemAngle2 = pitch; updateDataServer(); } + @Override + public void aimItemAt(int x, int y, int z) { + int deltaX = x - (int) Math.floor(posX); + int deltaY = y - (int) Math.floor(posY); + int deltaZ = z - (int) Math.floor(posZ); + + if (deltaX != 0 || deltaZ != 0) { + itemAngle1 = (float) (Math.atan2(deltaZ, deltaX) * 180f / Math.PI) + 180f; + } + double d3 = MathHelper.sqrt_double(deltaX * deltaX + deltaZ * deltaZ); + itemAngle2 = (float) (-(Math.atan2(deltaY, d3) * 180.0D / Math.PI)); + + setSteamDirection(deltaX, deltaY, deltaZ); + + updateDataServer(); + } + + private void updateRotationYaw(float maxStep) { + float step = MathHelper.wrapAngleTo180_float(itemAngle1 - rotationYaw); + + if (step > maxStep) { + step = maxStep; + } + + if (step < -maxStep) { + step = -maxStep; + } + + rotationYaw = rotationYaw + step; + } + + @Override + protected float func_110146_f(float targetYaw, float dist) { + if (worldObj.isRemote) { + float f2 = MathHelper.wrapAngleTo180_float(this.rotationYaw - this.renderYawOffset); + this.renderYawOffset += f2 * 0.5F; + float f3 = MathHelper.wrapAngleTo180_float(this.rotationYaw - this.renderYawOffset); + boolean flag = f3 < -90.0F || f3 >= 90.0F; + + this.renderYawOffset = this.rotationYaw - f3; + + if (f3 * f3 > 2500.0F) { + this.renderYawOffset += f3 * 0.2F; + } + + float newDist = dist; + if (flag) { + newDist *= -1.0F; + } + + return newDist; + } + return 0; + } + @Override public void setItemActive(final boolean isActive) { if (isActive != itemActive) { diff --git a/common/buildcraft/robotics/ai/AIRobotGotoBlock.java b/common/buildcraft/robotics/ai/AIRobotGotoBlock.java index ed6e5180..1510a780 100755 --- a/common/buildcraft/robotics/ai/AIRobotGotoBlock.java +++ b/common/buildcraft/robotics/ai/AIRobotGotoBlock.java @@ -38,7 +38,6 @@ public class AIRobotGotoBlock extends AIRobotGoto { finalX = x; finalY = y; finalZ = z; - robot.aimItemAt((int) Math.floor(finalX), (int) Math.floor(finalY), (int) Math.floor(finalZ)); } public AIRobotGotoBlock(EntityRobotBase robot, int x, int y, int z, double iMaxDistance) { @@ -53,7 +52,6 @@ public class AIRobotGotoBlock extends AIRobotGoto { finalX = path.getLast().x; finalY = path.getLast().y; finalZ = path.getLast().z; - robot.aimItemAt((int) Math.floor(finalX), (int) Math.floor(finalY), (int) Math.floor(finalZ)); setNextInPath(); } @@ -118,6 +116,7 @@ public class AIRobotGotoBlock extends AIRobotGoto { BlockIndex next = path.getFirst(); setDestination(robot, next.x + 0.5F, next.y + 0.5F, next.z + 0.5F); prevDistance = Double.MAX_VALUE; + robot.aimItemAt(next.x, next.y, next.z); } } diff --git a/common/buildcraft/robotics/ai/AIRobotGotoStation.java b/common/buildcraft/robotics/ai/AIRobotGotoStation.java index 8b2ae423..ed54655d 100755 --- a/common/buildcraft/robotics/ai/AIRobotGotoStation.java +++ b/common/buildcraft/robotics/ai/AIRobotGotoStation.java @@ -9,6 +9,8 @@ package buildcraft.robotics.ai; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.MathHelper; + import net.minecraftforge.common.util.ForgeDirection; import buildcraft.api.core.BlockIndex; @@ -69,6 +71,12 @@ public class AIRobotGotoStation extends AIRobot { stationIndex.z + 0.5F + stationSide.offsetZ * 0.5F)); } else { setSuccess(true); + if (stationSide.offsetY == 0) { + robot.aimItemAt(stationIndex.x + 2 * stationSide.offsetX, stationIndex.y, + stationIndex.z + 2 * stationSide.offsetZ); + } else { + robot.aimItemAt(MathHelper.floor_float(robot.getAimYaw() / 90f) * 90f + 180f, robot.getAimPitch()); + } robot.dock(station); terminate(); } diff --git a/common/buildcraft/robotics/render/RenderRobot.java b/common/buildcraft/robotics/render/RenderRobot.java index 2444f53e..019eaf7e 100644 --- a/common/buildcraft/robotics/render/RenderRobot.java +++ b/common/buildcraft/robotics/render/RenderRobot.java @@ -78,15 +78,15 @@ public class RenderRobot extends Render implements IItemRenderer { @Override public void doRender(Entity entity, double x, double y, double z, float f, float f1) { - doRender((EntityRobot) entity, x, y, z); + doRender((EntityRobot) entity, x, y, z, f1); } - private void doRender(EntityRobot robot, double x, double y, double z) { + private void doRender(EntityRobot robot, double x, double y, double z, float partialTicks) { GL11.glPushMatrix(); GL11.glTranslated(x, y, z); - float robotYaw = getSmoothYaw(robot); - GL11.glRotatef((-robotYaw / (2f * (float) Math.PI) * 360f) + 180f, 0.0f, 1.0f, 0.0f); + float robotYaw = this.interpolateRotation(robot.prevRenderYawOffset, robot.renderYawOffset, partialTicks); + GL11.glRotatef(-robotYaw, 0.0f, 1.0f, 0.0f); if (robot.getStackInSlot(0) != null) { GL11.glPushMatrix(); @@ -123,7 +123,7 @@ public class RenderRobot extends Render implements IItemRenderer { if (robot.itemInUse != null) { GL11.glPushMatrix(); - GL11.glRotatef((float) (robot.itemAngle2 / (2 * Math.PI) * 360), 0, 0, 1); + GL11.glRotatef(robot.itemAngle2, 0, 0, 1); if (robot.itemActive) { long newDate = new Date().getTime(); @@ -173,35 +173,6 @@ public class RenderRobot extends Render implements IItemRenderer { GL11.glPopMatrix(); } - private float getSmoothYaw(EntityRobot robot) { - if (robot.itemAngle1 == robot.renderItemAngle1) { - return robot.itemAngle1; - } - - float step = 0; - if (robot.itemAngle1 < robot.renderItemAngle1) { - step = (robot.itemAngle1 - robot.renderItemAngle1 <= Math.PI) ? -0.25f : 0.25f; - } else { - step = (robot.renderItemAngle1 - robot.itemAngle1 < Math.PI) ? -0.25f : 0.25f; - } - robot.renderItemAngle1 += step; - if (robot.renderItemAngle1 >= Math.PI) { - step *= -1; - robot.renderItemAngle1 -= (float) Math.PI; - } - if (robot.renderItemAngle1 <= -Math.PI) { - step *= -1; - robot.renderItemAngle1 += (float) Math.PI; - } - - if ((step < 0 && robot.renderItemAngle1 < robot.itemAngle1) || - (step > 0 && robot.itemAngle1 < robot.renderItemAngle1)) { - robot.renderItemAngle1 = robot.itemAngle1; - } - - return robot.renderItemAngle1; - } - @Override protected ResourceLocation getEntityTexture(Entity entity) { return ((EntityRobot) entity).getTexture(); @@ -302,5 +273,19 @@ public class RenderRobot extends Render implements IItemRenderer { OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, lastBrightnessX, lastBrightnessY); } + + } + + private float interpolateRotation(float prevRot, float rot, float partialTicks) { + float angle; + + for (angle = rot - prevRot; angle < -180.0F; angle += 360.0F) { + } + + while (angle >= 180.0F) { + angle -= 360.0F; + } + + return prevRot + partialTicks * angle; } } From 1ef57453bed4451c75c1c0bcf036a7c12a74d057 Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Wed, 13 May 2015 07:45:10 -0300 Subject: [PATCH 4/4] fix lists only using the first stack per line --- common/buildcraft/core/ItemList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/buildcraft/core/ItemList.java b/common/buildcraft/core/ItemList.java index e7678018..8e4ff0f2 100644 --- a/common/buildcraft/core/ItemList.java +++ b/common/buildcraft/core/ItemList.java @@ -200,7 +200,7 @@ public class ItemList extends ItemBuildCraft implements IList { return oreMatch(stacks[0], item); } else { for (ItemStack stack : stacks) { - if (stack != null && StackHelper.isMatchingItem(stacks[0], item, true, false)) { + if (stack != null && StackHelper.isMatchingItem(stack, item, true, false)) { return true; } }