June 12, 2012

Dev Update : AI going rogue

I've let run the latest development build a whole day in the fastest "real-time" mode available. And here are the results. This post will also explain various major changes which are going to happen in the next versions.

Also, I know there's a few coders reading this blog, so I guess you'd love reading about this "new" path-finding algorithms (read it, seriously) which have the same advantages as the good old A* with a 10x speed factor. That's something I may give a try as my pathfinder algorithm is basically my biggest bottleneck in terms of performances. The second one is especially interesting given how easy it is to implement, but it can't manage a weight for each node (meaning that it's a no-go for GRA but others should find it very very useful). The first one could be a winner, but it requires way more changes in my current code. Don't forget to read the PDF linked by the article as they are the real stuff.

Anyway, let's talk about GRA !

So there's both good and somewhat bad news. Anyway, here are the results of those tests. 

First, the good stuff. There's no memory loss. With a 12x12 city populated by roughly 5500 people and 10 gangs, the game takes basically 250Mb of RAM (+same in virtual memory) and never goes above this number. According to the debug tools, everything that should be cleaned is effectively cleaned when you reset the game. That is great, given the ridiculous amount of data the game is tracking down. It means that the engine behind it is doing great. Also there's no crash or unexpected error message, even when the whole map is taken by gangs, it still goes on without any unexpected problem. It means that the whole "faction warfare" stuff, even if it's still very bare-bone, works according to plan.  Also, the recent code optimizations are really taking effect, it's by far the most efficient build I made. And I can optimize the code even further relatively easily now that a few annoying hurdles have been removed.
I also improved some parts of the code so modding the game isn't so crash prone. Much more should be done about it at a latter stage. But now, at least,  if a NPC definition is missing from the game, it won't crash without warning. Instead, it will post a debug message and delete the said NPC.

Also, I made many changes to the faction warfare stuff. As there's no real save-game yet (and do not ask when it's going to happen, because the only answer you'll get is "1.0") you won't notice it. But those changes are very important for the long run. Basically, gangs will take over the city. People who live in a gang owned sector have a decent chance to respawn as a new gang member. However if the police liberate a given sector people who died and who are re-spawning will be civilian again. But as there's X gangs, and only one police faction in the vanilla game, after a dozen of hours of 'normal' play, the cops will be irrelevant. This is intended. This is a city going wrong. Also, I guess that the next versions will allow the military faction to clean up sectors once some threshold has been met. We'll see.

I also fixed a few loopholes in the faction AI. The main one being that if a faction doesn't "win" a sector takeover it can't recruit new people at all. This issue was causing most factions to die stupidly after their first unsuccessful attempt to get a new sector. However, for some reason I have yet to understand, The Brotherhood, which get the best equipment by far, is dying quickly in any map I generate. This is not intended, and I have yet to understand why before any new release. Basically my idea was to have 2 hard-coded factions who are supposed to rule the world and other minor ones who barely survive. And now, randomly generated factions with crappy gear are winning easily without any player input. This is unexpected. It may be pure AI but I am pretty sure there's a major bug somewhere preventing the brotherhood from recruiting new members or killing enemies.

This said, here are the major issues regarding the city simulator. The fact that gangs will take over the whole map put that into light. So here are the results in no particular order:

1. People are dying of hunger. Basically anyone who is a cop, thug, or patrol will die because of the lack of food. I added a fail-safe: people who are fighting won't have the "hunger" meter decrease. First because having an enemy disappearing in front of you is (at the least) very weird, a secondly because the adrenaline alone should get anyone to finish a fight. However, even if this change fixes the most ridiculous issues, it doesn't fix the main issue at all. Every 10 turns someone with enough money, who isn't even fighting dies of hunger. Usually when it happens they are on their way to a food store. I do know that players won't notice, won't give a fuck and so on, but as an AI guy it's bugging me to no end. However this is mostly fixable. First I should force NPC to stockpile food in their inventory, and i should should force them to calculate if they can die before reaching a given food source or not. 
You could ask "why it hasn't been done before?!" as it's a fairly logical question. The reason is CPU time. calculating distances between each food source and the NPC is processor intensive. At that time I tried to cut corners (especially as retrieving data from the map was way more CPU intensive than right now). Now that the whole city/data system is finalized, i guess i can allow NPC to use more CPU power to find which food source can save their live.

2. The economy is trash. Literally, the economic system has to be rewritten from scratch. I always knew it was temporary, but i never imagined how bad it really was.  Basically, if you give the game enough time every NPC will drop thousands of dollars when they die. This is very bad. First it prevent me from balancing the game in any way shape or form, as the player can sleep a few 100's turns and kill someone to get enough money to rule above all. Also the whole factory worker <-> courier <-> shop owner stuff doesn't work as intended.
So, once I am done with the "dungeon" stuff  (next release, somewhere this week), the economics will be the new major focus. Because at the moment, it's shameful to be honest. So, I am going to use the same system as dwarf fortress (reactions). Basically, we'll be able to create reactions, like: one meat + one bread = one burger. Civilians, instead of having predetermined jobs will answer to orders given by the factories, shops and stockpiles. The city will have a randomly generated income of basic resources  and civilian will try to deal with it to generate goods and food. This is a major change that's going to occupy the whole 0.7 branch. I kinda understand that from a player's point of view it won't matter. You want to kill stuff and get rewarded. Yay! But I will still follow the road i took with Pirate Guild and my other X3 plugins. I want something organic, if it makes sense.

It probably doesn't. So let's me clarify. The whole civilian stuff, and factory <-> shop stuff will be rewritten from scratch. The general idea is that instead of using scripted stuff for factories and civilian jobs, we'll be able to add our owns jobs through the various editors. In other words, once the system is perfected, moddable economic chains to create food/specific objcts will be created and NPC will react accordingly to build those. The fact that jobs will be given by the objects instead of having NPC X having job X means that it can be scaled, so even if a zombie apocalypse kill 90% of civilians, well, the 10% still available could still produce any good but at a much slower pace.  In other words, civilians won't have specific jobs. Activators will post "missions", like "i need 1 bread + 1 meat" to produce 1 "burger". that civilians will carry. Shops (according to type) will have missions like "I buy 1 food to put into inventory". With a priority system, idle NPC will pick "missions" offered by the activators and it should be enough to sustain the economy. The issue being that it's not very realistic. People will change job on the fly at first. I may be able to mitigate that with the skill system, though. But, tbh if it works according to plan, the benefits outweigh any issues by far.

3. The OOS pathfinder is trash. Here again it probably doesn't mean anything to you. But it's breaking the whole game a lot. The last update added the military base to the game. And as a result, anyone going from a point A to a point B but with the military base in between died. Leading to unwanted gore and loot at the said location. If it was only limited to this faction I could add some rule to avoid that when the player isn't looking around, but with the addition of dungeons, it's going to be a fairly common case. I can ignore the problem and delete the loot but depending on the general geography it can kill the whole city. I also could add a "private" setting for some sectors/buildings but it would force me to add a real pathfinder for all NPC, which would immediately kill the performances. So instead, I am going to cheat. If you're not here, nothing bad is going to happen and the game will assume that civilians will avoid "private" zones/buildings.


4. Thug / zombie / patrol. jobs are way too CPU intensive. After a whole day letting the game play. When there's only thugs on the map it takes 400ms to play. This is understandable as, yeah those jobs are time consuming (scan map for enemies, combat commands, all of this being very CPU intensive). But i'm pretty sure those "jobs" can be written in a more efficient way. Zombie is, by far, the worst.
However, given the fact that I am going to add a sound / light / stealth system in a near future i don't think it's a good time to optimize those jobs.

All in all. I am happy with the way GRA is responding. Yes, it's still very buggy. But hell, the engine handles 6.000 NPC per turn, without cheating and without slowdown. I do not claim having to most intelligent NPC ever, but making them smarter, given the current code, is not very CPU intensive anyway. More to the point there's room for improvement, as i can ask far away NPC to skip turn to give nearby ones more CPU power. This is a freaking good engine. I just hope I am going to do something fun with it ^^.

2 comments:

  1. Plain awsome glad GRA is going on your tracks keep it up!

    ReplyDelete
  2. Breath remains baited!

    ReplyDelete