Possible 'select' event for AI 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
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Possible 'select' event for AI side?

Post by WhiteWolf »

Hello,

I am looking for a way to fire an event every time before a move is made, for a side that's controlled by the AI. Technically, a 'select' event with first_time_only=no and respective filters would be perfect, however, it seems the AI doesn't actually select its units before moving them, and the event doesn't trigger.
Is there any way to do a similar event for an AI side?

Thanks
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
User avatar
Ravana
Forum Moderator
Posts: 2950
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Possible 'select' event for AI side?

Post by Ravana »

Exit hex?
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Possible 'select' event for AI side?

Post by WhiteWolf »

I was going to say not plausible, because the unit needs to stay where it is, because the targeted unit might die during the triggered event. I was under the impression the even if I call [cancel_action], the unit will move one hex because it's 'exit hex'. I tried it and it seems I thought wrong.

However, if the event does trigger, and the action is cancelled, will the AI try to move the unit again? Here's the train of events how it should go:
- A clash is concluded and a gamestate appears where before the next move, the said event must fire.
- The technical approach to monitor when the event can fire is then an exit hex event. The AI tries to make a move, but because of the previously achieved gamestate, the event fires and the gamestate changes again. Therefore the move is canceled to allow the AI to reconsider its move. (not allowing it to make a completely moronic and useless move).
- The AI should continue playing (and the event keeps monitoring), but the AI should try to move it's previously canceled unit again.
Does it though? Or will this mean that the canceled unit won't move during this turn?
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
User avatar
octalot
General Code Maintainer
Posts: 783
Joined: July 17th, 2010, 7:40 pm
Location: Austria

Re: Possible 'select' event for AI side?

Post by octalot »

Maybe explain what part of the AI behavior you want to change, and then see if anyone can suggest a different method to do it?
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Possible 'select' event for AI side?

Post by gfgtdf »

Remember that select events are unsynced so killing a unit from a select event like you wanted at first would have resulted in out of sync errors and corrupted replays anyways.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Possible 'select' event for AI side?

Post by WhiteWolf »

octalot wrote: April 24th, 2019, 5:49 pm Maybe explain what part of the AI behavior you want to change, and then see if anyone can suggest a different method to do it?
I don't want to change AI behaviour, I only want it to try to move its unit again if has been [cancel_action]-ed manually. Sadly, having tried it, the AI doesn't do that. So exit hex is not the way to go.
I managed to reach the desired effect with a mixture of turn refresh, attack end and die events. I had hoped there'd be a simpler, more consistent way of doing it, but at least it works now. :)
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Possible 'select' event for AI side?

Post by mattsc »

WhiteWolf wrote: April 25th, 2019, 6:50 pm I don't want to change AI behaviour, I only want it to try to move its unit again if has been [cancel_action]-ed manually. Sadly, having tried it, the AI doesn't do that.
Well, it depends. The AI in principle does not mark a unit as having-attempted-to-move and then won't reconsider it. However, the candidate actions of the AI have a fail-safe against infinite loops built in, which black-lists and disables a candidate action that tried to execute a move but did not change the game state. Some CAs move individual units before being re-evaluated, others move multiple units. In the latter case, if the CA had already moved a different unit, it would not be black-listed and move the unit after the canceled action. Or a different CA might move the unit even if the original CA was black-listed.

But the bottom line is, this will likely not result in the unit reliably being moved afterward and therefore is not feasible for what you want.
WhiteWolf wrote: April 25th, 2019, 6:50 pm I managed to reach the desired effect with a mixture of turn refresh, attack end and die events. I had hoped there'd be a simpler, more consistent way of doing it, but at least it works now. :)
There is a different way. You could add another candidate action to the AI that in its evaluation function checks whether the trigger has been met, and then fires whatever you want done in the execution function. This is not as difficult as it might sound, but I know most people don't like to "mess with" the AI setup. ;-) I can point you to examples of how to set something like this up if you want. I don't remember there already being a CA that does exactly this.
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Possible 'select' event for AI side?

Post by WhiteWolf »

mattsc wrote: April 25th, 2019, 7:17 pm There is a different way. You could add another candidate action to the AI that in its evaluation function checks whether the trigger has been met, and then fires whatever you want done in the execution function. This is not as difficult as it might sound, but I know most people don't like to "mess with" the AI setup. ;-) I can point you to examples of how to set something like this up if you want. I don't remember there already being a CA that does exactly this.
Well, I count myself as one of those people, who don't dive into AI setup territory, apart from 'aggression' and a few [goal]'s... :D But examples could come in handy, I'd be happy to learn :)
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Possible 'select' event for AI side?

Post by mattsc »

WhiteWolf wrote: May 1st, 2019, 3:50 pm Well, I count myself as one of those people, who don't dive into AI setup territory, apart from 'aggression' and a few [goal]'s... :D But examples could come in handy, I'd be happy to learn :)
Well, since you have not said what exactly you want to do, I cannot give you anything too specific, but a detailed description of how to do it, together with a simple example is given here. If you want an integrated mainline campaign example, I can also point you to scenario "Black Flag" in SotBE.:

You want to include the custom candidate action like this in your scenario. You can use whatever you want for id= and name=. location= needs to be changed to wherever the AI code is, of course.

In this example, the AI code is here. This is likely much more complex than what you need, but ... well, as I said, I don't know what you want to do. In summary, you want an evaluation function like the one starting at l.15. It checks some condition and returns 300,000 if the condition is met. That score is higher than any of the other CAs, which means the corresponding execution function will then be called before any of the other CAs. Otherwise, if the condition is not met, 0 is returned, which means nothing happens (but zero also means that the CA is not black-listed, because it does not try to make anything happen, so it will be evaluated again after the next AI move).

Then you also need an execution function, that does what you want it to do, such as that starting at l.27 (or the much simpler example on the wiki). Depending on what you want to do, it could be very simple or quite complex. For example, it could be simply something like this line, if you wanted to place a unit on the map. But, in this example, that needs to go along with this code (the actual code of the synced command). That code could also be reduced to a few lines in the simplest case.

Without knowing more about what you want to do, I can't be more specific, I'm afraid. Let me know if you have follow-up questions. I hope the SotBE example is not too "intimidating". I chose that specifically because it shows how to place new units on the map in a synced way. Other manipulations of the situation on the map can be done in similar ways. If you let me know what exactly you want to do, I can put a slimmed down example together for you. And if you don't want to ruin any surprises by stating this here, you can send that to me in a PM. ;)
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Possible 'select' event for AI side?

Post by WhiteWolf »

mattsc wrote: May 1st, 2019, 8:26 pmIf you let me know what exactly you want to do, I can put a slimmed down example together for you. And if you don't want to ruin any surprises by stating this here, you can send that to me in a PM. ;)
Oh, it's not a secret, I just wanted to avoid a damn long post. :) But here it is in a spoiler for space, trying to be to the point but informative.
Spoiler:
I will dig though the SotBE examples in the following days, thanks :)
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Possible 'select' event for AI side?

Post by mattsc »

Thanks for posting this. That's quite an impressive setup. If I had time for playing Wesnoth, I'd have to check that out. ;) Maybe sometime in the future.

Let me start by saying that I am not saying that you should switch to a CA system. You seem to have a setup that works, so you might want to consider calling that good enough. I'm just responding to your question if there is a different way of doing this. Whether you consider that worth pursuing is something I obviously can't tell you. So, given that, I'll let you have a look at the examples first and decide whether this is something you want to try out, and if so, I'll be happy to help you figure out how to set it up. For now, just a couple additional general comments:
WhiteWolf wrote: May 2nd, 2019, 9:11 am This is how the AI moves should look:
  1. At the start, check the gamestate if it's card is playable. If it is, play it. This evaluation is made after the round start random card, which is a side turn event. (so it can immediately react to weather for example)
  2. Make a move.
  3. Check if it's card is playable in the new gamestate. If it is, play it.
  4. Repeat steps 2,3 :D
That's pretty much exactly what the candidate action system does. Let me rephrase it slightly in the way how the AI works through it:
  1. For (before) each AI move, check whether the conditions in the "play the card" evaluation function are met (check whether the card is playable).
  2. If so, return 300,000, which is higher than the score of any other AI move, which means the "play the card" execution function is called (= play the card).
  3. If not, return 0, which means moving on to finding the AI move with the next highest score and executing that.
  4. Go back to step 1 and repeat, until no CA returns a score > 0 any more.
In other words, "playing the card" becomes just another move of the AI, and by having the highest score, it is always the first thing that gets checked (and executed, if applicable). Hope that makes sense. That's the very short version, more details are given here.
WhiteWolf wrote: May 2nd, 2019, 9:11 am What I am trying to explain is that either the executor function must call an event that is removable, or the executor function itself must be addable and removable inside an event.
That's easy to do (you'd add/remove the entire CA, not just the execution function, but that's just a technical detail).
WhiteWolf wrote: May 2nd, 2019, 9:11 amHaving it continuously and then blocking it by some conditions that are added or removed by events could probably be an option though.
Actually, all you'd have to do is add another condition to the evaluation function that returns 0 based on whatever condition you want met or not met when you don't want this to be active, and you can leave everything else in place. An evaluation function that retiurns 0 means that the respective execution function will not get called.
WhiteWolf wrote: May 2nd, 2019, 9:11 am I will dig though the SotBE examples in the following days
Sounds good. As I said, let me know if you're interested in looking into this and we can talk about the details.
User avatar
WhiteWolf
Forum Moderator
Posts: 769
Joined: September 22nd, 2009, 7:48 pm
Location: Hungary

Re: Possible 'select' event for AI side?

Post by WhiteWolf »

Actually I discovered a bug today - because the current system uses die events, and in the events that follow I need the refreshed unit count on the sides, but the dying unit is not removed until the end of the die event, I remove it by hand before firing the card event.
This means plague attacks are currently broken :doh: So I will be switching to the AI-way definitely :) I'll PM you if I find questions then.
Main UMC campaigns: The Ravagers - now for 1.16, with new bugs!
Old UMC works: The Underness Series, consisting of 5 parts: The Desolation of Karlag, The Blind Sentinel, The Stone of the North, The Invasion Of The Western Cavalry, Fingerbone of Destiny
Post Reply