LightFighter's Lua Lab

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

Moderators: Forum Moderators, Developers

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

LightFighter's Lua Lab

Post by bumbadadabum » October 23rd, 2010, 10:42 am

I've experimented a bit with lua and came up with this macro/tag:

Code: Select all

    [lua]
        code = <<
            local function effects(f)
		local filtered = wesnoth.get_units { x,y = f.x,y or id = f.id or type = f.type or error("~wml:[imagemod] expects a filter.", 0) }
		wesnoth.add_modification(filtered, "object", { { "effect", { apply_to = "image_mod", replace = "f.image" } } { "effect", { apply_to = "ellipse", ellipse = "f.ellipse" } } } )
	    end
            wesnoth.register_wml_action("imagemod", effects)
        >>
    [/lua]
What happens in-game, is that it keeps showing me this error:

Code: Select all

[string "..."]:3: '}' expected near '='
Can somebody tell me what I'm doing wrong?
Last edited by bumbadadabum on November 25th, 2010, 5:18 pm, edited 1 time in total.

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

Re: Lua question

Post by Luther » October 23rd, 2010, 4:13 pm

The 'x,y = f.x,y' is illegal. You would have to put in each element separately. I can post an example later.

EDIT:

Note that it's easier to put this all into a separate Lua file. It's a pain to edit Lua within WML.

I do not have 1.9 installed, so this code is very untested. :whistle: I'm sure there are errors.

Code: Select all

--Make the code a bit shorter
H = wesnoth.require('lua/helper.lua')
T = H.set_wml_tag_metatable{}

local function effects(f)

  --Use a standard unit filter provided by the user. You might be able to pass
  --f directly to get_units, but I would require a [filter] tag to be safe. You
  --don't need an error message because a nil filter simply catches all units.
  local filtered = wesnoth.get_units(H.get_child(f, 'filter'))

  --add_modification can only work on one unit at a time.
  for i, unit in ipairs(filtered) do
    wesnoth.add_modification(
      unit,
      'object',
      {
        --Take advantage of set_wml_tag_metatable, which was called at the top.
        T.effect{
          apply_to = "image_mod",
          --You probably want a string provided by the user, not a string
          --literal.
          replace = f.image
        },
        T.effect{
          apply_to = "ellipse",
          ellipse = f.ellipse
        }
      }
    )
  end
end

wesnoth.register_wml_action("imagemod", effects)
Let us know how it works.

EDITED to add another comment.
EDITED to fix a syntax error.
Last edited by Luther on October 23rd, 2010, 5:11 pm, edited 2 times in total.

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: Lua question

Post by bumbadadabum » October 23rd, 2010, 5:02 pm

That also doesn't work, same sort of error.

Code: Select all

[string "..."]:7: '}' expected near 'T'

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

Re: Lua question

Post by Luther » October 23rd, 2010, 5:13 pm

I forgot to put a comma between the two T tables. I've edited the code in my post. Try it again.

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: Lua question

Post by bumbadadabum » October 23rd, 2010, 8:51 pm

Thanks, it worked!

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: Lua question

Post by bumbadadabum » November 25th, 2010, 3:39 pm

Another quick question: Is there a way to make an #ifdef in lua?

Anonymissimus
Developer
Posts: 2458
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Lua question

Post by Anonymissimus » November 25th, 2010, 4:09 pm

No. Lua isn't preprocessed.

EDIT
Well, if you "embed" it into wml it is, or can be, but it is very questionable whether that makes sense at all. You need to match the syntaxes of both wml and lua and must write lua as if it was wml which is pointless.
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
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: LightFighter's Lua Lab

Post by bumbadadabum » November 25th, 2010, 5:20 pm

Finished the (ugly) quantity variable stacker:

Code: Select all

function quantity(cfg)
	if easy
	then
		wesnoth.set_variable("quantity", cfg.easy)
	end
	if medium
	then
		wesnoth.set_variable("quantity", cfg.medium)
	end
	if hard
	then
		wesnoth.set_variable("quantity", cfg.hard)
	end
end
        wesnoth.register_wml_action("quantity", quantity)

Code: Select all

	[event]
		name=preload
		first_time_only=no
#workarounds here
		#ifdef EASY
			{VARIABLE easy yes}
		#endif
		#ifdef MEDIUM
			{VARIABLE medium yes}
		#endif
		#ifdef HARD
			{VARIABLE hard yes}
		#endif
		{WML_TAGS}
	[/event]
It is really simple, but it should work.

AI
Developer
Posts: 2394
Joined: January 31st, 2008, 8:38 pm

Re: LightFighter's Lua Lab

Post by AI » November 26th, 2010, 2:17 pm

That only allows you three difficulty levels.
Northern Rebirth, for one, uses 4. It needs its own macros because of that, but now it can't use this tag either...

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: LightFighter's Lua Lab

Post by bumbadadabum » November 26th, 2010, 2:44 pm

AI wrote:That only allows you three difficulty levels.
Northern Rebirth, for one, uses 4. It needs its own macros because of that, but now it can't use this tag either...

Code: Select all

function quantity(cfg)
   if easy
   then
      wesnoth.set_variable("quantity", cfg.easy)
   end
   if medium
   then
      wesnoth.set_variable("quantity", cfg.medium)
   end
   if hard
   then
      wesnoth.set_variable("quantity", cfg.hard)
   end
   if nightmare
   then
      wesnoth.set_variable("quantity", cfg.nightmare)
   end
end
        wesnoth.register_wml_action("quantity", quantity)

Code: Select all

   [event]
      name=preload
      first_time_only=no
#workarounds here
      #ifdef EASY
         {VARIABLE easy yes}
      #endif
      #ifdef MEDIUM
         {VARIABLE medium yes}
      #endif
      #ifdef HARD
         {VARIABLE hard yes}
      #endif
      #ifdef NIGHTMARE
         {VARIABLE nightmare yes}
      #endif
      {WML_TAGS}
   [/event]
Usage:

Code: Select all

[quantity]
easy=10
medium=20
hard=30
[/quantity]
[gold]
 side=1
 amount=$quantity
[/gold]

AI
Developer
Posts: 2394
Joined: January 31st, 2008, 8:38 pm

Re: LightFighter's Lua Lab

Post by AI » November 26th, 2010, 2:58 pm

Yes, but what if someone wants to use 5 difficulties, or different difficulty DEFINEs? Should they have to replace the tag or craft a macro to work around its shortcomings?

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: LightFighter's Lua Lab

Post by bumbadadabum » November 26th, 2010, 3:22 pm

AI wrote:Yes, but what if someone wants to use 5 difficulties, or different difficulty DEFINEs? Should they have to replace the tag or craft a macro to work around its shortcomings?
Yes, they have to, but this system will allow UMC designers to make another type of difficulty system, like making formulas.

User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: LightFighter's Lua Lab

Post by bigkahuna » November 26th, 2010, 4:26 pm

IIRC, Invasion from the Unknown uses a macro of this sort without any Lua.

Code: Select all

#ifdef EASY
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_EASY}#enddef
#endif
#ifdef NORMAL
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_NORMAL}#enddef
#endif
#ifdef HARD
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD
{_ON_HARD}#enddef
#endif
Theoretically you could add:

#ifdef NIGHTMARE
#define DIFF _ON_EASY _ON_NORMAL _ON_HARD _ON_NIGHTMARE
{_ON_NIGHTMARE}

and alter the others accordingly. Usage:

{VARIABLE monkeys ({DIFF 12 14 16})}
or
[event]
name=turn {DIFF 2 4 6}

etc
Check out my campaign Sweet Revenge!
Join the new R2D forum!

silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: LightFighter's Lua Lab

Post by silene » November 26th, 2010, 5:41 pm

LightFighter wrote:It is really simple, but it should work.
You can make it even simpler.

Code: Select all

#ifdef EASY
{VARIABLE difficulty easy}
#endif
...

wesnoth.register_wml_action("quantity", function(cfg)
  wesnoth.set_variable("quantity", cfg[wesnoth.get_variable "difficulty"])
end)

User avatar
bumbadadabum
Developer
Posts: 1005
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands
Contact:

Re: LightFighter's Lua Lab

Post by bumbadadabum » November 26th, 2010, 8:35 pm

New code (thank you silene!)

Code: Select all

wesnoth.register_wml_action("quantity", function(cfg)
  wesnoth.set_variable("cfg.save_in", cfg[wesnoth.get_variable "difficulty"])
end)
Usage:

Code: Select all

[quantity]
save_in=foo
easy=b
medium=a
hard=r
[/quantity]

Post Reply