From 4472b68ecab8e73d665f66e3aeac1b70a11216e3 Mon Sep 17 00:00:00 2001 From: Quantum Date: Mon, 15 Nov 2021 02:43:01 -0500 Subject: [PATCH] "Optimized" EntityShockwave Do less shit per onUpdate(), its good for your tps Split explosion into a bunch of phases that run every onUpdate, code is ass but stopped the server from crashing Will need to do the same to EntityFalloutRain too but that's tomorrows problem Also tomorrows problem: make this less ass Also vim fixed a lot of whitespace things and I dont feel like dealing with that right now --- .../trinity/entities/EntityShockwave.java | 171 +++++++++++------- 1 file changed, 108 insertions(+), 63 deletions(-) diff --git a/src/main/java/trinity/entities/EntityShockwave.java b/src/main/java/trinity/entities/EntityShockwave.java index cdb928e..518eb98 100644 --- a/src/main/java/trinity/entities/EntityShockwave.java +++ b/src/main/java/trinity/entities/EntityShockwave.java @@ -45,13 +45,28 @@ import trinity.handler.Vec3; import trinity.init.ModBlocks; public class EntityShockwave extends Entity { - + private static final DataParameter SCALE = EntityDataManager.createKey(EntityShockwave.class, DataSerializers.VARINT); - - + + public int revProgress; public int radProgress; + private int iCounter = 0; + private boolean needsInit = true; + + private int blastState = 0; // Not started + private int blastCount = 0; + + private MutableBlockPos pos; + private MutableBlockPos pos2; + private MutableBlockPos pos3; + private MutableBlockPos pos4; + private MutableBlockPos pos5; + + private double dist; + private double circum; + public EntityShockwave(World p_i1582_1_) { super(p_i1582_1_); this.setSize(0, 0); @@ -83,87 +98,117 @@ public class EntityShockwave extends Entity { return bb; //return this.getEntityBoundingBox(); } - + @Override public void onUpdate() { if(!world.isRemote) { - MutableBlockPos pos = new BlockPos.MutableBlockPos(); - MutableBlockPos pos2 = new BlockPos.MutableBlockPos(); - MutableBlockPos pos3 = new BlockPos.MutableBlockPos(); - MutableBlockPos pos4 = new BlockPos.MutableBlockPos(); - MutableBlockPos pos5 = new BlockPos.MutableBlockPos(); - for(int i = 0; i < 512; i++) { - - Vec3 vec = Vec3.createVectorHelper(radProgress * 0.5, 0, 0); - double circum = radProgress * 2 * Math.PI * 2; - - /// - if(circum == 0) - circum = 1; - /// - - double part = 360D / circum; - - vec.rotateAroundY((float) (part * revProgress)); - - int x = (int) (posX + vec.xCoord); - int z = (int) (posZ + vec.zCoord); - - double dist = radProgress * 100 / getScale() * 0.5; - pos.setPos(x, posY, z); - pos2.setPos(x-1, posY, z); - pos3.setPos(x+1, posY, z); - pos4.setPos(x, posY, z-1); - pos5.setPos(x, posY, z-1); - blast(pos, dist); - blast(pos2, dist); - blast(pos3, dist); - blast(pos4, dist); - blast(pos5, dist); - - revProgress++; - - if(revProgress > circum) { - revProgress = 0; - radProgress++; - } - - if(radProgress > getScale() * 2D) { - - this.setDead(); - } + if(needsInit) { + pos = new BlockPos.MutableBlockPos(); + pos2 = new BlockPos.MutableBlockPos(); + pos3 = new BlockPos.MutableBlockPos(); + pos4 = new BlockPos.MutableBlockPos(); + pos5 = new BlockPos.MutableBlockPos(); + needsInit = false; + } + if(iCounter < 512*3) { + if(blastState == 0) { // Not started + Vec3 vec = Vec3.createVectorHelper(radProgress * 0.5, 0, 0); + circum = radProgress * 2 * Math.PI * 2; + + /// + if(circum == 0) + circum = 1; + /// + + double part = 360D / circum; + + vec.rotateAroundY((float) (part * revProgress)); + + int x = (int) (posX + vec.xCoord); + int z = (int) (posZ + vec.zCoord); + + dist = radProgress * 100 / getScale() * 0.5; + pos.setPos(x, posY, z); + pos2.setPos(x-1, posY, z); + pos3.setPos(x+1, posY, z); + pos4.setPos(x, posY, z-1); + pos5.setPos(x, posY, z-1); + blastState = 1; + } else if(blastState == 1) { + switch(blastCount) { + case 0: + blast(pos, dist); + blastCount++; + break; + case 1: + blast(pos2, dist); + blastCount++; + break; + case 2: + blast(pos3, dist); + blastCount++; + break; + case 3: + blast(pos4, dist); + blastCount++; + break; + case 4: + blast(pos5, dist); + blastCount++; + break; + case 5: + blastState = 2; + blastCount = 0; + break; + } + } else if(blastState == 2) { + revProgress++; + + if(revProgress > circum) { + revProgress = 0; + radProgress++; + } + + if(radProgress > getScale() * 2D) { + + this.setDead(); + blastState = 4; + } + blastState = 0; + } } + iCounter++; } } - + private void blast(MutableBlockPos pos, double dist) { - + int depth = 0; - + int topBlock =world.getTopSolidOrLiquidBlock(pos).getY(); - + for(int y = (topBlock-8); y <= topBlock+48; y++) { pos.setY(y); IBlockState b = world.getBlockState(pos); //int meta = world.getBlockMetadata(x, y, z); - - + + BlockPos left = new BlockPos(pos.getX()+1,pos.getY(),pos.getZ()); BlockPos right = new BlockPos(pos.getX()-1,pos.getY(),pos.getZ()); BlockPos up = new BlockPos(pos.getX(),pos.getY()+1,pos.getZ()); //BlockPos down = pos.add(0, -1, 0); BlockPos down2 = new BlockPos(pos.getX(),pos.getY()-1,pos.getZ()); BlockPos front = pos.add(0, 0, 1); - BlockPos back = pos.add(0, 0, -1); - + BlockPos back = pos.add(0, 0, -1); + boolean LR = (world.isAirBlock(left) && world.isAirBlock(right)); //boolean UD = (world.isAirBlock(up) && world.isAirBlock(down)); boolean FB = (world.isAirBlock(front) && world.isAirBlock(back)); - + IBlockState bd = world.getBlockState(down2); - + if((b.getMaterial() != Material.AIR || (b.getBlock()instanceof BlockFluidClassic)) && bd.getMaterial() == Material.AIR) { if(b.getBlock().getExplosionResistance(null)<=100) @@ -174,13 +219,13 @@ public class EntityShockwave extends Entity { continue; } } - + if(b.getMaterial() != Material.AIR && (LR || FB)) { world.setBlockToAir(pos); continue; } - + if(b.getMaterial() == Material.AIR) continue; @@ -206,7 +251,7 @@ public class EntityShockwave extends Entity { p_70014_1_.setInteger("scale", getScale()); p_70014_1_.setInteger("revProgress", revProgress); p_70014_1_.setInteger("radProgress", radProgress); - + } public void setScale(int i) { @@ -216,7 +261,7 @@ public class EntityShockwave extends Entity { public int getScale() { int scale = this.dataManager.get(SCALE); - + return scale == 0 ? 1 : scale; } }