Open General
by Luis Guzman

THE GAME AI

If you want to support
this site, please ...

Introduction

Although I think that Pg2 AI is not bad at all, the AI for OpenGen was developed from scratch instead of trying to replicate the Pg2 AI.
The implementation has been made based on these ideas:

  • The main objective for the AI is not to win the scenario, but to avoid losing it. However, the AI will not waste the chance to win if an opportunity presents itself
  • The AI must focus on maneuver and combat, and not so much on increasing its prestige by taking cities that do not provide tactical value.
  • The AI should not be too predictable, so that small differences in the human player's actions or in the combat result should generate significant variations in the actions taken by the AI, making the scenarios more re-playable.
  • The AI should not be somehow cautious, avoiding as far as possible to easily lose their units or letting themselves be surprised in unfavorable battles
  • AI units should be able to be configured with different stances, in order to mix offensive units with defensive ones, instead of being all defensive/aggressive
Thus although existing Pg2 campaigns can be played with Open General, they will play different due to the different AI.

Currently there are two playable versions of the AI: 0.91 (2013) and 2.0 (2014). The version to be used can be defined in the scenario (to play standalone) and in the campaign file (to play campaign). AI setting in Scenario/Campaign can be defined as "Default" to use the default ai version defined in the game executable. Once the scenario is loaded the player, can force to use a different AI version, but this option was added for testing purposes and could be disable in future releases.

Default AI version in game executable is 0.91 (2013), to allow scenarios made before AI 2.0 to play as designed, but Suite will create campaigns and scenarios to use Ai 2.0 by default

Next sections refer to AI version 2.0 as that version is what should be used.

How the AI works

The AI lacks of a strategical analysis phase in the current implementation, so it figures objectives and actions based on the information available at start of the AI player turn, not remembering the enemy positions at the end of the previous turn.

All AI units are assigned a Stance, either at design time in Suite, or by assigning the Stance defined for the AI player when the scenario is loaded.
Unit's Stance plays an important role on how the unit will behave.

When AI turn starts, the AI first set up a list of all the Important Hexes (IH), a list of the known enemies and a list of its own units. Then loads the custom config file/s if any, and create the list of valid units available to purchase in that turn, storing also the cheapest purchasable ground and air unit. Once the general data for the turn is set up, the AI start doing its work using this sequence or ai-phases:

  1. Scan Recons. AI Scan units able to use phased movement to use part of its movement allowance to open the area spotted. No attack as performed in this phase
  2. Deploy units from HQ if any.
  3. Initial fire. AI checks units from class: ATY, AT, FORT, AD/Flak, able to fire, to eventually bombard enemies in range
  4. Figures the hexes where its fighters can fly to escort/protect adjacent hexes, so Bombers, Artillery and other sensible units can expect some air coberture.
  5. Scan Units#1.. Scan "combat" units not embarked to eventually take actions
  6. Check again the hexes that fighters can protect, if their units move there.
  7. Scan air embarked units (except if airborne) to eventually move/drop them
  8. Prestige assignment and Purchasing new units
  9. Scan owned VH "in danger"
  10. Scan airborne units embarked to eventually move/drop them
  11. Scan Units#2. Scan units able to take actions yet, for a last chance to take actions

AI tries to keep their units alive, thus when any unit is short of ammo/fuel the AI either resupplied or moved to a safer position to resupply in next turn. For air units requiring fuel, the AI checks, when activating the plane, if it should fly toward a supply position to avoid crashing a the end of the turn or later.

And when units have loses below a threshold defined as percent of unit's base strength, the AI will reinforce/move to a safer position, to avoid losing them.

When AI has to scan units, it create a list of units likely to take actions, sorted by different criteria according the task to do, then each unit in the list is activated and a Tactical Map is set up pondering the goodness of each possible position/combat for the unit.

Players can enable to record the AI turn to a text file (ai_turn.log) which is saved into the Efolder's OPENLOG subfolder, which is a handy tool to analyze how AI worked out its turn.

Suite can set (at design times) some modifiers (Units View ..AI Tab) in order to change any AI unit behavior.

  • Hold position until turn.. the unit won't move until the givent turn
  • Avoid auto-hold = entrench.. Units entrenched at start of scenario, will hold its position until a turn bigger than the initital entrench value. This settings disables this rule
  • Anchored.. This setting makes the unit to hold its initital position as long as it's not forced to withdraw. These other ways are equivalent to this Suite setting:
    - Assign a transport with no movement
    - Set initial entrench bigger than 9 (>=10)
  • Fearless.. This setting makes the unit much more aggresive because it ignores own casualtis when figuring tac-map values
  • Set OH.. Define a specific OH for the unit, avoiding the engine to figure itself
  • OH from unit.. Define a specific OH for the unit, based on another "leading" unit
  • Follow/Inherit.. Tick to follow the actual position of the leading unit. Untick to copy in the unit the OH of the leading one
  • Free OH.. Define when the OH set at design should be released, avoiding to stick the unit close to the OH, if that OH cannot be entered.

Some terms used

SP Strength
ShouldResupply Unit is running out ammo or fuel or bot
ShouldReinforce Unit's SP are below (60% / 40%) depending unit stance (defensive / aggressive)
ShouldRefit ShouldReinforce or ShouldResupply
Weak unit Unit ShouldResupply or ShouldReinforce
EH Escape Hex
AP AirPort (Airfield)
FH Flag Hex giving prestige
IH Important Hex for AI: VH, SH/Port, EH, AP or FH
ATY Artillery class
AT AntiTank class

Assigning OH

The scenario file includes a flag to know if unit's OH is assigned at design time or later by AI engine. The OH set at design time is released when the unit is closer than the distance set at design time to release it, and then the next OH will be assigned by the AI engine. If unit has assigned (not released yet) an OH from design time, AI engine does nothing.

When the OH is not assigned at design time, the AI figures the best OH every time the unit is activated.

The first thing AI checks is if unit should retreat using an EH, then it assigns the closer EH as the unit's OH if any exist. Notice that maybe there is an enemy unit in the EH, but the EH never is captured, so AI keeps seeing it as a EH to escape through it.

If the unit's stance is defensive and anchored or holding a VH, the AI assigns that hex as the OH (thus the unit won't move), otherwise (not anchored nor guarding VH), the AI checks for the closer VH in danger and if any, that VH is assigned as the unit OH. If no VH is in danger, the closer owned VH is assigned as the OH.

If unit's stance is aggressive the closer VH controlled by enemy is assigned as OH and if enemy doesn't control any, the closer enemy unit's position is selected as the OH

For planes (either air units, or ground units embarked in air transport) the AI engine use a different logic to assign OH.

  1. For air units (Fighters, TB, LB)
    • if plane can run of supply, OH is set to closer free airport
    • if plane have low strength, OH is set to closer free airport
    • if plane is defined to retreat through a EH, the closer EH is set as the OH
    • otherwise the closer own VH (if stance is defensive) or closer enemy VH (if aggressive) is selected as the OH
    • if no OH is set yet, closer enemy position is set as OH for TB/LB
    • Fighters are usually moved to escort as many units as possible, but if no escort mission is needed, it is assigned the closer enemy plane as the OH

  2. Airborne units embarked in air transport
    • If airborne stance is aggressive, the closer enemy VH is set as the OH
    • If airborne stance is defensive the farthest enemy VH is selected as the OH

  3. Not Airborne units embarked in air transport
    • The closer airfield not occupied by enemy is set as the OH for that unit

The Tactical Map

Once created the list of units according a given criteria, the AI activates each unit in the list to conclude which is the best action/s to do.

When activating the unit, the AI does:

  • AI update list of current ownership of Important Hexes (VH/SH/AP/EH/FlagH)
  • AI create the list of the possible movements that the unit can move adding the starting position of the unit
  • AI assigns an OH to the unit (if not assigned yet by designer).
  • AI creates a tactical map representing the goodness of each position in the list to be filled with a value according a bunch of factors.
  • AI scans all known enemy units to evaluate its movements (as if map had no AI units) and the attacks that each enemy could do in next turn to the active unit, if that active unit would move each position in the list. That info is stored in the tactical map
  • AI scans their own units (except the active) to store in the tactical map the hexes that will be covered by Air/Ground Support Fire.
  • AI scans all hexes from the list to figure each tactical value, by assigning the different bonus/malus according the factors affecting the hex, and eventually adding the attack value of the best target to attack from that position
  • Finally the AI decides what is the best action/s to do according the higher value got in the tactical map.

    The most important factors adding/substracting to each hex's tactical value are:

    • The importance of holding the starting position (garrisoning IH or being highly entrenched)
    • The importance of the hex to move (VH,SH,EH,AP ...) to move/hold
    • The importance of the defensive position when unit has a defensive stance
    • The distance to the OH assigned: as closer as better (this makes units to move toward the OH)
    • the casualties that can suffer in next turn.
    • the risk to be surprised (moving through unspotted hexes)
    • the risk to be attacked by many enemies on next turn
    • if blocking friend/enemy movements
    • The support that can receive from the friend support units (Air/Ground Support Fire, Combat Support, Bridge units, fighters escort ...)
    • the support that can give to friend units (if artillery, AD, fighters, CS units, bridge units ...)
    The amount of bonus/malus depends on class units and relative importance of the factors. Also for air embarked units some other factors get more importance.

Attack value

To evaluate how good is attacking an enemy unit, the AI evaluates the combat forecast.

if the unit is a Kamikaze unit and estimated enemy loses is more than 1 SP, the attacl value is a factor of the cost of the target unit relative to the cost of the AI unit. If the enemy can be killed, the value is increased by a 10% plus the cost of the carried units if target is a container. No more values are added/substracted.

if unit is Fearless, its own casualties are discarded to get the attack value.

Otherwise the attack value (AtkVal) is set as 3*Enemy loses - 3*Own loses (all loses estimated).
This value is increased when:

  • enemy casualties are zero and unit is TB or LB (to make 0-0 forecast a bit more attractive for TB/LB)
  • If overrun is expected
  • If target is MSU and victory conditions are affected by enemy MSU. And even more if MSU is the last one to win
  • If forecast expects to kill the enemy
  • If position of target is VH to take or SH/EH
  • If position of target is in zoc of enemy VH/SH/EH
  • if target is in zoc of own VH
  • if target is in zoc of friend artillery
  • if enemy loses can turn the target as weak
  • if AI unit is LB having BombSize
  • if TB/LB and target has another enemy adjacent (so if fighter comes to escort can attack also)
  • if fighter attacking enemy air unit with favourable odds
  • if target is a container, and even higher if carrying other units.
  • If destroyer attacking enemy submarine
  • If submarine attacking enemy unit embarked in naval transport
  • If submarine attacking enemy naval unit (except submarines)
  • if target has suffered other attacks in this turn
  • if target has range of fire bigger than 1
  • if target has entrench bigger than 3 (bigger if attacker is artillery)
  • with a bonus depending on target unit:
    - Artillery and carrier=5; Fighter=4; TB/LB=3; AD/Flak=2; Recon=1; Rest=0;
    - Core+3; Leader+2; Engineer+1; Overstrength+1
And decreased when:
  • If AI unit is MSU and victory conditions are affected by own MSU, and much more if the AI is losing if killed
  • if own loses can turn the ai unit as weak.
  • if AI unit is hidden (so don't reveal for a minor combat)
  • if Fighter in first scan phase and target is a naval unit (should wait to scan#2 phase to attack)
  • if AI unit is a container
  • If resultant own loses are bigger or enemy loses lower than the threshold set for the phase and AI unit class, the AtkVal returns zero

Scan recons

This first phase tries to increase the spotted area, by moving units able to do phased movement in a way they can retreat if meeting too strong enemy units.
These units are skipped regardless they can move phased:

  • Embarked units
  • ATY/AD class units
  • MSU units if affecting victory conditions.
  • Units having 3 or less movement allowance
  • Weak units (as they should Resupply/Reinforce
  • Air units using fuel if current fuel is less than twice the movement allowance
  • Infantry located in VH/SH/AP
  • AD/AT entrenched in VH/SH/AP, if blocking enemy movement or the hex is in danger because of many enemies close

Units move toward the OH. using half of its movement allowance if there are enemies on sight. If they have the opportunity to capture spotted IH they will do

Initial fire

In this phase the AI scan units, deployed on map, from classes: Artillery, AD, Flak and Fortification.

For each unit, it checks all the available targets in range of the initial position and evaluate the attack value firing. That value is reduce by 10 if unit has low ammo/fuel or strength is lower than 60% / 40% for (defensive/aggressive stance)

Then the unit fires to the target having the bigger attack value

Scan units #1 and #2

in phases Scan units #1 / #2, the AI sorts the units so that it can activate them in this sequence:

  • Submarines, Destroyers, Naval Transport (embarked ground units), Light Cruiser, Battle Cruiser, Battleship, Carrier
  • Air Transport (if standalone), Tac Bomber, Level Bomber
  • Infantry, AntiTank, Tank
  • Artillery, Flak, AirDefense,
  • Fighters
  • Ground Transport (if standalone), Fortification, Recon

these units are skipped in scan #1 phase

  • If cannot move not fire
  • Embarked units in air transport
  • Bridge units laying pontoon on river
  • If CombatSupport special/leader
  • if "defenders" (ATY, Flak or AD)
  • if Air Unit (Fighter, TB, LB)
  • If anchored and ammo >3, only available targets from its position are checked
  • if anchored or ShouldRefit
  • if unit is Saboteur and ammo <5

In scan #2 phase, only these units are skipped

  • If cannot move not fire
  • Embarked units in air transport
  • If anchored and ammo >3, only available targets from its position are checked
  • if unit is Saboteur and ammo <5

The unit is activated, OH is checked and eventually assigned and their tactical-map is reset.
Different routines and combat threshold combat values are used depending the unit is:

  • Ground Unit embarked in Helo Transport.
  • Ground Unit embarked on Naval transport.
  • Naval unit
  • Air unit
  • "Defender" unit (ATY, Flak or AD)(
  • Recon unit
  • Rest of ground units (not Defenders nor Recon)
    • Once the tactical-map is updated with values for each position (including the values of possible attacks from each position), the best hex to move(best-position), the best target to attack (best-target) and if marked-to-disembark flags are checked by the engine to decide the action to take the active unit. The

      for Scan #1 phase:

      • if unit was embarked and has movement available and is marked-to-disembark and can disembark, it does.
      • if best-position is different than initial position
        • the active units moves to that best-position
        • if unit was embarked in HeloTransport and is marked-to-disembark in the new position and can move yet, it disembarks
        • if unit can clear mine in the new position, it attempts to remove it
        • if after moving, the unit cannot fire (probably because a surprise contact) the best-target is removed
      • if unit entered in hangar, no more actions are checked and best-target is cleared
      • if best-target is set, and unit moved, odds are checked again, and if still within valid threshold, the unit attack the target
      • if unit was defending a VH but get weak as to be probaly killed next turn, and can move out of the VH yet, and there are other unit able to enter its position, the unit moves out of the VH

      for Scan #2 phase:

      • If unit needs refit, it resupply/reinforce in their position an no further action is taken.
      • if unit was embarked and has movement available and is marked-to-disembark and can disembark, it does.
      • if best-position is different than initial position
        • the active units moves to that best-position
        • if unit was embarked in HeloTransport and is marked-to-disembark in the new position and can move yet, it disembarks
      • if unit can clear mine in the new position, it attempts to remove it
      • if unit can still fire from its position, checks possible tatgets and if any is within valid threshold, the unit attack the target
      • if after attack, unit can yet move, it is activated again
      • if Recon-type unit with remaining movement after taken any action, are activate again



Scan ground units embarked in air transport

As these units needs an airfield where they can move once disembarked, the AI always checks the first if the unit is over a position it can disembark, and if so, the unit is disembarked

AI will scan all available positions to move, discarding the hexes farthest to the OH than the distance to the OH from the starting position.

AI will try to avoid, hexes with expected loses higher than 2. Aggressive stance increase the loses accepted to 3, and if Fearless the unit ignores loses restriction. But if expected loses are less then the expected loses in the initial position, the position is considered although with a lower bonus

Flying through unspotted hexes or through enemy spotted hexes en range of enemy fighters are avoided too, unless bad weather.

The position were the unit can disembark get a high bonus, meaning that the unit will try to disembark on the first possible airfield it can, whem moving toward the OH.

Scan airborne units embarked in air transport

AI will scan all available positions to move, discarding the hexes farthest to the OH than the distance to the OH from the starting position.

AI will try to avoid, hexes with expected loses higher than 2. Aggressive stance increase the loses accepted to 3, and if Fearless the unit ignores loses restriction. But if expected loses are less then the expected loses in the initial position, the position is considered although with a lower bonus

flying through unspotted hexes or through enemy spotted hexes en range of enemy fighters are avoided too, unless bad weather.

When distance from any hex to the OH is less than the unit's movement allowance in ground +3 (+5 for ATY) but farthest than 2 (4 if ATY), and the unit can move by itself into the hex's terrain, then AI takes the position as "drop-able" assigning a bonus, which is bigger/lower depending on expected loses and own/enemy spotted.

The highest bonus is given to drop-able positions when the unit can actually drop into that hex

AI will drop the unit if starting position is drop-able or move to the hex with the higher tactical value, dropping it possible (phased movement and remaining movement)

VH in danger!

AI assigns a danger-level to every VH/SH/EH, at any time a unit is activated, when purchasing new units and when checking "VH in danger" phase. Higher danger-level (10) is when VH/SH/EH has no unit and enemy can move into it one or more units in next turn. If not empty but the ai unit is blocking at least 2 enemy units to move into, the danger level is 5 and if there are at least 2 enemy units than can move adjacent to the hex, the danger-level is 3.

When checking "VH in danger" phase, the AI scans only owned VH/SH being empty or with a danger-level>0.

Any unit able to move (not anchored, not holding, not defending already an VH/SH) are checked and if danger-level is less than 10 then units having CombatSupport special/leader, or having GD+GDM less than 5 or strength less then 40%, or entrenchment bigger than terrain entrenchment or not belonging to infantry class ,are not considered.

Then all units matching the aforementioned conditions are checked and the one that can move closewr to the threaten VH/SH is moved to protect it.

Purchasing

AI doesn't use its total prestige to purchase new units, as it reserves some prestige to reinforce units. AI figures all the prestige needed to reinforce all units with SP below the SP base at the time this phase is checked. AI reserves the percentage defined by $buy_refit (default is 70%).

If the remaining prestige is less than the cost of the cheaper unit or if less than $buy_minpres or the AI cannot deploy units, then the AI doesn't purchase anything.

If AI has prestige to spend, it first check if any VH is in danger and if it can purchase a unit to defend it moving from the closer SH.

If AI still has prestige it assigns the percentage defined by $buy_newair (default 50%) to buy air units in sequence Bomber,Fighter,Bomber, etc .. .

If still AI has prestige it scans owned SH and buy new units in the sequence defined by:

$buy_aggresive (default to 2, 1, 9, 5 ,1, 4, 2, 3) if AI global stance is defined as aggressive
$buy_defensive (default to 1 ,1 ,9 ,10,1, 4, 9, 2) if AI global stance is defined as defensive
Note: the numbers are the class id to buy, meaning 1 ,1 ,9 ,10, 1, 4, 9, 2 that AI will try to buy INF, INF, ATY, AD, INF, AT, ATY, TANK, looping through the sequence and buying one unit of the type defined until running out of prestige.