GeeCON Prague 2014*
The moose likes Game Development and the fly likes Cellular Automata for Cave Generation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Game Development
Bookmark "Cellular Automata for Cave Generation?" Watch "Cellular Automata for Cave Generation?" New topic
Author

Cellular Automata for Cave Generation?

Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
Hey all, newbie here. High school student, wanting to work in the game industry. Anyway, I've been working on a rogue-like for quite sometime now, and have gotten quite bored with the current cave construction, so I decided to do some research. Came across cellular automata, and have decided to give it a shot.

Yet what I implemented isn't working. (obviously. ) It compiles, but doesn't run. Any one spot anything awry? It has to be something tedious, I've been looking at the code and haven't found anything for a while now.

I put it into a main method with print instead of a jframe with images to make it easier.

Thanks!


Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

I can run your program, it just doesn't give any output because you have an infinite loop.

Try sticking some println statements in your code...see if you can find it.


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
I actually just found it after you posted. It was the one while loop; it was supposed to be an if statement.


Woops. *shrug* Sorry for the trouble, and thanks



Now that I have it working though, I'm not getting consistent data. Sometimes I get amazingly smooth, good-looking caves, yet others I get this:



There's the code that actually handles the cellular automata methods. Any idea on either what's wrong, or how I can get better looking/more consistent caves?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Hard to say from your code. You should cut it up in more methods to make it self documenting, and maybe it would be helpful if you explained what algorithm you were using in the first place.
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
As you wish.

Init method:


Generate method:


Cleanup method:


Print method:


Main method:



As you can see, I have some pretty horrendous arrow code in the generation and cleanup methods; if anyone has any ways on how to compact it, it would be greatly appreciated.

The method I'm using comes from roguebasin. (Stupid school IP block banning perfectly ok sites.) It seems to work fine with the output the author has, and as far as I can tell, mine is similar.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2384
    
  28

In your generate and cleanup methods look at the code that sets startMap[i][j]. Compare it with the code posted in roguebasin.

First of all you never add new walls which is why your walls are all skinny like.. Secondly, the condition should be reversed in one of those methods, which is why you have those "pillars" in middle of the room
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
Jayesh A Lalwani wrote:In your generate and cleanup methods look at the code that sets startMap[i][j]. Compare it with the code posted in roguebasin.

First of all you never add new walls which is why your walls are all skinny like.. Secondly, the condition should be reversed in one of those methods, which is why you have those "pillars" in middle of the room


Unless I'm wrong, don't I change the tiles into walls here:


And I reversed the floor condition. Thanks
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2384
    
  28

Look at the relevant code in roguebasin



Now look at your code





See the problem?
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
Yes, I do. My bad. I also changed some other parameters (like checking if it's a single/double wall on it's own in the 2nd Moore neighborhood).

I'm getting better results:


But I'm still at times getting duds:


That have some really small islands with huge open areas.


Is there any tweaks I can make to try to make these duds any more interesting (I was thinking of placing walls if there was no wall tiles within three indices of it.) If not, on what basis can I reject them besides manually?

Here's my updated methods btw:


Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2384
    
  28

Maybe try to tweak the floor count and wallcount thresholds. Right now, if a 5x5 grid has 3 walls and 22 floors, the walls will stay where they are, and the floors will stay where they are. So, a grid with 3 seperated pillars will have no change
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
Played around a little bit, and I think I reached a happy medium.



Now all I need to do is weed out the not-connected areas.

Thanks for your help. I'll +1 your posts and put this up as resolved. If you have any tips on compacting my arrow code though, it would be very, very greatly appreciated.
Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
Hey there
Is it ok with you if I ran your generator on my computer? This just looks really cool and a place I might wanna start dealing with cellular automata. (If so, you mind posting your most updated code?)
Don't worry I won't claim anything as my own, I just wanna have a look if that's ok.

EDIT: About the disconnected bits: you can find the "main" cave and use a flood fill method to mark it and all it's connected branches, then go over every spot and if it's not marked, turn it into a wall (this means that there will be a lot more walls than before but this system is pretty much the easiest).
Ty Wood
Greenhorn

Joined: Mar 12, 2012
Posts: 12
Meshulam Silk wrote:Hey there
Is it ok with you if I ran your generator on my computer? This just looks really cool and a place I might wanna start dealing with cellular automata. (If so, you mind posting your most updated code?)
Don't worry I won't claim anything as my own, I just wanna have a look if that's ok.

EDIT: About the disconnected bits: you can find the "main" cave and use a flood fill method to mark it and all it's connected branches, then go over every spot and if it's not marked, turn it into a wall (this means that there will be a lot more walls than before but this system is pretty much the easiest).


Sure thing! I also have a version where I flood out the smaller caves. PM me with which you want.
Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
(did you get the pm?)
 
GeeCON Prague 2014
 
subject: Cellular Automata for Cave Generation?