From 087f8129b93d6d57471b2a1f497044ad54cc0cea Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 3 Mar 2020 19:29:41 -0500 Subject: [PATCH] Tile code cleanup --- core/src/mindustry/ai/BlockIndexer.java | 51 ++++++++----------- core/src/mindustry/ai/Pathfinder.java | 12 ++--- core/src/mindustry/ai/WaveSpawner.java | 9 ++-- .../src/mindustry/editor/MapEditorDialog.java | 9 ++-- .../src/mindustry/graphics/BlockRenderer.java | 23 +++------ core/src/mindustry/graphics/MenuRenderer.java | 35 ++++--------- .../mindustry/graphics/MinimapRenderer.java | 6 +-- core/src/mindustry/world/Tile.java | 16 ++++++ gradle.properties | 2 +- tests/src/test/java/ZoneTests.java | 15 +++--- 10 files changed, 75 insertions(+), 103 deletions(-) diff --git a/core/src/mindustry/ai/BlockIndexer.java b/core/src/mindustry/ai/BlockIndexer.java index 5d8380fa21..a6338d02f5 100644 --- a/core/src/mindustry/ai/BlockIndexer.java +++ b/core/src/mindustry/ai/BlockIndexer.java @@ -78,18 +78,14 @@ public class BlockIndexer{ //create bitset for each team type that contains each quadrant structQuadrants = new GridBits[Team.all().length]; - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.tile(x, y); + for(Tile tile : world.tiles){ + process(tile); - process(tile); - - if(tile.entity != null && tile.entity.damaged()){ - notifyTileDamaged(tile.entity); - } - - if(tile.drop() != null) allOres.add(tile.drop()); + if(tile.entity != null && tile.entity.damaged()){ + notifyTileDamaged(tile.entity); } + + if(tile.drop() != null) allOres.add(tile.drop()); } for(int x = 0; x < quadWidth(); x++){ @@ -119,14 +115,11 @@ public class BlockIndexer{ if(structQuadrants == null) return; //go through every tile... ouch - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.tile(x, y); - if(tile.team() == team){ - int quadrantX = tile.x / quadrantSize; - int quadrantY = tile.y / quadrantSize; - structQuadrant(team).set(quadrantX, quadrantY); - } + for(Tile tile : world.tiles){ + if(tile.team() == team){ + int quadrantX = tile.x / quadrantSize; + int quadrantY = tile.y / quadrantSize; + structQuadrant(team).set(quadrantX, quadrantY); } } } @@ -403,20 +396,16 @@ public class BlockIndexer{ ores.put(item, new ObjectSet<>()); } - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - int qx = (x / quadrantSize); - int qy = (y / quadrantSize); + for(Tile tile : world.tiles){ + int qx = (tile.x / quadrantSize); + int qy = (tile.y / quadrantSize); - Tile tile = world.tile(x, y); - - //add position of quadrant to list when an ore is found - if(tile.drop() != null && scanOres.contains(tile.drop()) && tile.block() == Blocks.air){ - ores.get(tile.drop()).add(world.tile( - //make sure to clamp quadrant middle position, since it might go off bounds - Mathf.clamp(qx * quadrantSize + quadrantSize / 2, 0, world.width() - 1), - Mathf.clamp(qy * quadrantSize + quadrantSize / 2, 0, world.height() - 1))); - } + //add position of quadrant to list when an ore is found + if(tile.drop() != null && scanOres.contains(tile.drop()) && tile.block() == Blocks.air){ + ores.get(tile.drop()).add(world.tile( + //make sure to clamp quadrant middle position, since it might go off bounds + Mathf.clamp(qx * quadrantSize + quadrantSize / 2, 0, world.width() - 1), + Mathf.clamp(qy * quadrantSize + quadrantSize / 2, 0, world.height() - 1))); } } } diff --git a/core/src/mindustry/ai/Pathfinder.java b/core/src/mindustry/ai/Pathfinder.java index b40e3543da..33f4797467 100644 --- a/core/src/mindustry/ai/Pathfinder.java +++ b/core/src/mindustry/ai/Pathfinder.java @@ -1,13 +1,13 @@ package mindustry.ai; import arc.*; -import mindustry.annotations.Annotations.*; -import arc.struct.*; import arc.func.*; import arc.math.geom.*; -import arc.util.*; +import arc.struct.*; import arc.util.ArcAnnotate.*; +import arc.util.*; import arc.util.async.*; +import mindustry.annotations.Annotations.*; import mindustry.game.EventType.*; import mindustry.game.*; import mindustry.gen.*; @@ -45,10 +45,8 @@ public class Pathfinder implements Runnable{ created = new GridBits(Team.all().length, PathTarget.all.length); list = new Array<>(); - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - tiles[x][y] = packTile(world.rawTile(x, y)); - } + for(Tile tile : world.tiles){ + tiles[tile.x][tile.y] = packTile(tile); } //special preset which may help speed things up; this is optional diff --git a/core/src/mindustry/ai/WaveSpawner.java b/core/src/mindustry/ai/WaveSpawner.java index c4694dd79b..632969f75e 100644 --- a/core/src/mindustry/ai/WaveSpawner.java +++ b/core/src/mindustry/ai/WaveSpawner.java @@ -118,12 +118,9 @@ public class WaveSpawner{ flySpawns.clear(); groundSpawns.clear(); - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - - if(world.tile(x, y).overlay() == Blocks.spawn){ - addSpawns(x, y); - } + for(Tile tile : world.tiles){ + if(tile.overlay() == Blocks.spawn){ + addSpawns(tile.x, tile.y); } } } diff --git a/core/src/mindustry/editor/MapEditorDialog.java b/core/src/mindustry/editor/MapEditorDialog.java index 0cc46715fd..e8634f2194 100644 --- a/core/src/mindustry/editor/MapEditorDialog.java +++ b/core/src/mindustry/editor/MapEditorDialog.java @@ -254,12 +254,9 @@ public class MapEditorDialog extends Dialog implements Disposable{ ))); world.endMapLoad(); //add entities so they update. is this really needed? - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.rawTile(x, y); - if(tile.entity != null){ - tile.entity.add(); - } + for(Tile tile : world.tiles){ + if(tile.entity != null){ + tile.entity.add(); } } player.set(world.width() * tilesize/2f, world.height() * tilesize/2f); diff --git a/core/src/mindustry/graphics/BlockRenderer.java b/core/src/mindustry/graphics/BlockRenderer.java index 66c1a22490..b4ea7e5bef 100644 --- a/core/src/mindustry/graphics/BlockRenderer.java +++ b/core/src/mindustry/graphics/BlockRenderer.java @@ -53,12 +53,9 @@ public class BlockRenderer implements Disposable{ Draw.color(shadowColor); - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.rawTile(x, y); - if(tile.block().hasShadow){ - Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1); - } + for(Tile tile : world.tiles){ + if(tile.block().hasShadow){ + Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1); } } @@ -72,16 +69,12 @@ public class BlockRenderer implements Disposable{ Core.graphics.clear(Color.white); Draw.proj().setOrtho(0, 0, fog.getWidth(), fog.getHeight()); - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.rawTile(x, y); + for(Tile tile : world.tiles){ + float darkness = world.getDarkness(tile.x, tile.y); - float darkness = world.getDarkness(x, y); - - if(darkness > 0){ - Draw.color(0f, 0f, 0f, Math.min((darkness + 0.5f) / 4f, 1f)); - Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1); - } + if(darkness > 0){ + Draw.color(0f, 0f, 0f, Math.min((darkness + 0.5f) / 4f, 1f)); + Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1); } } diff --git a/core/src/mindustry/graphics/MenuRenderer.java b/core/src/mindustry/graphics/MenuRenderer.java index ec11615b97..cdcaf15673 100644 --- a/core/src/mindustry/graphics/MenuRenderer.java +++ b/core/src/mindustry/graphics/MenuRenderer.java @@ -170,13 +170,13 @@ public class MenuRenderer implements Disposable{ Draw.proj().setOrtho(0, 0, shadows.getWidth(), shadows.getHeight()); shadows.beginDraw(Color.clear); Draw.color(Color.black); - for(int x = 0; x < width; x++){ - for(int y = 0; y < height; y++){ - if(world.rawTile(x, y).block() != Blocks.air){ - Fill.rect(x + 0.5f, y + 0.5f, 1, 1); - } + + for(Tile tile : world.tiles){ + if(tile.block() != Blocks.air){ + Fill.rect(tile.x + 0.5f, tile.y + 0.5f, 1, 1); } } + Draw.color(); shadows.endDraw(); @@ -185,32 +185,19 @@ public class MenuRenderer implements Disposable{ Core.batch = batch = new CacheBatch(new SpriteCache(width * height * 6, false)); batch.beginCache(); - for(int x = 0; x < width; x++){ - for(int y = 0; y < height; y++){ - Tile tile = world.rawTile(x, y); - tile.floor().draw(tile); - } + for(Tile tile : world.tiles){ + tile.floor().draw(tile); } - for(int x = 0; x < width; x++){ - for(int y = 0; y < height; y++){ - Tile tile = world.rawTile(x, y); - if(tile.overlay() != Blocks.air){ - tile.overlay().draw(tile); - } - } + for(Tile tile : world.tiles){ + tile.overlay().draw(tile); } cacheFloor = batch.endCache(); batch.beginCache(); - for(int x = 0; x < width; x++){ - for(int y = 0; y < height; y++){ - Tile tile = world.rawTile(x, y); - if(tile.block() != Blocks.air){ - tile.block().draw(tile); - } - } + for(Tile tile : world.tiles){ + tile.block().draw(tile); } cacheWall = batch.endCache(); diff --git a/core/src/mindustry/graphics/MinimapRenderer.java b/core/src/mindustry/graphics/MinimapRenderer.java index 0b36119704..28f7de4312 100644 --- a/core/src/mindustry/graphics/MinimapRenderer.java +++ b/core/src/mindustry/graphics/MinimapRenderer.java @@ -128,10 +128,8 @@ public class MinimapRenderer implements Disposable{ } public void updateAll(){ - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - pixmap.draw(x, pixmap.getHeight() - 1 - y, colorFor(world.tile(x, y))); - } + for(Tile tile : world.tiles){ + pixmap.draw(tile.x, pixmap.getHeight() - 1 - tile.y, colorFor(tile)); } texture.draw(pixmap, 0, 0); } diff --git a/core/src/mindustry/world/Tile.java b/core/src/mindustry/world/Tile.java index b56d170c8b..0fe9e31bc5 100644 --- a/core/src/mindustry/world/Tile.java +++ b/core/src/mindustry/world/Tile.java @@ -243,6 +243,16 @@ public class Tile implements Position{ Call.setTile(this, block, team, rotation); } + /** set()-s this tile, except it's synced across the network */ + public void setFloorNet(Block floor, Block overlay){ + Call.setFloor(this, floor, overlay); + } + + /** set()-s this tile, except it's synced across the network */ + public void setFloorNet(Block floor){ + setFloorNet(floor, Blocks.air); + } + public byte rotation(){ return rotation; } @@ -526,6 +536,12 @@ public class Tile implements Position{ //remote utility methods + @Remote(called = Loc.server) + public static void setFloor(Tile tile, Block floor, Block overlay){ + tile.setFloor(floor.asFloor()); + tile.setOverlay(overlay); + } + @Remote(called = Loc.server) public static void removeTile(Tile tile){ tile.remove(); diff --git a/gradle.properties b/gradle.properties index bcde86fca5..9a6d877821 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m -archash=80007985ae0f1cb24531272e910844d0b4b5bb18 +archash=69ef047313449905aff6d1390d7136f9c7cdc986 diff --git a/tests/src/test/java/ZoneTests.java b/tests/src/test/java/ZoneTests.java index 0a3ce770af..267715eb3a 100644 --- a/tests/src/test/java/ZoneTests.java +++ b/tests/src/test/java/ZoneTests.java @@ -47,15 +47,12 @@ public class ZoneTests{ ObjectSet resources = new ObjectSet<>(); boolean hasSpawnPoint = false; - for(int x = 0; x < world.width(); x++){ - for(int y = 0; y < world.height(); y++){ - Tile tile = world.tile(x, y); - if(tile.drop() != null){ - resources.add(tile.drop()); - } - if(tile.block() instanceof CoreBlock && tile.team() == state.rules.defaultTeam){ - hasSpawnPoint = true; - } + for(Tile tile : world.tiles){ + if(tile.drop() != null){ + resources.add(tile.drop()); + } + if(tile.block() instanceof CoreBlock && tile.team() == state.rules.defaultTeam){ + hasSpawnPoint = true; } }