How it works - Ai priorization

Learn more about how the game is put together
More obscure knowledge and deprecated items
Post Reply
Stratego (dev)
Site Admin
Posts: 14750
Joined: Fri Apr 25, 2014 9:28 pm

How it works - Ai priorization

Post by Stratego (dev) » Sun Nov 15, 2020 6:57 pm

I make this while i am reading tru code how it works - so we have all the knowledge

(prelude:
- AI is running tru all units on game and assigns possible tasks to them (on enemies eg. attacking or asting a spell, on allies casting spells, on TC-s defending tasks so all that is fir to target unit)
- after that AI storts which task gets most score on that unit (one unit can have hundreds of possible tasks assigned to it) - this topic is about this step!
- after getting the highest score it assigns that task to the unit.
- and runs the tasks on all playe's units
)


so AI decides what task to give to a unit based on score if the given task in the given situation.
task can be, few examples: move, attack, use ability, occupy, goheal, mend, build things and such like this.

here are the defined tasks and their priorities so far
public static final int TASK_DO_WATER_CARRY = -1; //a carrier should do this
public static final int TASK_TC_GO_DEFEND = 0;
public static final int TASK_TC_ATTACK = 1;
public static final int TASK_TC_OCCUPY_ENEMY = 2;
public static final int TASK_TC_OCCUPY_NEUTRAL = 3;
public static final int TASK_ATTACK_ENEMY_UNIT = 4;
public static final int TASK_SUMMON_NEW_UNIT = 5; //
public static final int TASK_SPELL_ON_ENEMY_UNIT_OR_CONVERT = 6; //convert or any other enemy targeted spell
public static final int TASK_ATTACK_ENEMY_BUILDING = 7; //wall or tower
public static final int TASK_SAVE_OWN_ASS = 8; //if 40% or less power than do it, under 20%, harder
public static final int TASK_HEAL_UNIT = 9; //heal or mend
public static final int TASK_MEND_UNIT = 10; //mend wall or tower, mendable units
public static final int TASK_SPELL_ON_FRIENDLY_UNIT = 11; //cast buffers onto unit
public static final int TASK_TC_BUILD_DEFENSE = 12;
public static final int TASK_BUILD_FACTORY = 13;
public static final int TASK_WANDERING_AROUND = 19; //neutral annimals


score starts with 0

("i" = unit to set task to)


case TASK_TC_ATTACK, TASK_ATTACK_ENEMY_UNIT, TASK_ATTACK_ENEMY_BUILDING:
score = 0
- if target is one-hit killed: score = +10 + 10 * TargetUnitValue
- else if i can shoot target but that can not walk to me to hit: score = +3 + 2 * TargetUnitValue
- else if i am a mender or healer than: score = -10
- else if i am a transporter (like ship or wagon) having TRANSPORTING_VEIN: score = -100
- else score = 0

- if i have bonus agasisnt it: score = score +2

- if target is neutral than do not urge to kill score = -100
- if target is passive units like "wall"s score = -100

case TASK_SPELL_ON_FRIENDLY_UNIT, TASK_SPELL_ON_ENEMY_UNIT_OR_CONVERT:
score = 0
- score = effectPriority / 100 effect priorities are about 0..1000

case TASK_TC_GO_DEFEND:
score = 0
- if i can mend or heal: score = score -2
- else if i can carry units: score = score -1

- if i can attack: score = score +1

- if unit is already inside TC : score = score +10
- else if unit can go into tc with one move: score = score +5
- else if unit can go into tc with 1.5 move: score = score +1

case TASK_TC_OCCUPY_NEUTRAL, TASK_TC_OCCUPY_ENEMY:
score = 0
- if unit can occupy in one move: score = 50
- else if unit can occupy in 1.5 move: score = 10
- else if i can heal or mend score = -1

if TASK_TC_OCCUPY_NEUTRAL score = score +10

case TASK_SAVE_OWN_ASS:
if i am fatally injured: score = 3
else score = 1


case TASK_HEAL_UNIT, TASK_MEND_UNIT:
- i am nearby and target is underconstruction and is a factory: score = 9
- i am nearby and target is underconstruction and is NOT a factory: score = 7
- i am NOT nearby and target is underconstruction and is a factory: score = 8
- i am NOT nearby and target is underconstruction and is NOT a factory: score = 3

case TASK_BUILD_FACTORY:
- i am nearby score = 6
- else score = 2

case TASK_TC_BUILD_DEFENSE
- i am nearby score = 5
- else score = 1
- if i am close to enemies: score = score + 20

case TASK_DO_WATER_CARRY
score = 20

case TASK_WANDERING_AROUND
score = -1 (neutrals have only this task so scoring does not matter)


Distance modifier:
final score = ((biggest_task_prior(19) - current task prior) + score) / target_distance


Stratego (dev)
Site Admin
Posts: 14750
Joined: Fri Apr 25, 2014 9:28 pm

Re: How it works - Ai priorization

Post by Stratego (dev) » Mon Nov 16, 2020 8:57 pm

you must be kidding, machine learning lol :)

User avatar
Maxbirykov2004
Posts: 1022
Joined: Mon Mar 30, 2020 12:50 pm
Location: Belarus

Re: How it works - Ai priorization

Post by Maxbirykov2004 » Mon Nov 16, 2020 9:03 pm

Stratego (dev) wrote:
Mon Nov 16, 2020 8:57 pm
you must be kidding, machine learning lol :)
Why not, it isn't impossible... But its REALLY HARD...

Stratego (dev)
Site Admin
Posts: 14750
Joined: Fri Apr 25, 2014 9:28 pm

Re: How it works - Ai priorization

Post by Stratego (dev) » Mon Nov 16, 2020 9:37 pm

ok, that is a wrong way. there is no processing an storage capacity on a phone to do that, and even i have no that much "free time" to implement.

User avatar
Maxbirykov2004
Posts: 1022
Joined: Mon Mar 30, 2020 12:50 pm
Location: Belarus

Re: How it works - Ai priorization

Post by Maxbirykov2004 » Mon Nov 16, 2020 10:00 pm

Stratego (dev) wrote:
Mon Nov 16, 2020 9:37 pm
ok, that is a wrong way. there is no processing an storage capacity on a phone to do that, and even i have no that much "free time" to implement.
Okay :(

User avatar
b2198
Posts: 647
Joined: Mon Aug 30, 2021 5:48 pm
Location: Brazil

Re: How it works - Ai priorization

Post by b2198 » Thu May 12, 2022 1:46 am

Actually, running a simple neural network isn't THAT expensive in performance, though training it is. Ao games would also require a considerably complex one, so I don't know how much more expensive it would be... plus it would have to be retrained when updates are released, so... I don't think it's a viable solution. This last issue also probably makes it not viable to use genetic algorithms, and min-maxing and similar techniques are imo out of question due to the humongously large search space (though they could probably be used to search up until ~3 or maybe even 4 turns ahead but probably wouldn't improve the bots that much with just that), so as far as my knowledge goes (which isn't very far :joy:) using a score-based-decision approach like the current one is the best solution.

I do have some ideas to help the AI make some medium and long term plans, adding to the existing score system, but I'm currently working on TC generation, and that will still take a while before it's in a basic working state, and a long time before it's fully functional, and also the ideas I have require part of the TC generation code to use as reference in their decisions.
Green is the correct color, other colors are "less correct".

User avatar
Sunrise Samurai
Posts: 2662
Joined: Thu Jun 18, 2015 11:21 pm
Location: Florida, U.S.

Re: How it works - Ai priorization

Post by Sunrise Samurai » Tue Nov 08, 2022 8:21 pm

Stratego (dev) wrote:
Sun Nov 15, 2020 6:57 pm

case TASK_TC_GO_DEFEND:
score = 0
- if i can mend or heal: score = score -2
- else if i can carry units: score = score -1

- if i can attack: score = score +1

- if unit is already inside TC : score = score +10
- else if unit can go into tc with one move: score = score +5
- else if unit can go into tc with 1.5 move: score = score +1
Defensive units should probably have a +10 score for tc defense. Mostly anything classified as "shielded" but also giants, elephants, or other high hp options. If possible, have the highest current hp unit occupy the first slot at all times.

Similar task: ranged, caster, and healer units should attempt to seek shelter in structures or units which still allow them to attack/cast. This means ent warriors will be utilized as a defense for archers, mages stay in towers, etc so these units aren't presenting a target unnecessarily. TC is included in places they should shelter, but this isn't counted as defending the tc so more durable units will take the first slot.
The glorious sun rises again

Stratego (dev)
Site Admin
Posts: 14750
Joined: Fri Apr 25, 2014 9:28 pm

Re: How it works - Ai priorization

Post by Stratego (dev) » Wed Nov 09, 2022 8:19 pm

yes, these suggestions are new additions making it more and more clever. (so not fixing a problem but improving AI)

Post Reply

Return to “Abstract”