Discharges can fail if the total voltage is too low

Added Albedo compat for the marx generator
This commit is contained in:
malte0811 2017-08-15 21:37:53 +02:00
parent 2b2f5ca6c1
commit 505464c002
5 changed files with 211 additions and 140 deletions

View file

@ -30,7 +30,7 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8
minecraft {
version = "14.22.0.2453"
version = "14.21.1.2420"
runDir = "run"
replace '${version}', project.version
@ -43,14 +43,18 @@ repositories {
url 'http://maven.ic2.player.to/'
}
maven {
name 'jared maven'
url 'http://blamejared.com/maven'
name 'jared maven'
url 'http://blamejared.com/maven'
}
maven { // Albedo Lights
url 'https://repo.elytradev.com/'
}
}
dependencies {
compile "net.industrial-craft:industrialcraft-2:2.8.+"
deobfCompile "blusunrize:ImmersiveEngineering:0.12-+:deobf"
deobfCompile 'elucent:albedo:2.0-SNAPSHOT'
}
jar {

View file

@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip

View file

@ -32,12 +32,13 @@ import blusunrize.immersiveengineering.common.blocks.metal.*;
import blusunrize.immersiveengineering.common.blocks.wooden.TileEntityWallmount;
import blusunrize.immersiveengineering.common.util.Utils;
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
import elucent.albedo.event.GatherLightsEvent;
import elucent.albedo.lighting.Light;
import malte0811.industrialWires.*;
import malte0811.industrialWires.blocks.IBlockBoundsIW;
import malte0811.industrialWires.blocks.ISyncReceiver;
import malte0811.industrialWires.blocks.IWProperties;
import malte0811.industrialWires.blocks.TileEntityIWMultiblock;
import malte0811.industrialWires.client.render.TileRenderMarx;
import malte0811.industrialWires.hv.MarxOreHandler;
import malte0811.industrialWires.network.MessageTileSyncIW;
import malte0811.industrialWires.util.DualEnergyStorage;
@ -62,21 +63,26 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Optional;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.function.BiConsumer;
import static malte0811.industrialWires.blocks.hv.TileEntityMarx.FiringState.FIRE;
import static malte0811.industrialWires.util.MiscUtils.getOffset;
import static malte0811.industrialWires.util.MiscUtils.offset;
@Mod.EventBusSubscriber
public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable, ISyncReceiver, IBlockBoundsIW, IImmersiveConnectable, IIC2Connector,
IRedstoneConnector{
IRedstoneConnector {
//Only relevant client-side.
private static final Set<TileEntityMarx> FIRING_GENERATORS = Collections.newSetFromMap(new WeakHashMap<>());
private static final String TYPE = "type";
private static final String STAGES = "stages";
@ -93,7 +99,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
private int stageCount = 0;
public FiringState state = FiringState.CHARGING;
@SideOnly(Side.CLIENT)
public TileRenderMarx.Discharge dischargeData;
public Discharge dischargeData;
// Voltage=100*storedEU
private DualEnergyStorage storage = new DualEnergyStorage(50_000, 32_000);
private boolean hasConnection;
@ -103,7 +109,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
private boolean loaded = false;
private double leftover;
public TileEntityMarx(EnumFacing facing, IWProperties.MarxType type, boolean mirrored) {
TileEntityMarx(EnumFacing facing, IWProperties.MarxType type, boolean mirrored) {
this.facing = facing;
this.type = type;
this.mirrored = mirrored;
@ -201,15 +207,20 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
@Override
public void update() {
if (state==FiringState.FIRE) {
state = FiringState.CHARGING;
} else if (state==FiringState.NEXT_TICK) {
state = FiringState.FIRE;
if (world.isRemote) {
IndustrialWires.proxy.playMarxBang(this, getMiddle(), (float) getNormedEnergy(dischargeData.energy));
} else {
fire();
}
FIRING_GENERATORS.remove(this);
switch (state) {
case NEXT_TICK:
state = FIRE;
if (world.isRemote) {
FIRING_GENERATORS.add(this);
IndustrialWires.proxy.playMarxBang(this, getMiddle(), (float) getNormedEnergy(dischargeData.energy));
} else {
fire();
}
break;
case FIRE:
state = FiringState.CHARGING;
break;
}
if (!world.isRemote&&type== IWProperties.MarxType.BOTTOM) {
if (capVoltages==null||capVoltages.length!=stageCount) {
@ -258,36 +269,45 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
if (!world.isRemote) {
//calculate energy
double energyStored = 0;
boolean failed = capVoltages[0]<MAX_VOLTAGE*.5;
double totalVoltage = 0;
for (int i = 0;i<stageCount;i++) {
energyStored += .5*capVoltages[i]*capVoltages[i]*CAPACITANCE;
totalVoltage += capVoltages[i];
capVoltages[i] = 0;
}
if (totalVoltage<.1*MAX_VOLTAGE*stageCount) {
return;
}
failed |= totalVoltage<MAX_VOLTAGE*.3*stageCount;
net.updateValues();
NBTTagCompound data = new NBTTagCompound();
if (failed) {
energyStored = -1;
} else {
int seed = Utils.RAND.nextInt();
genDischarge((float) energyStored, seed);//TODO test on a dedicated server
data.setInteger("randSeed", seed);
handleEntities(energyStored);
handleOreProcessing(energyStored);//After entities to prevent killing the newly dropped items
}
data.setDouble("energy", energyStored);
IndustrialWires.packetHandler.sendToDimension(new MessageTileSyncIW(this, data), world.provider.getDimension());
handleEntities(energyStored);
handleOreProcessing(energyStored);//After entities to prevent killing the newly dropped items
}
}
public void handleOreProcessing(double energyStored) {
private void handleOreProcessing(double energyStored) {
BlockPos bottom = getBottomElectrode();
List<BlockPos> toBreak = new ArrayList<>(2*stageCount-2);
Vec3d origin = new Vec3d(bottom).addVector(.5, 1, .5);
Set<BlockPos> toBreak = new HashSet<>(dischargeData.vertices.length);
int ores = 0;
for (int i = 1;i<stageCount-1;i++) {
BlockPos here = bottom.up(i);
if (!world.isAirBlock(here)) {
toBreak.add(here);
for (int i = 1;i<dischargeData.vertices.length;i++) {
Vec3d vecHere = origin.add(dischargeData.vertices[i]);
BlockPos blockHere = new BlockPos(vecHere);
if (!world.isAirBlock(blockHere) && canBreak(blockHere)) {
toBreak.add(blockHere);
ores++;
}
double radius = Utils.RAND.nextDouble()*Math.abs(.5-i/(double)stageCount)*Math.sqrt(stageCount)*.5;
double angle = Utils.RAND.nextDouble()*Math.PI*2;
Vec3d offset = new Vec3d(Math.cos(angle)*radius, 0, Math.sin(angle)*radius);
BlockPos outside = here.add(new BlockPos(offset));
if (!outside.equals(here)&&canBreak(outside)) {
toBreak.add(outside);
}
}
if (ores>0) {
double energyPerOre = energyStored / ores;
@ -315,7 +335,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
}
public void handleEntities(double energyStored) {
private void handleEntities(double energyStored) {
Vec3d v0 = getMiddle();
AxisAlignedBB aabb = new AxisAlignedBB(v0, v0);
aabb = aabb.grow(0, stageCount/2-1,0);
@ -365,9 +385,12 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
//checks whether the given pos can't be broken because it is part of the generator
public boolean canBreak(BlockPos pos) {
BlockPos dischargePos = offset(pos, facing, mirrored, 1, 3, 0);
private boolean canBreak(BlockPos pos) {
BlockPos dischargePos = offset(this.pos, facing, mirrored, 1, 3, 0);
Vec3i offset = getOffset(dischargePos, facing, mirrored, pos);
if (offset.getZ()<1||offset.getZ()>=stageCount-1) {
return false;
}
return Math.abs(offset.getX())>Math.abs(offset.getY());
}
@ -379,19 +402,31 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
@Override
public void onSync(NBTTagCompound nbt) {
state = FiringState.NEXT_TICK;
if (dischargeData==null) {
dischargeData = new TileRenderMarx.Discharge(stageCount);
float energy = nbt.getFloat("energy");
if (energy>0) {
genDischarge(energy, nbt.getInteger("randSeed"));
} else {
if (dischargeData==null) {
dischargeData = new Discharge(stageCount);
}
dischargeData.energy = -1;
}
dischargeData.energy = nbt.getFloat("energy");
dischargeData.diameter = (float) getNormedEnergy(dischargeData.energy);
dischargeData.genMarxPoint(0, dischargeData.vertices.length-1);
}
public double getNormedEnergy(double total) {
private void genDischarge(float energy, int seed) {
if (dischargeData==null) {
dischargeData = new Discharge(stageCount);
}
dischargeData.energy = energy;
dischargeData.diameter = (float) getNormedEnergy(dischargeData.energy);
dischargeData.genMarxPoint(seed);
}
private double getNormedEnergy(double total) {
return total*2/(stageCount*MAX_VOLTAGE*MAX_VOLTAGE*CAPACITANCE);
}
AxisAlignedBB renderAabb = null;
private AxisAlignedBB renderAabb = null;
@Nonnull
@Override
public AxisAlignedBB getRenderBoundingBox() {
@ -405,7 +440,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
return renderAabb;
}
AxisAlignedBB collisionAabb = null;
private AxisAlignedBB collisionAabb = null;
@Override
public AxisAlignedBB getBoundingBox() {
if (collisionAabb ==null) {
@ -602,15 +637,8 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
//yellow determines whether a lower charge- than cap0-voltage will discharge the generator
master.allowSlowDischarge = master.net.channelValues[4] == 0;
}
public void tryTriggeredDischarge() {
if (capVoltages[0]>=8/15D* MAX_VOLTAGE) {
state = FiringState.NEXT_TICK;
} else {
for (int i = 0;i<stageCount;i++) {
capVoltages[i] = 0;
}
net.updateValues();
}
private void tryTriggeredDischarge() {
state = FiringState.NEXT_TICK;
}
@Override
@ -631,7 +659,7 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
}
}
public void setStageCount(int stageCount) {
void setStageCount(int stageCount) {
this.stageCount = stageCount;
rcTimeConst = 5D/stageCount;
timeFactor = Math.exp(-1/(20*rcTimeConst));
@ -644,19 +672,107 @@ public class TileEntityMarx extends TileEntityIWMultiblock implements ITickable,
return stageCount;
}
public Vec3d getMiddle() {
private Vec3d getMiddle() {
double middleY = pos.getY()+(stageCount)/2D;
Vec3i electrodXZ = getBottomElectrode();
return new Vec3d(electrodXZ.getX()+.5, middleY, electrodXZ.getZ()+.5);
}
public BlockPos getBottomElectrode() {
private BlockPos getBottomElectrode() {
return offset(pos, facing, mirrored, 1, 4, 0);
}
@Optional.Method(modid="albedo")
@SubscribeEvent
public static void gatherLights(GatherLightsEvent event) {
for (TileEntityMarx te:FIRING_GENERATORS) {
Vec3d origin = te.getMiddle().subtract(0, .5*te.stageCount+1,0);
Light.Builder builder = Light.builder()
.color(1, 1, 1)
.radius(5);
List<Light> toAdd = new ArrayList<>(te.stageCount*2-3);
if (te.dischargeData!=null&&te.dischargeData.energy>0) {
for (int i = 1;i<te.stageCount-1;i++) {
toAdd.add(builder.pos(origin.addVector(0, i, 0)).build());
}
}
origin = new Vec3d(offset(te.pos, te.facing, te.mirrored, 1, 0, 0))
.addVector(0, .75, 0)
.add(new Vec3d(te.facing.getDirectionVec()).scale(.25));
builder.radius(.5F);
for (int i = 0;i<te.stageCount-1;i++) {
toAdd.add(builder.pos(origin.addVector(0, i, 0)).build());
}
event.getLightList().addAll(toAdd);
}
}
public enum FiringState {
CHARGING,
NEXT_TICK,
FIRE;
FIRE
}
public static final class Discharge {
public float energy;
public Vec3d[] vertices;
public float diameter = .25F;
final int stageCount;
Discharge(int stages) {
stageCount = stages;
int count = 1;
while (count<stageCount) {
count <<= 1;
}
count = 8;
vertices = new Vec3d[2*count];
vertices[0] = new Vec3d(0, -.5F, 0);
for (int i = 1;i<vertices.length;i++) {
vertices[i] = new Vec3d(0, 0, 0);
}
vertices[vertices.length-1] = new Vec3d(0, stageCount-1.9375F, 0);
}
// Meant to be const
private final Vec3d side = new Vec3d(0, 0, 1);
//used for calculation buffering
private Vec3d diff;
private Vec3d center;
private Vec3d v0;
private Matrix4 transform = new Matrix4();
void genMarxPoint(int randSeed) {
genMarxPoint(0, vertices.length-1, new Random(randSeed));
}
/**
* @param min The first point of the discharge section to be generated. has to be pre-populated
* @param max The last point of the discharge section to be generated. has to be pre-populated
*/
void genMarxPoint(int min, int max, Random rand) {
int toGenerate = (min+max)/2;
diff = vertices[max].subtract(vertices[min]);
v0 = diff.crossProduct(side);
transform.setIdentity();
double diffLength = diff.lengthVector();
double noise = Math.sqrt(diffLength)*rand.nextDouble()*1/(1+Math.abs(stageCount/2.0-toGenerate))*.75;
if ((max-min)%2==1) {
noise *= (toGenerate-min)/(double)(max-min);
}
v0 = v0.scale((float) (noise/v0.lengthVector()));
diff = diff.scale(1/diffLength);
transform.rotate(Math.PI*2*rand.nextDouble(), diff.x, diff.y, diff.z);
center = vertices[max].add(vertices[min]).scale(.5);
vertices[toGenerate] = transform.apply(v0);
vertices[toGenerate] = center.add(vertices[toGenerate]);
if (toGenerate-min>1) {
genMarxPoint(min, toGenerate, rand);
}
if (max-toGenerate>1) {
genMarxPoint(toGenerate, max, rand);
}
}
}
}

View file

@ -245,7 +245,7 @@ public class ClientProxy extends CommonProxy {
@Override
public float getVolume() {
return .25F;
return .1F;
}
@Override
@ -308,6 +308,9 @@ public class ClientProxy extends CommonProxy {
@Override
public void playMarxBang(TileEntityMarx te, Vec3d pos, float energy) {
if (energy<0) {
energy = .05F;
}
PositionedSoundRecord sound = new PositionedSoundRecord(marxBang, SoundCategory.BLOCKS, 5*energy, 1, false, 0, ISound.AttenuationType.LINEAR, (float) pos.x, (float) pos.y, (float) pos.z);
ClientUtils.mc().getSoundHandler().playSound(sound);
playingSounds.put(te.getPos(), sound);

View file

@ -18,9 +18,9 @@
package malte0811.industrialWires.client.render;
import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4;
import malte0811.industrialWires.blocks.IWProperties;
import malte0811.industrialWires.blocks.hv.TileEntityMarx;
import malte0811.industrialWires.blocks.hv.TileEntityMarx.Discharge;
import malte0811.industrialWires.util.MiscUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
@ -29,44 +29,52 @@ import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
import static malte0811.industrialWires.blocks.hv.TileEntityMarx.FiringState.FIRE;
public class TileRenderMarx extends TileEntitySpecialRenderer<TileEntityMarx> {
@Override
public void render(TileEntityMarx te, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
final boolean debug = false;
//noinspection ConstantConditions,PointlessBooleanExpression
if (te.type== IWProperties.MarxType.BOTTOM&&(debug||te.state== TileEntityMarx.FiringState.FIRE)) {
prepare(x, y, z, te);
if (te.type == IWProperties.MarxType.BOTTOM && (debug || te.state == FIRE)) {
Tessellator tes = Tessellator.getInstance();
BufferBuilder vb = tes.getBuffer();
drawDischarge(te.dischargeData, vb, tes);
prepare(x, y, z, te);
if (te.dischargeData.energy>0) {
drawDischarge(te.dischargeData, vb, tes);
}
GlStateManager.popMatrix();
GlStateManager.pushMatrix();
Vec3i offset = MiscUtils.offset(BlockPos.ORIGIN, te.facing, te.mirrored, 1, 1, 0);
GlStateManager.translate(x+offset.getX(), y+offset.getY()+.75, z+offset.getZ());
GlStateManager.translate(x + offset.getX(), y + offset.getY() + .75, z + offset.getZ());
Vec3i facing = te.facing.getDirectionVec();
final float pos = .6875F;
GlStateManager.translate(-facing.getX()*pos, 0, -facing.getZ()*pos);
GlStateManager.translate(-facing.getX() * pos, 0, -facing.getZ() * pos);
//draw firing spark gaps
for (int i = 0;i<te.getStageCount()-1;i++) {
GlStateManager.pushMatrix();
GlStateManager.translate(0, i, 0);
GlStateManager.rotate(-45, facing.getX(), facing.getY(), facing.getZ());
GlStateManager.rotate(-Minecraft.getMinecraft().player.rotationYaw+180, 0, 1, 0);
vb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
drawDischargeSection(new Vector3f(0, -.2F, 0), new Vector3f(0, .2F, 0), .25F, vb);
tes.draw();
GlStateManager.popMatrix();
for (int i = 0; i < te.getStageCount() - 1; i++) {
renderGap(i, facing, vb, tes);
}
cleanUp();
te.state = TileEntityMarx.FiringState.CHARGING;
}
}
private void renderGap(int i, Vec3i facing, BufferBuilder vb, Tessellator tes) {
GlStateManager.pushMatrix();
GlStateManager.translate(0, i, 0);
GlStateManager.rotate(-45, facing.getX(), facing.getY(), facing.getZ());
//TODO fix
GlStateManager.rotate(-Minecraft.getMinecraft().player.rotationYaw + 180, 0, 1, 0);
vb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
drawDischargeSection(new Vec3d(0, -.2F, 0), new Vec3d(0, .2F, 0), .25F, vb);
tes.draw();
GlStateManager.popMatrix();
}
private void prepare(double x, double y, double z, TileEntityMarx te) {
setLightmapDisabled(true);
GlStateManager.pushMatrix();
@ -99,12 +107,12 @@ public class TileRenderMarx extends TileEntitySpecialRenderer<TileEntityMarx> {
tes.draw();
}
}
private void drawDischargeSection(Vector3f start, Vector3f end, float diameter, BufferBuilder vb) {
private void drawDischargeSection(Vec3d start, Vec3d end, float diameter, BufferBuilder vb) {
drawPart(start, end, diameter/3, diameter/3, WHITE_TRANSPARENT, WHITE, vb);
drawPart(start, end, 0, diameter/3, WHITE, WHITE, vb);
drawPart(start, end, -diameter/3, diameter/3, WHITE, WHITE_TRANSPARENT, vb);
}
private void drawPart(Vector3f start, Vector3f end, float offset, float width, float[] color1, float[] color2, BufferBuilder vb) {
private void drawPart(Vec3d start, Vec3d end, float offset, float width, float[] color1, float[] color2, BufferBuilder vb) {
vb.setTranslation(-offset-width/2, 0, 0);
vb.pos(start.x, start.y, start.z).color(color1[0], color1[1], color1[2], color1[3]).endVertex();
vb.pos(start.x+width, start.y, start.z).color(color2[0], color2[1], color2[2], color2[3]).endVertex();
@ -112,64 +120,4 @@ public class TileRenderMarx extends TileEntitySpecialRenderer<TileEntityMarx> {
vb.pos(end.x, end.y, end.z).color(color1[0], color1[1], color1[2], color1[3]).endVertex();
vb.setTranslation(0, 0, 0);
}
public static final class Discharge {
public float energy;
public Vector3f[] vertices;
public float diameter = .25F;
public final int stageCount;
public Discharge(int stages) {
stageCount = stages;
int count = 1;
while (count<stageCount) {
count <<= 1;
}
count = 8;
vertices = new Vector3f[2*count];
vertices[0] = new Vector3f(0, -.5F, 0);
for (int i = 1;i<vertices.length;i++) {
vertices[i] = new Vector3f();
}
vertices[vertices.length-1] = new Vector3f(0, stageCount-1.9375F, 0);
}
// Meant to be const
private final Vector3f up = new Vector3f(0, 1, 0);
private final Vector3f side = new Vector3f(0, 0, 1);
//used for calculation buffering
private final Vector3f diff = new Vector3f();
private final Vector3f center = new Vector3f();
private final Vector3f v0 = new Vector3f();
private final Matrix4 transform = new Matrix4();
/**
* @param min The first point of the discharge section to be generated. has to be pre-populated
* @param max The last point of the discharge section to be generated. has to be pre-populated
*/
public void genMarxPoint(int min, int max) {
World world = Minecraft.getMinecraft().world;
int toGenerate = (min+max)/2;
Vector3f.sub(vertices[max], vertices[min], diff);
Vector3f.cross(diff, side, v0);
transform.setIdentity();
double noise = Math.sqrt(diff.length())*world.rand.nextDouble()*1/(1+Math.abs(stageCount/2.0-toGenerate))*.75;
if ((max-min)%2==1) {
noise *= (toGenerate-min)/(double)(max-min);
}
v0.scale((float) (noise/v0.length()));
diff.scale(1/diff.length());
transform.rotate(Math.PI*2*world.rand.nextDouble(), diff.x, diff.y, diff.z);
Vector3f.add(vertices[max], vertices[min], center);
center.scale(.5F);
vertices[toGenerate] = transform.apply(v0);
//IELogger.info(toGenerate+" with noise "+noise+" shift-to-noise "+vertices[toGenerate].length()/noise);
Vector3f.add(vertices[toGenerate], center, vertices[toGenerate]);
if (toGenerate-min>1) {
genMarxPoint(min, toGenerate);
}
if (max-toGenerate>1) {
genMarxPoint(toGenerate, max);
}
}
}
}