LightFighter's Lua Lab

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

Moderator: Forum Moderators

User avatar
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

LightFighter's Lua Lab

Post by Astoria »

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 Astoria on November 25th, 2010, 5:18 pm, edited 1 time in total.
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Lua question

Post by Luther »

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
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: Lua question

Post by Astoria »

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

Code: Select all

[string "..."]:7: '}' expected near 'T'
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Lua question

Post by Luther »

I forgot to put a comma between the two T tables. I've edited the code in my post. Try it again.
User avatar
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: Lua question

Post by Astoria »

Thanks, it worked!
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
User avatar
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: Lua question

Post by Astoria »

Another quick question: Is there a way to make an #ifdef in lua?
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Lua question

Post by Anonymissimus »

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
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: LightFighter's Lua Lab

Post by Astoria »

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.
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: LightFighter's Lua Lab

Post by AI »

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
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: LightFighter's Lua Lab

Post by Astoria »

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]
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
AI
Developer
Posts: 2396
Joined: January 31st, 2008, 8:38 pm

Re: LightFighter's Lua Lab

Post by AI »

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
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: LightFighter's Lua Lab

Post by Astoria »

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.
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
User avatar
bigkahuna
Posts: 657
Joined: September 11th, 2010, 6:24 pm
Location: In your mind.

Re: LightFighter's Lua Lab

Post by bigkahuna »

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 »

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
Astoria
Inactive Developer
Posts: 1007
Joined: March 20th, 2008, 5:54 pm
Location: Netherlands

Re: LightFighter's Lua Lab

Post by Astoria »

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]
Formerly known as the creator of Era of Chaos and maintainer of The Aragwaithi and the Era of Myths.
Post Reply