diff --git a/src/main/java/com/hbm/main/ModEventHandlerRenderer.java b/src/main/java/com/hbm/main/ModEventHandlerRenderer.java index f70f25270..fd5bf7d78 100644 --- a/src/main/java/com/hbm/main/ModEventHandlerRenderer.java +++ b/src/main/java/com/hbm/main/ModEventHandlerRenderer.java @@ -428,8 +428,8 @@ public class ModEventHandlerRenderer { int[] ranges = ForgeModContainer.blendRanges; int distance = 0; - if(settings.fancyGraphics && settings.renderDistanceChunks >= 0 && settings.renderDistanceChunks < ranges.length) { - distance = ranges[settings.renderDistanceChunks]; + if(settings.fancyGraphics && settings.renderDistanceChunks >= 0) { + distance = ranges[Math.min(settings.renderDistanceChunks, ranges.length - 1)]; } float r = 0F; @@ -456,7 +456,7 @@ public class ModEventHandlerRenderer { if(doesBiomeApply) { fogRGBMultiplier = Vec3.createVectorHelper(r / divider, g / divider, b / divider); } else { - fogRGBMultiplier = Vec3.createVectorHelper(red, green, blue); + fogRGBMultiplier = null; } return fogRGBMultiplier; diff --git a/src/main/java/com/hbm/tileentity/machine/TileEntityHadron.java b/src/main/java/com/hbm/tileentity/machine/TileEntityHadron.java index 173feb6de..213fc6d59 100644 --- a/src/main/java/com/hbm/tileentity/machine/TileEntityHadron.java +++ b/src/main/java/com/hbm/tileentity/machine/TileEntityHadron.java @@ -3,6 +3,7 @@ package com.hbm.tileentity.machine; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import com.hbm.blocks.ModBlocks; @@ -106,6 +107,9 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs power = Library.chargeTEFromItems(slots, 4, power, maxPower); drawPower(); + + particles.addAll(particlesToAdd); + particlesToAdd.clear(); if(delay <= 0 && this.isOn && particles.size() < maxParticles && slots[0] != null && slots[1] != null && power >= maxPower * 0.75) { @@ -133,6 +137,25 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs } particlesToRemove.clear(); + + //Sort the virtual particles by momentum, and run through them until we have enough momentum to complete the recipe + //If we succeed, "collapse" the cheapest particle that had enough momentum + //If we fail to make anything, "collapse" the most expensive particle + if(particles.isEmpty() && !particlesCompleted.isEmpty()) { + ItemStack[] result = null; + Particle particle = null; + + particlesCompleted.sort((p1, p2) -> { return p1.momentum - p2.momentum; }); + for(Particle p : particlesCompleted) { + particle = p; + result = HadronRecipes.getOutput(p.item1, p.item2, p.momentum, analysisOnly); + if(result != null) break; + } + + process(particle, result); + + particlesCompleted.clear(); + } NBTTagCompound data = new NBTTagCompound(); data.setBoolean("isOn", isOn); @@ -151,9 +174,9 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs } } - private void process(Particle p) { - - ItemStack[] result = HadronRecipes.getOutput(p.item1, p.item2, p.momentum, analysisOnly); + private void process(Particle p, ItemStack[] result) { + //Collapse this particle to real by consuming power + p.consumePower(); if(result == null) { this.state = HadronRecipes.returnCode; @@ -166,9 +189,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs if((slots[2] == null || (slots[2].getItem() == result[0].getItem() && slots[2].stackSize < slots[2].getMaxStackSize())) && (slots[3] == null || (slots[3].getItem() == result[1].getItem() && slots[3].stackSize < slots[3].getMaxStackSize()))) { - for(int i = 2; i <= 3; i++ ) { - - //System.out.println("yes"); + for(int i = 2; i <= 3; i++) { if(slots[i] == null) slots[i] = result[i - 2].copy(); else @@ -252,17 +273,18 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs particlesToRemove.add(p); if(!p.isExpired()) - process(p); + particlesCompleted.add(p); p.expired = true; } static final int maxParticles = 1; - List particles = new ArrayList(); - List particlesToRemove = new ArrayList(); + List particles = new ArrayList(); + List particlesToRemove = new ArrayList(); + List particlesToAdd = new ArrayList(); + List particlesCompleted = new ArrayList(); private void updateParticles() { - for(Particle particle : particles) { particle.update(); } @@ -327,6 +349,25 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs this.stat_z = z; this.stat_success = false; } + + public void expire(Particle particle, EnumHadronState reason) { + if(particle.expired) + return; + + particle.consumePower(); + for(Particle p : particles) { + p.expired = true; + particlesToRemove.add(p); + } + worldObj.newExplosion(null, particle.posX + 0.5, particle.posY + 0.5, particle.posZ + 0.5, 10, false, false); + + //If any particles expire, cancel any succeeding particles, since they'll confuse the player + particlesCompleted.clear(); + + TileEntityHadron.this.state = reason; + TileEntityHadron.this.delay = delayError; + TileEntityHadron.this.setExpireStats(reason, particle.momentum, particle.posX, particle.posY, particle.posZ); + } public class Particle { @@ -347,6 +388,16 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs int cl1 = 0; boolean expired = false; + boolean cloned = false; + + //Quantum mechanical ass particle + //Virtual particles traverse the accelerator without consuming electrical power + //The cheapest valid route to the analysis chamber is then turned into a real particle, consuming power + List plugs = new ArrayList(); + + //Quantum particles should only traverse a schottky direction ONCE + //Keep a list of traversed diodes and directions + HashMap> history = new HashMap>(); public Particle(ItemStack item1, ItemStack item2, ForgeDirection dir, int posX, int posY, int posZ) { this.item1 = item1.copy(); @@ -361,22 +412,21 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs this.charge = 750; this.momentum = 0; } - - public void expire(EnumHadronState reason) { - - if(expired) - return; - - this.expired = true; - particlesToRemove.add(this); - worldObj.newExplosion(null, posX + 0.5, posY + 0.5, posZ + 0.5, 10, false, false); - //System.out.println("Last dir: " + dir.name()); - //System.out.println("Last pos: " + posX + " " + posY + " " + posZ); - //Thread.currentThread().dumpStack(); - TileEntityHadron.this.state = reason; - TileEntityHadron.this.delay = delayError; - TileEntityHadron.this.setExpireStats(reason, this.momentum, posX, posY, posZ); + //Clones the particle and gives it a new direction + public Particle clone(ForgeDirection dir) { + Particle p = new Particle(item1, item2, dir, posX, posY, posZ); + p.momentum = momentum; + p.charge = charge; + p.analysis = analysis; + p.isCheckExempt = isCheckExempt; + p.cl0 = cl0; + p.cl1 = cl1; + p.expired = expired; + p.plugs = new ArrayList(plugs); + p.history = new HashMap>(history); + p.cloned = true; + return p; } public boolean isExpired() { @@ -388,7 +438,13 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs if(expired) //just in case return; - changeDirection(this); + //Recently cloned particles have already a set direction, this prevents infinite recursion + if(cloned) { + cloned = false; + } else { + changeDirection(this); + } + makeSteppy(this); if(!this.isExpired()) //only important for when the current segment is the core @@ -397,7 +453,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs isCheckExempt = false; //clearing up the exemption we might have held from the previous turn, AFTER stepping if(charge < 0) - this.expire(EnumHadronState.ERROR_NO_CHARGE); + expire(this, EnumHadronState.ERROR_NO_CHARGE); if(cl0 > 0) cl0--; if(cl1 > 0) cl1--; @@ -427,6 +483,14 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs this.momentum += coilVal; } + + public void consumePower() { + for(TileEntityHadronPower plug : plugs) { + long bit = 10000; + int times = (int) (plug.getPower() / bit); + plug.setPower(plug.getPower() - times * bit); + } + } } /** @@ -452,7 +516,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs if(te instanceof TileEntityHadron) { if(p.analysis != 3) - p.expire(EnumHadronState.ERROR_NO_ANALYSIS); + expire(p, EnumHadronState.ERROR_NO_ANALYSIS); else this.finishParticle(p); @@ -460,7 +524,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs } if(block.getMaterial() != Material.air && block != ModBlocks.hadron_diode) - p.expire(EnumHadronState.ERROR_OBSTRUCTED_CHANNEL); + expire(p, EnumHadronState.ERROR_OBSTRUCTED_CHANNEL); if(block == ModBlocks.hadron_diode) p.isCheckExempt = true; @@ -532,7 +596,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs //not a valid coil: kablam! if(!isValidCoil(block)) { - p.expire(EnumHadronState.ERROR_EXPECTED_COIL); + expire(p, EnumHadronState.ERROR_EXPECTED_COIL); } else { p.charge -= coilVal; p.incrementCharge(block, meta, coilVal); @@ -566,7 +630,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs int times = (int) (plug.getPower() / bit); //how many charges the plug has to offer p.charge += times; - plug.setPower(plug.getPower() - times * bit); + p.plugs.add(plug); continue; } @@ -575,11 +639,8 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs if(p.isCheckExempt && ix + iy + iz == 2) { continue; } - - //System.out.println("Was exempt: " + p.isCheckExempt); - //worldObj.setBlock(a, b, c, Blocks.dirt); - p.expire(EnumHadronState.ERROR_MALFORMED_SEGMENT); + expire(p, EnumHadronState.ERROR_MALFORMED_SEGMENT); } } } @@ -591,14 +652,17 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs //if the analysis chamber is too big, destroy if(p.analysis > 3) - p.expire(EnumHadronState.ERROR_ANALYSIS_TOO_LONG); + expire(p, EnumHadronState.ERROR_ANALYSIS_TOO_LONG); if(p.analysis == 2) { - this.worldObj.playSoundEffect(p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5, "fireworks.blast", 2.0F, 2F); + //Only pop for the first particle + if(this.state != EnumHadronState.ANALYSIS) { + this.worldObj.playSoundEffect(p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5, "fireworks.blast", 2.0F, 2F); + NBTTagCompound data = new NBTTagCompound(); + data.setString("type", "hadron"); + PacketDispatcher.wrapper.sendToAllAround(new AuxParticlePacketNT(data, p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5), new TargetPoint(worldObj.provider.dimensionId, p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5, 25)); + } this.state = EnumHadronState.ANALYSIS; - NBTTagCompound data = new NBTTagCompound(); - data.setString("type", "hadron"); - PacketDispatcher.wrapper.sendToAllAround(new AuxParticlePacketNT(data, p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5), new TargetPoint(worldObj.provider.dimensionId, p.posX + 0.5, p.posY + 0.5, p.posZ + 0.5, 25)); } //if operating in line accelerator mode, halt after 2 blocks and staart the reading @@ -610,7 +674,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs //if the analysis stops despite being short of 3 steps in the analysis chamber, destroy if(p.analysis > 0 && p.analysis < 3) - p.expire(EnumHadronState.ERROR_ANALYSIS_TOO_SHORT); + expire(p, EnumHadronState.ERROR_ANALYSIS_TOO_SHORT); } } @@ -641,7 +705,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs if(diode.getConfig(p.dir.getOpposite().ordinal()) != DiodeConfig.IN) { //it appears as if we have slammed into the side of a diode, ouch - p.expire(EnumHadronState.ERROR_DIODE_COLLISION); + expire(p, EnumHadronState.ERROR_DIODE_COLLISION); } //there's a diode ahead, turn off checks so we can make the curve @@ -663,29 +727,33 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs p.isCheckExempt = true; TileEntityHadronDiode diode = (TileEntityHadronDiode)te; - - //the direction in which we were going anyway is an output, so we will keep going - if(diode.getConfig(dir.ordinal()) == DiodeConfig.OUT) { - return; - - //well then, iterate through some random directions and hope a valid output shows up - } else { - - List dirs = getRandomDirs(); - - for(ForgeDirection d : dirs) { - - if(d == dir || d == dir.getOpposite()) - continue; - - //looks like we can leave! - if(diode.getConfig(d.ordinal()) == DiodeConfig.OUT) { - //set the direction and leave this hellhole + + boolean hasTurnedCurrent = false; + + if(!p.history.containsKey(diode)) + p.history.put(diode, new ArrayList()); + + List usedDirections = p.history.get(diode); + + //Instance a new particle for each required fork + for(ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) { + if(!usedDirections.contains(d) && diode.getConfig(d.ordinal()) == DiodeConfig.OUT) { + if(!hasTurnedCurrent) { p.dir = d; - return; + hasTurnedCurrent = true; + } else { + Particle clone = p.clone(d); + clone.history.get(diode).add(d); + particlesToAdd.add(clone); } } } + + //Add the used direction to the main particle AFTER cloning, so the clones don't get incorrect travel history + usedDirections.add(p.dir); + + //If we managed to exit, keep going + if(hasTurnedCurrent) return; } //next step is air or the core, proceed @@ -714,7 +782,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs //it seems like there are two or more possible ways, which is not allowed without a diode //sorry kid, nothing personal } else { - p.expire(EnumHadronState.ERROR_BRANCHING_TURN); + expire(p, EnumHadronState.ERROR_BRANCHING_TURN); return; } } @@ -726,7 +794,7 @@ public class TileEntityHadron extends TileEntityMachineBase implements IEnergyUs return; } - p.expire(EnumHadronState.ERROR_OBSTRUCTED_CHANNEL); + expire(p, EnumHadronState.ERROR_OBSTRUCTED_CHANNEL); } /**