Author Topic: Food for thought ( about AI )  (Read 2237 times)

Mr War

  • Guest
Food for thought ( about AI )
« on: 29 August 2011, 11:08:49 »
I already use build limits to prevent the AI over building barracks, but often the AI doesn't build my farm equivalent building, so their troops die of lack of food.

How does the AI determine which buildings to build? And is there any way of influencing it within the xmls, other than build limits which are useful but do not solve all probs.
« Last Edit: 1 September 2011, 10:00:48 by titi »

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,238
    • View Profile
Re: Food for thought
« Reply #1 on: 29 August 2011, 19:02:12 »
I'll take a look at the code to give you the most accurate answer I can give (the answer depends on which CPU AI player is selected as each level has different priorities). I'll edit this post as i discover answers.

UPDATED:

In general the AI processes rules in the following order:

AiRuleWorkerHarvest
(called every 2 seconds)
What this rule does: Finds an idle harvester unit and tells it to harvest a resource
 
AiRuleRefreshHarvester
(called every 20 seconds)
What this rule does: Finds a harvester that is harvesting and re-assigns it to harvest a resource based on new priorities

AiRuleScoutPatrol
(called every 10 seconds)
What this rule does: If there are a minimum of 8 fighting units, send a unit out to scout the map (one of the other players start location)

AiRuleUnBlock
(called every 3 seconds)
What this rule does: If there are blocked units (units that are surrounded by other units) then tell units surrounded the blocked unit(s) to move away.

AiRuleReturnBase
(called every 5 seconds)
What this rule does: If there are idle units send them back to the base

AiRuleMassiveAttack
(called every 1 second)
What this rule does: a) If there are enemies in the sight of the AI player and b) the AI players base has at least 8 fighting units (then attack enemies anywhere on the map) OR if the AI players base has less than 8 fighting units, attack enemies within 25 cells of the base first.

AiRuleAddTasks
(called every 5 seconds)
What this rule does: if there are no existing tasks queued or there are less than 4 worker units try to add one of the following tasks (in priority order shown below):
  1. if there are less than 4 worker units, produce more workers
  2. For CPU Mega:
             Workers:
             - If worker units < 5 produce workers
             - If worker units < 10 produce more workers
             - If worker units ratio to all units < 20% produce more workers
             - If worker units ratio to all units < 30% produce more workers
            Fighters:
             - If fighter units < 10 produce fighters
             - If fighter units ratio to all units < 20% produce more fighters
             - If fighter units ratio to all units < 30% produce more fighters
             - If worker units >= 10 produce more fighters
             - If worker units >= 15 produce more fighters
             - If fighter units < 8 produce more fighters (also if > 9 buildings produce more fighters, also if > 12 buildings produce even more fighters)
            Buildings:
            - if building count < 6 or building ratio to all units < 20%, build a new building
            - if building count < 10 and worker count > 12, build a new building
           Upgrades:
           - if upgrade count == 0 and worker count > 5, start an upgrade
           - if upgrade count == 1 and worker count > 10, start an upgrade
           - if upgrade count == 2 and worker count > 15, start an upgrade
           - If fighter units >= 8, start an upgrade
      For CPU Easy:
             Workers:
             - If worker units < building count + 2 produce workers
             - If worker units > 5 and worker ratio to all units < 20% produce more workers
            Fighters:
             - If fighter units < 10 produce fighters
             - If fighter units ratio to all units < 20% produce more fighters
             - If fighter units ratio to all units < 30% produce more fighters
             - If worker units >= 10 produce more fighters
             - If worker units >= 15 produce more fighters
            Buildings:
            - if building count < 6 or building ratio to all units < 20%, build a new building
            - if building count < 10 and fighter count >= 8, build a new building
           Upgrades:
           - if upgrade count == 0 and worker count > 6, start an upgrade
           - if upgrade count == 1 and worker count > 7, start an upgrade
           - if upgrade count == 2 and worker count > 9, start an upgrade

    For CPU all others:
             Workers:
             - If worker units < 5 produce workers
             - If worker units < 10 produce more workers
             - If worker units ratio to all units < 20% produce more workers
             - If worker units ratio to all units < 30% produce more workers
            Fighters:
             - If fighter units < 10 produce fighters
             - If fighter units ratio to all units < 20% produce more fighters
             - If fighter units ratio to all units < 30% produce more fighters
             - If worker units >= 10 produce more fighters
             - If worker units >= 15 produce more fighters
            Buildings:
            - if building count < 6 or building ratio to all units < 20%, build a new building
            - if building count < 10 and worker count > 12, build a new building
           Upgrades:
           - if upgrade count == 0 and worker count > 5, start an upgrade
           - if upgrade count == 1 and worker count > 10, start an upgrade
           - if upgrade count == 2 and worker count > 15, start an upgrade
           - If fighter units >= 8, start an upgrade

AiRuleProduceResourceProducer
(called every 5 and 60 seconds)
What this rule does: If the faction uses a resource on the map and it is consumable and we have < 0 produce a harvester for that resource.
                                If the faction uses a resource on the map and it is static and we have < 20 produce a harvester for that resource.

AiRuleBuildOneFarm
(called every 10 seconds)
What this rule does: Find a unit that can produce a unit which has resource cost for a consumable resource if we have none of them (need at least 1 farm)

AiRuleProduce
(called every 2 seconds)
What this rule does: If we have a queued task to produce something, then this command will decide exactly what should be produced
<this command has tons of logic and will be documented later>

AiRuleBuild
(called every 2 seconds)
What this rule does: If we have a queued task to build something, then this command will decide exactly what should be built
<this command has tons of logic and will be documented later>

AiRuleUpgrade
(called every 2 seconds)
What this rule does: If we have a queued task to upgrade something, then this command will decide exactly what should be upgraded
<this command has tons of logic and will be documented later>

AiRuleExpand
(called every 30 seconds)
What this rule does: If there are resources on the map used by the faction and we have 'sighted' that resource, expand close to that resource, otherwise send scout to look for resources

AiRuleRepair
(called every 10 seconds)
What this rule does: If we have a damaged 'base', and we have units that can repair it, then send them to repair the base. Otherwise just look for the first damaged unit and try to repair it.
« Last Edit: 29 August 2011, 22:37:45 by softcoder »

Mr War

  • Guest
Re: Food for thought
« Reply #2 on: 30 August 2011, 06:32:01 »
Wow, this is excellent. Airulebuild is probably more trouble spot but really appreciate all of this. I also wondered whether I should add cow equivalents, like the AI may be expecting to have them. When u are done I think there ought to be a stickied AI rules guide thread, this is so useful for serious modders

Also, for anyone too easily critical of the AI, this could be quite educational. I think the AI is actually quite good for an open source game
« Last Edit: 30 August 2011, 06:37:12 by Mr War »

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,238
    • View Profile
Re: Food for thought
« Reply #3 on: 30 August 2011, 15:20:37 »
My personal feeling is to have much if this configured by XML, so that the modder can tweak their faction so that the AI plays it properly. I'll add more details in a bit to the post above.

Omega

  • MegaGlest Team
  • Dragon
  • ********
  • Posts: 6,167
  • Professional bug writer
    • View Profile
    • Personal site
Re: Food for thought
« Reply #4 on: 30 August 2011, 19:44:33 »
Or better yet: a Lua AI. ;) Wiki page.
« Last Edit: 18 June 2016, 18:41:43 by filux »
Edit the MegaGlest wiki: http://docs.megaglest.org/

My personal projects: http://github.com/KatrinaHoffert

wciow

  • Behemoth
  • *******
  • Posts: 968
    • View Profile
Re: Food for thought
« Reply #5 on: 31 August 2011, 05:27:32 »
I think it would be enough to just expose the hard-coded numbers to modders through an XML file. So instead of scouting after you have 8 units you could change it to scout after 4 etc.

Next step is for someone to create a simple tool which auto generates the XML from a UI and slots it in next to the other so it is selectable on starting the game. Then modders can enter a bunch of numbers hit "generate" and see how the game plays.  ;D
Check out my new Goblin faction - https://forum.megaglest.org/index.php?topic=9658.0

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,238
    • View Profile
Re: Food for thought ( about AI )
« Reply #6 on: 3 October 2011, 23:19:16 »
FYI, to learn about what AI players are doing edit glestuser.ini:

Code: [Select]
AiLog=99999
AiRedir=false

The first parameter is the log level (high the # the more details you get) and the second tells it to display realtime log info in the in-game chat console. Each faction logs its AI info too aiX.log where X is the faction index #. These are settings that have always existed in Glest.

Thanks

**UPDATE: a few things have changed in the AI logic as of svn rev#2603

- factions without a 'farm' type unit are ok now, you don't need to have a farm for the AI to play properly
- each faction now supports an option ai behavior section in its xml, the format is as follows:

Code: [Select]
<ai-behavior>
<worker-units>
<unit name="castle" minimum="1"/>
</worker-units>
<warrior-units>
                        <unit name="battle_machine" minimum="1"/>
<unit name="archer" minimum="1"/>
</warrior-units>
<resource-producer-units>
<unit name="pig" minimum="1"/>
</resource-producer-units>
<building-units>
<unit name="blacksmith" minimum="1"/>
</building-units>
<upgrades>
<upgrade name="stables"/>
<upgrade name="robotics"/>
<upgrade name="advanced_architecture"/>
</upgrades>
</ai-behavior>

In this example we show how to use all 4 categories of units (worker, warrior, resource producer and building)

Within each block, the order you enter the unit type is the priority order and the minimum tag tells the engine how many minimum of that unit type are desired. When the AI logic determines it needs a unit of a certain category it will FIRST look at this list to see if it can produce the unit (if the minimum count has not yet been met) then try to make that type first (starting from the top of the list in the category). Once these priority units have been created then the default logic is used.

The above xml example states the following:

- we desire 1 minimum castle when the AI wants to create a worker unit
- we desire 1 battle machine minimum and then 1 archer minimum when the AI want to create a warrior
- we desire 1 pig minimum when the AI wants to create a resource producer unit
- we desire 1 blacksmith minimum when the AI wants to build a building unit
- we desire the following upgrades in priority order, stables, robotics and advanced architecture

I tested this on the tech faction and it appears to work very nicely.
« Last Edit: 4 October 2011, 17:16:51 by softcoder »

Hagekura

  • Archmage
  • ******
  • Posts: 524
    • View Profile
    • Hageus_Iaponicus(@Hageus_Hagekura)さん | Twitter
Re: Food for thought ( about AI )
« Reply #7 on: 6 October 2011, 10:17:28 »
Great improvements! The AI behavior option looks fantastic.  I want to test the new options as soon as new version of MG has been released.
Bushido to iu wa shinu koto to mitsuketari.

Japanese Faction Mod

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,238
    • View Profile
Re: Food for thought ( about AI )
« Reply #8 on: 6 October 2011, 13:48:58 »
You could try it using Mr. War's binaries as they have the feature in that build.

Thanks

Mr War

  • Guest
Re: Food for thought ( about AI )
« Reply #9 on: 8 October 2011, 08:08:18 »
This is really cool, thanks. Looking forward to trying it.

Hagekura

  • Archmage
  • ******
  • Posts: 524
    • View Profile
    • Hageus_Iaponicus(@Hageus_Hagekura)さん | Twitter
Re: Food for thought ( about AI )
« Reply #10 on: 14 October 2011, 04:55:16 »
I've copied binaries in Coldwarpack and tested ai-behavior option. It worked quite nicely. Really impressive feature!

Is it possible to set multiple AI-behavior options for a faction so that AI can choice different tactics randomly (or player can select AI-behavior in game setup option menu)? It would be very interesting feature.
Bushido to iu wa shinu koto to mitsuketari.

Japanese Faction Mod