It’s time for more adventure crafting!
The first official resource I decided to add was food. I think it makes sense to start from the bottom and work my way up. These factions are just springing into existence, after all. They have to worry about the basic necessities! I don’t think you’ll find too many civilizations that decided to settle near a copper deposit instead of food and ended up being too successful.
The stock market might like copper but your tummy sure doesn’t. The production chain for food is also quite a bit simpler than metal. It’s easier to plant, grow, and eat a potato than it is to go through all the steps required to forge a set of armor.
I’m going to add just one type of food resource for each existing terrain type. That’s plains, forests, mountains, lakes, and ocean respectively. Eventually, though, there will be hundreds if not thousands of resources. The more resources there are the more unique the world will be due to factions having access to different materials. I’m also using real-world resources, such as corn or sheep, for the time being. I’ll introduce some make-believe resources later!
The first resource I’ll be adding is corn. I wanted to start with the most useful resources, but after some research, pretty much everyone disagrees on what is historically the most used old-world vegetable. It was a toss-up between grains and stuff like carrots, cabbages, and potatoes. I gave up and just choose what I personally think is the most recognizable veggie.
I set corn to spawn on generic land tiles (which I’m now referring to as plains) and the rest is history. The resource generator is seed friendly so the map will continue looking the same.
Here is what the world looks like after adding corn. It’s not very impressive, admittedly. The resources only occupy a single tile so there’s never going to be much to look at. It’s not like terrain generation where I’m causing huge sweeping changes across the entire map.
That’s actually it for resource generation! It feels odd to suddenly pivot away from resources, but I’d like to focus only on what needs doing rather than adding things for the sake of it.
The next thing I need to do is figure out how to fill the world with factions.
I eventually settled on using a BSP. That stands for binary space partition – which, like most programming related things, sounds way more complicated than it actually is. It’s just an efficient way to cram some arbitrary space with a bunch of rectangles. That’s all. It does this by taking a square and halving it. Then it takes those halves and halves them, too!
Here’s an example animation of the BSP process I painstakingly made by stitching a bunch of screenshots together. I start with a maximum rectangle size of 400 and reduce that by 10 every iteration. Eventually, a threshold is reached and it stops slicing those rectangles up.
How does this help me, though? If you saw the animation then you presumably noticed how quickly it can carve a large space into a bunch of smaller ones. I don’t have to do flood fills, distance calculations, collision detections, or any other time-consuming object placement routines. Instead, I can just take some of those rectangles and shove a faction in them
Each of these spaces is guaranteed to be separate from all the others due to how a BSP works so I can just choose one and spawn a faction at its center. Easy! It’s future-proofed, too. Even if the map size is dramatically increased it will still be just as quick.
I settled on a minimum size of 30. This means the BSP will keep trying to carve the map into pieces until its impossible to shrink any of the rectangles any further. This number results in a good number of spawn locations while also leaving enough room to see whats going on.
This is what the world looks like with the rectangles overlayed on them. Every single one of those rectangles is where a faction object will be placed. It’s not time to spawn any factions yet, though. I need to do some cleanup first to remove bad spawn positions.
I’m going to loop over each of these rectangles and compare it’s center coordinates to the terrain below it. If any of their center coordinates are ocean then I’m going to remove them.
This is what happens after I’ve discarded any rectangles whose center is ocean. I could get way more accurate by only erasing rectangles that are entirely ocean but decided not to. It would be a lot of work to implement that in a performance-friendly way. I’ll probably do that later, but since my current plan is to spawn factions at the center I don’t think it’s necessary.
Also, I’ve slightly shrunk all the rectangles. It’s easier on the eyes when they’re spaced out.
I got some spawn points up and running so now it’s time to start actually adding factions!
There we go. Each faction gets a random lightish color and is named after a random number in the 100-999 range. I wanted to give them randomly generated names but that ended up being too much work. I’ll do that later once I have some history generation to pull data from.
I made the random colors brighter, thickened the rectangle borders and gave them all a dark transparent background. It’s a bit easier to see things now! It’s not perfect but theres only so much I can do. This is just debugging info, anyway, so who cares? It’s for our eyes only.
It’s finally time to start actually simulating things – but what?
The earliest thing I can imagine any civilization doing is growing. That boils down to claiming territory and increasing their population size. I can’t think of a realistic way to simulate this, though, so we’re going to do it in a very programmey way. I’ll think of something better later!
Sure is a lot of ‘later’ going on…
I’m going to start by getting all the factions to look like proper countries. I’ll do this by using a floodfill. Lots of floodfills, actually! Each faction will simultaneously grow outwards and claim any territory they can get their greedy little hands on. They claim one pixel at a time and, at least for the time being, will be respecting one another’s borders by not taking claimed land.
That was pretty weird. It started out looking fine, but as time went on things really went off the rails! Especially in the upper right corner. This is happening because the floodfills are competing for space. The more limited the real estate becomes the more ridiculous things start to look. The factions in crowded areas ended up looking fine-ish due to relatively equal space but those near the ocean have plenty of room to thoughtlessly expand into.
Let’s move on.
I’m going to start off by making each faction’s initial expansion less perfectly square. This is happening because the floodfill is storing its expansion candidates in a list. Lists are ordered which means the order you populate them will also be the order you loop over them.
This is causing a square shape due to how the floodfill is expanding. I’m going to change to a set which isn’t ordered – which basically means the floodfill direction will be randomized.
Also, before any programmers yell at me, I know that using a set isn’t literally the same as randomization. It’s close enough to get the job done, though, and isn’t slow as snails.
That looks better. It’s starting to look like a territory map now! It’s still not perfect, though. I need to change how the floodfill detects neighboring tiles. It’s currently detecting neighbors in all eight directions. This causes the weird noisy looking mixture of territories you can see in some parts of the map. That’s happening because the floodfill is allowed to cut corners by moving diagonally. I’m going to restrict it to cardinal movement only.
The top example (principle) is cutting corners while the below example (cardinal) is not.
Here is a crude example of diagonal versus cardinal floodfilling. That top example is exactly what’s happening to make things look so noisy. Turning off diagonal flooding will fix that.
That’s way better! The map is so much cleaner now.
The next thing I need to do is restrict the floodfill to land tiles only.
Perfecto! The map is now full of a bunch of colored blobs that look vaguely like countries.

This is the kind of map I’m going for, by the way, if you can’t read my mind.
Unfortunately, I’ve decided not to take this approach at all. That’s prototyping, baby! Instead of digging in and doubling down, I’m going to admit my mistake and adjust accordingly. I left in the above content because it was fun to write and was hopefully an interesting read, too.
Let me explain some of my problems with this approach before changing course.
- I couldn’t figure out how to fix the out of control flooding issue that you saw in the very first example. It might look fixed right now but it’s not. Limiting the floodfill to land tiles has merely reduced its chance of occurring. The possibility is still there. Lurking in the shadows.
2] The faction borders are way too wiggly. I can’t figure out how to fix this, either. I know that real-world borders can sometimes be pretty funky (wtf Ireland lol) but more often than not they’re relatively straight or otherwise make some kind of sense. Thus, I don’t mind some squiggliness – but every single border shouldn’t be so crazy. More importantly, they should look more unique. They’re not literally the same but if you’ve seen one you’ve seen them all.
3] I’ve decided to handle world generation differently. Most procedural games will simulate things before playing in a process that’s usually called history generation. The issue I have with this approach is that the end result is a stagnant world. Instead of being part of history and helping shape the world, you’re just playing in the afterglow. You get to hear about great events but not take part in them. I’m going to take a more interactive approach by letting the player get directly involved in altering the course of history. I think this is essential for an adventure generator. What kind of great adventure doesn’t end up changing the world in some way shape or form? Can you think of any that don’t? I certainly can’t.
Thus, some change is needed. This method of magically spawning a bunch of factions out of thin air isn’t interactive enough and the territory grabbing floodfill is too artificial. I’m going to switch over to a more organic simulation. I’m going to abandon the idea of pre-generation and instead go for a state where the world is in a constant state of generation.
This idea of constant generation will leave the world open for player manipulation. This idea was inspired by my goals in the Super Project, but it’ll be happening on a smaller scale.
Much like how things are always changing in our world, they will always be changing in this world, too! The first thing I need to do is change how factions are created. They need to be formed in a more natural manner. I’m going to fill the world with agents and have population centers form when enough of them share the same tile. This is to simulate how smart people claim civilization is formed – which is people coming together to make urban settlements.
This is what the world looks like when it’s covered in 10,000 agents. Each of them is placed at a random coordinate. The only placement rule is that they can’t spawn on lake or ocean.
I should simulate movement next, but I don’t think it’s needed yet. There are so many agents on the map that I bet theres a few clumps of them that I can turn into a faction!
I was right, but only marginally so. The map is so big that even with 10,000 agents scattered across its surface there’s barely any agents occupying the same tile. I had to lower the bare minimum number of agents needed per tile to 7 before any factions would pop up. This gave me a grand total of 3 factions. It’s not perfect but its a nice good start!
I’m going to go ahead and get them moving now. Every turn the agents wiggle to a random tile. The idea here is to cross my fingers and hope enough of them visit the same tile.
It’s alive! I disabled the terrain view and faction generator so they’re easier to see.
I enabled faction creation but messed up the faction coordinates. They’re only being made along the x-axis which results in that weird diagonal conga line. Things gradually slow down because hundreds of factions are being created in the same position every single turn. The factions aren’t moving, by the way. It just looks like they are because new factions that have different colors are constantly being spawned. It creates a neat moving line illusion.
Fixed it. Factions are now being created at the correct coordinates! It’s pretty neat watching all these factions pop into existence. I’m going to increase the minimum agent count to a bigger number now. This should thin the faction count down. Also, those long glitchy lines that appear are what happens when text is drawn slightly off-screen in pygame.
I raised the number first to 30 and then gradually all the way down to 10. No deal. I let the simulation run for a good while, too, but nothing happened. The luck required for that many agents to appear on the same tile for one turn must be astronomical. I’d rather not increase the number of agents but I also don’t want to lower the agent per tile threshold. What do?
I think I need to improve the agents’ movement behavior. The whole random movement thing was a good starting point but it’s too unreliable. I was going to try treating the agents like magnets and subtly attracting them to one each other’s coordinates – but that sounds like a nightmare to implement in a performance-friendly way. I’ve messed with this kind of particle physics simulation in pygame before and it’s sloooow. This simulation is going to be running every turn while the player is playing so I have to be cautious. It’s fine if typical map generation takes a few minutes but no one is going to tolerate every turn taking that long!
I’m going to take some inspiration from ants for the next step. They’re individually dumb but capable of seemingly intelligent behavior in groups. They accomplish this by reacting to a few basic senses that can result in sweeping changes. This makes it look like they’re being directed by some kind of leadership when in reality they’re just a bunch of little autonomous drones reacting to basic stimuli.
I’m going to make the agents indirectly smart by feeding them sensory input. This way I can make them do smart things without actually giving them any AI.
Or, in other words, I can process ten thousand agents every turn without lag!
I’m going to create a scent array out of noise and overlay it across the terrain. The agents, when moving, will analyze their neighboring tiles and move to whichever one has the highest scent value. This should cause them to naturally congregate towards the same areas!
Whoa! It worked surprisingly well. That blue overlay is the scent array. It looks like solid blobs of blue, but it’s actually a smooth variety of noise float values. I didn’t choose to make it blue, by the way. It’s the default color that pygame applies to surfarrays. That’s a built-in tool that can be used to rapidly draw entire arrays in one pass instead of having to slowly loop over every single pixel. Either way, it’s a colossal pain in the arse to manually recolor.
Here is a different view that I manually drew one pixel at a time using standard greyscale noise coloration. It lets you better view the gradient of noise values. I wanted some parts of the map to be more attractive than others so I applied an aggressive threshold. That’s why some parts of the noise look so sharp. This way the agents are driven towards certain areas.
This is what the non-thresholded version looks like.
Anyway, the scent approach is far from perfect. If you scroll up and watch the animation you’ll notice how the majority of agents stop moving. That’s because they reach a position where none of their neighboring tiles have a higher scent value than the one they’re on. They’ve basically found the smelliest tile and logically refuse to budge from it.
My first idea was to implement a scaling approach where agents, upon getting stuck, would begin searching further and further out for a higher value tile. However, then I realized they would need to do some form of pathfinding to reach said tile. I’d also have to use a floodfill to find it in the first place. I’m good at optimizing things, but I don’t think even I can squish 10,000 floodfills and pathfinding calculations into a single frame without causing lag…
I’m going to try some simple brute force methods before getting fancy. First, I’m going to turn movement randomization back on – but now they’ll only move if they’re stuck. I’m hoping this will shake them free and help them find a better scent trail to follow.
I expected some progress but I didn’t think it would be almost perfect! Many of the agents that were once stuck are now wiggling themselves free and hooking up with other agents. If you compare this to the previous animation you’ll see way more factions being made.
Theres still some duds, though! I think these guys are remaining stuck because theres no scent trails near them to follow. That’s why the bottom left island populates much slower – especially in the lower middle portion. They’re moving, sure, but they’re in such a wasteland that they never catch a scent to follow. It’s like wandering around in a desert for water.
I think theres also another problem happening. If you pay close attention you’ll notice some agents moving back and forth between the same two spots. They get stuck and randomly move. Then, next turn, they detect their previous position at the best option and move back to it. They’re getting stuck in a back and forth loop instead of stuck idling in one position.
Before applying any fixes, though, I’m going to change something that I think makes a lot of sense. I’m going to delete any agents that exist on the same tile when a faction is made. If a faction is made then it stands to reason that people have created a settlement – so they shouldn’t be moving around anymore!
Here is the result! If you compare it to the previous image it takes longer for factions to start appearing. There’s less faction overlapping, too. The factions were previously flashing like crazy because agents were constantly producing them at the exact same location. This was causing a graphical layering issue that resulted in all that flashing. It can’t happen anymore now, though, because once a faction is created the founding agents are killed.
Then I implemented a heatmap visualizer so I can see the longterm movement of all the agents and it verified my thoughts. It doesn’t take long before movement comes to a halt.
You can see the heatmap die as time goes on. It starts out super hot from all the movement but everything slowly putters out. I’m not sure why but something is gumming up the works.
I think that’s enough for now. It’s been a long time since I posted anything, so I’m going to go ahead and release this post early. See ya next time!










































