Skip to content

Commit

Permalink
Merge pull request scp-fs2open#6063 from Goober5000/sexp_boolean
Browse files Browse the repository at this point in the history
allow boolean and numeric scripted SEXPs to return special values
  • Loading branch information
Goober5000 authored Mar 26, 2024
2 parents 0ffd9bc + 897e388 commit 6f94d67
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
40 changes: 39 additions & 1 deletion code/parse/sexp/LuaSEXP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "parse/parselo.h"
#include "parse/sexp.h"
#include "parse/sexp/sexp_lookup.h"
#include "scripting/api/objs/enums.h"
#include "scripting/api/objs/hudgauge.h"
#include "scripting/api/objs/message.h"
#include "scripting/api/objs/model.h"
Expand All @@ -32,7 +33,8 @@ using namespace luacpp;

namespace sexp {

static SCP_unordered_map<SCP_string, int> parameter_type_mapping{{ "boolean", OPF_BOOL },
static SCP_unordered_map<SCP_string, int> parameter_type_mapping {
{ "boolean", OPF_BOOL },
{ "number", OPF_NUMBER },
{ "ship", OPF_SHIP },
{ "shipname", OPF_SHIP },
Expand Down Expand Up @@ -337,6 +339,34 @@ luacpp::LuaValue LuaSEXP::sexpToLua(int node, int argnum, int parent_node) const
}
}
}
bool LuaSEXP::maybeExtractSexpSpecialRetVal(const luacpp::LuaValue& value, int& sexp_retval) {
// see if this return value is actually a SEXP_ enum
if (value.getValueType() == ValueType::USERDATA) {
try {
using namespace scripting::api;
enum_h enumeration;
value.getValue(l_Enum.Get(&enumeration)); // this might throw a LuaException
switch (enumeration.index) {
case LE_SEXP_TRUE:
case LE_SEXP_FALSE:
case LE_SEXP_KNOWN_FALSE:
case LE_SEXP_KNOWN_TRUE:
case LE_SEXP_UNKNOWN:
case LE_SEXP_NAN:
case LE_SEXP_NAN_FOREVER:
case LE_SEXP_CANT_EVAL:
sexp_retval = *enumeration.value;
return true;
default:
break;
}
} catch (const LuaException& le) {
// just ignore the exception because we follow a different code path if the conversion failed
SCP_UNUSED(le);
}
}
return false;
}
int LuaSEXP::getSexpReturnValue(const LuaValueList& retVals) const {
switch (_return_type) {
case OPR_NUMBER:
Expand All @@ -347,6 +377,10 @@ int LuaSEXP::getSexpReturnValue(const LuaValueList& retVals) const {
retVals.size());
return 0;
} else if (retVals[0].getValueType() != ValueType::NUMBER) {
int sexp_retval;
if (maybeExtractSexpSpecialRetVal(retVals[0], sexp_retval)) {
return sexp_retval;
}
Warning(LOCATION, "Wrong return type detected for Lua SEXP '%s', expected a number.", _name.c_str());
return 0;
} else {
Expand All @@ -360,6 +394,10 @@ int LuaSEXP::getSexpReturnValue(const LuaValueList& retVals) const {
retVals.size());
return SEXP_FALSE;
} else if (retVals[0].getValueType() != ValueType::BOOLEAN) {
int sexp_retval;
if (maybeExtractSexpSpecialRetVal(retVals[0], sexp_retval)) {
return sexp_retval;
}
Warning(LOCATION, "Wrong return type detected for Lua SEXP '%s', expected a boolean.", _name.c_str());
return SEXP_FALSE;
} else {
Expand Down
3 changes: 3 additions & 0 deletions code/parse/sexp/LuaSEXP.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class LuaSEXP : public DynamicSEXP {
// just a helper for parseTable
static bool parseCheckEndOfDescription();

// another helper
static bool maybeExtractSexpSpecialRetVal(const luacpp::LuaValue& value, int& sexp_retval);

public:
static std::pair<SCP_string, int> get_parameter_type(const SCP_string& name);
static int get_return_type(const SCP_string& name);
Expand Down
8 changes: 8 additions & 0 deletions code/scripting/api/objs/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ const lua_enum_def_list Enumerations[] = {
{"SCROLLBACK_SOURCE_SATISFIED", LE_SCROLLBACK_SOURCE_SATISFIED, true},
{"SCROLLBACK_SOURCE_COMMAND", LE_SCROLLBACK_SOURCE_COMMAND, true},
{"SCROLLBACK_SOURCE_NETPLAYER", LE_SCROLLBACK_SOURCE_NETPLAYER, true},
{"SEXP_TRUE", LE_SEXP_TRUE, SEXP_TRUE, true},
{"SEXP_FALSE", LE_SEXP_FALSE, SEXP_FALSE, true},
{"SEXP_KNOWN_FALSE", LE_SEXP_KNOWN_FALSE, SEXP_KNOWN_FALSE, true},
{"SEXP_KNOWN_TRUE", LE_SEXP_KNOWN_TRUE, SEXP_KNOWN_TRUE, true},
{"SEXP_UNKNOWN", LE_SEXP_UNKNOWN, SEXP_UNKNOWN, true},
{"SEXP_NAN", LE_SEXP_NAN, SEXP_NAN, true},
{"SEXP_NAN_FOREVER", LE_SEXP_NAN_FOREVER, SEXP_NAN_FOREVER, true},
{"SEXP_CANT_EVAL", LE_SEXP_CANT_EVAL, SEXP_CANT_EVAL, true},
};

const size_t Num_enumerations = sizeof(Enumerations) / sizeof(lua_enum_def_list);
Expand Down
8 changes: 8 additions & 0 deletions code/scripting/api/objs/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ enum lua_enum : int32_t {
LE_SCROLLBACK_SOURCE_SATISFIED,
LE_SCROLLBACK_SOURCE_COMMAND,
LE_SCROLLBACK_SOURCE_NETPLAYER,
LE_SEXP_TRUE,
LE_SEXP_FALSE,
LE_SEXP_KNOWN_FALSE,
LE_SEXP_KNOWN_TRUE,
LE_SEXP_UNKNOWN,
LE_SEXP_NAN,
LE_SEXP_NAN_FOREVER,
LE_SEXP_CANT_EVAL,
ENUM_NEXT_INDEX,
ENUM_COMBINATION,
ENUM_INVALID
Expand Down

0 comments on commit 6f94d67

Please sign in to comment.