From 1c04653ffdc30beca2122feca7480a48e1979266 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 30 Sep 2020 09:29:57 -0400 Subject: [PATCH 1/4] Autotiler cleanup and documentation --- .../src/mindustry/world/blocks/Autotiler.java | 110 ++++++++++++++---- .../world/blocks/distribution/Conveyor.java | 2 +- .../world/blocks/liquid/Conduit.java | 6 +- 3 files changed, 92 insertions(+), 26 deletions(-) diff --git a/core/src/mindustry/world/blocks/Autotiler.java b/core/src/mindustry/world/blocks/Autotiler.java index b9cff3840e..fd90c7fdce 100644 --- a/core/src/mindustry/world/blocks/Autotiler.java +++ b/core/src/mindustry/world/blocks/Autotiler.java @@ -14,20 +14,40 @@ import java.util.*; //TODO documentation public interface Autotiler{ - //holds some static temporary variables, required due to some RoboVM bugs + /** + * Holds some static temporary variables, required due to some RoboVM bugs + */ class AutotilerHolder{ static final int[] blendresult = new int[5]; static final BuildPlan[] directionals = new BuildPlan[4]; } - /** slices a texture region: - * mode == 0 -> no slice - * mode == 1 -> bottom - * mode == 2 -> top */ - default TextureRegion sliced(TextureRegion input, int mode){ - return mode == 0 ? input : mode == 1 ? botHalf(input) : topHalf(input); + /** + * The mode to slice a texture at. + */ + enum SliceMode{ + NO_SLICE, + BOTTOM, + TOP } + /** + * Slices a texture region depending on the SliceMode paramater + * + * @param input The TextureRegion to be sliced + * @param mode The SliceMode to be applied + * @return The sliced texture + */ + default TextureRegion sliced(TextureRegion input, SliceMode mode){ + return mode == SliceMode.NO_SLICE ? input : mode == SliceMode.BOTTOM ? botHalf(input) : topHalf(input); + } + + /** + * Get the top half of a texture + * + * @param input The TextureRegion to slice + * @return The top half of the texture + */ default TextureRegion topHalf(TextureRegion input){ TextureRegion region = Tmp.tr1; region.set(input); @@ -35,6 +55,12 @@ public interface Autotiler{ return region; } + /** + * Get the buttom half of a texture + * + * @param input The TextureRegion to slice + * @return The buttom half of the texture + */ default TextureRegion botHalf(TextureRegion input){ TextureRegion region = Tmp.tr1; region.set(input); @@ -82,6 +108,8 @@ public interface Autotiler{ int[] blendresult = AutotilerHolder.blendresult; blendresult[0] = 0; blendresult[1] = blendresult[2] = 1; + + // TODO code refactoring maybe? int num = (blends(tile, rotation, directional, 2, world) && blends(tile, rotation, directional, 1, world) && blends(tile, rotation, directional, 3, world)) ? 0 : (blends(tile, rotation, directional, 1, world) && blends(tile, rotation, directional, 3, world)) ? 1 : @@ -92,6 +120,8 @@ public interface Autotiler{ -1; transformCase(num, blendresult); + // Calculate bitmask for direction. + blendresult[3] = 0; for(int i = 0; i < 4; i++){ @@ -100,6 +130,8 @@ public interface Autotiler{ } } + // Calculate direction for non-square sprites. + blendresult[4] = 0; for(int i = 0; i < 4; i++){ @@ -112,24 +144,47 @@ public interface Autotiler{ return blendresult; } + /** + * Transforms the autotiler setting the connection and the y-scale + * + * @param num The number to use to transform the array + * @param bits The blending value array + */ default void transformCase(int num, int[] bits){ - if(num == 0){ - bits[0] = 3; - }else if(num == 1){ - bits[0] = 4; - }else if(num == 2){ - bits[0] = 2; - }else if(num == 3){ - bits[0] = 2; - bits[2] = -1; - }else if(num == 4){ - bits[0] = 1; - bits[2] = -1; - }else if(num == 5){ - bits[0] = 1; + switch (num){ + case 0: + bits[0] = 3; + break; + case 1: + bits[0] = 4; + case 2: + bits[0] = 2; + break; + case 3: + bits[0] = 2; + bits[2] = -1; + break; + case 4: + bits[0] = 1; + bits[2] = -1; + break; + case 5: + bits[0] = 1; } } + /** + * Check if a position is facing the secondary position at a rotation + * + * @param x The x coordinate of position 1 + * @param y The y coordinate of position 1 + * @param rotation The rotation of the tile on (x, y) + * + * @param x2 The x coordinate of position 2 + * @param y2 The y coordinate of position 2 + * + * @return If position 1 is facing position 2 at a certain angle + */ default boolean facing(int x, int y, int rotation, int x2, int y2){ return Point2.equals(x + Geometry.d4(rotation).x,y + Geometry.d4(rotation).y, x2, y2); } @@ -145,6 +200,8 @@ public interface Autotiler{ return checkWorld && blends(tile, rotation, direction); } + + // TODO docs -- use for direction? default boolean blends(Tile tile, int rotation, int direction){ Building other = tile.getNearbyEntity(Mathf.mod(rotation - direction, 4)); return other != null && other.team == tile.team() && blends(tile, rotation, other.tileX(), other.tileY(), other.rotation, other.block); @@ -168,7 +225,16 @@ public interface Autotiler{ || (!otherblock.rotatedOutput(otherx, othery) || Point2.equals(otherx + Geometry.d4(otherrot).x, othery + Geometry.d4(otherrot).y, tile.x, tile.y))); } - /** @return whether this tile is looking at the other tile. */ + /** + * Check if a position is facing the secondary position at a rotation + * + * @param tile The origin tile that is or is not facing the destinated `otherblock` + * @param rotation The rotation of the tile on (x, y) + * + * @param otherx The x coordinate of position 2 + * @param othery The y coordinate of position 2 + * @return whether this tile is looking at the other tile. + */ default boolean lookingAt(Tile tile, int rotation, int otherx, int othery, Block otherblock){ Tile facing = Edges.getFacingEdge(otherblock, otherx, othery, tile); return facing != null && diff --git a/core/src/mindustry/world/blocks/distribution/Conveyor.java b/core/src/mindustry/world/blocks/distribution/Conveyor.java index 19d769f85f..13ddc86414 100644 --- a/core/src/mindustry/world/blocks/distribution/Conveyor.java +++ b/core/src/mindustry/world/blocks/distribution/Conveyor.java @@ -124,7 +124,7 @@ public class Conveyor extends Block implements Autotiler{ int dir = rotation - i; float rot = i == 0 ? rotation * 90 : (dir)*90; - Draw.rect(sliced(regions[0][frame], i != 0 ? 1 : 2), x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, rot); + Draw.rect(sliced(regions[0][frame], i != 0 ? SliceMode.BOTTOM : SliceMode.TOP), x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, rot); } } diff --git a/core/src/mindustry/world/blocks/liquid/Conduit.java b/core/src/mindustry/world/blocks/liquid/Conduit.java index 0db85e80c0..fdb3a277a1 100644 --- a/core/src/mindustry/world/blocks/liquid/Conduit.java +++ b/core/src/mindustry/world/blocks/liquid/Conduit.java @@ -88,18 +88,18 @@ public class Conduit extends LiquidBlock implements Autotiler{ if((blending & (1 << i)) != 0){ int dir = r - i; float rot = i == 0 ? rotation : (dir)*90; - drawAt(x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, 0, rot, i != 0 ? 1 : 2); + drawAt(x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, 0, rot, i != 0 ? SliceMode.BOTTOM : SliceMode.TOP); } } Draw.z(Layer.block); Draw.scl(xscl, yscl); - drawAt(x, y, blendbits, rotation, 0); + drawAt(x, y, blendbits, rotation, SliceMode.NO_SLICE); Draw.reset(); } - protected void drawAt(float x, float y, int bits, float rotation, int slice){ + protected void drawAt(float x, float y, int bits, float rotation, SliceMode slice){ Draw.color(botColor); Draw.rect(sliced(botRegions[bits], slice), x, y, rotation); From fbb317aa112f261f19c964e20d218253c2dc30d0 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 30 Sep 2020 09:30:12 -0400 Subject: [PATCH 2/4] Update contributors --- core/assets/contributors | 1 + 1 file changed, 1 insertion(+) diff --git a/core/assets/contributors b/core/assets/contributors index 7cff98715f..69de5a3d8e 100644 --- a/core/assets/contributors +++ b/core/assets/contributors @@ -94,3 +94,4 @@ The Slaylord ThePlayerA YellOw139 PetrGasparik +LeoDog896 From 2b04ebb14d8f4de81dc35f0f72a7a80fb1022313 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 30 Sep 2020 09:46:33 -0400 Subject: [PATCH 3/4] Enum follows camelCase rules --- core/src/mindustry/world/blocks/Autotiler.java | 8 ++++---- .../src/mindustry/world/blocks/distribution/Conveyor.java | 2 +- core/src/mindustry/world/blocks/liquid/Conduit.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/mindustry/world/blocks/Autotiler.java b/core/src/mindustry/world/blocks/Autotiler.java index fd90c7fdce..f717638b3d 100644 --- a/core/src/mindustry/world/blocks/Autotiler.java +++ b/core/src/mindustry/world/blocks/Autotiler.java @@ -26,9 +26,9 @@ public interface Autotiler{ * The mode to slice a texture at. */ enum SliceMode{ - NO_SLICE, - BOTTOM, - TOP + noSlice, + bottom, + top } /** @@ -39,7 +39,7 @@ public interface Autotiler{ * @return The sliced texture */ default TextureRegion sliced(TextureRegion input, SliceMode mode){ - return mode == SliceMode.NO_SLICE ? input : mode == SliceMode.BOTTOM ? botHalf(input) : topHalf(input); + return mode == SliceMode.noSlice ? input : mode == SliceMode.bottom ? botHalf(input) : topHalf(input); } /** diff --git a/core/src/mindustry/world/blocks/distribution/Conveyor.java b/core/src/mindustry/world/blocks/distribution/Conveyor.java index 13ddc86414..351ab3b773 100644 --- a/core/src/mindustry/world/blocks/distribution/Conveyor.java +++ b/core/src/mindustry/world/blocks/distribution/Conveyor.java @@ -124,7 +124,7 @@ public class Conveyor extends Block implements Autotiler{ int dir = rotation - i; float rot = i == 0 ? rotation * 90 : (dir)*90; - Draw.rect(sliced(regions[0][frame], i != 0 ? SliceMode.BOTTOM : SliceMode.TOP), x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, rot); + Draw.rect(sliced(regions[0][frame], i != 0 ? SliceMode.bottom : SliceMode.top), x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, rot); } } diff --git a/core/src/mindustry/world/blocks/liquid/Conduit.java b/core/src/mindustry/world/blocks/liquid/Conduit.java index fdb3a277a1..1d9c5aa4bf 100644 --- a/core/src/mindustry/world/blocks/liquid/Conduit.java +++ b/core/src/mindustry/world/blocks/liquid/Conduit.java @@ -88,14 +88,14 @@ public class Conduit extends LiquidBlock implements Autotiler{ if((blending & (1 << i)) != 0){ int dir = r - i; float rot = i == 0 ? rotation : (dir)*90; - drawAt(x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, 0, rot, i != 0 ? SliceMode.BOTTOM : SliceMode.TOP); + drawAt(x + Geometry.d4x(dir) * tilesize*0.75f, y + Geometry.d4y(dir) * tilesize*0.75f, 0, rot, i != 0 ? SliceMode.bottom : SliceMode.top); } } Draw.z(Layer.block); Draw.scl(xscl, yscl); - drawAt(x, y, blendbits, rotation, SliceMode.NO_SLICE); + drawAt(x, y, blendbits, rotation, SliceMode.noSlice); Draw.reset(); } From 6db2376835b5b6d6e1d36c7df71469a4b5ad2548 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Wed, 30 Sep 2020 09:47:03 -0400 Subject: [PATCH 4/4] Revert to if-statement --- .../src/mindustry/world/blocks/Autotiler.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/core/src/mindustry/world/blocks/Autotiler.java b/core/src/mindustry/world/blocks/Autotiler.java index f717638b3d..35b58706e1 100644 --- a/core/src/mindustry/world/blocks/Autotiler.java +++ b/core/src/mindustry/world/blocks/Autotiler.java @@ -151,25 +151,20 @@ public interface Autotiler{ * @param bits The blending value array */ default void transformCase(int num, int[] bits){ - switch (num){ - case 0: - bits[0] = 3; - break; - case 1: - bits[0] = 4; - case 2: - bits[0] = 2; - break; - case 3: - bits[0] = 2; - bits[2] = -1; - break; - case 4: - bits[0] = 1; - bits[2] = -1; - break; - case 5: - bits[0] = 1; + if(num == 0){ + bits[0] = 3; + }else if(num == 1){ + bits[0] = 4; + }else if(num == 2){ + bits[0] = 2; + }else if(num == 3){ + bits[0] = 2; + bits[2] = -1; + }else if(num == 4){ + bits[0] = 1; + bits[2] = -1; + }else if(num == 5){ + bits[0] = 1; } }