Made save format support removed blocks

This commit is contained in:
Anuken
2020-06-04 14:50:08 -04:00
parent f112226178
commit 803f9e8eb6
12 changed files with 103 additions and 13 deletions

View File

@@ -393,6 +393,22 @@ public class Schematics implements Loadable{
});
}
public static void place(Schematic schem, int x, int y, Team team){
int ox = x - schem.width/2, oy = y - schem.height/2;
schem.tiles.each(st -> {
Tile tile = world.tile(st.x + ox, st.y + oy);
if(tile == null) return;
tile.setBlock(st.block, team, 0);
tile.rotation(st.rotation);
Object config = st.config;
if(tile.entity != null){
tile.entity.configureAny(config);
}
});
}
//region IO methods
/** Loads a schematic from base64. May throw an exception. */

View File

@@ -88,8 +88,8 @@ public abstract class SaveFileReader{
}
/** Reads a chunk of some length. Use the runner for reading to catch more descriptive errors. */
public int readChunk(DataInput input, boolean isByte, IORunner<DataInput> runner) throws IOException{
int length = isByte ? input.readUnsignedShort() : input.readInt();
public int readChunk(DataInput input, boolean isShort, IORunner<DataInput> runner) throws IOException{
int length = isShort ? input.readUnsignedShort() : input.readInt();
runner.accept(input);
return length;
}

View File

@@ -152,6 +152,9 @@ public abstract class SaveVersion extends SaveFileReader{
Tile tile = world.rawTile(i % world.width(), i / world.width());
stream.writeShort(tile.blockID());
//make note of whether there was an entity here
stream.writeBoolean(tile.entity != null);
//only write the entity for multiblocks once - in the center
if(tile.entity != null){
if(tile.isCenter()){
@@ -218,8 +221,9 @@ public abstract class SaveVersion extends SaveFileReader{
Tile tile = context.tile(i);
if(block == null) block = Blocks.air;
boolean isCenter = true;
boolean hadEntity = stream.readBoolean();
if(block.hasEntity()){
if(hadEntity){
isCenter = stream.readBoolean();
}
@@ -228,15 +232,20 @@ public abstract class SaveVersion extends SaveFileReader{
tile.setBlock(block);
}
if(block.hasEntity()){
if(hadEntity){
if(isCenter){ //only read entity for center blocks
try{
readChunk(stream, true, in -> {
byte revision = in.readByte();
tile.entity.readAll(Reads.get(in), revision);
});
}catch(Throwable e){
throw new IOException("Failed to read tile entity of block: " + block, e);
if(block.hasEntity()){
try{
readChunk(stream, true, in -> {
byte revision = in.readByte();
tile.entity.readAll(Reads.get(in), revision);
});
}catch(Throwable e){
throw new IOException("Failed to read tile entity of block: " + block, e);
}
}else{
//skip the entity region, as the entity and its IO code are now gone
skipRegion(stream, true);
}
}
}else{

View File

@@ -1,13 +1,78 @@
package mindustry.maps.generators;
import arc.*;
import arc.math.*;
import arc.struct.*;
import arc.util.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.game.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.defense.turrets.*;
import mindustry.world.blocks.defense.turrets.ItemTurret.*;
import mindustry.world.blocks.environment.*;
import java.io.*;
public class BaseGenerator{
static Array<Schematic> schematics = new Array<>();
public void generate(Tiles tiles, Array<Tile> cores, Tile spawn, Team team, Sector sector){
for(Tile tile : cores){
tile.clearOverlay();
tile.setBlock(Blocks.coreShard, team);
}
//TODO remove this, it's just a test
if(!Core.files.external("SCHEMATICOUTPUT").exists()){
Log.err("no schematics");
return;
}
if(schematics.isEmpty()){
schematics.addAll(Array.with(Core.files.external("SCHEMATICOUTPUT").list()).map(s -> {
try{
return Schematics.read(s);
}catch(IOException e){
throw new RuntimeException();
}
}));
}
Vars.state.rules.enemyCheat = true;
int range = 180;
int attempts = 3000;
int cx = cores.first().x, cy = cores.first().y;
outer:
for(int i = 0; i < attempts; i++){
Tmp.v1.rnd(Mathf.random(range));
int x = (int)(cx + Tmp.v1.x), y = (int)(cy + Tmp.v1.y);
Schematic res = schematics.random();
int ex = x - res.width/2, ey = y - res.height/2;
for(int rx = ex; rx <= ex + res.width; rx++){
for(int ry = ey; ry <= ey + res.height; ry++){
Tile tile = tiles.get(rx, ry);
if(tile == null || Vars.world.getDarkness(rx, ry) > 0 || tile.floor().isDeep() || (!tile.block().isAir() && !(tile.block() instanceof Rock && tile.block().destructible))) continue outer;
}
}
Schematics.place(res, x, y, team);
//add ammo
for(int rx = ex; rx <= ex + res.width; rx++){
for(int ry = ey; ry <= ey + res.height; ry++){
Tile tile = tiles.get(rx, ry);
if(tile != null && tile.entity instanceof ItemTurretEntity){
tile.entity.handleItem(tile.entity, ((ItemTurret)tile.block()).ammoTypes.keys().toArray().first());
}
}
}
}
}
}