Dixie's Lua Thread
Moderator: Forum Moderators
Re: Dixie's Lua Thread
Thanks! I will have to look up those special fields in the manual eventually, seems like there's a lot of potential in there.
About your untested function: it seems mostyl reasonnable, but I can't figure what n is. The length of cfg? Wouldn't it update itself with the function table.remove? Or is it merely to indicate how many children named name the function has to find and delete (in the case of an array)? Also, I'm guessing return is there so the for doesn't skip an entry in its loop, since the index has changed?
About your untested function: it seems mostyl reasonnable, but I can't figure what n is. The length of cfg? Wouldn't it update itself with the function table.remove? Or is it merely to indicate how many children named name the function has to find and delete (in the case of an array)? Also, I'm guessing return is there so the for doesn't skip an entry in its loop, since the index has changed?
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
Re: Dixie's Lua Thread
Hmm... I had misunderstood, the function isn't exactly what you need. It is a function for deleting the "n"-th child with given name, while you wanted to delete all the children with given name. So the code would rather look like:Dixie wrote:About your untested function: it seems mostyl reasonnable, but I can't figure what n is.
Code: Select all
function remove_children(cfg, name)
for i = #cfg,1,-1 do
if cfg[i][1] == name then table.remove(cfg, i) end
end
end
Re: Dixie's Lua Thread
Oooh the thread was almost off to the second page, there may yet be hope for me
So I am trying to have some functions defined for specific scenarios that can place labels and scenery and stuff. I figured I could create a file my_scenario_name.lua, and access it with wesnoth.require, changing the path depending on the args with string.format. So I have this:
So the problem is that for some reason, action isn't indexed. It always returns nil, for some reason. The path -must be- correct, I've checked it like a dozen times, plus it's mostly the same I've copied from my dofiles, which work. Also, string.format works with wesnoth.require, I tried to grab the helper with it and it worked. I just really can't see what I did wrong. You could suggest just doing it some other way, without wesnoth.require, but, well, there are some other functionnalities I wanted it this fitted well. And even so, I'd still like to understand why it won't work and learn from it.
Thank
So I am trying to have some functions defined for specific scenarios that can place labels and scenery and stuff. I figured I could create a file my_scenario_name.lua, and access it with wesnoth.require, changing the path depending on the args with string.format. So I have this:
Spoiler:
Thank
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: Dixie's Lua Thread
Try placing a
return scenario
at the end of the require'd file. I made that mistake too, should be made clearer.
return scenario
at the end of the require'd file. I made that mistake too, should be made clearer.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Re: Dixie's Lua Thread
Ok, totally unrelated with anything else in this thread, except that this is about Lua. I have been playing around a bit with wesnoth.find_reach to try and help someone in WML Workshop, and testing seems to indicate that there's an error in the wiki.
LuaWML:Pathfinder#wesnoth.find_reach
Also, and I,m not 100% sure about what I'm gonna say, but the stored hexes are represented by a table with three entries, where v[1]=x, v[2]=y, and v[3]=remaining mps after movement (I think). It took me some testing to figure out what the third value was for, might be worth mentionning on the wiki?
And while I'm at it, what would you suggest I do if I wanted to find reach using the maximum movement points of the unit?
LuaWML:Pathfinder#wesnoth.find_reach
But testing and printing stuff on the console, wesnoth.find_reach obivously doesn't use the maximum movement, but rather it's current movement points. Let's say I wesnoth.find_reach a 7 moves unit, and I get over 80 hexes. If I move around with him until he has only 1 movepoint left, wesnoth.find_reach only returns 7 hexes (the one the unit is standing on and the 6 adjacent hexes). Movecost is 1 everywhere, for the sake of testing, btw.LuaWML:Pathfinder#wesnoth.find_reach wrote: Returns all the locations reachable by a unit. The unit is given either by its two coordinates or by a proxy object. Only the maximum movement points of the unit are considered, not its current ones.
Also, and I,m not 100% sure about what I'm gonna say, but the stored hexes are represented by a table with three entries, where v[1]=x, v[2]=y, and v[3]=remaining mps after movement (I think). It took me some testing to figure out what the third value was for, might be worth mentionning on the wiki?
And while I'm at it, what would you suggest I do if I wanted to find reach using the maximum movement points of the unit?
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: Dixie's Lua Thread
...wiki wrote:The locations are stored as triples in an array. The first two elements of a triple are the coordinates of a reachable tile, the third one is the number of movement points left when reaching the tile.
The "current moves are used for calculation instead of max_moves" issue is right. Probably that's the way it's supposed to be and not a bug. Comment in the commented lines to use max_moves:
Code: Select all
local Hamel = wesnoth.get_units({ id = "Hamel" })[1]
--~ local moves = Hamel.moves
--~ Hamel.moves = Hamel.max_moves
local locs = wesnoth.find_reach(Hamel)
--~ Hamel.moves = moves
for index, table in ipairs(locs) do
wml_actions.label({ x = table[1], y = table[2], text = string.format("%u: %u", index, table[3])})
end
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Re: Dixie's Lua Thread
I guess the moves thing was kinda obvious, maybe I jsut didn't take the time to think it through, or I'm getting rusty, I dunno... Well thanks!
And the bit about the third value... I guess it was just me being dumb and overlooking it: it is pretty obivous. Darn
And the bit about the third value... I guess it was just me being dumb and overlooking it: it is pretty obivous. Darn
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
Re: Dixie's Lua Thread
Indeed, it's not a bug, that's how the engine works. No idea why I wrote that in the wiki, I will fix it.Anonymissimus wrote:The "current moves are used for calculation instead of max_moves" issue is right. Probably that's the way it's supposed to be and not a bug.
Re: Dixie's Lua Thread
I was not sure wether to post this in my WML thread or in my Lua thread, but since I kinda intend on coding this in lua (unless it surprisingly is easier in WML), I'll post it here.
So, this is the single most ambitious piece of code I've attempted in my relatively short (a few months) life as a coder. Basically, in the add-on I am working on, players can place "road" units, and I need a function that would find the longest line of such units for a side and return how long it is. Because an image is worth a thousand words: In the above, imagine each hex corner and each hex side is a Wesnoth hex, and each actually drawn hex's inside is void. For the purpose of the function I need, "corner" hexes don,t matter, as the units we are interested in can only placed on the sides. On the picture, each big red dot represents one such unit, and the smaller dots represent the possible paths. In this case, the green-dot path would be the longest, with 6 units.
This case is not protrayed in the above picture, but we also have to be wary of units going all around an hex (forming a loop), so as not to count any road twice for a single path/freeze the engine in a loop.
So far, I have this code, but I am stuck in two places (see comments), so I can't really test if I did something wrong. It can also probably be optimized quite a bit:
Thanks a lot for your insight in advance!
So, this is the single most ambitious piece of code I've attempted in my relatively short (a few months) life as a coder. Basically, in the add-on I am working on, players can place "road" units, and I need a function that would find the longest line of such units for a side and return how long it is. Because an image is worth a thousand words: In the above, imagine each hex corner and each hex side is a Wesnoth hex, and each actually drawn hex's inside is void. For the purpose of the function I need, "corner" hexes don,t matter, as the units we are interested in can only placed on the sides. On the picture, each big red dot represents one such unit, and the smaller dots represent the possible paths. In this case, the green-dot path would be the longest, with 6 units.
This case is not protrayed in the above picture, but we also have to be wary of units going all around an hex (forming a loop), so as not to count any road twice for a single path/freeze the engine in a loop.
So far, I have this code, but I am stuck in two places (see comments), so I can't really test if I did something wrong. It can also probably be optimized quite a bit:
Spoiler:
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
Re: Dixie's Lua Thread
There are a few things I don't understand. First, I don't understand the location filter you are using. How does it relate to what you draw on your picture? Second, I don't understand the value returned by sow_getting_path_length; it is some kind of tree. Shouldn't it be just an integer (the length of the longest path)?Dixie wrote:So far, I have this code, but I am stuck in two places (see comments), so I can't really test if I did something wrong. It can also probably be optimized quite a bit:
Anyway, you can make the function much shorter. (It returns an integer here, but it still uses the same filter as yours.)
Code: Select all
function sow_getting_path_length(player, start, seen_ids, length)
local actual_id = start.id
seen_ids = string.format("%s,%s", actual_id, seen_ids)
local actual_length = 1 + length
local best_length = actual_length
local next_road = wesnoth.get_units({type="sow_roadn,sow_roadne,sow_roadnw", side=player, {"filter_location",{ {"and"{radius=2, {"filter",{id=actual_id}}, {"not",{ {"filter",{id=seen_ids}} }} }} }} })
for i,v in ipairs(next_road) do
best_length = math.max(best_length, sow_getting_path_length(player, v, seen_ids, actual_length))
end
return best_length
end
Re: Dixie's Lua Thread
Yeah, I figured there were some wrong stuff about my code overnight. Like the tree thing, for instance.
I've tried my hand at a better image, by the way: Here is the code I have at present, largely based on your try:
I have played around with it a bit, and will have to take a better look at it, but at the time being, sow_longest_check always returns 1. I have some papers to write for university, so I can't put much more time on this right now, but maybe tonight. I was also not sure whether your code would consider the pale blue line on the picture, but I didn't get 'round to testing it just yet. The weird filter_location was incomplete and was aimed at seeing if a road was a start (only "adjacent" to none or only one other road), but I figured it might as well start counting from any road in the middle: it shouldn't be the longest in that case, and shouldn't matter.
Thanks again in advance!
I've tried my hand at a better image, by the way: Here is the code I have at present, largely based on your try:
Spoiler:
Thanks again in advance!
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
Re: Dixie's Lua Thread
It does consider it. But I now understand from your picture that it is not what you wanted. So, if I get it correctly, you want all the units that are from a road type at distance 2 of the current tile (but not the current tile) but not at a distance 2 of any previously visited tile. So the filter should be (with the usual "T = helper.set_wml_tag_metatable {}"):Dixie wrote:I was also not sure whether your code would consider the pale blue line on the picture, but I didn't get 'round to testing it just yet.
Code: Select all
type = "sow_roadn,sow_roadne,sow_roadnw",
side = player,
T.not { id = start.id },
T.filter_location {
T.and { radius = 2, T.filter { id = start.id } },
seen_ids and (T.not { radius = 2, T.filter { id = seen_ids } })
}
Re: Dixie's Lua Thread
The tag metatable thing scared me a bit at first, but I went ahead and read the wiki, and it doesn't look so bad. It's strictly a syntactic shortcut, where T.filter {} replaces {"filter",{}}, right? No other side effects?
By the way, your code gave me this error:
The faulty line being : T.not { id = start.id }
By the way, I thought some more about the filter I'd need. Here's another image: Your first alternative would have allowed both the green and the yellow paths. The last code you suggested would have allowed neither. Ideally, I'd like to allow the green but not the yellow. So I figured seen_ids needn't be in the radius 2 thing, but only in a simple "not" in the basic filter. And I also figured I'd create a temporary "ignore these ids" variable for other options at intersections, but that wouldn't disqualify the ignored roads for later use, if they somehow reconnect at some point.
Anyway, I currently have the following code, which looks OK to me but is subject to the same error as yours (shown above) because I used the metatable thing. I'd only need to correct this, and maybe your aproval on the final code (although if I could fix the error, I'd test and approve myself):
Thanks again!
By the way, your code gave me this error:
Code: Select all
20101205 19:24:29 error scripting/lua: ...ort/Wesnoth_1.9/data/add-ons/Settlers_of_Wesnoth/utils/sow_lua-utils.lua:1166: '<name>' expected near 'not'
stack traceback:
[C]: in function 'dofile'
[string "..."]:4: in function 'bytecode'
... 192.app/Contents/Resources/.//data/lua/wml-tags.lua:206: in function 'cmd'
... 192.app/Contents/Resources/.//data/lua/wml-tags.lua:220: in function <... 192.app/Contents/Resources/.//data/lua/wml-tags.lua:213>
By the way, I thought some more about the filter I'd need. Here's another image: Your first alternative would have allowed both the green and the yellow paths. The last code you suggested would have allowed neither. Ideally, I'd like to allow the green but not the yellow. So I figured seen_ids needn't be in the radius 2 thing, but only in a simple "not" in the basic filter. And I also figured I'd create a temporary "ignore these ids" variable for other options at intersections, but that wouldn't disqualify the ignored roads for later use, if they somehow reconnect at some point.
Anyway, I currently have the following code, which looks OK to me but is subject to the same error as yours (shown above) because I used the metatable thing. I'd only need to correct this, and maybe your aproval on the final code (although if I could fix the error, I'd test and approve myself):
Spoiler:
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth
Re: Dixie's Lua Thread
That particular line won't work because 'not' is a reserved word. As in "if not some_boolean then...". Change the "T.not" to "T['not']" and you should be fine.Dixie wrote: The faulty line being : T.not { id = start.id }
Re: Dixie's Lua Thread
By the way, it just occured to me: helper.set_wml_tag_metatable {} really is some kinda shortcut for wesnoth.fire, right or wrong? Or maybe it's only useful when used within filters or something and won't actually "fire" an action tag?
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth
Current projects: Internet meme Era, The Settlers of Wesnoth