The goal for now is to start shoving some agents into the game. The world won’t feel very alive if the player is all alone! Thankfully, artificial intelligence is kind of my specialty. It’s one of the things I feel most comfortable making. In fact, something I learned how to do several years ago will likely come in handy right now! That something is called goal oriented action planning. It’s absolutely perfect for whats needed. That ‘need’ is AI that are easy to create, can adapt to their environment and have personality.
The beauty of GOAP is that it’s not what you might call hard coded. It’s flexible instead of rigid. I’ll even throw the buzzword dynamic into the mix! It’s about as close to alive as I can imagine a pile of code ever being. Your average AI, or at-least the ones I usually make, are typically ran by one gigantic wall of code. You can basically think of this wall as a check list. The AI simply goes down this check list every frame and accomplishes its tasks in order. It has to have a answer for every edge case scenario in order to prevent it from getting stuck or worse. I’ve you ever seen a computer controlled player running endlessly into a wall its not a bug. It’s a lack of logic. It wasn’t given a way to detect that its stuck or a method to correct itself. This fact is what causes problems as it means even a fairly simple AI can quickly become a mess of spaghetti code.
The problem with this checklist method is that the bigger the checklist grows the more complicated things become. It will quickly end up looking like an overgrown tree instead of a linear path due to all the branches of thought constantly being added. This type of AI is common because its easy to make – but it becomes harder to maintain as the game expands in complexity. It just doesn’t scale well at all. The worst part is that there’s basically nothing you can do about it. There’s no magical way to implement a checklist style AI (usually referred to as a finite state machine) without the resulting spaghetti code. Either throw more code at it and dig your hole deeper or have crap AI.

This is a pseudocode example of a finite state machine. The aforementioned check list! This is actually based on real code that I use for most of the monsters in my games! If you were to convert it into real code then it should work right out of the box. You can already see the branches starting to form despite being such a simple AI. It shoots enemies when they’re present and searches for them when they’re not. That’s it.
Let me give you a test scenario. The AI needs to eat. It will die in a few turns from starvation if it doesn’t! Let’s also say a enemy is nearby. What does the AI do? Does it risk fighting the enemy them before they starve to death or do they run to find food? There’s no guarantee they can quickly defeat the enemy so the obvious answer is to find food. They have a good amount of health left and can tank some damage, after all. However, they don’t know where the food is! They have to randomly explore and hope for the best. They might starve while looking for it or be killed by the pursuing enemy. What if the poor AI is low on health, too? If they run to find food the enemy might kill them. If they fight the enemy it might kill them. If they do either they might starve and die anyway The choice isn’t so easy now, huh? This is one of the many problems with finite state machines. They need a hard coded response to every imaginable scenario and more often than not a clear answer doesn’t exist. Even if a answer does exist then its going to result in predictable agent behavior on top of being annoying to program.
That’s where GOAP comes in. In a way, it’s similar to a finite state machine. It’s not sentient or anything. It still uses the code you made! The difference is that this code is modular. The evil checklist is gone and there’s literally no consequence to making the AI smarter or the game deeper. I can not only give it a response to every feasible situation without the spaghetti code but it will also respond to said situations all on its own. The modules can be thought of as tools and the AI automatically picks the right tool for the job. There’s no such thing as too many tools, either! I can fill that toolbox to the brim without consequence. This is because each of the tools are entirely independent.
The best part is that these tools, being modular, can be added or removed at will. For example, let’s say a civilian, someone that has no experience fighting, just found and picked up a grenade. That grenade contains the code needed for the AI to use it and the act of looting it means the civilian is now capable of throwing it! This is a powerful system that lets the AI better react to its immediate environment. This thing might as well be a procedural chaos generator. Let it loose in a sandbox setting and enjoy!
The way it works is pretty simple. The name goal oriented action programming kind of explains things on its own. Every AI has a few goals and they automatically work to achieve said goals when needed. The need is based on some stats – so a hungry agent will look into a eating food. There’s not a wall of code guiding them, though. Instead, it’s the goal itself helping them. Each of the goals can basically be thought of as miniature finite state machines. They contain just enough code to help.
It does this by analyzing all of the agents options. If the agent is starving and the goal they’ve selected is to eat then every means of acquiring food will be considered. The best option will cost the least points and thus be chosen. If the agent is lost out in some random desert, for example, then there’s not going to be a restaurant anywhere nearby. That means getting fast food will be given an undesirably high cost. It doesn’t just stop their, though. The analysis goes all the way down the list in regards to the option of getting take out. That means it will also take into consideration the cost of buying it. If the agent doesn’t have any money then the cost will be raised even further! The next option could be hunting. There’s some camels nearby! Even though it’s dangerous that would probably end up being the best option available – unless they’re a vegetarian! If that’s the case then they might resort to killing a nearby trader and stealing his cookies.
Phew! That was a wall of text. In the next part I’ll start adding GOAP.