From abf8b915a12d91ba32ef0eca3c477e253d0acdd5 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 20:38:37 +0400 Subject: [PATCH 01/16] Normalize line endings --- src/cr0s/WarpDrive/SpaceEventHandler.java | 164 +++++++++++----------- src/cr0s/serverMods/ServerMods.java | 88 ++++++------ 2 files changed, 126 insertions(+), 126 deletions(-) diff --git a/src/cr0s/WarpDrive/SpaceEventHandler.java b/src/cr0s/WarpDrive/SpaceEventHandler.java index dc42e01f..0957ea3f 100644 --- a/src/cr0s/WarpDrive/SpaceEventHandler.java +++ b/src/cr0s/WarpDrive/SpaceEventHandler.java @@ -1,82 +1,82 @@ -/* - * Невесомость и отключение текучести жидкостей - */ -package cr0s.WarpDrive; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLiving; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.util.DamageSource; -import net.minecraft.util.MathHelper; -import net.minecraftforge.common.DimensionManager; -import net.minecraftforge.event.ForgeSubscribe; -import net.minecraftforge.event.entity.EntityJoinWorldEvent; -import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; -import net.minecraftforge.common.ForgeHooks; - -/** - * Обработчик событий в мире Space - * @author Cr0s - */ -public class SpaceEventHandler { - @ForgeSubscribe - public void livingUpdate(LivingUpdateEvent event) { - EntityLiving entity = event.entityLiving; - - final int HELMET_ID_SKUBA = 30082; - final int HELMET_ID_QUANTUM = 30174; - final int HELMET_ID_ADV_SOLAR = 30832; - final int HELMET_ID_HYB_SOLAR = 30833; - final int HELMET_ID_ULT_SOLAR = 30834; - final int HELMET_HEAD = 397; - - // Движение происходит в космическом пространстве - if (entity.worldObj.provider.dimensionId == WarpDrive.instance.spaceDimID) { - if (entity instanceof EntityPlayerMP) { - - if (isEntityInVacuum(entity)) { - if (!(entity.getCurrentArmor(3) != null && (entity.getCurrentArmor(3).itemID == HELMET_ID_SKUBA || entity.getCurrentArmor(3).itemID == HELMET_ID_QUANTUM || entity.getCurrentArmor(3).itemID == HELMET_HEAD - || entity.getCurrentArmor(3).itemID == HELMET_ID_ADV_SOLAR || entity.getCurrentArmor(3).itemID == HELMET_ID_HYB_SOLAR || entity.getCurrentArmor(3).itemID == HELMET_ID_ULT_SOLAR))) { - entity.attackEntityFrom(DamageSource.drown, 3); - } - } - - // Отправить назад на Землю - if (entity.posY < -10.0D) { - ((EntityPlayerMP)entity).mcServer.getConfigurationManager().transferPlayerToDimension(((EntityPlayerMP) entity), 0, new SpaceTeleporter(DimensionManager.getWorld(WarpDrive.instance.spaceDimID), 0, MathHelper.floor_double(entity.posX), 250, MathHelper.floor_double(entity.posZ))); - ((EntityPlayerMP)entity).setFire(30); - ((EntityPlayerMP)entity).setPositionAndUpdate(entity.posX, 250D, entity.posZ); - } - } - } - } - - /** - * Проверка, находится ли Entity в открытом космосе - * @param e - * @return - */ - private boolean isEntityInVacuum(Entity e) { - - int x = MathHelper.floor_double(e.posX); - int y = MathHelper.floor_double(e.posY); - int z = MathHelper.floor_double(e.posZ); - - final int CHECK_DISTANCE = 10; - - if (e.onGround) { return false; } - - for (int ny = y; ny > (y - CHECK_DISTANCE); ny--) { - if (!e.worldObj.isAirBlock(x, ny, z)) { - return false; - } - } - - if (!e.worldObj.canBlockSeeTheSky(x, y, z) || !e.worldObj.canBlockSeeTheSky(x, y - 1, z) ) { - return false; - } - - return true; - } -} +/* + * Невесомость и отключение текучести жидкостей + */ +package cr0s.WarpDrive; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.DamageSource; +import net.minecraft.util.MathHelper; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.event.ForgeSubscribe; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; +import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; +import net.minecraftforge.common.ForgeHooks; + +/** + * Обработчик событий в мире Space + * @author Cr0s + */ +public class SpaceEventHandler { + @ForgeSubscribe + public void livingUpdate(LivingUpdateEvent event) { + EntityLiving entity = event.entityLiving; + + final int HELMET_ID_SKUBA = 30082; + final int HELMET_ID_QUANTUM = 30174; + final int HELMET_ID_ADV_SOLAR = 30832; + final int HELMET_ID_HYB_SOLAR = 30833; + final int HELMET_ID_ULT_SOLAR = 30834; + final int HELMET_HEAD = 397; + + // Движение происходит в космическом пространстве + if (entity.worldObj.provider.dimensionId == WarpDrive.instance.spaceDimID) { + if (entity instanceof EntityPlayerMP) { + + if (isEntityInVacuum(entity)) { + if (!(entity.getCurrentArmor(3) != null && (entity.getCurrentArmor(3).itemID == HELMET_ID_SKUBA || entity.getCurrentArmor(3).itemID == HELMET_ID_QUANTUM || entity.getCurrentArmor(3).itemID == HELMET_HEAD + || entity.getCurrentArmor(3).itemID == HELMET_ID_ADV_SOLAR || entity.getCurrentArmor(3).itemID == HELMET_ID_HYB_SOLAR || entity.getCurrentArmor(3).itemID == HELMET_ID_ULT_SOLAR))) { + entity.attackEntityFrom(DamageSource.drown, 3); + } + } + + // Отправить назад на Землю + if (entity.posY < -10.0D) { + ((EntityPlayerMP)entity).mcServer.getConfigurationManager().transferPlayerToDimension(((EntityPlayerMP) entity), 0, new SpaceTeleporter(DimensionManager.getWorld(WarpDrive.instance.spaceDimID), 0, MathHelper.floor_double(entity.posX), 250, MathHelper.floor_double(entity.posZ))); + ((EntityPlayerMP)entity).setFire(30); + ((EntityPlayerMP)entity).setPositionAndUpdate(entity.posX, 250D, entity.posZ); + } + } + } + } + + /** + * Проверка, находится ли Entity в открытом космосе + * @param e + * @return + */ + private boolean isEntityInVacuum(Entity e) { + + int x = MathHelper.floor_double(e.posX); + int y = MathHelper.floor_double(e.posY); + int z = MathHelper.floor_double(e.posZ); + + final int CHECK_DISTANCE = 10; + + if (e.onGround) { return false; } + + for (int ny = y; ny > (y - CHECK_DISTANCE); ny--) { + if (!e.worldObj.isAirBlock(x, ny, z)) { + return false; + } + } + + if (!e.worldObj.canBlockSeeTheSky(x, y, z) || !e.worldObj.canBlockSeeTheSky(x, y - 1, z) ) { + return false; + } + + return true; + } +} diff --git a/src/cr0s/serverMods/ServerMods.java b/src/cr0s/serverMods/ServerMods.java index 92d61db4..248f5c63 100644 --- a/src/cr0s/serverMods/ServerMods.java +++ b/src/cr0s/serverMods/ServerMods.java @@ -1,45 +1,45 @@ -package cr0s.serverMods; - -import cpw.mods.fml.common.Mod; -import cpw.mods.fml.common.Mod.Init; -import cpw.mods.fml.common.Mod.Instance; -import cpw.mods.fml.common.Mod.PostInit; -import cpw.mods.fml.common.Mod.PreInit; -import cpw.mods.fml.common.event.FMLInitializationEvent; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.network.NetworkMod; -import net.minecraftforge.common.MinecraftForge; -import cr0s.WarpDrive.SpaceEventHandler; - -@Mod(modid="ServerMods", name="ServerMods", version="0.0.1") -@NetworkMod(clientSideRequired = false, serverSideRequired = true, connectionHandler = LoginHookClass.class) - -/** - * @author Cr0s - */ -public class ServerMods { - - // The instance of your mod that Forge uses. - @Instance("ServerMods") - public static ServerMods instance; - - @PreInit - public void preInit(FMLPreInitializationEvent event) { - // Stub Method - } - - @Init - public void load(FMLInitializationEvent event) { - // Включить авторизацию (включается автоматически) - //proxy.setupLoginHook(); - - // Снять урон от падения с джетпаком и крузис-тапками - MinecraftForge.EVENT_BUS.register(new AntiFallDamage()); - } - - @PostInit - public void postInit(FMLPostInitializationEvent event) { - // Stub Method - } +package cr0s.serverMods; + +import cpw.mods.fml.common.Mod; +import cpw.mods.fml.common.Mod.Init; +import cpw.mods.fml.common.Mod.Instance; +import cpw.mods.fml.common.Mod.PostInit; +import cpw.mods.fml.common.Mod.PreInit; +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.network.NetworkMod; +import net.minecraftforge.common.MinecraftForge; +import cr0s.WarpDrive.SpaceEventHandler; + +@Mod(modid="ServerMods", name="ServerMods", version="0.0.1") +@NetworkMod(clientSideRequired = false, serverSideRequired = true, connectionHandler = LoginHookClass.class) + +/** + * @author Cr0s + */ +public class ServerMods { + + // The instance of your mod that Forge uses. + @Instance("ServerMods") + public static ServerMods instance; + + @PreInit + public void preInit(FMLPreInitializationEvent event) { + // Stub Method + } + + @Init + public void load(FMLInitializationEvent event) { + // Включить авторизацию (включается автоматически) + //proxy.setupLoginHook(); + + // Снять урон от падения с джетпаком и крузис-тапками + MinecraftForge.EVENT_BUS.register(new AntiFallDamage()); + } + + @PostInit + public void postInit(FMLPostInitializationEvent event) { + // Stub Method + } } \ No newline at end of file From 53c60b64f138329cd0ab3bea19cc0af01ca7a4ab Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 20:52:05 +0400 Subject: [PATCH 02/16] Refactoring --- src/cr0s/WarpDrive/EntityJump.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 2ba92eaa..736766d1 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -87,7 +87,6 @@ public class EntityJump extends Entity { this.dir = _direction; shipLeft = shipRight = shipFront = shipBack = shipDown = shipUp = shipLength = 0; - this.dx = this.dz = 0; this.dx = _dx; this.dz = _dz; Xmax = Zmax = maxY = Xmin = Zmin = minY = 0; @@ -128,32 +127,34 @@ public class EntityJump extends Entity { worldObj.removeEntity(this); return; } - + if (minY < 0 || maxY > 256) { killEntity("Y-coord error!"); return; } - + // Skip tick, awaiting chunk generation if ((getTargetWorld().provider.dimensionId == worldObj.provider.dimensionId) && !checkForChunksGeneratedIn(getTargetWorld())) { return; } - + if (!isJumping) { this.toSpace = (dir == -1 && (maxY + distance > 255) && worldObj.provider.dimensionId != WarpDrive.instance.spaceDimID); this.fromSpace = (dir == -2 && (minY - distance < 0) && worldObj.provider.dimensionId == WarpDrive.instance.spaceDimID); - + System.out.println("[JE] Preparing to jump..."); axisalignedbb = AxisAlignedBB.getBoundingBox(Xmin, minY, Zmin, Xmax, maxY, Zmax); - + prepareToJump(); + + isJumping = true; } else { if (currentIndexInShip >= ship.length-1) { isJumping = false; finishJump(); } else { - moveEntitys(axisalignedbb, distance, dir, true); - moveShip(); + moveEntitys(axisalignedbb, distance, dir, true); + moveShip(); } } } @@ -314,27 +315,26 @@ public class EntityJump extends Entity { saveShip(shipSize); setBlocksUnderPlayers(false); - isJumping = true; this.currentIndexInShip = 0; msCounter = System.currentTimeMillis(); } - + /** * Finish jump: move entities, unlock worlds and delete self */ public void finishJump() { moveEntitys(axisalignedbb, distance, dir, false); setBlocksUnderPlayers(true); - + removeShip(); - + System.out.println("[JE] Finished. Jump took " + ((System.currentTimeMillis() - msCounter) / 1000F) + " seconds"); - + // Прыжок окончен - killEntity(""); + killEntity(""); } - + /** * Removing ship from world * From 79a71b7c64c28a061d378cc5eff6aa8832096365 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:07:49 +0400 Subject: [PATCH 03/16] Refactoring: precalculate target world --- src/cr0s/WarpDrive/EntityJump.java | 31 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 736766d1..c906398c 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -44,6 +44,7 @@ public class EntityJump extends Entity { public int minY; public int dx; public int dz; + public World targetWorld; // Collision point coordinates public int blowX, blowY, blowZ; boolean needToExplode = false; @@ -134,7 +135,7 @@ public class EntityJump extends Entity { } // Skip tick, awaiting chunk generation - if ((getTargetWorld().provider.dimensionId == worldObj.provider.dimensionId) && !checkForChunksGeneratedIn(getTargetWorld())) { + if ((targetWorld == worldObj) && !checkForChunksGeneratedIn(targetWorld)) { return; } @@ -159,31 +160,21 @@ public class EntityJump extends Entity { } } - public World getTargetWorld() { - if (toSpace) { - return DimensionManager.getWorld(WarpDrive.instance.spaceDimID); - } else if (fromSpace) { - return DimensionManager.getWorld(0); - } else { - return this.worldObj; - } - } - public void lockWorlds() { System.out.println("Locking worlds..."); - getTargetWorld().isRemote = true; + targetWorld.isRemote = true; // When warping between dimensions is need to lock both worlds - if (getTargetWorld().provider.dimensionId != worldObj.provider.dimensionId) { + if (targetWorld.provider.dimensionId != worldObj.provider.dimensionId) { worldObj.isRemote = true; } } public void unlockWorlds() { System.out.println("Unlocking worlds.."); - getTargetWorld().isRemote = false; + targetWorld.isRemote = false; - if (getTargetWorld().provider.dimensionId != worldObj.provider.dimensionId) { + if (targetWorld.provider.dimensionId != worldObj.provider.dimensionId) { worldObj.isRemote = false; } } @@ -274,6 +265,14 @@ public class EntityJump extends Entity { betweenWorlds = fromSpace || toSpace; + if (toSpace) { + targetWorld = DimensionManager.getWorld(WarpDrive.instance.spaceDimID); + } else if (fromSpace) { + targetWorld = DimensionManager.getWorld(0); + } else { + targetWorld = this.worldObj; + } + saveEntitys(axisalignedbb); System.out.println("[JE] Saved " + entityOnShip.size() + " entities from ship"); @@ -1058,7 +1057,7 @@ public class EntityJump extends Entity { } } - newTileEntity.worldObj = getTargetWorld(); + newTileEntity.worldObj = targetWorld; newTileEntity.validate(); if (!toSpace && !fromSpace) From d99fbb398c4c78cd696b1d14ca0b1bbffc5868a5 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:22:25 +0400 Subject: [PATCH 04/16] Refactoring: get rid of outer loop --- src/cr0s/WarpDrive/EntityJump.java | 36 ++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index c906398c..4987d2bc 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -217,25 +217,23 @@ public class EntityJump extends Entity { * @return */ public boolean checkForChunksGeneratedIn(World w) { - for (int y = minY; y <= maxY; y++) { - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { - final int newX = getNewXCoord(x, 0, z, this.distance, this.dir); - final int newZ = getNewZCoord(x, 0, z, this.distance, this.dir); - - int chunkX = newX >> 4; - int chunkZ = newZ >> 4; - - if (!w.getChunkProvider().chunkExists(chunkX, chunkZ)) { - messageToAllPlayersOnShip("Generating chunks..."); - w.getBlockId(newX, 128, newZ); - - return false; - } + // TODO: ходить не по координатам, а по координатам чанков, так быстрее. + for (int x = Xmin; x <= Xmax; x++) { + for (int z = Zmin; z <= Zmax; z++) { + final int newX = getNewXCoord(x, 0, z, this.distance, this.dir); + final int newZ = getNewZCoord(x, 0, z, this.distance, this.dir); + + int chunkX = newX >> 4; + int chunkZ = newZ >> 4; + + if (!w.getChunkProvider().chunkExists(chunkX, chunkZ)) { + messageToAllPlayersOnShip("Generating chunks..."); + w.getBlockId(newX, 128, newZ); + + return false; } } - } - + } return true; } @@ -247,10 +245,10 @@ public class EntityJump extends Entity { if (!(obj instanceof MovingEntity)) { continue; } - + MovingEntity me = (MovingEntity)obj; Entity entity = me.entity; - + if (entity instanceof EntityPlayer) { ((EntityPlayer)entity).sendChatToPlayer("[WarpCore] " + msg); } From 7d8a2d8af246684638a88f323ec2b3f5db8bd7b2 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:24:55 +0400 Subject: [PATCH 05/16] Refactoring: rename --- src/cr0s/WarpDrive/EntityJump.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 4987d2bc..a34f3189 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -154,7 +154,7 @@ public class EntityJump extends Entity { isJumping = false; finishJump(); } else { - moveEntitys(axisalignedbb, distance, dir, true); + moveEntities(axisalignedbb, distance, dir, true); moveShip(); } } @@ -271,9 +271,9 @@ public class EntityJump extends Entity { targetWorld = this.worldObj; } - saveEntitys(axisalignedbb); - System.out.println("[JE] Saved " + entityOnShip.size() + " entities from ship"); - + saveEntities(axisalignedbb); + System.out.println("[JE] Saved " + entityOnShip.size() + " entities from ship"); + if (!isCoordJump) { if (dir != -2 && dir != -1) { messageToAllPlayersOnShip("Jumping in direction " + dir + " degrees to distance " + distance + " blocks "); @@ -321,7 +321,7 @@ public class EntityJump extends Entity { * Finish jump: move entities, unlock worlds and delete self */ public void finishJump() { - moveEntitys(axisalignedbb, distance, dir, false); + moveEntities(axisalignedbb, distance, dir, false); setBlocksUnderPlayers(true); removeShip(); @@ -496,7 +496,7 @@ public class EntityJump extends Entity { return shipSize; } - public void saveEntitys(AxisAlignedBB axisalignedbb) { + public void saveEntities(AxisAlignedBB axisalignedbb) { this.entityOnShip = new ArrayList(); List list = worldObj.getEntitiesWithinAABBExcludingEntity(null, axisalignedbb); @@ -522,7 +522,7 @@ public class EntityJump extends Entity { * @param restorePositions восстановление старых позиций для предотвращения выпадения, либо перемещение на новую * @return */ - public boolean moveEntitys(AxisAlignedBB axisalignedbb, int distance, int direction, boolean restorePositions) { + public boolean moveEntities(AxisAlignedBB axisalignedbb, int distance, int direction, boolean restorePositions) { List list = this.entityOnShip; if (list != null) { From fc2a565ca245d151b87bd87c112e1cb797698f79 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:33:33 +0400 Subject: [PATCH 06/16] Refactoring: store entity position as double --- src/cr0s/WarpDrive/EntityJump.java | 15 ++++++++------- src/cr0s/WarpDrive/MovingEntity.java | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index a34f3189..15dabe5a 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -198,11 +198,11 @@ public class EntityJump extends Entity { if (entity instanceof EntityPlayer) { if (!removeBlocks) { - mySetBlock(worldObj, me.oldX, me.oldY - 2, me.oldZ, Block.dirt.blockID, 0, 1 + 2); + mySetBlock(worldObj, (int)me.oldX, (int)me.oldY - 2, (int)me.oldZ, Block.dirt.blockID, 0, 1 + 2); } else { - if (worldObj.getBlockId(me.oldX, me.oldY - 2, me.oldZ) == Block.dirt.blockID) { - mySetBlock(worldObj, me.oldX, me.oldY - 2, me.oldZ, 0, 0, 1 + 2); + if (worldObj.getBlockId((int)me.oldX, (int)me.oldY - 2, (int)me.oldZ) == Block.dirt.blockID) { + mySetBlock(worldObj, (int)me.oldX, (int)me.oldY - 2, (int)me.oldZ, 0, 0, 1 + 2); } } } @@ -507,7 +507,7 @@ public class EntityJump extends Entity { Entity entity = (Entity)o; - MovingEntity movingentity = new MovingEntity(entity, MathHelper.floor_double(entity.posX), MathHelper.floor_double(entity.posY), MathHelper.floor_double(entity.posZ)); + MovingEntity movingentity = new MovingEntity(entity, entity.posX, entity.posY, entity.posZ); // Добавим в список Entity entityOnShip.add(movingentity); @@ -537,9 +537,10 @@ public class EntityJump extends Entity { if (me == null) { continue; } - int oldEntityX = me.oldX; - int oldEntityY = me.oldY; - int oldEntityZ = me.oldZ; + // TODO: пересчитывать всё в вещественных координатах + int oldEntityX = (int)me.oldX; + int oldEntityY = (int)me.oldY; + int oldEntityZ = (int)me.oldZ; int newEntityX, newEntityY, newEntityZ; diff --git a/src/cr0s/WarpDrive/MovingEntity.java b/src/cr0s/WarpDrive/MovingEntity.java index 5c13aef9..103bdbc0 100644 --- a/src/cr0s/WarpDrive/MovingEntity.java +++ b/src/cr0s/WarpDrive/MovingEntity.java @@ -11,13 +11,13 @@ import net.minecraft.entity.Entity; * @author user */ public class MovingEntity { - public int oldX; - public int oldY; - public int oldZ; + public double oldX; + public double oldY; + public double oldZ; public Entity entity; - public MovingEntity(Entity e, int x, int y, int z) { + public MovingEntity(Entity e, double x, double y, double z) { this.entity = e; this.oldX = x; From f293d3a0b2cc5b3f14825e1113b05084f1ae5574 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:39:56 +0400 Subject: [PATCH 07/16] Refactoring: block movement --- src/cr0s/WarpDrive/EntityJump.java | 48 +++--------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 15dabe5a..5c2559ab 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -392,28 +392,13 @@ public class EntityJump extends Entity { *Ship moving */ public void moveShip() { - int blocksToMove = Math.min(BLOCKS_PER_TICK, ship.length - this.currentIndexInShip); + int blocksToMove = Math.min(BLOCKS_PER_TICK, ship.length - currentIndexInShip); System.out.println("[JE] Moving ship part: " + currentIndexInShip + "/" + ship.length + " [btm: " + blocksToMove + "]"); - // 1. Jump to space - if (toSpace) { - for (int index = 0; index < blocksToMove; index++) { - moveBlockToSpace(currentIndexInShip, distance, dir); - this.currentIndexInShip++; - } - // 2. Jump from space - } else if (fromSpace) { - for (int index = 0; index < blocksToMove; index++) { - moveBlockFromSpace(currentIndexInShip, distance, dir); - this.currentIndexInShip++; - } - // 3. Basic jump - } else { - for (int index = 0; index < blocksToMove; index++) { - moveBlock(currentIndexInShip, distance, dir); - this.currentIndexInShip++; - } + for (int index = 0; index < blocksToMove; index++) { + moveBlockSimple(currentIndexInShip, distance, dir, toSpace, fromSpace); + currentIndexInShip++; } } @@ -924,31 +909,6 @@ public class EntityJump extends Entity { return x >= Xmin && x <= Xmax && y >= minY && y <= maxY && z >= Zmin && z <= Zmax; } - public boolean moveBlock(int indexInShip, int distance, int direction) { - return moveBlockSimple(indexInShip, distance, direction, false, false); - } - - /** - * Перемещение одиночного блока из обычного мира в космос - * @param indexInShip - * @param distance - * @param direction - * @return - */ - public boolean moveBlockToSpace(int indexInShip, int distance, int direction) { - return moveBlockSimple(indexInShip, distance, direction, true, false); - } - - /** - * Перемещение одиночного блока из космоса в обычный мир - * @param indexInShip - * @param distance - * @param direction - * @return - */ - public boolean moveBlockFromSpace(int indexInShip, int distance, int direction) { - return moveBlockSimple(indexInShip, distance, direction, false, true); - } /** * Перемещение одиночного блока на новое место From b5d0df7fe2778fdc782d836a7cf5a535cfd46dd9 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:48:39 +0400 Subject: [PATCH 08/16] Refactoring: rename --- src/cr0s/WarpDrive/EntityJump.java | 62 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 5c2559ab..46775032 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -1053,24 +1053,24 @@ public class EntityJump extends Entity { } // Own implementation of setting blocks withow light recalculation in optimization purposes - public boolean mySetBlock(World w, int par1, int par2, int par3, int par4, int par5, int par6) + public boolean mySetBlock(World w, int x, int y, int z, int blockId, int blockMeta, int par6) { - if (par1 >= -30000000 && par3 >= -30000000 && par1 < 30000000 && par3 < 30000000) + if (x >= -30000000 && z >= -30000000 && x < 30000000 && z < 30000000) { - if (par2 < 0) + if (y < 0) { return false; } - else if (par2 >= 256) + else if (y >= 256) { return false; } else { - w.markBlockForUpdate(par1, par2, par3); - Chunk chunk = w.getChunkFromChunkCoords(par1 >> 4, par3 >> 4); + w.markBlockForUpdate(x, y, z); + Chunk chunk = w.getChunkFromChunkCoords(x >> 4, z >> 4); - return myChunkSBIDWMT(chunk, par1 & 15, par2, par3 & 15, par4, par5); + return myChunkSBIDWMT(chunk, x & 15, y, z & 15, blockId, blockMeta); } } else @@ -1081,65 +1081,65 @@ public class EntityJump extends Entity { // Incapsulation violation warning: // field Chunk.storageArrays has been turned from private to public in class Chunk.java - public boolean myChunkSBIDWMT(Chunk c, int par1, int par2, int par3, int par4, int par5) + public boolean myChunkSBIDWMT(Chunk c, int x, int y, int z, int blockId, int blockMeta) { - int j1 = par3 << 4 | par1; + int j1 = z << 4 | x; - if (par2 >= c.precipitationHeightMap[j1] - 1) + if (y >= c.precipitationHeightMap[j1] - 1) { c.precipitationHeightMap[j1] = -999; } int k1 = c.heightMap[j1]; - int l1 = c.getBlockID(par1, par2, par3); - int i2 = c.getBlockMetadata(par1, par2, par3); + int l1 = c.getBlockID(x, y, z); + int i2 = c.getBlockMetadata(x, y, z); - if (l1 == par4 && i2 == par5) + if (l1 == blockId && i2 == blockMeta) { return false; } else { - ExtendedBlockStorage extendedblockstorage = c.storageArrays[par2 >> 4]; + ExtendedBlockStorage extendedblockstorage = c.storageArrays[y >> 4]; if (extendedblockstorage == null) { - if (par4 == 0) + if (blockId == 0) { return false; } - extendedblockstorage = c.storageArrays[par2 >> 4] = new ExtendedBlockStorage(par2 >> 4 << 4, !c.worldObj.provider.hasNoSky); + extendedblockstorage = c.storageArrays[y >> 4] = new ExtendedBlockStorage(y >> 4 << 4, !c.worldObj.provider.hasNoSky); } - int j2 = c.xPosition * 16 + par1; - int k2 = c.zPosition * 16 + par3; + int j2 = c.xPosition * 16 + x; + int k2 = c.zPosition * 16 + z; - extendedblockstorage.setExtBlockID(par1, par2 & 15, par3, par4); + extendedblockstorage.setExtBlockID(x, y & 15, z, blockId); if (l1 != 0) { if (!c.worldObj.isRemote) { - Block.blocksList[l1].breakBlock(c.worldObj, j2, par2, k2, l1, i2); + Block.blocksList[l1].breakBlock(c.worldObj, j2, y, k2, l1, i2); } else if (Block.blocksList[l1] != null && Block.blocksList[l1].hasTileEntity(i2)) { - TileEntity te = worldObj.getBlockTileEntity(j2, par2, k2); - if (te != null && te.shouldRefresh(l1, par4, i2, par5, worldObj, j2, par2, k2)) + TileEntity te = worldObj.getBlockTileEntity(j2, y, k2); + if (te != null && te.shouldRefresh(l1, blockId, i2, blockMeta, worldObj, j2, y, k2)) { - c.worldObj.removeBlockTileEntity(j2, par2, k2); + c.worldObj.removeBlockTileEntity(j2, y, k2); } } } - if (extendedblockstorage.getExtBlockID(par1, par2 & 15, par3) != par4) + if (extendedblockstorage.getExtBlockID(x, y & 15, z) != blockId) { return false; } else { - extendedblockstorage.setExtBlockMetadata(par1, par2 & 15, par3, par5); + extendedblockstorage.setExtBlockMetadata(x, y & 15, z, blockMeta); // Removed light recalcalations /*if (flag) @@ -1165,22 +1165,22 @@ public class EntityJump extends Entity { TileEntity tileentity; - if (par4 != 0) + if (blockId != 0) { - if (Block.blocksList[par4] != null && Block.blocksList[par4].hasTileEntity(par5)) + if (Block.blocksList[blockId] != null && Block.blocksList[blockId].hasTileEntity(blockMeta)) { - tileentity = c.getChunkBlockTileEntity(par1, par2, par3); + tileentity = c.getChunkBlockTileEntity(x, y, z); if (tileentity == null) { - tileentity = Block.blocksList[par4].createTileEntity(c.worldObj, par5); - c.worldObj.setBlockTileEntity(j2, par2, k2, tileentity); + tileentity = Block.blocksList[blockId].createTileEntity(c.worldObj, blockMeta); + c.worldObj.setBlockTileEntity(j2, y, k2, tileentity); } if (tileentity != null) { tileentity.updateContainingBlockInfo(); - tileentity.blockMetadata = par5; + tileentity.blockMetadata = blockMeta; } } } From f352bacc040a3fc5aac2bb020cb403ed255c725c Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:52:21 +0400 Subject: [PATCH 09/16] Refactoring: delete useless function --- src/cr0s/WarpDrive/EntityJump.java | 36 ------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 46775032..f07fda8f 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -690,42 +690,6 @@ public class EntityJump extends Entity { return result; } - public int rotsincos(int i, boolean flag) { - // sin - if (flag) { - switch (i) { - case 0: - return 0; - - case 90: - return 1; - - case 180: - return 0; - - case 270: - return -1; - } - } // cos - else { - switch (i) { - case 0: - return 1; - - case 90: - return 0; - - case 180: - return -1; - - case 270: - return 0; - } - } - - return 0; - } - // Получение вектора в зависимости от направления прыжка // (3,14здец, конечно) public int[] getVector(int i) { From f95a0026adc25d55dfd97c38e4b899f958580228 Mon Sep 17 00:00:00 2001 From: Disasm Date: Tue, 9 Jul 2013 21:57:26 +0400 Subject: [PATCH 10/16] Refactoring: getVector simplification --- src/cr0s/WarpDrive/EntityJump.java | 126 ++++++----------------------- 1 file changed, 24 insertions(+), 102 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index f07fda8f..19a98faf 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -691,119 +691,41 @@ public class EntityJump extends Entity { } // Получение вектора в зависимости от направления прыжка - // (3,14здец, конечно) public int[] getVector(int i) { - int ai[] = { + int v[] = { 0, 0, 0 }; - if (dz == 1) { - switch (i) { - case 0: - ai[0] = 0; - ai[1] = 0; - ai[2] = 1; - break; + switch(i) { + case -1: + v[1] = 1; + break; - case 90: - ai[0] = 1; - ai[1] = 0; - ai[2] = 0; - break; + case -2: + v[1] = -1; + break; - case 180: - ai[0] = 0; - ai[1] = 0; - ai[2] = -1; - break; + case 0: + v[0] = dx; + v[2] = dz; + break; - case 270: - ai[0] = -1; - ai[1] = 0; - ai[2] = 0; - break; - } - } else if (dz == -1) { - switch (i) { - case 0: - ai[0] = 0; - ai[1] = 0; - ai[2] = -1; - break; + case 180: + v[0] = -dx; + v[2] = -dz; + break; - case 90: - ai[0] = -1; - ai[1] = 0; - ai[2] = 0; - break; + case 90: + v[0] = dz; + v[2] = -dx; + break; - case 180: - ai[0] = 0; - ai[1] = 0; - ai[2] = 1; - break; - - case 270: - ai[0] = 1; - ai[1] = 0; - ai[2] = 0; - break; - } - } else if (dx == 1) { - switch (i) { - case 0: - ai[0] = 1; - ai[1] = 0; - ai[2] = 0; - break; - - case 90: - ai[0] = 0; - ai[1] = 0; - ai[2] = -1; - break; - - case 180: - ai[0] = -1; - ai[1] = 0; - ai[2] = 0; - break; - - case 270: - ai[0] = 0; - ai[1] = 0; - ai[2] = 1; - break; - } - } else if (dx == -1) { - switch (i) { - case 0: - ai[0] = -1; - ai[1] = 0; - ai[2] = 0; - break; - - case 90: - ai[0] = 0; - ai[1] = 0; - ai[2] = 1; - break; - - case 180: - ai[0] = 1; - ai[1] = 0; - ai[2] = 0; - break; - - case 270: - ai[0] = 0; - ai[1] = 0; - ai[2] = -1; - break; - } + case 270: + v[0] = -dz; + v[2] = dx; } - return ai; + return v; } /** From ee40e765ae7c4e7af7af5752362c9d8a111696e7 Mon Sep 17 00:00:00 2001 From: Disasm Date: Wed, 10 Jul 2013 21:31:13 +0400 Subject: [PATCH 11/16] Bugfix --- src/cr0s/WarpDrive/EntityJump.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 19a98faf..3bf36a46 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -70,6 +70,8 @@ public class EntityJump extends Entity { public EntityJump(World world) { super(world); + + targetWorld = worldObj; } public EntityJump(World world, int x, int y, int z, int _dist, int _direction, int _dx, int _dz, TileEntityReactor parReactor) { @@ -92,7 +94,8 @@ public class EntityJump extends Entity { this.dz = _dz; Xmax = Zmax = maxY = Xmin = Zmin = minY = 0; - + targetWorld = worldObj; + System.out.println("[JE] Entity created"); this.reactor = parReactor; @@ -258,11 +261,9 @@ public class EntityJump extends Entity { public void prepareToJump() { boolean betweenWorlds; - - lockWorlds(); betweenWorlds = fromSpace || toSpace; - + if (toSpace) { targetWorld = DimensionManager.getWorld(WarpDrive.instance.spaceDimID); } else if (fromSpace) { @@ -271,6 +272,8 @@ public class EntityJump extends Entity { targetWorld = this.worldObj; } + lockWorlds(); + saveEntities(axisalignedbb); System.out.println("[JE] Saved " + entityOnShip.size() + " entities from ship"); From 7c1817ebca358b280868c97eb9c0d170c8ddd485 Mon Sep 17 00:00:00 2001 From: Disasm Date: Thu, 11 Jul 2013 00:37:36 +0400 Subject: [PATCH 12/16] Optimize bedrock detection --- src/cr0s/WarpDrive/EntityJump.java | 40 +++++++++--------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 3bf36a46..3463315e 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -49,6 +49,7 @@ public class EntityJump extends Entity { public int blowX, blowY, blowZ; boolean needToExplode = false; public boolean on = false; + public boolean bedrockOnShip = false; public JumpBlock ship[]; public TileEntityReactor reactor; @@ -305,13 +306,15 @@ public class EntityJump extends Entity { return; } - if (!checkForBedrockOnShip()) { - killEntity("Is bedrock on the ship. Aborting."); - messageToAllPlayersOnShip("Is bedrock on the ship. Aborting."); + bedrockOnShip = false; + int shipSize = getRealShipSize(); // sets bedrockOnShip + + if (bedrockOnShip) { + killEntity("Bedrock is on the ship. Aborting."); + messageToAllPlayersOnShip("Bedrock is on the ship. Aborting."); return; } - int shipSize = getRealShipSize(); saveShip(shipSize); setBlocksUnderPlayers(false); @@ -438,30 +441,6 @@ public class EntityJump extends Entity { return testDistance; } - /** - * Check for frobidden blocks on ship (bedrock) - * - */ - public boolean checkForBedrockOnShip() { - for (int y = minY; y <= maxY; y++) { - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { - int blockID = worldObj.getBlockId(x, y, z); - - if (blockID == 0) { - continue; - } - - if (blockID == Block.bedrock.blockID) { - return false; - } - } - } - } - - return true; - } - /* * Получить реальное количество блоков, из которых состоит корабль */ @@ -477,6 +456,11 @@ public class EntityJump extends Entity { if (blockID != 0) { shipSize++; } + + if (blockID == Block.bedrock.blockID) { + bedrockOnShip = true; + return shipSize; + } } } } From 8c8a185a028971d14376228228c1e863994c3771 Mon Sep 17 00:00:00 2001 From: Disasm Date: Thu, 11 Jul 2013 00:41:22 +0400 Subject: [PATCH 13/16] Refactoring: rename --- src/cr0s/WarpDrive/EntityJump.java | 31 ++++++++++++----------- src/cr0s/WarpDrive/TileEntityReactor.java | 16 ++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 3463315e..34b0c7b2 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -36,11 +36,11 @@ public class EntityJump extends Entity { public int shipDown; public int shipUp; public int shipLength; - public int Xmax; - public int Zmax; + public int maxX; + public int maxZ; public int maxY; - public int Xmin; - public int Zmin; + public int minX; + public int minZ; public int minY; public int dx; public int dz; @@ -93,7 +93,7 @@ public class EntityJump extends Entity { shipLeft = shipRight = shipFront = shipBack = shipDown = shipUp = shipLength = 0; this.dx = _dx; this.dz = _dz; - Xmax = Zmax = maxY = Xmin = Zmin = minY = 0; + maxX = maxZ = maxY = minX = minZ = minY = 0; targetWorld = worldObj; @@ -148,7 +148,7 @@ public class EntityJump extends Entity { this.fromSpace = (dir == -2 && (minY - distance < 0) && worldObj.provider.dimensionId == WarpDrive.instance.spaceDimID); System.out.println("[JE] Preparing to jump..."); - axisalignedbb = AxisAlignedBB.getBoundingBox(Xmin, minY, Zmin, Xmax, maxY, Zmax); + axisalignedbb = AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ); prepareToJump(); @@ -222,8 +222,8 @@ public class EntityJump extends Entity { */ public boolean checkForChunksGeneratedIn(World w) { // TODO: ходить не по координатам, а по координатам чанков, так быстрее. - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { final int newX = getNewXCoord(x, 0, z, this.distance, this.dir); final int newZ = getNewZCoord(x, 0, z, this.distance, this.dir); @@ -364,8 +364,8 @@ public class EntityJump extends Entity { int index = 0; for (int y = minY; y <= maxY; y++) { - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { if (ship == null) { killEntity("ship is null!"); return; @@ -446,10 +446,11 @@ public class EntityJump extends Entity { */ public int getRealShipSize() { int shipSize = 0; + for (int y = minY; y <= maxY; y++) { - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { int blockID = worldObj.getBlockId(x, y, z); // Пропускаем пустые блоки воздуха @@ -738,8 +739,8 @@ public class EntityJump extends Entity { } for (int y = minY; y <= maxY; y++) { - for (int x = Xmin; x <= Xmax; x++) { - for (int z = Zmin; z <= Zmax; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { int newX = getNewXCoord(x, y, z, testDistance, dir); int newY = getNewYCoord(x, y, z, testDistance, dir); int newZ = getNewZCoord(x, y, z, testDistance, dir); @@ -779,7 +780,7 @@ public class EntityJump extends Entity { * @return true, если находится */ public boolean isBlockInShip(int x, int y, int z) { - return x >= Xmin && x <= Xmax && y >= minY && y <= maxY && z >= Zmin && z <= Zmax; + return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ; } diff --git a/src/cr0s/WarpDrive/TileEntityReactor.java b/src/cr0s/WarpDrive/TileEntityReactor.java index 331709a5..7fafb0f6 100644 --- a/src/cr0s/WarpDrive/TileEntityReactor.java +++ b/src/cr0s/WarpDrive/TileEntityReactor.java @@ -470,10 +470,10 @@ public class TileEntityReactor extends TileEntity implements IEnergySink { System.out.println("[TE-WC] Moving ship to a beacon (" + beaconX + "; " + yCoord + "; " + beaconZ + ")"); EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, 1, 0, dx, dz, this); - jump.Xmax = maxX; - jump.Xmin = minX; - jump.Zmax = maxZ; - jump.Zmin = minZ; + jump.maxX = maxX; + jump.minX = minX; + jump.maxZ = maxZ; + jump.minZ = minZ; jump.maxY = maxY; jump.minY = minY; @@ -555,10 +555,10 @@ public class TileEntityReactor extends TileEntity implements IEnergySink { EntityJump jump = new EntityJump(worldObj, xCoord, yCoord, zCoord, distance, direction, dx, dz, this); - jump.Xmax = maxX; - jump.Xmin = minX; - jump.Zmax = maxZ; - jump.Zmin = minZ; + jump.maxX = maxX; + jump.minX = minX; + jump.maxZ = maxZ; + jump.minZ = minZ; jump.maxY = maxY; jump.minY = minY; From 5b66c1d84c41f1e4862395dcc1ee184d06c4834e Mon Sep 17 00:00:00 2001 From: Disasm Date: Thu, 11 Jul 2013 00:46:40 +0400 Subject: [PATCH 14/16] Optimize getRealShipSize() --- src/cr0s/WarpDrive/EntityJump.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 34b0c7b2..aff570d0 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -446,21 +446,20 @@ public class EntityJump extends Entity { */ public int getRealShipSize() { int shipSize = 0; - - for (int y = minY; y <= maxY; y++) { - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { int blockID = worldObj.getBlockId(x, y, z); // Пропускаем пустые блоки воздуха if (blockID != 0) { shipSize++; - } - if (blockID == Block.bedrock.blockID) { - bedrockOnShip = true; - return shipSize; + if (blockID == Block.bedrock.blockID) { + bedrockOnShip = true; + return shipSize; + } } } } From fc1ab2d8e7227566d9491fff51708691f06fe5b1 Mon Sep 17 00:00:00 2001 From: Disasm Date: Thu, 11 Jul 2013 00:50:20 +0400 Subject: [PATCH 15/16] Optimize saveShip() --- src/cr0s/WarpDrive/EntityJump.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index aff570d0..6b4a07ab 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -357,15 +357,14 @@ public class EntityJump extends Entity { * Saving ship to memory * * @param shipSize - * @param deleteShip */ public void saveShip(int shipSize) { ship = new JumpBlock[shipSize]; int index = 0; - for (int y = minY; y <= maxY; y++) { - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + for (int y = minY; y <= maxY; y++) { if (ship == null) { killEntity("ship is null!"); return; @@ -393,7 +392,7 @@ public class EntityJump extends Entity { System.out.println((new StringBuilder()).append("[JUMP] Ship saved: ").append((new StringBuilder()).append(ship.length).append(" blocks")).toString()); } - + /** *Ship moving */ From 1a419489d122e56c62d0528521223ad5e1d98a97 Mon Sep 17 00:00:00 2001 From: Disasm Date: Thu, 11 Jul 2013 07:51:57 +0400 Subject: [PATCH 16/16] Profiler code --- src/cr0s/WarpDrive/EntityJump.java | 12 +++++++++ src/cr0s/WarpDrive/LocalProfiler.java | 39 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/cr0s/WarpDrive/LocalProfiler.java diff --git a/src/cr0s/WarpDrive/EntityJump.java b/src/cr0s/WarpDrive/EntityJump.java index 6b4a07ab..657cbcb5 100644 --- a/src/cr0s/WarpDrive/EntityJump.java +++ b/src/cr0s/WarpDrive/EntityJump.java @@ -261,6 +261,7 @@ public class EntityJump extends Entity { } public void prepareToJump() { + LocalProfiler.start("EntityJump.prepareToJump"); boolean betweenWorlds; betweenWorlds = fromSpace || toSpace; @@ -303,6 +304,7 @@ public class EntityJump extends Entity { if (distance <= this.shipLength && !betweenWorlds && !isCoordJump) { killEntity("Not enough space for jump."); messageToAllPlayersOnShip("Not enough space for jump!"); + LocalProfiler.stop(); return; } @@ -312,6 +314,7 @@ public class EntityJump extends Entity { if (bedrockOnShip) { killEntity("Bedrock is on the ship. Aborting."); messageToAllPlayersOnShip("Bedrock is on the ship. Aborting."); + LocalProfiler.stop(); return; } @@ -321,6 +324,7 @@ public class EntityJump extends Entity { this.currentIndexInShip = 0; msCounter = System.currentTimeMillis(); + LocalProfiler.stop(); } /** @@ -359,6 +363,7 @@ public class EntityJump extends Entity { * @param shipSize */ public void saveShip(int shipSize) { + LocalProfiler.start("EntityJump.saveShip"); ship = new JumpBlock[shipSize]; int index = 0; @@ -367,6 +372,7 @@ public class EntityJump extends Entity { for (int y = minY; y <= maxY; y++) { if (ship == null) { killEntity("ship is null!"); + LocalProfiler.stop(); return; } @@ -391,12 +397,14 @@ public class EntityJump extends Entity { } System.out.println((new StringBuilder()).append("[JUMP] Ship saved: ").append((new StringBuilder()).append(ship.length).append(" blocks")).toString()); + LocalProfiler.stop(); } /** *Ship moving */ public void moveShip() { + LocalProfiler.start("EntityJump.moveShip"); int blocksToMove = Math.min(BLOCKS_PER_TICK, ship.length - currentIndexInShip); System.out.println("[JE] Moving ship part: " + currentIndexInShip + "/" + ship.length + " [btm: " + blocksToMove + "]"); @@ -405,6 +413,7 @@ public class EntityJump extends Entity { moveBlockSimple(currentIndexInShip, distance, dir, toSpace, fromSpace); currentIndexInShip++; } + LocalProfiler.stop(); } /** @@ -444,6 +453,7 @@ public class EntityJump extends Entity { * Получить реальное количество блоков, из которых состоит корабль */ public int getRealShipSize() { + LocalProfiler.start("EntityJump.getRealShipSize"); int shipSize = 0; for (int x = minX; x <= maxX; x++) { @@ -457,6 +467,7 @@ public class EntityJump extends Entity { if (blockID == Block.bedrock.blockID) { bedrockOnShip = true; + LocalProfiler.stop(); return shipSize; } } @@ -464,6 +475,7 @@ public class EntityJump extends Entity { } } + LocalProfiler.stop(); return shipSize; } diff --git a/src/cr0s/WarpDrive/LocalProfiler.java b/src/cr0s/WarpDrive/LocalProfiler.java new file mode 100644 index 00000000..ec4fe760 --- /dev/null +++ b/src/cr0s/WarpDrive/LocalProfiler.java @@ -0,0 +1,39 @@ +package cr0s.WarpDrive; + +import java.util.Stack; + +public class LocalProfiler { + private static class StackElement { + public long start; + public long internal; + public String name; + } + + private static Stack stack = new Stack(); + + public static void start(String name) { + StackElement e = new StackElement(); + e.start = System.nanoTime(); + e.internal = 0; + e.name = name; + stack.push(e); + } + + public static void stop() { + if (stack.isEmpty()) return; + + StackElement e = stack.pop(); + long end = System.nanoTime(); + long dt = end - e.start; + + if (!stack.isEmpty()) { + StackElement e2 = stack.peek(); + e2.internal += dt; + } + + long self = (dt - e.internal) / 1000; // in microseconds + long total = dt / 1000; + + System.out.println("[PROF] {" + e.name + "} self: " + (self/1000F) + "ms, total: " + (total/1000F) + "ms"); + } +}