package org.artisanlogiciel.games.maze.persist; import org.artisanlogiciel.games.maze.Brick; import org.artisanlogiciel.games.maze.LabyModel; import org.artisanlogiciel.games.minetest.Material; import org.artisanlogiciel.games.minetest.Node; import org.artisanlogiciel.games.minetest.WorlEditGenerator; import java.io.*; import static org.artisanlogiciel.games.minetest.Material.GRASS_MATERIAL; public class MazePersistWorldEdit { private LabyModel model; private WorlEditGenerator generator; public MazePersistWorldEdit(LabyModel model) { this.model = model; } void addNode(Node node) { if (node != null ) { generator.writeNode(node); } } private Node newNode(int x, int y, String material) { // x,y,z => x,z,y in minetest return new Node(x,0, y, Material.getMaterialByName(material)); } void addWalls(int pX, int pY) { short walls = model.getWalls(pX, pY); short wdrawn = 0; // todo int w = 2; int h = 2; int ox = 0; int oy = 0; int z = 0; String material = GRASS_MATERIAL; int x = ox + (pX * w); int y = oy + (pY * h); // copied from drawing, where order did matter, might not be the case here... //if ((pY == 0) && LabyModel.isFlagSet(walls, Brick.UP)) { if (LabyModel.isFlagSet(walls, Brick.UP)) { for ( int dx = 0; dx < w; dx++) { addNode(newNode(x + dx, y,material)); } wdrawn |= Brick.UP; } /* if (LabyModel.isFlagSet(walls, Brick.RIGHT)) { if (!LabyModel.isFlagSet(wdrawn, Brick.UP)) { addNode(new Node(x + (int) w, y, 0, material)); } addNode(new Node(x + (int) w, y + (int) h, z, material)); wdrawn |= Brick.RIGHT; } if (LabyModel.isFlagSet(walls, Brick.DOWN)) { if (!LabyModel.isFlagSet(wdrawn, Brick.RIGHT)) { addNode(new Node(x + (int) w, y + (int) h, z, material)); } addNode(new Node(x, y + (int) h, z, material)); wdrawn |= Brick.DOWN; } */ //if ((pX == 0) && LabyModel.isFlagSet(walls, Brick.LEFT)) { if (LabyModel.isFlagSet(walls, Brick.LEFT)) { for (int dy = 0; dy < h; dy ++) { addNode(newNode(x, y + dy, material)); } wdrawn |= Brick.LEFT; } } public void streamOut(String pFormat, OutputStream pOut) throws IOException { // WIP using WorldEditGenerator if ((pFormat == null) || (pFormat.equals("we"))) { Node refNode = new Node(0,0,0); StringBuilder builder = new StringBuilder(); generator = new WorlEditGenerator(builder,refNode); generator.writeStart(); // first raw format, not smart. DataOutputStream dataOut = new DataOutputStream(pOut); dataOut.flush(); for (int y = 0; y < model.getHeight(); y++) { for (int x = 0; x < model.getWidth(); x++) { addWalls(x,y); } } generator.writeEnd(); dataOut.write(builder.toString().getBytes()); dataOut.flush(); } else { throw new IOException("Format " + pFormat + " Not yet implemented."); } } public LabyModel parseInputStream(String pFormat, InputStream pIn) throws IOException { // TODO using WorldEditGenerator if ((pFormat == null) || (pFormat.equals("we"))) { // DataInputStream in = new DataInputStream(pIn); throw new IOException("Format " + pFormat + " Not yet implemented."); // should be at end of stream ? Not necessary can stream multiple // labs ( or tiling ). } else { throw new IOException("Format " + pFormat + " Not yet implemented."); } } }