read a .we minetest schema

- use ground has maze walls
This commit is contained in:
philippe lhardy
2020-11-04 22:12:17 +01:00
parent 6402766371
commit 52a2d3f1e3
13 changed files with 430 additions and 16 deletions

View File

@@ -19,6 +19,6 @@ public class Material {
@Override
public String toString() {
return "name";
return name;
}
}

View File

@@ -1,5 +1,7 @@
package org.artisanlogiciel.games.minetest;
import java.util.HashMap;
public class Node {
int x;
int y;
@@ -20,6 +22,16 @@ public class Node {
this.material = Material.DEFAULT;
}
public Node(HashMap<String, Object> map)
throws ClassCastException, NullPointerException
{
// FIXME WARNING HACK reverse x and z ...
x = ((Integer) map.get("x")).intValue();
y = ((Integer) map.get("z")).intValue();
z = ((Integer) map.get("y")).intValue();
material = Material.getMaterialByName( (String) map.get("name"));
}
public int getX() {
return x;
}
@@ -35,4 +47,14 @@ public class Node {
public Material getMaterial() {
return material;
}
@Override
public String toString() {
return "Node{" +
"x=" + x +
", y=" + y +
", z=" + z +
", material=" + material +
'}';
}
}

View File

@@ -0,0 +1,71 @@
package org.artisanlogiciel.games.minetest;
public class Range {
int min;
int max;
public int getMin() {
return min;
}
public int getMax() {
return max;
}
public Range()
{
// invalid range, means empty interval
min = 0;
max = -1;
}
public boolean isEmpty()
{
return ( min > max );
}
boolean union(Range other)
{
boolean update = false;
if ( ! other.isEmpty()) {
update = updateBounds(other.getMin()) | updateBounds(other.getMax());
}
return update;
}
boolean updateBounds(int v)
{
boolean update = false;
// special case where previous range was unset ( min > max ).
if ( isEmpty() )
{
min = v;
max = v;
return true;
}
if ( v < min )
{
update = true;
min = v;
}
if ( v > max )
{
update = true;
max = v;
}
return update;
}
public int getRangeSize() {
if (isEmpty())
{
return 0;
}
else
{
return max - min;
}
}
}

View File

@@ -0,0 +1,25 @@
package org.artisanlogiciel.games.minetest;
import java.util.HashMap;
public class Raw
extends Range {
// y is index
public HashMap<Integer,Node> nodes;
public Raw() {
super();
nodes = new HashMap<>();
}
public void addNode(Integer y, Node node)
{
updateBounds(y.intValue());
nodes.put(y,node);
}
public Node getNode( int y)
{
return nodes.get(new Integer(y));
}
}

View File

@@ -0,0 +1,50 @@
package org.artisanlogiciel.games.minetest;
import java.util.HashMap;
public class Slice
extends Range
{
// x is index
HashMap<Integer, Raw> raws;
public Range getRawRange() {
return rawRange;
}
Range rawRange;
public Slice() {
super();
raws = new HashMap<>();
rawRange = new Range();
}
public void addNodeInRaw(Integer x, Node node) {
updateBounds(x.intValue());
Raw r = raws.get(x);
if ( r == null )
{
r = new Raw();
raws.put(x,r);
}
r.addNode(new Integer(node.getY()), node);
rawRange.union(r);
}
public Node getNode(int x, int y)
{
Raw r = raws.get(x);
if ( r == null )
{
return null;
}
return r.getNode(y);
}
public Raw getRaw(Integer x)
{
return raws.get(x);
}
}

View File

@@ -1,5 +1,8 @@
package org.artisanlogiciel.games.minetest;
/**
* generate directly lua maps ( no intermediate LuaObject ).
*/
public class WorlEditGenerator
{

View File

@@ -0,0 +1,79 @@
package org.artisanlogiciel.games.minetest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class World
extends Range
{
List<Node> nodes;
// z range
HashMap<Integer,Slice> zSlices;
public World()
{
super();
nodes = new ArrayList<>();
zSlices = new HashMap<>();
}
public void addNode(Node node)
{
nodes.add(node);
addNodeInSlice(new Integer(node.getZ()),node);
}
private void addNodeInSlice(Integer z, Node node) {
updateBounds(z.intValue());
Slice s = zSlices.get(z);
Integer x = new Integer(node.getX());
if ( s == null )
{
s = new Slice();
zSlices.put(z,s);
}
s.addNodeInRaw(x,node);
}
public void addList(List<Object> objectList)
{
for (Object o : objectList)
{
if ( o instanceof HashMap )
{
HashMap<String,Object> map = (HashMap<String,Object>) o;
Node node = new Node(map);
addNode(node);
}
}
}
public Node getNode(int x, int y, int z)
{
Slice s = zSlices.get(new Integer(z));
if ( s == null )
{
return null;
}
else
{
return s.getNode(x,y);
}
}
public Slice getSlice(int z)
{
return zSlices.get(new Integer(z));
}
@Override
public String toString() {
return "World{" +
"nodes=" + nodes +
'}';
}
}

View File

@@ -0,0 +1,51 @@
package org.artisanlogiciel.games.minetest;
import org.artisanlogiciel.lua.CharProvider;
import org.artisanlogiciel.lua.LuaObject;
import org.artisanlogiciel.lua.Parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class WorldEditReader {
InputStream input;
public WorldEditReader(InputStream pIn)
{
input = pIn;
}
public World read()
throws IOException
{
World world = new World();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
byte b[] = new byte[128*1024];
input.read(b);
String contents = new String(b);
in.close();
System.out.println(contents);
CharProvider reader = new CharProvider(contents);
Parser parser = new Parser(reader);
LuaObject result = parser.parse();
if (result != null) {
System.out.println(result.toString());
Object we = result.wrapToJava();
System.out.println(we);
world.addList( (List<Object>) we);
System.out.println(world);
} else {
System.err.println("result null");
}
return world;
}
}