Sign in to follow this  

Game Ai advice

Recommended Posts

aussalmo    108

My first post here. Please forgive the post length. I'll provide some background. I'm a mature-age hobby programmer working in a small team that is working on an addon/enhancement for a commercial combat flight simulator game. The game already has collision models, flight models, aircraft models, maps, terrain textures & all those things necessary for a combat flight simulator. Aircraft and ground objects 'missions' are scripted in a text file ie waypoints with actions to be taken along the path. Objects already interact independently of scripted code. eg. An anti-aircraft unit will automatically start to fire at an enemy plane when the plane comes within range.


What we are aiming to do is to build a persistent-war for the sim. One where on-line players can jump in & the Ai is 'controlling' the war much as a 'General' would control his army. ie deciding where units are sent, what their missions will be, what the current overall strategic situation is like, what the strategic goals of the army objective will be (which may change over time or as the battle progresses). It is intended that this 'war' could run for weeks. The scripting language is C# which I'm confortable using. The game code exposes many different events within the game eg OnAircraftTakeoff method will be fired when an aircraft takes off from an airfield. game events, outcomes, stats and any other needed information would be stored in a MySql database (again we know how to do this OK).


I'm interested in using something like a simulated annealing approach to developing a strategic Ai to 'control/manage' the war. I'm not dumb, I understand the idea behind the algorithm heuristics, but I can't get my head around the practicalities of coding such an approach.


1. How to determine overall META strategy for an army? (This would be equivelent to a General controlling the army)

  • attack enemy airfields (to destroy planes)?
  • attack enemy shipping (to reduce supply)?
  • attack enemy factories (to reduce production)?


2. How would the Ai 'decide' when to switch META strategy?


3. Once META strategy is determined, how to get the Ai to 'decide' which missions to run & what strategy each squadron/army sub unit should use? (This would be equivelent to a wing commander, division Colonal or Brigade Commander controlling forces under his command)

  • Send bombers to bomb a factory? Which factory?
  • Send fighters to scout-out enemy aircraft as a defensive manouver?
  • When to move AA units to be more effective to intercepting enemy bombers?
  • When would a bomber squadron 'decide' not to travel a certain route to target due to AA losses?
Edited by aussalmo

Share this post

Link to post
Share on other sites
KnolanCross    1974

There are a few algorithms to determine your actions, I believe the most simple one is the minimax (


Here is my five minutes explanation to give you the main idea of the way it works (probably not really good, but there are thousands of great articles on the internet about it).


First must define an evaluation function that given an army, determines one score for each strategy, this is probably a hard and subjetive task and pretty likely you will need to put a lot of twinking on it. For instance of points you can check to determine the score of an strategy:

- you can check how well defended are the airfields, the shipping and the factories.

- In case of a direct conflict how many resources you will lost and compare it with enemy loses, you may also include some factor for resource aquisition (for instance, I will lose more resources, but I have a lot remaining, while my enemy has nearly none, so this is good for me) and rate of production (I will take 2 minutes to recreate the lost unities, my enemy will take 4, so this is good for me).


After you got this function, you run it for the possible strategies for your army, then you check the counter mesures your enemy would take and run the function to those, and so on. In the end you will have a decision tree (look at the wikipedia article, this forum won't let me post in svg format). Now you pick the best result when it is your turn, and the worst when it is your enemy's turn (hence minimax). In the end you will filter the path of the best strategy. Keep running this algorithm from time to time to assert if you should keep your plan.


This is one of multiple ways, I believe it is the most simple one, and a good one because it will make your logic more organized (as you just need to twink the evaluation function), otherwise you may end up in an if-else hell.

Edited by KnolanCross

Share this post

Link to post
Share on other sites
ApochPiQ    23004
I'm going to ignore the general engineering issues with your approach and just focus on what's hanging you up the worst: thinking about how to design AI.

The best place to start is emphatically NOT to pick some arbitrary algorithm or architecture and try to cram your game into that framework. That's a recipe for a lot of pain and wasted time.

Instead, you should think about the "I" part of AI. A handy mnemonic is "What would I do?" - not just "I" in the sense of an abbreviation for "intelligence", but also "I" as in "me."

Think about how you would play the game you're writing an AI for. Think about what kind of decisions you're making, and try to analyze why you make some decisions rather than others. The more detail you can pinpoint, the better.

Sooner or later you will reduce your game-playing to a set of rules: when this happens, react that way; and so on. Distill these into general principles (hostility means attacking; fear means running away; etc.) and you have the seeds of your AI. Once you understand how and why the game is played, you should have a pretty easy time turning that into code.

At this point - and no sooner - you can look for architectures that suit the particular rulesets you've come up with.

Share this post

Link to post
Share on other sites
Norman Barrows    7179

Instead, you should think about the "I" part of AI. A handy mnemonic is "What would I do?" - not just "I" in the sense of an abbreviation for "intelligence", but also "I" as in "me."
Think about how you would play the game you're writing an AI for.
As ApochPiQ says, this is all AI does, it just plays the game for you.
So to write AI, you pretend you're the AI, then just write code to do what you would do.
then you test it to see how good your strategy is.
and then comes the tweaking and perfecting. eliminating weaknesses in the strategy, adding more code to handle more special cases or handle common cases even better, etc.
the natural evolution of this process tends to lead to a hierarchy of expert systems and states. essentially a big decision tree. at each decision point, some branch is taken, defining a "state". 
so for example the top level expert system would select targeting planes vs supplies vs production as the overall strategy or "state". lets say it selects "production". so then the production strategy expert system takes over and makes the next lower level decision, and so on, right down to pull the stick back, kick hard left rudder, roll left, full throttle, "gunza! gunza! gunza!".
how an expert system at any level actually decides what to do is up to you. you can use stuff like min-max etc. but usually, just playing the game shows you what the AI ought to do, and it seldom requires weirdo complex AI algos.
for something like planes vs supply vs production, you might apply rules as follows:
1. if any of the 3 strategies can be accomplished much more easily than the others, use it. IE if their air force is almost dead, then try to finish them off. if their factories are almost gone, try to get the few remaining. if their merchant fleet is almost wiped out, go after that as the way to a quick victory.
2. else if they have a lot of planes, go after planes to establish air superiority.
3. else if merchant fleet is almost dead, go after supply.
4. else go after production. going after the enemies means of waging war is usually a very effective strategy.
coding up something like this would not be too difficult.
you might use some variables such as % left alive for air force, merchant fleet, and factories.
then the AI rules above reduce to simple checks like:
if (airforce < 10%) attack_planes_AI
else if (fleet < 10% attack_shiping_AI
else if (factories < 10%) attack_production_AI
else if (airforce > 50%) attack_planes_AI
else if (fleet < 33%) attack_shipping_AI
else attack_production_AI
the above is an example of what the code for the top level expert system might look like.

Share this post

Link to post
Share on other sites
Norman Barrows    7179

the behavior of the example above would be as follows:


at the start of the war, the AI would go after planes. once  the enemy air strength was reduced to 50%, it would switch to factories, unless the merchant fleet was 2/3's dead, in which case it would go after shipping. anywhere along the way, if enemy air power, shipping, or production falls to 10%, it would switch tactics and concentrate on that as a knock out blow.


already one improvement:


irregardless of the strategy (planes, ships, factories) you'd probably want to allocate 10% of your forces to each of the ones not chosen. IE 80% ships, 10% planes, 10% factories, etc. 


you'll also want to take into consideration the condition of your air force, shipping, and factories, and what the enemy is doing. if you're low on factories and the enemy is targeting your factories, you'll want to go after planes to protect your factories and avoid defeat.


often times, the checks for avoiding defeat will appear first:


if (half_dead()) run_away()


followed by the checks for achieving success:


if (hostiles_nearby()) fly_to_closest_hostile()

if (hostile_in_LineOfFire) shoot()

Share this post

Link to post
Share on other sites
LorenzoGatti    4442
The actual task of your AI is providing the combat flight simulator player with fun missions, not winning the war; as its only opponent is a copy of itself rather than a human strategist, it needs to make reasonable and plausible decisions, not particularly optimal ones. Subtly cheating against the player's faction when it's ahead would be a good way to ramp up difficulty for the player and make a campaign last longer. In exchange for not even trying to be optimal, your AI needs to care about what missions the player can choose: among several possible missions of similar type, how can you predict which one is more fun? Is there a good variety of available missions at all times?

Share this post

Link to post
Share on other sites
Norman Barrows    7179

The actual task of your AI is providing the combat flight simulator player with fun missions, not winning the war


so its really a campaign and mission generator, not AI to run a "side" in the game.


different, yet somewhat similar.


I did a space flight sim where a politics engine drove something like a dozen campaign generators, which in turn drove something like two or three dozen mission generators. and the mission generators could generate hundreds or thousands of possible missions, before you even got into number appearing and initial placement. the game had something ridiculous like 58,000 different possible missions.


again there, i used an of AI like expert system (the politics engine) to determine what type of campaign to run (similar to air superiority vs anti-shipping vs anti-production in this case). the generator for a given type of campaign controlled mission flow and assigned missions based on current game state, again using its own expert system to choose missions. a mission generator determined things like target type.


so the approach will be the same, but you'll think about it from the commander-in-chief's point of view: "if i were the boss, what strategies would i pursue under which circumstances?". this gives you your AI rules for selecting strategies.


once a strategy is selected, the question becomes: "given that i'm pursuing strategy 'X', what are appropriate missions to assign, and under which circumstances?".

this gives you your AI rules for assigning missions.


then you just code up the rules into your expert systems. one to choose strategy, and one for each strategy to choose missions. no real balancing required. just a little tweaking of the odds of getting this mission vs that mission kinda thing.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this