Automatic Fort GenerationHow is a Fortress Constructed?Dwarves arrive at an area. They have a reason for going there that will determine the overall structure of the fortress/settlement, and is seen as a property of the embark.
During their time constructing the fortress, they have certain general needs that need to be fulfilled. These were taken to be:
- Shelter: Cave or Keep (initially)
- Security: Doors, Gates, Traps,
- Food: Forage, Hunt, Farm, Animals, Fish
- Building Materials: Wood, Stone, Ice, Ceramics, Glass
- Sleeping Areas: Dormitories, bedrooms
- Industry: Crafts, something plentiful in the area
- Medical Facilities: Hospital, Water, Supplies
- Self-Actualisation: Temples, Libraries, Taverns, Guild Halls
- Beautification: Engraving, Smoothing, Gold Floors, Mist Generators (necessitating plumbing), fort "scale' grows over time
Although this lists ordering is roughly true as a priorities list for all fortresses, it only holds for the first iteration of the fortress; once all the needs are satisfied the Reason-For-Going should primarily influence the future evolution of the fortress. Ideally the Reason should consist of a small number of attributes that can collectively describe a large number of scenarios. Catagorically I think these can be described as a collection of needs, each consisting of a statement relating to what there was too much of, and what there was too little of. The outcomes of such needs are simple to define in terms of material needs, like MORE LEATHER, but become more nuanced around something like MORE EXCITEMENT, or MORE SECURITY. The latter could mean moving to a safe area, or moving to a saf
er area and fortifying the everloving fuck out of it. The former could mean colonising a volcano, or moving somewhere otherwise inhospitable.
I imagine scenarios generated this way could look something like MORE WOOD 200, which would aim to set up a colony producing 200 wood a year. Multiple needs would require prioritisation; weighing each need against how fulfilled it is at every phase of fortress construction, coupled with priority gives a way to navigate fortress construction over time.
So - the Reason is a prioritised list of needs that shapes the phased construction of a fortress. There are catagories of types of things to build to fulfill needs, and using the priorities from the Reason coupled with the instantaneous needs of the dwarves (and means they already have to fulfill them) the next phase of fortress construction can be planned. As a fortress generator, we are blessed, for we do not actually need to do the work or wait for it to complete, but over time we would like to include available materials into the equation. It seems the most extreme, granular version of this is a program that plays dwarf fortress; but while that would definitely get the result we want it is a bit overkill.
The list of available Needs for the Reason should be modeled on the above list of.. er.. other needs? Local Needs. The Reason contains the Global Needs, which are modeled after and shape the Local Needs. The Global Needs can contain abstract concepts though, and these need to be translated to Local Needs changing over time*.
I have no interest in writing direct mappings, as these would need to be updated whenever I wanted to add a property. Instead, each phase of fortress construction can be modeled as having a certain capacity for work, and the prioritised Needs used to direct the effort. The current Global Needs and Local Needs combine to form a list of priorities for the phase. Changing the granularity of the time scale considered would affect the kinds of fortresses being generated, as this would affect the construction capacity available in that time frame. At this point I imagine something like a fortress having a construction capacity of say, 300 actions, or 50 actions per dwarf over the considered time period; this is treated as a form of currency which is to be spent improving the fortress. It might be decided to spend some capacity on increasing the forts ability to generate food. In most cases dwarves will tend to try and spend as little capacity as is needed to fulfill the need, while planning for the future. It doesn't make sense to build a food area that could only feed 30 dwarves if you know you're aiming for at least 200. Either room should be left from growth, or it should be "known" by the generator that fortress construction is sometimes a two step affair, with a tiny fort created initially to house everyone while building the big fort**.
How is a Room/Area Constructed?So, at the moment, fortress construction looks like this:
- Arrive with a Reason
- Evaluate fortress capacities to generate list of Local Needs, priorities according to Needs catagories at the top
- Evaluate Global Needs to generate a list of Local Needs
- Interact these two lists in an as of yet unknown way
- Determine the construction capacity of the fortress from the population
- Build items off priorities list.***
Key problems that remain are figuring out how to interact these lists, and how to actually generate the rooms.
It feels like how these lists interact should be somehow dictated from the Global Needs.
The king ordering you to do something affects your decisions differently to you just wanting to do it on your own. The fact that your fort does not need to produce food because it expects it by caravan can only come from the Global Needs (as in a delivery of food, not a trade). Needing to prioritise a certain industry for export can be due to a local abundance, but the location of fortress could also have been chosen to exploit that, in which case the Global Need of "MORE MARBLE STATUES" could have dictated the location in an area with much marble, but also preferentially drives the fortress to have a marble industry that is larger that it might otherwise have been, earlier than other forts would. I can imagine the Reason not only a collection of Global Needs, but also containing certain flags that would constrain the development of the fortress somehow - some kind of limited selection of overarching themes to add flavour to the process.
As for actually generating the rooms... I think there is an exercise in graph theory waiting for me in the future to be able to plan it out properly, but in the general sense it will be generated from a hierarchy.
As an example, the fortress needs food. Currently, there are no facilities, and so the food priority is high. On this map, over the course of a year there is on average 100 meat available from hunting↕. There is no soil. There is water. There are 0.5 caravans a year from neighboring settlements. The need for Food requires some way to generate and store food. The Food Storage Area is a substructure of the Food Area↕↕. There are several factors influencing the size of the Food Storage Area; including the frequency of the food source, the number of dwarves in the fortress and the required stock to store. A military fortress might mandate a years worth of food be kept at all times, in case of a seige, where a peaceful villiage might just need stores to last the winter. The exact math for transforming "food generating capacity" and "storage needs" into a number of squares of stockpile feels like something that will be determined by fiddling around, but once that value is generated, it needs to be transformed into an actual area.
As a first effort, rectangles of a fixed ratio connected by straight corridors; at a later stage, a configureable architecture. The Reason and Global Needs can kind of arise naturally from the game at the moment - and caters for some specific contrived scenarios to be included - but the architecture should have a guiding hand in it. I forsee it as something like the world generator, exposing a lot of parameters to be tweaked and customised per civilisation, but (on some level) relying on fixed configurations as starting points for generating the complexity of a specific world. Things like:
- preferred shape for a room (square? rectangular? round? prototype shapes defined by points and concavities?)
- perferred type of material (initially single material, eventually linked with certain features, eg stone bases with wooden houses
- preferred location of the main fortress (above ground/below ground; biome peference )
- roof-height (as a function of fortress maturity, affecte by type of area and how many use it)
- willingness to abide an odd-shaped wall to fit a room into a rocky outcropping vs. willingness to mine it out and build something outside to make the inside more shapely
- overall fortress shape (high and narrow: an underground fort centered around a shaft, or a wizards tower; or flat: a series of houses, or single z-level underground fort)
- general spaciousness (a number relating average corridor length between areas to expected fortress population)
These properties should be able to provide the flavour of how to build the fortress↕↕↕ once the decisions have been made as to what size each room should be.... but I get ahead of myself. We've only just decided how big the food area is going to be in this example.
Depth first is fun but sometimes distracting and laborious way to explore a topic.In our food area, we have now determined the size of stockpile that is needed. Based on the map we choose to initially hunt for food, but given there is water we can make a farm area somewhere, this will just require too many actions to be a first iteration of our forts food source... but nevertheless in the future, this farm will be a part of our food area.
We should thus look at the embarkation Reason to determine what the forts projected size is to get a rough estimate of the overall floor area a finished food area would need. There's going to be a lot of heuristics and tinkering here...or maybe just assigning a max size to each thing and summing them.. but based on the projected food needs of what the dwarves consider their "finished fortress", a number of blocks is chosen that will represent the area where food is made.
This Final Food Area will contain a Final Food Storage Area, of which we are making a small piece right now¶. We can thus say something like "In this phase, we saw we will need, in the final fortress, a food area of about 500 blocks. 200 blocks of this will be required for food storage. Of those 200 blocks, we need to construct 50 to fulfill our projected food storage needs until the next phase, which leaves 200 actions for subsequent projects this phase".
Likewise, areas can be partitioned into little pieces for farms, kitchens, stills and all the things you would expect to find in an area named the Food Area. There is still something to be said for how these areas connect externally, and internally.
This partially leans back to the architecture config idea, since some may have a preference for rooms connected by corridors, where others mights prefer large structures with internal walls... but overall, there needs to be a way to decide where things lay relative to each other and how the connect them, beyond the concerns of architecture. Functional concerns - the Mad Demon King didn't get to be the Mad Demon King by ordering his food production be placed on the magma level, and his fortress be built on the surface¶¶. All races are fundamentally lazy and won't do more than is needed to accomplish a task.. even Beautification requires excess capacity.
There are fundamental relationships between areas and industries. Food areas would want to be close to the food source. Dining areas need to be close to the food store or, if they are beyond a certain distance, have a local stockpile of food, drink and mugs. On some level, every room and area has other rooms and areas they want to be close to, and perhaps others they don't want to be close to (nobody likes walking past the corpse pit).
Placing a room near something it wants to be near makes that room happy, and a fortress full of happy rooms is a happy fortress. With some rooms that need to be central to everything, it may not be possible to make all other rooms happy; but there are degrees of internal conflict that a fortress can tolerate, provided it is trajecting in a direction to make the fortress as a whole happier. The happiness of a room is a function of the status of its relationship with all other rooms it has relationships with, and new constructions use the current and projected future happiness of all rooms in the forts to guide their placement.
This would lead to very predictable results in the absence of a world and geology to consider; as the current algorithm would tend to find the same optima each time. Even with the architecture changing path lengths, room shapes and sizes, and preferred verticality, it would still find fundamentally similar solutions. On the one hand, merely introducing a map will provide a lot, at least near the surface and around the caves, providing hard constraints on where you can build will squeeze and push the fort into different shapes.. but I feel there is another element needed.
As a fortress generator, we are doubly-blessed, for we can see the entire map at once. In the future, requiring the dwarves to dig exploratory tunnels to determine where they can build could add a lot of character to a fort, but for now having them know it as if the voice of God spoke to them, or they had a special Dwarven rock-sensing sense will suffice. The geology of a world isn't generated as a random individual squares of different materials¶¶¶, there are areas that consist of mostly the same material, and there are veins of ore. I know I like nestling my functional areas inside a certain type of stone, and will even preferentially lengthen a piece of corridor so I can cross the border into a new type of stone for homogeneity in the new area. Sometimes, I mine out a pocket of orthoclase for a big, almost elliptical room. This can maybe be encapsulated as a sense of aesthetics in the architecture config, or as a trait in the Reason - willingness to delay work for aesthetic purposes. Small villiage, sure. Outpost on the front lines, no.
Using the world geology will also help to make things look better, but this still doesn't solve the problem of the basic fort layout. On the one hand, I want to avoid hard coding my ideas of how a fort should look into this. I mean, sure, there is absolutely no way to get away from these fortresses bearing my mark, but I don't want to fundamentally limit what kinds of things can come out of this without reason... that being said, it feels like I might have to have some kind of set of basic values for different fortress connectivities that make sense to avoid every fortress (in the absence of a world) looking the same for a given number of dwarves with the same industry, etc. Randomisation feels like too blunt a tool, and a seed value for spicing up otherwise deterministic choices... well, maybe, but I feel there is a layer of abstraction I'm missing here that would be able to do what I want in a more controlled way. I suspect that reading up on graph theory, and actually coding it for that matter, would be most inspiring.
...for now, though, the above is a satisfactory model to generate a variety of fortresses. So, areas and rooms are generated by:
- Select a type of construction from the priorities list.
- Determine which components need to be created or upgraded
- Determine if the current area has capacity, else designate a new maximally happy area (based on current and future for projections)▲
- In the area, place the room for maximum happiness. Populate the room according to the architecture config, available materials and fort maturity
- Evaluate fort
The last item on that list refers to any excess actions left over after optimally distributing the points among the prioritised Needs, and can be used on small beautification actions, or upgrading infrastructure - widening a corridor, smoothing a wall, looking if there is maybe a corridor that could be built to make all rooms happier at once without compromising fortress security, or violating the architecture config▲▲. There may also be value in doing half a thing with the left over points; nothing says "living fortress" like a half finished construction project.... and can you imagine how cool it would be if you wander onto the map and there's a stream of dwarves carrying stones to build something?
A Mental Test RunNow that the tools and abstractions governing the generation process has been loosely defined it should be possible to see what kinds of scenarios we can generate using them. This generator can go outside the realm of things that can currently happen in Dwarf Fortress, but just to motivate its construction. Running through an actual scenario should help point out any cracks in the current system.
I can imagine a goblin fortress built on slave labour. The fortress was constructed to make weapons and armor for soldiers in other fortresses - this Reason for this fortress might look something like MORE ARMOR 100, MORE SWORDS 100, to bias their industry and indicate they need to generate exports. The embark information might indicate 100 goblins and 20 trolls. The style guide for this type of fortress might say that this type of socity typically has 5% upper class, 15% middle management (slavedrivers) and 80% slaves, and these groups do not mix when eating or sleeping. Furthermore, sleeping areas are separated by species. The slave cast sleeps in dormitories. There is a subsection on troll sleeping quarters to generate something appropriate for them. Trolls cannot be nobles.
As this group arrives in the new area, they first build shelter. The architecture config for this society highly prioritises shelter for the upperclass / nobles, and requires one set of quarters that is more lavish that all others. If the slave force is large enough, the main throne room is constructed first. Otherwise, a temporary house in the field must be built to house the nobles until a fortress interior exists. Priority is generally given to the comfort of the nobles. Secondarily, some way of acquiring food is needed. The Reason for this fort may include that this fort subsists on caravans of food stolen by raiding parties - I'm not sure what goblins and trolls eat, but the thought of them farming seems slightly silly... but at this point simultaneous priority is given to a massive smithing area and housing for the middle management. Later there will be housing for the slaves. Terrible, cramped housing, physically distant from the nobles... but close enough to protect them if something attacks.
The size of the smithing area is based on the amount of laborors present, and the desired exports of weapons and armor. This translates into a size of area, which consists of stockpiles, workshops, and paths. The workshops need wood and or magma, so if depending on whether those things exist on the map those 'industries' might spawn in a limited fashion....or on the other hand, goblins may be able to work metal by magic. It would certainly make planing this scenario easier. This generator, not a human like me, would have reaslied it was needed to spawn the logging and charcoal generation industry before the smiths one.
I'm speaking in terms of goblins and slaves here just to stretch my brain away from dwarves for a moment, but everything I'm saying should translate directly back to dwarves. This world now has a freestanding structure somewhere that was used to house nobles; some kind of structure to house the middle management, and it is now getting some kind of shitty dirt hole for the slaves, just connected to the surface. There must be a reason a colony was placed here for the purpose of making weapons and armor, I'm going to choose to believe because it is a densely forested area that has the wood required to stoke the furnaces. As part of the embark information it should be specified that this fotress intends to import weapons and armor that doesnt fit their forces, like dwarf/elf and human armor, and melt it down to create new weapons and armor for the vile forces of darkness. This simplifies things but not requiring me to dream up a goblin mine, and, for the moment, leaves me reasonably happy that the above abstractions can encapsulate everything from the vanilla ideas they were generated from to some outlandish scenarios of the future.
An even better test, however, would be to play dwarf fortress while pretending I am the generator. That's kinda what got me into this position in the first place, when I had to restart a fort because of some combination of gamebreaking bug and personal stupidity, and I found a young version of the above running in the back of my head.
Yet the best test would be to start writing this and encountering actual problems. I'm a big fan of building castles in the air (or perhaps more appropriately, a fortress in the sky), but as TS Elliot rightly said, "betwen the thought at the action, there falls the shadow". There are definitely still things that need to be planned, such as how to manage what could reasonably be imported vs what should be made locally (clothes, for example?). Whittling down all the fluff to a core idea that can be developed is it's own kind of feat, but a neccesary first step to what could easily try and become an automatic dwarf fortress player if I'm not careful.
teatime is taken by a fey mood!
teatime claims an IDE.
I must have enums!
I must have a hierarchical class structure!
I must have definitions for object interactions!
teatime has begun a myseterious construction.
teatime works furiously!
▲▲▲
* Integrating Local Needs over time gives Global Needs... the calculus of fortress construction? Maybe we can
derive some way of interacting these things
** An abandoned little fortress next-to-and-accidentally-linked-with the main fort would make for a neat little feature in adventure mode.
*** This is an interesting optimisation problem, since sometimes it's better to do eg. all the food at once, and other times you're better off adding just a little capacity here and there. A military fort might need a little food generating capacity to keep it running when caravans can't get through, but would much rather have a chunk of wall constructed after the first phase. In this scenario we are not dreaming we are playing dwarf fortress, we are dreaming are are military dwarves setting up an advance outpost.
↕ I just discovered you can hunt things to extinction, so perhaps hunting should always be considered a transitionary food source.
↕↕ Note that a different incarnation of Food Storage Area is also a substructure of advanced Meeting Areas and Dining Areas.
↕↕↕ Which opens up GREAT possibilities for having fortresses being taken over and the new occupants expanding it using their sense of architecture....
¶ In advanced fortresses this area may undergo internal changes where the stockpiles are arranged better and specified on a more granular level.
¶¶ And yet, one might argue that is exactly how he became the Mad Demon King. Architecture config: "Hates citizens"; doesn't minimise path length for optimal industry.
¶¶¶ Side note, a civilisation having a geologist could enable them to make mining colonies that mine specific materials. Libraries teach people cool skills, it would be nice if they featured here somewhere - like a knowledge of hydraulics enabling certain advanced Dwarven Science to appear in its colonies.
▲ Ah, no, I think this is what was missing from my thoughts on connectivity. The initial plan might always be the same, because there are only so many way a small number of people will choose to live together, or set up a fixed size fort optimally. The food, dining and sleeping areas are linked in some way.... but as the fortress grows, as needs change, and requirements change, the planned areas and their locations may be insufficient, leading to new and interesting structures. No need to mess about with complex math to generate interesting structures, when events happening over time can do a lot of heavy lifting for you.
▲▲ Defining acceptable configurations of corridors numerically brings us back to graph theory.
▲▲▲ Alternatively, some years earlier:
teatime went stark raving mad!
teatime is running around babbling!