#include #include "script_value.hpp" #include "entity.hpp" #include "array.hpp" #include "function.hpp" #include "object.hpp" namespace scripting { /*********************************************** * Constructors **********************************************/ script_value::script_value(const game::VariableValue& value) : value_(value) { } script_value::script_value(void* value) { game::VariableValue variable{}; variable.type = game::SCRIPT_INTEGER; variable.u.intValue = reinterpret_cast(value); this->value_ = variable; } script_value::script_value(const int value) { game::VariableValue variable{}; variable.type = game::SCRIPT_INTEGER; variable.u.intValue = value; this->value_ = variable; } script_value::script_value(const unsigned int value) { game::VariableValue variable{}; variable.type = game::SCRIPT_INTEGER; variable.u.uintValue = value; this->value_ = variable; } script_value::script_value(const bool value) : script_value(static_cast(value)) { } script_value::script_value(const float value) { game::VariableValue variable{}; variable.type = game::SCRIPT_FLOAT; variable.u.floatValue = value; this->value_ = variable; } script_value::script_value(const double value) : script_value(static_cast(value)) { } script_value::script_value(const char* value) { game::VariableValue variable{}; variable.type = game::SCRIPT_STRING; variable.u.stringValue = game::SL_GetString(value, 0, game::SCRIPTINSTANCE_SERVER); const auto _ = gsl::finally([&variable]() { game::RemoveRefToValue(game::SCRIPTINSTANCE_SERVER, variable.type, variable.u); }); this->value_ = variable; } script_value::script_value(const std::string& value) : script_value(value.data()) { } script_value::script_value(const entity& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_OBJECT; variable.u.pointerValue = value.get_entity_id(); this->value_ = variable; } script_value::script_value(const array& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_OBJECT; variable.u.pointerValue = value.get_entity_id(); this->value_ = variable; } script_value::script_value(const object& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_OBJECT; variable.u.pointerValue = value.get_entity_id(); this->value_ = variable; } script_value::script_value(const function& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_FUNCTION; variable.u.codePosValue = value.get_pos(); this->value_ = variable; } script_value::script_value(const vector& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_VECTOR; variable.u.vectorValue = game::Scr_AllocVector(game::SCRIPTINSTANCE_SERVER, value); const auto _ = gsl::finally([&variable]() { game::RemoveRefToValue(game::SCRIPTINSTANCE_SERVER, variable.type, variable.u); }); this->value_ = variable; } /*********************************************** * Integer **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_INTEGER; } template <> bool script_value::is() const { return this->is(); } template <> bool script_value::is() const { return this->is(); } template <> int script_value::get() const { return this->get_raw().u.intValue; } template <> unsigned int script_value::get() const { return this->get_raw().u.uintValue; } template <> bool script_value::get() const { return this->get_raw().u.uintValue != 0; } /*********************************************** * Float **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_FLOAT; } template <> bool script_value::is() const { return this->is(); } template <> float script_value::get() const { return this->get_raw().u.floatValue; } template <> double script_value::get() const { return static_cast(this->get_raw().u.floatValue); } /*********************************************** * String **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_STRING || this->get_raw().type == game::SCRIPT_ISTRING; } template <> bool script_value::is() const { return this->is(); } template <> const char* script_value::get() const { return game::SL_ConvertToString(static_cast(this->get_raw().u.stringValue), game::SCRIPTINSTANCE_SERVER); } template <> std::string script_value::get() const { return this->get(); } /*********************************************** * Entity **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_OBJECT; } template <> entity script_value::get() const { return entity(this->get_raw().u.pointerValue); } /*********************************************** * Array **********************************************/ template <> bool script_value::is() const { if (this->get_raw().type != game::SCRIPT_OBJECT) { return false; } const auto id = this->get_raw().u.uintValue; const auto type = game::scr_VarGlob->variableList[id + 1].w.type & 0x1F; return type == game::SCRIPT_ARRAY; } template <> array script_value::get() const { return array(this->get_raw().u.uintValue); } /*********************************************** * Struct **********************************************/ template <> bool script_value::is() const { if (this->get_raw().type != game::SCRIPT_OBJECT) { return false; } const auto id = this->get_raw().u.uintValue; const auto type = game::scr_VarGlob->variableList[id + 1].w.type & 0x1F; return type == game::SCRIPT_STRUCT || type == game::SCRIPT_ENTITY; } template <> object script_value::get() const { return object(this->get_raw().u.uintValue); } /*********************************************** * Function **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_FUNCTION; } template <> function script_value::get() const { return function(this->get_raw().u.codePosValue); } /*********************************************** * Vector **********************************************/ template <> bool script_value::is() const { return this->get_raw().type == game::SCRIPT_VECTOR; } template <> vector script_value::get() const { return this->get_raw().u.vectorValue; } /*********************************************** * **********************************************/ const game::VariableValue& script_value::get_raw() const { return this->value_.get(); } std::string script_value::to_string() const { if (this->is()) { return utils::string::va("%i", this->as()); } if (this->is()) { return utils::string::va("%f", this->as()); } if (this->is()) { return this->as(); } if (this->is()) { const auto vec = this->as(); return utils::string::va("(%g, %g, %g)", vec.get_x(), vec.get_y(), vec.get_z() ); } if (this->is()) { const auto func = this->as(); const auto& name = func.get_name(); return utils::string::va("[[ %s ]]", name.data()); } return this->type_name(); } function_argument::function_argument(const arguments& args, const script_value& value, const int index) : values_(args) , value_(value) , index_(index) { } function_arguments::function_arguments(const arguments& values) : values_(values) { } }