diff --git a/docs/artificialintelligence/01-pcg/WaveFunctonCollapse.md b/docs/artificialintelligence/01-pcg/WaveFunctonCollapse.md new file mode 100644 index 00000000..6373f3bd --- /dev/null +++ b/docs/artificialintelligence/01-pcg/WaveFunctonCollapse.md @@ -0,0 +1,112 @@ +# Wave Function Collapse (WFC) for Games + +Ah, **Wave Function Collapse**—a term that sounds like it belongs in a science fiction novel, but in reality, it's an algorithm with a fancy name that doesn’t really involve any quantum magic. It's widely used in **Procedural Content Generation (PCG)**, a technique that game developers absolutely adore because it makes their lives easier. But don’t let the quantum-themed name fool you—there’s no need for Schrödinger’s cat or any collapsing universes here. + +!!! note "🚨 **Personal Opinion Alert**:" + + Who came up with this name? Sure, it sounds cool and science-y, but it's about as quantum as my morning coffee. Let’s be real: they chose it because it’s more marketable. 😂 + +In practice, WFC is just about solving constraint satisfaction problems. This magical-sounding technique was brought to the limelight by [Maxim Gumin](https://github.com/mxgmn/WaveFunctionCollapse) in 2016. Maxim showed that WFC could generate images and textures following specific rules, and game devs were like, “Yeah, we need that!” + +## WFC in a Nutshell 🥜 + +Imagine you have a blank world (a grid, pixels, voxels, whatever). You also have a set of rules about what goes where (e.g., no putting rivers next to mountains). WFC's job is to fill this world, slot by slot, making sure everything sticks to the rules. It's a bit like Sudoku—but instead of numbers, you're dealing with game elements like terrain tiles, rooms, or textures. Easy-peasy, right? + +Here’s the general rundown: + +* **Sampling the World**: First, create some slots to fill (think of pixels, textures, or rooms in a dungeon). +* **Set the Rules**: Define the constraints for your content—like which tile can sit next to which. No roads to nowhere! +* **Collapse the World**: Start with an empty world and let the algorithm fill in the blanks, one tile at a time, according to the rules. This is the "collapse" part. + +Now let's break it down in detail so you can use this in your next game and look like a genius. + + + +## Key Concepts of WFC 🤓 + +### Superposition (No, You’re Not in Physics Class) + +At the start, each slot can be _anything_—just like your favorite RPG character before you completely ruin the build with bad choices. 😅 In WFC, this means every tile can be in a "superposition" of many possible states. Over time, WFC reduces this mess of possibilities by applying rules, eventually collapsing each slot to a single state. + +### Adjacency Constraints: The “You Shall Not Pass” Rules 🚧 + +WFC’s bread and butter is adjacency constraints. You’ve got rules about which tiles can sit next to which. Picture a road tile that can only connect to other roads, or a water tile that refuses to be friends with desert tiles. It’s all about making sure the world makes sense. + +If you’re making a dungeon, for example, you can say, "Room A must connect to hallways, but Room B only likes to hang out with Room C and no one else." It’s like high school cliques, but for tiles. + +### Entropy: The "Hot Mess" Meter 🔥 + +Remember entropy from physics class? It’s basically a measure of chaos. But in WFC, entropy just means the number of options a tile has left. A tile with high entropy is still a hot mess—it could be _anything_. A tile with low entropy, though, has its life together—it has only a few valid options. WFC works by collapsing the low-entropy tiles first, like prioritizing the people who already know what they want for lunch. 🍔 + +### Backtracking: Undo Like a Pro ⏪ + +Sometimes, despite all the rules, WFC gets stuck. Maybe no valid tiles can go in a spot. This is when WFC says, “Oops, my bad,” and _backtracks_ to try again. It's the algorithm’s way of saying, "No worries, let's undo that and fix it." + +## Where Can You Use WFC in Games? 🎮 + + +### 1\. Procedural Level Generation 🌍 + +One of the coolest things about WFC is its ability to generate levels that are not only random but also make sense. No more roads to nowhere! + +* **Dungeon Generation**: Imagine generating dungeons where rooms, hallways, and dead-ends all adhere to your design rules. WFC handles this beautifully. +* **Overworld Maps**: You can create sprawling open-world maps with biomes that transition logically—deserts next to dunes, mountains surrounded by hills. Say goodbye to floating islands next to volcanoes! 🌋 + +### 2\. Texture Synthesis 🎨 + +WFC’s original job was to generate textures from small samples. In game development, this means you can take a tiny bit of grass or brick pattern and blow it up into a massive, seamless texture. You can forget those awkward texture seams that make your world look like a patchwork quilt gone wrong. + +### 3\. Tiling-Based Puzzle Games 🧩 + +WFC is also perfect for games where tile-matching mechanics reign supreme. You could generate puzzles or adjust game boards dynamically, keeping things fresh and players on their toes. + +### 4\. Story or Quest Generation 📜 + +WFC can go beyond just placing tiles—it can even help with generating quests or non-linear stories. Define a set of events and rules about how they fit together, and let WFC create unique quest chains that stay coherent. Move over, Dungeons & Dragons! 🐉 + +## How to Implement WFC in Games 🚀 + +### Step 1: Define Your Tile Set + +First, decide what your tiles represent—whether they’re rooms, terrain, or objects. Next, define the constraints for each tile. Room A connects to Hallways, Room B connects to Room C, and so on. + +### Step 2: Set Up the Grid and Superposition + +Set up a grid where each cell can be _any_ tile at first (superposition). WFC will gradually narrow down the possibilities based on neighboring tiles. + +### Step 3: Collapse Based on Entropy + +Now, the algorithm will look at each tile and decide which ones can go where. It collapses the tiles with the least options first (low entropy). Sort of like playing Tetris but with rules. 🎮 + +### Step 4: Propagate Constraints + +As tiles are placed, WFC propagates the constraints to neighboring tiles, limiting their options. It keeps going until the whole grid is filled. + +### Step 5: Handle Backtracking + +If WFC hits a dead end (it happens!), the algorithm backtracks, rewinds a bit, and tries again. Like Ctrl+Z but for algorithms. + +### Step 6: Optimize and Iterate + +Once you've got the basics down, you can start optimizing. You might add symmetry rules to make the generated worlds more aesthetically pleasing or speed up the algorithm with parallel processing. ⚙️ + +## The Pros and Cons of WFC 🎢 + +### Advantages + +* **Consistency**: WFC creates coherent worlds, avoiding jarring transitions like forests sitting right next to cities. 🌳🏙️ +* **Versatility**: WFC can handle 2D and 3D worlds, quests, or even story generation. +* **Efficient Input**: You only need a small input set and a few rules to generate huge worlds. + +### Challenges + +* **Complexity**: Setting up those rules is no joke. Too many constraints, and your world becomes boring. Too few, and it’s chaos. +* **Performance**: WFC can be slow with large grids or lots of rules. Optimize or be prepared for some serious lag. 🐢 +* **Overfitting**: Too many strict rules can lead to predictable or repetitive results. Mix it up for some randomness! + +Conclusion +---------- + +Wave Function Collapse is the ultimate game dev hack. It helps generate complex, rule-following worlds without losing the randomness that makes games exciting. Sure, it has its challenges—like any powerful tool—but once you get the hang of it, WFC can make your worlds, quests, and even stories feel richer and more dynamic. Plus, it saves you a ton of time! + +So, next time you need a procedural level, a tile-based puzzle, or a randomly generated quest, let WFC do the heavy lifting while you sit back and bask in the procedural glory. \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 8f73c1f4..0afc31f3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -20,6 +20,7 @@ nav: - Spatial Hashing: artificialintelligence/04-spatialhashing/README.md - KD-Tree: artificialintelligence/05-kdtree/README.md - Pathfinding: artificialintelligence/06-pathfinding/README.md + - Wave Function Collapse: artificialintelligence/01-pcg/WaveFunctionCollapse.md - Maze Data Structure: blog/posts/MazeDataStructure/MazeDataStructures.md - AI for Testing: artificialintelligence/07-automatedtesting/README.md - MinMax: artificialintelligence/09-minmax/README.md