Some work on improving and fixing particle accelerator

This commit is contained in:
Robert S 2014-10-22 10:42:33 -04:00
parent 621b557129
commit 14b00dba9c
3 changed files with 130 additions and 89 deletions

View file

@ -27,7 +27,7 @@ object EntityParticle
/** /**
* User client side to determine the velocity of the particle. * User client side to determine the velocity of the particle.
*/ */
val clientParticleVelocity: Float = 0.9f val ANITMATTER_CREATION_SPEED: Float = 0.9f
def canSpawnParticle(world: World, pos: Vector3): Boolean = def canSpawnParticle(world: World, pos: Vector3): Boolean =
{ {
@ -105,7 +105,7 @@ class EntityParticle(par1World: World) extends Entity(par1World) with IEntityAdd
{ {
if (this.ticksExisted % 10 == 0) if (this.ticksExisted % 10 == 0)
{ {
this.worldObj.playSoundAtEntity(this, Reference.prefix + "accelerator", 1f, (0.6f + (0.4 * (this.getParticleVelocity / EntityParticle.clientParticleVelocity))).asInstanceOf[Float]) this.worldObj.playSoundAtEntity(this, Reference.prefix + "accelerator", 1f, (0.6f + (0.4 * (this.getParticleVelocity / EntityParticle.ANITMATTER_CREATION_SPEED))).asInstanceOf[Float])
} }
val t: TileEntity = this.worldObj.getTileEntity(this.movementVector.xi, this.movementVector.yi, this.movementVector.zi) val t: TileEntity = this.worldObj.getTileEntity(this.movementVector.xi, this.movementVector.yi, this.movementVector.zi)
if (!(t.isInstanceOf[TileAccelerator])) if (!(t.isInstanceOf[TileAccelerator]))
@ -162,9 +162,9 @@ class EntityParticle(par1World: World) extends Entity(par1World) with IEntityAdd
val dongLi: Vector3 = new Vector3 val dongLi: Vector3 = new Vector3
dongLi.add(this.movementDirection) dongLi.add(this.movementDirection)
dongLi.multiply(acceleration) dongLi.multiply(acceleration)
this.motionX = Math.min(dongLi.x + this.motionX, EntityParticle.clientParticleVelocity) this.motionX = Math.min(dongLi.x + this.motionX, EntityParticle.ANITMATTER_CREATION_SPEED)
this.motionY = Math.min(dongLi.y + this.motionY, EntityParticle.clientParticleVelocity) this.motionY = Math.min(dongLi.y + this.motionY, EntityParticle.ANITMATTER_CREATION_SPEED)
this.motionZ = Math.min(dongLi.z + this.motionZ, EntityParticle.clientParticleVelocity) this.motionZ = Math.min(dongLi.z + this.motionZ, EntityParticle.ANITMATTER_CREATION_SPEED)
this.isAirBorne = true this.isAirBorne = true
this.lastTickPosX = this.posX this.lastTickPosX = this.posX
this.lastTickPosY = this.posY this.lastTickPosY = this.posY
@ -223,7 +223,7 @@ class EntityParticle(par1World: World) extends Entity(par1World) with IEntityAdd
this.worldObj.playSoundAtEntity(this, Reference.prefix + "antimatter", 1.5f, 1f - this.worldObj.rand.nextFloat * 0.3f) this.worldObj.playSoundAtEntity(this, Reference.prefix + "antimatter", 1.5f, 1f - this.worldObj.rand.nextFloat * 0.3f)
if (!this.worldObj.isRemote) if (!this.worldObj.isRemote)
{ {
if (this.getParticleVelocity > EntityParticle.clientParticleVelocity / 2) if (this.getParticleVelocity > EntityParticle.ANITMATTER_CREATION_SPEED / 2)
{ {
val radius: Float = 1f val radius: Float = 1f
val bounds: AxisAlignedBB = AxisAlignedBB.getBoundingBox(this.posX - radius, this.posY - radius, this.posZ - radius, this.posX + radius, this.posY + radius, this.posZ + radius) val bounds: AxisAlignedBB = AxisAlignedBB.getBoundingBox(this.posX - radius, this.posY - radius, this.posZ - radius, this.posX + radius, this.posY + radius, this.posZ + radius)

View file

@ -30,11 +30,11 @@ class GuiAccelerator(player: EntityPlayer, tileEntity: TileAccelerator) extends
{ {
status = "\u00a72Idle" status = "\u00a72Idle"
} }
this.fontRendererObj.drawString("Velocity: " + Math.round((this.tileEntity.velocity / EntityParticle.clientParticleVelocity) * 100) + "%", 8, 27, 4210752) this.fontRendererObj.drawString("Velocity: " + Math.round((this.tileEntity.velocity / EntityParticle.ANITMATTER_CREATION_SPEED) * 100) + "%", 8, 27, 4210752)
this.fontRendererObj.drawString("Energy Used:", 8, 38, 4210752) this.fontRendererObj.drawString("Energy Used:", 8, 38, 4210752)
this.fontRendererObj.drawString(new UnitDisplay(UnitDisplay.Unit.JOULES, this.tileEntity.totalEnergyConsumed).toString, 8, 49, 4210752) this.fontRendererObj.drawString(new UnitDisplay(UnitDisplay.Unit.JOULES, this.tileEntity.totalEnergyConsumed).toString, 8, 49, 4210752)
this.fontRendererObj.drawString(new UnitDisplay(UnitDisplay.Unit.WATT, Settings.ACCELERATOR_ENERGY_COST_PER_TICK * 20).toString, 8, 60, 4210752) this.fontRendererObj.drawString(new UnitDisplay(UnitDisplay.Unit.WATT, Settings.ACCELERATOR_ENERGY_COST_PER_TICK * 20).toString, 8, 60, 4210752)
this.fontRendererObj.drawString("N?A", 8, 70, 4210752) this.fontRendererObj.drawString("Voltage: N?A", 8, 70, 4210752)
this.fontRendererObj.drawString("Antimatter: " + this.tileEntity.antimatter + " mg", 8, 80, 4210752) this.fontRendererObj.drawString("Antimatter: " + this.tileEntity.antimatter + " mg", 8, 80, 4210752)
this.fontRendererObj.drawString("Status:", 8, 90, 4210752) this.fontRendererObj.drawString("Status:", 8, 90, 4210752)
this.fontRendererObj.drawString(status, 8, 100, 4210752) this.fontRendererObj.drawString(status, 8, 100, 4210752)

View file

@ -1,24 +1,25 @@
package resonantinduction.atomic.machine.accelerator package resonantinduction.atomic.machine.accelerator
import io.netty.buffer.ByteBuf
import net.minecraft.block.Block import net.minecraft.block.Block
import net.minecraft.block.material.Material import net.minecraft.block.material.Material
import net.minecraft.entity.player.EntityPlayer import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagCompound
import net.minecraft.network.Packet
import net.minecraftforge.common.util.ForgeDirection import net.minecraftforge.common.util.ForgeDirection
import resonant.api.{IElectromagnet, IRotatable} import resonant.api.{IElectromagnet, IRotatable}
import resonant.engine.ResonantEngine
import resonant.lib.content.prefab.java.TileElectricInventory import resonant.lib.content.prefab.java.TileElectricInventory
import resonant.lib.network.Synced import resonant.lib.network.discriminator.{PacketTile, PacketType}
import resonant.lib.network.discriminator.PacketAnnotation import resonant.lib.network.handle.TPacketIDReceiver
import resonant.lib.utility.BlockUtility import resonant.lib.utility.BlockUtility
import resonantinduction.atomic.AtomicContent import resonantinduction.atomic.AtomicContent
import resonantinduction.atomic.items.ItemAntimatter import resonantinduction.atomic.items.ItemAntimatter
import resonantinduction.core.{Reference, Settings} import resonantinduction.core.{Reference, ResonantInduction, Settings}
import universalelectricity.core.transform.vector.Vector3 import universalelectricity.core.transform.vector.Vector3
class TileAccelerator extends TileElectricInventory(Material.iron) with IElectromagnet with IRotatable
class TileAccelerator extends TileElectricInventory(Material.iron) with IElectromagnet with IRotatable with TPacketIDReceiver
{ {
final val DESC_PACKET_ID = 2;
/** /**
* Multiplier that is used to give extra anti-matter based on density (hardness) of a given ore. * Multiplier that is used to give extra anti-matter based on density (hardness) of a given ore.
*/ */
@ -26,18 +27,20 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
/** /**
* The total amount of energy consumed by this particle. In Joules. * The total amount of energy consumed by this particle. In Joules.
*/ */
@Synced var totalEnergyConsumed: Float = 0 var totalEnergyConsumed: Double = 0
/** /**
* The amount of anti-matter stored within the accelerator. Measured in milligrams. * The amount of anti-matter stored within the accelerator. Measured in milligrams.
*/ */
@Synced var antimatter: Int = 0 var antimatter: Int = 0
var entityParticle: EntityParticle = null var entityParticle: EntityParticle = null
@Synced var velocity: Float = 0 var velocity: Float = 0
@Synced private var clientEnergy: Double = 0 var clientEnergy: Double = 0
private var lastSpawnTick: Int = 0 var lastSpawnTick: Int = 0
//Constructor //Constructor
this.setSizeInventory(4) this.setSizeInventory(4)
this.setCapacity(Settings.ACCELERATOR_ENERGY_COST_PER_TICK * 20)
this.setMaxTransfer(Settings.ACCELERATOR_ENERGY_COST_PER_TICK)
override def update override def update
{ {
@ -45,46 +48,16 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
if (!worldObj.isRemote) if (!worldObj.isRemote)
{ {
clientEnergy = energy.getEnergy clientEnergy = energy.getEnergy
velocity = 0 velocity = getParticleVel()
if (entityParticle != null) outputAntimatter()
{
velocity = entityParticle.getParticleVelocity.asInstanceOf[Float]
}
if (AtomicContent.isItemStackEmptyCell(getStackInSlot(1)))
{
if (getStackInSlot(1).stackSize > 0)
{
if (antimatter >= 125)
{
if (getStackInSlot(2) != null)
{
if (getStackInSlot(2).getItem eq AtomicContent.itemAntimatter)
{
val newStack: ItemStack = getStackInSlot(2).copy
if (newStack.stackSize < newStack.getMaxStackSize)
{
decrStackSize(1, 1)
antimatter -= 125
newStack.stackSize += 1
setInventorySlotContents(2, newStack)
}
}
}
else
{
antimatter -= 125
decrStackSize(1, 1)
setInventorySlotContents(2, new ItemStack(AtomicContent.itemAntimatter))
}
}
}
}
if (worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord)) if (worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord))
{ {
if (energy.checkExtract) if (energy.checkExtract)
{ {
if (entityParticle == null) if (entityParticle == null)
{ {
//Create new particle if we have materials to spawn it with
if (getStackInSlot(0) != null && lastSpawnTick >= 40) if (getStackInSlot(0) != null && lastSpawnTick >= 40)
{ {
val spawn_vec: Vector3 = asVector3 val spawn_vec: Vector3 = asVector3
@ -105,6 +78,7 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
{ {
if (entityParticle.isDead) if (entityParticle.isDead)
{ {
//Handle strange matter creation
if (entityParticle.didParticleCollide) if (entityParticle.didParticleCollide)
{ {
if (worldObj.rand.nextFloat <= Settings.darkMatterSpawnChance) if (worldObj.rand.nextFloat <= Settings.darkMatterSpawnChance)
@ -114,18 +88,21 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
} }
entityParticle = null entityParticle = null
} }
else if (velocity > EntityParticle.clientParticleVelocity) else if (velocity > EntityParticle.ANITMATTER_CREATION_SPEED)
{ {
//Create antimatter if we have hit max speed
worldObj.playSoundEffect(xCoord, yCoord, zCoord, Reference.prefix + "antimatter", 2f, 1f - worldObj.rand.nextFloat * 0.3f) worldObj.playSoundEffect(xCoord, yCoord, zCoord, Reference.prefix + "antimatter", 2f, 1f - worldObj.rand.nextFloat * 0.3f)
val generatedAntimatter: Int = 5 + worldObj.rand.nextInt(antiMatterDensityMultiplyer) val generatedAntimatter: Int = 5 + worldObj.rand.nextInt(antiMatterDensityMultiplyer)
antimatter += generatedAntimatter antimatter += generatedAntimatter
//Cleanup
totalEnergyConsumed = 0 totalEnergyConsumed = 0
entityParticle.setDead entityParticle.setDead
entityParticle = null entityParticle = null
} }
if (entityParticle != null) if (entityParticle != null)
{ {
worldObj.playSoundEffect(xCoord, yCoord, zCoord, Reference.prefix + "accelerator", 1.5f, (0.6f + (0.4 * (entityParticle.getParticleVelocity) / EntityParticle.clientParticleVelocity)).asInstanceOf[Float]) worldObj.playSoundEffect(xCoord, yCoord, zCoord, Reference.prefix + "accelerator", 1.5f, (0.6f + (0.4 * (entityParticle.getParticleVelocity) / EntityParticle.ANITMATTER_CREATION_SPEED)).asInstanceOf[Float])
} }
} }
energy.extractEnergy energy.extractEnergy
@ -149,17 +126,55 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
} }
if (ticks % 5 == 0) if (ticks % 5 == 0)
{ {
for (player <- getPlayersUsing)
{
sendPacketToPlayer(player, getDescPacket)
}
} }
lastSpawnTick += 1 lastSpawnTick += 1
} }
} }
/**
* Converts antimatter storage into item if the condition are meet
*/
def outputAntimatter()
{
//Do we have an empty cell in slot one
if (AtomicContent.isItemStackEmptyCell(getStackInSlot(1)) && getStackInSlot(1).stackSize > 0)
{
// Each cell can only hold 125mg of antimatter TODO maybe a config for this?
if (antimatter >= 125)
{
if (getStackInSlot(2) != null)
{
// If the output slot is not empty we must increase stack size
if (getStackInSlot(2).getItem eq AtomicContent.itemAntimatter)
{
val newStack: ItemStack = getStackInSlot(2).copy
if (newStack.stackSize < newStack.getMaxStackSize)
{
decrStackSize(1, 1)
antimatter -= 125
newStack.stackSize += 1
setInventorySlotContents(2, newStack)
}
}
}
else
{
//Output to slot 2 and decrease volume of antimatter
antimatter -= 125
decrStackSize(1, 1)
setInventorySlotContents(2, new ItemStack(AtomicContent.itemAntimatter))
}
}
}
}
override def activate(player: EntityPlayer, side: Int, hit: Vector3): Boolean = override def activate(player: EntityPlayer, side: Int, hit: Vector3): Boolean =
{ {
if (!world.isRemote) player.openGui(ResonantInduction, 0, world, xi, yi, zi)
{
player.openGui(AtomicContent, 0, world, xi, yi, zi)
}
return true return true
} }
@ -169,57 +184,70 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
if (itemToAccelerate != null) if (itemToAccelerate != null)
{ {
antiMatterDensityMultiplyer = Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER antiMatterDensityMultiplyer = Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER
try
{
val potentialBlock: Block = Block.getBlockFromItem(itemToAccelerate.getItem) val potentialBlock: Block = Block.getBlockFromItem(itemToAccelerate.getItem)
if (potentialBlock != null) if (potentialBlock != null)
{ {
antiMatterDensityMultiplyer = Math.abs(BlockUtility.getBlockHardness(potentialBlock)).asInstanceOf[Int] antiMatterDensityMultiplyer = BlockUtility.getBlockHardness(potentialBlock).asInstanceOf[Int] * Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER
if (antiMatterDensityMultiplyer <= 0) if (antiMatterDensityMultiplyer <= 0)
{
antiMatterDensityMultiplyer = 1
}
}
}
catch
{
case err: Exception =>
{ {
antiMatterDensityMultiplyer = Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER antiMatterDensityMultiplyer = Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER
} }
} if (antiMatterDensityMultiplyer > 1000)
}
}
override def getDescriptionPacket: Packet =
{ {
return ResonantEngine.instance.packetHandler.toMCPacket(new PacketAnnotation(this)) antiMatterDensityMultiplyer = 1000 * Settings.ACCELERATOR_ANITMATTER_DENSITY_MULTIPLIER
}
}
}
} }
/** /////////////////////////////////////////
* Reads a tile entity from NBT. /// Packet Handling ///
*/ ////////////////////////////////////////
override def read(buf: ByteBuf, id: Int, player: EntityPlayer, packet: PacketType): Boolean =
{
//Client only packets
if (world.isRemote)
{
if (id == DESC_PACKET_ID)
{
this.velocity = buf.readFloat()
this.totalEnergyConsumed = buf.readDouble();
this.antimatter = buf.readInt();
this.energy.setEnergy(buf.readDouble())
return true;
}
}
return super.read(buf, id, player, packet);
}
override def getDescPacket: PacketTile =
{
return new PacketTile(xi, yi, zi, Array(DESC_PACKET_ID, velocity, totalEnergyConsumed, antimatter, energy.getEnergy))
}
/////////////////////////////////////////
/// Save handling ///
////////////////////////////////////////
override def readFromNBT(par1NBTTagCompound: NBTTagCompound) override def readFromNBT(par1NBTTagCompound: NBTTagCompound)
{ {
super.readFromNBT(par1NBTTagCompound) super.readFromNBT(par1NBTTagCompound)
totalEnergyConsumed = par1NBTTagCompound.getFloat("totalEnergyConsumed") totalEnergyConsumed = par1NBTTagCompound.getDouble("energyUsed")
antimatter = par1NBTTagCompound.getInteger("antimatter") antimatter = par1NBTTagCompound.getInteger("antimatter")
} }
/**
* Writes a tile entity to NBT.
*/
override def writeToNBT(par1NBTTagCompound: NBTTagCompound) override def writeToNBT(par1NBTTagCompound: NBTTagCompound)
{ {
super.writeToNBT(par1NBTTagCompound) super.writeToNBT(par1NBTTagCompound)
par1NBTTagCompound.setFloat("totalEnergyConsumed", totalEnergyConsumed) par1NBTTagCompound.setDouble("energyUsed", totalEnergyConsumed)
par1NBTTagCompound.setInteger("antimatter", antimatter) par1NBTTagCompound.setInteger("antimatter", antimatter)
} }
override def getAccessibleSlotsFromSide(side: Int): Array[Int] = /////////////////////////////////////////
{ /// Inventory Overrides ///
return Array[Int](0, 1, 2, 3) ////////////////////////////////////////
}
override def canInsertItem(slotID: Int, itemStack: ItemStack, j: Int): Boolean = override def canInsertItem(slotID: Int, itemStack: ItemStack, j: Int): Boolean =
{ {
@ -247,7 +275,11 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
return false return false
} }
def isRunning: Boolean = /////////////////////////////////////////
/// Field Getters & Setters ///
////////////////////////////////////////
override def isRunning: Boolean =
{ {
return true return true
} }
@ -261,4 +293,13 @@ class TileAccelerator extends TileElectricInventory(Material.iron) with IElectro
{ {
world.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, direction.ordinal, 3) world.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, direction.ordinal, 3)
} }
/** get velocity for the particle and @return it as a float */
def getParticleVel(): Float =
{
if (entityParticle != null)
return entityParticle.getParticleVelocity.asInstanceOf[Float]
else
return 0
}
} }