diff --git a/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java b/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java index 2ace8d96b9..fc83ce3e3b 100644 --- a/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java +++ b/annotations/src/main/java/mindustry/annotations/impl/AssetsProcess.java @@ -114,11 +114,11 @@ public class AssetsProcess extends BaseProcessor{ dispose.addStatement("arc.Core.assets.unload(" + filename + ")"); dispose.addStatement(name + " = null"); - type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), name, Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio.mock.Mock" + rtype.substring(rtype.lastIndexOf(".") + 1)+ "()").build()); + type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), name, Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.mock.Mock" + rtype.substring(rtype.lastIndexOf(".") + 1)+ "()").build()); }); if(classname.equals("Sounds")){ - type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), "none", Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.audio.mock.Mock" + rtype.substring(rtype.lastIndexOf(".") + 1)+ "()").build()); + type.addField(FieldSpec.builder(ClassName.bestGuess(rtype), "none", Modifier.STATIC, Modifier.PUBLIC).initializer("new arc.mock.Mock" + rtype.substring(rtype.lastIndexOf(".") + 1)+ "()").build()); } type.addMethod(loadBegin.build()); diff --git a/build.gradle b/build.gradle index 8aa9762606..2b00a2ab94 100644 --- a/build.gradle +++ b/build.gradle @@ -298,7 +298,7 @@ project(":tools"){ compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" compile "org.reflections:reflections:0.9.12" - compile arcModule("backends:backend-sdl") + compile arcModule("backends:backend-headless") } } diff --git a/core/assets/planets/TODO.dat b/core/assets/planets/TODO.dat new file mode 100644 index 0000000000..143e63ffb9 Binary files /dev/null and b/core/assets/planets/TODO.dat differ diff --git a/core/src/mindustry/game/Stats.java b/core/src/mindustry/game/Stats.java index 2cb2a4e430..bd0ae219c1 100644 --- a/core/src/mindustry/game/Stats.java +++ b/core/src/mindustry/game/Stats.java @@ -39,7 +39,7 @@ public class Stats{ //weigh used fractions float frac = 0f; - Array obtainable = Array.with(zone.resources).select(i -> i instanceof Item && ((Item)i).type == ItemType.material).as(Item.class); + Array obtainable = Array.with(zone.data.resources).select(i -> i instanceof Item && ((Item)i).type == ItemType.material).as(Item.class); for(Item item : obtainable){ frac += Mathf.clamp((float)itemsDelivered.get(item, 0) / capacity) / (float)obtainable.size; } diff --git a/core/src/mindustry/maps/generators/MapGenerator.java b/core/src/mindustry/maps/generators/MapGenerator.java index d85501faf6..54e5469bc1 100644 --- a/core/src/mindustry/maps/generators/MapGenerator.java +++ b/core/src/mindustry/maps/generators/MapGenerator.java @@ -49,7 +49,7 @@ public class MapGenerator extends Generator{ for(Tile tile : tiles){ if(tile.block() instanceof StorageBlock && !(tile.block() instanceof CoreBlock) && world.getSector() != null){ - for(Content content : world.getSector().resources){ + for(Content content : world.getSector().data.resources){ if(content instanceof Item && Mathf.chance(0.3)){ tile.entity.items().add((Item)content, Math.min(Mathf.random(500), tile.block().itemCapacity)); } diff --git a/core/src/mindustry/maps/planet/TestPlanetGenerator.java b/core/src/mindustry/maps/planet/TestPlanetGenerator.java index 3bd9d3293d..0e307c107b 100644 --- a/core/src/mindustry/maps/planet/TestPlanetGenerator.java +++ b/core/src/mindustry/maps/planet/TestPlanetGenerator.java @@ -65,7 +65,6 @@ public class TestPlanetGenerator implements PlanetGenerator{ height *= 1.2f; height = Mathf.clamp(height); - return arr[Mathf.clamp((int)(temp * arr.length), 0, arr.length - 1)][Mathf.clamp((int)(height * arr[0].length), 0, arr[0].length - 1)]; } } diff --git a/core/src/mindustry/mod/ContentParser.java b/core/src/mindustry/mod/ContentParser.java index fc11b1ba59..1b7a179a48 100644 --- a/core/src/mindustry/mod/ContentParser.java +++ b/core/src/mindustry/mod/ContentParser.java @@ -3,10 +3,10 @@ package mindustry.mod; import arc.*; import arc.assets.*; import arc.audio.*; -import arc.audio.mock.*; import arc.files.*; import arc.func.*; import arc.graphics.*; +import arc.mock.MockSound; import arc.struct.Array; import arc.struct.*; import arc.util.ArcAnnotate.*; diff --git a/core/src/mindustry/mod/ModLoadingSound.java b/core/src/mindustry/mod/ModLoadingSound.java index 558b29ce28..0b1197eb02 100644 --- a/core/src/mindustry/mod/ModLoadingSound.java +++ b/core/src/mindustry/mod/ModLoadingSound.java @@ -1,8 +1,8 @@ package mindustry.mod; import arc.audio.*; -import arc.audio.mock.*; import arc.math.geom.*; +import arc.mock.MockSound; import arc.util.ArcAnnotate.*; public class ModLoadingSound implements Sound{ diff --git a/core/src/mindustry/type/Planet.java b/core/src/mindustry/type/Planet.java index f81af07d6a..0fb7698674 100644 --- a/core/src/mindustry/type/Planet.java +++ b/core/src/mindustry/type/Planet.java @@ -1,14 +1,18 @@ package mindustry.type; +import arc.files.*; import arc.math.geom.*; import arc.scene.ui.layout.*; import arc.struct.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; +import mindustry.*; import mindustry.ctype.*; import mindustry.graphics.*; import mindustry.graphics.PlanetGrid.*; import mindustry.maps.planet.*; +import mindustry.type.Sector.*; public class Planet extends UnlockableContent{ /** Mesh used for rendering. Created on load() - will be null on the server! */ @@ -32,9 +36,25 @@ public class Planet extends UnlockableContent{ this.size = 3; grid = PlanetGrid.newGrid(size); + sectors = new Array<>(grid.tiles.length); for(int i = 0; i < grid.tiles.length; i++){ - sectors.add(new Sector(this, grid.tiles[i])); + //TODO load sector data + sectors.add(new Sector(this, grid.tiles[i], new SectorData())); + } + + //read data + Fi data = Vars.tree.get("planets/" + name + ".dat"); + if(data.exists()){ + try(Reads read = data.reads()){ + short dsize = read.s(); + for(int i = 0; i < dsize; i++){ + sectors.get(i).data.read(read); + } + } + }else{ + //TODO crash instead - this is a critical error! + Log.err("Planet {0} is missing its data file."); } } diff --git a/core/src/mindustry/type/Sector.java b/core/src/mindustry/type/Sector.java index 91401c859e..2bb9169a3d 100644 --- a/core/src/mindustry/type/Sector.java +++ b/core/src/mindustry/type/Sector.java @@ -2,10 +2,11 @@ package mindustry.type; import arc.math.geom.*; import arc.util.*; +import arc.util.io.*; +import mindustry.*; import mindustry.ctype.*; import mindustry.graphics.PlanetGrid.*; - -import static mindustry.Vars.state; +import mindustry.world.*; /** A small section of a planet. */ public class Sector{ @@ -14,16 +15,16 @@ public class Sector{ public final Ptile tile; public final int id; - //TODO generate a class file with 2D arrays of resources for each sector for each planet - public final Content[] resources = {}; + public final SectorData data; //TODO implement a dynamic (?) launch period public int launchPeriod = 10; - public Sector(Planet planet, Ptile tile){ + public Sector(Planet planet, Ptile tile, SectorData data){ this.planet = planet; this.tile = tile; this.rect = makeRect(); this.id = tile.id; + this.data = data; } //TODO implement @@ -31,7 +32,6 @@ public class Sector{ return metCondition() && wave % launchPeriod == 0; } - public boolean metCondition(){ //TODO implement return false; @@ -83,4 +83,43 @@ public class Sector{ return result.set(center).add(right, nx).add(top, ny); } } + + /** Cached data about a sector. */ + public static class SectorData{ + public Content[] resources = {}; + public int spawnX, spawnY; + + public Block[] floors = {}; + public int[] floorCounts = {}; + + public void write(Writes write){ + write.s(resources.length); + for(Content resource : resources){ + write.b(resource.getContentType().ordinal()); + write.s(resource.id); + } + write.s(spawnX); + write.s(spawnY); + write.s(floors.length); + for(int i = 0; i < floors.length; i++){ + write.s(floors[i].id); + write.i(floorCounts[i]); + } + } + + public void read(Reads read){ + resources = new Content[read.s()]; + for(int i = 0; i < resources.length; i++){ + resources[i] = Vars.content.getByID(ContentType.all[read.b()], read.s()); + } + spawnX = read.s(); + spawnY = read.s(); + floors = new Block[read.s()]; + floorCounts = new int[floors.length]; + for(int i = 0; i < floors.length; i++){ + floors[i] = Vars.content.block(read.s()); + floorCounts[i] = read.i(); + } + } + } } diff --git a/tools/build.gradle b/tools/build.gradle index e527ef1e10..726a7acfb2 100644 --- a/tools/build.gradle +++ b/tools/build.gradle @@ -344,6 +344,13 @@ task genSprites(dependsOn: classes, type: JavaExec){ workingDir = genFolder } +task genSectorData(dependsOn: classes, type: JavaExec){ + main = "mindustry.tools.SectorDataGenerator" + classpath = sourceSets.main.runtimeClasspath + standardInput = System.in + workingDir = "../core/assets/" +} + task updateBundles(dependsOn: classes, type: JavaExec){ file(genFolder).mkdirs() diff --git a/tools/src/mindustry/tools/SectorDataGenerator.java b/tools/src/mindustry/tools/SectorDataGenerator.java new file mode 100644 index 0000000000..9d21679415 --- /dev/null +++ b/tools/src/mindustry/tools/SectorDataGenerator.java @@ -0,0 +1,93 @@ +package mindustry.tools; + +import arc.*; +import arc.files.*; +import arc.mock.*; +import arc.struct.*; +import arc.util.*; +import arc.util.io.*; +import mindustry.*; +import mindustry.core.*; +import mindustry.ctype.*; +import mindustry.net.Net; +import mindustry.type.*; +import mindustry.type.Sector.*; +import mindustry.world.*; + +import static mindustry.Vars.*; + +public class SectorDataGenerator{ + + public static void main(String[] args){ + ArcNativesLoader.load(); + + Core.files = new MockFiles(); + Core.app = new MockApplication(); + Core.settings = new MockSettings(); + + headless = true; + net = new Net(null); + tree = new FileTree(); + Vars.init(); + content.createBaseContent(); + + logic = new Logic(); + netServer = new NetServer(); + world = new World(); + + content.init(); + + for(Planet planet : content.getBy(ContentType.planet)){ + int[] count = {0}; + + Fi fi = Fi.get("planets").child(planet.name + ".dat"); + + Array list = planet.sectors.map(sector -> { + SectorData data = new SectorData(); + + ObjectIntMap floors = new ObjectIntMap<>(); + ObjectSet content = new ObjectSet<>(); + + world.loadSector(sector); + + for(Tile tile : world.tiles){ + Item item = tile.floor().itemDrop; + Liquid liquid = tile.floor().liquidDrop; + if(item != null) content.add(item); + if(liquid != null) content.add(liquid); + + if(!tile.block().isStatic()){ + floors.increment(tile.floor()); + } + } + + //sort counts in descending order + Array> entries = floors.entries().toArray(); + entries.sort(e -> -e.value); + //remove all blocks occuring < 30 times - unimportant + entries.removeAll(e -> e.value < 30); + + data.floors = new Block[entries.size]; + data.floorCounts = new int[entries.size]; + for(int i = 0; i < entries.size; i++){ + data.floorCounts[i] = entries.get(i).value; + data.floors[i] = entries.get(i).key; + } + + data.resources = content.asArray().sort(Structs.comps(Structs.comparing(Content::getContentType), Structs.comparingInt(c -> c.id))).toArray(Content.class); + + if(count[0]++ % 5 == 0){ + Log.info("&lyDone with sector &lm{0}/{1}", count[0], planet.sectors.size); + } + + return data; + }); + + //write data + try(Writes write = fi.writes()){ + write.s(list.size); + list.each(s -> s.write(write)); + } + } + } +}