Have an side AI confront a given enemy side

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
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Have an side AI confront a given enemy side

Post by denispir »

Let us say that an AI side (or alliance as is discussed below) tactically has to fulfill 2 or more goals. I will restrain the problem to being confronted to more than 2 or more enemy sides. (We are placed in the POV of the AI, thus "enemy" always means enemy of the AI side, not of the player, which may be ally or enemy.)

As an example, consider such a player feedback: "I recalled in 2 turns a strong commando of mixed deadly attacks and high resistance to deal with the weaker SW enemy, then enough to form a defense line against the main enemy at the border of the central hill & forest area..." A very basic and simple tactic, indeed. Yet, how to do that with an AI?

I was exploring the issue and quickly reached the conclusion that it is +/- impossible; unless I missed it all. Instead, we should have 2 AI sides in such a case, thus build an alliance, right? But even then, simply saying to a given AI side "You, confront (rather) this enemy side!" looks like an enigmatic challenge. In fact, all allied AI sides will basically fight all enemy sides the same way. (If I place an enemy side much closer or on the path to another, then indeed the AI side will concentrate on it, but the given tactical pb vanishes and it's not the same scenario anyway.) Actually, a great majority of tactics/strategies involve such concentration on different ennemies/areas/pathes/positions/fronts, etc... I guess. Thus, if I'm right, one can barely "tacticalise" AI sides, even by splitting them into several sides.

There are ways to get around the issue and indirectly have a side more or less do that anyway, using a mix of:
* "move-to-target" goals toward units and/or leader of this side
* "attacks" targets toward units and/or leader of this side
* "avoid" set on other enemy sides
Bur this all looks contrived (and possibly inefficient in given circumstances, especially because move-to has lowest priority, even more if an enemy is human and thus can "read" and react to the AI's tactic, not to mention learn it and replay appropriately...).

Another solution, albeit still indirect and contrived, is to use the goto micro AI. Advantages are simplicity, certainly greater efficiency since it is a goto (highest priority) not a move-to goal (terms are misleading), finally units can be released once there and then behave "normally" (from an AI POV ;) again) without even having to cancel or change the micro AI.

PS: I guess it is possible to make several groups from a single side, by both combining different micro AIs on the same side and using fake vars or traits on units to assign them an AI. (But does this work if it's twice the same micro AI? with different settings.)
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Have an side AI confront a given enemy side

Post by mattsc »

Yeah, what you write is a good summary of the the options you have with the Wesnoth AI. From what you write it also looks like you understand the limitations of the different approaches, so I'll just make a few additional comments.
denispir wrote: January 8th, 2019, 5:30 pm Another solution, albeit still indirect and contrived, is to use the goto micro AI. Advantages are simplicity, certainly greater efficiency since it is a goto (highest priority) not a move-to goal (terms are misleading)
Just adding that you can change the ca_score of the Goto Micro AI for it to be inserted at any place in the candidate action chain. In fact, in principle you can change the scores of any of the CAs, but for the default AI CAs, that generally does not make much sense.
denispir wrote: January 8th, 2019, 5:30 pm PS: I guess it is possible to make several groups from a single side, by both combining different micro AIs on the same side and using fake vars or traits on units to assign them an AI. (But does this work if it's twice the same micro AI? with different settings.)
Yes, you can insert as many Goto MAIs as you want to and use different unit filters to have them apply only to certain units.
denispir wrote: January 8th, 2019, 5:30 pm then enough to form a defense line against the main enemy at the border of the central hill & forest area...
Forming a defensive line along the border of good terrain is very difficult to do consistently for an AI (any AI, not just Wesnoth'). I've done some experiments with that and still have not really come up with a satisfactory solution, although our "Fred" AI does show some promise at least. [Disclaimer: I have no AI background, I'm just somebody who likes to play around with this. Obviously it is possible to do this in principle, but I'm not sure if I'll ever get there with the limited experience and time I have.] Anyways, point is, the current Wesnoth AI simply cannot do that.
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: Have an side AI confront a given enemy side

Post by denispir »

Holà @mattsc!

By chance I had a new look at MicroAIs (wanted to check whether there are general rules about how they deal with or take into account standard params like agression & caution, found only single-AI specific rules), and stepped again on the Wolves MicroAI (single-pack) which I had overlooked. It now seems to me that the name is a misnomer... If we call unit-set what is selected by a unit-filter, then this AI rules a given unit-set to fight against another unit-set, provided I understand the description correctly. Typically a side would fight against another side, which is precisely what I was looking for.

I hope to have time to try and explore better, eg with a map figuring 2x2 sides (placed in cross as in card games), where each side would be governed by this AI to (rather) fight its left neighbour. What may be missing is for a unit-set to travel toward tha mass of its ennemies, since with this MicroAI a unit-set (wolf pack) does not seem to have this behaviour: the description only says wolves actively chase after the closest prey and try to corner it ... Or maybe yes? (A human opponent would thus easily trump the AI by offering single enticing baits who would lure away the whole pack!?) Anyway, we have a ready-made solution with the general GOTO MicroAI to be used first for a few turns, before switching to wolf pack when the few first enemies (more than 1) are met, for instance.

EDIT: The description seems to suggest that wolves would only attack their defined preys (even ignoring an easy kill?). This is not what we want in general, I guess. Since the MicroAI code must somehow modify both the movement and attack (CAs) of the standard AI, then we could probably modify it in turn to have the pack only (+/- strongly) prefere defined prey, giving them a preference weight.

PS: For people who have not yet explorer MicroAIs: have a look at the Wolves MicroAI, and the whole page page for general considerations and other pretty interesting MicroAIs.
User avatar
beetlenaut
Developer
Posts: 2814
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: Have an side AI confront a given enemy side

Post by beetlenaut »

denispir wrote: January 11th, 2019, 4:15 pm What may be missing is for a unit-set to travel toward tha mass of its ennemies
You could figure out where the center of mass is with nothing more than WML, then move your units towards it. Store all the locations on the map into an array. Then, for each unit, store it's reachable locations into a temporary array. For each reachable location of a unit, increase a counter on that hex in the main location array. This will create a "heat map" of sorts showing the hexes controlled by the enemy. The hex with the highest number is the center of the mass of the enemy units. (There are some edge cases that would require farther processing, but that's the general idea.) You could consider only the units of a specific side if you want. When your units are within a turn of the defined movement goal, you could release them to attack at will. Of course you would have to adjust the microAI each turn, but that's easier than the previous steps.

Other things could be done with this data. One idea is to give a weight to each enemy unit by adding up the value of the heat map counter in each of its adjacent hexes.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Have an side AI confront a given enemy side

Post by mattsc »

Salut denispir,

First of all, good to hear that a combination of the Goto and Wolves MAI will (for the most part) do what you want. If that's not good enough though, the MAIs are all written in Lua, so if a small tweak is needed that is not possible with the standard MAIs, the code can easily be imported into your scenario/campaign/modification and adapted as needed. (If you don't know how to do that, I'd be happy to help.) And, as beetlenaut writes, it is of course also possible to use a combination of WML and AIs.

Now, some more specific comments again:
denispir wrote: January 11th, 2019, 4:15 pm wanted to check whether there are general rules about how they deal with or take into account standard params like agression & caution, found only single-AI specific rules
The general rule is given in one of the bullets at the very beginning of the (admittedly very long) page, so it's easily overlooked:
Micro AIs wiki wrote: The default AI aspects are, for the most part, not taken into account by the Micro AIs. For example, unless noted otherwise, specifying a value for aggression does not change the MAI behavior, and locations specified in an [avoid] tag generally can be entered by units controlled by an MAI. This is a feature of the Micro AIs, not a bug. If you feel that a certain default AI aspect should also apply to a Micro AI, please let us know in the Micro AIs Feedback thread.
Exceptions from that, when aspects are taken into account, are listed with the individual AIs.
denispir wrote: January 11th, 2019, 4:15 pm It now seems to me that the name is a misnomer... [...] Typically a side would fight against another side, which is precisely what I was looking for.
Well, yes, but this AI is set up to work under specific rules, trying to simulate animal behavior (you can check out the 'animals' test scenario for a demonstration). Wolves would not head toward the main mass of the enemies. They single out the "easiest" prey, in this case defined as the closest, and go after that. But as I said, that could easily be modified in the code and added to a campaign.

denispir wrote: January 11th, 2019, 4:15 pm The description seems to suggest that wolves would only attack their defined preys (even ignoring an easy kill?).
The default behavior is that they attack any enemy (see the description at the beginning of the AI), but this can be changed with the attack_only_prey parameter. So both behaviors exist and can be chosen.
denispir wrote: January 11th, 2019, 4:15 pm Since the MicroAI code must somehow modify both the movement and attack (CAs) of the standard AI
Actually, that's not how it works. Instead it adds extra candidate actions that take control of the wolves before the default AI CAs.
denispir wrote: January 11th, 2019, 4:15 pm A human opponent would thus easily trump the AI by offering single enticing baits who would lure away the whole pack!?
That is correct. This was not meant as an AI that is the main opponent of a human player. But it is not different from the default AI, for which you can do that also. In fact, the wolves' attacks are left to the default AI.

I think/hope I've addressed your questions with that. Let me know if I missed something and good to hear that you are trying to get the most out of the Micro AIs.
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: Have an side AI confront a given enemy side

Post by denispir »

Holà @ mattsc!

Thank you for your nice and precise answer. In the meantime, I have progressed a bit in knowledge of your micro AIs, mainly by exploring their code directly, thus ultimately landing on the CA modules that seem to hold the core logic (I used to know Lua quite well, which helps a lot, but my knowledge of, say, Wesnoth-Lua libraries or helper modules [not to mention the hidden but necessary Lua/cpp interface] are null, so that my understanding is still limited) (is there any developer guide about that, aimed at helping potential Wesnoth-Lua developpers to catch up with the present state of the art?).

Whatever, the result is that I finally "fell in love" with the micro AI approach, "intuiting" that it's a much better "paradigm" than the all-in-one adopted by the standard AI (RCA), both in general AI quality and from the pov of game authors (mainline or UMC), the aim being that they actually can choose and configurate good-enough AI side behaviour.

I may develop in another thread if ever I have time and energy to dedicate to such a goal (I mean to actively participate). For now, globally, my point of view is that a standard core AI should only deal with optimising technical and strategic level (lower and higher level goals ultimately meet: eg gaining time, occupying good defense tiles, preserving HPs), while tactical ie medium level goals, which are by far the most difficult ones, are to be left to (very) specialised micro AIs driving sets or groups of units --micro AIs which delegate the technical and strategic levels to the base AI. Read: this is more or less what you presently do with your micro AIs, except that there may be a wide set of micro AI types specialised for common or exotic tactical objectives (possibly each with a range of variant each time generalising would lead to over-complication and corresponding innumerable bugs); more like the assassin or guardian ones, rather than specialised for unit types like the animal ones.

[Despite its numerous flaws imv, Lua is certainly an adequate language for that, in any case I know no better one.]
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Have an side AI confront a given enemy side

Post by mattsc »

Thanks, denispir. :) Based on what you write, you might enjoy checking out the AI we call Fred. It's an attempt to put together an AI with "real" operations and tactics layers that is entirely independent of the default AI. It is incomplete and has a whole bunch of problems and currently only works on the Freelands map, but if you manage to see past that, it might be interesting to check out. Actually, after a many-months break, I just started working on Fred again a couple days ago...
denispir wrote: January 13th, 2019, 8:23 pm [...] but my knowledge of, say, Wesnoth-Lua libraries or helper modules [not to mention the hidden but necessary Lua/cpp interface] are null, so that my understanding is still limited) (is there any developer guide about that, aimed at helping potential Wesnoth-Lua developpers to catch up with the present state of the art?).
Wesnoth' Lua is described here, with the library functions listed in the links of section 4. This does not include the ai_helper functions, but the comments at the beginning of a lot of the functions in the ai_helper.lua file itself are reasonably detailed.
denispir wrote: January 13th, 2019, 8:23 pm I may develop in another thread if ever I have time and energy to dedicate to such a goal (I mean to actively participate). For now, globally, my point of view is that a standard core AI should only deal with optimising technical and strategic level (lower and higher level goals ultimately meet: eg gaining time, occupying good defense tiles, preserving HPs), while tactical ie medium level goals, which are by far the most difficult ones, are to be left to (very) specialised micro AIs driving sets or groups of units --micro AIs which delegate the technical and strategic levels to the base AI. Read: this is more or less what you presently do with your micro AIs, except that there may be a wide set of micro AI types specialised for common or exotic tactical objectives (possibly each with a range of variant each time generalising would lead to over-complication and corresponding innumerable bugs); more like the assassin or guardian ones, rather than specialised for unit types like the animal ones.
I am looking forward to what you come up with If you ever get around to that. In the meantime, if you have specific suggestions of what could be added to the Micro AIs, we're always open to suggestions. No promises though, for a whole bunch of reasons, of course. ;)
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: Have an side AI confront a given enemy side

Post by denispir »

Thank you !
I will meet Fred...
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: Have an side AI confront a given enemy side

Post by denispir »

Hello @Beetlenaut, also maybe @mattsc,

Thank you for this pretty interesting reply. I like very much the idea of a "heat map" (really new to me) (density map?), as well as weighting it according to what I'd call "target importance" (this, I had already thought at it, not only for leaders but for categories of units, eg mages, skeletons, healers). [On the other hand, I think we do not need and maybe should not try to find the best possible or cleverest methods or algorithms. Not only for a first approach but even ultimately and in principle.]

I am thinking at a rather simple but hopefully not stupid approach, interesting enough for human players, in both cases were the AI is ally or opponent.

First, units governed by such an AI as well enemies targeted are determined by a parameter. The "group" or "troop" it governs may be or not rather grouped initially, but in any case one main point of the (micro) AI is to let them perform a "grouping movement" (could be its AI name, or of one of its ccomponents). The set of targeted enemies is as it is, but a game author considering this AI should use it for enemy sets that are or will naturally group & regroup, esp when governed by a human, typically a mass of fighters in combat.

A possible method may be such. On each new turn, determine:
  • the center of the mass of ennemies (heat map or just barycenter/center of mass)
  • 3 subgroups of our own units termed "early" "late" "medium", depending on how early they may reach the goal (due to present proximity & terrain speed)
Then, when processing those units one by one:
  • Late units just progress as much as possible (ignoring potential combats).
  • Early units just basically wait if no combat is possible (both attacks to and from enemies), thus tending to group with later units. If there is potential combat, then depending on situation, especially ratio of forces in presence, either attack, stand fast on best terrain, or retreat (thus grouping with farther units).
    Medium units either progress or stay on place depending on whether early units retreated.
First refinements, necessary I guess, would be to exclude outlier units among:
  • enemy units considered by this AI, since the point of the AI is not to chase lonely or remote units, but instead to establish oe reinforce frontlines or zones of combat; as a side effect this would prevent a human opponent luring away the whole group using 1-2 units
  • our own units, to avoid delaying the whole group for 1-2 units only (they will eventually join anyway if and when possible)
There are I guess obvious gross methods to determine outliers without computationnally intensive calculations like std dev (and again wo do not need accuracy), and we need this only once per turn, not per unit.

Then, once we are or rather the troop is there, or very close to, I think the AI framework is such that we have 2 possibilities:
Release the group's units to the std AI.
Transfer them to another one.
Keep them in charge, but delegate combat to the std CA.
The 2 last ones aim at introducing or changing std behaviour in front or melee combat, I think in particular at a kind of "survival" or "safest recover" CA to have important units be protected (esp high XP), survive, or heal --what the RCA AI seems to do very badly (Fred apparently much better). Another point is group combat, esp the order of attacks on a same unit (terrible by RCA, or maybe inexistent), as well as the priority of targets (including high XP again).

I would enjoy critics, while I'm exploring LuaWml and existing micro AIs for the ways to realise that.

PS: What about making a forum dedicated to AI (design and usage; official and custom; general & micro AI) ?
denispir
Posts: 184
Joined: March 14th, 2013, 12:26 am

Re: Have an side AI confront a given enemy side

Post by denispir »

Hello @Beetlenaut, also maybe @mattsc,

Thank you for this pretty interesting reply. I like very much the idea of a "heat map" (really new to me) (density map?), as well as weighting it according to what I'd call "target importance" (this, I had already thought at it, not only for leaders but for categories of units, eg mages, skeletons, healers). [On the other hand, I think we do not need and maybe should not try to find the best possible or cleverest methods or algorithms. Not only for a first approach but even ultimately and in principle.]

I am thinking at a rather simple but hopefully not stupid approach, interesting enough for human players, in both cases were the AI is ally or opponent.

First, units governed by such an AI as well enemies targeted are determined by a parameter. The "group" or "troop" it governs may be or not rather grouped initially, but in any case one main point of the (micro) AI is to let them perform a "grouping movement" (could be its AI name, or of one of its ccomponents). The set of targeted enemies is as it is, but a game author considering this AI should use it for enemy sets that are or will naturally group & regroup, esp when governed by a human, typically a mass of fighters in combat.

A possible method may be such. On each new turn, determine:
  • the center of the mass of ennemies (heat map or just barycenter/center of mass)
  • 3 subgroups of our own units termed "early" "late" "medium", depending on how early they may reach the goal (due to present proximity & terrain speed)
Then, when processing those units one by one:
  • Late units just progress as much as possible (ignoring potential combats).
  • Early units just basically wait if no combat is possible (both attacks to and from enemies), thus tending to group with later units. If there is potential combat, then depending on situation, especially ratio of forces in presence, either attack, stand fast on best terrain, or retreat (thus grouping with farther units).
    Medium units either progress or stay on place depending on whether early units retreated.
First refinements, necessary I guess, would be to exclude outlier units among:
  • enemy units considered by this AI, since the point of the AI is not to chase lonely or remote units, but instead to establish or reinforce frontlines or zones of combat; as a side effect this would prevent a human opponent luring away the whole group using 1-2 units
  • our own units, to avoid delaying the whole group for 1-2 units only (they will eventually join anyway if and when possible)
There are I guess obvious gross methods to determine outliers without computationnally intensive calculations like std dev (and again wo do not need accuracy), and we need this only once per turn, not per unit.

Then, once we are or rather the troop is there, or very close to, I think the AI framework is such that we have 2 possibilities:
Release the group's units to the std AI.
Transfer them to another one.
Keep them in charge, but delegate combat to the std CA.
The 2 last ones aim at introducing or changing std behaviour in front or melee combat, I think in particular at a kind of "survival" or "safest recover" CA to have important units be protected (esp high XP), survive, or heal --what the RCA AI seems to do very badly (Fred apparently much better). Another point is group combat, esp the order of attacks on a same unit (terrible by RCA, or maybe inexistent), as well as the priority of targets (including high XP again).

I would enjoy critics, while I'm exploring LuaWml and existing micro AIs for the ways to realise that.

PS: What about making a forum dedicated to AI (design and usage; official and custom; general & micro AI) ?
Post Reply