Files
artloglaby/java/org/artisanlogiciel/games/maze/persist/MazePeristWorldEdit.java
philippe lhardy 8211147b28 minetest worledit .we format export
- harcoded generation of lua content of .we export
- export something, but not yet correct ( walls are points ... )
2020-11-03 17:33:25 +01:00

111 lines
3.7 KiB
Java

package org.artisanlogiciel.games.maze.persist;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.LabyModel;
import org.artisanlogiciel.games.minetest.Node;
import org.artisanlogiciel.games.minetest.WorlEditGenerator;
import java.io.*;
public class MazePeristWorldEdit {
private LabyModel model;
private WorlEditGenerator generator;
private static final String GRASS_MATERIAL = "default:dirt_with_grass";
public MazePeristWorldEdit(LabyModel model) {
this.model = model;
}
void addNode(Node node)
{
if (node != null )
{
generator.writeNode(node);
}
}
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 + (int) (pX * w);
int y = oy + (int) (pY * h);
// copied from drawing, where order did matter, might not be the case here...
if ((pY == 0) && LabyModel.isFlagSet(walls, Brick.UP)) {
addNode(new Node(x,y,z,material));
addNode(new Node(x + (int) w,y,z,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(wdrawn, Brick.DOWN)) {
addNode(new Node(x, y + (int) h, z, material));
}
addNode(new Node(x, y, z, 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,"default:dirt_with_grass");
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.");
}
}
}