Lua Questions

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

Moderators: Forum Moderators, Developers

User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Lua Questions

Post by Crendgrim » January 7th, 2012, 10:29 am

8680: Just out of curiosity, is there any reason you prefer semicolons over commas?
UMC Story Images — Story images for your campaign!

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » January 7th, 2012, 6:00 pm

@Crendgrim: It's more that I prefer terminators over separators, and commas used as terminators just don't look right.

@battlestar: Note that you could use commas instead of all the semicolons except for the last one, and that you can omit a semicolon/comma when the next non-whitespace character is }.

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » February 29th, 2012, 6:26 pm

Thanks 8680.

Generally speaking, is a mixture of Lua and WML better than pure WML? I'm considering a piece of code that consist of a WML supra-structure with a few WML macros and a lot of macros containing Lua. Is that a good idea?
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » February 29th, 2012, 6:44 pm

Yes, but a better idea would be to replace the WML macros containing Lua with WML tags implemented in Lua. See the Lua Pack for how to do this.

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » February 29th, 2012, 6:55 pm

Sounds good, thanks. This way I won't have to re-do as much WML into Lua (big turn-off when thinking about turning thousands of lines of WML into Lua).
Big thanks to 8680 for agreeing to help with coding for a campaign. Help from anyone else is of course welcomed and credit will be given as credit is due.

Here're some choices (I already have written the WML for the menu interface that fires the macros that correspond to each attack):
-
electric mantle:
When activated, every adjacent square with an enemy has a 50% chance of being struck with lightning, receiving 6 neutral arcane damage. A single adjacent enemy to the victim is struck with 4 neutral arcane lightning. Then a single adjacent enemy to the second victim is struck with 2 neutral arcane lightning. (A chain lightning)
-
disarm:
An attack on a single enemy, disables the enemy's physical attack capabilities for 2 turns (return to normal after 2 turns, and multiple units can be affected at the same time, would love to see how this is done with Lua).
For this attack, I have written an WML macro that allows the player to choose a single enemy target within radius of 2:

Code: Select all

{FINAL_SPELL_TARGET_ENEMY {CHAR_ID} 2 spell_target_x spell_target_y}
Where spell_target_x and spell_target_y are the variables that return the coordinates.
Please don't hesitate if any questions arise.

Again, your help is much appreciated.
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » June 21st, 2012, 2:35 pm

Hi,

8680 wrote this bit for me:

Code: Select all

function requiredXP(currentLevel)
    return currentLevel == 1 and 50 or
        math.floor(requiredXP(currentLevel - 1) + 50 * (1.6 - math.log10(currentLevel)) + 0.5)
end
but I couldn't figure out how to use it even after looking at the luaWML wiki. Trying to use the value calculated by requiredXP(currentLevel) in [set_variable] or [modify_unit], to set the experience to level values.

Thanks
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » June 21st, 2012, 4:37 pm

You’d need to call it from Lua. Here’s an (untested) WML tag interface.
Spoiler:
I’ll keep doing small things like this, but I don’t think I’ll be fully participating in your project; it’s too far advanced; I think it might be easier for me to rewrite everything from scratch than jump in now.

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » June 24th, 2012, 8:21 pm

Thanks again, this is the end product, and it's tested and working. ^_^
Spoiler:
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » June 24th, 2012, 8:42 pm

Put the [lua] tag in the campaign #ifdef, it should be more efficient that way.

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » June 26th, 2012, 6:32 am

Thanks 8680
Spoiler:
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » June 26th, 2012, 6:02 pm

You changed the XP required to advance from level 1 to 115?

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » June 26th, 2012, 6:38 pm

Not really, turns out currentLevel that's fed into the function was actually the value of previous level, so now it's become:

Lvl 1: 50 (defined by unit file)
Lvl 2: 115 (return currentLevel == 1 and 115)
Lvl 3: 180 (first actual calculation)
Lvl 4: 236 (etc. by calculation)

I was having a little trouble with modifying the unit level before the level was fed into the function, and I figured it was very close to original values anyway... so I started working on something else.

Code: Select all

# TODO: consider combining with SCC_CHOOSE_UPG macro below
#define SCC_LEVEL_UP
[event]
	name=post advance
	first_time_only=no
	[filter] # right after a unit with status isHero=yes advances
		[filter_wml]
			[status]
				isHero=yes
			[/status]
		[/filter_wml]
	[/filter]
	[store_unit] # stored hero in question
		[filter]
			x,y=$x1,$y1
		[/filter]
		variable=upgrading_hero
	[/store_unit]
	[store_required_xp]
		current_level=$upgrading_hero.level
		variable=reqxp
	[/store_required_xp]
	[set_variable]
		name=new_level
		value=$upgrading_hero.level
	[/set_variable]
	[set_variable]
		name=new_level
		add=1
	[/set_variable]
	
	[modify_unit]
		[filter]
			id=$upgrading_hero.id
		[/filter]
		max_experience=$reqxp
		level=$new_level
	[/modify_unit]
	{CLEAR_VARIABLE upgrading_hero}
	{CLEAR_VARIABLE reqxp}
	{CLEAR_VARIABLE new_level}
[/event]
#enddef
I know I probably could've fixed it by moving two of those [set_variable] tags and split the [modify_unit] tag... >_>
LUA: Llama Under Apprenticeship
Hell faction: completed

User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 » June 26th, 2012, 9:46 pm

I suppose I wasn’t clear enough originally, apologies. requiredXP(currentLevel) was supposed to return the XP required for a PC currently at level currentLevel to advance to the next level. Here, I’ll extend the WML interface:
Spoiler:

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar » June 26th, 2012, 10:18 pm

Thanks, I like your codes better. I'll implement them and consider level up "experience" determination codes done after testing. It's kind of a separate entity from level up "upgrades", which improves unit stats. The only thing shared between them is timing (both have event name=post advance).


For now, the main thing to code for is this:
"TODO: randomly choose 3 nonrepeating upgrades (with value >0 and <= max_upg_~~) from list of upgrades in the unit's [variables] tag and feed the upgrade names into variables: upg_choice_1, upg_choice_2, and upg_choice_3."

It's the main step for the level up upgrades. I'll leave that to you for whenever you're able to do it, thanks!
LUA: Llama Under Apprenticeship
Hell faction: completed

Post Reply