Researching Junction Gate's Computer AI System

Another week and a little further down the rabbit hole of systems development. This week I again ran into the necessity of taking a step back to make sure that I design and set up game systems the right way, rather than plugging in dummy functionality and causing rework later on. As a result, coding took a backburner to some research and systems design. The good news is that I was able to establish the initial design for several key upcoming systems in the game and start work on the coding. I'll cover some of them briefly and then talk about the game's AI system.

Ships, Travel, and Locations

In preparation for the 0.1.0 release, some rudimentary systems need to be in place, to be expanded in more detail for subsequent releases. Among them are ship generation, travel mechanics, and a location system (basically the galaxy map). I'll be talking about each of these in depth in future updates, but here's a brief overview:

  • Ships - The data structures for the first trade ships are almost done and the first trade fleets are being generated, though the ships still need to be outfitted and filled with cargo. I've also figured out how I want ships to work in the beta. They'll be much more precious commodities. Each will be customizable and upgradable with actual components, with smart default builds. The 0.1.0 release, however, won't allow the player to do anything with ships yet.
  • Travel, Locations, & Exploration - The design for the sector>system>location>colony system is almost complete, and I have a basic idea of how I want travel to work, but there are a few more details left to work out. Expect more on this subject next week. I also figured out how the exploration mechanic will work.

In light of the expanded scope of this release, I've updated the roadmap and feature comparison pages to better reflect what's being worked on for 0.1.0. The new features are necessary and, while they may delay 0.1.0 a little, they will help future releases immensely.

The Computer AI System

In the Junction Gate Alpha, my first attempt at any sort of computer AI ever, the computer players can be somewhat finicky. They mostly work as intended, but they aren't very smart or adaptive. The code itself is basically a set of rules encoded into a series of if/else statements. There's no "system"; the AI for each game entity is unique and there is no code reuse. It's messy and making changes or maintenance to the code isn't fun.

As I began making preparations for implementing the AI for the trade ships, I realized I needed a more robust, flexible, and maintainable AI system. Some of the different entities that will require AI in the game include: traders, ship captains, CEOs, colony governors, faction leaders, and pirates. I needed a system for all of those types of NPCs, so I started my research.

The type of system I settled on is called a Utility AI system. Rather than a rigid if/else structure of conditions and rules, the Utility AI system compiles a list of possible actions an NPC can perform, scores each action with a numerical score, and then selects an appropriate action. The score for each can change based on the current situation and can be individualized to each NPC based on their unique characteristics. The action selected can be the action with the best score, a random selection from the top options, or a weighted random selection. Introducing randomness into the decision-making process add variety and makes the NPCs less predictable.

One of the first applications of this new system in Junction Gate will be the trade ship NPCs, so let's use them as an example. Let's say a trade ship arrives at a colony with a hold full of cargo. Upon arriving, the trade ship captain has several available options:

  1. Plan a new destination and leave immediately.
  2. Stay and refuel.
  3. Stay and buy goods.
  4. Stay and sell goods.

The Utility AI system retrieves the colony's data, the ship's data, and the captain's data and passes them to each action function to be evaluated and scored. Once the scores return, they might look something like this:

  1. Stay and sell goods. - The colony is willing to buy goods that the ship has in its cargo hold. Score: 95
  2. Stay and buy goods. - The colony has goods that could be profitable elsewhere. However, money could still be made by selling goods. Score: 85
  3. Stay and refuel. - The ship has enough fuel to reach other destinations, but the fuel tanks aren't 100% full. Score: 55
  4. Plan a new destination and leave immediately. - The ship can still make a profit, buy goods, and the fuel isn't topped up. Score: 5

The system might eliminate Option 4 at this time because the score doesn't meet the arbitrary (in this example) minimum threshold of 20. If we simply go with the best score, Option 1 would be the choice. However, the system could also choose randomly or take a weighted random selection from Options 1-3. For the sake of illustration, let's say that the trade ship captain chooses Option 1. After completion, it's again decision time. Now that the situation has changed, the scores are recalculated and now look like this:

  1. Stay and buy goods. - The colony has goods that could be profitable elsewhere. Score: 95
  2. Stay and refuel. - The ship has enough fuel to reach other destinations, but the fuel tanks aren't 100% full. Score: 55
  3. Plan a new destination and leave immediately. - The ship can still buy goods and the fuel isn't topped up. Score: 15
  4. Stay and sell goods. - There are no goods in the cargo hold that the colony will buy. Score: 0

With new information, the scores have changed and the trade captain's priorities have now shifted. Selling goods has been zeroed out, so it's off the board. Leaving moved up, but the score is still under the threshold. Now buying goods seems like the best option, so the captain does that.

  1. Stay and refuel. - The ship has enough fuel to reach other destinations, but the fuel tanks aren't 100% full. Score: 55
  2. Plan a new destination and leave immediately. - The ship's fuel isn't topped up, but there are no other activities to do at this colony. Score: 45
  3. Stay and sell goods. - There are no goods in the cargo hold that the colony will buy. Score: 0
  4. Stay and buy goods. - The colony does not have goods that could be profitable elsewhere or the captain's trade budget for this colony is depleted. Score: 0

Trading is now off the table, but leaving has finally passed the minimum score threshold. However, being a prudent captain, he fills up his fuel tanks because you never know what might happen.

  1. Plan a new destination and leave immediately. - All systems are go, there's nothing else to do at this colony. Score: 95
  2. Stay and sell goods. - There are no goods in the cargo hold that the colony will buy. Score: 0
  3. Stay and buy goods. - The colony does not have goods that could be profitable elsewhere or the captain's trade budget for this colony is depleted. Score: 0
  4. Stay and refuel. - The fuel tanks are 100% full. Score: 0

Now that the captain has decided to leave, the Utility AI looks at the available destinations and assigns each of them a score. Some factors going into the score might include distance, profitability, and threat level. Once the location has been decided, off the ship goes to start the whole process over again once it arrives.

As you might be able to see, the Utility AI system opens up a whole new world of possibilities for the game. Some of them could include NPCs with different personalities, varying aversions to risk, and the ability to become more experienced and choose better options over time. I have a lot of ideas for this system, but it will take some time to build and a lot of balancing and experimentation to work them out, so stay tuned.

Additional Utility AI Resources

If you're interested in learning more about Utility Theory for Artificial Intelligence in games, here are some of the resources that I found:

The above videos weren't embeddable here, but I'd really recommend them if you want to learn more on the subject, especially the first video.

Development This Week

This coming week is my wife's and my 9th wedding anniversary, so work on the game will be a little lighter than usual. However, I anticipate continuing to work on the systems mentioned above. I hope to have some of the AI system worked out by next week, perhaps enough to have trade ships traveling from one location to another. It might take a little longer than just this week, but once that is in place, I'll be able to return to working on the trading UI. Have a great week!

comments powered by Disqus

Updates