Got modular battery working with UE

This commit is contained in:
Calclavia 2013-08-07 00:33:50 -04:00
parent c5bad9c491
commit a93571ad33
8 changed files with 122 additions and 113 deletions

@ -1 +1 @@
Subproject commit 688cfc13e8d045a25924c81c2e130dce1d3f842a
Subproject commit b3efc76d869dc6d9a104c9338cc1cf8c32e307b8

View file

@ -44,7 +44,7 @@ public class ItemCapacitor extends ItemUniversalElectric
@Override
public float getMaxElectricityStored(ItemStack theItem)
{
return 25;
return 20;
}
}

View file

@ -4,6 +4,7 @@
package resonantinduction.battery;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -13,25 +14,26 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.PacketHandler;
import resonantinduction.api.ITesla;
import resonantinduction.base.IPacketReceiver;
import resonantinduction.base.ListUtil;
import resonantinduction.tesla.TeslaGrid;
import universalelectricity.compatibility.TileEntityUniversalElectrical;
import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.core.item.IItemElectric;
import universalelectricity.core.vector.Vector3;
import com.google.common.io.ByteArrayDataInput;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
/**
* A modular battery with no GUI.
*
* @author AidanBrady
*/
public class TileEntityBattery extends TileEntityUniversalElectrical implements IPacketReceiver, IInventory, ITesla
public class TileEntityBattery extends TileEntityUniversalElectrical implements IPacketReceiver, IInventory
{
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
@ -46,18 +48,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
@Override
public void updateEntity()
{
ticks++;
super.updateEntity();
if (ticks == 1)
if (!this.worldObj.isRemote)
{
TeslaGrid.instance().register(this);
}
if (!worldObj.isRemote)
{
if (ticks == 5 && !structure.isMultiblock)
if (this.ticks == 5 && !structure.isMultiblock)
{
update();
this.update();
}
if (structure.visibleInventory[0] != null)
@ -80,7 +77,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
float batteryNeeded = battery.getMaxElectricityStored(itemStack) - battery.getElectricityStored(itemStack);
float toGive = Math.min(energyStored, Math.min(battery.getTransfer(itemStack), batteryNeeded));
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + removeEnergy(toGive, true));
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) + provideElectricity(toGive, true).getWatts());
}
if (structure.visibleInventory[2] != null)
@ -92,7 +89,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
float batteryStored = battery.getElectricityStored(itemStack);
float toReceive = Math.min(energyNeeded, Math.min(battery.getTransfer(itemStack), batteryStored));
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - addEnergy(toReceive, true));
battery.setElectricity(itemStack, battery.getElectricityStored(itemStack) - receiveElectricity(toReceive, true));
}
if (prevStructure != structure)
@ -114,6 +111,13 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
{
updateClient();
}
for (EntityPlayer player : this.playersUsing)
{
PacketDispatcher.sendPacketToPlayer(PacketHandler.getTileEntityPacket(this, this.getNetworkedData(new ArrayList()).toArray()), (Player) player);
}
this.produce();
}
}
@ -142,13 +146,6 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
}
}
@Override
public void invalidate()
{
TeslaGrid.instance().unregister(this);
super.invalidate();
}
@Override
public void readFromNBT(NBTTagCompound nbtTags)
{
@ -261,11 +258,10 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
}
}
/**
* @return added energy
*/
public float addEnergy(float amount, boolean doAdd)
@Override
public float receiveElectricity(ElectricityPack receive, boolean doAdd)
{
float amount = receive.getWatts();
float added = 0;
for (ItemStack itemStack : structure.inventory)
@ -294,15 +290,14 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return added;
}
/**
* @return removed energy
*/
public float removeEnergy(float amount, boolean doRemove)
@Override
public ElectricityPack provideElectricity(ElectricityPack pack, boolean doRemove)
{
float amount = pack.getWatts();
List<ItemStack> inverse = ListUtil.inverse(structure.inventory);
float removed = 0;
for (ItemStack itemStack : inverse)
{
if (itemStack.getItem() instanceof IItemElectric)
@ -326,7 +321,7 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
}
}
return removed;
return ElectricityPack.getFromWatts(removed, this.getVoltage());
}
@Override
@ -558,27 +553,21 @@ public class TileEntityBattery extends TileEntityUniversalElectrical implements
return false;
}
@Override
public float transfer(float transferEnergy, boolean doTransfer)
{
return addEnergy(transferEnergy, doTransfer);
}
@Override
public boolean canReceive(TileEntity transferTile)
{
return this.getMaxEnergyStored() - this.getEnergyStored() > 0;
}
@Override
public float getRequest(ForgeDirection direction)
{
return 0;
return this.getMaxEnergyStored() - this.getEnergyStored();
}
@Override
public float getProvide(ForgeDirection direction)
{
return 0;
return this.getEnergyStored();
}
@Override
public EnumSet<ForgeDirection> getOutputDirections()
{
return EnumSet.allOf(ForgeDirection.class);
}
}

View file

@ -9,13 +9,13 @@ import universalelectricity.core.vector.Vector3;
* @author Calclavia
*
*/
public class ThreadPathfinding extends Thread
public class ThreadEMPathfinding extends Thread
{
private boolean isCompleted = false;
private PathfinderEMContractor pathfinder;
private Vector3 start;
public ThreadPathfinding(PathfinderEMContractor pathfinder, Vector3 start)
public ThreadEMPathfinding(PathfinderEMContractor pathfinder, Vector3 start)
{
this.pathfinder = pathfinder;
this.start = start;

View file

@ -53,7 +53,7 @@ public class TileEntityEMContractor extends TileEntityAdvanced implements IPacke
/**
* Pathfinding
*/
private ThreadPathfinding thread;
private ThreadEMPathfinding thread;
private PathfinderEMContractor pathfinder;
private Set<EntityItem> pathfindingTrackers = new HashSet<EntityItem>();
private TileEntityEMContractor linked;
@ -142,24 +142,40 @@ public class TileEntityEMContractor extends TileEntityAdvanced implements IPacke
{
if (this.linked != null && !this.linked.isInvalid())
{
ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(this).translate(0.5), new Vector3(this).translate(new Vector3(this.getDirection())).translate(0.5), TileEntityTesla.dyeColors[dyeID]);
if (this.ticks % (2 + this.worldObj.rand.nextInt(2)) == 0)
{
ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(this).translate(0.5), new Vector3(this).translate(new Vector3(this.getDirection())).translate(0.5), TileEntityTesla.dyeColors[dyeID]);
}
if (this.pathfinder != null)
{
Vector3 lastTurn = null;
ForgeDirection lastDirection = null;
for (int i = 0; i < this.pathfinder.results.size(); i++)
{
Vector3 result = this.pathfinder.results.get(i).clone();
if (TileEntityEMContractor.canBePath(this.worldObj, result))
{
if (i - 1 >= 0)
{
Vector3 prevResult = this.pathfinder.results.get(i - 1).clone();
ResonantInduction.proxy.renderElectricShock(this.worldObj, prevResult.clone().translate(0.5), result.clone().translate(0.5), TileEntityTesla.dyeColors[dyeID]);
Vector3 difference = prevResult.clone().difference(result);
final ForgeDirection direction = difference.toForgeDirection();
if (this.ticks % (2 + this.worldObj.rand.nextInt(2)) == 0)
{
ResonantInduction.proxy.renderElectricShock(this.worldObj, prevResult.clone().translate(0.5), result.clone().translate(0.5), TileEntityTesla.dyeColors[dyeID]);
}
if (lastTurn == null || direction != lastDirection)
{
lastTurn = result;
lastDirection = direction;
}
AxisAlignedBB bounds = AxisAlignedBB.getAABBPool().getAABB(result.x, result.y, result.z, result.x + 1, result.y + 1, result.z + 1);
List<EntityItem> entities = this.worldObj.getEntitiesWithinAABB(EntityItem.class, bounds);
@ -538,7 +554,7 @@ public class TileEntityEMContractor extends TileEntityAdvanced implements IPacke
{
if (TileEntityEMContractor.canBePath(this.worldObj, start) && TileEntityEMContractor.canBePath(this.worldObj, target))
{
this.thread = new ThreadPathfinding(new PathfinderEMContractor(this.worldObj, target), start);
this.thread = new ThreadEMPathfinding(new PathfinderEMContractor(this.worldObj, target), start);
this.thread.start();
}
}

View file

@ -159,7 +159,7 @@ public class FXElectricBolt extends EntityFX
*/
for (int i = 1; i < splitAmount; i++)
{
Vector3 newOffset = segment.difference.getPerpendicular().rotate(this.rand.nextFloat() * 360, segment.difference).scale((this.rand.nextFloat() - 0.5F) * offset);
Vector3 newOffset = segment.difference.getPerpendicular().rotate(this.rand.nextFloat() * 360, segment.difference).clone().scale((this.rand.nextFloat() - 0.5F) * offset);
Vector3 basePoint = startPoint.clone().translate(subSegment.clone().scale(i));
newPoints[i] = new BoltPoint(basePoint, newOffset);
@ -178,7 +178,7 @@ public class FXElectricBolt extends EntityFX
if ((i != 0) && (this.rand.nextFloat() < splitChance))
{
Vector3 splitrot = next.difference.xCrossProduct().rotate(this.rand.nextFloat() * 360, next.difference);
Vector3 diff = next.difference.clone().rotate((this.rand.nextFloat() * 0.66F + 0.33F) * splitAngle, splitrot).scale(splitLength);
Vector3 diff = next.difference.clone().rotate((this.rand.nextFloat() * 0.66F + 0.33F) * splitAngle, splitrot).clone().scale(splitLength);
this.maxSplitID += 1;
this.parentIDMap.put(this.maxSplitID, next.splitID);
BoltSegment split = new BoltSegment(newPoints[i], new BoltPoint(newPoints[(i + 1)].base, newPoints[(i + 1)].offset.clone().translate(diff)), segment.alpha / 2f, next.id, this.maxSplitID);
@ -303,54 +303,49 @@ public class FXElectricBolt extends EntityFX
if (segment != null && segment.id <= renderlength)
{
double renderWidth = this.boltWidth * ((new Vector3(player).distance(segment.start) / 5f + 1f) * (1 + segment.alpha) * 0.5f);
renderWidth = Math.min(this.boltWidth, Math.max(renderWidth, 0));
if (segment.difference.getMagnitude() > 0 && segment.difference.getMagnitude() != Double.NaN && segment.difference.getMagnitude() != Double.POSITIVE_INFINITY && renderWidth > 0 && renderWidth != Double.NaN && renderWidth != Double.POSITIVE_INFINITY)
Vector3 diffPrev = playerVector.crossProduct(segment.prevDiff).scale(renderWidth / segment.sinPrev);
Vector3 diffNext = playerVector.crossProduct(segment.nextDiff).scale(renderWidth / segment.sinNext);
Vector3 startVec = segment.start;
Vector3 endVec = segment.end;
float rx1 = (float) (startVec.x - interpPosX);
float ry1 = (float) (startVec.y - interpPosY);
float rz1 = (float) (startVec.z - interpPosZ);
float rx2 = (float) (endVec.x - interpPosX);
float ry2 = (float) (endVec.y - interpPosY);
float rz2 = (float) (endVec.z - interpPosZ);
tessellator.setColorRGBA_F(this.particleRed, this.particleGreen, this.particleBlue, (1.0F - (this.particleAge >= 0 ? ((float) this.particleAge / (float) this.particleMaxAge) : 0.0F) * 0.6f) * segment.alpha);
tessellator.addVertexWithUV(rx2 - diffNext.x, ry2 - diffNext.y, rz2 - diffNext.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx1 - diffPrev.x, ry1 - diffPrev.y, rz1 - diffPrev.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx1 + diffPrev.x, ry1 + diffPrev.y, rz1 + diffPrev.z, 0.5D, 1.0D);
tessellator.addVertexWithUV(rx2 + diffNext.x, ry2 + diffNext.y, rz2 + diffNext.z, 0.5D, 1.0D);
/**
* Render the bolts balls.
*/
if (segment.next == null)
{
Vector3 diffPrev = playerVector.crossProduct(segment.prevDiff).scale(renderWidth / segment.sinPrev);
Vector3 diffNext = playerVector.crossProduct(segment.nextDiff).scale(renderWidth / segment.sinNext);
Vector3 startVec = segment.start;
Vector3 endVec = segment.end;
float rx1 = (float) (startVec.x - interpPosX);
float ry1 = (float) (startVec.y - interpPosY);
float rz1 = (float) (startVec.z - interpPosZ);
float rx2 = (float) (endVec.x - interpPosX);
float ry2 = (float) (endVec.y - interpPosY);
float rz2 = (float) (endVec.z - interpPosZ);
tessellator.setColorRGBA_F(this.particleRed, this.particleGreen, this.particleBlue, (1.0F - (this.particleAge >= 0 ? ((float) this.particleAge / (float) this.particleMaxAge) : 0.0F) * 0.6f) * segment.alpha);
Vector3 roundEnd = segment.end.clone().translate(segment.difference.clone().normalize().scale(renderWidth));
float rx3 = (float) (roundEnd.x - interpPosX);
float ry3 = (float) (roundEnd.y - interpPosY);
float rz3 = (float) (roundEnd.z - interpPosZ);
tessellator.addVertexWithUV(rx3 - diffNext.x, ry3 - diffNext.y, rz3 - diffNext.z, 0.0D, 0.0D);
tessellator.addVertexWithUV(rx2 - diffNext.x, ry2 - diffNext.y, rz2 - diffNext.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx1 - diffPrev.x, ry1 - diffPrev.y, rz1 - diffPrev.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx1 + diffPrev.x, ry1 + diffPrev.y, rz1 + diffPrev.z, 0.5D, 1.0D);
tessellator.addVertexWithUV(rx2 + diffNext.x, ry2 + diffNext.y, rz2 + diffNext.z, 0.5D, 1.0D);
tessellator.addVertexWithUV(rx3 + diffNext.x, ry3 + diffNext.y, rz3 + diffNext.z, 0.0D, 1.0D);
}
/**
* Render the bolts balls.
*/
if (segment.next == null)
{
Vector3 roundEnd = segment.end.clone().translate(segment.difference.clone().normalize().scale(renderWidth));
float rx3 = (float) (roundEnd.x - interpPosX);
float ry3 = (float) (roundEnd.y - interpPosY);
float rz3 = (float) (roundEnd.z - interpPosZ);
tessellator.addVertexWithUV(rx3 - diffNext.x, ry3 - diffNext.y, rz3 - diffNext.z, 0.0D, 0.0D);
tessellator.addVertexWithUV(rx2 - diffNext.x, ry2 - diffNext.y, rz2 - diffNext.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx2 + diffNext.x, ry2 + diffNext.y, rz2 + diffNext.z, 0.5D, 1.0D);
tessellator.addVertexWithUV(rx3 + diffNext.x, ry3 + diffNext.y, rz3 + diffNext.z, 0.0D, 1.0D);
}
if (segment.prev == null)
{
Vector3 roundEnd = segment.start.clone().difference(segment.difference.clone().normalize().scale(renderWidth));
float rx3 = (float) (roundEnd.x - interpPosX);
float ry3 = (float) (roundEnd.y - interpPosY);
float rz3 = (float) (roundEnd.z - interpPosZ);
tessellator.addVertexWithUV(rx1 - diffPrev.x, ry1 - diffPrev.y, rz1 - diffPrev.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx3 - diffPrev.x, ry3 - diffPrev.y, rz3 - diffPrev.z, 0.0D, 0.0D);
tessellator.addVertexWithUV(rx3 + diffPrev.x, ry3 + diffPrev.y, rz3 + diffPrev.z, 0.0D, 1.0D);
tessellator.addVertexWithUV(rx1 + diffPrev.x, ry1 + diffPrev.y, rz1 + diffPrev.z, 0.5D, 1.0D);
}
if (segment.prev == null)
{
Vector3 roundEnd = segment.start.clone().difference(segment.difference.clone().normalize().scale(renderWidth));
float rx3 = (float) (roundEnd.x - interpPosX);
float ry3 = (float) (roundEnd.y - interpPosY);
float rz3 = (float) (roundEnd.z - interpPosZ);
tessellator.addVertexWithUV(rx1 - diffPrev.x, ry1 - diffPrev.y, rz1 - diffPrev.z, 0.5D, 0.0D);
tessellator.addVertexWithUV(rx3 - diffPrev.x, ry3 - diffPrev.y, rz3 - diffPrev.z, 0.0D, 0.0D);
tessellator.addVertexWithUV(rx3 + diffPrev.x, ry3 + diffPrev.y, rz3 + diffPrev.z, 0.0D, 1.0D);
tessellator.addVertexWithUV(rx1 + diffPrev.x, ry1 + diffPrev.y, rz1 + diffPrev.z, 0.5D, 1.0D);
}
}
}
@ -425,7 +420,7 @@ public class FXElectricBolt extends EntityFX
Vector3 prevDiffNorm = this.prev.difference.clone().normalize();
Vector3 diffNorm = this.difference.clone().normalize();
this.prevDiff = diffNorm.clone().translate(prevDiffNorm).normalize();
this.sinPrev = Math.sin(diffNorm.anglePreNorm(prevDiffNorm.scale(-1)) / 2);
this.sinPrev = Math.sin(diffNorm.anglePreNorm(prevDiffNorm.clone().scale(-1)) / 2);
}
else
{
@ -438,7 +433,7 @@ public class FXElectricBolt extends EntityFX
Vector3 nextDiffNorm = this.next.difference.clone().normalize();
Vector3 diffNorm = this.difference.clone().normalize();
this.nextDiff = diffNorm.clone().translate(nextDiffNorm).normalize();
this.sinNext = Math.sin(diffNorm.anglePreNorm(nextDiffNorm.scale(-1)) / 2);
this.sinNext = Math.sin(diffNorm.anglePreNorm(nextDiffNorm.clone().scale(-1)) / 2);
}
else
{

View file

@ -5,6 +5,7 @@ package resonantinduction.render;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
@ -87,8 +88,9 @@ public class RenderBattery extends TileEntitySpecialRenderer
for (int slot = 0; slot < 4; slot++)
{
Vector3 sideVec = new Vector3(t).modifyPositionFromSide(correctSide(direction));
Block block = Block.blocksList[sideVec.getBlockID(t.worldObj)];
if (!t.worldObj.isAirBlock((int) sideVec.x, (int) sideVec.y, (int) sideVec.z))
if (block != null && block.isOpaqueCube())
{
continue;
}

View file

@ -84,7 +84,6 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
super.updateEntity();
boolean doPacketUpdate = this.getEnergyStored() > 0;
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord);
/**
* Only transfer if it is the bottom controlling Tesla tower.
@ -121,7 +120,7 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
/**
* Make sure Tesla is not part of this tower.
*/
if (!this.connectedTeslas.contains(tesla) && tesla.canReceive(this) && tileEntity != tesla && !(tileEntity instanceof TileEntityBattery && ((TileEntityBattery) tileEntity).structure.locations.contains(new Vector3((TileEntity) tesla))))
if (!this.connectedTeslas.contains(tesla) && tesla.canReceive(this))
{
if (tesla instanceof TileEntityTesla)
{
@ -220,11 +219,13 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
}
/**
* Draws power from furnace below it. TODO: MAKE UNIVERSAL
* Draws power from furnace below it.
*
* @author Calclavia
*/
if (tileEntity instanceof TileEntityFurnace)
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord - 1, this.zCoord);
if (tileEntity instanceof TileEntityFurnace && this.getRequest(ForgeDirection.DOWN) > 0)
{
TileEntityFurnace furnaceTile = (TileEntityFurnace) tileEntity;
@ -272,10 +273,11 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
BlockFurnace.updateFurnaceBlockState(furnaceTile.furnaceBurnTime > 0, furnaceTile.worldObj, furnaceTile.xCoord, furnaceTile.yCoord, furnaceTile.zCoord);
}
}
}
else if (tileEntity instanceof TileEntityBattery && this.canReceive)
{
this.transfer(((TileEntityBattery) tileEntity).removeEnergy(TRANSFER_CAP, true), true);
if (this.ticks % 20 == 0)
{
this.produce();
}
}
if (!this.worldObj.isRemote && this.getEnergyStored() > 0 != doPacketUpdate)
@ -561,13 +563,18 @@ public class TileEntityTesla extends TileEntityUniversalElectrical implements IT
@Override
public float getRequest(ForgeDirection direction)
{
return 0;
return this.getMaxEnergyStored() - this.getEnergyStored();
}
@Override
public float getProvide(ForgeDirection direction)
{
return this.getEnergyStored();
if (direction != ForgeDirection.UP && direction != ForgeDirection.DOWN)
{
return this.getEnergyStored();
}
return 0;
}
@Override