Make AI not prioritize certain unit for attacks

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
Nyanyanyan
Posts: 35
Joined: May 11th, 2018, 10:40 pm

Make AI not prioritize certain unit for attacks

Post by Nyanyanyan » January 17th, 2019, 2:05 pm

Hi, I made an ability that lets a unit respawn at the location of one of the other units of it's faction upon death.

I worked out most of the kinks, but one problem I still have is that the AI seems to 100% prioritize the unit with this ability.
This leads to the AI basically being locked into attacking the ever-respawning unit until it is out of sight.

Stat-wise, the unit I gave this ability to is a normal ghost with a poison/plague weapon special. I set the level to 0 and the cost to 1 in the hopes that it'll make the AI think the unit is worthless, but that doesn't seem to work.

I have plenty of experience in coding abilities but never touched the AI in any way.

What I'm basically looking for is a way to tell the AI to not prioritize a certain unit for attacks, preferably in the unit code itself since this is for an Era with maps made by other people.

I'd appreciate any help you could give me.
Thanks in advance.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.

enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Make AI not prioritize certain unit for attacks

Post by enclave » January 17th, 2019, 5:35 pm

I'll give some example of how I use [micro_ai] tag to "control" ai in a way that i like better, but Im not sure if there is a way to directly tell ai to avoid a unit properly.. for that you would need to read wiki help on all possibilities of micro_ai tag, maybe you can find something...

Code: Select all

[event]
name=side turn 1
first_time_only=no
[micro_ai]
            side=$side_number
            ai_type=goto
            action=add
            ca_id=hac_range2_1hex_enemy_$side_number  ## range2 attack enemy
            [filter]
				ability=hac_ranged2
				[filter_location]
				[filter]
				[filter_side]
				[enemy_of]
				side=$side_number
				[/enemy_of]
				[/filter_side]
				[/filter]
				radius=4
				[/filter_location]
            [/filter]
            [filter_location]
				[and]
				[filter]
					[filter_side]
					[enemy_of]
					side=$side_number
					[/enemy_of]
					[/filter_side]
				[/filter]
				radius=2
				[/and]
				[and]
				[filter]
				ability=hac_ranged2
				side=$side_number
				[/filter]
				radius=1
				[/and]
			[not]
				[filter]
				[filter_side]
				[enemy_of]
				side=$side_number
				[/enemy_of]
				[/filter_side]
				[/filter]
				radius=1
			[/not]
			[not]
				terrain=M*,M*^*,W*,S*
			[/not]
            [/filter_location]
            unique_goals=yes
				ca_score=210009
				ignore_enemy_at_goal=no
				ignore_units=no
  [/micro_ai]
[/event]
the example is unfortunately quite complicated, what it does it makes a unit with ranged ability to go 1 hex away from enemy (because in my add-on ranged attacks are happening from ranged distance, not adjacent to a unit). Just see if it can be any useful to you... ask questions, ill try to answer when i have time (just currently dont have time, so i couldnt simplify the example for you sorry.. not sure if needed either)

PS. basically the micro ai has 2 filter sections, [filter] to filter which units should do certain things and then below [filter_location] to tell where these certain units should go.. so the ranged attack example could be just what you need, just add some ability filter and remove my ability filters.. ask questions anyway if it's a bit complicated (the example is used for "special units" to attack "any enemy unit", you would just need to reverse it for your specific use.. coz you want "any ai unit" to attack "special unit", in the case of this example they will just nearly never go adjacent to your "special unit") The trade-off is that the ai will start to behave really stupid... I recommend you to read [micro_ai] examples in wiki, u might find something more useful than my example. But example is just showing more or less hwo it works in real life..

mattsc
Posts: 1121
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Make AI not prioritize certain unit for attacks

Post by mattsc » January 17th, 2019, 10:20 pm

Nyanyanyan wrote:
January 17th, 2019, 2:05 pm
I worked out most of the kinks, but one problem I still have is that the AI seems to 100% prioritize the unit with this ability.
This leads to the AI basically being locked into attacking the ever-respawning unit until it is out of sight.

Stat-wise, the unit I gave this ability to is a normal ghost with a poison/plague weapon special. I set the level to 0 and the cost to 1 in the hopes that it'll make the AI think the unit is worthless, but that doesn't seem to work.
Yeah, level does not play a role in the attack evaluation, other than to determine whether the unit could level up. Cost does, but it's only one of many contributions and might not be the dominant rating depending on the situation on the map. I assume that in your case a ghost (any ghost) is simply an attractive target to the AI, so it will keep attacking those and doesn't really care (or know) that this is the same unit over and over again.
Nyanyanyan wrote:
January 17th, 2019, 2:05 pm
What I'm basically looking for is a way to tell the AI to not prioritize a certain unit for attacks, preferably in the unit code itself since this is for an Era with maps made by other people.
There's really no way of setting priorities of individual units, or unit types, or groups of units for attacks by the default AI other than excluding them entirely using the attacks aspect. But then the AI will never attack those units. I guess you could set the aspect to exclude your unit in an event the first time it is killed, and then reset it in another event when only this unit is left.

You could try to make the unit less attractive to the AI. A very strong emphasis is given to damage done to the unit, and in particular to being able to kill it, so simply increasing its hitpoints, or maybe resistances may be a solution. That's really the only thing I can think of on the unit side itself.

Or you could, as enclave suggests, set up other AIs, such as micro AIs or a custom AI, that take control of the AI units before the default attacks kick in. For example, you could set up a Simple Attack MAI to use the other AI units before the default AI attacks. But as with the attacks aspect, that's really an all or nothing thing.

I'm afraid none of those options will do exactly what you want, but short of writing your own attack candidate action, that exact behavior might not be possible.

[Finlly, just to mention it for completeness, as I assume you have thought of this and rejected it for gameplay or story reasons: one simple solution would be to make sure the unit respawns out of range of the AI.]

User avatar
octalot
Developer
Posts: 407
Joined: July 17th, 2010, 7:40 pm

Re: Make AI not prioritize certain unit for attacks

Post by octalot » January 18th, 2019, 12:08 pm

mattsc wrote:
January 17th, 2019, 10:20 pm
You could try to make the unit less attractive to the AI. A very strong emphasis is given to damage done to the unit, and in particular to being able to kill it, so simply increasing its hitpoints, or maybe resistances may be a solution.
Maybe give it a "defend-only" weapon that reduces damage when it respawns, and then remove that weapon again in the player's refresh phase?

User avatar
Nyanyanyan
Posts: 35
Joined: May 11th, 2018, 10:40 pm

Re: Make AI not prioritize certain unit for attacks

Post by Nyanyanyan » January 18th, 2019, 4:52 pm

Upon reading and trying a lot of what you guys wrote I tried setting the unit parameters in a way that units couldn't possibly want to attack my unit, with a huge healthpool and no useful abilities whatsoever, but they still attacked it like mad.

I did some trial and error and found out that the reason for this behaviour was the way I tried making the unit unable to advance. What I did was put the required xp to 0 and the amla to nothing, which did eliminate any xp from the unit, but apparently confuses the ai to no end.

I changed the xp and removed the amla in favor of a simple advancement option and that fixed it.

Thank you all for your help.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.

mattsc
Posts: 1121
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Make AI not prioritize certain unit for attacks

Post by mattsc » January 18th, 2019, 5:11 pm

Nyanyanyan wrote:
January 18th, 2019, 4:52 pm
I did some trial and error and found out that the reason for this behaviour was the way I tried making the unit unable to advance. What I did was put the required xp to 0 and the amla to nothing, which did eliminate any xp from the unit, but apparently confuses the ai to no end.
Hmm, that's interesting. There's a division by XP needed to advance in the attack evaluation code that, if that is zero, could cause problems depending on how it is implemented. I'll have a look at that sometime to see if that if the cause of the issue and if so whether it should be fixed in the AI code or further upstream (e.g. by not allowing required XP to be zero). Thanks for pointing this out.

enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Make AI not prioritize certain unit for attacks

Post by enclave » January 19th, 2019, 7:24 am

mattsc wrote:
January 17th, 2019, 10:20 pm
Yeah, level does not play a role in the attack evaluation, other than to determine whether the unit could level up. Cost does ...
so what if he would try to [store_unit] and change the cost to 1 gold or 0 gold and then [unstore_unit]..? i mean i guess its irrelevant now since he found a fix :) just curious.. well.. well done :)

mattsc
Posts: 1121
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Make AI not prioritize certain unit for attacks

Post by mattsc » January 20th, 2019, 3:25 pm

enclave wrote:
January 19th, 2019, 7:24 am
so what if he would try to [store_unit] and change the cost to 1 gold or 0 gold and then [unstore_unit]..? i mean i guess its irrelevant now since he found a fix :) just curious.. well.. well done :)
Nyanyanyan said in the original post that he tried to set the gold to 1 and that it did not make a difference. If there's a divison by zero involved, anything else you do does not really matter...

enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: Make AI not prioritize certain unit for attacks

Post by enclave » January 20th, 2019, 6:41 pm

mattsc wrote:
January 20th, 2019, 3:25 pm
Nyanyanyan said in the original post that he tried to set the gold to 1 and that it did not make a difference. If there's a divison by zero involved, anything else you do does not really matter...
ah yes, thanks very much, i somehow missed it.. only saw the level to 0 thing.. somehow skipped the 1 gold.. thanks.

Post Reply