From 9ebb4c7d12061b72487af76d274b38480be02e31 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 12 Apr 2019 15:30:34 -0400 Subject: [PATCH] Removed link byte, replaced with rotation --- core/src/io/anuke/mindustry/core/World.java | 6 +- .../io/anuke/mindustry/editor/EditorTile.java | 6 +- .../io/anuke/mindustry/editor/EditorTool.java | 4 +- core/src/io/anuke/mindustry/io/MapIO.java | 4 +- .../anuke/mindustry/io/SaveFileVersion.java | 4 +- core/src/io/anuke/mindustry/world/Tile.java | 78 +++++++++++-------- .../mindustry/world/blocks/BuildBlock.java | 3 +- .../world/blocks/distribution/Conveyor.java | 4 + .../src/test/java/power/PowerTestFixture.java | 2 +- 9 files changed, 64 insertions(+), 47 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/World.java b/core/src/io/anuke/mindustry/core/World.java index cca3314d2a..af3ac67537 100644 --- a/core/src/io/anuke/mindustry/core/World.java +++ b/core/src/io/anuke/mindustry/core/World.java @@ -305,7 +305,11 @@ public class World implements ApplicationListener{ } public void setBlock(Tile tile, Block block, Team team){ - tile.setBlock(block, team); + setBlock(tile, block, team, 0); + } + + public void setBlock(Tile tile, Block block, Team team, int rotation){ + tile.setBlock(block, team, rotation); if(block.isMultiblock()){ int offsetx = -(block.size - 1) / 2; int offsety = -(block.size - 1) / 2; diff --git a/core/src/io/anuke/mindustry/editor/EditorTile.java b/core/src/io/anuke/mindustry/editor/EditorTile.java index 60d77001e2..8a7690a8b9 100644 --- a/core/src/io/anuke/mindustry/editor/EditorTile.java +++ b/core/src/io/anuke/mindustry/editor/EditorTile.java @@ -45,7 +45,7 @@ public class EditorTile extends Tile{ @Override public void setBlock(Block type){ - Block previous = wall; + Block previous = block; if(previous == type) return; super.setBlock(type); op(TileOp.get(x, y, (byte)OpType.block.ordinal(), previous.id, type.id)); @@ -84,8 +84,8 @@ public class EditorTile extends Tile{ protected void changed(){ entity = null; - if(wall == null){ - wall = Blocks.air; + if(block == null){ + block = Blocks.air; } if(floor == null){ diff --git a/core/src/io/anuke/mindustry/editor/EditorTool.java b/core/src/io/anuke/mindustry/editor/EditorTool.java index f1b97b7d95..56af578d06 100644 --- a/core/src/io/anuke/mindustry/editor/EditorTool.java +++ b/core/src/io/anuke/mindustry/editor/EditorTool.java @@ -19,7 +19,7 @@ public enum EditorTool{ byte link = tile.getLinkByte(); - if(tile.block() instanceof BlockPart && link != 0){ + if(tile.isLinked()){ x -= (Pack.leftByte(link) - 8); y -= (Pack.rightByte(link) - 8); @@ -27,7 +27,7 @@ public enum EditorTool{ } //do not. - if(tile.block() instanceof BlockPart){ + if(tile.isLinked()){ return; } diff --git a/core/src/io/anuke/mindustry/io/MapIO.java b/core/src/io/anuke/mindustry/io/MapIO.java index 87f9c787c1..5d925a8899 100644 --- a/core/src/io/anuke/mindustry/io/MapIO.java +++ b/core/src/io/anuke/mindustry/io/MapIO.java @@ -167,7 +167,7 @@ public class MapIO{ stream.writeByte(tile.getBlockID()); if(tile.block() instanceof BlockPart){ - stream.writeByte(tile.link); + stream.writeByte(tile.getLinkByte()); }else if(tile.entity != null){ stream.writeByte(Pack.byteByte(tile.getTeamID(), tile.getRotation())); //team + rotation stream.writeShort(/*(short)tile.entity.health*/tile.block().health); //health @@ -293,7 +293,7 @@ public class MapIO{ tile.setBlock(block); if(block == Blocks.part){ - tile.link = stream.readByte(); + tile.setLinkByte(stream.readByte()); }else if(tile.entity != null){ byte tr = stream.readByte(); short health = stream.readShort(); diff --git a/core/src/io/anuke/mindustry/io/SaveFileVersion.java b/core/src/io/anuke/mindustry/io/SaveFileVersion.java index 44d7fd6329..0cbe383901 100644 --- a/core/src/io/anuke/mindustry/io/SaveFileVersion.java +++ b/core/src/io/anuke/mindustry/io/SaveFileVersion.java @@ -67,7 +67,7 @@ public abstract class SaveFileVersion{ stream.writeByte(tile.getBlockID()); if(tile.block() == Blocks.part){ - stream.writeByte(tile.link); + stream.writeByte(tile.getLinkByte()); }else if(tile.entity != null){ stream.writeByte(Pack.byteByte(tile.getTeamID(), tile.getRotation())); //team + rotation stream.writeShort((short)tile.entity.health); //health @@ -136,7 +136,7 @@ public abstract class SaveFileVersion{ tile.setBlock(block); if(block == Blocks.part){ - tile.link = stream.readByte(); + tile.setLinkByte(stream.readByte()); }else if(tile.entity != null){ byte tr = stream.readByte(); short health = stream.readShort(); diff --git a/core/src/io/anuke/mindustry/world/Tile.java b/core/src/io/anuke/mindustry/world/Tile.java index 2b902dc36b..2c91f78601 100644 --- a/core/src/io/anuke/mindustry/world/Tile.java +++ b/core/src/io/anuke/mindustry/world/Tile.java @@ -16,13 +16,7 @@ import io.anuke.mindustry.world.modules.*; import static io.anuke.mindustry.Vars.*; - public class Tile implements Position, TargetTrait{ - /** - * The coordinates of the core tile this is linked to, in the form of two bytes packed into one. - * This is relative to the block it is linked to; negate coords to find the link. - */ - public byte link = 0; /** Tile traversal cost. */ public byte cost = 1; /** Weight of [ground] units on this tile. */ @@ -30,7 +24,7 @@ public class Tile implements Position, TargetTrait{ /** Tile entity, usually null. */ public TileEntity entity; public short x, y; - protected Block wall; + protected Block block; protected Floor floor; /** Rotation, 0-3. Also used to store offload location, in which case it can be any number. */ private byte rotation; @@ -42,20 +36,20 @@ public class Tile implements Position, TargetTrait{ public Tile(int x, int y){ this.x = (short)x; this.y = (short)y; - wall = floor = (Floor)Blocks.air; + block = floor = (Floor)Blocks.air; } - public Tile(int x, int y, byte floor, byte wall){ + public Tile(int x, int y, byte floor, byte block){ this(x, y); this.floor = (Floor)content.block(floor); - this.wall = content.block(wall); + this.block = content.block(block); changed(); } - public Tile(int x, int y, byte floor, byte wall, byte rotation, byte team){ + public Tile(int x, int y, byte floor, byte block, byte rotation, byte team){ this(x, y); this.floor = (Floor)content.block(floor); - this.wall = content.block(wall); + this.block = content.block(block); this.rotation = rotation; changed(); this.team = team; @@ -67,7 +61,7 @@ public class Tile implements Position, TargetTrait{ } public byte getBlockID(){ - return wall.id; + return block.id; } public byte getFloorID(){ @@ -133,7 +127,7 @@ public class Tile implements Position, TargetTrait{ } public Block block(){ - return wall; + return block; } public Floor overlay(){ @@ -142,7 +136,7 @@ public class Tile implements Position, TargetTrait{ @SuppressWarnings("unchecked") public T cblock(){ - return (T)wall; + return (T)block; } @Override @@ -158,27 +152,35 @@ public class Tile implements Position, TargetTrait{ return team; } + public void setBlock(Block type, Team team, int rotation){ + preChanged(); + this.block = type; + this.team = (byte)team.ordinal(); + this.rotation = 0; + this.rotation = (byte)Mathf.mod(rotation, 4); + changed(); + } + public void setBlock(Block type, int rotation){ preChanged(); - if(rotation < 0) rotation = (-rotation + 2); - this.wall = type; - this.link = 0; - setRotation((byte)(rotation % 4)); + this.block = type; + this.rotation = 0; + this.rotation = (byte)Mathf.mod(rotation, 4); changed(); } public void setBlock(Block type, Team team){ preChanged(); - this.wall = type; + this.block = type; this.team = (byte)team.ordinal(); - this.link = 0; + this.rotation = 0; changed(); } public void setBlock(Block type){ preChanged(); - this.wall = type; - this.link = 0; + this.block = type; + this.rotation = 0; changed(); } @@ -241,7 +243,7 @@ public class Tile implements Position, TargetTrait{ public boolean breakable(){ Block block = block(); - if(link == 0){ + if(!isLinked()){ return (block.destructible || block.breakable || block.update); }else{ return getLinked() != this && getLinked().getLinked() == null && getLinked().breakable(); @@ -253,21 +255,21 @@ public class Tile implements Position, TargetTrait{ } public boolean isLinked(){ - return link != 0; + return block == Blocks.part; } public byte getLinkByte(){ - return link; + return rotation; } public void setLinkByte(byte b){ - this.link = b; + this.rotation = b; } /** Sets this to a linked tile, which sets the block to a part. dx and dy can only be -8-7. */ public void setLinked(byte dx, byte dy){ setBlock(Blocks.part); - link = Pack.byteByte((byte)(dx + 8), (byte)(dy + 8)); + rotation = Pack.byteByte((byte)(dx + 8), (byte)(dy + 8)); } /** @@ -315,12 +317,10 @@ public class Tile implements Position, TargetTrait{ /** Returns the block the multiblock is linked to, or null if it is not linked to any block. */ public Tile getLinked(){ - if(link == 0){ + if(!isLinked()){ return null; }else{ - byte dx = Pack.leftByte(link); - byte dy = Pack.rightByte(link); - return world.tile(x - (dx - 8), y - (dy - 8)); + return world.tile(x + linkX(rotation), y + linkY(rotation)); } } @@ -449,7 +449,7 @@ public class Tile implements Position, TargetTrait{ @Override public boolean isDead(){ - return false; //tiles never die + return entity == null; } @Override @@ -483,6 +483,16 @@ public class Tile implements Position, TargetTrait{ Block floor = floor(); return floor.name + ":" + block.name + ":" + content.block(overlay) + "[" + x + "," + y + "] " + "entity=" + (entity == null ? "null" : (entity.getClass())) + - (link != 0 ? " link=[" + (Pack.leftByte(link) - 8) + ", " + (Pack.rightByte(link) - 8) + "]" : ""); + (isLinked() ? " link=[" + linkX(rotation) + ", " + linkY(rotation) + "]" : ""); + } + + /**Returns the relative X from a link byte.*/ + public static int linkX(byte value){ + return -((byte)((value >> 4) & (byte)0x0F) - 8); + } + + /**Returns the relative Y from a link byte.*/ + public static int linkY(byte value){ + return -((byte)(value & 0x0F) - 8); } } \ No newline at end of file diff --git a/core/src/io/anuke/mindustry/world/blocks/BuildBlock.java b/core/src/io/anuke/mindustry/world/blocks/BuildBlock.java index 076bb4d301..e74230f79a 100644 --- a/core/src/io/anuke/mindustry/world/blocks/BuildBlock.java +++ b/core/src/io/anuke/mindustry/world/blocks/BuildBlock.java @@ -51,8 +51,7 @@ public class BuildBlock extends Block{ public static void onConstructFinish(Tile tile, Block block, int builderID, byte rotation, Team team){ if(tile == null) return; float healthf = tile.entity == null ? 1f : tile.entity.healthf(); - tile.setRotation(rotation); - world.setBlock(tile, block, team); + world.setBlock(tile, block, team, rotation); if(tile.entity != null){ tile.entity.health = block.health * healthf; } diff --git a/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java b/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java index fe2f550a6c..1e602a1ff5 100644 --- a/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java +++ b/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java @@ -167,6 +167,10 @@ public class Conveyor extends Block{ public void unitOn(Tile tile, Unit unit){ ConveyorEntity entity = tile.entity(); + if(entity.clogHeat > 0.5f){ + return; + } + entity.noSleep(); float speed = this.speed * tilesize / 2.4f; diff --git a/tests/src/test/java/power/PowerTestFixture.java b/tests/src/test/java/power/PowerTestFixture.java index d9e36dd318..fd3bff5eca 100644 --- a/tests/src/test/java/power/PowerTestFixture.java +++ b/tests/src/test/java/power/PowerTestFixture.java @@ -81,7 +81,7 @@ public class PowerTestFixture{ // Since this part shall not be part of the test and would require more work anyway, we manually set the block and floor // through reflections and then simulate part of what the changed() method does. - Field field = Tile.class.getDeclaredField("wall"); + Field field = Tile.class.getDeclaredField("block"); field.setAccessible(true); field.set(tile, block);