Game Engine Project Part 2: The Goal Oriented Action Planning Agents
Date: June 12, 2023
The second part of my final year University project was creating Agents that work inside the game environment and produce real-time dynamic behaviour within their environment. The choice AI technique that I decided on is Goal Oriented Action Planning (GOAP). GOAP was developed by Jeff Orkin and was used in the game F.E.A.R which was praised for its dynamic AI behaviour. This technique was also originally based on the Stanford Research Institute Problem Solver (STRIPS) which is an automated planner which uses first-order logic to bring a world state to a desired goal through resolution.
GOAP similarly uses a set of goals and actions to dynamically generate an action plan for an agent based on predicate logic and priorities. This produces an effect of complex behaviour in the agent as it reformulates plans based on its immediate situation. The system was developed in 2003, in response to the increased desire for more complex AI in games, this system reduces the complexity of working on autonomous agents by separating out logic into different actions. The disadvantage of this system is that the complexity of traversing the search space increases based on the number of actions available and creating scripted behaviour for AI is increased in difficulty. The following sections will make a brief summary of how the Agents within my game work.
For every update of the Agent there are updates to its world state based on its external sensors. The world states are set as a combination of bits that indicate to the agent whether certain conditions have been met. These are the World States I have added for each agent:
• AT_HOME
• PATROLLED
• HAS_ENERGY
• ALERT
• AGENT_ANGRY
• AGENT_TIRED
• AGENT_HURT
• ATTACK_SIGHTED
• INVESTIGATED
• ENEMY_DEAD
• ENEMY_VISIBLE
• COOLDOWN
The goals of the agent are updated every loop of the engine. If the current goal is invalid, then that goal will be removed and the next goal will be searched for using a priority level and whether that goal is valid. The validity of the goal is checked against using the current world state of the agent.
Agents also have access to actions; each action has a set of preconditions that need to be fulfilled before they can be used. After an action has been completed then there will also be an effect which alters that agents World State.
Each agent uses a small finite state machine, where the agent can switch between a MOVE, IDLE and ACTION state. The IDLE state has the agent clear all action plans and destinations and generates a new action plan using the current goal. The planner uses the A-Star algorithm to plan a path through possible action. The Goal state and World state are compared and then an Action is chosen with a Postcondition that changes the World state to that goal state. The Precondition of that action is then added to the Goal state, this process repeats until there is a match in World state and Goal state. Each iteration of the A star algorithm chooses the action with the lowest F value which uses a combination of action cost taken from the start to the current action and a heuristic looking at the differences between the updated World State and Goal State.
If an action path is created, then the agent will change to the ACTION state where the action will execute if it is within range. An Agent will produce a command to be sent to the enemy controller using the action plan. If the action is not within range, then the agent will switch to the MOVE state and plan a path to the destination.
Path Planning
The path planning is implemented using the A star algorithm using a Euclidean distance heuristic. This was chosen as agent are set within a grid map where they can move diagonally. Each grid tile of the map is associated with a node and the path planner goes through possible routes finding the lowest distance route to the destination.
Not all goals and actions could be implemented because of time constraints. The goals that did not get included were the Calm Down, Stay Alive and Flee goals. In addition, different attack type actions, dodge actions and an ANGRY world state could not be completed within time.
Below is a clip of an agent in action going from a resting state to a combat state and then finally to an investigation state.
Comparison Agent
A comparison Agent was also made to compare against the GOAP Agent, the design choice for this would have been an agent with a larger state machine, however because of time constraints an agent that responds to the user being in range was created as an alternative. The Agent uses the MOVE and ACTION states. The Agent has a cone shaped sensor which only detects collision if the player is detected to be in range using the physics collision call backs, a Box2D ray cast is sent to determine if the agent is facing the opponent, a Rotate Command is sent to the controller to adjust the Agent’s direction. An attack Command will also be sent to the controller once both conditions of being in range and in view are met.
To conclude a game engine, a game and two AIs were created. The game engine was used to produce a simple game with multiple enemy agents. The GOAP agent used could autonomously plan out actions based on the state of the environment and brings the benefit of being easily customizable in the actions and goals given to it.
Finally, I would like to mention the collection of resources produced by Jeff Orkin that I used when planning out the GOAP agent. These resources can be found here.