Skip to content
Permalink
Browse files

Powerlines now arc if the voltage is above 600V. Arc intensity depend…

…ant on voltage.
  • Loading branch information...
Ri5ux Ri5ux
Ri5ux authored and Ri5ux committed Jul 15, 2019
1 parent 247dcd1 commit 5724b3aecff8aa7bd9a660ef0e389765a58e5424
Showing with 104 additions and 2 deletions.
  1. +104 −2 src/main/java/org/avp/tile/TileEntityPowerline.java
@@ -1,11 +1,24 @@
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 org.avp.block.BlockPowerline;

import net.minecraft.util.EnumFacing;

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.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

public class TileEntityPowerline extends TileEntityElectrical implements IVoltageProvider, IVoltageReceiver
{
@@ -19,6 +32,95 @@ public void update()
{
super.update();
this.updateEnergyAsReceiver();

if (this.voltage > 600 && this.world.getWorldTime() % 8 == 0)
{
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) && electrical.getVoltage() < this.getVoltage())
{
connections++;
}
}
}
}

boolean canArc = connections < 1;

if (canArc)
{
Random rand = new Random();
double maxArcDist = (this.getVoltage() / 600);
float m = 1.75F;
double x = this.pos.getX() + 0.5;
double y = this.pos.getY() + 0.5;
double z = this.pos.getZ() + 0.5;
Pos t = getNextGroundingPoint(new Pos(x, y + 1, z), this.world);
Entity target = Entities.getRandomEntityInCoordsRange(world, EntityLivingBase.class, new Pos(this), (int) Math.round(maxArcDist));
double dist = y - t.y;
double arcWidth = MDXMath.map((double) this.voltage, 600D, 10000D, 0.05D, 0.5D);
float damageMult = (float) (arcWidth * 20F);

if (target != null)
{
t = new Pos(target.getPosition()).add(target.width / 2, 0, target.width / 2);
m = 8F;
dist = target.getDistance(x, y, z);
target.attackEntityFrom(DamageSources.electricity, damageMult);
target.setFire(3);
}

if (dist <= maxArcDist)
{
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, x, y, z, targetX, targetY, targetZ, age, 0.24F * dist, 0.1F, (float) arcWidth, color);
Game.minecraft().effectRenderer.addEffect(arc);
}
}
}
}

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

if (isPositionAcceptable(position, world))
{
return position;
}
}

return pos;
}

public static boolean isPositionAcceptable(Pos pos, World world)
{
if (pos != null && world != null)
{
Pos newPos = new Pos(pos.x, pos.y, pos.z);
Block b = newPos.getBlock(world);
return b != net.minecraft.init.Blocks.AIR && !(b instanceof BlockPowerline);
}

return false;
}

@Override

0 comments on commit 5724b3a

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