enclave's Lua thread

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

Moderators: Forum Moderators, Developers

Re: enclave's Lua thread

Postby enclave » August 13th, 2016, 6:58 pm

Hi!
Anyone knows if there is a lua code for [damage] tag in
Code: Select all
 [object]
          silent=yes
         [effect]
            apply_to=attack
            name=club
            [set_specials]
               [damage]

?
Particularily the part with backstab=yes
Thank you!
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby enclave » October 10th, 2016, 8:34 pm

Hi again! Some rather advanced questions:
1) anyone managed to make select event work in 1.12 ? (in multiplayer games) without OOS?
I mean is there way to synchronize the game state with lua or something..?

2) any lua to temporarily modify the cursor image during multiplayer game? (doesnt matter the OOS i guess..)
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby gfgtdf » October 10th, 2016, 8:47 pm

I mean is there way to synchronize the game state with lua or something..?


in 1.13 you can use [do_command][fire_event][/fire_event][/do_command] to fire an event form a select even in a synced way. In 1.12 you can do similar with the ai.synced_command function but you'd have to ask someone else on how to get an instance of the ai table. There is a function wesnoth.debug_ai() to get the ai table but wesnoth.debug_ai() only wokrs in debug mode
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Postby enclave » October 10th, 2016, 8:58 pm

interesting.. has maybe anyone tried something like wesnoth.select_unit in select event? is select_unit synchronized?

PS. by the way is wesnoth.highlight_hex(x, y) highlighting hex just for 1 player or for everyone?

PPS. is this synced?
wesnoth.get_displayed_unit()
Returns a proxy to the unit currently displayed in the side pane of the user interface, if any.


PPPS. i normaly only ask in forums when trying every single function will take me hours/days.. while somebody may have done it already and can confirm its doable this way or not..
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby gfgtdf » October 10th, 2016, 9:18 pm

PS. by the way is wesnoth.highlight_hex(x, y) highlighting hex just for 1 player or for everyone?


well this ofcourse depends on whether this rund from a synced context or not, if it runs from a synced contenxt this means the code run on every cleint and tus the unti is selected on all clients, if you use this from a select event this code onyl runs on one client so only that client ill select that other 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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Postby enclave » October 11th, 2016, 3:21 pm

ah.. thx.. so I guess my previous questions about synchronization also depend on the event?
is wesnoth.synchronize_choice working only for dialog? not going to work in select event anyhow?
Thanks...
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby gfgtdf » October 11th, 2016, 3:57 pm

when yourin an normal select event triggered by the user, your only change to make gamestate changes in a synced way is to
1) use ai.synced_command or [do_command] like i said before.
2) Set some variable and use that in a later synced evnet, for example a moveto event, in this case you must quer< that variable withvia wesnoth.synchronize_choice
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Postby enclave » October 11th, 2016, 6:20 pm

thx gfgtdf i will "google" the wikipedia for help with ai_command.. maybe can find something..

for now, maybe somebody has an example code of these luas (ai_command, syncronize_choice in moveto event) that he or she would like to share..? Thanks!
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby gfgtdf » October 11th, 2016, 10:50 pm

Well one way to might be to register a custom ai action and then store the ai table in a global lua vaiable, this only works if you have at least one ai side in your scenario though.
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Postby enclave » October 14th, 2016, 2:41 am

The answer was already given here in the end of page1 by gfgtdf: https://forums.wesnoth.org/viewtopic.php?f=58&t=44712
And the answer to question #5 is - it is a table


Hi! a new question..
Idealy I think Elvish_Pillager could answer this, but maybe somebody else also knows?

If I use the following code:
Code: Select all
wesnoth.theme_items.unit_weapons = function()
  local wunit = wesnoth.get_displayed_unit()
  if not wunit then return {} end
 return { {"element",{
 tooltip="some text",
 text="some text"}} }
  end

Then it removes all information about unit attacks or attack specials..

What would I need to change/add to the code to:
1) Insert new line.. so that it would look like:
some text1
some text2
2) keep all original attack information (and insert new line after)
3) keep all original attack information (but replace ranged attack with custom text) ?

Thanks very much in advance, even if you could answer just a single one of 3..

PS. 4) if you know how to add image to it, would be cool.. but I assume it only works for status.. or am I wrong?

PPS. I tried this:
Code: Select all
table.insert(s, { "element", {
in different variations, but couldn't get it to work yet.
Spoiler:

5) Maybe somebody knows what type of variable is "s" in table.insert(s, { "element", { ?
When I put nothing, says, not enough variables or something, when I put "text" says error again that table expected not string..

I will probably figure it all out myself today.. Just asked this question yesterday to be able to wake up and start coding with some information.. but well.. not today :)

Thanks!
Last edited by enclave on October 20th, 2016, 1:10 pm, edited 2 times in total.
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby enclave » October 20th, 2016, 12:00 am

Hi everyone,
I have a very confusing question, and before I ask it I will explain a little bit details..
I am playing around with wesnoth.theme_items ... and I will be honest, I am modifying the top panel information for my particular add-on.. which has 4 resource types, food, wood, gold, stone.. I am trying to make the panel display different values for different players.. like it displays normaly gold, villages, income, upkeep.. each player can see different information, related to each individual player..
Maybe I need some information about wesnoth.theme_items.side_playing .. maybe about wesnoth.theme_items.position .. maybe I need something similar to wesnoth.get_displayed_unit() for top panel.. so I could use the variables of it..

So I tried to phrase my following question as best as I could:

Does anyone know what lua code would be able to tell me which player (or side) is currently in front of monitor..?
What i mean is that for example there are 3 players:
player1 side 1
player2 side 2
player3 side 3
It is currently player1 turn, so $side_number or wesnoth.get_variable("side_number") would give me 1 as a result..
But the gold displayed in top panel for side 1 is different than the gold displayed for side 2 or side 3..
I am currently player2 side 2 viewing my top panel and it says 80 gold.. what command could tell me that it is at the moment particular player 2 viewing 80 gold..? something that would return side 2?

At the moment if I do this code:
Code: Select all
local old_display_terrain = wesnoth.theme_items.terrain
wesnoth.theme_items.terrain = function()
local oldvalue = old_display_terrain()
     local newtag1 = T.element { image="items/straw-bale1.png~SCALE(32,32)~CROP(7,7,18,18)~SCALE(18,18)",
     tooltip="food" }
     local newtag2 = T.element { text=wesnoth.get_variable("player["..wesnoth.get_variable("side_number").."].food").." ",
     tooltip="Amount of food current side has"}
     --
     local newtag3 = T.element { image="terrain/forest/deciduous-summer-small.png~CROP(42,35,63,75)~SCALE(18,18)",
     tooltip="wood" }
     local newtag4 = T.element { text=wesnoth.get_variable("player["..wesnoth.get_variable("side_number").."].wood").." ",
     tooltip="Amount of wood current side has"}
     --
     local newtag5 = T.element { image="scenery/rock2.png~CS(42,42,42)~CROP(12,17,53,40)~SCALE(16,16)",
     tooltip="stone" }
     local newtag6 = T.element { text=wesnoth.get_variable("player["..wesnoth.get_variable("side_number").."].stone").." ",
     tooltip="Amount of stone current side has"}
     --
     local newtag7 = T.element { image="items/gold-coins-small.png~CROP(22,24,31,30)~SCALE(17,17)",
     tooltip="gold" }
     local newtag8 = T.element { text=wesnoth.get_variable("player["..wesnoth.get_variable("side_number").."].gold").."     ",
     tooltip="Amount of gold current side has"}
      table.insert(oldvalue,1,newtag1)
     table.insert(oldvalue,2,newtag2)
     table.insert(oldvalue,3,newtag3)
     table.insert(oldvalue,4,newtag4)
     table.insert(oldvalue,5,newtag5)
     table.insert(oldvalue,6,newtag6)
     table.insert(oldvalue,7,newtag7)
     table.insert(oldvalue,8,newtag8)
  return oldvalue
end

Then it shows same amount of resources to all sides and players and probably even observers.. shows the current side turn resources to everyone.

Thank you very much in advance..!
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby gfgtdf » October 20th, 2016, 1:15 pm

wesnoth 1.13 has the function wesnoth.get_viewing_side which returns the side that the ui curently shows the game for.

In wesnoth 1.12 there is no such function, you could however write sucha function although its not easy. One approach woudl be to first write a function liek
Code: Select all
-- parses a text value of from wesnoth.theme_items.income to a number
function parse_income_str(str)
  --TODO
end

function get_vieweing_side_income()
  return parse_income_str(helper.get_child(wesnoth.theme_items.income, "text"))
end

function is_viewing_side(side)
  wesnoth.sides[side].income = wesnoth.sides[side].income + 10000
  local res = get_vieweing_side_income() > 5000
  wesnoth.sides[side].income = wesnoth.sides[side].income - 10000
end

function get_viewing_side()
  for i = 1, #wesnoth.sides do
    if is_viewing_side(i) then
      return i
    end
  end
end


Note that this code won't work as it, since its untested and the function parse_income_str still needs to be implemented.
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: enclave's Lua thread

Postby enclave » October 20th, 2016, 3:39 pm

gfgtdf, Please save your time and skip reading the next 2 posts, read my last post, maybe you know answer?
Thanks gfgtdf so much!!! I will try to study this and other materials since your main idea is that it is doable, I wasn't sure, I thought there would be 2 options:
1) there was an easy function that would return viewing side for UI (but thanks to you I know it doesn't exist in 1.12)
2) I would have to create something like that myself.. (so at least now I don't need to spend time looking for existing function)

Do you know how I could extract upkeep amount string or integer from a table?
Code: Select all
local current_display_upkeep = wesnoth.theme_items.upkeep


I was thinking that maybe I could set every side a set amount of upkeep, since it's always 0 gold in my mod anyway so it would not affect anything (for example side 1 , upkeep = 1, side 2 = upkeep 2, etc) and then make lua an if that would check with wesnoth.theme_items.upkeep and only update the display if wesnoth.theme_items.upkeep is matching the $all_sides[$side_number].upkeep

But I think I have a problem, I don't know how to extract the upkeep value from wesnoth.theme_items.upkeep because it is a table... Maybe you know the command I need to use to get the value?

for example I do:
Code: Select all
local current_display_upkeep = wesnoth.theme_items.upkeep

than I can do
Code: Select all
local display_upkeep = current_display_upkeep()

and now display_upkeep it is still a table.. not a string or integer..
how to convert it into string or integer? how to extract the value of upkeep?
is it something like?
Code: Select all
local display_upkeep_string = display_upkeep[1].upkeep

I'm really bad with lua, and my worst part is that I nearly dont understand any reading theory but learn quickly if i see practical code :(
or do i need to use something like this:
Code: Select all
function get_child(display_upkeep, upkeep)
    for i = 1, #display_upkeep do
        local upkeep_string = display_upkeep[i]
        if upkeep_string[1] == upkeep then return upkeep_string[2] end
    end
end

I really don't know.. I assume with your incredible to me lua level it will not take you much time to figure out the function I need..
Thanks so much, again, you saving me..
Last edited by enclave on October 21st, 2016, 12:04 am, edited 1 time in total.
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby enclave » October 20th, 2016, 9:08 pm

I tried tens or hundred of different versions of how to extract the table element from wesnoth.theme_items.upkeep
but still nothing works..
my best shots were something like:
Code: Select all
local current_display_upkeep = wesnoth.theme_items.upkeep
local display_upkeep = current_display_upkeep()
wesnoth.message("lala","lala" .. display_upkeep.T.element.text[1] .. "lala")

and of course it doesnt work.. and im not surprised at all...
I tried to do it without T, with T, without element, with element, without text, with text.. with name, with value.. with numbers and without numbers, etc etc
there must be a way.. but it always says I get a nil value..

I keep trying but I don't get the structure..
Code: Select all
wesnoth.message("bum","bum" .. display_upkeep["T"]["element"].text .. "bum")


PS. i just realized I can use this code of yours for it
Code: Select all
function get_vieweing_side_income()
  return parse_income_str(helper.get_child(wesnoth.theme_items.income, "text"))
end


I just need to figure out how exactly function in lua work..
can I just assign variable to that function like this?
Code: Select all
local display.upkeep = get_vieweing_side_income()

Will try in a sek.. stupid me..
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: enclave's Lua thread

Postby enclave » October 21st, 2016, 12:12 am

gfgtdf, I have tried your function making code like this:

Code: Select all
local current_display_upkeep = wesnoth.theme_items.upkeep
local display_upkeep = current_display_upkeep()

function get_vieweing_side_upkeep()
  --return parse_upkeep_str(helper.get_child(wesnoth.theme_items.upkeep, "text"))
  --return parse_upkeep_str(helper.get_child(display_upkeep, "text"))
  return parse_upkeep_str(helper.get_child(current_display_upkeep, "text"))
end
local abc = get_vieweing_side_upkeep()
wesnoth.message("bum","bum" .. abc .. "bum")


It gives an error saying: "attempt to get length of local 'cfg' (a function value)"
Ah.. bloody stupid me again.. I need to create parse_upkeep_str() function to make it work...
Heh, at least I'm closer to the win.. need to use my brain next time better... whole day doing stupid things..

i'm just so unfamiliar with lua, that I couldn't even understand what is what.. but I studied some information today :))) a bit of useful time spent too.

PS. couldn't we just do it with "tonumber"?
Code: Select all
function get_vieweing_side_upkeep()
  return tonumber(helper.get_child(wesnoth.theme_items.upkeep, "text"))
end
or it doesn't work this way? I'm going to sleep, so will try tomorrow..

PPS. I tried now and the function itself does not work :(
Tried different variations, more or less like so:
Code: Select all
function get_vieweing_side_upkeep()
local current_display_upkeep = wesnoth.theme_items.upkeep
local display_upkeep = current_display_upkeep()
  --return helper.get_child(wesnoth.theme_items.upkeep, "text")
  --return helper.get_child(display_upkeep, "text")
  --return helper.get_child(current_display_upkeep, "text")
  return helper.get_child(display_upkeep, "text")
end
local abc = get_vieweing_side_upkeep()
wesnoth.message("bum","bum" .. abc .. "bum")

It always gives and error, in it's current state it says attempt to concatenate local 'abc' a nil value
but in the other 2 variations says same thing as before:
"attempt to get length of local 'cfg' (a function value)"

I don't know where the mistake could be.. should we not include "element" somewhere or T.element? we only trying to get text.. I have no idea how it works.. Shouldn't we specify array index or something..? when we getting child..
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

PreviousNext

Return to Lua Labs

Who is online

Users browsing this forum: No registered users and 1 guest