• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Where To Start With Strategic AI?

Hello Everyone,

It has taken a long time, but I have finally created the art assets that I need to make my strategy game. I know exactly how my game will work, but what I want to do is teach the AI how to play it. So I'm just looking for some pointers on how one begins such a task.

In the month and a half that I've owned Game Maker 2 I've learned about many things. It seems to me that the most relevant to my current task would be states, yet do they alone offer enough complexity for the task at hand, or should I be looking to something else, maybe something that I haven't learned about yet?

Just to give a scenario from my game: let's say there are two AI nations on a map and they oppose one another. Yet, each AI nation needs to know that it should prioritise dealing with other neutral entities on the map at the beginning, because they pose a greater threat than the opposing nation. How would you begin to craft an AI that is capable of making such decisions dynamically and based upon the resources available to them? Would it just be a case of having the AI enter into a one of a long list of states based upon their present circumstances?

Just for the record: I already understand motion planning grids and have implemented one. It's AI behaviour that I'm inquiring about here.

Edit: And diplomacy is also a factor here, which makes the task at hand even more complicated to me.
 
Last edited:

poorlocke

Member
I would suggest attributes and states.

For example the attributes would be integers that define the ai behavior like

GML:
//Minimum attribute 0 maximum 5

aggression = 2; //This defines the frequency of attacks in game days
curiosity = 4; //This defines the frequency of scouting missions
industry = 3; //This defines how much priority is given to developing new technology
etc...

//Then battle behavior states
enum aiBehavior
    {
        neutral = 0,
        aggressive = 1,
        defensive = 2,
        strategic = 3,
        etc...
    }
Then at the ai controller step I would have certain timers that trigger either on a set frequency or when the player interacts with the ai. Something like
GML:
if timeToCheckAiUpdate

    {

        switch (aggression)

            {

                case 1:

                    //Send troops outside ai base gates to guard

                break;

                case 2:

                    //Send more troops further away from the gate

                break;

                case 3:

                    //Send my second to best troops to attack the player

                break;

                case 4:

                    //Send my best troops with small support to the player

                break;

                case 5:

                    //Keep the base guards and send everything to the player

                break;

            }

        switch (curiosity)

            {

                case 1:

                    //Send scouts just around my base for resources

                break;

                case 2:

                    //Send scouts further away for resources

                break;

                case 3:

                    //Send better scouts even further for enemies

                break;

                etc...

            }

        switch (industry)

            {

                case 1:

                    //If resources allow, build basic stuff

                break;

                case 2:

                    //If resources allow, build slightly advanced stuff

                break;

                etc...

            }
        //Now do a check on the other factions
        if neighbor_1.aggression > aggression
            {
                //calculate a risk factor and make changes to my attributes
            }
        if neighbor_2.industry > industry
            {
                //calculate an industrial development factor and see if you can get
                //more resources to advance my industry
            }
        etc...
    }
This is extremely basic and on-the-fly but this is a general idea to get started with. Of course you would need more complex attributes for your AI.
 

TheWaffle

Member
This is a hard question to answer without fully understanding your idea....

BUT

If you fail, you may become frustrated (like I did in my turn-based war game with Nucs) ... It is playable as is, but without AI there is no game :(
SO.
Think about what you want the AI to do. PLAN THAT FIRST before doing anything else!! Very hard to add that later. Experiment with ideas ...
DO NOT USE Neural Networks .... slow and just don't do the job. Learning curve sucks too ....

do some mini-projects that you can use to test AI ideas.....

1 - how to search for things .... resourses, enemies, bases ......
2 - how to hide from enemies .... when to run away if outnumbered ...
3 - test line of sight concepts .... maybe use a round ball with an arrow to show that ....
4 - path finding ... (always need this, you will probably need a custom one, GM is too generic)
5 - some kind of goal system ... some things will take minutes for your AI to figure out,
you need some way to pause the "thought" and resume it later ... example:
in pathfinding, I sometimes break this down into several steps ...
a- clear the grid system to make room for the next search
b - fill the grid with the important items (a and b can be combined sometimes)
c - fill horizontal checks
d - fill vertical checks
e - fill diagonal checks
f - compare solutions / if none found, try adding flood fills and recheck on step g

just an example. But, for all thinking problems, you need some method of breaking it down ...
in my Nucs game, possible moves listed, ranks of moves, possible threats (from area weapons), then evaluate, decide
BUT *******
the MOST IMPORTANT THING is YOU MUST HAVE FUN....
If testing the AI is not fun, YOU won't do it and give up.
So, find a way to make it fun to do ....

Then, port it as a module into your main game project .... so each AI component would be it's own module and
was tested by itself without any other concerns ...
 

Binsk

Member
I think the biggest thing when approaching this is to tackle it in small bites. Implement little pieces at a time that work stand-alone and then try weaving them together. As for actually weaving them together to make an intelligent AI there are a number of tools you could turn to but a decent one I would suggest looking into implementing would be a GOAP system (Goal Oriented Action Planning).

If you are familiar with path finding, like A* or something, you can almost think of GOAP as A* for logic. The general idea is you design a whole bunch of 'action' nodes. Each action is effectively a generic task with a 'cost' weight attached. This weight can be updated depending on game state. You then use your GOAP system and give it a target goal and it stitches the best series of actions together to get there (like an A* path; least-cost route wins).

You then go through this list of actions and process them one by one. The GOAP system shouldn't actually handle executing the actions, it should only handle the planning. Each action might have its own state machine and series of things to monitor / execute but if each action is handled independent to everything else it can provide a clean AI system that is easy to implement and modify. Need a new feature? Add a new action w/ a weight and a new stand-alone state machine to process it. Done.

Super generic overview but that would be my recommendation when it comes to AI that need to plan and keep track of lots of moving parts.
 

Mr Magnus

Viking King
There are a lot of ways to handle a complicated AI, but in general you'll need to plan it out. Imagine how you'd solve a problem if you're an AI, and try to get that down to a process that you can code.

A common method is to give the AI all the actions the AI can perform, and ask them to somehow give each action a score or a rating.

Another is the blackboard AI. You have a bunch of simpler AI's that each are tasked with finding solutions to specific problems: one AI is focused on military. Another is focused on the economy of your country. another is focused on what it should research. All these AI will then suggest what they want to do to a "blackboard": a central AI that takes these ideas and tries to weave them together and prioritize the actions most important right now.

Another is a statebound AI: the AI has a series of states that inform what its priority is right at that moment. When the state machine warrants it it will switch to a new state and then go prioritize something else, and so on and so forth.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Chess AIs these days commonly use a method like this:
  • Sort all possible moves and score them, so that a move that moves you towards winning increases your score (eliminating enemy pieces, holding favorable positions, etc)
  • For all of THOSE moves, repeat the procedure, giving you a tree of possible moves (and repeat this recursively for as long as practical)
  • Go through the tree and see if you can find a state where you won. If so, try to steer play towards this state by taking the moves that led there.
  • Of course, your opponent is also trying to win. So you might as well ignore any path where they take bad moves (they're not gonna happen) and only search down subtrees where they make, say, the 5 best moves.
I forget what this approach is called, but the big idea is that you're predicting what your opponent will do and optimizing your playstyle for that, instead of trying to be smart in a vacuum
 
Thanks everyone. Sorry, I should have been more clear about the kind of game I'm making: it's an Age of Empires style top down game where the AI has to gather resources, produce units, and engage in diplomacy. Actually it's closer to the Seven Kingdoms games than anything, though they're not as well known.

So it seems that I should start seeing what I can do with states, maybe have a starting state for each nation called "Scouting" or "Expansion" and see what I can do with that. Each settlement has a population, one third of which the AI needs to be able to distribute among five different areas of research based upon its needs.
 
Okay I've attached a screenshot of my little test area to better convey the kind of game I'm making here.

There's the blue Atlantean settlement, and the gray neutral settlement (the game has three eras of technological progress, and this is the "Primitive" era where all nations are pretty much the same, aesthetically). The Atlantean settlement has a population of 100, one third of which the AI can assign to different tasks at any given time. This also includes removing six of them to form a warband on the world map (population grows at 1 new citizen per minute up to a maximum that depends upon the kind of settlement it isā€”100 in the case of villages).

The actual mechanics are very simple. The neutral settlement has a defensive value of 5, whereas an Atlantean warband has a combat value of 2, meaning that a minimum of three Atlantean warbands would be needed to overcome the neutral settlement and turn it Atlantean.

So even with this simple interaction, the Atlantean AI needs to understand the way in which it can utilise the usable population that it has, it needs to recognise the defensive value of its target settlement and recruit the appropriate number of warbands to deal with it, and it then needs to send those warbands to do so.

Ultimately, however, combat will not be the only way to gain control over other settlements, be they neutral or of an opposing nationā€”the AI should be able to do so through cultural subversion and religious conversion too. However, if I can understand how to get the AI to figure out how to attack a village in the way I described, then I can apply the same logic to everything else. It's just finding that starting place.
 

Attachments

Last edited:

Rob

Member
If your AI has an overall state, eg expansionist_aggressive or expansionist_cultural, you can let it know all of the neutral settlements in the game (or within a radius) and prioritise them on closeness/ease of "capture" (militarily for expansionist_aggressive etc).
The AI should know how many "points" it needs in whatever area (army/religion/culture) to "capture" that settlement, and then act accordingly.
 

TheWaffle

Member
I have done much much "reading" on where to begin .... if still lost, start here:

behavior trees

State Machine theory is too restrictive (special) for advanced AI .... State Machine is best used for "simple" problems
such as animation management or attack/run away AI .... for more complex stuff, you need a tree ....
BEFORE you watch this video ....
GET COFFEE or whatever ..... AND
GET COMFORTABLE .... prepare for things to get deep fast ....

Next, if all this makes sense, decide how to use it ...
The simplest way is just a massive amount of if statements ...
You can break it up with scripts ... but, at the root of the method, that is what this is.

I'll be implementing something like this for my RPG game for the quest system...

shaun spalding quest

Shaun's quest system is actually a great example of a simple behavior tree.
The advanced options in the first video would suggest spawning "thinkers" (my word)
to monitor game states and AI ... Shaun talks about that too but felt his example was too simple...
 
Top