monochromatic's lua thread
Moderator: Forum Moderators
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: elvish_sovereign gets back to it
Finally, some progress! School has been choking me a part couple weeks, but nonetheless I've managed to squeeze some time into working on this. It's a replacement for LIMIT_RECRUITS and LIMIT_CONTEMPORANEOUS_RECRUITS, which I think is clunky. Here you can put it into a nice, clean tag and it actually works!
Code: Select all
-- Replacement for the LIMIT_RECRUIT and LIMIT_CONTEMPORANEOUS_RECRUITS macros
--
-- Use in WML:
-- [event]
-- name = recruit,side turn
--
-- [limit_recruits]
-- # SUF, don't use a [filter] tag
-- value = # number of limited recruits
-- cumulative = # whether the total units should be cumulative or not
-- # LIMIT_RECRUITS is cumulative, LIMIT_CONTEMPORANEOUS_RECRUITS is not
-- # default is no
-- [/limit_recruits]
-- [/event]
helper = wesnoth.require "lua/helper.lua"
wml_actions = wesnoth.wml_actions
function wml_actions.limit_recruits(cfg)
local value = (tonumber(cfg.value)) or helper.wml_error("Missing or wrong required 'value' attribute in [limit_recruits]")
local cumulative = cfg.cumulative
local filter = wesnoth.get_units(cfg)
if #filter >= value then
for index,unit in ipairs(filter) do
wml_actions.disallow_recruit { side = unit.side , type = unit.type }
end
elseif not cumulative then
if #filter < value then
for index,unit in ipairs(filter) do
wml_actions.allow_recruit { side = unit.side , type = unit.type }
end
end
end
end
Last edited by monochromatic on March 6th, 2011, 3:08 pm, edited 3 times in total.
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: elvish_sovereign gets back to it
Nice work, e_s I see two things that can be improved here:Also,
By the way, your "false" between quotes is considered a string value by Lua, and strings are always true; special values like true, false and nil must not have quotes. At the end, removing or "false" will allow you to replaceMuch simpler, no?
I usually tend to place here a tonumber() ( tonumber(cfg.number) ). Why? Because if someone attempts to feed a string value (like number=fifteen), tonumber returns nil (because it fails to convert it to a numerical value) and allows you to fire an error message likeelvish_sovereign wrote:Code: Select all
local number = cfg.number or helper.wml_error("Missing required 'value' attribute in [limit_recruits]")
Code: Select all
helper.wml_error("Missing or wrong required 'value' attribute in [limit_recruits]")
There is usually no need to declare it as false (unless you have a good reason to do so, like doing a different action if cumulative == nil). If cfg.cumulative isn't supplied by user, it will be automatically nil; and both nil and false are considered false by Lua, everything else is true (including 0 and "").elvish_sovereign wrote:Code: Select all
local cumulative = cfg.cumulative or "false"
By the way, your "false" between quotes is considered a string value by Lua, and strings are always true; special values like true, false and nil must not have quotes. At the end, removing or "false" will allow you to replace
withelvish_sovereign wrote:Code: Select all
elseif cumulative == "false" then
Code: Select all
elseif not cumulative then
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: elvish_sovereign gets back to it
Awesome! Edited my post above with also a minor flavor change. Interesting to know that Lua reads "false" as a string. nil might be easier in the future.
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: elvish_sovereign gets back to it
Okay, question here. I've seen this mentioned a number of times and I've read it on the wiki. What does a "proxy" unit/array refer to exactly? Thanks.
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: elvish_sovereign gets back to it
It's an "accessor" to a C++ variable or object. In the units section in LuaWMl I had written about it.
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
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: monochromatic's lua thread
Okay, new question: is there a way to divide two numbers and guarantee it's not a floating point value?
For example:
Is there a way to make it so something like that will be 2 rather than 2.25 or whatever? It helps for hitpoints/moves etc. Also if there isn't, is there a workaround?
I have a feeling I'm missing something soo obvious.
For example:
x = 9 / 4 --x = 2.25
Is there a way to make it so something like that will be 2 rather than 2.25 or whatever? It helps for hitpoints/moves etc. Also if there isn't, is there a workaround?
I have a feeling I'm missing something soo obvious.
Re: monochromatic's lua thread
Something like math.floor?
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: monochromatic's lua thread
Totally didn't know that! /me wonders where 'floor' comes from..
Re: monochromatic's lua thread
Imagine a building with multiple stories. Each story represents an integer value - or more precisely, each story represents the space between two integers. Then for each story, the floor is one integer while the ceiling is another. Hence math.floor and math.ceil.
Last edited by melinath on April 6th, 2011, 1:49 pm, edited 1 time in total.
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: monochromatic's lua thread
To complete the answer with a concrete example, here there is what happens to the Lua prompt with and without rounding functions:monochromatic wrote:For example: x = 9 / 4 --x = 2.25
Code: Select all
linux@linux-desktop:~$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print ( 9 / 4 )
2.25
> print ( math.floor ( 9 / 4 ) )
2
> print ( math.ceil ( 9 / 4 ) )
3
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: monochromatic's lua thread
Awesome! Okay new question, I have this line of code which is giving me errors:
So the engine is giving me some errors about it being a table, not a WML table (or the other way around). How do I store the unit properly? I've seen it work without subtags.
Code: Select all
local frostbite_units = wesnoth.get_units { side = side_number , { "not" , { id = "Buggles" }} , { "filter_location" , { terrain = "A*,*^F*a,Ms,Ha" , { "not" , { terrain = "*^V*" }}}}}
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: monochromatic's lua thread
Hmm... perhaps the problem is elsewhere, as I copied your line in this tag (I changed the terrain code only, because the map that I used has no snow):and it returned me this in the console:clear sign that units were acquired correctly. By the way, I'm on 1.9.5+svn, 49123M.
Code: Select all
function wml_actions.frost(cfg)
local frostbite_units = wesnoth.get_units { side = side_number , { "not" , { id = "Buggles" }} , { "filter_location" , { terrain = "G*" , { "not" , { terrain = "*^V*" }}}}}
for index, unit in ipairs ( frostbite_units ) do
print ( unit.id )
end
end
Code: Select all
Yeti-55
Wose-57
Wose-59
Elvish Archer-63
Elvish Archer-65
Wose-67
Dwarvish Guardsman-69
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
-
- Posts: 1549
- Joined: June 18th, 2009, 1:45 am
Re: monochromatic's lua thread
Okay then the problem is else-ware. Thanks!
EDIT: Fixed it! It was something wrong with my for loop.
EDIT: Fixed it! It was something wrong with my for loop.