Change control over side to AI and back mid turn

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

Change control over side to AI and back mid turn

Post by Nyanyanyan » October 4th, 2019, 4:38 pm

For a mod I'd like to change the control over a player's side to the ai for the movement of some units and then back to the player.

The first thing I thought of was simply switch the control of the side to controller=ai at the beginning of the turn and then give it over to the player once the AI is done, but I didn't find anything I could attach a switch event to, that indicates the AI actually being done (save for it ending the turn).

The second thing I tried was putting in a menu item that switches the control to the AI after the player is done moving, which would require a bit more work but at least it would work in theory.
Problem here is that for some reason the AI does not move, despite me switching the ai to ai_default_rca.cfg and setting controller=ai. It also does not end the turn or do anything else.

This is my code:

Code: Select all

[event]
	name=prestart
	first_time_only=no
	[set_menu_item]
		id=give_ai_command
		description=_"Move Command"
		[command]
			[modify_side]
				side=1
				controller=ai
				switch_ai=ai/ais/ai_default_rca.cfg
			[/modify_side]
		[/command]
	[/set_menu_item]
[/event]
Any insights would be appreciated since my knowledge of the Wesnoth AI is very limited.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.

User avatar
Celtic_Minstrel
Developer
Posts: 1532
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Change control over side to AI and back mid turn

Post by Celtic_Minstrel » October 5th, 2019, 1:09 am

Hmm... this is somewhat interesting. There is a way to do the precise opposite of what you want (where the AI moves some units, then allows the player to move others), but I don't know of a way to let the player move some units and then the AI takes over until the end of the turn. Could this reverse feature possibly get you the result you're looking for?

(There is also the "droid" functionality that can be invoked by a command, but I believe that sets the side to AI until the player reverses it by issuing the command again, so it's probably also not what you want; and I don't know of a way to invoke it from WML or Lua.)
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

gfgtdf
Developer
Posts: 1190
Joined: February 10th, 2013, 2:25 pm

Re: Change control over side to AI and back mid turn

Post by gfgtdf » October 5th, 2019, 1:45 am

hmm sounds like a bug, my first suspect woudl be the equivalent of this line https://github.com/wesnoth/wesnoth/blob ... 1499-L1504 missing in the code that changes controllers from wml. can you run (c++) source patches to test if we give you them?
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
Nyanyanyan
Posts: 35
Joined: May 11th, 2018, 10:40 pm

Re: Change control over side to AI and back mid turn

Post by Nyanyanyan » October 5th, 2019, 10:05 am

Celtic_Minstrel wrote:
October 5th, 2019, 1:09 am
Could this reverse feature possibly get you the result you're looking for?
It wouldn't be ideal but I'd apprciate if you'd tell me how to do that.

gfgtdf wrote:
October 5th, 2019, 1:45 am
can you run (c++) source patches to test if we give you them?
I don't think I have a compiler for c++ installed if that's needed but if you give me instructions I could install the needed components.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.

User avatar
Celtic_Minstrel
Developer
Posts: 1532
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Change control over side to AI and back mid turn

Post by Celtic_Minstrel » October 5th, 2019, 2:46 pm

So what I was envisioning is a little involved, but the general outline would be:
  1. Set the side to AI initially, rather than human (ie, controller=ai in the [side] tag).
  2. Add parameters to the AI that make it only consider a subset of the units. If you don't do this, even units that it doesn't move will have their moves zeroed. I'm not 100% sure how to do this, though...
  3. Add an extra Lua candidate action with a lower priority than all other candidate actions. It does nothing but call ai.fallback_human().
That said, gfgtdf's bugfix is likely going to be more effective than this method.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

gfgtdf
Developer
Posts: 1190
Joined: February 10th, 2013, 2:25 pm

Re: Change control over side to AI and back mid turn

Post by gfgtdf » October 5th, 2019, 3:38 pm

the bugfix https://github.com/wesnoth/wesnoth/comm ... e3407ef162 is pushed to master and i tested that it works , hopefully i don't forget to backport it to 1.14 before a 1.14.10 release.
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
Nyanyanyan
Posts: 35
Joined: May 11th, 2018, 10:40 pm

Re: Change control over side to AI and back mid turn

Post by Nyanyanyan » October 5th, 2019, 8:50 pm

gfgtdf wrote:
October 5th, 2019, 3:38 pm
the bugfix is pushed to master
Does that mean it's fixed in 1.15.x ?

And I'm not big on lua or AI parameters, so I don't think I can implement that workaround @celtic.
Author of Age of Lords and the Revolution and Civil War and Expendable Leaders 2 multiplayer mods.

gfgtdf
Developer
Posts: 1190
Joined: February 10th, 2013, 2:25 pm

Re: Change control over side to AI and back mid turn

Post by gfgtdf » October 5th, 2019, 9:42 pm

It means that when there is a new weanoth version that will be 1.14.10 or 1.15.2 it will probably be fixed in that version.
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
Nyanyanyan
Posts: 35
Joined: May 11th, 2018, 10:40 pm

Re: Change control over side to AI and back mid turn

Post by Nyanyanyan » October 5th, 2019, 9:47 pm

Great, thanks.
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: Change control over side to AI and back mid turn

Post by mattsc » October 6th, 2019, 2:12 am

I know you said that you'll probably not do it this way, but just for the record (in case somebody looks up this thread in the future) I'll add one thing: Celtic_Minstrel's method should work, but I'd probably do a minor variation on it: Instead of the candidate action in Step 3. returning a very low score, I'd set it up to return a variable score: zero at the beginning, and then a very high score (higher than all the other CAs) once the turn-over-to-player condition is met. That way you do not need to restrict Step 2. to only a subset of the units. You "only" need to make sure that the units you want moved by the AI are moved first (which might or might not be easier, depending on your scenario setup).

Post Reply