it is done
This commit is contained in:
262
core/src/mindustry/maps/generators/BasicGenerator.java
Normal file
262
core/src/mindustry/maps/generators/BasicGenerator.java
Normal file
@@ -0,0 +1,262 @@
|
||||
package mindustry.maps.generators;
|
||||
|
||||
import arc.struct.*;
|
||||
import arc.func.Intc2;
|
||||
import arc.math.Mathf;
|
||||
import arc.math.geom.Geometry;
|
||||
import arc.math.geom.Point2;
|
||||
import arc.util.Structs;
|
||||
import arc.util.noise.Simplex;
|
||||
import mindustry.content.Blocks;
|
||||
import mindustry.world.*;
|
||||
import mindustry.world.blocks.Floor;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public abstract class BasicGenerator extends RandomGenerator{
|
||||
protected static final DistanceHeuristic manhattan = (x1, y1, x2, y2) -> Math.abs(x1 - x2) + Math.abs(y1 - y2);
|
||||
|
||||
protected Array<Block> ores;
|
||||
protected Simplex sim = new Simplex();
|
||||
protected Simplex sim2 = new Simplex();
|
||||
|
||||
public BasicGenerator(int width, int height, Block... ores){
|
||||
super(width, height);
|
||||
this.ores = Array.with(ores);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(Tile[][] tiles){
|
||||
int seed = Mathf.random(99999999);
|
||||
sim.setSeed(seed);
|
||||
sim2.setSeed(seed + 1);
|
||||
super.generate(tiles);
|
||||
}
|
||||
|
||||
public void ores(Tile[][] tiles){
|
||||
pass(tiles, (x, y) -> {
|
||||
if(ores != null){
|
||||
int offsetX = x - 4, offsetY = y + 23;
|
||||
for(int i = ores.size - 1; i >= 0; i--){
|
||||
Block entry = ores.get(i);
|
||||
if(Math.abs(0.5f - sim.octaveNoise2D(2, 0.7, 1f / (40 + i * 2), offsetX, offsetY + i*999)) > 0.26f &&
|
||||
Math.abs(0.5f - sim2.octaveNoise2D(1, 1, 1f / (30 + i * 4), offsetX, offsetY - i*999)) > 0.37f){
|
||||
ore = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void terrain(Tile[][] tiles, Block dst, float scl, float mag, float cmag){
|
||||
pass(tiles, (x, y) -> {
|
||||
double rocks = sim.octaveNoise2D(5, 0.5, 1f / scl, x, y) * mag
|
||||
+ Mathf.dst((float)x / width, (float)y / height, 0.5f, 0.5f) * cmag;
|
||||
|
||||
double edgeDist = Math.min(x, Math.min(y, Math.min(Math.abs(x - (width - 1)), Math.abs(y - (height - 1)))));
|
||||
double transition = 5;
|
||||
if(edgeDist < transition){
|
||||
rocks += (transition - edgeDist) / transition / 1.5;
|
||||
}
|
||||
|
||||
if(rocks > 0.9){
|
||||
block =dst;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void noise(Tile[][] tiles, Block floor, Block block, int octaves, float falloff, float scl, float threshold){
|
||||
sim.setSeed(Mathf.random(99999));
|
||||
pass(tiles, (x, y) -> {
|
||||
if(sim.octaveNoise2D(octaves, falloff, 1f / scl, x, y) > threshold){
|
||||
Tile tile = tiles[x][y];
|
||||
this.floor = floor;
|
||||
if(tile.block().solid){
|
||||
this.block = block;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void overlay(Tile[][] tiles, Block floor, Block block, float chance, int octaves, float falloff, float scl, float threshold){
|
||||
sim.setSeed(Mathf.random(99999));
|
||||
pass(tiles, (x, y) -> {
|
||||
if(sim.octaveNoise2D(octaves, falloff, 1f / scl, x, y) > threshold && Mathf.chance(chance) && tiles[x][y].floor() == floor){
|
||||
ore = block;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void tech(Tile[][] tiles){
|
||||
Block[] blocks = {Blocks.darkPanel3};
|
||||
int secSize = 20;
|
||||
pass(tiles, (x, y) -> {
|
||||
int mx = x % secSize, my = y % secSize;
|
||||
int sclx = x / secSize, scly = y / secSize;
|
||||
if(noise(sclx, scly, 10f, 1f) > 0.63f && (mx == 0 || my == 0 || mx == secSize - 1 || my == secSize - 1)){
|
||||
if(Mathf.chance(noise(x + 0x231523, y, 40f, 1f))){
|
||||
floor = Structs.random(blocks);
|
||||
if(Mathf.dst(mx, my, secSize/2, secSize/2) > secSize/2f + 2){
|
||||
floor = Blocks.darkPanel4;
|
||||
}
|
||||
}
|
||||
|
||||
if(block.solid && Mathf.chance(0.7)){
|
||||
block = Blocks.darkMetal;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void distort(Tile[][] tiles, float scl, float mag){
|
||||
Block[][] blocks = new Block[width][height];
|
||||
Floor[][] floors = new Floor[width][height];
|
||||
|
||||
each((x, y) -> {
|
||||
float cx = x + noise(x, y, scl, mag) - mag / 2f, cy = y + noise(x, y + 1525215f, scl, mag) - mag / 2f;
|
||||
Tile other = tiles[Mathf.clamp((int)cx, 0, width-1)][Mathf.clamp((int)cy, 0, height-1)];
|
||||
blocks[x][y] = other.block();
|
||||
floors[x][y] = other.floor();
|
||||
});
|
||||
|
||||
pass(tiles, (x, y) -> {
|
||||
floor = floors[x][y];
|
||||
block = blocks[x][y];
|
||||
});
|
||||
}
|
||||
|
||||
public void scatter(Tile[][] tiles, Block target, Block dst, float chance){
|
||||
pass(tiles, (x, y) -> {
|
||||
if(!Mathf.chance(chance)) return;
|
||||
if(floor == target){
|
||||
floor = dst;
|
||||
}else if(block == target){
|
||||
block = dst;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void each(Intc2 r){
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
r.get(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected float noise(float x, float y, float scl, float mag){
|
||||
return (float)sim2.octaveNoise2D(1f, 0f, 1f / scl, x + 0x361266f, y + 0x251259f) * mag;
|
||||
}
|
||||
|
||||
public void pass(Tile[][] tiles, Intc2 r){
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
floor = tiles[x][y].floor();
|
||||
block = tiles[x][y].block();
|
||||
ore = tiles[x][y].overlay();
|
||||
r.get(x, y);
|
||||
tiles[x][y] = new Tile(x, y, floor.id, ore.id, block.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void brush(Tile[][] tiles, Array<Tile> path, int rad){
|
||||
path.each(tile -> erase(tiles, tile.x, tile.y, rad));
|
||||
}
|
||||
|
||||
public void erase(Tile[][] tiles, int cx, int cy, int rad){
|
||||
for(int x = -rad; x <= rad; x++){
|
||||
for(int y = -rad; y <= rad; y++){
|
||||
int wx = cx + x, wy = cy + y;
|
||||
if(Structs.inBounds(wx, wy, width, height) && Mathf.dst(x, y, 0, 0) <= rad){
|
||||
Tile other = tiles[wx][wy];
|
||||
other.setBlock(Blocks.air);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Array<Tile> pathfind(Tile[][] tiles, int startX, int startY, int endX, int endY, TileHueristic th, DistanceHeuristic dh){
|
||||
Tile start = tiles[startX][startY];
|
||||
Tile end = tiles[endX][endY];
|
||||
GridBits closed = new GridBits(width, height);
|
||||
IntFloatMap costs = new IntFloatMap();
|
||||
PriorityQueue<Tile> queue = new PriorityQueue<>(tiles.length * tiles[0].length / 2, (a, b) -> Float.compare(costs.get(a.pos(), 0f) + dh.cost(a.x, a.y, end.x, end.y), costs.get(b.pos(), 0f) + dh.cost(b.x, b.y, end.x, end.y)));
|
||||
queue.add(start);
|
||||
boolean found = false;
|
||||
while(!queue.isEmpty()){
|
||||
Tile next = queue.poll();
|
||||
float baseCost = costs.get(next.pos(), 0f);
|
||||
if(next == end){
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
closed.set(next.x, next.y);
|
||||
for(Point2 point : Geometry.d4){
|
||||
int newx = next.x + point.x, newy = next.y + point.y;
|
||||
if(Structs.inBounds(newx, newy, width, height)){
|
||||
Tile child = tiles[newx][newy];
|
||||
if(!closed.get(child.x, child.y)){
|
||||
closed.set(child.x, child.y);
|
||||
child.rotation(child.relativeTo(next.x, next.y));
|
||||
costs.put(child.pos(), th.cost(child) + baseCost);
|
||||
queue.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Array<Tile> out = new Array<>();
|
||||
|
||||
if(!found) return out;
|
||||
|
||||
Tile current = end;
|
||||
while(current != start){
|
||||
out.add(current);
|
||||
Point2 p = Geometry.d4(current.rotation());
|
||||
current = tiles[current.x + p.x][current.y + p.y];
|
||||
}
|
||||
|
||||
out.reverse();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
public void inverseFloodFill(Tile[][] tiles, Tile start, Block block){
|
||||
IntArray arr = new IntArray();
|
||||
arr.add(start.pos());
|
||||
while(!arr.isEmpty()){
|
||||
int i = arr.pop();
|
||||
int x = Pos.x(i), y = Pos.y(i);
|
||||
tiles[x][y].cost = 2;
|
||||
for(Point2 point : Geometry.d4){
|
||||
int newx = x + point.x, newy = y + point.y;
|
||||
if(Structs.inBounds(newx, newy, width, height)){
|
||||
Tile child = tiles[newx][newy];
|
||||
if(child.block() == Blocks.air && child.cost != 2){
|
||||
child.cost = 2;
|
||||
arr.add(child.pos());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < width; x ++){
|
||||
for(int y = 0; y < height; y++){
|
||||
Tile tile = tiles[x][y];
|
||||
if(tile.cost != 2 && tile.block() == Blocks.air){
|
||||
tile.setBlock(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface DistanceHeuristic{
|
||||
float cost(int x1, int y1, int x2, int y2);
|
||||
}
|
||||
|
||||
public interface TileHueristic{
|
||||
float cost(Tile tile);
|
||||
}
|
||||
}
|
||||
23
core/src/mindustry/maps/generators/Generator.java
Normal file
23
core/src/mindustry/maps/generators/Generator.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package mindustry.maps.generators;
|
||||
|
||||
import mindustry.game.*;
|
||||
import mindustry.world.*;
|
||||
|
||||
public abstract class Generator{
|
||||
public int width, height;
|
||||
protected Schematic loadout;
|
||||
|
||||
public Generator(int width, int height){
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public Generator(){
|
||||
}
|
||||
|
||||
public void init(Schematic loadout){
|
||||
this.loadout = loadout;
|
||||
}
|
||||
|
||||
public abstract void generate(Tile[][] tiles);
|
||||
}
|
||||
170
core/src/mindustry/maps/generators/MapGenerator.java
Normal file
170
core/src/mindustry/maps/generators/MapGenerator.java
Normal file
@@ -0,0 +1,170 @@
|
||||
package mindustry.maps.generators;
|
||||
|
||||
import arc.struct.*;
|
||||
import arc.math.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.util.*;
|
||||
import mindustry.content.*;
|
||||
import mindustry.game.*;
|
||||
import mindustry.io.*;
|
||||
import mindustry.maps.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.world.*;
|
||||
import mindustry.world.blocks.*;
|
||||
import mindustry.world.blocks.storage.*;
|
||||
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
public class MapGenerator extends Generator{
|
||||
private Map map;
|
||||
private String mapName;
|
||||
private Array<Decoration> decorations = Array.with(new Decoration(Blocks.stone, Blocks.rock, 0.003f));
|
||||
/**
|
||||
* The amount of final enemy spawns used. -1 to use everything in the map.
|
||||
* This amount of enemy spawns is selected randomly from the map.
|
||||
*/
|
||||
public int enemySpawns = -1;
|
||||
/** Whether floor is distorted along with blocks. */
|
||||
public boolean distortFloor = false;
|
||||
|
||||
public MapGenerator(String mapName){
|
||||
this.mapName = mapName;
|
||||
}
|
||||
|
||||
public MapGenerator(String mapName, int enemySpawns){
|
||||
this.mapName = mapName;
|
||||
this.enemySpawns = enemySpawns;
|
||||
}
|
||||
|
||||
public MapGenerator decor(Decoration... decor){
|
||||
this.decorations.addAll(decor);
|
||||
return this;
|
||||
}
|
||||
public void removePrefix(String name){
|
||||
this.mapName = this.mapName.substring(name.length() + 1);
|
||||
}
|
||||
|
||||
{
|
||||
decor(new Decoration(Blocks.snow, Blocks.snowrock, 0.01), new Decoration(Blocks.ignarock, Blocks.pebbles, 0.03f));
|
||||
}
|
||||
|
||||
public Map getMap(){
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Schematic loadout){
|
||||
this.loadout = loadout;
|
||||
map = maps.loadInternalMap(mapName);
|
||||
width = map.width;
|
||||
height = map.height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(Tile[][] tiles){
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
tiles[x][y] = new Tile(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
SaveIO.load(map.file);
|
||||
Array<Point2> players = new Array<>();
|
||||
Array<Point2> enemies = new Array<>();
|
||||
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
if(tiles[x][y].block() instanceof CoreBlock && tiles[x][y].getTeam() == defaultTeam){
|
||||
players.add(new Point2(x, y));
|
||||
tiles[x][y].setBlock(Blocks.air);
|
||||
}
|
||||
|
||||
if(tiles[x][y].overlay() == Blocks.spawn && enemySpawns != -1){
|
||||
enemies.add(new Point2(x, y));
|
||||
tiles[x][y].setOverlay(Blocks.air);
|
||||
}
|
||||
|
||||
if(tiles[x][y].block() instanceof BlockPart){
|
||||
tiles[x][y].setBlock(Blocks.air);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
Tile tile = tiles[x][y];
|
||||
|
||||
for(Decoration decor : decorations){
|
||||
if(x > 0 && y > 0 && (tiles[x - 1][y].block() == decor.wall || tiles[x][y - 1].block() == decor.wall)){
|
||||
continue;
|
||||
}
|
||||
|
||||
if(tile.block() == Blocks.air && !(decor.wall instanceof Floor) && tile.floor() == decor.floor && Mathf.chance(decor.chance)){
|
||||
tile.setBlock(decor.wall);
|
||||
}else if(tile.floor() == decor.floor && decor.wall.isOverlay() && Mathf.chance(decor.chance)){
|
||||
tile.setOverlay(decor.wall);
|
||||
}else if(tile.floor() == decor.floor && decor.wall.isFloor() && !decor.wall.isOverlay() && Mathf.chance(decor.chance)){
|
||||
tile.setFloor((Floor)decor.wall);
|
||||
}
|
||||
}
|
||||
|
||||
if(tile.block() instanceof StorageBlock && !(tile.block() instanceof CoreBlock) && world.getZone() != null){
|
||||
for(Item item : world.getZone().resources){
|
||||
if(Mathf.chance(0.3)){
|
||||
tile.entity.items.add(item, Math.min(Mathf.random(500), tile.block().itemCapacity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(enemySpawns != -1){
|
||||
if(enemySpawns > enemies.size){
|
||||
throw new IllegalArgumentException("Enemy spawn pool greater than map spawn number for map: " + mapName);
|
||||
}
|
||||
|
||||
enemies.shuffle();
|
||||
for(int i = 0; i < enemySpawns; i++){
|
||||
Point2 point = enemies.get(i);
|
||||
tiles[point.x][point.y].setOverlay(Blocks.spawn);
|
||||
|
||||
int rad = 10, frad = 12;
|
||||
|
||||
for(int x = -rad; x <= rad; x++){
|
||||
for(int y = -rad; y <= rad; y++){
|
||||
int wx = x + point.x, wy = y + point.y;
|
||||
double dst = Mathf.dst(x, y);
|
||||
if(dst < frad && Structs.inBounds(wx, wy, tiles) && (dst <= rad || Mathf.chance(0.5))){
|
||||
Tile tile = tiles[wx][wy];
|
||||
if(tile.overlay() != Blocks.spawn){
|
||||
tile.clearOverlay();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Point2 core = players.random();
|
||||
if(core == null){
|
||||
throw new IllegalArgumentException("All zone maps must have a core.");
|
||||
}
|
||||
|
||||
schematics.placeLoadout(loadout, core.x, core.y);
|
||||
|
||||
world.prepareTiles(tiles);
|
||||
world.setMap(map);
|
||||
}
|
||||
|
||||
public static class Decoration{
|
||||
public final Block floor;
|
||||
public final Block wall;
|
||||
public final double chance;
|
||||
|
||||
public Decoration(Block floor, Block wall, double chance){
|
||||
this.floor = floor;
|
||||
this.wall = wall;
|
||||
this.chance = chance;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
core/src/mindustry/maps/generators/RandomGenerator.java
Normal file
44
core/src/mindustry/maps/generators/RandomGenerator.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package mindustry.maps.generators;
|
||||
|
||||
import arc.struct.StringMap;
|
||||
import mindustry.content.Blocks;
|
||||
import mindustry.maps.Map;
|
||||
import mindustry.world.Block;
|
||||
import mindustry.world.Tile;
|
||||
|
||||
import static mindustry.Vars.world;
|
||||
|
||||
public abstract class RandomGenerator extends Generator{
|
||||
protected Block floor;
|
||||
protected Block block;
|
||||
protected Block ore;
|
||||
|
||||
public RandomGenerator(int width, int height){
|
||||
super(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(Tile[][] tiles){
|
||||
for(int x = 0; x < width; x++){
|
||||
for(int y = 0; y < height; y++){
|
||||
floor = Blocks.air;
|
||||
block = Blocks.air;
|
||||
ore = Blocks.air;
|
||||
generate(x, y);
|
||||
tiles[x][y] = new Tile(x, y, floor.id, ore.id, block.id);
|
||||
}
|
||||
}
|
||||
|
||||
decorate(tiles);
|
||||
|
||||
world.setMap(new Map(new StringMap()));
|
||||
}
|
||||
|
||||
public abstract void decorate(Tile[][] tiles);
|
||||
|
||||
/**
|
||||
* Sets {@link #floor} and {@link #block} to the correct values as output.
|
||||
* Before this method is called, both are set to {@link Blocks#air} as defaults.
|
||||
*/
|
||||
public abstract void generate(int x, int y);
|
||||
}
|
||||
Reference in New Issue
Block a user