#include #include "script_value.hpp" #include "entity.hpp" #include "array.hpp" #include "functions.hpp" namespace scripting { /*************************************************************** * Constructors **************************************************************/ script_value::script_value(const game::VariableValue& value) : value_(value) { } 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); const auto _ = gsl::finally([&variable]() { game::RemoveRefToValue(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 vector& value) { game::VariableValue variable{}; variable.type = game::SCRIPT_VECTOR; variable.u.vectorValue = game::Scr_AllocVector(value); const auto _ = gsl::finally([&variable]() { game::RemoveRefToValue(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; } 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)); } template <> std::string script_value::get() const { return this->get(); } /*************************************************************** * 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->objectVariableValue[id].w.type; 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->objectVariableValue[id].w.type; return type == game::SCRIPT_STRUCT; } /*************************************************************** * Function **************************************************************/ template <> bool script_value::is>() const { return this->get_raw().type == game::SCRIPT_FUNCTION; } /*************************************************************** * 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); } /*************************************************************** * 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>()) { return utils::string::va("[[ function ]]"); } return this->type_name(); } }