Skip to content
Permalink
Browse files

Fixed a power loop with transformers caused by the acceptance of powe…

…r from all sides.
  • Loading branch information...
Ri5ux Ri5ux
Ri5ux authored and Ri5ux committed Jul 21, 2019
1 parent 8b034a6 commit 4ef1c11fd0cafe0e8423d50c4692277db872c6fc
@@ -1,12 +1,14 @@
package org.avp.client.model.tile;

import org.avp.api.power.IPowerConnection;
import org.avp.tile.TileEntityElectrical;

import com.asx.mdx.lib.client.util.models.Model;

import net.minecraft.client.model.ModelRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;


public class ModelCable extends Model
@@ -54,6 +56,15 @@ public ModelCable()
this.cableNorth.setRotationPoint(0.0F, 0.0F, 0.0F);
this.cableNorth.addBox(-0.5F, 0.0F, -8.0F, 1, 1, 7, 0.0F);
}

private boolean shouldHideCableInDirection(TileEntity tile, EnumFacing facing, BlockPos pos)
{
TileEntity side = tile.getWorld().getTileEntity(pos);
boolean canProvideToReceiver = (side instanceof TileEntityElectrical && ((TileEntityElectrical) side).canProvideEnergyToReceiver(facing));
boolean canAcceptFromSide = (side instanceof IPowerConnection && ((IPowerConnection) side).canConnectPower(facing));

return !(canProvideToReceiver || canAcceptFromSide);
}

@Override
public void render(Object obj)
@@ -73,23 +84,12 @@ public void render(Object obj)

if (tile != null)
{
TileEntity leftTile = tile.getWorld().getTileEntity(tile.getPos().add(1, 0, 0));
cableWest.isHidden = !(leftTile instanceof TileEntityElectrical && ((TileEntityElectrical) leftTile).canProvideEnergyToReceiver(EnumFacing.EAST));

TileEntity rightTile = tile.getWorld().getTileEntity(tile.getPos().add(-1, 0, 0));
cableEast.isHidden = !(rightTile instanceof TileEntityElectrical && ((TileEntityElectrical) rightTile).canProvideEnergyToReceiver(EnumFacing.WEST));

TileEntity topTile = tile.getWorld().getTileEntity(tile.getPos().add(0, 1, 0));
cableTop.isHidden = !(topTile instanceof TileEntityElectrical && ((TileEntityElectrical) topTile).canProvideEnergyToReceiver(EnumFacing.UP));

TileEntity bottomTile = tile.getWorld().getTileEntity(tile.getPos().add(0, -1, 0));
cableBottom.isHidden = !(bottomTile instanceof TileEntityElectrical && ((TileEntityElectrical) bottomTile).canProvideEnergyToReceiver(EnumFacing.DOWN));

TileEntity backTile = tile.getWorld().getTileEntity(tile.getPos().add(0, 0, 1));
cableSouth.isHidden = !(backTile instanceof TileEntityElectrical && ((TileEntityElectrical) backTile).canProvideEnergyToReceiver(EnumFacing.NORTH));

TileEntity frontTile = tile.getWorld().getTileEntity(tile.getPos().add(0, 0, -1));
cableNorth.isHidden = !(frontTile instanceof TileEntityElectrical && ((TileEntityElectrical) frontTile).canProvideEnergyToReceiver(EnumFacing.SOUTH));
cableWest.isHidden = shouldHideCableInDirection(tile, EnumFacing.EAST, tile.getPos().add(1, 0, 0));
cableEast.isHidden = shouldHideCableInDirection(tile, EnumFacing.WEST, tile.getPos().add(-1, 0, 0));
cableTop.isHidden = shouldHideCableInDirection(tile, EnumFacing.UP, tile.getPos().add(0, 1, 0));
cableBottom.isHidden = shouldHideCableInDirection(tile, EnumFacing.DOWN, tile.getPos().add(0, -1, 0));
cableSouth.isHidden = shouldHideCableInDirection(tile, EnumFacing.NORTH, tile.getPos().add(0, 0, 1));
cableNorth.isHidden = shouldHideCableInDirection(tile, EnumFacing.SOUTH, tile.getPos().add(0, 0, -1));

nodeX.isHidden = cableEast.isHidden && cableWest.isHidden;
nodeY.isHidden = cableTop.isHidden && cableBottom.isHidden;
@@ -3,10 +3,13 @@
import org.avp.AliensVsPredator;
import org.avp.tile.TileEntityTransformer;

import com.asx.mdx.lib.client.util.Draw;
import com.asx.mdx.lib.client.util.OpenGL;
import com.asx.mdx.lib.util.Game;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;


@@ -49,6 +52,39 @@ public void render(TileEntityTransformer tile, double x, double y, double z, flo
AliensVsPredator.resources().models().TRANSFORMER.draw(transformer);
}
OpenGL.popMatrix();

OpenGL.pushMatrix();
if (Game.minecraft().objectMouseOver != null && Game.minecraft().objectMouseOver.getBlockPos() != null)
{
TileEntity tileOver = Game.minecraft().player.world.getTileEntity(Game.minecraft().objectMouseOver.getBlockPos());
OpenGL.translate(x+ 0.5F, y + 2F, z + 0.5F);

if (tileOver != null && tileOver == tile)
{
float scale = 0.02F;
OpenGL.scale(scale, scale, scale);
OpenGL.disableLight();

OpenGL.pushMatrix();
{
OpenGL.rotate(Game.minecraft().player.rotationYaw + 180, 0F, -1F, 0F);

OpenGL.pushMatrix();
{
OpenGL.rotate(Game.minecraft().player.rotationPitch + 180, -1F, 0F, 0F);
Draw.drawString(((float) tile.getVoltage()) + "V", -20, 30, tile.getVoltage() <= 0 ? 0xFFFF0000 : 0xFF00FF00);
scale = 0.5F;
OpenGL.scale(scale, scale, scale);
Draw.drawString((tile + "").replace(tile.getClass().getName(), ""), -20, 80, 0xFF00AAFF);
}
OpenGL.popMatrix();
}
OpenGL.popMatrix();

OpenGL.enableLight();
}
}
OpenGL.popMatrix();
}
}
}
@@ -3,6 +3,7 @@
import java.util.Random;

import org.avp.DamageSources;
import org.avp.api.power.IPowerConnection;
import org.avp.api.power.IVoltageProvider;
import org.avp.api.power.IVoltageReceiver;

@@ -26,7 +27,7 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class TileEntityElectrical extends TileEntity implements ITickable
public abstract class TileEntityElectrical extends TileEntity implements ITickable, IPowerConnection
{
protected double voltage;
protected double voltagePrev;
@@ -246,43 +247,43 @@ public void updateEnergyAsReceiver()
{
this.voltagePrev = this.voltage;

TileEntity surroundingTile = null;

for (EnumFacing direction : EnumFacing.VALUES)
{
TileEntity tile = this.world.getTileEntity(this.getPos().offset(direction));
TileEntity t = this.world.getTileEntity(this.getPos().offset(direction));

if (tile != null && tile instanceof TileEntityElectrical)
if (t != null && t instanceof TileEntityElectrical)
{
TileEntityElectrical electrical = (TileEntityElectrical) tile;
TileEntityElectrical e = (TileEntityElectrical) t;

if (electrical instanceof IVoltageProvider)
if (e instanceof IVoltageProvider)
{
IVoltageProvider provider = (IVoltageProvider) electrical;
IVoltageProvider provider = (IVoltageProvider) e;

if (electrical.canProvideEnergyToReceiver(direction) && provider.canConnectPower(direction) && electrical.getVoltage() > this.getVoltage())
/** Check if we can pull power from surrounding providers **/
if (e.canProvideEnergyToReceiver(direction) && provider.canConnectPower(direction))
{
this.receiveVoltage(direction.getOpposite(), provider.extractVoltage(direction.getOpposite(), electrical.getVoltage() - this.getVoltage(), false), false);
/** Make sure this receiver can accept power on the side we're pulling power from **/
if (this.canReceiveVoltageFromSide(direction))
{
/** Make sure the provider has a higher voltage than this receiver does **/
if (e.getVoltage() > this.getVoltage())
{
double newVoltage = provider.extractVoltage(direction.getOpposite(), e.getVoltage() - this.getVoltage(), false);
this.receiveVoltage(direction.getOpposite(), newVoltage, false);
}
}
}
}
}
}

TileEntity surroundingTile = null;

for (EnumFacing direction : EnumFacing.VALUES)
{
TileEntity tile = this.world.getTileEntity(this.getPos().offset(direction));

if (tile != null && tile instanceof TileEntityElectrical)
{
TileEntityElectrical tee = (TileEntityElectrical) tile;

if (tee.getBoost() == 0 && tee.getVoltage() > this.getVoltage() && tile instanceof IVoltageProvider)
if (e.getBoost() == 0 && e.getVoltage() > this.getVoltage() && t instanceof IVoltageProvider)
{
surroundingTile = tile;
surroundingTile = t;
}
else if (tee.getVoltage() > 0 && tee.getBoost() != 0 && direction == tee.getSourcePowerDirection())
else if (e.getVoltage() > 0 && e.getBoost() != 0 && direction == e.getSourcePowerDirection())
{
surroundingTile = tile;
surroundingTile = t;
}
}
}
@@ -291,7 +292,6 @@ else if (tee.getVoltage() > 0 && tee.getBoost() != 0 && direction == tee.getSour
{
this.setVoltage(0);
}

}

/**
@@ -345,12 +345,18 @@ public double receiveVoltage(EnumFacing from, double maxReceive, boolean simulat
}
return this.voltage;
}

@Override
public boolean canConnectPower(EnumFacing from)
{
return true;
}

public boolean canReceiveVoltageFromSide(EnumFacing from)
{
return true;
return this.canConnectPower(from);
}

public int getNearbyConnectionCount()
{
int connections = 0;
@@ -382,7 +388,7 @@ public int getNearbyConnectionCount()
}
}
}

return connections;
}

@@ -395,7 +401,7 @@ public boolean canArc()

return false;
}

public Pos getArcOrigin()
{
Pos pos = new Pos(this.pos);
@@ -11,7 +11,6 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;


public class TileEntityTransformer extends TileEntityElectrical implements IVoltageProvider, IVoltageReceiver, IRotatableYAxis
{
private EnumFacing direction;
@@ -100,7 +99,13 @@ public EnumFacing getSourcePowerDirection()
@Override
public boolean canConnectPower(EnumFacing from)
{
return from == direction;
return from == direction || from == direction.getOpposite();
}

@Override
public boolean canProvideEnergyToReceiver(EnumFacing side)
{
return side == direction;
}

@Override
@@ -121,13 +126,6 @@ public double extractVoltage(EnumFacing from, double maxExtract, boolean simulat

return 0;
}

@Override
public boolean canProvideEnergyToReceiver(EnumFacing side)
{
return side == direction || side == direction.getOpposite();
}

@Override
public double getMaxVoltage(EnumFacing from)
{

0 comments on commit 4ef1c11

Please sign in to comment.
You can’t perform that action at this time.