Add 'round' key to [set_variable]...

...(http://www.wesnoth.org/forum/viewtopic.php?f=10&t=22879)
This commit is contained in:
Alexander van Gessel 2008-11-28 00:17:03 +01:00
parent a1d02c7657
commit 62d91312e6
3 changed files with 66 additions and 0 deletions

View file

@ -27,6 +27,8 @@ Version 1.5.6+svn:
* Added a border and blurring to story screens' text blocks.
* Improved wrapping in the new widgets.
* Improved easy close handling.
* WML engine:
* Added the 'round' key to [set_variable].
* Miscellaneous and bug fixes:
* Included extra headers for certain g++ versions (patch #1113).

View file

@ -470,6 +470,49 @@ Xu , Xu , Qxu , Qxu , Ql , Ql
y=2,2
terrain=Re
[/terrain]
[label]
x,y=2,2
text="Math test"
[/label]
[event]
name=moveto
first_time_only=no
[filter]
side=1
x,y=2,2
[/filter]
[message]
speaker=narrator
image=wesnoth-icon.png
message="Enter a number."
[text_input]
variable=number
text=0
[/text_input]
[/message]
[message]
speaker=narrator
image=wesnoth-icon.png
message="How should we round $number|?
`(any integer)
`ceil
`floor"
[text_input]
variable=round
[/text_input]
[/message]
{VARIABLE rounded $number}
# {SET_VARIABLE rounded round $round}
[set_variable]
name=rounded
round=$round
[/set_variable]
[message]
speaker=narrator
image=wesnoth-icon.png
message="Rounding $number| to $round| results in $rounded|."
[/message]
[/event]
[/event]
[item]

View file

@ -1097,6 +1097,27 @@ namespace {
}
}
const std::string round_val = cfg["round"];
if(round_val.empty() == false) {
double value = lexical_cast<double>(var.c_str());
if (round_val == "ceil") {
//TODO precision stuff
value = ceil(value);
} else if (round_val == "floor") {
//TODO same
value = floor(value);
} else {
// We assume the value is an integer.
// Any non-numerical values will be interpreted as 0
// Which is probably what was intended anyway
const int decimals = atoi(round_val.c_str());
value *= pow(10, decimals); //add $decimals zeroes
value = round(value);
value *= pow(10, -decimals); //and remove them
}
var = str_cast(value);
}
const t_string string_length_target = cfg["string_length"];
if(string_length_target.empty() == false) {
const int value = string_length_target.str().length();