John C. Jensen

Unity Developer

Unity Game Developer specializing in C# programming

Colony Sim AI Prototype

One of my favorite types of game is the base builder/colony sim genre; I also love games that rely heavily on emergent gameplay via interconnected systems. Additionally, I’ve always wished that NPCs in open world games (Especially MMOs!) would be more lifelike, instead of being what are essentially background animatronic characters, endlessly hammering on a wall to no effect.

With this in mind, I set out to create an AI framework that could approximate the NPC behavior I had imagined. My idea was that each NPC should have a set of goals that they want to achieve. The goals would periodically be evaluated and given a Priority score, then any goals over a minimum threshold would be selected from at random, weighted by priority.

I thought I was terribly clever for creating a unique AI system. However, if I’d done any prior reading, I would have realized this already existed. Whoops.

Regardless, my prototype worked as I’d imagined, and I considered it a successful experiment. I created the terrain using Gaia, the rest of the art is various free assets.

Design & Mechanics

Utility-based AI

NPCs have Goals ranging from Team-wide goals like “Construct buildings” and Personal goals such as “Don’t starve,” which will be used as an example here:

  • Each NPC can be given one or more Roles, which are collections of Goals.
  • Goal priority is scored based on a combination of Urgency (How hungry am I?) vs Expected Completion Time (How far away is the food?)
  • Personality traits also skew the priority score; for example, a high Gluttony score makes them more likely to prioritize eating.
  • Goals that score higher than a threshold, will be a High priority. High priority Goals always take precedence. (An NPC won’t take a nap if he’s starving.) 
  • Once chosen, each Goal has a set of Actions associated with it. Locate Food, Go To Item, Collect Item, and Eat Food.
  • After the selected Goal’s Actions have been attempted, or enough time has passed, the NPC will re-evaluate their Goals.

NPC Task System

With multiple NPCs operating simultaneously, I needed a way to keep them from attempting the same jobs and stepping on each other’s toes. For example, a construction site needs one more Stone, but three different workers each bring one.

My solution was a Task system; a team has a list of tasks automatically generated based on different occurrences, usually initiated by the player.

For instance, if the player places a construction site for a new building, tasks to harvest the necessary resources and haul them to the site will be generated, as well as tasks to perform the actual construction once the resources are hauled. If the player sets the Food threshold above the currently owned amount, an equivalent number of “Gather Food” tasks will be created.

If an NPC’s chosen Goal is to perform a community task, it will “check out” the task, preventing another NPC from working on it. As an example, if it has inventory room to carry five Stones, it could check out five Haul Stone tasks. Once the tasks are completed, they are removed from the list.


Debug visualization of the Task system for the construction of a building

Inventory System

The first iteration of my Inventory system that I later expanded on for Heal Plz.

  • Variable sized per-entity inventories.
  • NPCs can harvest from rocks/trees and collect the resources.
  • NPCs can interact with other inventories, giving items to other NPCs, or adding or removing them from containers and construction sites.
  • NPCs can remotely ‘claim’ items from containers to prevent other friendly NPCs from taking an item before they can.
Storage Box inventory, "Claimed" items have a red background

Construction System

The building construction system allows the player to place a construction site that friendly NPCs/the player can haul resources to and work on building it.

  • Building Blueprints are a ScriptableObject that have have resource requirements specified in order to construct it.
  • The player chooses the location and orientation of the build site; can also be used for placing pre-built objects, such as storage chests.
  • Integrated with the Task system, NPCs will automatically harvest and haul the required materials.
  • The construction site has an Inventory to store the required materials.
  • When a required resource is placed into the inventory, the Player or NPCs can perform a Build action to consume it and apply it to the building. When all resources are applied, the building is completed and usable.
Custom Inspector for a building blueprint

NPC Knowledge/Communication System

To prevent NPCs from having omniscient knowledge of everything in the world, I created a team knowledge & communication system.

By default, each NPC is only aware of objects it has personally seen. However, by interacting with other NPCs or the player, they can automatically share the locations of objects they’re aware of.

This allows the player to explore and locate resources to be later harvested by NPCS.