AI questions (possibly mattsc could help?)

The place to post your WML questions and answers.

Moderator: Forum Moderators

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
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

AI questions (possibly mattsc could help?)

Post by enclave »

Hi, I'm on wesnoth 1.12
Trying many many things to make AI retreat, but it doesn't care much...

Important information:
map has NO VILLAGES (imagine creep wars map or similar) but it has a castle

1) I tried to [modify side] aggression to -100, caution to 1, grouping to defensive (not tried none), even change attack depth etc, this had 0 effect...
When AI has enemy in few hexes away he either attacks (no matter that aggression is -100,-1, caution is 1) or just stands on his position, does NOT go back to avoid damage anyhow.

2) I tried to make a goal,

Code: Select all

			[modify_ai]
			  side=$CTB_temp_side
			  action=add
			  path=goal[]
				  [goal]
					  id=ctb_retreat_here_$CTB_temp_side
					  name=target_location
					  [criteria] 
					  	x,y=$x1,$y1
					  [/criteria]
					  value=100
				  [/goal]
			  [/modify_ai]
In BEST CASE 1 unit will go to that hex, the rest will go BANZAI and attack enemies (with aggression -100,-1, caution=1)

3) The only thing that helped A LITTLE was to add [avoid] which I dont have much clue how to write (because of terrible documentation and examples on AI tag scattered in like 4-5 different topics of wiki), so I tried with filter side and with just x,y values like so:

Code: Select all

	[modify_side]
			side=$CTB_temp_side
			[ai]
				[avoid] 
					[filter]
						side=3,4
						radius=4
					[/filter]
						radius=4
						##x=8-20
						##y=0-20
				[/avoid]
			[/ai]
			[/modify_side]
It works much better with x=8-20 y=0-20 but it's really really inconvenient way to write it for me.. so I have to use [filter] side and radius...
The units then go back to the target set in the goal and gather there... BUT only the ones who are not adjacent to enemies.
No matter what I use, x y range or filter side, the units who are engaged in combat and are adjacent to enemies DO NOT CARE ABOUT ANY GOALS at all..
They dont care about aggression, caution, AVOID tags, they dont care about nothing.

And my question is: really? There is no way to control ai behavior? :( cant ai just go back if he is adjacent to enemy?!?
Please please prove me wrong and tell me that i'm stupid and that there is a way!!!! Tell me how!!!???
User avatar
Samonella
Posts: 382
Joined: January 8th, 2016, 5:41 pm
Location: USA

Re: AI questions (possibly mattsc could help?)

Post by Samonella »

Have you tried messing around with [micro_ai]s? I think something close to this would do the trick:

Code: Select all

     [micro_ai]
            side=$CTB_temp_side
            ai_type=goto
            action=add
            ca_id=retreat
            [filter]
                < something with [filter_adjacent] >
            [/filter]
            [filter_location]
                x,y=$x1,$y1
            [/filter_location]
        [/micro_ai]
The last few months have been nothing but one big, painful reminder that TIMTLTW.

Creator of Armory Mod, The Rising Underworld, and Voyage of a Drake: an RPG
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: AI questions (possibly mattsc could help?)

Post by enclave »

Samonella wrote: March 20th, 2018, 10:57 pm Have you tried messing around with [micro_ai]s? I think something close to this would do the trick:
Ill try, thanks very much for idea. More ideas also welcome!
User avatar
Celtic_Minstrel
Developer
Posts: 2207
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: AI questions (possibly mattsc could help?)

Post by Celtic_Minstrel »

enclave wrote: March 20th, 2018, 9:27 pm 1) I tried to [modify side] aggression to -100, caution to 1, grouping to defensive (not tried none), even change attack depth etc, this had 0 effect...
When AI has enemy in few hexes away he either attacks (no matter that aggression is -100,-1, caution is 1) or just stands on his position, does NOT go back to avoid damage anyhow.
I assume you mean using an [ai] tag inside [modify_side]? Because keys like aggression and caution have no meaning directly in [modify_side].

The AI decides whether to attack a unit based on expected damage received and expected damage dealt. I don't know the exact formula here, but an aggression of 1 means it puts no weight on received damage, while an aggression of 0 means it puts equal weights on them and thus doesn't attack if it can expect to receive more damage than it deals. A high negative aggression will typically prevent it from attacking unless it will take no damage in exchange.

But the corollary to that is that lowering aggression does not totally prevent it from attacking. It only means most units won't be considered as potential targets! The aggression value has no effect on anything other than actual attacks. In particular, it has nothing to do with the AI's built-in retreat mechanism.

-----

Caution is the value that the AI uses to decide when to retreat. A value of 0 means it never retreats. A high value will make it more likely to retreat. So, I would expect setting the caution to a really high value, for example 100, would force the AI to unconditionally retreat. I'm not sure if there are other details that might get in the way of that, though. In particular, caution is also used when moving to a destination defined by [goal], so if you're using that to specify the actual location of the retreat, then raising caution too high might be detrimental. And note that relying on the built-in retreat candidate action probably leaves you no control over where they retreat to, so a [goal] is very likely needed for full control. Still, I'd try setting caution to no less than 2, and probably a little higher.

-----
enclave wrote: March 20th, 2018, 9:27 pm 2) I tried to make a goal,

Code: Select all

			[modify_ai]
			  side=$CTB_temp_side
			  action=add
			  path=goal[]
				  [goal]
					  id=ctb_retreat_here_$CTB_temp_side
					  name=target_location
					  [criteria] 
					  	x,y=$x1,$y1
					  [/criteria]
					  value=100
				  [/goal]
			  [/modify_ai]
In BEST CASE 1 unit will go to that hex, the rest will go BANZAI and attack enemies (with aggression -100,-1, caution=1)
The AIWML page clearly states that goals have no influence whatsoever on attacks. If you want the AI to not attack while retreating, you'll need to take additional measures. For example, setting the caution higher, using the attacks aspect, or removing the attacks candidate action altogether. I don't recall exactly how the goals system works, but I think your problem is very likely the fact that, once a unit has reached the location, the goal is both satisfied and no longer attainable, because the AI can't move to an occupied location. I would suggest adding a radius to the filter.

-----
enclave wrote: March 20th, 2018, 9:27 pm 3) The only thing that helped A LITTLE was to add [avoid] which I dont have much clue how to write (because of terrible documentation and examples on AI tag scattered in like 4-5 different topics of wiki), so I tried with filter side and with just x,y values like so:

Code: Select all

	[modify_side]
			side=$CTB_temp_side
			[ai]
				[avoid] 
					[filter]
						side=3,4
						radius=4
					[/filter]
						radius=4
						##x=8-20
						##y=0-20
				[/avoid]
			[/ai]
			[/modify_side]
Just like goals, avoid tags have nothing to do with attacks. They make the AI avoid specific locations, which means they only apply when the AI is moving units. In other words, if they end up near an enemy by some other means, they'll happily attack it.

-----

In short, the methods you tried are almost entirely unrelated to the AI's attack algorithms, which is why they're not working. Here's two things you can try that do have something to do with attacks:

1. The attacks aspect. This causes the AI to ignore certain pairings for possible attacks. It's documented in more detail on the wiki, but basically something like this should be similar to what you want:

Code: Select all

[aspect]
        id=attacks
        [facet]
            invalidate_on_gamestate_change=yes
            [filter_own]
            [/filter_own]
            [filter_enemy]
                side=3,4
            [/filter_enemy]
        [/facet]
[/aspect]
An empty filter matches all units if I recall correctly, so that means units of sides 3 and 4 won't be considered as possible attack targets. To undo this you need to use [modify_ai]; provided you haven't used the attacks aspect anywhere else in your scenario, this should work:

Code: Select all

[modify_ai]
	side=2
	action=delete
	path=aspect[attacks].facet[*]
[/modify_ai]
-----

The other way to prevent attacks is to use [modify_ai] to delete the attacks candidate action altogether. The syntax for that would be:

Code: Select all

[modify_ai]
	side=2
	action=delete
	path=stage[main_loop].candidate_action[combat]
[/modify_ai]
If you want to get it back later so that the AI can attack again, this should work:

Code: Select all

[modify_ai]
	side=2
	action=add
	path=stage[main_loop].candidate_action[]
	{AI_CA_COMBAT}
[/modify_ai]
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: AI questions (possibly mattsc could help?)

Post by mattsc »

I don't think I've ever been called out in a topic title ... :hmm:

Celtic_Minstrel pretty much said it all (thanks for writing all of that up!). Just a couple additional comments:

I suggest you read up on the descriptions of aggression and caution on AiWML. That page explains in quite some detail what those parameters (called aspects) affect and what approximate values you need to choose to get a desired effect.

I'd also suggest to read up on how the AI's candidate action system works. If you get at least a general feeling for how this works (just the overall principle, not the details), you will notice immediately that setting goals will not (cannot possibly) keep the AI from attacking. There's a lot of things you can make the AI do, it is actually very configurable even without writing your own candidate actions, but not without understanding how it works. For example, you might also notice that setting goto_x/goto_y for the units might (I don't know for sure as I don't have all the details) work for what you want to accomplish, since the Goto CA is always the first to be executed. This is similar to, but not as configurable as, the Goto Micro AI Samonella suggested.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: AI questions (possibly mattsc could help?)

Post by enclave »

Wow Thank You So Much Everyone for all your replies, especially Celtic_Minstrel, I can only guess how long it took you to write it all down, thanks so much!
Now I more or less see the whole picture and possibilities with AI.

Straight away I tried to set caution to 100, the adjacent unit to enemy still attacked, the others retreated (possibly affected by avoid tag, not caution, because that's more or less the effect I already had), the radius may have the meaning but doubtfully since IF the units retreat, they all gather around x,y target, even if the actual x,y is occupied by one of them.
And yes I used [ai] inside the [modify_side] tag, then checked if it works using debug and viewing "ai overview" for relevant team and also "ai config full", thanks very much for mentioning, it may help many others who will once come across this post while looking for help with ai behavior control..

From the wiki I thought that maximum for caution is 1, where it says that in this case the unit will not attack if he is weaker, now after reading the links you provided I noticed that maximum mentioned was actually 2 when describing the behavior with leader. Still even with 100 caution it attacks adjacent enemy (in my case it makes my unit die, so it was a suicide attack actually). Would be helpful if wiki had mentioned the maximum and minimum possible values for everything, would help newbies like me to do tests to see what actually works and what doesn't work at all (That's off-topic, so...)

Then I tried your second suggestion with:

Code: Select all

[modify_side]
			side=$CTB_temp_side
			[ai]
				[aspect]
				id=attacks
				[facet]
				invalidate_on_gamestate_change=yes
				[filter_own]
				[/filter_own]
				[filter_enemy]
					side=3,4
				[/filter_enemy]
				[/facet]
				[/aspect]
			[/ai]
			[/modify_side]
The way you wrote it was suggesting me to use modify side tag and then ai tag with aspect inside, not sure if I understood it right.
The result of it was the following config in "ai config full":

Code: Select all

[aspect]
 engine="cpp"
 id="attacks"
 invalidate_on_gamestate_change=yes
 invalidate_on_minor_gamestate_change=no
 invalidate_on_tod_change=yes
 invalidate_on_turn_start=yes
 name="composite_aspect"
 [facet]
  engine=""
  id=""
  invalidate_on_gamestate_change=yes
  invalidate_on_minor_gamestate_change=no
  invalidate_on_tod_change=yes
  invalidate_on_turn_start=yes
  name=""
  [filter_enemy]
   side="3,4"
  [filter_enemy]
 [/facet]
 [default]
  engine="cpp"
  id=""
  invalidate_on_gamestate_change=yes
  invalidate_on_minor_gamestate_change=no
  invalidate_on_tod_change=yes
  invalidate_on_turn_start=yes
  name="ai_default_rca::aspect_attacks"
 [/default]
[/aspect]
and the result was again the same, adjacent to enemy units attacked, the others retreated.
When I applied it the first time, I forgot to set side of the [modify_side] and the enemies did not attack (I guess it affected all teams), so I'd say it works as such, but again has no affect on adjacent enemies.

And the last one:

Code: Select all

[modify_ai]
	side=$CTB_temp_side
	action=delete
	path=stage[main_loop].candidate_action[combat]
[/modify_ai]
This has worked great! The minor trade-off was that was one unit trapped between enemies and he did not attack anything even if he would get 0 damage in return. But it's MINOR because my main target was achieved... all adjacent to enemies units did not attack and retreated to the target zone safely..

I will later try to gain more control of the AI trying all your links provided including micro_ai. For now I'm very happy with result. Thank you all again!!!
User avatar
Celtic_Minstrel
Developer
Posts: 2207
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: AI questions (possibly mattsc could help?)

Post by Celtic_Minstrel »

I think adding the attacks aspect that I posted can be done either with [modify_side] (as you did) or with [modify_ai] (which would be like my example of how to undo it, but change delete to add and put the aspect tag in there as well).
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply