Skip to content

Commit f5cef24

Browse files
committed
fix NaN for functional-if-then-else
Any NaN encountered in the evaluation of functional-if-then-else should be isolated to the branch on which it occurs. Fixes #6536. Also improve some help text for clarity.
1 parent 1c82141 commit f5cef24

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

code/parse/sexp.cpp

+30-17
Original file line numberDiff line numberDiff line change
@@ -12438,27 +12438,39 @@ bool is_implicit_argument_provider_op(const int op_const)
1243812438
// Goober5000
1243912439
int sexp_functional_if_then_else(int node)
1244012440
{
12441-
int num1, num2, n;
12442-
bool is_nan, is_nan_forever;
12441+
int num1, num2, n = node;
12442+
bool is_nan1, is_nan_forever1, is_nan2, is_nan_forever2;
1244312443

12444-
Assertion(CAR(node) >= 0, "The condition in functional-if-then-else must be an operator!");
12444+
Assertion(CAR(n) >= 0, "The condition in functional-if-then-else must be an operator!");
1244512445

1244612446
// decision time
12447-
int condition = eval_sexp(CAR(node));
12447+
int condition = eval_sexp(CAR(n));
12448+
n = CDR(n);
1244812449

1244912450
// we need to evaluate both numbers regardless of which one we pick
12450-
n = CDR(node);
12451-
eval_nums(n, is_nan, is_nan_forever, num1, num2);
12452-
if (is_nan)
12453-
return SEXP_NAN;
12454-
if (is_nan_forever)
12455-
return SEXP_NAN_FOREVER;
12451+
num1 = eval_num(n, is_nan1, is_nan_forever1);
12452+
n = CDR(n);
12453+
num2 = eval_num(n, is_nan2, is_nan_forever2);
1245612454

1245712455
// pick one
1245812456
if (condition == SEXP_TRUE || condition == SEXP_KNOWN_TRUE)
12459-
return num1;
12457+
{
12458+
if (is_nan1)
12459+
return SEXP_NAN;
12460+
else if (is_nan_forever1)
12461+
return SEXP_NAN_FOREVER;
12462+
else
12463+
return num1;
12464+
}
1246012465
else
12461-
return num2;
12466+
{
12467+
if (is_nan2)
12468+
return SEXP_NAN;
12469+
else if (is_nan_forever2)
12470+
return SEXP_NAN_FOREVER;
12471+
else
12472+
return num2;
12473+
}
1246212474
}
1246312475

1246412476
// Goober5000
@@ -38052,9 +38064,10 @@ SCP_vector<sexp_help_struct> Sexp_help = {
3805238064
"\t1:\tName of ship to check (evaluation returns NAN until ship is in-mission)." },
3805338065

3805438066
{ OP_HITS_LEFT, "Hits left (Status operator)\r\n"
38055-
"\tReturns the current level of the specified ship's hull as a percentage.\r\n\r\n"
38067+
"\tReturns the current level of the specified ship's hull as a percentage Note: for ships that have not arrived yet, or have departed, "
38068+
" or have been destroyed (even if the ship is 'in-mission' but doing a death roll) this returns NaN.\r\n\r\n"
3805638069
"Returns a numeric value. Takes 1 argument...\r\n"
38057-
"\t1:\tName of ship to check (evaluation returns NAN until ship is in-mission)." },
38070+
"\t1:\tName of ship to check (evaluation returns NAN unless ship is in-mission and alive)." },
3805838071

3805938072
{ OP_HITS_LEFT_SUBSYSTEM, "Hits left subsystem (status operator, deprecated)\r\n"
3806038073
"\tReturns the current level of the specified ship's subsystem integrity as a percentage of the damage done to *all "
@@ -38064,7 +38077,7 @@ SCP_vector<sexp_help_struct> Sexp_help = {
3806438077
"this operator is deprecated. Mission designers are strongly encouraged to use hits-left-subsystem-specific rather than "
3806538078
"the optional boolean parameter.\r\n\r\n"
3806638079
"Returns a numeric value. Takes 2 or 3 arguments...\r\n"
38067-
"\t1:\tName of ship to check (evaluation returns NAN until ship is in-mission).\r\n"
38080+
"\t1:\tName of ship to check (evaluation returns NAN unless ship is in-mission and alive).\r\n"
3806838081
"\t2:\tName of subsystem on ship to check.\r\n"
3806938082
"\t3:\t(Optional) True/False. When set to true only the subsystem supplied will be tested; when set to false (the default), "
3807038083
"all subsystems of that type will be tested." },
@@ -38076,7 +38089,7 @@ SCP_vector<sexp_help_struct> Sexp_help = {
3807638089
"example, if the integrity of all engine subsystems (that is, the combined strength of all engines divided by the maximum "
3807738090
"total strength of all engines) is less than 30%, the player cannot warp out.\r\n\r\n"
3807838091
"Returns a numeric value. Takes 2 arguments...\r\n"
38079-
"\t1:\tName of ship to check (evaluation returns NAN until ship is in-mission)\r\n"
38092+
"\t1:\tName of ship to check (evaluation returns NAN unless ship is in-mission and alive)\r\n"
3808038093
"\t2:\tName of subsystem type to check\r\n" },
3808138094

3808238095
{ OP_HITS_LEFT_SUBSYSTEM_SPECIFIC, "hits-left-subsystem-specific (status operator)\r\n"
@@ -38086,7 +38099,7 @@ SCP_vector<sexp_help_struct> Sexp_help = {
3808638099
"and will not appear in the operator list; it can only be used if you type it in manually. Old missions using hits-left-subsystem "
3808738100
"will still work, but mission designers are strongly encouraged to use the new operators instead.)\r\n\r\n"
3808838101
"Returns a numeric value. Takes 2 arguments...\r\n"
38089-
"\t1:\tName of ship to check (evaluation returns NAN until ship is in-mission)\r\n"
38102+
"\t1:\tName of ship to check (evaluation returns NAN unless ship is in-mission and alive)\r\n"
3809038103
"\t2:\tName of subsystem to check\r\n" },
3809138104

3809238105
{ OP_SIM_HITS_LEFT, "Simulated Hits left (Status operator)\r\n"

0 commit comments

Comments
 (0)