enclave's Lua thread

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

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

Re: enclave's Lua thread

Post by enclave »

thx gfgtdf ill look into your info ;)
PS. i found that any moveto event makes filtered units (units matching the filter) stop before attack, even if its empty.. any ideas anyone? maybe allow undo could help or something? any any ideas? thanks..
gfgtdf
Developer
Posts: 1431
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

yes allow undo should help, but ofc you should only do this if this is really undoable.

what teh engine 'thinks' behind this is like this: "an non-undoable event occured, this usually implies a gamestate change so we should give the player a chance to reconsider his move/attack"
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Hi,

I know there is a code like this to select a unit:

Code: Select all

	[select_unit]
		x,y=$x1,$y1
		fire_event=yes
	[/select_unit]
but is there anything to deselect it in Lua or WML? Thanks very much..
PS. If anyone knows how to select unit in lua, let me know.. maybe i can find the opposite way if i know how to select..
PPS. I tried to make [select_unit] twice, no luck.
gfgtdf
Developer
Posts: 1431
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

in wensoth 1.13 wesnoth.select_unit(nil) should deselect the unit.
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

thanks..
any ideas for 1.12? it deselects with end turn.. but what else could do it without ending turn?
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: enclave's Lua thread

Post by zookeeper »

To get rid of the selection in 1.12, I'd try storing+unstoring the selected unit, or something similar.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

zookeeper wrote:To get rid of the selection in 1.12, I'd try storing+unstoring the selected unit, or something similar.
Thanks! Will try that, and post back if it worked :D
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

unfortunately not worked for me.. (it has to work when watching replay)
tried many different cheats.. storing (with and without kill=yes, even when unit disappears, the hex is still selected) unstoring and also the following (including other, not listed, animate unit, x,y=0,0 etc):

Code: Select all

[select_unit]
		x,y=$CG_archer.x,$CG_archer.y
		highlight=no
		fire_event=yes
	[/select_unit]
	[scroll_to_unit]
		x,y=$CG_archer.x,$CG_archer.y
		immediate=true
		for_side=$side_number
	[/scroll_to_unit]
[lock_view]
[/lock_view]
[unlock_view]
[/unlock_view]
[lua]
code = <<
wesnoth.select_hex(wesnoth.get_variable("CG_archer.x"),wesnoth.get_variable("CG_archer.y"), false, false)
>>
[/lua]
ran out of ideas.. I guess have to wait with that feature until 1.13 becomes populated..
will be happy to read more thoughts if anyone has any ideas..
PS. it might work for a player who is playing and in certain circumstances be useful.. I'll check it some day and post back. But I really needed it to work for observers.
PPS. maybe it works when storing unstoring but is not synced to be visible for observers? Then maybe somebody knows the lua variable name/address which I could synchronize in some synced event something that was saying unit[x].selected=false? I might sound stupid.. but who knows, maybe something like this exists? :D Thanks in advance everyone who spent time on this..
PPPS. I can confirm that storing/unstoring has no effect on selection. even put a delay between them, with kill.. the hex remains selected all the time (same for current player or observer):

Code: Select all

[store_unit]
   [filter]
      x=$x1
      y=$y1
   [/filter]
   kill=yes
   variable=CG_selected_unit
[/store_unit]
[delay]
   time=500
[/delay]
[unstore_unit]
   variable=CG_selected_unit
   animate=no
[/unstore_unit]
[redraw]
[/redraw]
{CLEAR_VARIABLE CG_selected_unit}
Spent couple of hours trying to find some kind of cheat, but nothing worked.. given up. It feels like deselect would work if I triggered some kind of attack event ([harm_unit]for example), but I have no idea how to make such a thing unnoticed by players during moveto event, feels unhackable.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Hi, today we discovered that 1.12 random generator has a bug (seems like in 1.13.2 it is fixed) where different clients see same sides placed in different castles.. for example red leader starts in 14.5 for most players, but some players see it as orange.. and then orange side may see that somebody else is moving his units.. when it's not even orange turn.
It knew about the bug for a long time but thought it was problem of my mod, but it was not.. today we tried it on default era and default random maps built into 1.12 wesnoth.
I tried to use the code following code to fix it, but it does not fix.. any ideas what I do wrong?

Code: Select all

[event]
	name=turn 1
		[store_unit]
		   [filter]
		   [/filter]
		   variable=ns_oos_all_units
		[/store_unit]
	[lua]
		code = <<
local result = wesnoth.synchronize_choice(
function()
return { value = wesnoth.get_variable("ns_oos_all_units") }
end,
function()
return { value = wesnoth.get_variable("ns_oos_all_units") }
end)
wesnoth.set_variable("ns_oos_all_units",result.value)
>>
         [/lua]
		{FOREACH ns_oos_all_units ns_oos_counter1dd}
		   [unstore_unit]
		      variable=ns_oos_all_units[$ns_oos_counter1dd]
		      animate=no
		   [/unstore_unit]
		{NEXT ns_oos_counter1dd}
[/event]
Thanks very much in advance!

PS. ah i just tried to mark kill=yes and it looks like i have problem with unstoring :) as nothing has reappeared..
cant find my ns_oos_all_units container now.. so I guess lua code is wrong.. and replacing it with nil

Changed code:

Code: Select all

		[lua]
		code = <<
top = wesnoth.get_variable("ns_oos_all_units.length")-1
if wesnoth.get_variable("ns_oos_all_units.length")>0 then
   for k = 0, top , 1 do
      local result = wesnoth.synchronize_choice(
      function()
      return { value = wesnoth.get_variable("ns_oos_all_units["..k.."]") }
      end,
      function()
      return { value = wesnoth.get_variable("ns_oos_all_units["..k.."]") }
      end)
      wesnoth.set_variable("ns_oos_all_units["..k.."]",result.value)
      end
   end
>>
[/lua]
but this for some reason has eaten half of the units... and half reappeared..
gfgtdf
Developer
Posts: 1431
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Post by gfgtdf »

your problem is that { value = wesnoth.get_variable("ns_oos_all_units["..k.."]") } is not a valid wml table so synchronize_choice returns nil (somhow wesnoth 1.12 won't guive you a proper error message unless you use debug mode). Remember how wml data is encoded in lua ( https://wiki.wesnoth.org/LuaWML#Encodin ... Lua_tables ) and that wesnoth.get_variable("ns_oos_all_units["..k.."]") returns a table not an attribute.

Also you don't need to use a loop, just store the unit in a nested variable so that you use get_varaible on the container, here an updated code:

Code: Select all


[event]
   name=turn 1
      [store_unit]
         [filter]
         [/filter]
         variable=ns_oos.all_units
         kill=yes 
      [/store_unit]
   [lua]
      code = <<
local result = wesnoth.synchronize_choice(
function()
return wesnoth.get_variable("ns_oos")
end
)
wesnoth.set_variable("ns_oos", result)
>>
         [/lua]
      {FOREACH ns_oos.all_units ns_oos_counter1dd}
         [unstore_unit]
            variable=ns_oos.all_units[$ns_oos_counter1dd]
            animate=no
         [/unstore_unit]
      {NEXT ns_oos_counter1dd}
[/event]
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Thanks very much gfgtdf!!! This has worked and now I understand lua a little bit better.. I wasn't sure I could do this without value in result etc.. Now I will test if it fixed OOS and I hope it did..!
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

Regarding my previous post, I'm quite sure that it fixed oos problem.. but haven't had a chance to test it enough to tell for sure.

Could somebody tell me how to write any (ideally all) of the following 3 wml into lua code?:

Code: Select all

[store_locations]
   x,y=5,25
   [not]
      terrain=M*,W*,M*^*,W*^*,Ai*,Ai*^*,Rrc,Q*,X*,Q*^*,X*^*,Iwr,Rp^Ecf,Rp^Dr,_off^_usr
   [/not]
   variable=saved_terrains
[/store_locations]

Code: Select all

[store_locations]
   x=1-10
   y=1-10
   [not]
      terrain=_off^_usr
   [/not]
   variable=ns_cube
[/store_locations]

Code: Select all

[store_locations]
   find_in=ns_cube
   terrain=M*,M*^*
   variable=ns_mountains
[/store_locations]
That's the only example in wiki and it doesn't help me somehow..:

Code: Select all

for i,loc in ipairs(wesnoth.get_locations { terrain = "Gg" }) do
    wesnoth.set_terrain(loc[1], loc[2], "Rr")
end
Thanks in advance!
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Post by enclave »

True, wesnoth.fire is a way to go.. but I was wondering if it could be done with wesnoth.get_locations and if yes then how.. if somebody could help me understand I would later try to compare the speed of these 2 different methods..

for now I will actually try to convert my codes into wesnoth.fire and later I still hope somebody could help with wesnoth.get_locations..
User avatar
Ravana
Forum Moderator
Posts: 2934
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: enclave's Lua thread

Post by Ravana »

I understand that it should work as

Code: Select all

wesnoth.get_locations { x = "5", y = "25", { "not", { terrain = "M*,W*,M*^*,W*^*,Ai*,Ai*^*,Rrc,Q*,X*,Q*^*,X*^*,Iwr,Rp^Ecf,Rp^Dr,_off^_usr"} } })
Post Reply