Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cellular Automata for Cave Generation?

 
Ty Wood
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ty Wood
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the relevant code in roguebasin



Now look at your code





See the problem?
 
Ty Wood
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(did you get the pm?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic