Adaptive AI Engine for RTS Games

Discussing the theory and practice

Archive for the ‘RTS Game Programming’ Category

Understanding AI of ORTS Game

Posted by Ogail on October 4, 2009

  • Attacking

from AttackCommander.c file

  1. We have a set of units which will be used in the attack. If this set is empty then return.
  2. Else, we get the center of this mass of units.
  3. Update enemies set by clearing the old one, and making new querying about the current set of enemies.
  4. Get the nearest tank as the target. If there’s no tanks, then get the nearest control center, otherwise there’s nothing to attack.
  5. Get the location of this target.
  6. Define a SquadCombatAI
  7. For each a unit of the attacker units:
    1. Determine the distance square between it and the target.
    2. Given that the unit is idel:
      1. If the target enters the combat distance square , then the unit switch its state to the fighting state.
      2. If not, then make the unit move towards the target, and change its mode to moving.
    3. Given the unit is moving, if the target enters the combat distance square, then fight.
    4. Given the unit is fighting:
      1. If the target exits the distance square, then IDLE.
      2. Otherwise, enable the unit motion or disable it.
  • Base managing

from BaseCommander.c file

  1. We have three main sets : unitsToBuild, buildingsToBuild, and unitsQueued
  2. We also have the obj2buildng set in which, each element is a unit (e.g. worker, builder) associated with the building he works at. So this set needs to be updated each game loop.
  3. For each builder in the finishedbuilders queue, delete the building which this builder he had built from the unitsqueued queue. And then clear the finished builders.
  4. If we have units need of training, then while the unitsTobuild queue isn’t empty:
    1. Pop a unit from the queue.
    2. Find non busy building which can train this unit.
  5. If we have buildings to be built, then start building these buildings. While the buildingsToBuild queue isn’t empty:
    1. Get a bulding which needs to be build.
    2. Try to acquire a worker for this building.
    3. If we have a worker, remove it (to simulate that he has moved to the building).
    4. Add the worker to the activebuilders set.

  • Building

from BuildCommander.c file

If we have a worker and the game in one of the following state:

  • Planning:
    • Find build site.
    • Move to the next site.
  • Ready:
    • Trying to build for 3 times, if it can’t be built, then stop trying.
    • Defining the buildwatch

  • Gathering resources

from GatherCommander.c file

The purpose of GatherCommander is to mine any minerals around a base as efficiently as possible given a set of workers.

  1. If this the first view, then initialize the gather commander by getting the nearby minerals.
  2. If it’s not the first time, and we have minerals, then assess changes to them:
    1. Determining if this mineral is new or not.
    2. If it’s new
      1. Clear the current minerals records set, and fill it with the new set of active minerals records. Now we must clear the newminerals set, as it has been added to the active minerals.
      2. Allocate a route for this mineral, suitable for the number of works of this mineral.
      3. Compute Mineral Clusters.
      4. Compute routes.
  3. Perform workers actions.
  • Defensing

from DefenseCommander.c file

  1. We have a set of units that are used to defend. If this set is empty, so nothing can be done and the base isn’t under control. L
  2. Determine the enemies attacking , and list them in the enemies set. And define a SquadCombatAI object.
  3. If the enemies set is empty, then set the undercontrol flag set to true, and make the following changes for each unit:
    1. Disable the unit motion.
    2. Update the game state:
      1. If the unit state is chasing or attacking, then change it to stopped.
      2. If its state is returning , then the undercontrol flag set to false (as the base didn’t became under control yet.)
      3. If its state is stopped, determine where it is stopped, if it is out of its zone, move it for a free spot, or keep it till we find a free spot later.
  4. If there are enemies, get the best enemy and for each unit:
    1. Get the distance between it and the target.
    2. If the unit state is stopped or attacking
      1. And if the distance is more than 120, then move the unit towards the target, so the state is chasing now.
      2. If the distance is 120 or less, then attack without mercy J
    3. If the unit state is chasing and the distance less than 80, then change the state to attacking.
    4. If it is attacking already, then enable unit motion, and put this unit in the back of the units set.
  • Scouting

from ScoutCommander.c file

  1. we have a set of units work as scouts (explorers) , for each one of them:
    1. Find the borders locations, if the set of locations is empty, then set the finished flag to true.
    2. If the scout is stopped, then get the location with best distance, and move the scout to it.
    3. If we don’t have any more locations to move scouts to (finished ==true), then start Reconnaissance( gathering information)
    4. Retrieve active minerals which were unknown for us.
    5. For each scout:
      1. If it is stopped, get a location of any random one of the found active minerals
      2. Move the scout to it.

About running the game:

What I could do is to run the two .exe files (orts, and ortsg), but I couldn’t issue any game test from the tournaments folders nor adding any sample AI code, because I couldn’t run the game until hours from your call.


Posted in RTS Game Programming | Leave a Comment »

How to Develop Strategy Games

Posted by Ogail on June 26, 2009

How to Develop Strategy Games

Article source: Strategy Game Programming with DirectX 9.0

  • Choose your game story theme:
    • Science Fiction الخيال العلمي
    • Medieval: العصور الوسطى
    • Western: الغرب
    • Post-apocalyptic: بعد دمار الأرض
  • Story elements:
    • The plot
      • This gives game depth and meaning
      • Gives back group information about the game
      • NOD example
      • Plot defines series of events that lead to the purpose of the game
      • Plot could be based on: war, deception or finance
    • The purpose
      • Gives the player reason for winning the game
  • Setting Objectives:
    • Real tasks that the player should do to win the game
  • Notes:
    • Always add milestone goals
    • Always remember balance is the key
    • Make cost of anything mixture between many resources, this will make player not concentrating on a specific resource
  • Empire Earth Goals Flow Chart:

  • Unit Characteristics:
    • Cost
      • Make balance between cost and strength
    • Unit Speed:
      • Many games make speed relative to armor/power
      • Think about new ideas like considering the environment in your calculations
      • Calculating Unit Speed:
        • Make a basic system from 1 to 100, 1 being slowest 100 fastest
    • Unit Armor:
      • Referring to defensive value of a unit.
      • Calculating defensive value:
        • Problems is you want certain types of armor to react differently to various weapons
        • Create a table:

  • To calculate damaged effected: inflicted x armor rating = points defected to armor
  • Hit points:
    • Each unit should have a hit points that after consuming them the unit is destroyed
  • Unit Firepower
    • Take into consideration:
      • Rate of fire
        • More powerful weapon, the slower it fires
        • To calculate it you should determine how many times/minutes a weapon can fire
      • Damage type
        • Imagine many damage types (fire-based, projectile-based…)
      • Special damage
        • Like when Nuclear Missile is Lunched there is a dirty that kills people after it
      • Velocity
        • The leaser beam is faster than projectile because its as speed of the light
        • Calculating projectile velocity:
          • Like the unit speed, but a standard system that 1->100
  • Resource Management:
    • Pick Your Poison:
      • List the goals that the player should satisfy via resources:
        • Feed the population
        • Build an infrastructure
        • Raise an army
      • Define resources:
        • That will satisfy each of the previous goals
      • Gathering resources (Constraints to Gather Them):
        • Could be manually or automatic
        • If automatic you should ask the question: How does the player build building?
        • While you define resource try to define goal with it (like fisherman near sea)
      • Determine Gathering Rate
    • Resource Balance:
      • TA example (only 2 resources in any portion of the map)
      • Balanced example (grow rate/carrier)
  • RTS Technology Trees:
    • Design technology example:

  • Different Types of Technology Trees:
    • Infrastructure
      • Deals with buildings
      • Without it the player can’t progress to bigger and better things
    • Weapons (and let loose the dogs of war)
      • If the tree is complicated then the player has no clue what the weapons mean!
    • Upgrades
      • Upgrading infrastructure and units with paths
  • Keys is choosing cost of technology:
    • Be sure the technology takes time to research. The more valuable the technology is, the longer it should take to invent
    • Be sure the technology requires resources that pertain to it. In other words, do not make a technology require a resource that has nothing to do with it
    • Be sure the technology costs a reasonable amount. If the technology is very valuable, it should cost a good deal of resources. Do not go overboard, though. If the technology costs more resources than it is going to save, it is worthless.

Posted in RTS Game Programming | 1 Comment »

Game Architecture

Posted by Ogail on June 26, 2009

Game Architecture


  • Any game is consisting from 2 parts:
    • Game Engine
      • Rendering Engine
      • Audio Engine
      • Input Engine
      • AI Engine
      • Scripting
      • Networking
    • Game Logic
      • Connecting all previous engines together
  • Game engines are developed in component based architecture
  • Strategy games way of thinking:
    • Micromanagement: maintenance of resources and individual units
    • Macromanagemet: economic development and large-scale strategic maneuvering
  • Game tactics:
    • Rush attack
    • RTS Games are stochastic and TBS Games are deterministic
    • The difference here is that, in real-time strategy, a decision has to be arrived in a short time-frame, so the best conceivable course of action in that time-frame is used
  • Tactics vs. Strategy:
    • Tactics = Short-term military objectives

Strategy = Long term military objectives

Posted in RTS Game Programming | Leave a Comment »