enclave's WML Macros questions Thread
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.
Re: enclave's WML Macros questions Thread
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.beetlenaut wrote:You'll just have to do some extra checking. If itnumerically_equals = 0
, but doesn't alsoequals = "0"
, then you know they entered some non-numbers. Otherwise, they entered something valid.
Re: enclave's WML Macros questions Thread
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:gfgtdf wrote:So it's probably better to use tonumber(value) ~= nil to check whether am attribute is a valid number.
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]
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]
Code: Select all
else
wesnoth.set_variable("variable_is_numeric","false")
{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..
- Pentarctagon
- Project Manager
- Posts: 5603
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: enclave's WML Macros questions Thread
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: enclave's WML Macros questions Thread
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..
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..
Re: enclave's WML Macros questions Thread
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.
Re: enclave's WML Macros questions Thread
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 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.. ?
Re: enclave's WML Macros questions Thread
Thanks very much zookeeper!!!zookeeper wrote: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 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.. ?
Re: enclave's WML Macros questions Thread
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!
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!
Re: enclave's WML Macros questions Thread
https://github.com/wesnoth/wesnoth/blob ... attack.hpp
While you might still want to test it, you can test that function separate from wesnoth.
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;
}
Re: enclave's WML Macros questions Thread
Hi, is it c++ or something?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;
What the "?" and ":" means there? sorry =] I could try googling, but I dont even know which type of C it is
Why 2 lines are similar to each other ?
- James_The_Invisible
- Posts: 538
- Joined: October 28th, 2012, 1:58 pm
- Location: Somewhere in the Northlands, fighting dark forces
- Contact:
Re: enclave's WML Macros questions Thread
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.
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.
My content: For Power series
Ports: Danse Macabre Story of the Wose
Guides: How to translate your UMC
Ports: Danse Macabre Story of the Wose
Guides: How to translate your UMC
- Pentarctagon
- Project Manager
- Posts: 5603
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: enclave's WML Macros questions Thread
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: enclave's WML Macros questions Thread
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..
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...?
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
}
}
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...?
- Pentarctagon
- Project Manager
- Posts: 5603
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: enclave's WML Macros questions Thread
Those
Lua does not follow C++ in this, since the code:
prints
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))
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: enclave's WML Macros questions Thread
THANKS so much Pentarctagon!!! You helped me a huge! Thanks Ravana for your formula!
PS. Here is how I converted formula into lua: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
So it would be
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.
PS. Here is how I converted formula into lua:
Spoiler:
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 )
}
}
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.