View Full Version : An attempt at mapping using economics.

08-28-2010, 08:49 PM
This is probably going to be a big thread for me and I have been putting off starting it but, well, theres been a number of posts in the last few days so now is as good a time as any.

I have had an idea for a very long time to try to make a map using character economics. I mentioned it in this thread (http://www.cartographersguild.com/showthread.php?t=4322) and in there I said it was an old idea. So this has been rattling about in my head for a few years now.

The idea is to make a program that auto generates a map by monitoring a continuous simulation of the everyday life of characters in a fantasy village or town. The basic premise is that we randomize some stats for some characters - and these stats will be nothing like D&D or game stats - and then let them loose. They will do stuff to satisfy their needs and desires one of which is greed so that they attempt to maximize their wealth. So its like running SimCity in full auto mode but in fantasy.

I know of lots of people who have made auto generating mappers and some auto city mappers but these fall into several camps. One is the pure random approach and the other is fractal or L-system. Now I am not saying that any of these systems is a bad one but I personally have no faith that they will encode the complexity of human (or non human) behavior. We don't build random cities. They are grown.

Theres been many people who post up generators for characters and I have even seen one which tracked a persons state, got them married, had some kids and died. So that it generated a full town listing of all characters will spouses, brothers, children, parents and a full obituary. So thats not so different from what I have in mind.

The key thing tho is that I don't know of anyone yet who has tried to plot the map of that village of tracked people.

I mentioned in that thread a while back that its a very ambitious project and a very hard program to code up. For about a month now I have been scribbling making notes and drawing a lot of tables, boxes and arrows and I think I have whittled it down to something that I can code.

I have for a while been hacking it out. I took GeoTerSys and gutted it and reinstalled a new set of commands. Then set about writing up the tables of stats, items, world, and transactions. I have been coding for some days now and not got to run it and produce a single pixels worth of map yet but I can dump the status and get about 10 screen full's of stats and I made the very first transition tree search the other day. So I *think* it might be possible that this is still a goer. But I am not entirely sure I have to say.

The program is another of my closed source things - sorry - but I will be dishing out betas (with maybe some small limitations) to anyone who wants to have a go. I don't really know where this will end up. Maybe it works real well and maybe it falls flat. I may use it to model other non fantasy mapping too cos I am programming it as an engine so we change rules and items and we simulate something else. So I just don't know.

So where am I with it. Whats the dirt ? Well I have scribbled out what I think is the minimum set of stats, needs and so on to get one human into a base map and start working on it. Oh I should also say that you do in fact have to enter a base map. That consists of just a 4 color bitmap showing where the trees, ground, rock, and river are. The idea is that the map it produces will fit into that layout so you can take a bit of your world region and just blat down the basic resources available and then let it grow a city on top of it.

So far a human will wander around and never die of starvation or thirst but would like less of each. He does not die from lack of sleep or exposure but will really want to prevent being subject to them. He will have basic skills in everything a peasant might do and carry basic tools like an axe, a hammer, and also have some grain on him for planting. He starts with no money, no house, no land, no quantifiable possessions of any type.

Then he can move to water and drink, chop down a tree into logs, plant some grain, bash up some rock or rest on the ground. These things I call transactions and will satisfy some thirst, hunger and rest or increases his wealth in the case of the logs which are worth more (to him) than trees or aggregate being worth more than rock in the ground.

The idea is that he must satisfy his needs and then optimize his desires by looking at the supply and demand of all item types from his point of view (which might be different from the next man in which case there is scope to trade).

Once I have him drinking, planting corn and eating, I am hoping that he will build a log cabin, a house or a shack or something. His choice on what and where he builds it might be beneficial to him or a detriment. As he moves about it will track the paths he is laying down and I hope from the choices that people make and the paths they use to get about it will eventually generate a map.

It will also generate a full list of characters with stats like some of the other programs out there, but, it will also have their skills, their bio, history, their current state and even their future desires. And it should all make sense. I.e. We will know that Bob cuts trees and sells them to Jim the blacksmith and we will know for what price and how often. We will know how much tax is required to keep the village operating, who owns and has inherited all the buildings. It will have a kind of newspaper of notable events like major burglaries, scandals, devastation, invasions and so on.

The output map might be quite simple and used as a basis for a more sophisticated renderer such as something that POVray or a 3D app or virtual tour might be able to use. My intention is that it will take the base maps out of GeoTerSys (tho its designed to be a breeze to put these in manually) and then, aside from writing a bitmap of the village, write out a set of files so that ViewingDale can move around and access all the extra info about the people on the map. But neither of those apps will be required for it to be extremely useful.

Yeah its a tough programming exercise and I am not saying that I will be capable of actually making it happen. But I have started on it and ill post about its progress right here ! But I reserve the right to quit on it and not look like a ninny, OK ?

08-28-2010, 09:14 PM
That sounds, sounds... incredible! I'd definitely love to see results from this, even try your beta stuff? I'm pretty sure now that I can't write this sort of thing myself but I've studied physics and electronics and I've spent most of my life looking at pure logic (of the electroinic kind) so I can analyse stuff. If I can help in that area, let me know!
You've obviously considered the complexity of this rather seriously. How deep into commodities and skills are you going to go? Are you going to allow external economic stimuli or natural disasters or.. or... crikey.
If it ever get's far enough to need a 3D guy, definitely let me know!
Very best of luck!

08-29-2010, 01:32 AM
wow - that sounds awesome ... and that could be legen.... wait for it.. .dary ;)
I'm very impressed of you even thinking of making such a complex program - really cool ... and I so got a vision of "settlers" when I read your text, always love those cute little graphics of the people going around doing their jobs. So WHEN you get it all to work - add cute graphics - then we can just sit an watch a city build it self :)
Really looking forward to seing how it progress... and dump some screens and stuff once in a while :)

08-29-2010, 04:43 AM
Good luck with this. Sounds very intriguing and I'm looking forward to watching it progress. :)

08-29-2010, 09:19 AM
I'm very interested in what you are planning to use for developing this system. I would love to see you keep a development journal for everyone to read and maybe share ideas. Have you picked a language or any packages to work with yet?

I'm thinking a system that takes a rough, low res height map + tile data and then generated refined grids only in areas your AI actually uses may be a good path. This could allow you to do fairly decent sized regions without needing the entire high-res data set to work with, so you save yourself some hard drive space. (Especially useful if you want to save milestones of your simulation so you can go back and nudge things later.)

Also consider how often you actually update a given AI entity. If an AI has their home, their fields, production levels, and trade agreements, then you can basically freeze the majority of their AI and let the program focus on the trade alone. Pathing between their usual routes doesn't need to be updated, just stored for density factor. (If too many other AI attempt to use the existing paths, then it may trigger an upgrade to the road, or force other AIs to move away/find new roads.) Focus on the 'settler' AI, and keep a list of established AI and only go back to them when they reach given thresholds of wealth/resources.

If you get too detailed too quickly, then you run into the "Dwarf Fortress" problem, where the system can bog down with only a few hundred agents running around. Abstractions are key.

08-29-2010, 12:43 PM
@crayons: Glad your interested. Your post was one of the set that made me think "Aw heck I just gotta start this". You said that you had the rendering of your city fairly well under control but you lacked the shape and road system of the town which is the bit I am hoping to generate.

@tilt: Sure ill put in some real basic pics in a mo if you like but there not much to look at.

@Ramah: Thanks

@Talroth: Ill be keeping my dev journal right here - its gonna be a big thread I think. I was initially thinking of developing it in Perl cos its all text and stats stuff but I know that when we get a lot of action then it could slow down and I thought that better prevent that now. So its all in C++. As said the guts of it is taken from my GTS app which is an experimental terreain generator. That was done for multi processor but I have already decided that this one is going to be single threaded.

So far I input a 2D base map which is just some color. I might be able to add the height map in later but for now I was thinking that we will just assume it will be all flat and not worry about gradients. If we get a cliff then ok I think I need to worry about it but for now getting a dude to take a drink and eat something is the first priority.

In terms of update rate I was thinking that one unit of time would be about 1/100th of a day or about 15 mins. In that time he could do something moderately substantial without being too fine. He might only be moving one square so if we need him to get to some trees, chop them and get back across the map he might end up getting hungry again. The unit of time is flexible and I will have to see how fast this runs.

I see what your saying about the pathing. I was thinking of assigning a role to a character so that once he settled into a role then he would be fixed. Like a blacksmith doesn't get a midlife crisis and want to start being a builder or whatever. Maybe I could recheck it just now and again. Once their role is fixed down then what your suggesting about not calculating the AI any more sounds like a good idea. One to think about.

I have a 'path' value for all the map squares so that as people walk over them it climbs up a bit and then every turn it drops a teeny bit. I was thinking if a cart rolls over it then it goes up faster. Character when moving from place to place are going to calculate their cost to move as a loss of earnings based on their personal turnover. So moving a long way costs a lot if the character is making a lot of money. In that case I am hoping he will then see the sense in buying stuff from people close to him at a higher price than he could get it himself if he didnt have to move but not if he has to move to get it. Then the 'path' factor for the squares he moves over biases the costs so that roads are cheaper then rough terrain. Again this is all speculative so I hope that works out.

I haven't heard of Dwarf Fortress - as you can tell I haven't done one of these before and it sounds like you have some experience so I will take what you say with some gravitas. I can see exactly what you mean tho in that when I have a big map and hundreds of characters then it will slow down. I am not aiming for a real time thing where we need to keep a game frame rate up. If I could calculate a years worth of map time in an hour then Id be happy. That would be 365 * 100 iterations per 3600 seconds tho. So 10 per sec sounds ok and that would imply that I should be able to get a few hundred or more characters in play.

I'm starting out with a 32x32 bitmap and the aim is to work with something like 512x512. Since each square contains quite a bit of info I think I could render the 512 squares into something larger with good detail. I would like enough res so that when it plops down houses and streets then it gives enough shape to them that we can render them out with some nice 3D or use the thatching scripts I developed.

Currently, when it saves the bitmap it will expand the squares to 9x9 pixels and mark on where the characters are too. So here is a pretty numpty picture showing my starting test patch of ground with trees, mud, rock and freshwater plus one guy standing at 10,10.


08-29-2010, 01:22 PM
Crayons was mentioning how deep the skills and items and disasters etc its going to run. Well firstly there are skills and I am saying that each race comes with 1 level in many skills. Humans start with 1 in farming, building, capentry, lumberjack, etc right up getting a 1 in blacksmith basic and miller. We would say these are unskilled tasks or semi-skilled. All the fine blacksmith work needs to be done by someone who gets a blacksmith advanced skill which you gain when you have say 100 pts in basic. So after a smith makes a easy cheap stuff like cartwheel hubs or whatever then the system adds one basic point. When he gets 100 it gives him 1 pt in advanced and he can start to make weapons or armour etc.

Goblins and other races will also have skills but not farming etc. I don't know what but they will be different.

Races have needs and desires of which the basic one is health. You must try to stay healthy. So no suicides in my land. You need to satisfy health before starting the next need and it goes down the list till you run out of needs and then your on to desired of which you can do in any order. Your character stats determine which biases to apply so that some people desire some things more than others. Wealth is a basic desire but there will also be piety, sex, destruction, battle etc.

People have items of which I can keep adding them. At the mo I am getting stuck with money. I would like for every trade to be done with money cos bartering is difficult to program since there will be some excess wealth on one side of the transaction or the other. The trouble is unless there is a mint there is no coin. Even if I said all money was abstract then who starts with some in order to buy something. Money must come from a central bank done with a mint - like a royal mint. Money is issued which drives up inflation and production of wealth drives down inflation. So I dunno - help ? Maybe I need to give everybody some abstract coin and inflate it across the board. So you have a gp in your pocket but lo, someone generates loads of wealth so now everyone has 2gps in their pocket without doing anything because inflation has gone down, your gp is worth twice as much. Or the price of everything just halved and you keep the same amount of gps in your pocket. Odd ? Possibly - anyone study economics ? I'm not used to a closed small system - esp one without a mint or a central bank.

Anyway, for now if noone trades then MrNewOnTheBlock has to chop some trees into logs and build a cabin, plant some seed and raise crops and drink from the stream to get his needs sorted out. Then he can worry about how much money he wants and whether theres any hot chicks to oggle at and marry or gods to worship or whatever. Were not there yet tho so no need for money at this premature stage.

Because Goblins have a different set of needs and desired, one of them I think will be the need for chaos which will be driven by the amount of ordered wealth he can see. So if I model a goblin village and run it in then they will see all those nice neat houses worth a fortune and try to destroy them. Then the people will hopefully see that goblins cost money and fight them or pay for protection. So yes, I think invasions and disasters are on the cards. I think thievery and other evils of the world will add to the mix as well. We may get public hangings for reoffending thieves.... Or maybe it will drive some able fighting soldier types to become bounty hunters and then maybe even a sheriff or some kind of structured law enforcement will arise out of it.

One of the things I am particularly keen to model is the idea of working for somebody where a cut of your profits go to your employer. I am keen to see how this affects the dynamics of a society. Another one which would be interesting but I think can wait is the idea of a global welfare, social services and tax system. At what point does it become desirable to have these in a village or town.

Lots of ideas but gotta get that single man to drink first. He spawns healthy but thirsty and hungry and those two trump shelter at mo.

Steel General
08-29-2010, 07:17 PM
This sounds like quite the interesting project, good luck with it! :)

08-29-2010, 08:34 PM
Yes, quite interesting. It does sound a bit like Dwarf Fortress only you are going at it from a different direction and interest. Still, you might be interested in checking it out. It's an Ascii game but you can get some graphic sprites to work on it. It generates a random map similar to what you have there only uses the ascii character set to indicate rivers, minerals etc. Dwarves have interests and jobs you can assign the jobs but you kind of want them to become good at something so you don't do it too much. Anyway, it might be worth looking at as I believe you can get the source code as well.

As mentioned already, this is an interesting project. It will be fun to keep watch.

08-30-2010, 08:41 AM
Ah I see Dwarf Fortress is not a type of issue but a specific package. I have been looking at it and indeed it is somewhat along the same lines. I think where they want it to be *is* the same thing except that they haven't got there. Here is a link to their dev list:


Now unless I am mistaken almost all of that is not complete. Am I reading that right ? So if that is the case then they have not got it such that people will plant crops, harvest them, possibly eat them or mill them and cook into bread. So these are the things that I would like to tackle first. I'm not getting the idea from their write up that they are using economics as well. For instance does the price of crops or bread drop when there is a surplus of them ? Also I can see more similarities where you get henchmen. This I would like to model but not in the same way. It looks like you get them somehow in their game whereas I would like for people to hire henchmen because they are cheap and offer services in the same way that people will trade for goods and they will use a paranoia or neurotics desire factor and they wealth sum to drive their need for more protection. So yeah, certainly parallels but I don't know enough about what they are doing to see whether its the same thing. It seems subtly different to me based on the things they are concentrating on. It seems like they are going at it from the point of view of a game whereas I am looking to make the map and list of people in it. I don't want it to be like a game. I would like it so that you enter the base map in and then let it rip for a few hours and come back to see a fully populated built up city with all the professions and people listed for it. Practically no user input at all. Then you can RPG in it to your hearts content afterward.

Anyone played it a lot ? Can anyone summarize if it does indeed have people planting, growing, farming, building etc ?

08-31-2010, 06:27 PM
You might look into academic use of agent-based programming, which has been used to understand the ecology of
human settlement during the medieval warm period in what became the US southwest, think Mesa Verde and the
Anasazi peoples. It treats the map as a productive surface of varying qualities, and set loose a small number of
agents that behave like a family of paleo Native Americans: gather food, fuel, build, garden, age, reproduce and
split up when a locality becomes too crowded. As I recall, there is specialized Linux software to host this kind of programming.

09-01-2010, 09:31 AM
Thanks Marc. I had not even heard of agent based programming. It seems this path has been well trod ! As you can gather I am not particularly into AI or game theory etc but just into the idea that this might be a good way to make maps. The wikipedia entries are a good start:


its fascinating how my language used to describe what I wanted is so closely matched to these articles.

I briefly considered using prolog as the main language since its declarative. Then I thought maybe I could write this in GnuMake then gave up on both ideas and reasoned that id need to write my own declarative language or engine to drive it in this way. And I can see that I am not alone there either...


which seems similar to what I am doing except that my notation is totally different (They even mention the rules being in prolog format). Its also interesting that I had in my mind some ideas about trying to reprogram the rules after this map exercise to see if it would predict real world economics and I can see thats what people have been doing all this time with them already. Yeah so thats opened up a whole can of goodness right there. I always seem to find this. I think of a really good idea. Wait for ages for someone to implement it and see nothing for years and then as soon as I start its pointed out that its been done to death. C'est la vie.

09-01-2010, 09:51 AM
As much as I very much appreciate the new info its taken the shine off my new results which I was quite pleased about.

If I set my newly spawned human male (Called Andy) to be healthy but thirsty, hungry, without rest or wealth and run it up then it gives:

Character Andy would like to be drinking water.
They need the following:
1.000000 Bucket(s) of at least 50.000000% quality Fresh water
which is available by moving 9,19 to 19,29.
and it is free for the taking.

If I make him less thirsty then:

Character Andy would like to be eating harvested crops.
They need the following:
1.000000 Sack(s) of at least 50.000000% quality Harvest
which would be available right here.
if they start by planting seed.

If I make him less hungry then:

Character Andy would like to be resting in shack.
They need the following:
1.000000 Shack(s) of at least 100.000000% quality Wooden Shack
which would be available right here.
if they start by chopping down trees.

and if rested then:

Character Andy would like to be building a shack.
They need the following:
1.000000 Arm load(s) of at least 100.000000% quality Timber
which would be available right here.
if they start by chopping down trees.

So this is all ok for the time being. In the last case you can see that he wants to build a shack which starts by chopping down trees and misses some stages like planing the wood into planks. So its doing a search through the possible transactions forming possible paths through it. So whilst he cant build the shack in one go he knows thats what he wants to do and how to start doing it. Its like this that the language should really be declarative like GnuMake or prolog. All through the paths its tracking the costs involved at each stage and trying to work out the best course of action based on his needs and desires. So hopefully soon ill have him walking to the water and therefore generating a bit of path.

As you can see its looking quite likely that as soon as he has taken a drink from the river he will plant crops right on the waters edge. Im not unhappy about that since you usually camp next to a stream and I see no reason why thats not a bad move. Of course he will have a longer trek to the forest if he wants wood but so be it. We will probably find that he builds his hut right next to the wood and ferry between his crops & water and his shack and the wood. Those building closer are probably going to be more productive.

09-02-2010, 03:56 AM
so when andy gots his drink and food an shack (not the naughty one) - he just need a table, quill and ink and he can start mapping ;)

09-02-2010, 05:34 AM
I've been wanting to do something like this, to the extent of starting making notes on ideas. Nice to see that I'm not the only one, and also that someone is having more progress than me.

Keep at it, sounds like you've got a good thing going. :)

09-02-2010, 05:44 AM
Thanks Alfar. Its going to be slow progress for a while until the engine is more complete then I will be able to add more interesting stuff and see what happens.

so when andy gots his drink and food an shack (not the naughty one) - he just need a table, quill and ink and he can start mapping ;)

Heh yeah that would be good AI wouldn't it. Computer spawns new villager. Computer applies skills to him including level 30 in cartography. Computer makes request of villager what kind of village he wants. Save & Exit !

On my Dev list I have a few immediate things I need to do. In order:
1.) Get human to work out best path to stated transaction location.
2.) Get them walking along path adding to path amounts on the map.
3.) Making character perform the chosen transaction when at the transaction location.
4.) I need to group similar items together into collections. This would allow me to set a requirement of more than 1 arm load of planks to make a shack for example.
5.) I need transactions which don't result in a desire change or an item being produced but are events. One of the events is to modify the quality of an item. I.e. If they are planing wood into planks then the planks should start rough and get better. Or crops get tended to grow faster, or patch up the shack into a better one.

Dunno when I will be able to do some of this next.

09-02-2010, 12:34 PM
One aspect you seem to be preparing for, but not ready to implement, is waste production/disposal and sanitation. If that's the case, great. If you forgot, don't forget he'll need to visit the privy every once in a while.

Also, you had wanted to avoid barter and try money (without a mint) because of the inequalities of trades in barter. Would it be easy enough for you to use the barter method with a minimal amount of money? If everyone had needs and everyone is trading something they have for something they need and the difference in relative values is made up of money you won't have inflation since the money is just an equalizing value, not an absolute value itself. Of course then the whole rich paying for goods and services thing stops working.

Alternatively you could have money enter like it would have for most cities, minted off screen and brought in by a merchant who will pay near the going rate for goods (sometimes more, sometimes less). Once people get prosperous enough, they would rather buy the goods off of the traveling merchant than work for them themselves, but local inflation would still happen a bit between merchant visits. If too much cash is flooding the village and there isn't any food, people are less likely to sell their grain at any price.

I think what makes the most sense, though, is a barter based economy with something (probably grain) required the most and the easiest to produce so that it can also act as the base monitary unit. (i.e. for Andy a log is worth 4 grain, plained wood is worth 7 grain and a full house worth 30 grain.) Since it is consumed regularly, you wouldn't have inflation.

However you do it, you would also likely want to have a way for the AI to own/control the land and buildings, so that the person who owned the lumber mill could get a cut for it's use, same for the person who owned the field, the flourmill, the distillery, the bakery, blacksmith etc. All of these functions the starting AI could do on their own in their hovel (except produce grain), but the AI skilled at the task could do it for cheeper. Perhaps even include skilled carpenter/builders who are the only one capable of building/repairing special buildings like the mills and blacksimiths, that way your first settler doesn't build a hovel and a blacksmith shop, and a bakery and a... These special buildings could accelerate production times or decrease production costs (or both) over the work from hovel model.

You've probably considered all/most of the above, and if so then I'm just feeling all excited for you. I hope that if the concept reminders were unneeded, at least that excitement is conveyed.

09-02-2010, 12:35 PM
This might be rather obvious and/or you've probably had thoughts on this already but...
I was thinking about "Andy's" decision about where to build his home. It's a fascinating decision and since it has a large bearing on the "map" concept of the model I thought I'd put in my penniworth on what might affect that decision.

Since Andy lives alone his thought processes would be "fur trapper" like. Self preservation and self sufficiency would be prevalent - he'd probably have his hut by the water and his crops (to keep animals away) and trek for wood. - he doesn't really need to protect the wood! His needs for wood would be large (capital investment) at the start - to build his home, and then just for fuel and tools etc at need - a "gathering" type exercise rather than a crop?
However, as part of a community he could specialise, a farmer or a woodsman or fisherman, and trade/barter straight away for his house... His economy becomes more cash "crop" oriented.
Andy the Fur Trapper would do this sort of thing with the "external" world but the value/profit of his "cash crop" is diminished by distance unless his is the only forest around! Or he has something else equally "unavailable" (fur) and so on.
Initially, I think I'd approach the model from the fur trapper perspective (as you have I guess), treating subsistence as mandatory and then let him stockpile "profit" in terms of his cash crop. His decision on improving the quality of his cash crops is probably secondary to that? Ultimately, though, he has to make a decision on his "trade", what his main product is, I think. Improving "quality" is a factor dependant on that specialisaton. To make "planks" means diverting time from just hacking down trees. Time that is not (initially) profitable - indeed, to start with he will have to "scrap" his failures (e.g burn the wood, or eat the gritty flour himself!). This in itself is complicated, do you intend to include product longevity? Bread goes off, wood rots etc?
I'm sure this is all "running" before he walks but I can't help thinking about this project!
Don't feel deflated if someone else has already done something similar, most things have already been "done", but not always the same and doing it yourself is so much more FUN !! Andy is your own little man working out that he needs to drink and so on!
Personally, I'd just love to see height fields brought in as soon as possible for the movement decisions, I'd love to see how it creates desire lines! However, don't be lead astray by such importunings!

09-02-2010, 02:18 PM
When I was talking about Dwarf Fortress, I was merely talking about how poorly it preforms as more and more actors are added into it. From what I've seen of how it preforms I would bet it stems from poor memory usage as things expand in 3D arrays. Actors need to be strictly limited to how far around the world they can look, and you may need to do some work with how information is looked up and shared. (ie, plan to allow main roads with path finding nodes to be put in place. Preprocess commonly used paths through your road network. Consider path finding from both ends of a problem with goals of finding paths to central travel networks. If an actor wants Wood then look at main wood sources and see which is closer to an existing road. Only open up roads to new sources when old ones are beginning to deplete, or if an untapped one becomes far 'closer' to get to. Things can become 'closer' because of congestion on roads, or expanding away from an existing source.)

By relying on pre-paths, you can greatly reduce the amount of actual processing done.

Another option is to avoid doing 100% precision simulation, and using a list of action queues. Actor John wants wood, and it is sourced by walking to Road Node A, then to B, C, D. John plans this, stores it in his personal action list. Action Queues can be sorted on the length of time a task will take to complete, and so an Actor will be placed in the Queue that corresponds to how long their next action will take to complete. This way the computer never looks at them again till enough 'ticks' go by for their action to finish.

In the case of John you can either split it up as an entire trip and action, or at way points. I might be tempted to go with checking at each way point, and then flagging the road he is on to account for traffic evenly, rather than having to calculate out when which part of the road is occupied.

A "Personal Action Stack" could be something that has a base case like:
"Calculate Next Action"

When the system pulls an actor off the current tick's Action Queue and sees the "Calculate Next Action" it can run the full AI on it, figure out what it needs, possibly looking at what it has done lately, and pick an action for it:

"Gather wood"

What does he need to do to gather wood? Path to wood source. Then the computer finds a suitable course for him to get to the wood and your "Personal Action Stack" for that actor looks like:
Walk Node A, Walk Node B, Walk Node C, Walk Node D, Gather Wood, Calculate Next Action.

Walk to node A would take 5 Ticks to do, so he is dropped into the Action Queue for 5 Ticks from now. 5 Ticks later it gets back to him, pops "Walk Node A" off his stack, updates his actual position, calculates how long to Walk Node B, and places him in whatever Action Queue for how long it takes.

09-02-2010, 06:11 PM
If I describe what I am doing in a little bit more detail then I think it will answer some of these questions.

I have a creature list with one item on it called Andy. All creatures have stats but I am not getting too bothered about them at mo. They also have skills which allow them to do certain things. They also have an item list of things they own on them personally.

There is a map where each square has an item list which could be blank or might have say a tree on it as an item and it has an owner which starts with nobody. The square also says what basic kind of terrain it is. Land, water, sea etc.

All items have an ownership one of which is the 'nobody' case which trees start out with. Each item has a quality percentage factor and a quantity. There is also an items stats list with an entry for each type of item. So you would have one for trees, one for logs, one for bread etc. Each stat contains the base price of the item and the prevailing current market price for it.

Finally and crucially there is a transaction list. Each transaction has a required type of ground param, a required skill list, a required items list. It also has a list of consumed items and a list of produced items and it can be set so that it produces a satisfaction of desire amount instead. I have discovered that I need an even production as well but lets ignore this for a while.

To start with it loops over all squares in the map and modifies some of the params of it. All items for each square are checked and if it finds some special cases it does some things. If it sees crops then it increases the quality of them. If it gets to 100% then it replaces the crops with harvest. Harvest does indeed start dropping in quality by a bit cos it does rot. If that reaches 0 then it removes the item off of the map and marks the square as not owned. It also marks down the path factor for it by a little bit.

It then loops of creatures and looks at their item list and also rots harvest and bread. It marks him as a little bit more thirsty and hungry and should up his health by a little bit too. Then for this character it works out which items he can get availability on. These are items he can gather (or buy if he had any money) if he goes to a place and just takes or trades it right there. After that it runs thorugh the transaction list and looks at them all ignoring any transaction he cannot possibly perform because he lacks a static requirement like a skill or there is no place on the map it could be performed. It also temporarily ignores the transaction if he hasn't got the required items for it. Some of them will be satisfied (like planting seed) so that he can potentially obtain crops. Then it repeats the search for all the ones he lacked only items for and checks them again. Now it sees that because he can potentially obtain crops it says you can get harvest. So now he can potentially get crops or harvest. Once this has completed he has a list of items he has, he can get or can get if he makes a few transactions. All of the costs involved are managed so that by the end he knows how much consumed items and how far he has to go to make a shack by planing wood by chopping trees by moving to the wood. If the guy is making a lot of money generally then he has high personal turnover so each round spent doing something is costing a lot. For a subsistence peasant its not costing him much at all. So the value of a shack divided by the cost to make the shack depends on a) the prevailing value of shacks, b) how much this guy needs a shack and c) how long it takes him to make a shack. It does that kind of calculation over all the items and stages to work out which one gives the best return on time effort (where time is money anyway) or investment.

Now when I was coding it I got to the point of the valuations and I did indeed think ok so what happens if this guy has something which is costing him more than its giving back in return. I though of a house which needed upkeep but was not generating good rent cos maybe there's too many empty houses in the village. He would not value it highly but what if it was actually a negative value. So I can see where taking a dump leaves you with an item which needs to be disposed of ! That sounds purile but it was the number 1 reason why cities maxed out in size in the middle ages so I think it has to be considered !

Ok so onto specific questions...
I would intend skill to be used in one of two ways. Either a) when you create an item via a transaction it generates it with a quality based on the skill of the person doing the transaction and b) id like to do a transaction where you can improve the quality of an item by a specific amount in one turn based on the skill level. Therefore, when a skilled carpenter makes planks he could make them in 3 turns but a noob makes them in 10 so planks to a carpenter are worth less than a noob so that would imply the noob might trade with the carpenter since there is a mismatch in the valuation of the goods.

For bartering, the coding is easy as its just a substitution of items and ownership. The difficulty is in the decision process to do it. I think that if two people value items differently then its easy as Andy goes up to Bob and says I want your planks for 20gps and just does it. With bartering we have to find two items, one on each side where the Andy's valuation of one of his items is worth less to him than one of Bob items but at the same time Bob has to think his item is worth less than Andy's item. In which case they swap both thinking they got a good deal. Which, if Andy is a carpenter with planks and Bob owns lots of chickens with more eggs than he knows what do with them then a trade like that is perfectly reasonable. So I think its doable but I just gotta grind out the item comparison for every creature in play. If we add a merchant into the mix with a cart load of stuff not obtainable in the game up to that point then I am sure he will make some trades with the initial price being daylight robbery.

A real issue looming on the horizon tho is what if Andy is not a carpenter but has loads of planks and is trying to build a house. Say Bob is a potter and can make tiles. Does Andy value the planks low cos he has a lot of them ? Is he going to trade planks for tiles only to find himself short of planks now to build his house. Future speculation is the challenge.

The future speculation thing is at work here too. Currently, and I have no idea how to move beyond this bit, every creature makes the move or transaction that boosts his standing at the time of calculation and based on what is available at that moment. If someone is in the process of building a smithy then a canny businessman might gather resources for items which the smithy is key to making such that he is the most ready, first to market and thus doing the daylight robbery. Putting that level of AI into the system is something I think ill wait for a long time. In that same sense he would likely build that hut or shack at the place where he is first in most need of it. I.e. if he is tired he may go on sleeping rough and getting by just enough to chop trees and plane wood. The moment he can build the shack then he does so and sleeps in it. I dont think he will consider the future costs involved in tracking to and fro the shack cos its in a duff position. The idea is that some people have made better decisions than others and will progress faster. Sort of like genetic algorithms and the Darwin's theory of selection at work.

I havent added fur trapping or hunting in the forest for food but these could be added. A probability of getting some food per turn spent in a wood area. Same goes for fishing too. Also to be afraid of the forest I would need to put in a -ve adjustment for being close to it. I could also spawn nasty creatures like gobbos from the forest regions which might hamper those who live close and thus the Darwin selection would favor those further away. Or it might make those near the forest good at battle and become soldiers. I don't know. Id prefer to run with the latter idea as its more generic to the engine and does not introduce another special parameter.

Certainly distance to forest and all items is a factor in valuation so long as the character has a good personal value turnover and thats easy to keep track of as a rolling average.

I haven't tried Dwarf Fortress but it says it runs on some ludicrous sized game grid. My intentions are that this should be run on about 500x500 so the map scanning should not be too severe. Its a localized not world sized thing. I would rather have more resolution than more physical distances. Ideally id like a mile or two of area and I thought that a square should be 5ft or perhaps 10ft which does make for 1-2 K square of size. Beyond that I think people would adventure, travel and so on not so much build their village, town or city.

In terms of path traversal I would not have a distinct network as such but rely on the path parameter of the map and ensure that the character must walk each square looking for the best next square. Maybe that would not be good enough and he could get stuck but I need something where each square on the map is considered for it value. All roads decay back into dirt if left alone and get more path like if walked over.

Your ideas about grouping actions and waiting the full duration with no processing is a good idea but the characters can change their minds about what they want to do depending on market changes. I'll bear it in mind. Obviously we need to see how fast it goes first after a little straight up optimization before I worry about this sort of thing. If I have to tho, this is a good plan.

09-02-2010, 08:41 PM
Done dev item 4 so now if rested it looks better with:

Character Andy would like to be building a shack.
They need the following:
100.000000 Arm load(s) of at least 100.000000% quality Timber
which would be available right here.
if they start by chopping down trees.

09-02-2010, 09:27 PM
This is fascinating stuff, Redrobes. Wish I could help out by you're out of my depth. I've played around with artificial life programming a bit but Ai is beyond me.

09-07-2010, 06:29 PM
Dev Item 5 done now - at least for item quality improvement. So its making a much better assessment of the amount of work involved in getting a hunk of rough raw materials into engineering materials. A shack now costs about 20K and is worth 10K so this is a little more like it. Now, Andy would rather sleep rough than spend 20K on a shack to rest in at this stage and when all fit and rested he is opting to chop trees instead of building that shack for his primary source of wealth. That's also more in line with expectation. I could see Andy the scavenger with his axe and handful of grain thinking ok what to do ??? I know, lets start building a full on castle all by myself cos they're worth a fortune ! I am hoping that as he keeps chopping wood and since he owns the wood then he will eventually chop enough down that the cost of the shack will fall until its stable 10K value / falling costs becomes something that will be his primary focus. Also, the idea is that after some chopping the price of logs should drop so maybe he might decide that chopping trees for the sake of it is not worth it but then the value of the shack would become dominant and that he would then chop trees in order to make the shack even tho logs are cheap. This is where it all gets interesting and only then will we know whether this is an idea that we can use to map the town.

After I get him moving and performing these transactions I think I need to adjust the prices of items to see if he changes his mind about stuff he has a choice in.

09-08-2010, 03:12 AM
sounds like a sound decision - he's a smart guy that Andy fella :) ... I'm so looking forward to hearing/seeing more :)

Steel General
09-08-2010, 08:12 AM
Just wait until Andy gets married! :D

09-08-2010, 12:18 PM
Lust is definitely on the agenda...

Been doing a teeny bit of UI work today so I can output the map state with a little more info on it. Really I need ViewingDale cos there is too much to fit in so I would like to zoom in and see all the items dropped at a certain point but its a lot of faff and id like to be able to make movies of the actions per progression / turn. So enhanced the bitmap output to this. Now you can see a tree on all the green squares and dirt on the brown, rock on the grey and water on the blue. If he chopped a tree down then he should produce some logs on the map. The icons are done but ill wait until it actually happens and then you would see them all. I need to make a key for the items I have so far. But I know you all like some pics and this thread is rather lite on them so far. Next stage is get him moving to the water by finding lowest cost route. This is probably something I expect many people have programmed but ill take a simple approach for the first stab by making him go sideways or diagonal depending on the amount he is off and the path values to bias it and stuff doing an overall path search at this stage - but I know I will have to in the future or else he will get stuck in a corner of ground that cannot be traversed. Im more interested in getting the economic bit going right first tho.

Oh yes, also doing some timing and its currently going at 200 turns per sec which is a bit slower than ideal but alright for the moment.

09-08-2010, 01:33 PM
yeah.. a picture (and thats worth a thousand words, so you saved a lot of work there) ... looks cool, a bit way to go yet for the settlers graphics *lol*

09-08-2010, 02:40 PM
This is incredibly awesome.
It's really nothing like DF, and unless you want to show all the things happening in real time, I don't think the time it takes is that crucial. If you are using it to generate a fantasy city map, based on economy, for use--for instance--in a video game or rpg (obviously the user of the final map probably has to spend some art time making it appropriate looking for their game--but it's a base, of a realistic city, based on the need of it's original inhabitants and it's immediate environment), then it's totally different from DF.

Personally I'd be very interested in seeing something like that. Especially something that iterated over time, so you can input, say, "100 colonists, wealthy, very religious" for your people, and for your starting environment, "Harbor, forest within 2 km, temperate climate", "1000 years later, no major wars or invasions".

Then from humble roots, you'd have an actual, working city. Obviously you'd need some random factors (people dying, philosophies that can be interjected, such as "planned city" vs "organic growth") but it would make for a wild program.

09-08-2010, 03:25 PM
I always like seeing people with 1 post and join date of a year and a half :) Welcome to the guild streever ! Yes thats exactly it. We have oodles of techniques around here for taking an existing layout and souping it up into something which looks fantastic but were always missing the layout and once you have the city we all have to populate it all up. So this is supposed to fix the two bits were missing. Crayons has some good POV ray rendering for buildings going on and I did a nifty script based thing to thatch a set of height mapped buildings with all the thatch or roofing tiles looking right for the building. So we need to know where the building are located such that its still possible to get in and out of them sensibly and they all ought to reuse existing walls to make terraced housing because walls are expensive so reusing them on both sides is much cheaper. Very difficult to tell a computer where to put them down without the context were building up as part of the AI growth progression.

At mo I have a spawn human percentage and I have hacked it so that its either off or on because I am in debug and want one or not but the idea is that people will randomly and very occasionally spawn out of thin air but on the main be born from lustful meetings of people. People will also have a random chance of dying based on some simple actuarial graph but again I expect that on the whole they will have a substantial chance of dying due to battle and accidental losses.

You wont put forest within 2km but input a really simple terrain map with no man made stuff on it. It will determine where the trees and harbor are. I think for the time being there will only be temperate but its being programmed with hard coded but modify able rules in it. I.e. it would be easy for me to add them. I don't know if I need rules to say if its cold enough then you can build an igloo etc but I see no reason why that could not be done.

In terms of growth and planned. I think there will be a point at which we will see that there needs to be a village council, town council, city admin etc. At some stage there would be enough of a need to protect the city from invaders and permanent manned soldiers need to be on guard. Who is going to pay them ? It has to come from a city wide tax. So no tax no central protection and you rely on mob rule. So tax is something that I am keen to discover if I can model to a point where we can see its useful. Once you have a big enough tax and central government then they might have enough clout to buy out houses and trash them in order to put a new road in. I may ensure that their value bias for roads is very high for example. It may need a type of human with some different skills in it like city administrative stuff in order to trigger new transactions which no one else can do. But since its kinda programmable then I think thats all on the cards if a long way off at this stage.

The one thing I think I will never be able to fix is future speculative thinking as outlined in the above posts. So I don't think that I can get an admin to put a whole new road down because, speculatively, he would believe it would help transport around the city and increase its GDP. But he could take any road of at least 50% quality and apply his transactions to that and boost all those roads and get them all cobbled and paved into highways at the expense of all houses currently blocking them. I can make him super paranoid so that he hires masses and masses of henchmen to protect him and therefore create the standing guard. Not sure at this stage how at this point that I can make anyone build a wall around a city to speculatively protect it in case of future invasion.

1000 years is quite a long time. My turn is about 1/4 hr or so and it looks like its going to calculate one man on a 32x32 map (super small) at 200 x 1/4hrs per second. So 2 days per second per man. There's still a possibility that I could go 4x that speed with a quad core etc but for now its single threaded. We'll worry about performance when I have it all going to a certain acceptable state.

I will do a icon key shortly but gotta shoot for a while...

09-15-2010, 06:21 AM
Ok I got some good progress late last night with a sesh on it. The guts of the engine is going now. Its not all that elegant and I will look at refactoring large sections of it but it proves the point that its doable. I have Andy going around and using his skills and making stuff and its changing the perceived values of goods and adjusting the path behind him as he goes. I have it so that he bashes up rock into aggregate, shapes them into stones and improves the quality of them. Chops trees into logs, planes them into planks. Plants seed, tends crops and harvests them. Then he also drinks, eats, and sleeps. Eventually he builds a shack ! I have also been optimizing some of the worst areas of it so its running at about 1000 progressions per sec again though I have to add some more chronic slow code in for the best path algo. I have a neat idea to speed that up tho when it goes in and I am having some good thoughts about trading based on an Ebay type approach with a public auction for all items so that there is a bidding system in place which is much more like real life with the stock market. One assumption I had was that two characters would have to be at the same spot to trade but that prevents a market type auction so I think ill make two people meet to trade but the bids can come in across the map and if he is outbid then he does not trade. I.e. Andy and Bob are on the same square but Charlie says "if you have any X then ill give you Y for it" even if Charlie is not in the transaction the information about possible sales is known to all so that he would not have to be involved to still affect the outcome of Bills sale.

Anyway. Heres the latest map and theres a movie ill try to attach. Its only a couple of meg and worth watching if your interested in this thread. Its XviD as usual so use VLC if your having trouble getting it to play.

Oh yeah I changed the original simple map so there was less border so he stays in the middle more. Its attached as well.

09-15-2010, 10:41 AM
That's absolutely brilliant!! I've watched the vid several times and , while it's no "Casablanca" :D it's fascinating trying to deduce Andy's motivations!
I'm wondering ig the path "fade" time is a bit fast? As it is at the moment he may not develop multiple paths? Would I be right in assumming that a "developed" pre-trodden, path has a saving of effort for Andy?
It might also be interesting to develop "soil goodness"? Soil near water being better than further away - and maybe factor in soil usage - so that Andy might start "rotating" his crops about a bit?
I say this as he seems to have settled at the junction of 3 "natural" commodities and walks to the fourth. He plants on the path to there. If he were constrained by soil quality to plant nearer water it might open more paths?
Just my thoughts!
Speaking of which - I'd love to be able to see Andy's thoughts. Some of those God games give the little folk "thought bubbles" - Theme Park, Theme Hospital etc. A status bar at the bottom might be nice with his current "desire" factors perhaps!?
Keep up the excellent work! I may not always feel qualified to comment (most of the time probably!) but I keep coming to this thread to see if something new is showing.
Are you going to add a Mrs Andy at some point?

09-15-2010, 11:07 AM
Yes the path fades a bit quick and your also right in that its going to bias him based on the cost of taking the path or going cross country except that I haven't coded that bit yet and he takes the most direct route all the time so it does not build up much. Also I think more people will trod it down a bit more. The path coding is quite involved so im leaving that for a short while but its soon on the list of things to do. Maybe I should do that one next.

The soil goodness is a great idea. Id have to add a new custom variable tho. At mo he plants seed and then it grows slowly. He can make a tend crops transaction to make them grow faster so you will see it change from the yellow stripes of crops to the green hatch of fully grown harvest. If the harvest is not collected then it drops in quality progressively until it rots into nothing and you see them vanish. So if it rots then it could stay at 100% but if harvested then it could drop 20% or so and modify the growth rate. If he is really hungry and has no more carried harvest then you will see him plant crops like crazy until one harvests. So with poor soil he would be forced to plant more and more to get one harvest. All non planted dirt could gain quality slowly.

A Mrs Andy... Andi ? ;) Yes in time. At mo there is no gender differences at all so it makes no difference. Id be more keen to see Bob (or Bobbi !) make an appearance and maybe there would be some chance to trade / barter and see how two people together interact with stuff.

I could add those status bars for one chosen character at the bottom I think. I reckon that would help debug it a bit more. Another set of stats that would be good to see is his valuation of the market. As he cuts a tree down he values logs less and then makes planked timber instead and then values them less. Then he builds a house then suddenly he gets all interested in timber again.

By the end of this run its hard to see what he's going to want to do next. He has a house, crops and water to drink. If I run it longer then he builds a second house. So its probably time I expanded the list of items and transactions too so that it gets more interesting. For example id like to add "extend a shack" to make it bigger or improve it till it looks like a house. I want to start seeing streets appear but I think were a long way off of getting to sensible street layout yet.

09-15-2010, 11:43 AM
Cool stuff. Very fun to watch the behaviors...please post more if you get interesting stuff up.

09-15-2010, 11:51 AM
Thanks, ill get a bit more going then ill post a beta just to play around with. Remember theres no fancy UI or source code for these tho. It runs a script and then you can ask it to boot out an image like the ones I post. But you can get to see all of the parameters and desires etc. Its not all that useful at this point.

09-20-2010, 09:37 AM
I have some more anims to post as I was working on it again last night. This time tho ill give you some links instead of attaching a zip as they are about 2Mb to 4Mb each.

This time I debugged it and optimized it a bit more to handle more than one person. So this is another anim for 1 person with the new code and then same code but with 3 and then 10 people in it. This time however they have all been speeded up 10x so you get a frame for ever 10 turns. Its now generating the data faster at about 1000 turns per sec but if I have to set it to export the image each frame its a lot slower at about 10 per sec. Still, speed is not looking like an issue now.

A couple more things. I have marked squares that are owned in a red border. So you can see which ones people have reserved. Sometimes they pay attention to reserved things and sometimes not. Its based on the actual transaction being done. I have set the bashing up rock to aggregate as being an item which is now owned so that they all need to go to a different heap and do their own work now.

Anim4a (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim4a.avi)
Anim4b (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim4b.avi)
Anim4c (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim4c.avi)

Its interesting that they seem to generally clump the houses together but thats not been programmed in at this point tho I will be adding code so that its cheaper to expand an existing house rather than build a new one. Also they are still not following pre-trodden paths yet.

09-20-2010, 10:14 AM
And it's going to be totally AWESOME

09-20-2010, 02:13 PM
one could also argue that sense of security, social interaction and easy of trade would be reasons to live closer together :)

09-21-2010, 12:30 PM
Because I know you like more movies...

I put in the code to make people stick to paths. Its using a brute force technique which should work all the time and produce optimal results except that I cheat with the algorithm to make it a whole lot faster by dropping one of the outer loops which to those programmers out there will know that means a lot. So its not optimal results any more - just pretty good. Its doing 400 turns per sec with 3 people at mo.

The first is 5 characters, 1 frame per turn.
Second is 3 characters 10x speed
Third is 3 characters 100x speed so we can see what happens in the longer term at the location.

Anim5a (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim5a.avi)
Anim5b (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim5b.avi)
Anim5c (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim5c.avi)

Next I need to put in some crucial changes to the pathing. A) People should not be able to build a house on a path and B) people should not be able to walk through someone elses house. Then we might get some form of streets forming. Its from here that things start getting exciting !

09-23-2010, 04:56 PM
I'm loving this!
Some thoughts.
The food thing is rather too "easy" I think. At some point I feel that there should be seasons and stockpiling of food - fields need to become as permanent a feature as buildings (almost). Many country lanes end up following field boundaries (when they aren't following contours, rivers, sheep on a grass "high" and so forth) - as you know! Once fields become more permanent "features" then concepts of crop rotation can come into effect. Obviously traffic shouldn't cross fields with crops but could use fallow fields - thus permitting road evolution to occur more slowly in agricultural areas.
I think the roads should persist even longer.
Besides not allowing traffic (therefore roads) through buildings, the opposite should also apply - no building on roads (of age "x" TBD)?

09-24-2010, 06:54 AM
I agree with you though I think a field for one person is about all that you need. When the size of the map increases then I think we will see more people needing food. Its very hard to persuade the app to do anything speculative so storing food is difficult. The only way I can do that is to make the cost or value of stored food more than current harvest. Also, when I get a mill it will start consuming harvest at a larger rate to provide flour so I think then it will force farmers to plant more and you will see more squares in crops.

On the houses and paths front I agree. I said that we need two things. No houses on paths and people cant walk through houses. I will try that out real soon and see how that goes. I will also much increase the cost of people walking through a field but not stop them doing so. Then maybe that will prevent planted areas becoming paths and hopefully steer people around them. Might be able to try that tonight.

Its a dilemma whether to stop a recently used field being used again as a field to make it a permanent feature or whether to down grade its potential because of the requirement for crop rotation. I also don't want to have them so permanent such that a growing village will wrap around fields and thus have the village green being a cabbage patch. I think that tho people should walk around a field if possible, if they do walk over one then not only should the path value increase but the crops on it should decrease. If someone plants a field right on a narrow opening then people will trample it all away rapidly.

09-28-2010, 02:42 PM
I have another movie. This one has houses preventing the non owner from being able to pass through it. Also the houses cant initially be built on a path. I have added some more code where fields also now partially prevent access and that crops now grow in two stages. One where the farmer can increase growth rate then it goes into a phase where they cant. It forces them to replant a new square instead of standing on it tending it to completion. I added some code but had to take it out again but I will recode it differently such that houses decay and can be upkept again. In this way, houses that are not visited by the owner decay. That may be the case when the area is so built up that he cannot get to his house. This is how I would like to guarantee access to all houses. I am also now counting the access to all materials so that characters bias what they are doing based on the ability to get at more of the stuff. Therefore they are prepared to lose common stuff for rare stuff or want stuff they don't own more. This, of course, is in preparation for trading which I see as essential to the cause here.

I like this movie because at the end it has some nice paths to the resources on the map. In a sense, some intelligent order has come out of the economics - something I am hoping that we will see more and more of, else this whole program is pointless. What were seeing as the intelligence is merely the optimization that comes from minimizing the expenditure of effort so I have something right there, Tho you will see people bouncing between two squares which is clearly wrong so I will have to look into why thats happening.

Anim6a (http://www.viewing.ltd.uk/Temp/CG/Economap/Anim6a.avi)

I thought id include a pic of the anim if left to continue until the trees almost depleted. Now that its built loads of houses they are in sort of streets or at least blocks of built up areas. This is why I need to ruin some of the houses so that we can form new streets. You can see now that the streets are well worn but they don't form any kind of sensible structure since there's no reason to go to and from all of these houses. Eventually I think this will sort itself out.

09-29-2010, 02:41 PM
It's definitely taking shape! Great stuff yet again!
I think you're right that things will start to gel more when the economics settles in.
How are you structuring the concept of accumulated "wealth" per individual? At the moment it appears that they all just build houses and this is fine if your 10 Andys represents more than that. If not then the housing price would be at rock bottom by the time of this end screenshot! :D You have a lot of very poor Andys!
I'm not sure building decay is necessarily the right mechanism for creating space for roads. I would suggest that the buildings would not be built in the first place? Keep it at one building per Andy and have different sizes (wealth) perhaps? In other words have 2 sheds by all means, but make them joined together and cost exponentially more?
Other things to throw into the melting pot? You've probably thought of these already?
What does a "community" usually build early on? A church? A marketplace? A community hall? A pub? People are a sociable bunch so you could consider building in such things. People also need leisure time and meet each other... a certain amount of "need" should be doing these things. Visiting other people's houses, going to the community building and so on.
I'm also thinking that to support this your Andys should choose a "proffession" rather than be Jacks (of all Trades!) which sets up your trade system. (This doesn't necessarily negate trade between two stonemasons, they just specialise in different types of rock, no need to model that just put that sort of trade as lower "need" perhaps.) Trade requires "meeting" at one or the others house or market, which means that people can't be out all day chopping trees, they also have to sell... and that's what shopping hours/market days are all about. I've worked in shops (jewellers as a matter of fact = rocks!), you can spend a lot of time doing nothing waiting for someone to buy something.
What criteria are used to decide where to build a house? Once someone's profession becomes, say, lumberjack, and all his other needs are bought then he wants to be close to the forest and "the shops"/market, he no longer needs to be near the stone field or any other raw source of "commodities"...
You also probably need to stick in some "logical thinking" - once a road becomes established enough and long enough then building next to it is far more logical.
Many towns are created just because the road was there and a commodity was there. I was brought up in just such a town, built on the old roman Watling Street (A5) and a good spot for clay and therefore a pottery...that's a theory anyway! A fair amount of your building "boom" seems to be "infill"?
Anyway, like I said, I'm just throwing in ideas here!
Keep it going!

09-30-2010, 11:02 AM
Cheers Crayons, Accumulated wealth: Yes its true that as Andy, Bob, Charlie, Dave, Eathan, Fred etc all build more and more houses their sense of house value drops. Its a symptom of the fact that there are no more transactions built into the system at this point. They cant go on to build a castle or whatever. When I add more and add more consumable transactions - like drinking beer etc then their wealth will get depreciated over time.

Right now all these people act alone with almost no person to person interaction. All the interaction is that one person can path beat and another can move easier across the map using that path and also when they build a house or plant crops then other people now cant use that square. But trade is the key to getting professions. All people at mo must be Jacks of all trades cos each man is an island. When they trade and their skills get adjusted by their successes of transactions then I think they will fall into roles. When a farmer can grow crops faster than the lumberjack he will have more crops and value them less till he sells them then the amount of crops he has will fall, he values them more and will grow some more. I.e. he will stick at being a farmer and Mr lumberjack will not grow anything but trade logs for food instead. In an ideal world - i.e. the target here - we want to add 3 people to the map and after a while one will be the farmer, one the lumberjack and the other the stone mason. Each will stick by their own resource and go off and barter for the missing ones. So we want the amount that a good farmer can grow, a good mason can produce bricks and a good lumberjack can produce timber to be all about the same wealth creating rate. At the start tho, everybody must be a farmer to satisfy their hunger and later on the chaos of the initial conditions will sort them into groups. It may also make people bunch up. Since two similar farmers standing next to each other would equalize their resources by trade. It may be tho that anyone else buying harvest will go to the one with the more harvest to get it cheaper and thus that farmer will be the one to get more (different -> valuable) resources. I don't know how that's going to pan out.

I will definitely add the transaction to extend an existing house so that it will be worth more with less cost than to start a new one. That is key to getting streets. Your right in that at present its all like infill. When a person has enough materials to make a house they look at the closest square that they can satisfy a house build from where they are not from the most ideal point.

I have the mill and smithy next on the build list but at mo no transactions to make one. But those will grind harvest into flour and the smithy will make what I am calling mechanical items like cart axles etc. It will be cool to turn planks into a boat and go fishing.

I would like to have shops too of course. The first will be a bakery taking flour from the mill and turning into bread. It would be better for people to buy bread than scavenge. Bread will go off after a few days but I am hoping that I can tune the costs so that people would prefer bread. Once its selling lots of bread then the mill will have a consumer of flour and thus keep the farmers all going. We need a similar sink of mech items to keep the smithy going. So maybe ill make carts out of them or that to upkeep the mill or something complicated requires nails or other thing all lumped under mech grade 1.

So with all that laid out. Do you have any ideas how I could bias the choice of a house build position ? It has to be done using numbers I have right now at the point of the build not a speculative AI thing about where you might think there is best chance of limiting the travel to and from the house. For example I could keep a long term weighted average of all the positions he has been so that its keeping a constant center of gravity type position of his routes and try to put the house as close to that as possible. That kind of thing ?

Yeah, this is where it all gets interesting cos most of the engine code is in there and we can mess about with all the tweaking to see what happens.

09-30-2010, 09:56 PM
Write your AI as priority preferences. That is when looking to fill a 'food desire', the AI first looks for a source/supply of bread, then failing that will fall back to looking for other supplies.

If you don't already then you may also want to keep a 'supply list', rather than any kind of method that looks at the world map first for resources. That way the AI can quickly skim a list to see if something exists before it makes a choice on if it wants to go after that, or fall back on other options, rather than scanning the entire map.

10-01-2010, 09:03 AM
I have something in mind for the bread priority. What I have is a need that must be satisfied so he will look at all the items he can eat whether he cariies them or not and if nothing then there is the path of planting crops and waiting till they harvest and then eating that. So he could do that, carry harvest, buy harvest from someone or maybe he can carry bread or buy it (or if he happens to be the baker maybe he can bake some). So in all cases he has a quantity of need to to be full up to get to 100% and the amount he is missing is the amount of value he will get if he eats. There is also his personal turnover which is the average amount of value he is making per round. So if he must plant the crops and wait then it might take 10 turns before he can eat so if he is completely destitute then no problem, he will just wait because 10 x 0 = 0. But if he is a tradesman or whatever making money then it will cost a lot to wait that long before he can get back to work again. Its the cost of his loss of earnings that's more costly than the food itself. So I am hoping that different people of different wealth will eat different things. I will make it so that eating a little bit of harvest only raises your full up amount by a bit so he needs to spend a few rounds eating whereas bread will do much more in one turn.

Your right tho in that I may have to implement a min level of standard of living so that if he is used to eating bread or his daily turnover is > x then he just doesn't consider eating harvest any more and we will let the value for money drive the max he can do.

I keep a supply list but I generate it per round per person so in a sense its one full scan per turn. I divide up all items into real ones and potential (virtual) ones. The real ones are all the things he can get access to whether he owns, he can pick up or buy. Potential are all the ones where if he does X then it generates Y so Y is potentially available. Like a shack if you have the amount of bricks and timber to do it with. I group all similar items together so he also knows the maximum amount of the stuff that he can get hold of at a push. I.e. if there are 200 trees on the map then there are 200 log transactions and 200 plank transactions from the logs. So working out if there are enough planks to make a shack is known. I refer to the list a lot but it is dumped at the end of the character turn - so not as efficient as it could be. My issue tho is that people will reevaluate the markets per turn so items he might have been able to trade with last round might be off limits this round. Also as everyone moves about the costs involved in obtaining all the items changes. I accept that this isn't going to scale well tho.

10-01-2010, 11:49 AM
Well, why drop the list at the end of the round? Just build and edit it as things are processed.

You may even want to try going with having a list/array of the types of resources your AI can use, and then having that list point to different kinds of structures to better sort out where the actual resources are. (Such as a quad tree or similar.) This way your AI actors can not only have quick and easy access to what resources are available, but the general location.

You could also sort some resources with a more complex data structure that will keep two lists, a primary and secondary source. Bakeries will be the primary source for most AI to buy bread from, and they will usually seek a baker first to buy bread from, rather than seeing if "random-dude-on-street-22" has any bread to sell.

10-01-2010, 09:27 PM
I have an item list per person built dynamically and also an item stats list which is populated once and contains all the static stuff about items. For example whether they are portable, whether you need to own the square to put one down, whether you get to own the square if you do, the set value of the item before personal preferences applied, that sort of thing. I can see what you mean and its a neat idea. This list could point to all the locations of the items for which it relates. I dig that. I can see how the quad tree would help too and that modifying the list is more efficient than building a new one. All of your points are spot on but solve a performance issue but I am more interested in getting the basic algo right than getting it going a lot faster. I guess if this were a games AI then I would need to do all of these things.

I am programming it based on programming convenience which is more lame than a true games engine I would agree but I need to get even more complexity in there yet and I would worry that keeping bugs out of the quad tree and getting the maintenance of the list exactly correct would be hard. Its iterative so if there are any rounding errors or slight miscalculations then they would build up over time quite fast. By dumping the list and rebuilding it I can ignore any incidental inaccuracies. My GeoTerSys uses a maintained set of parameters and these did gain and lose the most trivial amounts of stuff and it would kill it dead after thousands of iterations so I thought to avoid all that again.

At mo I am making a list of all items then looking at generating the potential item list by looking at all the transactions and seeing if they are possible. Your method is undoubtedly faster in that you would start by looking at the transactions and then looking at seeing if you have the necessary items from the maintained list.

When I build the list of accessible items when I see them I make a note of the location of the item. When I generate the potential item I make a note of the real item which is cheapest for that transaction and call it the key item. That is the item that I try to solve getting first if I choose that transaction. I.e. all items, both real and potential have a location specifier. When I choose a transaction I review the item list required but this time I have pointers to all the items on the system where they are located which was generated at the time to know if the transaction was possible and then see if I own and hold them. If not then I move to collect it or I have booleans marked to show I need to trade. So in short, I know what I have to do to satisfy this transaction if I choose it. Then its just a case of working out which transactions make for the best profit over loss. Then thats my choice and I go move, trade, or do the transaction as required.

You have probably isolated the point in my app so far that will be the critical performance block but for now I can live with less performance than a game might demand if it makes for simpler data structures. Id rather look through a simple list than traverse a quad tree and rather bin and start a new list than manage the maintenance of an iterative one. Do you see ? I understand you might take issue with that idea if you come from a game engine designer perspective and I would agree with you. I may rewrite the app when I know exactly what I need to do to solve it - then I think I would concentrate on getting it quicker. Perhaps then this economics based engine might be pretty useful to games designers or in a situation where it needs to be a lot bigger. Maybe that might be interesting to super computer type analysis of a very large data set completely outside of RPG maps.

10-01-2010, 11:30 PM
Don't mind me, I'm just throwing out ideas on how I would tackle problems. They might be useful later, or to someone else that comes along and tries a similar simulation. (Actually a lot of this might be useful to me for my Norse inspired adventure/farm simulator I'm working on.)

10-02-2010, 05:28 AM
Your points are excellent. I particularly like the one about collecting the items of a type under one grouping instead of in the one central bin of items. Usually I know what I am looking for at the point of search so a lot of my loops have the search_type == item_type kind of thing in it. If I looked in the right list then I could avoid all that so it was a great point and would speed it up for little change in the app.

Do you have any links to your farm sim ? Are you planning on using AI or economics to drive that or some other mechanism ?

10-02-2010, 02:28 PM
My sim is being built as a portfolio game, and is still in the early stages. It is going to be a layered goal based AI for most actors, while the player gets control over one family in the settlement. So, no links or anything yet, I'm still picking away at some of the core mechanics as there are a lot of little chunks I need to work away at. (Things like the tiling of the map for LOD, and procedural generation of building designs. It is going to be a free placement/movement map with a hex/triangle based height map for terrain.)

10-04-2010, 03:14 PM
So with all that laid out. Do you have any ideas how I could bias the choice of a house build position ? It has to be done using numbers I have right now at the point of the build not a speculative AI thing about where you might think there is best chance of limiting the travel to and from the house. For example I could keep a long term weighted average of all the positions he has been so that its keeping a constant center of gravity type position of his routes and try to put the house as close to that as possible. That kind of thing ?

I've have been thinking about this! However I can't really come up with anything you won't have already considered, I imagine. The following is likely very obvious but it's my thought processes...
Most of this is stuff I think you have on your "to do" list, and I'm really looking forward to seeing them arrive !!

Salent points?
I think a fundamental is the decision of profession.
In times of yore a persons workplace was also his home.
Most of a person's time is taken up with work (and sleep) - he cuts down trees every day but probably only goes to the "shops" once a week (say).
"Refined" products are easier to transport than raw materials.

So a person will generally live next to his raw materials. That is biased by
a) the quantity(/quality?) of raw materials present - ie. how long he can work the site - this is forward thinking but not really speculative - the quantity is measurable
b) "distance" from his customers - this is based on the "road value" factor - time and effort. This may be significant if aspects of "perishability" of the goods is considered. Grain can be transported further than bread. Bread goes "off" faster, for example.

What should transpire, I would imagine, is a radial pattern. Mining/forestry - any non-perishable product can be as far from the centre as it likes - proximity need is more about "shopping".
Long term perishables producers, eg. farms are closer.
Short term perishables, eg. processed foods, fish etc are even closer

So what is at the centre? The place where all this is traded.
Why is that place there? Because in the grand scheme of things that's where the next echelon of trade progresses. I call to mind the history of Stilton. It got its name from where it was distributed from, not where it was made.

On the detail side of house placement.
I think you might want to introduce building "sides" and "separation" - people don't generally build immediately in front of or behind someone else's house - I imagine there would be some sort of shouting involved! :D A "separation" factor would probably be required - this would be dependant on profession? A farmer is likely to shout much more about crowding than a blacksmith
One exception to this would be at cross roads, where building (say) one space away and behind someone else is "OK". I would imagine that the major of the two roads would get populated more first....? At some point adding to the end of this becomes "too far" compared to living on a "side street". A crossroads represents a minor "plus" factor for placement as it adds "value" in terms of ease of access, I guess.

How a farm doesn't end up in the middle of a town that grows around it? Well, for this you need land values. A "close in" farmer will sell his high value land and eventually move? I'm unsure about this.

Ultimately, I think the town has to have that focal point - the market. That, in turn, needs the external communications links.

"Common Land" - I thought I'd throw this one in here. Market's and parks and some other spaces could be designated as Common Land, this should add a certain extra permanency and protection for such spaces...?

Like I said at the start, I can't help you on the code front, but I hope this sort of logical assessment is useful?

10-07-2010, 07:54 AM
I started to code up the bartering but ran into a problem cos I only have the list of items per person one at a time. So the longer I do this the more I think Talroth is correct and that I need a general repository of items. Theres a coding technical problem with that with the way I have implemented it so this post is one for the C++ coders.

I was using standard template library (STL) vector class and thats convenient because it overrides the '[' and ']' operators to allow quick indexing ops. Great... but, if you want to specify the location of an item in some array then you need to specify the array and the index for it. There are quite a few of these arrays and so this is a bit of a problem. For example you might have to name all the arrays and then go and search which array you mean via some switch statement. Thats horrible. The obvious solution is to just have a pointer to the item you want to keep track of and screw which array its being held in. So thats what I did when I coded it up. Theres been this nagging little demon sat on my shoulder tho and it keeps reminding me that there is a big big problem with this. The issue is that if I add or remove any item from any array then it might invalidate the pointer because behind STL vector it reallocated the whole array if it gets too big. So pretty much, going with this idea I have to ensure that all items have been added to the array before I start grabbing pointers and I mark items I delete with a tag which says deleted but not actually remove it from the array. So ok cool.

But now I need to get pointers to other peoples items which will change if I now barter stuff so that when I calculate the next guys stuff it will change from the last time. If you see what I mean. Maybe its possible to code around this but I threw the towel in at this point and said oh sod this STL vector crap, it has to go and changed all the code from using the '[' and ']' operators to using the full iterator notation which is a real pain cos its so wordy and kludgy. So once that was done I binned the vector class and switched over to the list class. The advantage of the list class is that it never reallocates the items in the list so you can grab a pointer to them at any time and as long as you don't delete the item then the pointer is safe. But changing items in the list does not affect the other items in the list so all pointers to them are now ok.

I measured the completely unexpected drop in speed. Ok I expected a little drop cos you have to jump via a pointer from one item to the next but in general I am traversing all the items in lists in sequence so I am not continuously running through them in some random order. Perhaps a 2x speed loss. I was totally taken aback when I got about 10,000x speed drop !

So I dunno now. I think something very lame is going on and I may have to drop STL and use my own implementation of lists. I know that I can write a list class maybe a few times slower than vector but not this amount. When I do that then I can have a central repository of items where I can add and delete from it and keep hold of the pointers to items for reference later. I think there's something very broken with the MS STL list class.

So its being worked on a bit but not in the mapping sense for a mo while I get this knocked into shape.

11-04-2010, 06:52 PM
Im getting a bit of break from the day job which might not last but whilst it does I can continue on this for a bit more. I rewrote a large chunk of it to handle a pool of items and now that I know what everyone can see I implemented the bartering thing and that seems to be working ok. It does not happen too often but then I am only implementing a small number of people so it would probably increase with more people. The problem with this app is debugging. Its iterative and spread across many people. You have large sets of items which changes on each turn. Oh well.

I have another anim but its a lot like the previous ones.

11-28-2010, 12:48 PM
Sorry RR, I meant to reply earlier but I've been away for a bit!
It's still looking magnificent, but I think the road usage alement is still a bit weak. I feel that in order to gain some sort of permanence to roads they should have a much more beneficial effect for using them over "cutting your own path". Basically, at the moment the roads are what I'd call "out of focus" - the added "value" of worn paths needs a steeper gradient, as it were, to tighten up the focus?
I can imagine, for example, a situation where a new resource is starting to be used and an existing road can take you partway there for next to no effort and then you need to "cut" a new path at an angle from that to reach the new destination. If, in this example, the existing road has a usage value of zero then you'd go up that and then cut a road at 90 degrees from it to your destination? Reality is a number somewhere...
Please keep up the good work! I think you may be getting somewhere really useful for road layouts even if you might be having second thoughts about the economics???

11-29-2010, 08:01 AM
I know what you mean and I am in agreement with you in principle. I deliberately added the blur to the roads to try to smooth out the difficulty of travel across terrain so that we would get more curvy roads and better layout otherwise it all looks like a US city block or strong 45deg lines going everywhere. The idea is to take the final generated terrain with the blurry roads and then put it through a procedural texture generator which should paint them in a nicer sharp version. Maybe I should show that happening. It would also paint the trees with a tree texture, water with one and so on so it looks like a proper map. I think the res might have to be bumped up a bit tho.

I knew before I started that this would be a right bitch to program and it really is. You get odd funnnies happening at many many iterations into the sim then you make a slight change and the whole sim is then different and the funny goes away and you cant reproduce the effect again. Then a bit later something else funny happens. Its more of the nature of iterative programs rather than economics that is the issue.

The basis of what could be generated is definitely possible. You could still make a full history of the town and people and have them all named and have all their interactions to the others all mapped out but the limit to its possibilities is down to the complexity of the program and how much complication can be modeled before the programmer goes insane ;)

I'll run a sim and show the kind of final map I had in mind tho cos maybe people are getting the wrong impression that these output movies and pics are thought to be the final map. They are just the debug version of it showing as much info as I can manage to squeeze onto an image in one go.

11-29-2010, 11:41 AM
This is very interesting, and I was following it until a while ago.....I don't have the time to read back atm so I will ask instead of reading for the answer.....Is farming taken into account at all in the economics of the programming?

11-29-2010, 12:27 PM
Yes, when they are hungry then they look for food. When none is available then they would either try to barter for some or else as a final resort they would probably plant some seed. The reason I say last resort is that the program works out how long it will take before the food turns into harvest so they can eat it and its quite a long time. So when they plant seed they have to wait for it and may spend their turn tending crops using their farmer skill. When the crops get to about 50% then they cant be tended after that and they have to wait for the final harvesting. The reason for that 2nd phase is that if not then they would plant seed and stand over it like a vulture tending it to death until it harvested. When they are unable to tend the crops then then cant do any more so they plant more crops. So this way it generates multiple fields of crop. The thing is tho that once the first field harvests they eat some and carry the rest. Then the second field harvests and they don't need it so potentially they can sell it. So in effect you will only see one or two fields being worked at once unless every one is hungry in which case there is a mass planting. There is no intelligence enough to predict that the market for crops will be high so lets start planting before anyone is hungry. This might be mitigated when there are many many people to average the demand out or else if there is a mill that is a constant need for crops (grain). Maybe if they valued stored harvest then they might sell it to the grain mountain even if it all goes to waste. Perhaps that is a real reason why its done in real life. You must support the mechanism to generate crops in the times when the market does not demand it in case the market rises in the future. Anyway, that bit of it is not in the program, hence not too many fields at once.

12-01-2010, 05:56 AM
I've been following this thread although much of it is beyond me. It would be really cool if you could run this sim over a world generated in Fractal Terrains to generate cities, towns, villages etc and other 'human' elements on the map.

12-01-2010, 09:25 AM
Yes that is its primary purpose. I think you would need to run it once per village / town or city but in principle the idea is make up the world using a procedural method, extract bits of it using a shader that gives rock, trees, water (sea and fresh) and land (plus I might add gradient in the future too) and then let this app make the man made bits of it and make the map + store it all in a database so we can access all the people in it too.

05-30-2011, 07:59 PM

This is a really cool thing you're doing here... has there been any progress this year?

05-30-2011, 08:10 PM
I have kinda got tied up it many other little projects. I wanted to do this application to see if it was possible. I think it might be possible but its very hard to debug when it doesn't do what you expect and is a bit of a time sink so its on the back burner. I hope people can read about this and take something away from it. I learned a few things doing the app - what works and what doesn't. To take it to the level I would have liked tho is going to take some significant amount of work and I don't have the time right now. I'm still glad to talk about what I did and what I would do differently or similar next time. If someone were to be paid as a game dev to make something then I think they would have the incentive to take the time hit to make this more of a reality. I think Talroths posts are very relevant and I think others have more experience of writing AI systems than I have.

I'm doing the MeDem at the mo which is the Middle Earth Digital Elevation Project and that's a bit of a time sink too but it generates some excellent material. There will be a point when we might like to populate that huge huge map with some semblance of creature activity and its too big a map to do by hand so I think in the micro scale a program like this could be used. So I might dig it out once more and soup it up a bit and let it loose on a bigger map.

The one thing I got from the results is that, tho its generating a sort of map from the workers moving about, I think its going to have to deal with a lot more complexity of transactions before the map is going to look anything like something we would recognize as being sensible human activity on a bit of terrain. There will be some critical mass of complexity tho when it starts to look good and then adding more should make it better but to get to the critical point might take a lot more work and time.

05-31-2011, 03:27 AM
to bad, but understandable ... it was a fun project though :)