Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.minecraft.src.ic2.common;
- import java.util.List;
- import java.util.Random;
- import net.minecraft.src.AxisAlignedBB;
- import net.minecraft.src.Block;
- import net.minecraft.src.ChunkCoordinates;
- import net.minecraft.src.Container;
- import net.minecraft.src.DamageSource;
- import net.minecraft.src.Entity;
- import net.minecraft.src.EntityItem;
- import net.minecraft.src.EntityLiving;
- import net.minecraft.src.EntityPlayer;
- import net.minecraft.src.InventoryPlayer;
- import net.minecraft.src.Item;
- import net.minecraft.src.ItemStack;
- import net.minecraft.src.Material;
- import net.minecraft.src.NBTTagCompound;
- import net.minecraft.src.TileEntity;
- import net.minecraft.src.World;
- import net.minecraft.src.mod_IC2;
- import net.minecraft.src.ic2.api.*;
- import net.minecraft.src.ic2.platform.*;
- /**
- * TE class of Nuclear reactor. Seperated from Generators due to it' complexity.
- * does NOT extend TEBaseGenerator, will need seperate Wiring Algorythms
- * @author Alblaka
- */
- public class TileEntityNuclearReactor extends TileEntityMachine implements IEnergySource, IHasGui, IReactor {
- public static Random randomizer = new Random();
- public TileEntityNuclearReactor() {
- super(54);
- updateTicker = randomizer.nextInt(getTickRate());
- }
- @Override
- public void onUnloaded() {
- if (Platform.isSimulating() && addedToEnergyNet) {
- EnergyNet.getForWorld(worldObj).removeTileEntity(this);
- addedToEnergyNet = false;
- }
- if (Platform.isRendering()) {
- AudioManager.removeSources(this);
- audioSourceMain = null;
- audioSourceGeiger = null;
- }
- super.onUnloaded();
- }
- @Override
- public String getInvName() {
- return "Nuclear Reactor";
- }
- /**
- * EUf Output
- * Calculated every tickRate() frames in advance
- */
- public short output = 0;
- /**
- * Ticker to reduce CPU-usage.
- * Assume'd to be 10;
- */
- public int updateTicker;
- /**
- * Heat of the reactor.
- * High heat = Bad.
- */
- public int heat = 0;
- /**
- * Loads heat and output.
- */
- @Override
- public void readFromNBT(NBTTagCompound nbttagcompound)
- {
- super.readFromNBT(nbttagcompound);
- try {
- heat = nbttagcompound.getInteger("heat");
- } catch (Exception e) {
- heat = nbttagcompound.getShort("heat");
- }
- output = nbttagcompound.getShort("output");
- }
- /**
- * Saves heat and output.
- */
- @Override
- public void writeToNBT(NBTTagCompound nbttagcompound) {
- super.writeToNBT(nbttagcompound);
- nbttagcompound.setInteger("heat", heat);
- nbttagcompound.setShort("output", (short)output);
- }
- @Override
- public void updateEntity() { // WARNING: only executed server-side, see canUpdate()
- super.updateEntity();
- if (!addedToEnergyNet) {
- EnergyNet.getForWorld(worldObj).addTileEntity(this);
- addedToEnergyNet = true;
- }
- sendEnergy(output);
- if (updateTicker++ % getTickRate() != 0) return;
- if (!worldObj.doChunksNearChunkExist(xCoord, yCoord, zCoord, 2)) {
- output = 0;
- } else {
- dropAllUnfittingStuff(); // Clean up the inventory of the Reactor
- if (heat > 0) {
- heat -= coolReactorFromOutside();
- if (heat <= 0) {
- heat = 0;
- } else if (calculateHeatEffects()) { // explosion
- return;
- }
- }
- output = 0; // Recalculate output
- processChambers();
- //System.out.println("Output: "+output+"EUf / Heat: "+heat);
- setActive(heat>=1000 || output > 0); // "Displayed" Activity = Either heated or producing
- onInventoryChanged(); // AS the reactor merely works every 10 frames, this will be fine to be called every time. Ntm inventory WILL change quite every time
- }
- NetworkManager.updateTileEntityField(this, "output");
- }
- /**
- * Will eject anything "unfitting" from it's inventory into the world.
- * Unfitting: Either out of the reactors chamber (size changed?) or not used by the Reactor at all.
- */
- public void dropAllUnfittingStuff()
- {
- int size = getReactorSize();
- for (int x = 0; x < 9; x++)
- {
- for (int y = 0; y < 6; y++)
- {
- ItemStack stack = getMatrixCoord(x,y);
- if (stack == null)
- {
- continue;
- }
- if (stack.stackSize <= 0)
- {
- setMatrixCoord(x,y,null);
- continue;
- }
- if (x >= size || !isUsefulItem(stack))
- { // If the item is beyond reactor size || the item is useless
- eject(stack);
- setMatrixCoord(x,y,null);
- //System.out.println("Dropping "+x+"/"+y);
- }
- }
- }
- }
- /**
- * Will spawn the given itemstack into the world (if !!Platform.isSimulating()).
- * Don't forget to set the previous position of the itemstack to null!
- * @param drop The ItemStack meant to be ejected
- */
- public void eject (ItemStack drop)
- {
- if (!Platform.isSimulating() || drop==null)
- {
- return;
- }
- // Standard Drop-Code, C&P from Block.dropBlockAsItem_do
- float f = 0.7F;
- double d = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
- double d1 = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
- double d2 = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
- EntityItem entityitem = new EntityItem(worldObj, (double)xCoord + d, (double)yCoord + d1, (double)zCoord + d2, drop);
- entityitem.delayBeforeCanPickup = 10;
- worldObj.spawnEntityInWorld(entityitem);
- return;
- }
- /**
- * @return true, if the given ItemStack is used by the Reactor in any Way
- */
- public static boolean isUsefulItem(ItemStack item) {
- if (item == null) return false;
- int id = item.itemID;
- if (id == Ic2Items.uraniumCell.itemID ||
- id == Ic2Items.coolingCell.itemID ||
- id == Ic2Items.integratedReactorPlating.itemID ||
- id == Block.ice.blockID ||
- id == Item.bucketWater.shiftedIndex ||
- id == Item.bucketLava.shiftedIndex ||
- id == Item.bucketEmpty.shiftedIndex ||
- id == Ic2Items.integratedHeatDisperser.itemID ||
- id == Ic2Items.depletedIsotopeCell.itemID ||
- id == Ic2Items.reEnrichedUraniumCell.itemID ||
- id == Ic2Items.nearDepletedUraniumCell.itemID) {
- return true;
- }
- return false;
- }
- /**
- * Will iterize adjacent blocks and search for stuff which can be taken to cool the reactor.
- * @return amount of heat reduced;
- */
- public int coolReactorFromOutside()
- {
- int cool = 1; // Standard cooling.
- int air = 0;
- for (int x = xCoord-1; x <= xCoord+1; x++)
- {
- for (int y = yCoord-1; y <= yCoord+1; y++)
- {
- for (int z = zCoord-1; z <= zCoord+1; z++)
- {
- if (worldObj.getBlockId(x, y, z) == Ic2Items.reactorChamber.itemID)
- { // Chambers provide increased cooling
- cool+=2;
- }
- if (worldObj.getBlockMaterial(x, y, z)==Material.water)
- { // Water cools for 1 point
- cool++;
- }
- if (worldObj.getBlockId(x, y, z)==0)
- { // Air cools for 1/4th point
- air++;
- }
- if (worldObj.getBlockId(x, y, z)==Block.fire.blockID)
- { // Fire is bad
- air-=2;
- }
- if (worldObj.getBlockMaterial(x, y, z)==Material.lava)
- { // Lava disables cooling
- cool-=3;
- }
- }
- }
- }
- cool+=air/4;
- if (cool < 0)
- {
- return 0;
- }
- //System.out.println("Total Outside-ish cooling: "+cool);
- return cool;
- }
- /**
- * Calculates the effect of the Reactor's heat
- * @return true if the reactor exploded (stops updateTick)
- */
- @SuppressWarnings("rawtypes")
- public boolean calculateHeatEffects()
- {
- if (heat < 4000 || !Platform.isSimulating() || mod_IC2.explosionPowerReactorMax <= 0.0F)
- { // Save CPU, below 4000 nothing will happen, either way - also only apply effects if the config explosion maximum is >0
- return false;
- }
- int size = getReactorSize();
- int maxHeat = getMaxHeat();
- float power = ((float)heat)/((float)maxHeat); // Ranges from 0F (cold) to 1F (explodingly hot)
- if (power >= 1F) {// BoomHeat
- explode();
- return true;
- }
- if (power >= 0.85F && worldObj.rand.nextFloat() <= 4*(power-0.7F))
- { // Turn material into lava / fire
- int[] coord = getRandCoord(2);
- if (coord!= null)
- {
- //System.out.println("Attempting lava on "+coord[0]+"/"+coord[1]+"/"+coord[2]);
- int id = worldObj.getBlockId(coord[0], coord[1], coord[2]);
- if (id == 0)
- {
- worldObj.setBlockWithNotify(coord[0], coord[1], coord[2], Block.fire.blockID);
- }
- else
- {
- Material mat = Block.blocksList[id].blockMaterial;
- if (id != Block.bedrock.blockID)
- {
- if (mat==Material.rock || mat == Material.iron || mat == Material.lava || mat == Material.ground|| mat == Material.clay)
- {
- worldObj.setBlockAndMetadataWithNotify(coord[0], coord[1], coord[2], Block.lavaMoving.blockID, 15);
- }
- else
- {
- worldObj.setBlockWithNotify(coord[0], coord[1], coord[2], Block.fire.blockID);
- }
- }
- }
- }
- }
- if (power >= 0.7F)
- { // Deal damage to nearby players, with chance F
- //System.out.println("Burning ent's");
- List list1 = worldObj.getEntitiesWithinAABB(EntityLiving.class, AxisAlignedBB.getBoundingBox(xCoord-3, yCoord-3, zCoord-3, xCoord+4, yCoord+4, zCoord+4));
- for (int l = 0; l < list1.size(); l++)
- {
- Entity ent = (Entity) list1.get(l);
- ent.attackEntityFrom(IC2DamageSource.radiation, 1); //TODO Add hazmat suits?
- }
- }
- if (power >= 0.5F)
- { // Puff water
- int[] coord = getRandCoord(2);
- if (coord!= null)
- {
- //System.out.println("Dampening water on "+coord[0]+"/"+coord[1]+"/"+coord[2]);
- int id = worldObj.getBlockId(coord[0], coord[1], coord[2]);
- if (id > 0 && Block.blocksList[id].blockMaterial==Material.water)
- {
- worldObj.setBlockWithNotify(coord[0], coord[1], coord[2], 0);
- }
- }
- }
- if (power >= 0.4F && worldObj.rand.nextFloat() <= 1.5F*(power-0.4F))
- { // Create fire, ignite flameables
- int[] coord = getRandCoord(2);
- if (coord!= null)
- {
- //System.out.println("Setting fire on "+coord[0]+"/"+coord[1]+"/"+coord[2]);
- int id = worldObj.getBlockId(coord[0], coord[1], coord[2]);
- if (id > 0)
- {
- Material mat = Block.blocksList[id].blockMaterial;
- if (mat==Material.wood || mat == Material.leaves || mat == Material.cloth)
- {
- worldObj.setBlockWithNotify(coord[0], coord[1], coord[2], Block.fire.blockID);
- }
- }
- }
- }
- return false;
- }
- /**
- * Returns a random coordinate set within the specified block radius
- * @return int[3] coordinates, or null if invalid radius or the Reactor itself was chooen.
- */
- public int[] getRandCoord(int radius)
- {
- if (radius <= 0)
- {
- return null;
- }
- int[] c = new int[3];
- c[0]=xCoord+worldObj.rand.nextInt(2*radius+1)-radius;
- c[1]=yCoord+worldObj.rand.nextInt(2*radius+1)-radius;
- c[2]=zCoord+worldObj.rand.nextInt(2*radius+1)-radius;
- if (c[0]==xCoord && c[1]==yCoord && c[2]==zCoord)
- {
- return null;
- }
- return c;
- }
- /**
- * Main calculation method of Nuclear Reactor.
- * Accesses the inventory-content and calculates Energy gain, heat production, etc.
- */
- public void processChambers()
- {
- int size = getReactorSize();
- for (int y = 0; y < 6; y++)
- {
- for (int x = 0; x < size; x++)
- {
- processChamber(x,y);
- }
- }
- }
- /**
- * Calculates the Nuclear Reactor'ing of the given slot
- * @param x Row
- * @param y Column
- */
- public void processChamber(int x, int y)
- {
- ////System.out.println("Chamber "+x+"/"+y);
- if (getMatrixCoord(x,y)==null)
- { // Empty chamber
- return;
- }
- int id = getMatrixCoord(x,y).itemID;
- if (id == Ic2Items.coolingCell.itemID)
- { // Coolants cool down
- if (getMatrixCoord(x,y).getItemDamage()>0)
- {
- damageReactorItem(getMatrixCoord(x,y), -1);
- }
- }
- if (id == Ic2Items.integratedReactorPlating.itemID)
- { // Platings only cool slowly
- if (getMatrixCoord(x,y).getItemDamage()>0 && worldObj.rand.nextInt(10)==0)
- {
- damageReactorItem(getMatrixCoord(x,y), -1);
- }
- }
- if (id == Ic2Items.nearDepletedUraniumCell.itemID ||
- id == Ic2Items.depletedIsotopeCell.itemID ||
- id == Ic2Items.reEnrichedUraniumCell.itemID)
- { // Depleted stuff keeps emitting low heat
- heat++;
- }
- if (id == Item.bucketWater.shiftedIndex)
- { // WaterBuckets act as one-time coolers
- if (heat > 4000)
- {
- heat-=250;
- getMatrixCoord(x,y).itemID=Item.bucketEmpty.shiftedIndex;
- }
- }
- if (id == Item.bucketLava.shiftedIndex)
- { // Lava-Buckets act as suicidal HEATERS
- heat+=2000;
- getMatrixCoord(x,y).itemID=Item.bucketEmpty.shiftedIndex;
- }
- if (id == Block.ice.blockID)
- { // Ice is a continous cooler
- if (heat > 300)
- {
- heat-=300;
- getMatrixCoord(x,y).stackSize--;
- if (getMatrixCoord(x,y).stackSize <= 0)
- {
- setMatrixCoord(x,y,null);
- }
- }
- }
- if (id == Ic2Items.integratedHeatDisperser.itemID)
- { // Heat Dispersers will attempt to balance surrounding heat levels
- disperseHeat(x,y);
- }
- if (id == Ic2Items.uraniumCell.itemID && produceEnergy())
- { // Uranisation, if energy ned
- generateEnergy(x,y);
- }
- }
- /**
- * Sub-Method
- * Will attempt to balance the heat of all surroundings
- */
- public void disperseHeat(int x, int y)
- {
- switchHeat(x,y,x-1,y);
- switchHeat(x,y,x+1,y);
- switchHeat(x,y,x,y-1);
- switchHeat(x,y,x,y+1);
- // Balance up to 25 points between cooler and reactor
- int rebalance = ((getMatrixCoord(x,y).getItemDamage()-heat)+1)/2; //+1 to ensure "1 heat" is still transferred
- if (rebalance > 0)
- { // Cooler sends heat
- if (rebalance > 25)
- {
- rebalance = 25;
- }
- heat+=rebalance;
- damageReactorItem(getMatrixCoord(x,y), -1 * rebalance);
- }
- else
- { // Cooler takes heat
- rebalance*=-1;
- if (rebalance > 25)
- {
- rebalance = 25;
- }
- heat-=rebalance;
- damageReactorItem(getMatrixCoord(x,y), rebalance);
- }
- }
- /**
- * Switches up to 6 points of heat between the two grid-spots, trying to balance both
- * Assumes first coordinate to be the cooler, no null-checks
- */
- public void switchHeat(int x, int y, int x2, int y2)
- {
- if (getMatrixCoord(x2,y2)==null)
- {
- return;
- }
- int id = getMatrixCoord(x2,y2).itemID;
- if (id != Ic2Items.coolingCell.itemID && id != Ic2Items.integratedReactorPlating.itemID)
- {// Nothing heatable
- return;
- }
- int heat = getMatrixCoord(x,y).getItemDamage();
- int heat2 = getMatrixCoord(x2,y2).getItemDamage();
- int rebalance = (heat-heat2)/2;
- //System.out.println(""+x+"/"+y+" sends "+rebalance+" heat to "+x2+"/"+y2);
- if (rebalance > 0)
- { // Cooler sends heat
- if (rebalance > 6)
- {
- rebalance = 6;
- }
- getMatrixCoord(x,y).setItemDamage(heat-rebalance);
- if (id == Ic2Items.coolingCell.itemID)
- { // Coolant
- damageReactorItem(getMatrixCoord(x2,y2), rebalance);
- }
- else
- { // Plating
- spreadHeat(x2,y2,rebalance,false);
- }
- }
- else
- { // Cooler takes heat
- rebalance*=-1;
- if (rebalance > 6)
- {
- rebalance = 6;
- }
- damageReactorItem(getMatrixCoord(x,y), rebalance);
- getMatrixCoord(x2,y2).setItemDamage(heat2-rebalance);
- }
- }
- /**
- * Generate Energy from a Uran-Cell, while creating heat, etc
- * @param x Row
- * @param y Column
- */
- public void generateEnergy(int x, int y)
- {
- int pulses = 1+isUranium(x+1,y)+isUranium(x-1,y)+isUranium(x,y+1)+isUranium(x,y-1); // Uran produces more energy + heat if clustered
- //System.out.println("Generating Energy on "+x+"/"+y+" , "+pulses+" Pulses");
- output+= pulses*pulsePower(); // Energy Generation
- pulses += enrichDepleted(x+1,y)+enrichDepleted(x-1,y)+enrichDepleted(x,y+1)+enrichDepleted(x,y-1); // Enrich Depleted Isotopes
- for (; pulses > 0; pulses--) // Heat generation
- {
- int takers = canTakeHeat(x+1,y,true,true)+canTakeHeat(x-1,y,true,true)+canTakeHeat(x,y+1,true,true)+canTakeHeat(x,y-1,true,true);
- int genHeat;
- switch (takers)
- {
- case 2: genHeat = 4; break;
- case 3: genHeat = 2; break;
- case 4: genHeat = 1; break;
- default: genHeat = 10; break;
- }
- //System.out.println("Heat produced: "+genHeat*takers);
- if (takers == 0)
- {
- heat+=genHeat; // Reactor heats up, bad
- }
- else
- { // Radiate heat to all surroundings.. not-heatables will ignore it
- giveHeatTo(x+1,y,genHeat);
- giveHeatTo(x-1,y,genHeat);
- giveHeatTo(x,y+1,genHeat);
- giveHeatTo(x,y-1,genHeat);
- }
- }
- if (getMatrixCoord(x,y).getItemDamage()==9999 && worldObj.rand.nextInt(3)==0)
- {
- setMatrixCoord(x,y, Ic2Items.nearDepletedUraniumCell.copy());
- }
- else
- {
- /*if (getMatrixCoord(x,y).getItemDamage() < 9000){
- getMatrixCoord(x,y).setItemDamage(9950);}*/ // Useful for testing stuff at the end
- damageReactorItem(getMatrixCoord(x,y), 1); // of the reactor cycle :D
- }
- }
- /**
- * @return 1 if the given slot is uranium, 0 otherwise
- */
- public int isUranium(int x, int y)
- {
- if (getMatrixCoord(x,y)!= null && getMatrixCoord(x,y).itemID == Ic2Items.uraniumCell.itemID)
- {
- return 1;
- }
- return 0;
- }
- /**
- * Enriches Depleted Isotopes on the given gridspot, if present.
- * @return 1 if there was an isotope to enrich, 0 otherwise
- */
- public int enrichDepleted(int x, int y)
- {
- if (getMatrixCoord(x,y)==null || getMatrixCoord(x,y).itemID != Ic2Items.depletedIsotopeCell.itemID)
- {
- return 0;
- }
- int oneInChance = 8;
- if (heat >= 3000)
- {
- oneInChance=4;
- }
- if (heat >= 6000)
- {
- oneInChance=2;
- }
- if (heat >= 9000)
- {
- oneInChance=1;
- }
- if (worldObj.rand.nextInt(oneInChance)!=0)
- {
- return 1;
- }
- if (getMatrixCoord(x,y).getItemDamage()<=0)
- {
- setMatrixCoord(x,y, Ic2Items.reEnrichedUraniumCell.copy());
- }
- else
- {
- damageReactorItem(getMatrixCoord(x,y), -2);
- }
- return 1;
- }
- /**
- * @param countPlating Shall Plating be considered a heat-taker, too?
- * @param countCooler Same for coolers
- * @return 1 if the given slot can take heat, 0 otherwise
- */
- public int canTakeHeat(int x, int y, boolean countPlating, boolean countCooler)
- {
- if (getMatrixCoord(x,y)==null)
- {
- return 0;
- }
- int id = getMatrixCoord(x,y).itemID;
- if (id == Ic2Items.coolingCell.itemID ||
- id == Ic2Items.integratedReactorPlating.itemID && countPlating ||
- id == Ic2Items.integratedHeatDisperser.itemID && countCooler)
- {
- return 1;
- }
- return 0;
- }
- /**
- * Distributes heat to the given grid-slot.
- * Will do nothing if the given slot can't take heat.
- * @param heat Amount of heat
- */
- public void giveHeatTo(int x, int y, int heat)
- {
- if (canTakeHeat(x,y,true,true)==0)
- { // I wasnt meant to take the heat
- return;
- }
- if (getMatrixCoord(x,y).itemID == Ic2Items.integratedReactorPlating.itemID)
- {
- spreadHeat(x,y,heat,true);
- }
- else
- {
- damageReactorItem(getMatrixCoord(x,y), heat);
- }
- }
- /**
- * Calculates how to send heat around via a Plating
- * Will spread heat to adjacent tiles and store 1 point by itself
- * @param heat Amount of heat
- * @param primary Used to prevent lockups. Heat send to further Platings get's false
- */
- public void spreadHeat (int x, int y, int heat, boolean primary)
- {
- int takers = canTakeHeat(x+1,y,primary,false)+canTakeHeat(x-1,y,primary,false)+canTakeHeat(x,y+1,primary,false)+canTakeHeat(x,y-1,primary,false);
- if (takers == 0)
- { // Need to take the whole heat by myself
- //System.out.println("Plating takes "+heat+" heat by itself.");
- damageReactorItem(getMatrixCoord(x,y), heat);
- //System.out.println("Taking heat for myself, now on heat "+getMatrixCoord(x,y).getItemDamage());
- return;
- }
- for (;heat%takers!=0 && getMatrixCoord(x,y).getItemDamage()>0;)
- { // Get some heat from the plating itself
- heat++;
- damageReactorItem(getMatrixCoord(x,y), -1);
- }
- int genHeat = heat/takers;
- //System.out.println("Splitting "+heat+" from "+x+"/"+y+" into "+genHeat+" each for "+takers+" sinks.");
- heat-=genHeat*takers; // Rest
- if (heat > 0)
- {
- damageReactorItem(getMatrixCoord(x,y), heat);
- }
- spreadHeatTo(x-1,y,genHeat, primary);
- spreadHeatTo(x+1,y,genHeat, primary);
- spreadHeatTo(x,y-1,genHeat, primary);
- spreadHeatTo(x,y+1,genHeat, primary);
- }
- /**
- * Distributes heat to the given grid-slot from a Plating
- * Will do nothing if the given slot can't take heat.
- * @param heat Amount of heat
- * @param toPlatings Spread to platings?
- */
- public void spreadHeatTo(int x, int y, int heat, boolean toPlatings)
- {
- if (canTakeHeat(x,y,toPlatings,false)==0)
- { // I wasnt meant to take the heat
- return;
- }
- if (getMatrixCoord(x,y).itemID == Ic2Items.integratedReactorPlating.itemID && toPlatings)
- {
- spreadHeat(x,y,heat,false);
- }
- else
- {
- damageReactorItem(getMatrixCoord(x,y), heat);
- }
- }
- /**
- * Checks for redstone input, active Redstone will shutdown the Reactor.
- * @return !Redstone
- */
- public boolean produceEnergy()
- {
- return !worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
- }
- /**
- * Returns the correct ItemStack, as if the Array of ItemStacks would be a 2-dimensional Matrix of size 6by9
- * @param x Row of ItemStack
- * @param y Column of ItemStack
- * @return The specified ItemStack
- */
- public ItemStack getMatrixCoord(int x, int y) {
- if (x < 0 || x >= 9 || y < 0 || y >= 6) return null;
- return super.getStackInSlot(x+y*9);
- }
- /**
- * Method to deal damage to items within the reactor
- * @param itemStack The ItemStack to be damaged
- * @param damage The amount of damage to be dealt, can be negative for cooling
- */
- public void damageReactorItem(ItemStack itemStack, int damage)
- {
- if(!itemStack.isItemStackDamageable())
- {
- return;
- }
- itemStack.setItemDamage(itemStack.getItemDamage() + damage);
- if (itemStack.getItemDamage() > itemStack.getMaxDamage())
- {
- itemStack.stackSize--;
- if(itemStack.stackSize < 0)
- {
- itemStack.stackSize = 0;
- }
- itemStack.setItemDamage(0);
- }
- }
- @Override
- public ItemStack getStackInSlot(int i) {
- int x = i % 9;
- int size = getReactorSize();
- if (x >= size) { // return last valid slot in row
- return getMatrixCoord(size-1, i/9);
- } else {
- return super.getStackInSlot(i);
- }
- }
- /**
- * Sets the slot specified by the coordinates to the given stack, as if the Array of ItemStacks would be a 2-dimensional Matrix of size 6by9
- * @param x Row of ItemStack
- * @param y Column of ItemStack
- * @param stack The specified ItemStack
- */
- public void setMatrixCoord(int x, int y, ItemStack stack) {
- if (x < 0 || x >= 9 || y < 0 || y >= 6) return;
- super.setInventorySlotContents(x+y*9, stack);
- }
- @Override
- public void setInventorySlotContents(int i, ItemStack itemstack) {
- int x = i % 9;
- int size = getReactorSize();
- if (x >= size) { // return last valid slot in row
- setMatrixCoord(size-1, i/9, itemstack);
- } else {
- super.setInventorySlotContents(i, itemstack);
- }
- }
- /**
- * Returns the "size" of the Reactor Chamber = The number of coloumns the GUI has.
- * Determined by 3 + count of adjancent Chamber Blocks
- * @return Reactor's Size as int
- */
- public short getReactorSize() {
- if (worldObj == null) return 9;
- short rows = 3;
- for (Direction direction: Direction.values()) {
- TileEntity target = direction.applyToTileEntity(this);
- if (target instanceof TileEntityReactorChamber) {
- rows++;
- }
- }
- return rows;
- }
- /**
- * Specifyable TickRate of the reactor
- * @return 20
- */
- public int getTickRate()
- {
- return 20;
- }
- /**
- * Specifyable power generated from each "uranium pulse" in EUf
- * @return 10
- */
- public static int pulsePower()
- {
- return mod_IC2.energyGeneratorNuclear; // 1 cell == 10 EU per tick * 20 ticks * 10000 life length = 2-10 kk EU
- }
- public boolean isAddedToEnergyNet() {
- return addedToEnergyNet;
- }
- @Override
- public boolean emitsEnergyTo(TileEntity receiver, Direction direction)
- {
- return true;
- }
- @Override
- public int getMaxEnergyOutput()
- {
- return 240*pulsePower();
- }
- /**
- * Hook for the Energy Net. Provides an amount of power "send".
- * @return Return the un-used power
- */
- public int sendEnergy(int send)
- {
- send = EnergyNet.getForWorld(worldObj).emitEnergyFrom(this, send);
- if (send > 0 && worldObj.getBlockTileEntity(xCoord,yCoord+1,zCoord) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord,yCoord+1,zCoord)).sendEnergy(send);
- }
- if (send > 0 && worldObj.getBlockTileEntity(xCoord,yCoord-1,zCoord) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord,yCoord-1,zCoord)).sendEnergy(send);
- }
- if (send > 0 && worldObj.getBlockTileEntity(xCoord+1,yCoord,zCoord) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord+1,yCoord,zCoord)).sendEnergy(send);
- }
- if (send > 0 && worldObj.getBlockTileEntity(xCoord-1,yCoord,zCoord) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord-1,yCoord,zCoord)).sendEnergy(send);
- }
- if (send > 0 && worldObj.getBlockTileEntity(xCoord,yCoord,zCoord+1) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord,yCoord,zCoord+1)).sendEnergy(send);
- }
- if (send > 0 && worldObj.getBlockTileEntity(xCoord,yCoord,zCoord-1) instanceof TileEntityReactorChamber)
- {
- send = ((TileEntityReactorChamber)worldObj.getBlockTileEntity(xCoord,yCoord,zCoord-1)).sendEnergy(send);
- }
- return send;
- }
- @Override
- public ContainerIC2 getGuiContainer(EntityPlayer entityPlayer) {
- return new ContainerNuclearReactor(entityPlayer, this);
- }
- @Override
- public String getGuiClassName(EntityPlayer entityPlayer) {
- return "GuiNuclearReactor";
- }
- @Override
- public void onGuiClosed(EntityPlayer entityPlayer) {
- }
- @Override
- public void onNetworkUpdate(String field) {
- if (field.equals("output")) {
- if (output > 0) {
- if (lastOutput <= 0) {
- if (audioSourceMain == null) audioSourceMain = AudioManager.createSource(this, PositionSpec.Center, "Generators/NuclearReactor/NuclearReactorLoop.ogg", true, false, AudioManager.defaultVolume);
- if (audioSourceMain != null) audioSourceMain.play();
- }
- if (output < 40) {
- if (lastOutput <= 0 || lastOutput >= 40) {
- if (audioSourceGeiger != null) audioSourceGeiger.remove();
- audioSourceGeiger = AudioManager.createSource(this, PositionSpec.Center, "Generators/NuclearReactor/GeigerLowEU.ogg", true, false, AudioManager.defaultVolume);
- if (audioSourceGeiger != null) audioSourceGeiger.play();
- }
- } else if (output < 80) {
- if (lastOutput < 40 || lastOutput >= 80) {
- if (audioSourceGeiger != null) audioSourceGeiger.remove();
- audioSourceGeiger = AudioManager.createSource(this, PositionSpec.Center, "Generators/NuclearReactor/GeigerMedEU.ogg", true, false, AudioManager.defaultVolume);
- if (audioSourceGeiger != null) audioSourceGeiger.play();
- }
- } else if (output >= 80) {
- if (lastOutput < 80) {
- if (audioSourceGeiger != null) audioSourceGeiger.remove();
- audioSourceGeiger = AudioManager.createSource(this, PositionSpec.Center, "Generators/NuclearReactor/GeigerHighEU.ogg", true, false, AudioManager.defaultVolume);
- if (audioSourceGeiger != null) audioSourceGeiger.play();
- }
- }
- } else if (lastOutput > 0) {
- if (audioSourceMain != null) audioSourceMain.stop();
- if (audioSourceGeiger != null) audioSourceGeiger.stop();
- }
- lastOutput = output;
- }
- super.onNetworkUpdate(field);
- }
- @Override
- public float getWrenchDropRate() {
- return 0.8f;
- }
- public boolean addedToEnergyNet = false;
- public AudioSource audioSourceMain;
- public AudioSource audioSourceGeiger;
- private short lastOutput = 0;
- // IReactor
- @Override
- public ChunkCoordinates getPosition() {
- return new ChunkCoordinates(xCoord, yCoord, zCoord);
- }
- @Override
- public World getWorld() {
- return worldObj;
- }
- @Override
- public int getHeat() {
- return heat;
- }
- @Override
- public void setHeat(int heat) {
- this.heat = heat;
- }
- @Override
- public int addHeat(int amount) {
- heat += amount;
- return heat;
- }
- @Override
- public ItemStack getItemAt(int x, int y) {
- return getMatrixCoord(x, y);
- }
- @Override
- public void setItemAt(int x, int y, ItemStack item) {
- setMatrixCoord(x, y, item);
- }
- @Override
- public void explode() {
- float boomPower = 10;
- for (int y = 0; y < 6; y++) {
- for (int x = 0; x < getReactorSize(); x++) {
- if (getMatrixCoord(x,y)!=null && getMatrixCoord(x,y).itemID == Ic2Items.uraniumCell.itemID) { // Plating reduces devastion
- boomPower += 3;
- } else if (getMatrixCoord(x,y)!=null && getMatrixCoord(x,y).itemID == Ic2Items.integratedReactorPlating.itemID) { // Plating reduces devastion
- boomPower -= 1;
- }
- }
- }
- if (boomPower > mod_IC2.explosionPowerReactorMax) boomPower = mod_IC2.explosionPowerReactorMax;
- worldObj.setBlockWithNotify(xCoord, yCoord, zCoord, 0);
- ExplosionIC2 explosion = new ExplosionIC2(worldObj, null, xCoord, yCoord, zCoord, boomPower, 0.01F, 1.5F, IC2DamageSource.nuke);
- explosion.doExplosion();
- }
- @Override
- public int getMaxHeat() {
- int maxHeat = 10000;
- maxHeat += 1000*(getReactorSize()-3);
- for (int y = 0; y < 6; y++)
- {
- for (int x = 0; x < getReactorSize(); x++)
- {
- if (getMatrixCoord(x,y)!=null && getMatrixCoord(x,y).itemID == Ic2Items.integratedReactorPlating.itemID)
- { // Additional Plating
- maxHeat+=100;
- }
- }
- }
- return maxHeat;
- }
- @Override
- public int getOutput() {
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement