Improvements to TileEntityRift

1. Removed several fields and functions referring to the newer rendering
code. The functions were just cluttering up the code and the fields were
consuming additional memory that was never being used for anything.
2. Updated NBT reading and writing functions to give some tags proper
names and to remove references to unused fields.
Removed the tag for riftCloseTimer because it was unnecessary alongside
the shouldClose flag. If a server reboots while a rift is closing, the
rift can start over upon reloading.
3. Renamed some fields and functions to have better names.
4. Changed the various checks for closing rifts. There were a few
redundant parts. We don't have to put calls to "this.invalidate()"
everywhere on top of explicitly removing the tile entity and destroying
its block.
5. Rewrote update timing checks. The rift spread and Enderman spawning
calls have been separated to distribute the impact of updating a rift.
Also, a flaw in the timing logic meant that the calculations for
particle offsets would only run when a rift was first created. That's
been fixed.
This commit is contained in:
SenseiKiwi 2014-06-26 07:06:17 -04:00
parent 364ba11f81
commit a0629b51a3

View file

@ -1,7 +1,6 @@
package StevenDimDoors.mod_pocketDim.tileentities; package StevenDimDoors.mod_pocketDim.tileentities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -13,7 +12,6 @@ import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import StevenDimDoors.mod_pocketDim.ServerPacketHandler; import StevenDimDoors.mod_pocketDim.ServerPacketHandler;
import StevenDimDoors.mod_pocketDim.mod_pocketDim; import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.config.DDProperties; import StevenDimDoors.mod_pocketDim.config.DDProperties;
@ -33,97 +31,71 @@ public class TileEntityRift extends DDTileEntityBase
private static final int MAX_RIFT_SPREAD_CHANCE = 256; private static final int MAX_RIFT_SPREAD_CHANCE = 256;
private static final int HOSTILE_ENDERMAN_CHANCE = 1; private static final int HOSTILE_ENDERMAN_CHANCE = 1;
private static final int MAX_HOSTILE_ENDERMAN_CHANCE = 3; private static final int MAX_HOSTILE_ENDERMAN_CHANCE = 3;
private static final float[] POCKET_RENDER_COLOR= {1,1,1,.7F}; private static final int UPDATE_PERIOD = 200;
private static final float[] DEFAULT_RENDER_COLOR= {1,1,1,1}; private static final int CLOSING_PERIOD = 40;
private static Random random = new Random(); private static Random random = new Random();
private int age = 0; private int updateTimer;
private int updateTimer = 0; private int closeTimer = 0;
private int riftCloseTimer = 0;
public int xOffset = 0; public int xOffset = 0;
public int yOffset = 0; public int yOffset = 0;
public int zOffset = 0; public int zOffset = 0;
public boolean shouldClose = false; public boolean shouldClose = false;
private boolean hasUpdated = false;
public DimLink nearestRiftData; public DimLink nearestRiftData;
public int spawnedEndermenID = 0; public int spawnedEndermenID = 0;
public HashMap<Integer, double[]> renderingCenters = new HashMap<Integer, double[]>();
public TileEntityRift()
{
// Vary the update times of rifts to prevent all the rifts in a cluster
// from updating at the same time.
updateTimer = random.nextInt(UPDATE_PERIOD);
}
@Override @Override
public void updateEntity() public void updateEntity()
{ {
// Determines if rift should render white closing particles and spread closing effect to other rifts nearby if (PocketManager.getLink(xCoord, yCoord, zCoord, worldObj.provider.dimensionId) == null)
if (this.shouldClose)
{ {
closeRift();
}
else if (PocketManager.getLink(xCoord, yCoord, zCoord, worldObj.provider.dimensionId) == null)
{
this.invalidate();
if (worldObj.getBlockId(xCoord, yCoord, zCoord) == mod_pocketDim.blockRift.blockID) if (worldObj.getBlockId(xCoord, yCoord, zCoord) == mod_pocketDim.blockRift.blockID)
{ {
worldObj.setBlockToAir(xCoord, yCoord, zCoord); worldObj.setBlockToAir(xCoord, yCoord, zCoord);
this.invalidate();
return;
} }
else
{
this.invalidate();
}
return;
} }
if (worldObj.getBlockId(xCoord, yCoord, zCoord) != mod_pocketDim.blockRift.blockID) if (worldObj.getBlockId(xCoord, yCoord, zCoord) != mod_pocketDim.blockRift.blockID)
{ {
worldObj.removeBlockTileEntity(xCoord, yCoord, zCoord);
this.invalidate(); this.invalidate();
return; return;
} }
//The code for the new rift rendering hooks in here, as well as in the ClientProxy to bind the TESR to the rift. // Check if this rift should render white closing particles and
//It is inactive for now. // spread the closing effect to other rifts nearby.
/** if (this.shouldClose)
if(rand.nextInt(15) == 1)
{ {
age = age + 1; closeRift();
this.calculateNextRenderQuad(age, rand); return;
} }
this.clearBlocksOnRift();
**/
//This code should execute once every 10 seconds if (updateTimer >= UPDATE_PERIOD)
if (updateTimer >= 200)
{ {
this.spawnEndermen(mod_pocketDim.properties); this.spawnEndermen(mod_pocketDim.properties);
this.grow(mod_pocketDim.properties);
updateTimer = 0; updateTimer = 0;
} }
else if (updateTimer == 0) else if (updateTimer == UPDATE_PERIOD / 2)
{ {
this.calculateOldParticleOffset(); //this also calculates the distance for the particle stuff. this.calculateParticleOffsets();
this.spread(mod_pocketDim.properties);
} }
updateTimer++; updateTimer++;
} }
private void clearBlocksOnRift()
{
//clears blocks for the new rending effect
for (double[] coord : this.renderingCenters.values())
{
int x = MathHelper.floor_double(coord[0] + 0.5);
int y = MathHelper.floor_double(coord[1] + 0.5);
int z = MathHelper.floor_double(coord[2] + 0.5);
// Right side
if (!mod_pocketDim.blockRift.isBlockImmune(worldObj, this.xCoord + x, this.yCoord + y, this.zCoord + z))
{
worldObj.setBlockToAir(this.xCoord + x, this.yCoord + y, this.zCoord + z);
}
// Left side
if (!mod_pocketDim.blockRift.isBlockImmune(worldObj, this.xCoord - x, this.yCoord - y, this.zCoord - z))
{
worldObj.setBlockToAir(this.xCoord - x, this.yCoord - y, this.zCoord - z);
}
}
}
private void spawnEndermen(DDProperties properties) private void spawnEndermen(DDProperties properties)
{ {
if (worldObj.isRemote || !properties.RiftsSpawnEndermenEnabled) if (worldObj.isRemote || !properties.RiftsSpawnEndermenEnabled)
@ -174,14 +146,14 @@ public class TileEntityRift extends DDTileEntityBase
private void closeRift() private void closeRift()
{ {
NewDimData dimension = PocketManager.getDimensionData(worldObj); NewDimData dimension = PocketManager.getDimensionData(worldObj);
if (riftCloseTimer == 20) if (closeTimer == CLOSING_PERIOD / 2)
{ {
ArrayList<DimLink> rifts= dimension.findRiftsInRange(worldObj, 6, xCoord, yCoord, zCoord); ArrayList<DimLink> riftLinks = dimension.findRiftsInRange(worldObj, 6, xCoord, yCoord, zCoord);
if (rifts.size()>0) if (riftLinks.size() > 0)
{ {
for(DimLink riftToClose : rifts) for (DimLink riftLink : riftLinks)
{ {
Point4D location = riftToClose.source(); Point4D location = riftLink.source();
TileEntityRift rift = (TileEntityRift) worldObj.getBlockTileEntity(location.getX(), location.getY(), location.getZ()); TileEntityRift rift = (TileEntityRift) worldObj.getBlockTileEntity(location.getX(), location.getY(), location.getZ());
if (rift != null) if (rift != null)
{ {
@ -191,9 +163,8 @@ public class TileEntityRift extends DDTileEntityBase
} }
} }
} }
if (riftCloseTimer > 40) if (closeTimer >= CLOSING_PERIOD)
{ {
this.invalidate();
if (!this.worldObj.isRemote) if (!this.worldObj.isRemote)
{ {
DimLink link = PocketManager.getLink(this.xCoord, this.yCoord, this.zCoord, worldObj); DimLink link = PocketManager.getLink(this.xCoord, this.yCoord, this.zCoord, worldObj);
@ -204,13 +175,12 @@ public class TileEntityRift extends DDTileEntityBase
} }
worldObj.setBlockToAir(xCoord, yCoord, zCoord); worldObj.setBlockToAir(xCoord, yCoord, zCoord);
worldObj.playSound(xCoord, yCoord, zCoord, "mods.DimDoors.sfx.riftClose", (float) .7, 1, true); worldObj.playSound(xCoord, yCoord, zCoord, "mods.DimDoors.sfx.riftClose", (float) .7, 1, true);
this.worldObj.removeBlockTileEntity(xCoord, yCoord, zCoord);
return; return;
} }
riftCloseTimer++; closeTimer++;
} }
private void calculateOldParticleOffset() private void calculateParticleOffsets()
{ {
if (updateNearestRift()) if (updateNearestRift())
{ {
@ -218,68 +188,16 @@ public class TileEntityRift extends DDTileEntityBase
this.xOffset = this.xCoord - location.getX(); this.xOffset = this.xCoord - location.getX();
this.yOffset = this.yCoord - location.getY(); this.yOffset = this.yCoord - location.getY();
this.zOffset = this.zCoord - location.getZ(); this.zOffset = this.zCoord - location.getZ();
int distance = Math.abs(xOffset) + Math.abs(yOffset) + Math.abs(zOffset);
} }
else else
{ {
this.xOffset=0; this.xOffset = 0;
this.yOffset=0; this.yOffset = 0;
this.xOffset=0; this.xOffset = 0;
} }
this.onInventoryChanged(); this.onInventoryChanged();
} }
private void calculateNextRenderQuad(float age, Random rand)
{
int maxSize = MathHelper.floor_double((Math.log(Math.pow(age+1,2))));
int iteration=0;
while(iteration< maxSize)
{
iteration++;
double fl =Math.log(iteration+1)/(iteration);
double[] coords= new double[4];
double noise = ((rand.nextGaussian())/(2+iteration/3+1));
if(!this.renderingCenters.containsKey(iteration-1))
{
if (rand.nextBoolean())
{
coords[0] = fl*1.5;
coords[1] = rand.nextGaussian()/5;
coords[2] = 0;
coords[3] = 1;
}
else
{
coords[0] = 0;
coords[1] = rand.nextGaussian()/5;
coords[2] = fl*1.5;
coords[3] = 0;
}
this.renderingCenters.put(iteration-1,coords);
iteration--;
}
else if(!this.renderingCenters.containsKey(iteration))
{
if(this.renderingCenters.get(iteration-1)[3]==0)
{
coords[0]=noise/2+this.renderingCenters.get(iteration-1)[0];
coords[1]=noise/2+this.renderingCenters.get(iteration-1)[1];
coords[2]= this.renderingCenters.get(iteration-1)[2]+fl;
coords[3] = 0;
}
else
{
coords[0]=this.renderingCenters.get(iteration-1)[0]+fl;
coords[1]=noise/2+this.renderingCenters.get(iteration-1)[1];
coords[2]=noise/2+this.renderingCenters.get(iteration-1)[2];
coords[3] = 1;
}
this.renderingCenters.put(iteration,coords);
}
}
}
@Override @Override
public boolean shouldRenderInPass(int pass) public boolean shouldRenderInPass(int pass)
{ {
@ -292,13 +210,10 @@ public class TileEntityRift extends DDTileEntityBase
{ {
return countAncestorLinks(link.parent()) + 1; return countAncestorLinks(link.parent()) + 1;
} }
else
{
return 0; return 0;
} }
}
public void grow(DDProperties properties) public void spread(DDProperties properties)
{ {
if (worldObj.isRemote || !properties.RiftSpreadEnabled if (worldObj.isRemote || !properties.RiftSpreadEnabled
|| random.nextInt(MAX_RIFT_SPREAD_CHANCE) < RIFT_SPREAD_CHANCE || this.shouldClose) || random.nextInt(MAX_RIFT_SPREAD_CHANCE) < RIFT_SPREAD_CHANCE || this.shouldClose)
@ -329,13 +244,10 @@ public class TileEntityRift extends DDTileEntityBase
public void readFromNBT(NBTTagCompound nbt) public void readFromNBT(NBTTagCompound nbt)
{ {
super.readFromNBT(nbt); super.readFromNBT(nbt);
this.renderingCenters = new HashMap<Integer, double[]>(); this.updateTimer = nbt.getInteger("updateTimer");
this.updateTimer = nbt.getInteger("count");
this.riftCloseTimer = nbt.getInteger("count2");
this.xOffset = nbt.getInteger("xOffset"); this.xOffset = nbt.getInteger("xOffset");
this.yOffset = nbt.getInteger("yOffset"); this.yOffset = nbt.getInteger("yOffset");
this.zOffset = nbt.getInteger("zOffset"); this.zOffset = nbt.getInteger("zOffset");
this.age = nbt.getInteger("age");
this.shouldClose = nbt.getBoolean("shouldClose"); this.shouldClose = nbt.getBoolean("shouldClose");
this.spawnedEndermenID = nbt.getInteger("spawnedEndermenID"); this.spawnedEndermenID = nbt.getInteger("spawnedEndermenID");
} }
@ -344,9 +256,7 @@ public class TileEntityRift extends DDTileEntityBase
public void writeToNBT(NBTTagCompound nbt) public void writeToNBT(NBTTagCompound nbt)
{ {
super.writeToNBT(nbt); super.writeToNBT(nbt);
nbt.setInteger("age", this.age); nbt.setInteger("updateTimer", this.updateTimer);
nbt.setInteger("count", this.updateTimer);
nbt.setInteger("count2", this.riftCloseTimer);
nbt.setInteger("xOffset", this.xOffset); nbt.setInteger("xOffset", this.xOffset);
nbt.setInteger("yOffset", this.yOffset); nbt.setInteger("yOffset", this.yOffset);
nbt.setInteger("zOffset", this.zOffset); nbt.setInteger("zOffset", this.zOffset);