enclave's WML Macros questions Thread

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

beetlenaut wrote:You'll just have to do some extra checking. If it numerically_equals = 0, but doesn't also equals = "0", then you know they entered some non-numbers. Otherwise, they entered something valid.
Thanks beetlenaut, your solution seems more simple, and it looks like it would work. It's the first time in a long time I actually had to check whether a variable is a string or a number.. so rare it happens in WML.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

gfgtdf wrote:So it's probably better to use tonumber(value) ~= nil to check whether am attribute is a valid number.
For those who are wondering about this one I will try to write the way it works, but example is not tested and may fail to work due to mistakes or mistypes:
So as per my example.. you have a string that you need to check whether it is a number/integer/float or a letter/sentence/string

Code: Select all

[text_input]
   label="Enter number here (letters will result in error):"
   variable=input_text_result
[/text_input]
The variable input_text_result needs to be checked..
So you do:

Code: Select all

[lua]
code = <<
if tonumber(wesnoth.get_variable("input_text_result")) ~= nil then wesnoth.set_variable("variable_is_numeric","true")
else
wesnoth.set_variable("variable_is_numeric","false")
end
>>
[/lua]
[if]
   [variable]
      name=variable_is_numeric
      boolean_equals=true
   [/variable]
   [then]
            ## your code if user entered the number
   [/then]
   [else] 
              ## error message if user entered anything except the number..
   [/else]
[/if]
You could not include this lua else code if you make sure that you clear the variable prior to lua check, or it may give you wrong results from previous checks

Code: Select all

else
wesnoth.set_variable("variable_is_numeric","false")
so its either this or you would have to do {CLEAR_VARIABLE variable_is_numeric} before lua check or after your WML check [/else][/if]

I wanted to make it all in WML, because I prefer not to mix things with lua where possible.. but not everything can be done in WML sometimes..
User avatar
Pentarctagon
Project Manager
Posts: 5528
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: enclave's WML Macros questions Thread

Post by Pentarctagon »

If you're using 1.13, or once 1.14 comes out, you can also use lua to make your own conditional WML tags.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

hi, i would still like to know, if anyone has ideas, how to make sounds heard only to specific players..
for example player shops something and runs out of gold and there is "out of gold" sound or something similar.. how do you make it work only for player 5 lets say.. ? do you use sound source and then just remove it straight away or what?
Thanks..
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: enclave's WML Macros questions Thread

Post by Ravana »

With 1.12 you can check if side 5 controller is human. But might be that this way only works when used in Lua context.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: enclave's WML Macros questions Thread

Post by zookeeper »

enclave wrote:for example player shops something and runs out of gold and there is "out of gold" sound or something similar.. how do you make it work only for player 5 lets say.. ?
In that particular case you probably have a [message] for that anyway, and if so then you can just use sound,side_for=outofgold.ogg,5.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

zookeeper wrote:
enclave wrote:for example player shops something and runs out of gold and there is "out of gold" sound or something similar.. how do you make it work only for player 5 lets say.. ?
In that particular case you probably have a [message] for that anyway, and if so then you can just use sound,side_for=outofgold.ogg,5.
Thanks very much zookeeper!!!
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

Hi, anyone knows exactly how the swarm number of attacks is calculated? (formula)
knowing the minimal number of attacks, maximum number of attacks and health (I would also need to know if health percentage is floored or ceiled or other, so ideally I would need to know the formula exactly or which folder of core I could possibly look for it to try to find it?)
I can guess the formula but I would like to be 100% correct and match the original formula, where hardest part is the health percentage floored or ceiled or none, and then if the number of attacks is floored or ceiled... otherwise I would need to do 100ds of tests to see what is happening.. and spend lot of time.
Thanks!
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: enclave's WML Macros questions Thread

Post by Ravana »

https://github.com/wesnoth/wesnoth/blob ... attack.hpp

Code: Select all

/** Calculates the number of blows resulting from swarm. */
inline unsigned swarm_blows(unsigned min_blows, unsigned max_blows, unsigned hp, unsigned max_hp)
{
	return hp >= max_hp
		? max_blows
		: max_blows < min_blows
			? min_blows - (min_blows - max_blows) * hp / max_hp
			: min_blows + (max_blows - min_blows) * hp / max_hp;
}
While you might still want to test it, you can test that function separate from wesnoth.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

Ravana wrote: ? max_blows
: max_blows < min_blows
? min_blows - (min_blows - max_blows) * hp / max_hp
: min_blows + (max_blows - min_blows) * hp / max_hp;
Hi, is it c++ or something?
What the "?" and ":" means there? sorry =] I could try googling, but I dont even know which type of C it is :D
Why 2 lines are similar to each other :D?
User avatar
James_The_Invisible
Posts: 534
Joined: October 28th, 2012, 1:58 pm
Location: Somewhere in the Northlands, fighting dark forces
Contact:

Re: enclave's WML Macros questions Thread

Post by James_The_Invisible »

Should be C++. I know like nothing about it but I remember similar syntax from PHP. I read it as:
if hp >= max_hp then return max_blows
else if max_blows < min_blows then return min_blows - (min_blows - max_blows) * hp / max_hp
else return min_blows + (max_blows - min_blows) * hp / max_hp.
User avatar
Pentarctagon
Project Manager
Posts: 5528
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: enclave's WML Macros questions Thread

Post by Pentarctagon »

Whoever wrote the code just really really wanted to get it all in one statement. More legibly, it reads:

Code: Select all

if(hp >= max_hp)
{
    return max_blows;
}
else
{
    if(max_blows < min_blows)
    {
        return min_blows - (min_blows - max_blows) * hp / max_hp;
    }
    else
    {
        return min_blows + (max_blows - min_blows) * hp / max_hp;
    }
}
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

oh wow.. Thank you all 3 of you! You helped a lot!
But I still have problem, Imaginary situation:
i have swarm attack where 3 is min blows, 10 is maximum blows..
The unit has 22 hp of 40 hp..

If follow the decoded C++ formula..

Code: Select all

if(22 >= 40)
{
    return 10;
}
else
{
    if(10 < 3) ------- is this situation even possible at all? I guess not.. so we skip it.
    {
        return min_blows - (min_blows - max_blows) * hp / max_hp;
    }
    else
    {
        return 3 + (10 - 3) * 22 / 40;  --- this is like ((7*22=154) / 40 = 3.85) + 3 = 6.85
    }
}
6.85 is not rounded.. if I ceil it will be 7.. is i floor it will be 6.. if i round closest it is 7...
How do I convert this formula into lua? How c++ rounds this? I know for sure that it will be either 6 or 7 attacks... it will not be 6.85 (but how did we achieve that? what would happen if result wass 6.45?) Is c++ always rounding to closest.. Ravana was it complete formula?
Sorry for so many questions.. it just doesn't work the way I see it at the moment...?
User avatar
Pentarctagon
Project Manager
Posts: 5528
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: enclave's WML Macros questions Thread

Post by Pentarctagon »

Those unsigned arguments are shorthand for unsigned int, and integer division always truncates the remainder: 4.9 becomes 4, -4.9 becomes -4.

Lua does not follow C++ in this, since the code:

Code: Select all

wesnoth.message(tostring(5/2))
prints 2.5.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: enclave's WML Macros questions Thread

Post by enclave »

THANKS so much Pentarctagon!!! You helped me a huge! Thanks Ravana for your formula!

PS. Here is how I converted formula into lua:
Spoiler:
But I met a problem...
When I tested unit HP and was dropping it by 1 with each test, everything worked perfectly same as original swarm, apart from when unit hp became below 0... The original swarm always stayed at minimal number of strikes 3, while my formula was reducing strikes by 1.
And here is example.. Unit hp is -42

Code: Select all

if(-42 >= 40)
{
    return 10;
}
else
{
    if(10 < 3)
    {
        return min_blows - (min_blows - max_blows) * hp / max_hp;
    }
    else
    {
        3 + (10 - 3) * -42 / 40;  (3+7*-42/40 = 3 - 294/40 = 3 - 7.35 = -4.35 )
    }
}
So it would be round(-4.35) so -4 ideally...
While in reality, real swarm is showing 3
And my stupid round formula is showing -5 (it's not meant to be working with negative values, but where it gets 5 instead of 4..? I have no idea..)
:))
conclusion:
Original formula is incomplete or wrongly decoded.. or swarm just stops being calculated when unit's health drops below 1 hp..
My lua formula is working correctly for positive values of HP.. but goes nuts with negative values.
Post Reply