mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-02-11 11:17:11 +07:00
Editor generation cleanup
This commit is contained in:
parent
4f21ea7799
commit
514e2503fa
@ -45,9 +45,8 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
private AsyncExecutor executor = new AsyncExecutor(1);
|
||||
private AsyncResult<Void> result;
|
||||
boolean generating;
|
||||
private GenTile returnTile = new GenTile();
|
||||
|
||||
private GenTile[][] buffer1, buffer2;
|
||||
private long[] buffer1, buffer2;
|
||||
private Cons<Seq<GenerateFilter>> applier;
|
||||
CachedTile ctile = new CachedTile(){
|
||||
//nothing.
|
||||
@ -108,42 +107,36 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
/** Applies the specified filters to the editor. */
|
||||
public void applyToEditor(Seq<GenerateFilter> filters){
|
||||
//writeback buffer
|
||||
GenTile[][] writeTiles = new GenTile[editor.width()][editor.height()];
|
||||
|
||||
for(int x = 0; x < editor.width(); x++){
|
||||
for(int y = 0; y < editor.height(); y++){
|
||||
writeTiles[x][y] = new GenTile();
|
||||
}
|
||||
}
|
||||
long[] writeTiles = new long[editor.width() * editor.height()];
|
||||
|
||||
for(GenerateFilter filter : filters){
|
||||
input.begin(filter, editor.width(), editor.height(), editor::tile);
|
||||
|
||||
//write to buffer
|
||||
for(int x = 0; x < editor.width(); x++){
|
||||
for(int y = 0; y < editor.height(); y++){
|
||||
Tile tile = editor.tile(x, y);
|
||||
input.apply(x, y, tile.block(), tile.floor(), tile.overlay());
|
||||
filter.apply(input);
|
||||
writeTiles[x][y].set(input.floor, input.block, input.overlay, tile.team());
|
||||
writeTiles[x + y*world.width()] = PackTile.get(input.block.id, input.floor.id, input.overlay.id);
|
||||
}
|
||||
}
|
||||
|
||||
editor.load(() -> {
|
||||
//read from buffer back into tiles
|
||||
for(int x = 0; x < editor.width(); x++){
|
||||
for(int y = 0; y < editor.height(); y++){
|
||||
Tile tile = editor.tile(x, y);
|
||||
GenTile write = writeTiles[x][y];
|
||||
for(int i = 0; i < editor.width() * editor.height(); i++){
|
||||
Tile tile = world.tiles.geti(i);
|
||||
long write = writeTiles[i];
|
||||
|
||||
Block block = content.block(PackTile.block(write)), floor = content.block(PackTile.floor(write)), overlay = content.block(PackTile.overlay(write));
|
||||
|
||||
//don't mess up synthetic stuff.
|
||||
if(!tile.synthetic() && !content.block(write.block).synthetic()){
|
||||
tile.setBlock(content.block(write.block));
|
||||
if(!tile.synthetic() && !block.synthetic()){
|
||||
tile.setBlock(block);
|
||||
}
|
||||
|
||||
tile.setFloor((Floor)content.block(write.floor));
|
||||
tile.setTeam(Team.get(write.team));
|
||||
tile.setOverlay(content.block(write.ore));
|
||||
}
|
||||
tile.setFloor((Floor)floor);
|
||||
tile.setOverlay(overlay);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -206,15 +199,8 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
rebuildFilters();
|
||||
}
|
||||
|
||||
GenTile[][] create(){
|
||||
GenTile[][] out = new GenTile[editor.width() / scaling][editor.height() / scaling];
|
||||
|
||||
for(int x = 0; x < out.length; x++){
|
||||
for(int y = 0; y < out[0].length; y++){
|
||||
out[x][y] = new GenTile();
|
||||
}
|
||||
}
|
||||
return out;
|
||||
long[] create(){
|
||||
return new long[(editor.width() / scaling) * (editor.height() / scaling)];
|
||||
}
|
||||
|
||||
void rebuildFilters(){
|
||||
@ -322,9 +308,15 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
selection.show();
|
||||
}
|
||||
|
||||
GenTile dset(Tile tile){
|
||||
returnTile.set(tile);
|
||||
return returnTile;
|
||||
long pack(Tile tile){
|
||||
return PackTile.get(tile.blockID(), tile.floorID(), tile.overlayID());
|
||||
}
|
||||
|
||||
Tile unpack(long tile){
|
||||
ctile.setFloor((Floor)content.block(PackTile.floor(tile)));
|
||||
ctile.setBlock(content.block(PackTile.block(tile)));
|
||||
ctile.setOverlay(content.block(PackTile.overlay(tile)));
|
||||
return ctile;
|
||||
}
|
||||
|
||||
void apply(){
|
||||
@ -355,6 +347,7 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
|
||||
result = executor.submit(() -> {
|
||||
try{
|
||||
int w = pixmap.getWidth();
|
||||
world.setGenerating(true);
|
||||
generating = true;
|
||||
|
||||
@ -362,24 +355,24 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
//write to buffer1 for reading
|
||||
for(int px = 0; px < pixmap.getWidth(); px++){
|
||||
for(int py = 0; py < pixmap.getHeight(); py++){
|
||||
buffer1[px][py].set(editor.tile(px * scaling, py * scaling));
|
||||
buffer1[px + py*w] = pack(editor.tile(px * scaling, py * scaling));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(GenerateFilter filter : copy){
|
||||
input.begin(filter, editor.width(), editor.height(), (x, y) -> buffer1[Mathf.clamp(x / scaling, 0, pixmap.getWidth()-1)][Mathf.clamp(y / scaling, 0, pixmap.getHeight()-1)].tile());
|
||||
input.begin(filter, editor.width(), editor.height(), (x, y) -> unpack(buffer1[Mathf.clamp(x / scaling, 0, pixmap.getWidth()-1) + w* Mathf.clamp(y / scaling, 0, pixmap.getHeight()-1)]));
|
||||
|
||||
//read from buffer1 and write to buffer2
|
||||
pixmap.each((px, py) -> {
|
||||
int x = px * scaling, y = py * scaling;
|
||||
GenTile tile = buffer1[px][py];
|
||||
input.apply(x, y, content.block(tile.block), content.block(tile.floor), content.block(tile.ore));
|
||||
long tile = buffer1[px + py * w];
|
||||
input.apply(x, y, content.block(PackTile.block(tile)), content.block(PackTile.floor(tile)), content.block(PackTile.overlay(tile)));
|
||||
filter.apply(input);
|
||||
buffer2[px][py].set(input.floor, input.block, input.overlay, Team.get(tile.team));
|
||||
buffer2[px + py * w] = PackTile.get(input.block.id, input.floor.id, input.overlay.id);
|
||||
});
|
||||
|
||||
pixmap.each((px, py) -> buffer1[px][py].set(buffer2[px][py]));
|
||||
pixmap.each((px, py) -> buffer1[px + py*w] = buffer2[px + py*w]);
|
||||
}
|
||||
|
||||
for(int px = 0; px < pixmap.getWidth(); px++){
|
||||
@ -388,10 +381,10 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
//get result from buffer1 if there's filters left, otherwise get from editor directly
|
||||
if(filters.isEmpty()){
|
||||
Tile tile = editor.tile(px * scaling, py * scaling);
|
||||
color = MapIO.colorFor(tile.floor(), tile.block(), tile.overlay(), Team.derelict);
|
||||
color = MapIO.colorFor(tile.block(), tile.floor(), tile.overlay(), Team.derelict);
|
||||
}else{
|
||||
GenTile tile = buffer1[px][py];
|
||||
color = MapIO.colorFor(content.block(tile.floor), content.block(tile.block), content.block(tile.ore), Team.derelict);
|
||||
long tile = buffer1[px + py*w];
|
||||
color = MapIO.colorFor(content.block(PackTile.block(tile)), content.block(PackTile.floor(tile)), content.block(PackTile.overlay(tile)), Team.derelict);
|
||||
}
|
||||
pixmap.draw(px, pixmap.getHeight() - 1 - py, color);
|
||||
}
|
||||
@ -411,39 +404,4 @@ public class MapGenerateDialog extends BaseDialog{
|
||||
world.setGenerating(false);
|
||||
});
|
||||
}
|
||||
|
||||
private class GenTile{
|
||||
public byte team;
|
||||
public short block, floor, ore;
|
||||
|
||||
GenTile(){
|
||||
}
|
||||
|
||||
public void set(Block floor, Block wall, Block ore, Team team){
|
||||
this.floor = floor.id;
|
||||
this.block = wall.id;
|
||||
this.ore = (!floor.asFloor().hasSurface() && ore.asFloor().needsSurface) ? 0 : ore.id;
|
||||
this.team = (byte)team.id;
|
||||
}
|
||||
|
||||
public void set(GenTile other){
|
||||
this.floor = other.floor;
|
||||
this.block = other.block;
|
||||
this.ore = other.ore;
|
||||
this.team = other.team;
|
||||
}
|
||||
|
||||
public GenTile set(Tile other){
|
||||
set(other.floor(), other.block(), other.overlay(), other.team());
|
||||
return this;
|
||||
}
|
||||
|
||||
Tile tile(){
|
||||
ctile.setFloor((Floor)content.block(floor));
|
||||
ctile.setBlock(content.block(block));
|
||||
ctile.setOverlay(content.block(ore));
|
||||
ctile.setTeam(Team.get(team));
|
||||
return ctile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ public class MinimapRenderer implements Disposable{
|
||||
private int colorFor(Tile tile){
|
||||
if(tile == null) return 0;
|
||||
int bc = tile.block().minimapColor(tile);
|
||||
Color color = Tmp.c1.set(bc == 0 ? MapIO.colorFor(tile.floor(), tile.block(), tile.overlay(), tile.team()) : bc);
|
||||
Color color = Tmp.c1.set(bc == 0 ? MapIO.colorFor(tile.block(), tile.floor(), tile.overlay(), tile.team()) : bc);
|
||||
color.mul(1f - Mathf.clamp(world.getDarkness(tile.x, tile.y) / 4f));
|
||||
|
||||
return color.rgba();
|
||||
|
@ -80,7 +80,7 @@ public class MapIO{
|
||||
@Override
|
||||
public void setBlock(Block type){
|
||||
super.setBlock(type);
|
||||
int c = colorFor(Blocks.air, block(), Blocks.air, team());
|
||||
int c = colorFor(block(), Blocks.air, Blocks.air, team());
|
||||
if(c != black){
|
||||
walls.draw(x, floors.getHeight() - 1 - y, c);
|
||||
floors.draw(x, floors.getHeight() - 1 - y + 1, shade);
|
||||
@ -119,7 +119,7 @@ public class MapIO{
|
||||
if(overlayID != 0){
|
||||
floors.draw(x, floors.getHeight() - 1 - y, colorFor(Blocks.air, Blocks.air, content.block(overlayID), Team.derelict));
|
||||
}else{
|
||||
floors.draw(x, floors.getHeight() - 1 - y, colorFor(content.block(floorID), Blocks.air, Blocks.air, Team.derelict));
|
||||
floors.draw(x, floors.getHeight() - 1 - y, colorFor(Blocks.air, content.block(floorID), Blocks.air, Team.derelict));
|
||||
}
|
||||
if(content.block(overlayID) == Blocks.spawn){
|
||||
map.spawns ++;
|
||||
@ -141,17 +141,17 @@ public class MapIO{
|
||||
for(int x = 0; x < pixmap.getWidth(); x++){
|
||||
for(int y = 0; y < pixmap.getHeight(); y++){
|
||||
Tile tile = tiles.getn(x, y);
|
||||
pixmap.draw(x, pixmap.getHeight() - 1 - y, colorFor(tile.floor(), tile.block(), tile.overlay(), tile.team()));
|
||||
pixmap.draw(x, pixmap.getHeight() - 1 - y, colorFor(tile.block(), tile.floor(), tile.overlay(), tile.team()));
|
||||
}
|
||||
}
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
public static int colorFor(Block floor, Block wall, Block ore, Team team){
|
||||
public static int colorFor(Block wall, Block floor, Block overlay, Team team){
|
||||
if(wall.synthetic()){
|
||||
return team.color.rgba();
|
||||
}
|
||||
return (wall.solid ? wall.mapColor : !ore.useColor ? floor.mapColor : ore.mapColor).rgba();
|
||||
return (wall.solid ? wall.mapColor : !overlay.useColor ? floor.mapColor : overlay.mapColor).rgba();
|
||||
}
|
||||
|
||||
public static Pixmap writeImage(Tiles tiles){
|
||||
|
@ -26,14 +26,14 @@ public abstract class GenerateFilter{
|
||||
//save to buffer
|
||||
for(int i = 0; i < tiles.width * tiles.height; i++){
|
||||
Tile tile = tiles.geti(i);
|
||||
buffer[i] = TileBuffer.get(tile.blockID(), tile.floorID(), tile.overlayID());
|
||||
buffer[i] = PackTile.get(tile.blockID(), tile.floorID(), tile.overlayID());
|
||||
}
|
||||
|
||||
for(int i = 0; i < tiles.width * tiles.height; i++){
|
||||
Tile tile = tiles.geti(i);
|
||||
long b = buffer[i];
|
||||
|
||||
in.apply(tile.x, tile.y, Vars.content.block(TileBuffer.block(b)), Vars.content.block(TileBuffer.floor(b)), Vars.content.block(TileBuffer.overlay(b)));
|
||||
in.apply(tile.x, tile.y, Vars.content.block(PackTile.block(b)), Vars.content.block(PackTile.floor(b)), Vars.content.block(PackTile.overlay(b)));
|
||||
apply();
|
||||
|
||||
tile.setFloor(in.floor.asFloor());
|
||||
@ -151,7 +151,7 @@ public abstract class GenerateFilter{
|
||||
}
|
||||
|
||||
@Struct
|
||||
class TileBufferStruct{
|
||||
class PackTileStruct{
|
||||
short block, floor, overlay;
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class NoiseFilter extends GenerateFilter{
|
||||
new SliderOption("octaves", () -> octaves, f -> octaves = f, 1f, 10f),
|
||||
new SliderOption("falloff", () -> falloff, f -> falloff = f, 0f, 1f),
|
||||
new BlockOption("target", () -> target, b -> target = b, anyOptional),
|
||||
new BlockOption("floor", () -> floor, b -> floor = b, floorsOnly),
|
||||
new BlockOption("floor", () -> floor, b -> floor = b, floorsOptional),
|
||||
new BlockOption("wall", () -> block, b -> block = b, wallsOptional)
|
||||
);
|
||||
}
|
||||
@ -29,8 +29,8 @@ public class NoiseFilter extends GenerateFilter{
|
||||
float noise = noise(in.x, in.y, scl, 1f, octaves, falloff);
|
||||
|
||||
if(noise > threshold && (target == Blocks.air || in.floor == target || in.block == target)){
|
||||
in.floor = floor;
|
||||
if(block != Blocks.air) in.block = block;
|
||||
if(floor != Blocks.air) in.floor = floor;
|
||||
if(block != Blocks.air && in.block != Blocks.air) in.block = block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user