Where should I store the code for computer decision making / classical AI in a strategy game?

Godot Version

4.7.2

Question

I’m planning to make a strategy game, one in which human and nonhuman players make decisions to move units around a map, but I’m not sure how to organize that code.

For some game genres it makes sense to attach scripts for behavior directly to NPC scenes. But in a strategy game, there is a changing set of territory controlled by each player and NPC, so their behavior shouldn’t be stored in scripts that are attached to any node on the board that might change ownership.

I suspect that I’ll attach a script for managing game state near the root of the board / map scene, and that I’ll split out that code into separate files if there’s a lot.

But it’s not so clear to me where to put the NPC decision making logic. Should it be contained in an otherwise empty / invisible scene?

I’m using this post to think out loud, so it’s somewhat answered, but any insight is appreciated : )

It depends on how you structure the ai. Big games / simulations will calculate maps on a large interval update ticks (1-5 seconds) eg. danger map, resource map, vulnerability map etc giving each grid cell a score; all the agents in the game use simple logic to respond.

If (danger is < allied strength)

-> aggressive stance

If (danger is > allied strength)

-> defensive stance

If (danger is > allied strength *2)
->Flee

If this is your structure, each agent essentially ‘subscribes’ to a map set (bot) or the players direct orders . The more old school way is for every player (human or bot) to have a big list of which agents it controls and issue orders directly

Attach it to a dedicated node near root, same as you’re doing for overall state. Depending on the game, it can even be a part of state management script if you’re not one of those people who think that 500 lines of code is a lot.