Dixie's Lua Thread

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

Moderator: Forum Moderators

Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Dixie's Lua Thread

Post by Luther »

Not quite. set_wml_tag_metatable is a shortcut for writing subtags within a WML table. IMO, if you break lines at the right places and indent, this makes it look more like real WML. wesnoth.fire simply executes an action tag. Action tags are those found at these pages: DirectActionsWML, InterfaceActionsWML, and InternalActionsWML (plus any you write yourself in Lua).
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Thanks for your replies!

Okay, so I am now trying to change maps using wesnoth.fire("replace_map", { ... }). I originally had the "map without a header" problem:

Code: Select all

wesnoth.fire("replace_map", {map=string.format("{~add-ons/Settlers_of_Wesnoth/maps/sow_%s.map}", game_stats.map), expand=true, reduce=true})
But i fixed it using Exasperation's solution as outlined here. I do get this error now, though:
Invalid WML found: replace_map: Map dimension(s) decrease but shrink is not set
Here's my code:

Code: Select all

local sow_map_file = wesnoth.dofile(string.format("~add-ons/Settlers_of_Wesnoth/maps/sow_%s.lua",game_stats.map))
wesnoth.fire("replace_map", {map=sow_map_file, expand=true, reduce=true})
I don't understand: what's the "shrink"? There's no field named "shrink"! Is it because I allow it to both expand and reduce? Does setting "reduce=true" imply the map should absolutely reduce? I put it to true mostly to be safe...

Thanks in advance!
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Dixie's Lua Thread

Post by silene »

Dixie wrote:Invalid WML found: replace_map: Map dimension(s) decrease but shrink is not set
Here's my code:

Code: Select all

local sow_map_file = wesnoth.dofile(string.format("~add-ons/Settlers_of_Wesnoth/maps/sow_%s.lua",game_stats.map))
wesnoth.fire("replace_map", {map=sow_map_file, expand=true, reduce=true})
In fact, the fields are called expand= and shrink=.
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Hmm, the wiki should be corrected, then. Thanks :)
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Ok, a new day, a new problem. Everything was working top notch on local multiplayer, so I took it to the next level: the multiplayer server. I tested it using my GF's laptop to better notice OOSes. And it appears I have a big OOS issue, right at the start. Woo!

So basically, the problem is that the map should be randomly generated each game. In local mulitplayer, no problem. I have a function that does this, plus a random function that uses BfW's "rand", since math.random is known to cause OOSes. Here it is:
Spoiler:
I used to have it in a prestart event, but the game wouldn't let me launch it since it is apparently not synced. No biggie: I once again trusted the wiki and placed it in a "turn 1" event (since they are supposedly synced under 1.9.2):
Spoiler:
Unfortunately, it apparently isn't, since both computers didn't get the same map. I know there seldom are MP experts, but any help is appreciated. Is something wrong in my random function? Would there be a better event for that kind of thing? ...

Thanks in advance!

Edit:

I've looked at 1.8's Competitive Gaming add-on, which randomizes map and sides, and it uses a switch and a next_scenario to randomize maps. I could use such a method to do part of the job I want, but not all... :hmm:
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Dixie's Lua Thread

Post by Anonymissimus »

As far as your random function is concerned you're doing it the way you should I think. You could use helper.rand (but's thats probably no solution to the problem).

Maybe start narrowing it down - e.g. do you get same results on both clients from the random function only.
Or maybe use wesnoth.synchronize_choice ? (It's not only for choices apparently but rather for everything that needs synchronization.)

Also, maybe I could test your settlers addon a bit with you since construction & management games are my real passion as opposed to tactical ones like wesnoth. :P

PS Nice pictures :P
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

wesnoth.synchronize choice seems interesting. My gut feeling tells me I'm gonna have to sprinkle it everywhere... Unless I include it directly in my random function, perhaps... Thanks for a good lead!

About testing: for the time being, I like being able to test with laptops side by side so I can really see how it all behaves, but when it all seems to be okay, I'll be looking forward to spreading the add-on and play games with you guys :)

Another concern I seem to have (but I am not certain): can you show dialogs (ideally with options) to players while it's not their turn? In other words: is the "side_for" key valid in MP? So far, my test results are ambiguous, and I'm not really sure how I can fix the trading system if this doesn't work... :hmm:

PS: thanks but... what pictures? :|
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Dixie's Lua Thread

Post by silene »

Dixie wrote:wesnoth.synchronize choice seems interesting. My gut feeling tells me I'm gonna have to sprinkle it everywhere... Unless I include it directly in my random function, perhaps...
Yes, include it in your random function.

Code: Select all

function sow_random(min, max)
   return wesnoth.synchronize_choice(function()
      if not max then min, max = 1, min end
      local something = ......
      return { value = something }
   end).value
end
The difficult part is now what to put into "something". The best choice would be math.random, but it is disabled. (I have also been stuck without it, when writing an AI, so I feel it was a bad idea disabling it. I will presumably reenable it.) Using [set_variable]random= would be a mistake, since it would happen on one client only, causing the other ones to desynchronize. Still, you could use it out of synchronize_choice, and just ignore the result on other clients; it is a bit inefficient from a bandwidth point of view, but hopefully it would work. Another possibility would be to write your own random generator, possibly based on [set_variable]time=stamp. Here is the version with [set_variable]random=.

Code: Select all

function sow_random(min, max)
   if not max then min, max = 1, min end
   wesnoth.fire("set_variable", { name = "LUA_random", rand = string.format("%d..%d", min, max) })
   local res = wesnoth.get_variable "LUA_random"
   wesnoth.set_variable "LUA_random"
   return wesnoth.synchronize_choice(function() return { value = res } end).value
end
Dixie wrote:can you show dialogs (ideally with options) to players while it's not their turn?
Not with options. A key concept of Wesnoth MP is that your own turn should never be waiting on the result from another computer. So everything that would cause such a wait is disabled. (In fact, persistent variables infrige on this concept, but hopefully it is not noticeable from a human point of view, and nobody abused it yet by modifying their version of Wesnoth.)
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Ok, so now the maps match, cool! I was afraid I'd have to synchronize the wesnoth.get_locations and stuff so that tables would be output in the same order, but no. There's still something I haven't got about synced variables and stuff, though.

So at start-up, I have this event:
Spoiler:
And apparently, sow_set_goal is not synced. It is used to determine how many points will be required to win the game. It is not set for other clients, since as soon as the first person scores a point (that is, at the end of his first turn), the game is ended for the non-host client. Here's the function:
Spoiler:
At first, I thought maybe helper.get_user_choice wasn't synced, so I tried this, which failed:
Spoiler:
Lastly, the mission objectives shown in the WML event are not set for the other clients, they get the default "Defeat all enemy leaders".
silene wrote:
Dixie wrote:can you show dialogs (ideally with options) to players while it's not their turn?
Not with options. A key concept of Wesnoth MP is that your own turn should never be waiting on the result from another computer. So everything that would cause such a wait is disabled. (In fact, persistent variables infrige on this concept, but hopefully it is not noticeable from a human point of view, and nobody abused it yet by modifying their version of Wesnoth.)
Dang, I'll have to think of something else, which will complicate things up... hmmm... :hmm: I may have some ideas, but suggestions are of course welcome.

Thanks again!
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Dixie's Lua Thread

Post by silene »

Dixie wrote:At first, I thought maybe helper.get_user_choice wasn't synced, so I tried this, which failed:
get_user_choice (that is, [message]) and synchronize_choice rely on the same synchronization mechanism, so I would have been surprised if combining both of them had helped. I have no good suggestion at this point, so you may want to post a full testcase.

The only thing I can think of is quite farfetched, so I doubt that's what it is. If the remote clients were to make less call to your random function, there would still be some data from synchronize_choice in the replay pending, and the [message] tag would not grab the last one (set by [message]) but one before (set by random). But it's not that easy to write WML that behaves differently on different clients, it mostly happens when depending on side controllers or things like debug.
Dixie wrote:Dang, I'll have to think of something else, which will complicate things up... hmmm... :hmm: I may have some ideas, but suggestions are of course welcome.
Can't you just have a dummy first turn when people make all their choices, and then the scenario would actually start at turn 2?
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Yeah, I think I have an alternative for the trading system. Coded most of it: basically, the trading player makes an offer, which sets a boolean to true, and side turns will be skipped/ended until another player accepts or we are back to the original player with nobody having accepted. Couldn't try it out yet, though.

I will try other things for the OOS problem. Maybe I'll try to just disable this function for now and see if every option in the add-on is OOS. I dunno, I'll see. I thought that when WML variables were setted, they were on all clients. That's kinda a bother :hmm:

I don't have much time this weekend, though. See you on tuesday! And thanks again.
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Well, I still haven't figured out all the OOS stuff, and my schedule for the next few months got loaded pretty bad. I'm not saying I won't be able to gather an hour or two a week starting in january, but it will be moving pretty slowly, if at all, until at least april/may. In that regard, if you are still interested, Anonymissimus (or anybody else), I'll distribute the current add-on on the forum if you want to try and fiddle with it. Look at the add-on's development thread for more info.

Thanks!
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Dixie's Lua Thread

Post by Anonymissimus »

I've looked a bit into it. If none else volunteers I may take it...I know Settlers of Catan only remotely however (don't have the board or pc game myself). This addon is probably a good testcase for the lua interface, especially in combination with multiplayer. ;)
However, when starting local multiplayer it welcomes me at once with a big bad C++ crash (so I can't even get to know the addon) (crash is maybe related to the theme stuff, and not neccessarily the addon's fault...)

EDIT
It's intended for wesnoth's 1.9 branch ?!
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
User avatar
Dixie
Posts: 1757
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Dixie's Lua Thread

Post by Dixie »

Huh, yeah, 1.9 stuff, cause of some new shiney Lua functions that weren't allowed in 1.8, sorry :hmm: Hope it ain't a problem!

Also, if the theme is a problem, you can just disable it altogether, it's merely a little candy on top because a lot of the standard BfW UI has little to no point for this add-on.

I was however suggesting it just like this because you had said you might be interested, you don't absolutely have to do it if you don't have the time or do not want to for any reason, I won't hold it against you, or anybody else for that matter :)
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Dixie's Lua Thread

Post by Anonymissimus »

Dixie wrote:Huh, yeah, 1.9 stuff, cause of some new shiney Lua functions that weren't allowed in 1.8, sorry :hmm: Hope it ain't a problem!
No, 1.8 would have been a problem.
(Actually I think that 1.9 is even more stable than 1.8, leaving out the whiteboard...)

I've isolated the cause of the crash, it's the image in the help message, seems some gui2 bug. So I can at least check it out.

ps. the lua is certainly not "basic" ;)

EDIT
I'm still new to multiplayer development - how would I test it appropriately ? Opening two instances of wesnoth and connecting with two different nicks to the server and playing with me then ?
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Post Reply