A new week, and a new AI
20/05/2025:
New AI: New week, new post, and the beginnings of a new AI. Let's call it Al, as in AL.
Compared to the earlier pacifist-AI, Al's a beast! I'm excited! It’s a lot more aggressive, and that’s exactly what I was aiming for. You’re not going to just cruise to galactic dominance anymore – you’ll have to seriously fight for it.
I wanted to give you a peek under the hood at how Al makes its decisions so I asked Gemini give a short introduction to Al's innards.
Al's New Operating Model: The Hybrid Thinker
We call our new AI a "Hybrid Thinker" (sidenote: no, we really do not..) because it combines several intelligent approaches to decision-making. In the world of game AI, developers have a toolkit of different methods, including:
- Finite State Machines (FSMs): Think of these as moods or modes for the AI (e.g., "Peaceful Growth," "Aggressive Expansion").
- Behavior Trees (BTs): These are like complex flowcharts that map out sequences of actions and decisions.
- Utility AI: This is about scoring every possible action the AI could take, then picking the "best" one based on the current situation. It's like the AI asking, "What's the most useful thing I can do right now?"
- Goal-Oriented Action Planning (GOAP): Here, the AI sets a high-level goal (like "Capture that juicy planet!") and then figures out a step-by-step plan to achieve it.
Other Tricks in the Game AI Toolkit
(a.k.a. things I probably won’t implement but still enjoy reading about at 2am)
In addition to the usual suspects – FSMs, BTs, Utility AI, and GOAP – game developers (or solo devs furiously googling “how to make enemy less dumb”) have a few more tools at their disposal. Some are fancy, some are weird, and some are probably overkill for my little pixel spaceships. But hey, knowledge is power.
Here are a few more models that exist out in the wild:
1. Influence Maps
These are like heatmaps overlaid on the game world, where each tile or region is given a "value" based on threat, opportunity, or presence.
Used for: enemy avoidance, tactical movement, or making your AI look like it's thinking several steps ahead when it's really just playing hot-and-cold.
2. Fuzzy Logic
Instead of binary yes/no decisions, fuzzy logic lets the AI make kinda sorta choices.
“Is this planet worth attacking?” – eh, 73% yes, 27% nope
Good when you want your AI to feel more organic, and less like a spreadsheet in motion.
3. Neural Networks & Machine Learning
The nuclear option. Train your AI with thousands of matches and let it figure stuff out on its own.
Great for complex games, terrible for dev sanity.
Also: requires data, time, and probably a PhD.
4. Blackboards
A shared "notebook" where different AI components post facts and updates (like "enemy spotted" or "need reinforcements").
Works best when your AI has many agents or subsystems coordinating together.
5. Decision Trees / ID3 / Random Forests
These come from the world of data science. Instead of coding logic by hand, you let data shape a branching decision structure.
More used in analytics or adaptive AI (e.g., AI learns your playstyle), but can be used in games too.
The new AI is a hybrid that leans heavily on a few of the first four core ideas to bring "Al" to life:
- Strategic "Moods" or Phases (A Simple FSM): At the heart of Al is the
AIController
. This acts as its strategic brain, primarily operating in one of two "moods" or phases: EXPAND or DEFEND. At the start of each turn, Al assesses the overall game state – for example, whether it has enough planets or if it has lost all its bases – and decides which strategic mood best fits the current situation. This top-level state guides its general focus. - Utility-Driven Choices (Scoring What's Best): This is where a lot of the new smarts come in!
- For planetary development (handled by the
buildEconomy
function), Al now scores different building options (mines, shipyards, defenses) based on their perceived importance (using internalWEIGHTS
) and its current needs. It tries to pick the most "useful" thing to build on each planet. - When looking for planets to attack or expand to (using the
tryExpand
function), Al scores potential targets based on factors like distance, whether there's a handy starlane connection, and who currently owns the planet. This helps it prioritize more promising targets.
- For planetary development (handled by the
- Rule-Based Logic (The Fine Print): Underlying these smarter choices are still many specific rules. Al constantly checks if it has enough resources for a project, if the right tech or shipyard level is available for certain ships, or if a fleet has reached its target size before launching.
- Budget Allocation (Managing the Coffers): Right off the bat each turn, Al (via the
makeBudget
function) divides its available resources (credits and minerals) into two main pots: a portion for economic growth and another for powering the war machine. Currently, it dedicates about 40% of its income to economic development, ensuring it doesn't neglect its foundations even when building up forces.
Delving Deeper into Al's Behavior:
- Intelligent Planet Development: When Al develops its planets (using the
buildEconomy
function), it's no longer building randomly. As mentioned, it scores different construction options. For instance, mines are usually high on the priority list, but upgrading shipyards is also seen as crucial. Al checks what a planet needs most, what it can afford, and what its current infrastructure allows. - Considered Ship Construction: Ships (managed by the
buildShips
function) are built within the current "war budget" and according to shipyard capacity. Al favors more powerful ship types (Cruiser > Destroyer > Fighter) if its shipyard level permits their construction. However, it doesn’t clog its shipyards with endless queues, generally aiming to build one ship per shipyard per turn. - More Aggressive Expansion and Target Selection (
tryExpand
): This is one of the biggest upgrades!- Al actively scans the galaxy to find the best targets for expansion – whether they are neutral or player-controlled.
- Each potential target is scored based on several factors:
- Value: How many resources or strategic advantages does the planet offer? (This is still evolving, but the basic idea is there).
- Accessibility: Is the planet nearby? Is there a direct starlane to it? Starlanes provide a significant bonus! Distant and isolated targets are less appealing.
- Owner: Neutral planets are easier pickings than player-controlled ones, so they might be slightly favored in the early game.
- Defenses (Implicitly): While the v11 code doesn't yet explicitly score the strength of defenses in this specific
tryExpand
function, weakly defended and valuable targets will naturally rise higher in the scoring (as there are no negative points for defenses factored in here yet). This means "Honeypot" planets – those intentionally tempting but undefended treasures – are now much more likely to be in Al's sights!
- Fleet Assembly: Once a target is chosen, Al attempts to send a small fleet (aiming for around 8 ships, defined by
FLEET_TARGET
), not just single stragglers. However, the first available ship (LAUNCH_MIN
) will move out immediately if ready, with others following until the target fleet strength is met or ships run out.
- Emergency – All Bases Lost (
recoverLostBase
): If Al is truly cornered and loses all its planets, it doesn’t give up! It enters a special "survival mode":- If it still has ships, it will desperately try to recapture its former homeworld or any other weakly defended planet. All remaining ships are focused on this single objective.
- If it has no ships left, it might even attempt an "emergency claim" on the nearest neutral planet just to get some kind of foothold back in the galaxy.
What Does This Mean For You, The Player?
I hope these changes make Al a more unpredictable, challenging, and ultimately, a more fun opponent. It should now be better at recognizing opportunities, exploiting weaknesses, and reacting to changes in the game state. “Honeypot” planets (heavily developed but undefended) are now very attractive targets for Al. I have an inkling that you could use these kind of planets as a bait for weening out the enemy forces with your nearby surprise fleet.
This is an ongoing process, however, and there's a lot of tinkering ahead. But I'm glad I cannot win every game eyes closed anymore. TBH I haven't won a single game yet against Al.
Happy gaming,
Pecatus
======
Edit 22/05/25:
After few more playtests it seems the AI isn't that hard at all, and it's pretty one dimensional. For some reason it won't build mines on other planets which will get it overrun after you destroy it's conquering fleets. Tinkering continues.
Files
SpaceWar - a tiny 4x strategygame
Build fleets. Develop planets. Conquer the galaxy.
More posts
- Thanks for checking in!8 days ago
Leave a comment
Log in with itch.io to leave a comment.