Dungeon Generation ( Code inside )

I was trying to understand how dungeon generators work and i come across this code (under Mit license) :



Coke and Code Dungeon Generator



Here are the results :









I post the jme render code.

[java]

public class DungeonMap extends SceneControl

{

public static void main(String[] args)

{

Scene scene = Scene.getScene();

scene.add(new Spotlight(false));

scene.add(new DungeonMap());

scene.start();

}



@Override

public void onInit()

{

Scene scene = Scene.getScene();

scene.getPlayer().setCanLevitate(true);



//code and coke map import.

Map map = new Map(System.currentTimeMillis(), (int)scene.getWorldBounds().x, (int)scene.getWorldBounds().y, 30, 300, 6, 6, 10, 10, 5000, true);

map.generate();



Vector3f playerStartLocation = Vector3f.POSITIVE_INFINITY;

for (int i = 0; i < map.getWidth(); i++)

{

for (int j = 0; j < map.getHeight(); j++)

{

if (!(map.getTile(i,j)==0) && (i <= playerStartLocation.x) && (j <= playerStartLocation.z)) playerStartLocation = new Vector3f(i+0.5f, 0, j+0.5f);

}

}

scene.getPlayer().getNode().move(playerStartLocation);



Material emptyTile = null;//assetManager.loadMaterial(“Materials/Lava.j3m”);

Material fullTile = scene.getAssetManager().loadMaterial(“Materials/CobbleStoneGround.j3m”);

Material wall = scene.getAssetManager().loadMaterial(“Materials/CobbleStoneGround.j3m”);



scene.getCollidables().attachChild( createFloor(map, fullTile, false, emptyTile, true, wall, 0.0f, 1) );

}



public Node createFloor(Map floor, Material fullTile, boolean displayEmptyTiles, Material emptyTile, boolean createWalls, Material wallMaterial, float wallWidth, float wallHeight)

{

Node ground = new Node(“Ground”);

Box wall = CreationUtilities.createLowerLeftBox(1f, wallHeight, wallWidth);



for (int x = 0; x < floor.getWidth(); x++)

{

for (int y = 0; y < floor.getHeight(); y++)

{

if (displayEmptyTiles || (!displayEmptyTiles && !(floor.getTile(x,y)==0)))//if ! empty

{

Geometry tile = CreationUtilities.createHorizontalQuad(x, 0, y, 1, 1);

//tile.setShadowMode(ShadowMode.Receive);

if (floor.getTile(x, y)==0) tile.setMaterial(emptyTile);

else tile.setMaterial(fullTile);



ground.attachChild(tile);

}



if (createWalls)

{

//Left wall

if (!(floor.getTile(x, y)==0) && ((y == 0) || (floor.getTile(x,y - 1)==0)))

{

Geometry leftWall = new Geometry(“Left Wall (” + x + “,” + y + “)”, wall);

leftWall.setMaterial(wallMaterial);

leftWall.move(x, 0, y - wallWidth);

leftWall.setShadowMode(ShadowMode.Cast);

ground.attachChild(leftWall);

}



//South wall

if (!(floor.getTile(x, y)==0) && ((x == 0) || (floor.getTile(x - 1,y)==0)))

{

Geometry southWall = new Geometry(“South Wall (” + x + “,” + y + “)”, wall);

southWall.setMaterial(wallMaterial);

southWall.rotate(0, FastMath.HALF_PI, 0);

southWall.move(x - wallWidth, 0, y + 1);

southWall.setShadowMode(ShadowMode.Cast);

ground.attachChild(southWall);

}



//Right wall

if (!(floor.getTile(x, y)==0) && ((y + 1 >= floor.getHeight()) || (floor.getTile(x, y + 1)==0)))

{

Geometry rightWall = new Geometry(“Right Wall (” + x + “,” + y + “)”, wall);

rightWall.setMaterial(wallMaterial);

rightWall.move(x, 0, y + 1);

rightWall.setShadowMode(ShadowMode.Cast);

ground.attachChild(rightWall);

}



//Top wall

if (!(floor.getTile(x, y)==0) && ((x + 1 >= floor.getWidth()) || (floor.getTile(x+1, y)==0)))

{

Geometry topWall = new Geometry(“Top Wall (” + x + “,” + y + “)”, wall);

topWall.setMaterial(wallMaterial);

topWall.rotate(0, FastMath.HALF_PI, 0);

topWall.move(x + 1, 0, y + 1);

topWall.setShadowMode(ShadowMode.Cast);

ground.attachChild(topWall);

}

}//if

}//for

}//for

GeometryBatchFactory.optimize(ground);

TangentBinormalGenerator.generate(ground);



return ground;

}//renderFloor

}

[/java]

6 Likes

Cool. As always, an SDK plug would be cool :wink:

normen said:
Cool. As always, an SDK plug would be cool ;)


yeah.

a plugin is in my @toDo list, but it will take me some time to create the gui and learn the plugin api.

Woah! That is sweet! :slight_smile:

Looks really nice, but seems to be missing some stuff to make it runnable. Was this intended to be self-contained, or just a sample on how to use the referenced code?

ok i packaged it, in a self-contained file, it now uses a simple application.



http://petomancer.googlecode.com/files/Dungeon%20Generator.zip



Got it working nicely here.

It provides a really nice working basis for future ideas.



Thank you very much.

tralala , if you don’t mind I will add your code to Simple Examples project: http://code.google.com/p/jme-simple-examples/



This is a project with simple JME examples/tests.

of course, you are welcome

1 Like

Added. Thanks for your sharing!

http://code.google.com/p/jme-simple-examples/source/browse/#hg%2FJMESimpleExamples%2Fsrc%2Fpetomancer%2Fdungeon%2Fcodeandcoke



I hope it will be useful for many people.