Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wave Function Collapse
#1
In the process of putting together some examples and tutorials for upcoming updates to the GX game engine project I've been working on for a bit, I thought it might be cool to look at some procedural map generation examples.  Historically, I've primarily worked with games that use "hand-drawn" maps.  GX has a map maker program that can be used for this.  There are a whole host of games types though that use procedural generated maps, so that for each play you have a new world to explore.

Anyway, in the process of looking into this further I began experimenting with the wave function collapse algorithm.  It's a pretty neat way to generate maps from a sample one that is created manually.  That map is then analyzed and used as a template or rule set for then generating new maps that have the same basic features.  For a more in depth discussion of how this all works I would recommend this article.

Here is my initial attempt at implementing the algorithm in QB64/QBJS.  You can run it from the embedded version below.  If you want more room to see the output you can also launch it in a new tab from this link.


To try it out in QB64 just download and unzip the wfc.zip file that is attached to a location on your filesystem.  Then compile and run the main.bas file.  (This program uses relative paths, so make sure you have the "Create exe in same directory as source" option selected.)

This is definitely still a work in progress.  For this to be used for generating very large maps, some additional optimization will need to be applied.  I would also like to look at being able to render multiple layers, as well as potentially just generating a specific region of the map at a time which would allow you to potentially create different "biomes".

If it works out and seems useful I may add it as an optional include in future releases of GX.


Attached Files
.zip   wfc.zip (Size: 335.08 KB / Downloads: 296)
Reply
#2
Amazing work. I want to implement my own version of WFC in my rouge-like. Are the adjacency rules auto generated or predefined?
Reply
#3
(06-24-2024, 08:30 PM)justsomeguy Wrote: Amazing work. I want to implement my own version of WFC in my rouge-like. Are the adjacency rules auto generated or predefined?
Thanks @justsomeguy! 

The adjacency rules are generated from a map that I created manually.  So, I load that map first, find all the unique tiles and then walk through each x,y position on the map and create the adjacency rules based on what I find.  This is done in the AnalyzeMap sub.

The nice thing about this approach is that you can use the same algorithm for any tileset just by creating a sample map with an editor.  Coding the adjacency rules manually would be pretty painstaking unless you had a very simplistic tileset.
Reply
#4
+1 this is pretty cool. i am thinking this might be fun to try with words from a given source text.
b = b + ...
Reply
#5
This is the coolest piece of compsci I've seen posted at phoenix edition to date.
Reply
#6
Well Pete's pretty pissed off. I generated 18 maps, and managed to loose 18 new virtual Titleist golf balls in the way too many water hazards!!!

Pete Big Grin 

+2 from me.
Reply
#7
(06-25-2024, 03:37 AM)Pete Wrote: Well Pete's pretty pissed off. I generated 18 maps, and managed to loose 18 new virtual Titleist golf balls in the way too many water hazards!!!

Pete Big Grin 

+2 from me.

Ha! We need to create you a new template map that is just fairway and green.
Reply
#8
Oh heck now I remember where I saw WFC before! Dang what memory!

aaditya's was a bit shorter in LOC. Smile

@dbox you still working on this?
b = b + ...
Reply
#9
(07-08-2024, 11:25 PM)bplus Wrote: Oh heck now I remember where I saw WFC before! Dang what memory!

aaditya's was a bit shorter in LOC. Smile

@dbox you still working on this?

Yep, I’m still planning to add support for multiple map layers.  So that, for example, if you have tree or shrub tiles that can be placed on top of grass tiles they could also be procedurally generated.  I should have an example of this in the near future.

Yes, his is definitely shorter in lines of code. It’s a somewhat different application of the algorithm.  I don’t typically focus very much on minimizing LOC just for the sake of it.  Plus, there is a fair amount of the code in my example that is just there to visualize how the algorithm works.
Reply
#10
Yeah LOC is certainly not the be all end all of coding. It does teach about tightening up your code to most efficient... Getting things running correctly is first priority and making it understandable to yourself 5 years from now, 5 days for me, is also important.

Gotta say, AP's code caught my attention because lots of great results for LOC. When I checked out a github link, I realized this stuff is like AI without a bunch of neural net stuff.

https://github.com/mxgmn/WaveFunctionCol...ree/master
ReadMe file might be same source as dbox in OP.

Definitely something worth mastering.
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)