From dd45b43d7f54ac04945e681b173555a8b7c70e9f Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 16 Jul 2018 18:42:29 -0400 Subject: [PATCH] Classes moved / Fixed autotile bugs / Added sector display --- core/assets/bundles/bundle.properties | 1 + core/src/Mindustry.gwt.xml | 2 +- core/src/io/anuke/mindustry/Vars.java | 3 +- core/src/io/anuke/mindustry/core/UI.java | 2 + core/src/io/anuke/mindustry/core/World.java | 6 +- core/src/io/anuke/mindustry/maps/Sectors.java | 23 ++- .../generation}/GenProperties.java | 2 +- .../generation}/WorldGenerator.java | 160 ++++++++++-------- .../mindustry/ui/dialogs/SectorsDialog.java | 95 ++++++++++- .../mindustry/ui/fragments/MenuFragment.java | 2 +- .../io/anuke/mindustry/world/BaseBlock.java | 2 + .../world/blocks/distribution/Conveyor.java | 2 +- .../world/blocks/distribution/Junction.java | 1 + .../world/blocks/distribution/Sorter.java | 1 + 14 files changed, 221 insertions(+), 81 deletions(-) rename core/src/io/anuke/mindustry/{world/mapgen => maps/generation}/GenProperties.java (97%) rename core/src/io/anuke/mindustry/{world/mapgen => maps/generation}/WorldGenerator.java (69%) diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index f016813f44..b6b9a4db15 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -49,6 +49,7 @@ text.joingame=Join Game text.addplayers=Add/Remove Players text.customgame=Custom Game text.campaign=Campaign +text.sectors=Sectors text.quit=Quit text.maps=Maps text.maps.none=[LIGHT_GRAY]No maps found! diff --git a/core/src/Mindustry.gwt.xml b/core/src/Mindustry.gwt.xml index 6cee396a19..5e6f994b46 100644 --- a/core/src/Mindustry.gwt.xml +++ b/core/src/Mindustry.gwt.xml @@ -17,6 +17,6 @@ - + \ No newline at end of file diff --git a/core/src/io/anuke/mindustry/Vars.java b/core/src/io/anuke/mindustry/Vars.java index fd8d756027..247d617982 100644 --- a/core/src/io/anuke/mindustry/Vars.java +++ b/core/src/io/anuke/mindustry/Vars.java @@ -45,9 +45,10 @@ public class Vars{ public static final int saveSlots = 64; public static final float itemSize = 5f; public static final int tilesize = 8; + public static final int sectorSize = 256; public static final Locale[] locales = {new Locale("en"), new Locale("fr"), new Locale("ru"), new Locale("uk", "UA"), new Locale("pl"), new Locale("de"), new Locale("pt", "BR"), new Locale("ko"), new Locale("in", "ID"), - new Locale("ita"), new Locale("es"), new Locale("zh","TW")}; + new Locale("ita"), new Locale("es"), new Locale("zh","TW")}; public static final Color[] playerColors = { Color.valueOf("82759a"), Color.valueOf("c0c1c5"), diff --git a/core/src/io/anuke/mindustry/core/UI.java b/core/src/io/anuke/mindustry/core/UI.java index 12222b9688..259340491a 100644 --- a/core/src/io/anuke/mindustry/core/UI.java +++ b/core/src/io/anuke/mindustry/core/UI.java @@ -61,6 +61,7 @@ public class UI extends SceneModule{ public LocalPlayerDialog localplayers; public UnlocksDialog unlocks; public ContentInfoDialog content; + public SectorsDialog sectors; private Locale lastLocale; @@ -167,6 +168,7 @@ public class UI extends SceneModule{ localplayers = new LocalPlayerDialog(); unlocks = new UnlocksDialog(); content = new ContentInfoDialog(); + sectors = new SectorsDialog(); Group group = Core.scene.getRoot(); diff --git a/core/src/io/anuke/mindustry/core/World.java b/core/src/io/anuke/mindustry/core/World.java index f0f85207a3..50f37c93df 100644 --- a/core/src/io/anuke/mindustry/core/World.java +++ b/core/src/io/anuke/mindustry/core/World.java @@ -17,7 +17,7 @@ import io.anuke.mindustry.maps.Maps; import io.anuke.mindustry.maps.Sectors; import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Tile; -import io.anuke.mindustry.world.mapgen.WorldGenerator; +import io.anuke.mindustry.maps.generation.WorldGenerator; import io.anuke.ucore.core.Events; import io.anuke.ucore.core.Timers; import io.anuke.ucore.entities.EntityPhysics; @@ -51,6 +51,10 @@ public class World extends Module{ maps.dispose(); } + public WorldGenerator generator(){ + return generator; + } + public Sectors sectors(){ return sectors; } diff --git a/core/src/io/anuke/mindustry/maps/Sectors.java b/core/src/io/anuke/mindustry/maps/Sectors.java index 03c85c7a62..39500db4d8 100644 --- a/core/src/io/anuke/mindustry/maps/Sectors.java +++ b/core/src/io/anuke/mindustry/maps/Sectors.java @@ -5,15 +5,18 @@ import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.GridPoint2; import com.badlogic.gdx.utils.Array; +import io.anuke.mindustry.content.blocks.Blocks; +import io.anuke.mindustry.maps.generation.WorldGenerator.GenResult; +import io.anuke.mindustry.world.ColorMapper; import io.anuke.ucore.core.Settings; import io.anuke.ucore.util.Geometry; import io.anuke.ucore.util.GridMap; -import static io.anuke.mindustry.Vars.headless; +import static io.anuke.mindustry.Vars.*; public class Sectors{ - private static final int sectorSize = 256; - private static final int sectorImageSize = 8; + private static final int sectorImageSize = 16; + private GridMap grid = new GridMap<>(); public Sectors(){ @@ -32,8 +35,9 @@ public class Sectors{ sector.unlocked = true; if(sector.texture == null) createTexture(sector); + //TODO fix for(GridPoint2 point : Geometry.d4){ - createSector(sector.x + point.x, sector.y + point.y); + // createSector(sector.x + point.x, sector.y + point.y); } } @@ -45,6 +49,7 @@ public class Sectors{ sector.x = (short)x; sector.y = (short)y; sector.unlocked = false; + grid.put(x, y, sector); } public void load(){ @@ -74,9 +79,15 @@ public class Sectors{ int worldX = sector.x * sectorSize; int worldY = sector.y * sectorSize; - for(int x = worldX; x < (worldX + sectorSize); x++){ - for(int y = worldY; y < (worldY + sectorSize); y++){ + for(int x = 0; x < sectorImageSize; x++){ + for(int y = 0; y < sectorImageSize; y++){ + int toX = x * sectorSize / sectorImageSize; + int toY = y * sectorSize / sectorImageSize; + GenResult result = world.generator().generateTile(sector.x, sector.y, toX, toY); + + int color = ColorMapper.getBlockColor(result.wall == Blocks.air ? result.floor : result.wall); + pixmap.drawPixel(x, sectorImageSize - 1 - y, color); } } diff --git a/core/src/io/anuke/mindustry/world/mapgen/GenProperties.java b/core/src/io/anuke/mindustry/maps/generation/GenProperties.java similarity index 97% rename from core/src/io/anuke/mindustry/world/mapgen/GenProperties.java rename to core/src/io/anuke/mindustry/maps/generation/GenProperties.java index b11aca6b8c..9c94ec3e94 100644 --- a/core/src/io/anuke/mindustry/world/mapgen/GenProperties.java +++ b/core/src/io/anuke/mindustry/maps/generation/GenProperties.java @@ -1,4 +1,4 @@ -package io.anuke.mindustry.world.mapgen; +package io.anuke.mindustry.maps.generation; public class GenProperties{ public long seed; diff --git a/core/src/io/anuke/mindustry/world/mapgen/WorldGenerator.java b/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java similarity index 69% rename from core/src/io/anuke/mindustry/world/mapgen/WorldGenerator.java rename to core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java index a5e507e505..b2d3fe8e0f 100644 --- a/core/src/io/anuke/mindustry/world/mapgen/WorldGenerator.java +++ b/core/src/io/anuke/mindustry/maps/generation/WorldGenerator.java @@ -1,7 +1,6 @@ -package io.anuke.mindustry.world.mapgen; +package io.anuke.mindustry.maps.generation; import com.badlogic.gdx.math.GridPoint2; -import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.IntArray; @@ -19,17 +18,38 @@ import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.blocks.Floor; import io.anuke.ucore.noise.RidgedPerlin; import io.anuke.ucore.noise.Simplex; +import io.anuke.ucore.util.Bits; import io.anuke.ucore.util.Geometry; import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.SeedRandom; +import static io.anuke.mindustry.Vars.sectorSize; import static io.anuke.mindustry.Vars.state; import static io.anuke.mindustry.Vars.world; public class WorldGenerator{ - private int seed; - int oreIndex = 0; + private final int seed = 0; + private int oreIndex = 0; + + private Simplex sim = new Simplex(seed); + private Simplex sim2 = new Simplex(seed + 1); + private Simplex sim3 = new Simplex(seed + 2); + + private SeedRandom random = new SeedRandom(seed + 3); + + private GenResult result = new GenResult(); + private ObjectMap decoration; + + public WorldGenerator(){ + decoration = Mathf.map( + Blocks.grass, Blocks.shrub, + Blocks.stone, Blocks.rock, + Blocks.ice, Blocks.icerock, + Blocks.snow, Blocks.icerock, + Blocks.blackstone, Blocks.blackrock + ); + } /**Loads raw map tile data into a Tile[][] array, setting up multiblocks, cliffs and ores. */ public void loadTileData(Tile[][] tiles, MapTileData data, boolean genOres, int seed){ @@ -146,74 +166,12 @@ public class WorldGenerator{ } public void generateMap(Tile[][] tiles, int seed){ - MathUtils.random.setSeed((long) (Math.random() * 99999999)); - Simplex sim = new Simplex(Mathf.random(99999)); - Simplex sim2 = new Simplex(Mathf.random(99999)); - Simplex sim3 = new Simplex(Mathf.random(99999)); - - SeedRandom random = new SeedRandom(Mathf.random(99999)); - int width = tiles.length, height = tiles[0].length; - ObjectMap decoration = new ObjectMap<>(); - - decoration.put(Blocks.grass, Blocks.shrub); - decoration.put(Blocks.stone, Blocks.rock); - decoration.put(Blocks.ice, Blocks.icerock); - decoration.put(Blocks.snow, Blocks.icerock); - decoration.put(Blocks.blackstone, Blocks.blackrock); - for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ - Block floor = Blocks.stone; - Block wall = Blocks.air; - - double elevation = sim.octaveNoise2D(3, 0.5, 1f / 100, x, y) * 4.1 - 1; - double temp = sim3.octaveNoise2D(7, 0.54, 1f / 320f, x, y); - - double r = sim2.octaveNoise2D(1, 0.6, 1f / 70, x, y); - double edgeDist = Math.max(width / 2, height / 2) - Math.max(Math.abs(x - width / 2), Math.abs(y - height / 2)); - double dst = Vector2.dst(width / 2, height / 2, x, y); - double elevDip = 30; - - double border = 14; - - if(edgeDist < border){ - elevation += (border - edgeDist) / 6.0; - } - - if(temp < 0.35){ - floor = Blocks.snow; - }else if(temp < 0.45){ - floor = Blocks.stone; - }else if(temp < 0.65){ - floor = Blocks.grass; - }else if(temp < 0.8){ - floor = Blocks.sand; - }else if(temp < 0.9){ - floor = Blocks.blackstone; - elevation = 0f; - }else{ - floor = Blocks.lava; - } - - if(dst < elevDip){ - elevation -= (elevDip - dst) / elevDip * 3.0; - }else if(r > 0.9){ - floor = Blocks.water; - elevation = 0; - - if(r > 0.94){ - floor = Blocks.deepwater; - } - } - - if(wall == Blocks.air && decoration.containsKey(floor) && random.chance(0.03)){ - wall = decoration.get(floor); - } - - Tile tile = new Tile(x, y, (byte) floor.id, (byte) wall.id); - tile.elevation = (byte) Math.max(elevation, 0); + GenResult result = generateTile(0, 0, x, y); + Tile tile = new Tile(x, y, (byte)result.floor.id, (byte)result.wall.id, (byte)0, (byte)0, result.elevation); tiles[x][y] = tile; } } @@ -243,6 +201,72 @@ public class WorldGenerator{ prepareTiles(tiles, seed, true); } + public void setSector(int sectorX, int sectorY){ + random.setSeed(Bits.packLong(sectorX, sectorY)); + } + + public GenResult generateTile(int sectorX, int sectorY, int localX, int localY){ + int x = sectorX * sectorSize + localX; + int y = sectorY * sectorSize + localY; + + Block floor = Blocks.stone; + Block wall = Blocks.air; + + double elevation = sim.octaveNoise2D(3, 0.5, 1f / 100, x, y) * 4.1 - 1; + double temp = sim3.octaveNoise2D(7, 0.54, 1f / 320f, x, y); + + double r = sim2.octaveNoise2D(1, 0.6, 1f / 70, x, y); + double edgeDist = Math.max(sectorSize / 2, sectorSize / 2) - Math.max(Math.abs(x - sectorSize / 2), Math.abs(y - sectorSize / 2)); + double dst = Vector2.dst(sectorSize / 2, sectorSize / 2, x, y); + double elevDip = 30; + + double border = 14; + + if(edgeDist < border){ + elevation += (border - edgeDist) / 6.0; + } + + if(temp < 0.35){ + floor = Blocks.snow; + }else if(temp < 0.45){ + floor = Blocks.stone; + }else if(temp < 0.65){ + floor = Blocks.grass; + }else if(temp < 0.8){ + floor = Blocks.sand; + }else if(temp < 0.9){ + floor = Blocks.blackstone; + elevation = 0f; + }else{ + floor = Blocks.lava; + } + + if(dst < elevDip){ + elevation -= (elevDip - dst) / elevDip * 3.0; + }else if(r > 0.9){ + floor = Blocks.water; + elevation = 0; + + if(r > 0.94){ + floor = Blocks.deepwater; + } + } + + if(wall == Blocks.air && decoration.containsKey(floor) && random.chance(0.03)){ + wall = decoration.get(floor); + } + + result.wall = wall; + result.floor = floor; + result.elevation = (byte) Math.max(elevation, 0); + return result; + } + + public class GenResult{ + public Block floor, wall; + public byte elevation; + } + public class OreEntry{ final float frequency; final Item item; diff --git a/core/src/io/anuke/mindustry/ui/dialogs/SectorsDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/SectorsDialog.java index 9a7b42de8b..a6daa13aad 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/SectorsDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/SectorsDialog.java @@ -1,8 +1,101 @@ package io.anuke.mindustry.ui.dialogs; +import com.badlogic.gdx.math.Rectangle; +import io.anuke.mindustry.maps.Sector; +import io.anuke.ucore.core.Graphics; +import io.anuke.ucore.graphics.Draw; +import io.anuke.ucore.graphics.Lines; +import io.anuke.ucore.scene.Element; +import io.anuke.ucore.scene.event.InputEvent; +import io.anuke.ucore.scene.event.InputListener; +import io.anuke.ucore.scene.utils.Cursors; +import io.anuke.ucore.scene.utils.ScissorStack; + +import static io.anuke.mindustry.Vars.world; + public class SectorsDialog extends FloatingDialog{ + private Rectangle clip = new Rectangle(); public SectorsDialog(){ - super(""); + super("$text.sectors"); + + addCloseButton(); + setup(); + } + + void setup(){ + content().clear(); + + content().add(new SectorView()).grow(); + } + + class SectorView extends Element{ + float panX, panY; + float lastX, lastY; + float sectorSize = 100f; + + SectorView(){ + addListener(new InputListener(){ + @Override + public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ + Cursors.setHand(); + lastX = x; + lastY = y; + return true; + } + + @Override + public void touchDragged(InputEvent event, float x, float y, int pointer){ + panX -= x - lastX; + panY -= y - lastY; + + lastX = x; + lastY = y; + } + + @Override + public void touchUp(InputEvent event, float x, float y, int pointer, int button){ + Cursors.restoreCursor(); + } + }); + } + + @Override + public void draw(){ + Draw.alpha(alpha); + + float clipSize = Math.min(width, height); + int shownSectors = Math.round(clipSize/sectorSize/2f + 1f); + clip.setSize(clipSize).setCenter(x + width/2f, y + height/2f); + Graphics.flush(); + boolean clipped = ScissorStack.pushScissors(clip); + + int offsetX = (int)(panX / sectorSize); + int offsetY = (int)(panY / sectorSize); + + for(int x = -shownSectors; x <= shownSectors; x++){ + for(int y = -shownSectors; y <= shownSectors; y++){ + int sectorX = offsetX + x; + int sectorY = offsetY + y; + + float drawX = x + width/2f+ sectorX * sectorSize - offsetX * sectorSize - panX % sectorSize; + float drawY = y + height/2f + sectorY * sectorSize - offsetY * sectorSize - panY % sectorSize; + + if(world.sectors().get(sectorX, sectorY) == null){ + world.sectors().unlockSector(sectorX, sectorY); + } + + Sector sector = world.sectors().get(sectorX, sectorY); + Draw.rect(sector.texture, drawX, drawY, sectorSize, sectorSize); + Lines.stroke(2f); + Lines.crect(drawX, drawY, sectorSize, sectorSize); + + } + } + + Draw.reset(); + Graphics.flush(); + if(clipped) ScissorStack.popScissors(); + } } } diff --git a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java index 2390108a84..7b92914c96 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java @@ -148,7 +148,7 @@ public class MenuFragment extends Fragment{ dialog.content().add(new MenuButton("icon-play-2", "$text.campaign", () -> { dialog.hide(); - ui.levels.show(); + ui.sectors.show(); })).width(bw).colspan(2); dialog.content().row(); diff --git a/core/src/io/anuke/mindustry/world/BaseBlock.java b/core/src/io/anuke/mindustry/world/BaseBlock.java index b9884bb247..5f1236e53c 100644 --- a/core/src/io/anuke/mindustry/world/BaseBlock.java +++ b/core/src/io/anuke/mindustry/world/BaseBlock.java @@ -23,6 +23,8 @@ public abstract class BaseBlock{ public boolean outputsLiquid = false; public boolean singleLiquid = true; + public boolean outputsItems = false; + public int itemCapacity; public float liquidCapacity = 10f; public float liquidFlowFactor = 4.9f; 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 5d3ab38b06..8c20945cf7 100644 --- a/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java +++ b/core/src/io/anuke/mindustry/world/blocks/distribution/Conveyor.java @@ -126,7 +126,7 @@ public class Conveyor extends Block{ Tile other = tile.getNearby(Mathf.mod(tile.getRotation() - direction, 4)); if(other != null) other = other.target(); - if(other == null || !(other.block().hasItems) /*|| !(other.block().outputsLiquid)*/) return false; + if(other == null || (!other.block().outputsItems && !other.block().hasItems)) return false; return (tile.getNearby(tile.getRotation()) == other) || (!other.block().rotate || other.getNearby(other.getRotation()) == tile); } diff --git a/core/src/io/anuke/mindustry/world/blocks/distribution/Junction.java b/core/src/io/anuke/mindustry/world/blocks/distribution/Junction.java index f3267a1cf1..f5e795d5be 100644 --- a/core/src/io/anuke/mindustry/world/blocks/distribution/Junction.java +++ b/core/src/io/anuke/mindustry/world/blocks/distribution/Junction.java @@ -20,6 +20,7 @@ public class Junction extends Block{ update = true; solid = true; instantTransfer = true; + outputsItems = true; group = BlockGroup.transportation; } diff --git a/core/src/io/anuke/mindustry/world/blocks/distribution/Sorter.java b/core/src/io/anuke/mindustry/world/blocks/distribution/Sorter.java index 726aa1aadb..d45bb84078 100644 --- a/core/src/io/anuke/mindustry/world/blocks/distribution/Sorter.java +++ b/core/src/io/anuke/mindustry/world/blocks/distribution/Sorter.java @@ -27,6 +27,7 @@ public class Sorter extends Block implements SelectionTrait{ update = true; solid = true; instantTransfer = true; + outputsItems = true; group = BlockGroup.transportation; configurable = true; }