monochromatic's lua thread

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

Moderator: Forum Moderators

monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: elvish_sovereign gets back to it

Post by monochromatic »

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.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: elvish_sovereign gets back to it

Post by Elvish_Hunter »

Nice work, e_s ^_^ I see two things that can be improved here:
elvish_sovereign wrote:

Code: Select all

local number = cfg.number or helper.wml_error("Missing required 'value' attribute in [limit_recruits]")
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 like

Code: Select all

helper.wml_error("Missing or wrong required 'value' attribute in [limit_recruits]")
Also,
elvish_sovereign wrote:

Code: Select all

local cumulative = cfg.cumulative or "false"
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 "").
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
elvish_sovereign wrote:

Code: Select all

elseif cumulative == "false" then
with

Code: Select all

elseif not cumulative then
Much simpler, no? :wink:
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)
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: elvish_sovereign gets back to it

Post by monochromatic »

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. :geek:
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: elvish_sovereign gets back to it

Post by monochromatic »

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.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: elvish_sovereign gets back to it

Post by Anonymissimus »

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 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
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: monochromatic's lua thread

Post by monochromatic »

Okay, new question: is there a way to divide two numbers and guarantee it's not a floating point value?
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.
User avatar
melinath
Posts: 1298
Joined: May 20th, 2009, 7:42 am

Re: monochromatic's lua thread

Post by melinath »

Something like math.floor?
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: monochromatic's lua thread

Post by monochromatic »

Totally didn't know that! /me wonders where 'floor' comes from..
User avatar
melinath
Posts: 1298
Joined: May 20th, 2009, 7:42 am

Re: monochromatic's lua thread

Post by melinath »

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.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: monochromatic's lua thread

Post by Elvish_Hunter »

monochromatic wrote:For example: x = 9 / 4 --x = 2.25
To complete the answer with a concrete example, here there is what happens to the Lua prompt with and without rounding functions:

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
As you can see, floor rounds down to the nearest integer, while ceil rounds up to the nearest integer.
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)
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: monochromatic's lua thread

Post by monochromatic »

Awesome! Okay new question, I have this line of code which is giving me errors:

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*" }}}}}
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.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: monochromatic's lua thread

Post by Elvish_Hunter »

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):

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
and it returned me this in the console:

Code: Select all

Yeti-55
Wose-57
Wose-59
Elvish Archer-63
Elvish Archer-65
Wose-67
Dwarvish Guardsman-69
clear sign that units were acquired correctly. By the way, I'm on 1.9.5+svn, 49123M.
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)
monochromatic
Posts: 1549
Joined: June 18th, 2009, 1:45 am

Re: monochromatic's lua thread

Post by monochromatic »

Okay then the problem is else-ware. Thanks!
EDIT: Fixed it! It was something wrong with my for loop.
Post Reply