Skip to content
Permalink
Browse files

Unified the electric arc system for all electronic devices.

  • Loading branch information...
Ri5ux Ri5ux
Ri5ux authored and Ri5ux committed Jul 16, 2019
1 parent 4a1c077 commit 1084da5d9a82125dcde7c52121d8e826669e2056
Showing with 182 additions and 165 deletions.
  1. +181 −33 src/main/java/org/avp/tile/TileEntityElectrical.java
  2. +1 −132 src/main/java/org/avp/tile/TileEntityPowerline.java
@@ -1,33 +1,48 @@
package org.avp.tile;

import java.util.Random;

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

import com.asx.mdx.lib.client.entityfx.EntityFXElectricArc;
import com.asx.mdx.lib.util.Game;
import com.asx.mdx.lib.util.MDXMath;
import com.asx.mdx.lib.world.Pos;
import com.asx.mdx.lib.world.entity.Entities;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;

import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public abstract class TileEntityElectrical extends TileEntity implements ITickable
{
protected double voltage;
protected double voltagePrev;
protected double srcVoltage;
protected double thresholdVoltage;
protected double resistance;
protected double boost;
protected int updateFrequency;
protected double voltage;
protected double voltagePrev;
protected double srcVoltage;
protected double thresholdVoltage;
protected double resistance;
protected double boost;
protected int updateFrequency;
protected boolean isSrc;

public TileEntityElectrical(boolean isSource)
{
this.isSrc = isSource;
this.thresholdVoltage = 110;
this.srcVoltage = 120;
this.updateFrequency = 50;
/** 1000 / 50Hz = 20 Ticks **/
this.updateFrequency = 50;/** 1000 / 50Hz = 20 Ticks **/
this.resistance = 0.1;
this.boost = 0;
}
@@ -58,7 +73,7 @@ public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setDouble("voltage", this.voltage);

return nbt;
}

@@ -89,8 +104,9 @@ public double getResistance()
}

/**
* @param resistance - The amount of resistance this component will apply
* on components extracting energy from it.
* @param resistance
* - The amount of resistance this component will apply on components extracting energy
* from it.
*/
public void setResistance(double resistance)
{
@@ -106,16 +122,17 @@ public double getThresholdVoltage()
}

/**
* @param thresholdVoltage - The threshold voltage required for this
* component to operate.
* @param thresholdVoltage
* - The threshold voltage required for this component to operate.
*/
public void setThresholdVoltage(double thresholdVoltage)
{
this.thresholdVoltage = thresholdVoltage;
}

/**
* @param side - EnumFacing from the receiver.
* @param side
* - EnumFacing from the receiver.
* @return If this side can provide energy to the receiver.
*/
public boolean canProvideEnergyToReceiver(EnumFacing side)
@@ -132,7 +149,8 @@ public double getVoltage()
}

/**
* @param voltage - The amount of voltage this component should contain.
* @param voltage
* - The amount of voltage this component should contain.
*/
public void setVoltage(double voltage)
{
@@ -148,7 +166,8 @@ public double getBoost()
}

/**
* @param rf - The amount of boost this component should contain.
* @param rf
* - The amount of boost this component should contain.
*/
public void setBoost(double boost)
{
@@ -164,7 +183,8 @@ public int getUpdateFrequency()
}

/**
* @param freq - The rate at which this source component should update its voltage.
* @param freq
* - The rate at which this source component should update its voltage.
*/
public void setUpdateFrequency(int freq)
{
@@ -188,7 +208,8 @@ public EnumFacing getSourcePowerDirection()
}

/**
* @param srcVoltage - The voltage this source component should provide.
* @param srcVoltage
* - The voltage this source component should provide.
*/
public void setSourceVoltage(double srcVoltage)
{
@@ -204,13 +225,14 @@ public boolean isSource()
}

/**
* @param isSrc - Set true if this should be a source component.
* @param isSrc
* - Set true if this should be a source component.
*/
public void setIsSource(boolean isSrc)
{
this.isSrc = isSrc;
}

@Override
public void update()
{
@@ -275,9 +297,12 @@ else if (tee.getVoltage() > 0 && tee.getBoost() != 0 && direction == tee.getSour
/**
* Returns the amount of energy to be extracted from this component.
*
* @param from - The direction this request was sent from.
* @param maxExtract - The amount of energy we're trying to extract.
* @param simulate - If true, this request will only be simulated.
* @param from
* - The direction this request was sent from.
* @param maxExtract
* - The amount of energy we're trying to extract.
* @param simulate
* - If true, this request will only be simulated.
* @return - The amount of energy to be extracted.
*/
public double extractVoltage(EnumFacing from, double maxExtract, boolean simulate)
@@ -293,14 +318,17 @@ public double extractVoltage(EnumFacing from, double maxExtract, boolean simulat
}

/**
* Returns the amount of energy this component will contain after adding
* the specified amount of energy.
* Returns the amount of energy this component will contain after adding the specified amount of
* energy.
*
* @param from - The direction this request was sent from.
* @param maxReceive - The amount of energy this component is receiving.
* @param simulate - If true, this request will only be simulated.
* @return The amount of energy this component will contain after adding
* the specified amount of energy.
* @param from
* - The direction this request was sent from.
* @param maxReceive
* - The amount of energy this component is receiving.
* @param simulate
* - If true, this request will only be simulated.
* @return The amount of energy this component will contain after adding the specified amount of
* energy.
*/
public double receiveVoltage(EnumFacing from, double maxReceive, boolean simulate)
{
@@ -317,9 +345,129 @@ public double receiveVoltage(EnumFacing from, double maxReceive, boolean simulat
}
return this.voltage;
}

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

public int getNearbyConnectionCount()
{
int connections = 0;

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

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

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

if (electrical.canProvideEnergyToReceiver(direction) && provider.canConnectPower(direction))
{
connections++;
}
}

if (electrical instanceof IVoltageReceiver)
{
if (electrical.getVoltage() < this.getVoltage())
{
connections++;
}
}
}
}

return connections;
}

public boolean canArc()
{
if (this.voltage > 600 && this.world.getWorldTime() % 8 == 0)
{
return getNearbyConnectionCount() <= 1;
}

return false;
}

public Pos getArcOrigin()
{
Pos pos = new Pos(this.pos);
return pos.add(0.5D, 0.5D, 0.5D);
}

public void tryElectricArc()
{
if (canArc())
{
Random rand = new Random();
double maxArcDist = (this.getVoltage() / 600);
float m = 1.75F;
Pos origin = this.getArcOrigin();
Pos t = getNextGroundingPoint(this, origin.add(0, 1, 0), this.world);
Entity target = Entities.getRandomEntityInCoordsRange(world, EntityLivingBase.class, new Pos(this), (int) Math.floor(maxArcDist));
double distG = origin.y - t.y;
double distE = 1000;
double dist = distG;
double arcWidth = MDXMath.map((double) this.voltage, 600D, 10000D, 0.05D, 0.5D);
float damageMult = (float) (arcWidth * 100F);

if (target != null)
{
Pos p = new Pos(target.getPosition()).add(target.width / 2, 0, target.width / 2);
distE = target.getDistance(origin.x, origin.y, origin.z);

if (distE <= distG && distE <= maxArcDist)
{
t = p;
m = 8F;
dist = distE;
target.attackEntityFrom(DamageSources.electricity, damageMult);
target.setFire(3);
}
}

if (dist <= maxArcDist)
{
if (FMLCommonHandler.instance().getSide() == Side.CLIENT)
{
this.spawnArc(t, m, rand, dist, origin, arcWidth);
}
}
}
}

@SideOnly(Side.CLIENT)
public void spawnArc(Pos t, float m, Random rand, double dist, Pos origin, double arcWidth)
{
float targetX = (float) (t.x + (rand.nextFloat() / m) - (rand.nextFloat() / m));
float targetY = (float) (t.y + 1);
float targetZ = (float) (t.z + (rand.nextFloat() / m) - (rand.nextFloat() / m));
int age = 4;
int color = 0xFF8866CC;
EntityFXElectricArc arc = new EntityFXElectricArc(world, origin.x, origin.y, origin.z, targetX, targetY, targetZ, age, 0.24F * dist, 0.1F, (float) arcWidth, color);
Game.minecraft().effectRenderer.addEffect(arc);
}

public static Pos getNextGroundingPoint(TileEntityElectrical electrical, Pos pos, World world)
{
for (int y = (int) pos.y - 1; y > 0; y--)
{
Pos position = new Pos(pos.x, y - 1, pos.z);
Block b = position.getBlock(world);

if (b != net.minecraft.init.Blocks.AIR)
{
return position;
}
}

return pos;
}
}

0 comments on commit 1084da5

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