diff --git a/common/buildcraft/silicon/BlockLaser.java b/common/buildcraft/silicon/BlockLaser.java index 0e2ad009..d1a27d75 100644 --- a/common/buildcraft/silicon/BlockLaser.java +++ b/common/buildcraft/silicon/BlockLaser.java @@ -8,25 +8,27 @@ */ package buildcraft.silicon; +import static net.minecraft.util.AxisAlignedBB.getBoundingBox; + +import java.util.List; + import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.IIcon; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.util.Vec3; +import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import buildcraft.core.CreativeTabBuildCraft; +import buildcraft.core.ICustomHighlight; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import net.minecraft.util.MovingObjectPosition; -import buildcraft.core.ICustomHighlight; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.Vec3; -import java.util.List; - -import static net.minecraft.util.AxisAlignedBB.getBoundingBox; public class BlockLaser extends BlockContainer implements ICustomHighlight { @@ -118,12 +120,13 @@ public class BlockLaser extends BlockContainer implements ICustomHighlight { @Override public IIcon getIcon(int i, int j) { - if (i == ForgeDirection.values()[j].getOpposite().ordinal()) + if (i == ForgeDirection.values()[j].getOpposite().ordinal()) { return textureBottom; - else if (i == j) + } else if (i == j) { return textureTop; - else + } else { return textureSide; + } } @@ -145,4 +148,9 @@ public class BlockLaser extends BlockContainer implements ICustomHighlight { textureBottom = par1IconRegister.registerIcon("buildcraft:laser_bottom"); textureSide = par1IconRegister.registerIcon("buildcraft:laser_side"); } + + @Override + public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) { + return false; + } } diff --git a/common/buildcraft/silicon/SiliconProxyClient.java b/common/buildcraft/silicon/SiliconProxyClient.java index 2a0c7785..5e20b489 100644 --- a/common/buildcraft/silicon/SiliconProxyClient.java +++ b/common/buildcraft/silicon/SiliconProxyClient.java @@ -8,6 +8,8 @@ */ package buildcraft.silicon; +import buildcraft.silicon.render.RenderLaserBlock; +import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.client.registry.RenderingRegistry; public class SiliconProxyClient extends SiliconProxy { @@ -15,5 +17,7 @@ public class SiliconProxyClient extends SiliconProxy { public void registerRenderers() { SiliconProxy.laserBlockModel = RenderingRegistry.getNextAvailableRenderId(); RenderingRegistry.registerBlockHandler(new SiliconRenderBlock()); + + ClientRegistry.bindTileEntitySpecialRenderer(TileLaser.class, new RenderLaserBlock()); } } diff --git a/common/buildcraft/silicon/TileLaser.java b/common/buildcraft/silicon/TileLaser.java index ec5ef581..127fbfba 100644 --- a/common/buildcraft/silicon/TileLaser.java +++ b/common/buildcraft/silicon/TileLaser.java @@ -8,36 +8,39 @@ */ package buildcraft.silicon; -import buildcraft.api.power.ILaserTarget; -import buildcraft.BuildCraftCore; -import buildcraft.BuildCraftFactory; -import buildcraft.BuildCraftMod; -import buildcraft.BuildCraftSilicon; -import buildcraft.api.core.Position; -import buildcraft.api.core.SafeTimeTracker; -import buildcraft.api.gates.IAction; -import buildcraft.api.gates.IActionReceptor; -import buildcraft.api.power.IPowerReceptor; -import buildcraft.api.power.PowerHandler; -import buildcraft.api.power.PowerHandler.PowerReceiver; -import buildcraft.api.power.PowerHandler.Type; -import buildcraft.core.EntityEnergyLaser; -import buildcraft.core.IMachine; -import buildcraft.core.TileBuildCraft; -import buildcraft.core.proxy.CoreProxy; -import buildcraft.core.triggers.ActionMachineControl; - import java.util.LinkedList; import java.util.List; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.util.ForgeDirection; +import buildcraft.BuildCraftCore; +import buildcraft.api.core.NetworkData; +import buildcraft.api.core.Position; +import buildcraft.api.core.SafeTimeTracker; +import buildcraft.api.gates.IAction; +import buildcraft.api.gates.IActionReceptor; +import buildcraft.api.power.ILaserTarget; +import buildcraft.api.power.IPowerReceptor; +import buildcraft.api.power.PowerHandler; +import buildcraft.api.power.PowerHandler.PowerReceiver; +import buildcraft.api.power.PowerHandler.Type; +import buildcraft.core.Box; +import buildcraft.core.EntityLaser; +import buildcraft.core.IMachine; +import buildcraft.core.LaserData; +import buildcraft.core.TileBuildCraft; +import buildcraft.core.triggers.ActionMachineControl; public class TileLaser extends TileBuildCraft implements IPowerReceptor, IActionReceptor, IMachine { private static final float LASER_OFFSET = 2.0F / 16.0F; - private EntityEnergyLaser laser = null; + + @NetworkData + public LaserData laser = new LaserData(); + private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(10); private final SafeTimeTracker searchTracker = new SafeTimeTracker(100, 100); private final SafeTimeTracker networkTracker = new SafeTimeTracker(3); @@ -46,6 +49,15 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown; private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F); + private static final short POWER_AVERAGING = 100; + private int powerIndex = 0; + + @NetworkData + private double powerAverage = 0; + + private final double power[] = new double[POWER_AVERAGING]; + + public TileLaser() { powerHandler = new PowerHandler(this, Type.MACHINE); initPowerProvider(); @@ -56,12 +68,23 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction powerHandler.setPerdition(PERDITION); } + @Override + public void initialize () { + super.initialize(); + + laser.head = new Position(xCoord, yCoord, zCoord); + laser.tail = new Position(xCoord + 2, yCoord + 2, zCoord + 2); + } + @Override public void updateEntity() { super.updateEntity(); - if (!!worldObj.isRemote) + laser.iterateTexture(); + + if (!!worldObj.isRemote) { return; + } // If a gate disabled us, remove laser and do nothing. if (lastMode == ActionMachineControl.Mode.Off) { @@ -89,9 +112,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction // We have a table and can work, so we create a laser if // necessary. - if (laser == null) { - createLaser(); - } + laser.isVisible = true; // We have a laser and may update it if (laser != null && canUpdateLaser()) { @@ -103,7 +124,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction laserTarget.receiveLaserEnergy(power); if (laser != null) { - laser.pushPower(power); + pushPower(power); } onPowerSent(power); @@ -128,8 +149,9 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction protected boolean isValidTable() { - if (laserTarget == null || laserTarget.isInvalidTarget() || !laserTarget.requiresLaserEnergy()) + if (laserTarget == null || laserTarget.isInvalidTarget() || !laserTarget.requiresLaserEnergy()) { return false; + } return true; } @@ -185,18 +207,13 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction } } - if (targets.isEmpty()) + if (targets.isEmpty()) { return; + } laserTarget = targets.get(worldObj.rand.nextInt(targets.size())); } - protected void createLaser() { - - laser = new EntityEnergyLaser(worldObj, new Position(xCoord, yCoord, zCoord), new Position(xCoord, yCoord, zCoord)); - worldObj.spawnEntityInWorld(laser); - } - protected void updateLaser() { int meta = getBlockMetadata(); @@ -229,18 +246,16 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction Position tail = new Position(laserTarget.getXCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F, laserTarget.getYCoord() + 9F / 16F, laserTarget.getZCoord() + 0.475 + (worldObj.rand.nextFloat() - 0.5) / 5F); - laser.setPositions(head, tail); + laser.head = head; + laser.tail = tail; - if (!laser.isVisible()) { - laser.show(); + if (!laser.isVisible) { + laser.isVisible = true; } } protected void removeLaser() { - if (laser != null) { - laser.setDead(); - laser = null; - } + laser.isVisible = false; } @Override @@ -308,4 +323,34 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction lastMode = ActionMachineControl.Mode.Off; } } + + private void pushPower(double received) { + powerAverage -= power[powerIndex]; + powerAverage += received; + power[powerIndex] = received; + powerIndex++; + + if (powerIndex == power.length) { + powerIndex = 0; + } + } + + public ResourceLocation getTexture() { + double avg = powerAverage / POWER_AVERAGING; + + if (avg <= 1.0) { + return EntityLaser.LASER_TEXTURES[0]; + } else if (avg <= 2.0) { + return EntityLaser.LASER_TEXTURES[1]; + } else if (avg <= 3.0) { + return EntityLaser.LASER_TEXTURES[2]; + } else { + return EntityLaser.LASER_TEXTURES[3]; + } + } + + @Override + public AxisAlignedBB getRenderBoundingBox() { + return new Box(this).extendToEncompass(laser.tail).getBoundingBox(); + } } diff --git a/common/buildcraft/silicon/render/RenderLaserBlock.java b/common/buildcraft/silicon/render/RenderLaserBlock.java new file mode 100755 index 00000000..2a4bca27 --- /dev/null +++ b/common/buildcraft/silicon/render/RenderLaserBlock.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team + * http://www.mod-buildcraft.com + * + * BuildCraft is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://www.mod-buildcraft.com/MMPL-1.0.txt + */ +package buildcraft.silicon.render; + +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; +import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; +import net.minecraft.tileentity.TileEntity; + +import org.lwjgl.opengl.GL11; + +import buildcraft.core.render.RenderLaser; +import buildcraft.silicon.TileLaser; + +public class RenderLaserBlock extends TileEntitySpecialRenderer { + + @Override + public void renderTileEntityAt(TileEntity tileentity, double x, double y, double z, float f) { + TileLaser laser = ((TileLaser) tileentity); + + if (laser != null) { + GL11.glPushMatrix(); + GL11.glPushAttrib(GL11.GL_ENABLE_BIT); + GL11.glEnable(GL11.GL_CULL_FACE); + GL11.glEnable(GL11.GL_LIGHTING); + GL11.glEnable(GL11.GL_BLEND); + GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + + GL11.glTranslated(x, y, z); + GL11.glTranslated(-tileentity.xCoord, -tileentity.yCoord, -tileentity.zCoord); + + GL11.glPushMatrix(); + RenderLaser.doRenderLaser(TileEntityRendererDispatcher.instance.field_147553_e, + laser.laser, laser.getTexture()); + GL11.glPopMatrix(); + + //GL11.glEnable(GL11.GL_LIGHTING); + GL11.glPopAttrib(); + GL11.glPopMatrix(); + } + } +}