Basic non-in-game editor functionality

This commit is contained in:
Anuken
2019-05-10 00:01:48 -04:00
parent 69575dbab3
commit d26f533111
9 changed files with 41 additions and 20 deletions

View File

@@ -25,7 +25,7 @@ public class DrawOperation{
public void undo(MapEditor editor){ public void undo(MapEditor editor){
for(int i = array.size - 1; i >= 0; i--){ for(int i = array.size - 1; i >= 0; i--){
long l = array.get(i); long l = array.get(i);
array.set(i, TileOp.value(l, get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l)))); array.set(i, TileOp.get(TileOp.x(l), TileOp.y(l), TileOp.type(l), get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l)); set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l));
} }
} }
@@ -33,7 +33,7 @@ public class DrawOperation{
public void redo(MapEditor editor){ public void redo(MapEditor editor){
for(int i = 0; i < array.size; i++){ for(int i = 0; i < array.size; i++){
long l = array.get(i); long l = array.get(i);
array.set(i, TileOp.value(l, get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l)))); array.set(i, TileOp.get(TileOp.x(l), TileOp.y(l), TileOp.type(l), get(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l))));
set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l)); set(editor, editor.tile(TileOp.x(l), TileOp.y(l)), TileOp.type(l), TileOp.value(l));
} }
} }

View File

@@ -14,7 +14,7 @@ import static io.anuke.mindustry.Vars.ui;
//TODO somehow remove or replace this class with a more flexible solution //TODO somehow remove or replace this class with a more flexible solution
public class EditorTile extends Tile{ public class EditorTile extends Tile{
public EditorTile(int x, int y, short floor, short overlay, short wall){ public EditorTile(int x, int y, int floor, int overlay, int wall){
super(x, y, floor, overlay, wall); super(x, y, floor, overlay, wall);
} }
@@ -93,6 +93,8 @@ public class EditorTile extends Tile{
if(block.hasEntity()){ if(block.hasEntity()){
entity = block.newEntity(); entity = block.newEntity();
entity.tile = this;
entity.block = block;
entity.health = block.health; entity.health = block.health;
entity.cons = new ConsumeModule(entity); entity.cons = new ConsumeModule(entity);
if(block.hasItems) entity.items = new ItemModule(); if(block.hasItems) entity.items = new ItemModule();

View File

@@ -50,11 +50,9 @@ public class MapEditor{
reset(); reset();
loading = true; loading = true;
//TODO redundant and does nothing since tiles are overwritten
createTiles(map.width, map.height);
tags.putAll(map.tags); tags.putAll(map.tags);
//TODO this actually creates the tiles, which are not editor tiles //TODO this actually creates the tiles, which are not editor tiles
MapIO.loadMap(map); MapIO.loadMap(map, EditorTile::new);
checkLinkedTiles(); checkLinkedTiles();
renderer.resize(width(), height()); renderer.resize(width(), height());
loading = false; loading = false;

View File

@@ -12,6 +12,7 @@ import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.game.Version; import io.anuke.mindustry.game.Version;
import io.anuke.mindustry.maps.Map; import io.anuke.mindustry.maps.Map;
import io.anuke.mindustry.world.*; import io.anuke.mindustry.world.*;
import io.anuke.mindustry.world.Tile.TileConstructor;
import io.anuke.mindustry.world.blocks.Floor; import io.anuke.mindustry.world.blocks.Floor;
import java.io.*; import java.io.*;
@@ -60,6 +61,10 @@ public class MapIO{
SaveIO.load(map.file); SaveIO.load(map.file);
} }
public static void loadMap(Map map, TileConstructor cons){
SaveIO.load(map.file, cons);
}
public static Pixmap generatePreview(Map map) throws IOException{ public static Pixmap generatePreview(Map map) throws IOException{
Time.mark(); Time.mark();
Pixmap floors = new Pixmap(map.width, map.height, Format.RGBA8888); Pixmap floors = new Pixmap(map.width, map.height, Format.RGBA8888);

View File

@@ -5,6 +5,7 @@ import io.anuke.arc.collection.ObjectMap.Entry;
import io.anuke.arc.collection.StringMap; import io.anuke.arc.collection.StringMap;
import io.anuke.arc.util.io.CounterInputStream; import io.anuke.arc.util.io.CounterInputStream;
import io.anuke.arc.util.io.ReusableByteOutStream; import io.anuke.arc.util.io.ReusableByteOutStream;
import io.anuke.mindustry.world.Tile.TileConstructor;
import java.io.*; import java.io.*;
@@ -102,7 +103,7 @@ public abstract class SaveFileReader{
return map; return map;
} }
public abstract void read(DataInputStream stream, CounterInputStream counter) throws IOException; public abstract void read(DataInputStream stream, CounterInputStream counter, TileConstructor tiles) throws IOException;
public abstract void write(DataOutputStream stream) throws IOException; public abstract void write(DataOutputStream stream) throws IOException;

View File

@@ -6,6 +6,8 @@ import io.anuke.arc.util.io.CounterInputStream;
import io.anuke.arc.util.io.FastDeflaterOutputStream; import io.anuke.arc.util.io.FastDeflaterOutputStream;
import io.anuke.mindustry.Vars; import io.anuke.mindustry.Vars;
import io.anuke.mindustry.io.versions.Save1; import io.anuke.mindustry.io.versions.Save1;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.Tile.TileConstructor;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
@@ -131,28 +133,33 @@ public class SaveIO{
} }
public static void load(FileHandle file) throws SaveException{ public static void load(FileHandle file) throws SaveException{
load(file, Tile::new);
}
public static void load(FileHandle file, TileConstructor cons) throws SaveException{
try{ try{
//try and load; if any exception at all occurs //try and load; if any exception at all occurs
load(new InflaterInputStream(file.read(bufferSize))); load(new InflaterInputStream(file.read(bufferSize)), cons);
}catch(SaveException e){ }catch(SaveException e){
e.printStackTrace(); e.printStackTrace();
FileHandle backup = file.sibling(file.name() + "-backup." + file.extension()); FileHandle backup = file.sibling(file.name() + "-backup." + file.extension());
if(backup.exists()){ if(backup.exists()){
load(new InflaterInputStream(backup.read(bufferSize))); load(new InflaterInputStream(backup.read(bufferSize)), cons);
}else{ }else{
throw new SaveException(e.getCause()); throw new SaveException(e.getCause());
} }
} }
} }
public static void load(InputStream is) throws SaveException{ /** Loads from a deflated (!) input stream.*/
public static void load(InputStream is, TileConstructor cons) throws SaveException{
try(CounterInputStream counter = new CounterInputStream(is); DataInputStream stream = new DataInputStream(counter)){ try(CounterInputStream counter = new CounterInputStream(is); DataInputStream stream = new DataInputStream(counter)){
logic.reset(); logic.reset();
readHeader(stream); readHeader(stream);
int version = stream.readInt(); int version = stream.readInt();
SaveVersion ver = versions.get(version); SaveVersion ver = versions.get(version);
ver.read(stream, counter); ver.read(stream, counter, cons);
}catch(Exception e){ }catch(Exception e){
throw new SaveException(e); throw new SaveException(e);
}finally{ }finally{

View File

@@ -11,6 +11,7 @@ import io.anuke.mindustry.game.*;
import io.anuke.mindustry.type.ContentType; import io.anuke.mindustry.type.ContentType;
import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.Tile.TileConstructor;
import java.io.*; import java.io.*;
@@ -29,18 +30,20 @@ public abstract class SaveVersion extends SaveFileReader{
return new SaveMeta(map.getInt("version"), map.getLong("saved"), map.getLong("playtime"), map.getInt("build"), map.get("mapname"), map.getInt("wave"), JsonIO.read(Rules.class, map.get("rules", "{}"))); return new SaveMeta(map.getInt("version"), map.getLong("saved"), map.getLong("playtime"), map.getInt("build"), map.get("mapname"), map.getInt("wave"), JsonIO.read(Rules.class, map.get("rules", "{}")));
} }
@Override
public final void write(DataOutputStream stream) throws IOException{ public final void write(DataOutputStream stream) throws IOException{
write(stream, new StringMap()); write(stream, new StringMap());
} }
public final void read(DataInputStream stream, CounterInputStream counter) throws IOException{ @Override
public final void read(DataInputStream stream, CounterInputStream counter, TileConstructor tiles) throws IOException{
region("meta", stream, counter, this::readMeta); region("meta", stream, counter, this::readMeta);
region("content", stream, counter, this::readContentHeader); region("content", stream, counter, this::readContentHeader);
region("map", stream, counter, this::readMap); region("map", stream, counter, in -> readMap(in, tiles));
region("entities", stream, counter, this::readEntities); region("entities", stream, counter, this::readEntities);
} }
public void write(DataOutputStream stream, StringMap extraTags) throws IOException{ public final void write(DataOutputStream stream, StringMap extraTags) throws IOException{
region("meta", stream, out -> writeMeta(out, extraTags)); region("meta", stream, out -> writeMeta(out, extraTags));
region("content", stream, this::writeContentHeader); region("content", stream, this::writeContentHeader);
region("map", stream, this::writeMap); region("map", stream, this::writeMap);
@@ -127,7 +130,7 @@ public abstract class SaveVersion extends SaveFileReader{
} }
} }
public void readMap(DataInput stream) throws IOException{ public void readMap(DataInput stream, TileConstructor constructor) throws IOException{
int width = stream.readUnsignedShort(); int width = stream.readUnsignedShort();
int height = stream.readUnsignedShort(); int height = stream.readUnsignedShort();
@@ -144,11 +147,11 @@ public abstract class SaveVersion extends SaveFileReader{
short oreid = stream.readShort(); short oreid = stream.readShort();
int consecutives = stream.readUnsignedByte(); int consecutives = stream.readUnsignedByte();
tiles[x][y] = new Tile(x, y, floorid, oreid, (short)0); tiles[x][y] = constructor.construct(x, y, floorid, oreid, (short)0);
for(int j = i + 1; j < i + 1 + consecutives; j++){ for(int j = i + 1; j < i + 1 + consecutives; j++){
int newx = j % width, newy = j / width; int newx = j % width, newy = j / width;
tiles[newx][newy] = new Tile(newx, newy, floorid, oreid, (short)0); tiles[newx][newy] = constructor.construct(newx, newy, floorid, oreid, (short)0);
} }
i += consecutives; i += consecutives;

View File

@@ -8,6 +8,7 @@ import io.anuke.mindustry.game.Version;
import io.anuke.mindustry.gen.Serialization; import io.anuke.mindustry.gen.Serialization;
import io.anuke.mindustry.io.SaveIO; import io.anuke.mindustry.io.SaveIO;
import io.anuke.mindustry.maps.Map; import io.anuke.mindustry.maps.Map;
import io.anuke.mindustry.world.Tile;
import java.io.*; import java.io.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@@ -52,7 +53,7 @@ public class NetworkIO{
player.resetID(id); player.resetID(id);
player.add(); player.add();
SaveIO.getSaveWriter().readMap(stream); SaveIO.getSaveWriter().readMap(stream, Tile::new);
}catch(IOException e){ }catch(IOException e){
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -36,12 +36,12 @@ public class Tile implements Position, TargetTrait{
block = floor = (Floor)Blocks.air; block = floor = (Floor)Blocks.air;
} }
public Tile(int x, int y, short floor, short overlay, short wall){ public Tile(int x, int y, int floor, int overlay, int wall){
this.x = (short)x; this.x = (short)x;
this.y = (short)y; this.y = (short)y;
this.floor = (Floor)content.block(floor); this.floor = (Floor)content.block(floor);
this.block = content.block(wall); this.block = content.block(wall);
this.overlay = overlay; this.overlay = (short)overlay;
//update entity and create it if needed //update entity and create it if needed
changed(); changed();
@@ -454,4 +454,8 @@ public class Tile implements Position, TargetTrait{
public String toString(){ public String toString(){
return floor.name + ":" + block.name + ":" + content.block(overlay) + "[" + x + "," + y + "] " + "entity=" + (entity == null ? "null" : (entity.getClass())); return floor.name + ":" + block.name + ":" + content.block(overlay) + "[" + x + "," + y + "] " + "entity=" + (entity == null ? "null" : (entity.getClass()));
} }
public interface TileConstructor{
Tile construct(int x, int y, int floor, int overlay, int wall);
}
} }