1
0
mirror of https://github.com/Anuken/Mindustry.git synced 2024-11-11 14:56:10 +03:00

Moved over map generator / Fixed logic updating on both threads

This commit is contained in:
Anuken 2018-06-20 11:09:44 -04:00
parent 33a278ccb4
commit 56bb23400a
9 changed files with 41 additions and 280 deletions

View File

@ -102,7 +102,7 @@ public class Logic extends Module {
@Override
public void update(){
if(!doUpdate) return;
if(threads.isEnabled() && !threads.isOnThread()) return;
if(!state.is(State.menu)){

View File

@ -111,6 +111,10 @@ public class ThreadHandler {
return enabled;
}
public boolean isOnThread(){
return impl.isOnThread();
}
private void runLogic(){
try {
while (true) {

View File

@ -39,6 +39,9 @@ public class FloorRenderer {
}
public void drawFloor(){
if(cache == null){
return;
}
OrthographicCamera camera = Core.camera;
@ -103,6 +106,10 @@ public class FloorRenderer {
}
public void beginDraw(){
if(cache == null){
return;
}
cbatch.setProjectionMatrix(Core.camera.combined);
cbatch.beginDraw();
@ -110,10 +117,18 @@ public class FloorRenderer {
}
public void endDraw(){
if(cache == null){
return;
}
cbatch.endDraw();
}
public void drawLayer(CacheLayer layer){
if(cache == null){
return;
}
OrthographicCamera camera = Core.camera;
int crangex = (int)(camera.viewportWidth * camera.zoom / (chunksize * tilesize))+1;

View File

@ -1,8 +1,7 @@
package io.anuke.mindustry.server.mapgen;
package io.anuke.mindustry.world.mapgen;
public class GenProperties {
public long seed;
public SpawnStyle spawns;
public MapStyle maps;
public OreStyle ores;
public RiverType riverType;
@ -11,15 +10,6 @@ public class GenProperties {
public FoliageStyle foliage;
public EnvironmentStyle environment;
enum SpawnStyle{
/**spawn in a wide arc with branching paths*/
arc,
/**spawn in one big group*/
grouped,
/**surround player spawn*/
surround
}
enum MapStyle{
/**256x512*/
longY,

View File

@ -0,0 +1,20 @@
package io.anuke.mindustry.world.mapgen;
import io.anuke.mindustry.io.MapTileData;
import io.anuke.ucore.noise.RidgedPerlin;
import io.anuke.ucore.noise.Simplex;
public class ProcGen {
public RidgedPerlin rid = new RidgedPerlin(1, 1);
public Simplex sim = new Simplex();
public MapTileData generate(GenProperties props){
MapTileData data = new MapTileData(300, 300);
for (int x = 0; x < data.width(); x++) {
for (int y = 0; y < data.height(); y++) {
}
}
return null;
}
}

View File

@ -1,109 +0,0 @@
package io.anuke.mindustry.server.mapgen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntIntMap;
import com.badlogic.gdx.utils.IntSet;
import io.anuke.ucore.util.Mathf;
public class Colorizer {
Color tmp = new Color();
float[] hsv1 = new float[3];
float[] hsv2 = new float[3];
float target = 240f;
float shift = 12f;
float e = 0.05f;
public void process(FileHandle in, FileHandle out){
for(FileHandle child : in.list()){
if(child.isDirectory()){
process(child, out);
}else if(child.extension().equals("png")){
PixmapIO.writePNG(out.child(child.name()), colorize(new Pixmap(child)));
}
}
}
public Pixmap colorize(Pixmap pixmap){
Array<Array<Color>> colors = new Array<>();
IntSet used = new IntSet();
for(int x = 0; x < pixmap.getWidth(); x ++){
for(int y = 0; y < pixmap.getHeight(); y ++){
tmp.set(pixmap.getPixel(x, y));
if(tmp.a <= 0.1f || used.contains(Color.rgba8888(tmp))) continue;
used.add(Color.rgba8888(tmp));
boolean found = false;
outer:
for(Array<Color> arr : colors){
for(Color color : arr){
if(isSameShade(color, tmp)){
arr.add(tmp.cpy());
found = true;
break outer;
}
}
}
if(!found){
colors.add(Array.with(tmp.cpy()));
}
}
}
colors.forEach(a -> a.sort((c1, c2) -> Float.compare(c1.toHsv(hsv1)[2], c2.toHsv(hsv2)[2])));
IntIntMap map = new IntIntMap();
for(Array<Color> arr : colors){
for(int i = 0; i < arr.size; i ++){
int shift = arr.size - 1 - i;
map.put(Color.rgba8888(arr.get(i)), Color.rgba8888(shift(arr.get(i), shift)));
}
}
Pixmap result = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), pixmap.getFormat());
for(int x = 0; x < pixmap.getWidth(); x ++) {
for (int y = 0; y < pixmap.getHeight(); y++) {
result.drawPixel(x, y, map.get(pixmap.getPixel(x, y), 0));
}
}
return result;
}
Color shift(Color color, int amount){
color.toHsv(hsv1);
float h = hsv1[0];
/*if(hsv1[1] < e){
hsv1[1] += amount * 0.1f;
h = Mathf.lerp(0f, target, amount * 0.08f);
}*/
float s = amount * shift;
if(Math.abs(h - target) < s){
h = target;
}else{
if(h > target) h -= s;
if(h < target) h += s;
}
hsv1[0] = h;
tmp.fromHsv(hsv1);
tmp.a = color.a;
return tmp;
}
boolean isSameShade(Color a, Color b){
a.toHsv(hsv1);
b.toHsv(hsv2);
return Mathf.near(hsv1[0], hsv2[0], e*360f) && Mathf.near(hsv1[1], hsv2[1], e);
}
}

View File

@ -1,55 +0,0 @@
package io.anuke.mindustry.server.mapgen;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.ColorMapper;
import io.anuke.mindustry.world.ColorMapper.BlockPair;
import io.anuke.ucore.util.Mathf;
public class MapImage {
public final Pixmap pixmap;
public final int width, height;
public MapImage(Pixmap pixmap){
this.pixmap = pixmap;
this.width = pixmap.getWidth();
this.height = pixmap.getHeight();
}
public MapImage(int width, int height){
this(new Pixmap(width, height, Format.RGBA8888));
}
public Block wall(int x, int y){
BlockPair pair = ColorMapper.get(pixmap.getPixel(x, y));
return pair.wall;
}
public Block floor(int x, int y){
BlockPair pair = ColorMapper.get(pixmap.getPixel(x, y));
return pair.floor;
}
public void set(int x, int y, Block block){
pixmap.drawPixel(x, y, ColorMapper.getColor(block));
}
public Block get(int x, int y){
BlockPair pair = ColorMapper.get(pixmap.getPixel(x, y));
return pair.dominant();
}
public boolean solid(int x, int y){
BlockPair pair = ColorMapper.get(pixmap.getPixel(x, y));
return pair.dominant().solid;
}
public boolean has(int x, int y){
return Mathf.inBounds(x, y, width, height);
}
public int pack(int x, int y){
return x + y*width;
}
}

View File

@ -1,90 +0,0 @@
package io.anuke.mindustry.server.mapgen;
import com.badlogic.gdx.ai.pfa.Connection;
import com.badlogic.gdx.ai.pfa.DefaultGraphPath;
import com.badlogic.gdx.ai.pfa.Heuristic;
import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder;
import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.world.Block;
import io.anuke.ucore.util.Geometry;
public class MapPathfinder {
private IndexedGraph<Integer> graph = new MapGraph();
private DefaultGraphPath<Integer> path = new DefaultGraphPath<>();
private IndexedAStarPathFinder<Integer> finder = new IndexedAStarPathFinder<>(graph);
private Heuristic<Integer> heuristic;
private MapImage image;
public Array<GridPoint2> find(int startx, int starty, int endx, int endy, Evaluator eval){
finder.searchNodePath(image.pack(startx, starty), image.pack(endx, endy),
(heuristic = (node, endNode) ->
eval.cost(image.get(node % image.width, node / image.width),
node % image.width,
node / image.width)), path);
Array<GridPoint2> arr = new Array<>();
for(int i : path.nodes){
arr.add(new GridPoint2(i % image.width, i / image.width));
}
return arr;
}
private class MapGraph extends DefaultGraphPath<Integer> implements IndexedGraph<Integer>{
private Array<Connection<Integer>> cons = new Array<>();
@Override
public int getIndex(Integer node) {
return node;
}
@Override
public int getNodeCount() {
return image.width * image.height;
}
@Override
public Array<Connection<Integer>> getConnections(Integer fromNode) {
int x = fromNode % image.width;
int y = fromNode / image.width;
cons.clear();
for(GridPoint2 p : Geometry.d4){
if(image.has(x + p.x, y + p.y)){
cons.add(new MapConnection(fromNode, image.pack(x + p.x, y + p.y)));
}
}
return cons;
}
}
private class MapConnection implements Connection<Integer>{
int from, to;
MapConnection(int from, int to){
this.from = from;
this.to = to;
}
@Override
public float getCost() {
return heuristic.estimate(from, to);
}
@Override
public Integer getFromNode() {
return from;
}
@Override
public Integer getToNode() {
return to;
}
}
interface Evaluator{
int cost(Block block, int x, int y);
}
}

View File

@ -1,14 +0,0 @@
package io.anuke.mindustry.server.mapgen;
import io.anuke.mindustry.io.Map;
import io.anuke.ucore.noise.RidgedPerlin;
import io.anuke.ucore.noise.Simplex;
public class ProcGen {
public RidgedPerlin rid = new RidgedPerlin(1, 1);
public Simplex sim = new Simplex();
public Map generate(GenProperties props){
return null;
}
}