From 78f9500b1d70da70f1f3b4412d6a1c2e01bc1a5e Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Sat, 9 May 2015 21:29:37 -0400 Subject: [PATCH] Work on scripting --- src/scriptengine/script_engine.cpp | 17 ++- src/scriptengine/script_engine.hpp | 7 +- src/scriptengine/script_gui.cpp | 208 +++++++++++------------------ src/scriptengine/script_gui.hpp | 2 - src/scriptengine/script_kart.cpp | 136 ++++++++++--------- src/scriptengine/script_track.cpp | 8 -- src/scriptengine/script_utils.cpp | 165 +++++++++++++++++++++++ src/scriptengine/script_utils.hpp | 34 +++++ src/scriptengine/scriptvec3.cpp | 18 ++- 9 files changed, 375 insertions(+), 220 deletions(-) create mode 100644 src/scriptengine/script_utils.cpp create mode 100644 src/scriptengine/script_utils.hpp diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 885bd22a2..138658e36 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -24,6 +24,7 @@ #include "scriptengine/script_engine.hpp" #include "scriptengine/script_gui.hpp" #include "scriptengine/script_track.hpp" +#include "scriptengine/script_utils.hpp" #include "scriptengine/scriptstdstring.hpp" #include "scriptengine/scriptvec3.hpp" #include @@ -150,7 +151,7 @@ void ScriptEngine::runScript(std::string scriptName, std::functionGetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)"); - func = Scripting::Track::registerScriptCallbacks(m_engine, scriptName); + func = registerScriptCallbacks(m_engine, scriptName); if (func == NULL) { @@ -244,6 +245,16 @@ void ScriptEngine::runScript(std::string scriptName, std::functionGetModule(0)->GetFunctionByDecl(function_name.c_str()); + return func; +} + +//----------------------------------------------------------------------------- + void ScriptEngine::cleanupCache() { for (auto curr : m_script_cache) @@ -265,11 +276,9 @@ void ScriptEngine::configureEngine(asIScriptEngine *engine) RegisterVec3(engine); //register Vec3 Scripting::Track::registerScriptFunctions(m_engine); - Scripting::Kart::registerScriptFunctions(m_engine); - Scripting::Physics::registerScriptFunctions(m_engine); - + Scripting::Utils::registerScriptFunctions(m_engine); Scripting::GUI::registerScriptFunctions(m_engine); Scripting::GUI::registerScriptEnums(m_engine); diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index e1c596454..9dca59770 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -43,9 +43,6 @@ namespace Scripting void registerScriptFunctions(asIScriptEngine *engine); void registerScriptEnums(asIScriptEngine *engine); - - asIScriptFunction* - registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName); } class ScriptEngine @@ -58,7 +55,9 @@ namespace Scripting void runScript(std::string scriptName); void runScript(std::string scriptName, std::function callback); void cleanupCache(); - + asIScriptFunction* + registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName); + private: asIScriptEngine *m_engine; std::map m_script_cache; diff --git a/src/scriptengine/script_gui.cpp b/src/scriptengine/script_gui.cpp index 60118cd71..783351819 100644 --- a/src/scriptengine/script_gui.cpp +++ b/src/scriptengine/script_gui.cpp @@ -34,114 +34,64 @@ #include #include //debug +/** \cond DOXYGEN_IGNORE */ namespace Scripting { +/** \endcond */ + namespace GUI { - //getter for key binding for player action enums - void getKeyBinding(asIScriptGeneric *gen) + /** \addtogroup Scripting + * @{ + */ + /** \addtogroup GUI + * @{ + */ + + /** Get the key bound to a player action (enum GUI::PlayerAction)*/ + std::string getKeyBinding(int Enum_value) { - int Enum_value = (int)gen->GetArgDWord(0); InputDevice* device = input_manager->getDeviceManager()->getLatestUsedDevice(); DeviceConfig* config = device->getConfiguration(); irr::core::stringw control; PlayerAction ScriptAction = (PlayerAction)Enum_value; control = config->getBindingAsString(ScriptAction); std::string key = std::string(irr::core::stringc(control).c_str()); - void *key_pointer = &key; - gen->SetReturnObject(key_pointer); + return key; } - // Displays the message specified in displayMessage( string message ) within the script - void displayMessage(asIScriptGeneric *gen) + /** Show the specified message in a popup */ + void displayMessage(std::string* input) { - std::string *input = (std::string*)gen->GetArgAddress(0); irr::core::stringw out = StringUtils::utf8_to_wide(input->c_str()); new TutorialMessageDialog((out), true); } - - void translate(asIScriptGeneric *gen) + /** Get translated version of string */ + std::string translate(std::string* input) { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - irr::core::stringw out = translations->fribidize(translations->w_gettext(input->c_str())); - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); + return StringUtils::wide_to_utf8(out.c_str()); } - void insertValues1(asIScriptGeneric *gen) + /** Translate string and insert values. e.g. GUI::translate("Hello %s !", "John") */ + std::string translate(std::string* formatString, std::string* arg1) { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - - irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(input->c_str()), - StringUtils::utf8_to_wide(arg1->c_str())); - - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); - } - - void insertValues2(asIScriptGeneric *gen) - { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - std::string *arg2 = (std::string*)gen->GetArgAddress(2); - - irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(input->c_str()), - StringUtils::utf8_to_wide(arg1->c_str()), - StringUtils::utf8_to_wide(arg2->c_str())); - - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); - } - - void insertValues3(asIScriptGeneric *gen) - { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - std::string *arg2 = (std::string*)gen->GetArgAddress(2); - std::string *arg3 = (std::string*)gen->GetArgAddress(3); - - irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(input->c_str()), - StringUtils::utf8_to_wide(arg1->c_str()), - StringUtils::utf8_to_wide(arg2->c_str()), - StringUtils::utf8_to_wide(arg3->c_str())); - - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); - } - - void translateAndInsertValues1(asIScriptGeneric *gen) - { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - - irr::core::stringw out = translations->w_gettext(input->c_str()); + irr::core::stringw out = translations->w_gettext(formatString->c_str()); out = StringUtils::insertValues(out, StringUtils::utf8_to_wide(arg1->c_str())); out = translations->fribidize(out); - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); + return StringUtils::wide_to_utf8(out.c_str()); } - void translateAndInsertValues2(asIScriptGeneric *gen) + /** Translate string and insert values. e.g. GUI::translate("Hello %s !", "John") */ + std::string translate(std::string* formatString, std::string* arg1, std::string* arg2) { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - std::string *arg2 = (std::string*)gen->GetArgAddress(2); - - irr::core::stringw out = translations->w_gettext(input->c_str()); + irr::core::stringw out = translations->w_gettext(formatString->c_str()); out = StringUtils::insertValues(out, StringUtils::utf8_to_wide(arg1->c_str()), @@ -149,19 +99,14 @@ namespace Scripting out = translations->fribidize(out); - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); + return StringUtils::wide_to_utf8(out.c_str()); } - void translateAndInsertValues3(asIScriptGeneric *gen) + /** Translate string and insert values. e.g. GUI::translate("Hello %s !", "John") */ + std::string translate(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3) { - // Get the arguments - std::string *input = (std::string*)gen->GetArgAddress(0); - std::string *arg1 = (std::string*)gen->GetArgAddress(1); - std::string *arg2 = (std::string*)gen->GetArgAddress(2); - std::string *arg3 = (std::string*)gen->GetArgAddress(3); - - irr::core::stringw out = translations->w_gettext(input->c_str()); + irr::core::stringw out = translations->w_gettext(formatString->c_str()); out = StringUtils::insertValues(out, StringUtils::utf8_to_wide(arg1->c_str()), @@ -170,70 +115,73 @@ namespace Scripting out = translations->fribidize(out); - // Return the string - new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); + return StringUtils::wide_to_utf8(out.c_str()); } - - void scriptLogInfo(asIScriptGeneric *gen) + /** @}*/ + /** @}*/ + + // UNDOCUMENTED PROXIES : Use proxies to have different signatures, then redirect to the + // documented function whose name is exposed in angelscript (these proxies exist so that + // angelscript can properly resolve overloads, but doxygen can still generate the right docs + /** \cond DOXYGEN_IGNORE */ + std::string proxy_translate(std::string* formatString) { - std::string input = *(std::string*)gen->GetArgAddress(0); - Log::info("Script", "%s", input.c_str()); + return translate(formatString); } - void scriptLogWarning(asIScriptGeneric *gen) + std::string proxy_translateAndInsertValues1(std::string* formatString, std::string* arg1) { - std::string input = *(std::string*)gen->GetArgAddress(0); - Log::warn("Script", "%s", input.c_str()); + return translate(formatString, arg1); } - void scriptLogError(asIScriptGeneric *gen) + std::string proxy_translateAndInsertValues2(std::string* formatString, std::string* arg1, std::string* arg2) { - std::string input = *(std::string*)gen->GetArgAddress(0); - Log::error("Script", "%s", input.c_str()); + return translate(formatString, arg1, arg2); } + std::string proxy_translateAndInsertValues3(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3) + { + return translate(formatString, arg1, arg2, arg3); + } + /** \endcond */ + void registerScriptFunctions(asIScriptEngine *engine) { int r; // of type asERetCodes engine->SetDefaultNamespace("GUI"); - r = engine->RegisterGlobalFunction("void displayMessage(string &in)", asFUNCTION(displayMessage), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string getKeyBinding(int input)", asFUNCTION(getKeyBinding), asCALL_GENERIC); assert(r >= 0); - - r = engine->RegisterGlobalFunction("string translate(const string &in)", asFUNCTION(translate), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in)", asFUNCTION(translateAndInsertValues1), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in)", asFUNCTION(translateAndInsertValues2), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(translateAndInsertValues3), asCALL_GENERIC); assert(r >= 0); - - engine->SetDefaultNamespace("Utils"); - r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in)", asFUNCTION(insertValues1), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in)", asFUNCTION(insertValues2), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(insertValues3), asCALL_GENERIC); assert(r >= 0); - - r = engine->RegisterGlobalFunction("void logInfo(const string &in)", asFUNCTION(scriptLogInfo), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("void logWarning(const string &in)", asFUNCTION(scriptLogWarning), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("void logError(const string &in)", asFUNCTION(scriptLogError), asCALL_GENERIC); assert(r >= 0); + r = engine->RegisterGlobalFunction("void displayMessage(const string &in)", asFUNCTION(displayMessage), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string getKeyBinding(int input)", asFUNCTION(getKeyBinding), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string translate(const string &in)", asFUNCTION(proxy_translate), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in)", asFUNCTION(proxy_translateAndInsertValues1), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in)", asFUNCTION(proxy_translateAndInsertValues2), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string translate(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(proxy_translateAndInsertValues3), asCALL_CDECL); assert(r >= 0); } void registerScriptEnums(asIScriptEngine *engine) { + // TODO: document enum in doxygen-generated scripting docs engine->SetDefaultNamespace("GUI"); - engine->RegisterEnum("PA"); - engine->RegisterEnumValue("PA", "STEER_LEFT", PA_STEER_LEFT); - engine->RegisterEnumValue("PA", "STEER_RIGHT", PA_STEER_RIGHT); - engine->RegisterEnumValue("PA", "ACCEL", PA_ACCEL); - engine->RegisterEnumValue("PA", "BRAKE", PA_BRAKE); - engine->RegisterEnumValue("PA", "NITRO", PA_NITRO); - engine->RegisterEnumValue("PA", "DRIFT", PA_DRIFT); - engine->RegisterEnumValue("PA", "RESCUE", PA_RESCUE); - engine->RegisterEnumValue("PA", "FIRE", PA_FIRE); - engine->RegisterEnumValue("PA", "LOOK_BACK", PA_LOOK_BACK); - engine->RegisterEnumValue("PA", "PAUSE_RACE", PA_PAUSE_RACE); - engine->RegisterEnumValue("PA", "MENU_UP", PA_MENU_UP); - engine->RegisterEnumValue("PA", "MENU_DOWN", PA_MENU_DOWN); - engine->RegisterEnumValue("PA", "MENU_LEFT", PA_MENU_LEFT); - engine->RegisterEnumValue("PA", "MENU_RIGHT", PA_MENU_RIGHT); - engine->RegisterEnumValue("PA", "MENU_SELECT", PA_MENU_SELECT); - engine->RegisterEnumValue("PA", "MENU_CANCEL", PA_MENU_CANCEL); + engine->RegisterEnum("PlayerAction"); + engine->RegisterEnumValue("PlayerAction", "STEER_LEFT", PA_STEER_LEFT); + engine->RegisterEnumValue("PlayerAction", "STEER_RIGHT", PA_STEER_RIGHT); + engine->RegisterEnumValue("PlayerAction", "ACCEL", PA_ACCEL); + engine->RegisterEnumValue("PlayerAction", "BRAKE", PA_BRAKE); + engine->RegisterEnumValue("PlayerAction", "NITRO", PA_NITRO); + engine->RegisterEnumValue("PlayerAction", "DRIFT", PA_DRIFT); + engine->RegisterEnumValue("PlayerAction", "RESCUE", PA_RESCUE); + engine->RegisterEnumValue("PlayerAction", "FIRE", PA_FIRE); + engine->RegisterEnumValue("PlayerAction", "LOOK_BACK", PA_LOOK_BACK); + engine->RegisterEnumValue("PlayerAction", "PAUSE_RACE", PA_PAUSE_RACE); + engine->RegisterEnumValue("PlayerAction", "MENU_UP", PA_MENU_UP); + engine->RegisterEnumValue("PlayerAction", "MENU_DOWN", PA_MENU_DOWN); + engine->RegisterEnumValue("PlayerAction", "MENU_LEFT", PA_MENU_LEFT); + engine->RegisterEnumValue("PlayerAction", "MENU_RIGHT", PA_MENU_RIGHT); + engine->RegisterEnumValue("PlayerAction", "MENU_SELECT", PA_MENU_SELECT); + engine->RegisterEnumValue("PlayerAction", "MENU_CANCEL", PA_MENU_CANCEL); } } + +/** \cond DOXYGEN_IGNORE */ } +/** \endcond */ diff --git a/src/scriptengine/script_gui.hpp b/src/scriptengine/script_gui.hpp index f5d5434f6..804abf1f8 100644 --- a/src/scriptengine/script_gui.hpp +++ b/src/scriptengine/script_gui.hpp @@ -29,8 +29,6 @@ namespace Scripting { //script engine functions void registerScriptFunctions(asIScriptEngine *engine); - asIScriptFunction* - registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName); void registerScriptEnums(asIScriptEngine *engine); } diff --git a/src/scriptengine/script_kart.cpp b/src/scriptengine/script_kart.cpp index 8642fbf00..d950b2ccf 100644 --- a/src/scriptengine/script_kart.cpp +++ b/src/scriptengine/script_kart.cpp @@ -25,99 +25,105 @@ //debug #include - +/** \cond DOXYGEN_IGNORE */ namespace Scripting { +/** \endcond */ namespace Kart { - //Squashes the specified kart, specified time - void squashKart(asIScriptGeneric *gen) + /** \addtogroup Scripting + * @{ + */ + /** \addtogroup Kart + * @{ + */ + + /** Squashes the specified kart, for the specified time */ + void squash(int idKart, float time) { - int id = (int)gen->GetArgDWord(0); - float time = gen->GetArgFloat(1); - AbstractKart* kart = World::getWorld()->getKart(id); + AbstractKart* kart = World::getWorld()->getKart(idKart); kart->setSquash(time, 0.5); //0.5 * max speed is new max for squashed duration } - //Teleports the kart to the specified Vec3 location - void teleportKart(asIScriptGeneric *gen) + + /** Teleports the kart to the specified Vec3 location */ + void teleport(int idKart, Vec3* position) { - int id = (int)gen->GetArgDWord(0); - Vec3 *position = (Vec3*)gen->GetArgAddress(1); + // TODO: must we delete "position" ? - float x = position->getX(); - float y = position->getY(); - float z = position->getZ(); - - AbstractKart* kart = World::getWorld()->getKart(id); - kart->setXYZ(btVector3(x, y, z)); + AbstractKart* kart = World::getWorld()->getKart(idKart); + kart->setXYZ(*position); unsigned int index = World::getWorld()->getRescuePositionIndex(kart); btTransform s = World::getWorld()->getRescueTransform(index); const btVector3 &xyz = s.getOrigin(); s.setRotation(btQuaternion(btVector3(0.0f, 1.0f, 0.0f), 0.0f)); World::getWorld()->moveKartTo(kart, s); } - //Attempts to project kart to the given 2D location, to the position - //with height 0, at a 45 degree angle. - void jumpKartTo(asIScriptGeneric *gen) - { - //attempts to project kart to target destination - //at present, assumes both initial and target location are - //on the same horizontal plane (z=k plane) and projects - //at a 45 degree angle. - int id = (int)gen->GetArgDWord(0); - float x = gen->GetArgFloat(1); - float y = gen->GetArgFloat(2); - //float velocity = gen->GetArgFloat(3); - //angle = pi/4 so t = v/(root 2 * g) - //d = t * v/root 2 so d = v^2/(2g) => v = root(2dg) - //component in x = component in y = root (dg) - AbstractKart* kart = World::getWorld()->getKart(id); - Vec3 pos = kart->getXYZ(); - float dx = x - pos[0]; - float dy = y - pos[2]; //blender uses xyz, bullet xzy - float d = (sqrtf(dx*dx + dy*dy)); - float normalized_dx = dx / d; - float normalized_dy = dy / d; - float g = 9.81f; - float velocity = sqrtf(d * g); - - kart->setVelocity(btVector3(velocity * normalized_dx, velocity, velocity * normalized_dy)); + /** Attempts to project kart to the given 2D location, to the position + * with height 0, at a 45 degree angle. + */ + // TODO: not sure what this function is for + //void jumpTo(asIScriptGeneric *gen) + //{ + // //attempts to project kart to target destination + // //at present, assumes both initial and target location are + // //on the same horizontal plane (z=k plane) and projects + // //at a 45 degree angle. + // int id = (int)gen->GetArgDWord(0); + // + // float x = gen->GetArgFloat(1); + // float y = gen->GetArgFloat(2); + // //float velocity = gen->GetArgFloat(3); + // //angle = pi/4 so t = v/(root 2 * g) + // //d = t * v/root 2 so d = v^2/(2g) => v = root(2dg) + // //component in x = component in y = root (dg) + // AbstractKart* kart = World::getWorld()->getKart(id); + // Vec3 pos = kart->getXYZ(); + // float dx = x - pos[0]; + // float dy = y - pos[2]; //blender uses xyz, bullet xzy + // float d = (sqrtf(dx*dx + dy*dy)); + // float normalized_dx = dx / d; + // float normalized_dy = dy / d; + // float g = 9.81f; + // float velocity = sqrtf(d * g); + // + // kart->setVelocity(btVector3(velocity * normalized_dx, velocity, velocity * normalized_dy)); + //} + + /** Returns the location of the corresponding kart. */ + Vec3 getLocation(int idKart) + { + AbstractKart* kart = World::getWorld()->getKart(idKart); + return kart->getXYZ(); } - //Bind kart location - void getKartLocation(asIScriptGeneric *gen) + + /** Sets the kart's velocity to the specified value. */ + void setVelocity(int idKart, Vec3* position) { - int id = (int)gen->GetArgDWord(0); - - AbstractKart* kart = World::getWorld()->getKart(id); - Vec3 kart_loc = kart->getXYZ(); - void *pointer = &kart_loc; - - gen->SetReturnObject(pointer); - } - //Bind setter for velocity - void setVelocity(asIScriptGeneric *gen) - { - int id = (int)gen->GetArgDWord(0); - Vec3 *position = (Vec3*)gen->GetArgAddress(1); - float x = position->getX(); float y = position->getY(); float z = position->getZ(); - AbstractKart* kart = World::getWorld()->getKart(id); + AbstractKart* kart = World::getWorld()->getKart(idKart); kart->setVelocity(btVector3(x, y, z)); } + + /** @}*/ + /** @}*/ + void registerScriptFunctions(asIScriptEngine *engine) { int r; - engine->SetDefaultNamespace("Karts"); - r = engine->RegisterGlobalFunction("void squashKart(int id, float time)", asFUNCTION(squashKart), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("void teleportKart(int id, Vec3 &in)", asFUNCTION(teleportKart), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("void setVelocity(int id, Vec3 &in)", asFUNCTION(setVelocity), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("void jumpKartTo(int id, float x, float y)", asFUNCTION(jumpKartTo), asCALL_GENERIC); assert(r >= 0); - r = engine->RegisterGlobalFunction("Vec3 getKartLocation(int id)", asFUNCTION(getKartLocation), asCALL_GENERIC); assert(r >= 0); + engine->SetDefaultNamespace("Kart"); + r = engine->RegisterGlobalFunction("void squash(int id, float time)", asFUNCTION(squash), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("void teleport(int id, const Vec3 &in)", asFUNCTION(teleport), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("void setVelocity(int id, const Vec3 &in)", asFUNCTION(setVelocity), asCALL_CDECL); assert(r >= 0); + //r = engine->RegisterGlobalFunction("void jumpTo(int id, float x, float y)", asFUNCTION(jumpTo), asCALL_GENERIC); assert(r >= 0); + r = engine->RegisterGlobalFunction("Vec3 getLocation(int id)", asFUNCTION(getLocation), asCALL_CDECL); assert(r >= 0); } } + +/** \cond DOXYGEN_IGNORE */ } +/** \endcond */ diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index 7cfb2602b..fce2a3dfa 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -38,14 +38,6 @@ namespace Scripting namespace Track { - //register callbacks - // TODO: move this out of Track namespace, it's not specific to tracks - asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine, std::string function_name) - { - asIScriptFunction *func; - func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str()); - return func; - } /* void disableAnimation(std::string *name, void *memory) { diff --git a/src/scriptengine/script_utils.cpp b/src/scriptengine/script_utils.cpp new file mode 100644 index 000000000..fcb19efad --- /dev/null +++ b/src/scriptengine/script_utils.cpp @@ -0,0 +1,165 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "script_track.hpp" + +#include "animations/three_d_animation.hpp" +#include "input/device_manager.hpp" +#include "input/input_device.hpp" +#include "input/input_manager.hpp" +#include "modes/world.hpp" +#include "states_screens/dialogs/tutorial_message_dialog.hpp" +#include "tracks/track.hpp" +#include "tracks/track_object.hpp" +#include "tracks/track_object_manager.hpp" + +#include +#include "scriptarray.hpp" + +#include +#include //debug + +/** \cond DOXYGEN_IGNORE */ +namespace Scripting +{ + /** \endcond */ + + namespace Utils + { + /** \addtogroup Scripting + * @{ + */ + /** \addtogroup Utils + * @{ + */ + // TODO: build these variations with variadic templates? + + /** Replaces placeholders with values. Note, in angelscript, omit the trailing number. + * e.g. Utils::insertValues("Hello %s !", "world"); + */ + std::string insertValues(std::string* formatString, std::string* arg1) + { + irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(formatString->c_str()), + StringUtils::utf8_to_wide(arg1->c_str())); + + return StringUtils::wide_to_utf8(out.c_str()); + } + + /** Replaces placeholders with values. Note, in angelscript, omit the trailing number. + * e.g. Utils::insertValues("Hello %s %s !", "John", "Doe"); + */ + std::string insertValues(std::string* formatString, std::string* arg1, std::string* arg2) + { + irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(formatString->c_str()), + StringUtils::utf8_to_wide(arg1->c_str()), + StringUtils::utf8_to_wide(arg2->c_str())); + + return StringUtils::wide_to_utf8(out.c_str()); + } + + /** Replaces placeholders with values. Note, in angelscript, omit the trailing number. + * e.g. Utils::insertValues("Hello %s %s %s !", "Mr", "John", "Doe"); + */ + std::string insertValues(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3) + { + irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(formatString->c_str()), + StringUtils::utf8_to_wide(arg1->c_str()), + StringUtils::utf8_to_wide(arg2->c_str()), + StringUtils::utf8_to_wide(arg3->c_str())); + + return StringUtils::wide_to_utf8(out.c_str()); + } + + /** Replaces placeholders with values. Note, in angelscript, omit the trailing number. + * e.g. Utils::insertValues("%s %s %s %s !", "Hello", "Mr", "John", "Doe"); + */ + std::string insertValues(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3, std::string* arg4) + { + irr::core::stringw out = StringUtils::insertValues(StringUtils::utf8_to_wide(formatString->c_str()), + StringUtils::utf8_to_wide(arg1->c_str()), + StringUtils::utf8_to_wide(arg2->c_str()), + StringUtils::utf8_to_wide(arg3->c_str()), + StringUtils::utf8_to_wide(arg4->c_str())); + + return StringUtils::wide_to_utf8(out.c_str()); + } + + /** Log to the console */ + void logInfo(std::string* log) + { + Log::info("Script", "%s", log->c_str()); + } + + /** Log warning to the console */ + void logWarning(std::string* log) + { + Log::warn("Script", "%s", log->c_str()); + } + + /** Log error to the console */ + void logError(std::string* log) + { + Log::error("Script", "%s", log->c_str()); + } + /** @}*/ + /** @}*/ + + // UNDOCUMENTED PROXIES : Use proxies to have different signatures, then redirect to the + // documented function whose name is exposed in angelscript (these proxies exist so that + // angelscript can properly resolve overloads, but doxygen can still generate the right docs + /** \cond DOXYGEN_IGNORE */ + std::string proxy_insertValues1(std::string* formatString, std::string* arg1) + { + return insertValues(formatString, arg1); + } + std::string proxy_insertValues2(std::string* formatString, std::string* arg1, std::string* arg2) + { + return insertValues(formatString, arg1, arg2); + } + std::string proxy_insertValues3(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3) + { + return insertValues(formatString, arg1, arg2, arg3); + } + std::string proxy_insertValues4(std::string* formatString, std::string* arg1, std::string* arg2, + std::string* arg3, std::string* arg4) + { + return insertValues(formatString, arg1, arg2, arg3, arg4); + } + /** \endcond */ + + void registerScriptFunctions(asIScriptEngine *engine) + { + int r; // of type asERetCodes + engine->SetDefaultNamespace("Utils"); + r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in)", asFUNCTION(proxy_insertValues1), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in)", asFUNCTION(proxy_insertValues2), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in, const string &in)", asFUNCTION(proxy_insertValues3), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("string insertValues(const string &in, const string &in, const string &in, const string &in, const string &in)", asFUNCTION(proxy_insertValues4), asCALL_CDECL); assert(r >= 0); + + r = engine->RegisterGlobalFunction("void logInfo(const string &in)", asFUNCTION(logInfo), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("void logWarning(const string &in)", asFUNCTION(logWarning), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("void logError(const string &in)", asFUNCTION(logError), asCALL_CDECL); assert(r >= 0); + } + } + +/** \cond DOXYGEN_IGNORE */ +} +/** \endcond */ diff --git a/src/scriptengine/script_utils.hpp b/src/scriptengine/script_utils.hpp new file mode 100644 index 000000000..8920d8d36 --- /dev/null +++ b/src/scriptengine/script_utils.hpp @@ -0,0 +1,34 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_SCRIPT_UTILS_HPP +#define HEADER_SCRIPT_UTILS_HPP + +#include + +#include + +namespace Scripting +{ + namespace Utils + { + void registerScriptFunctions(asIScriptEngine *engine); + } + +} +#endif diff --git a/src/scriptengine/scriptvec3.cpp b/src/scriptengine/scriptvec3.cpp index 0f7bc9306..1be851d6f 100644 --- a/src/scriptengine/scriptvec3.cpp +++ b/src/scriptengine/scriptvec3.cpp @@ -27,40 +27,44 @@ namespace Scripting { - - void Constructor(void *memory) { // Initialize the pre-allocated memory by calling the // object constructor with the placement-new operator new(memory)Vec3(); } + void Destructor(void *memory) { // Uninitialize the memory by calling the object destructor ((Vec3*)memory)->~Vec3(); } - void ConstructVector3FromFloats(float a, float b, float c, void *memory){ + + void ConstructVector3FromFloats(float a, float b, float c, void *memory) + { //Constructor using 3 floats new (memory)(Vec3)(Vec3(a, b, c)); } + //Print for debugging purposes void printVec3(asIScriptGeneric *gen) { Vec3 *script_vec3 = (Vec3*)gen->GetArgObject(0); std::cout << script_vec3->getX() << "," << script_vec3->getY() << "," << script_vec3->getZ() << std::endl; } + void RegisterVec3(asIScriptEngine *engine) { int r; - r = engine->RegisterObjectType("Vec3", sizeof(Vec3), asOBJ_VALUE); assert(r >= 0); + r = engine->RegisterObjectType("Vec3", sizeof(Vec3), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK); assert(r >= 0); // Register the behaviours r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert(r >= 0); r = engine->RegisterObjectMethod("Vec3", "Vec3 &opAssign(const Vec3 &in)", asMETHODPR(Vec3, operator =, (const Vec3&), Vec3&), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectBehaviour("Vec3", asBEHAVE_CONSTRUCT, "void f(float, float, float)", asFUNCTION(ConstructVector3FromFloats), asCALL_CDECL_OBJLAST); assert(r >= 0); - r = engine->RegisterGlobalFunction("void printVec3(Vec3 a)", asFUNCTION(printVec3), asCALL_GENERIC); assert(r >= 0); - - + r = engine->RegisterGlobalFunction("void printVec3(Vec3 a)", asFUNCTION(printVec3), asCALL_GENERIC); assert(r >= 0); + r = engine->RegisterObjectMethod("Vec3", "float &getX()", asMETHOD(Vec3, getX), asCALL_THISCALL); assert(r >= 0); + r = engine->RegisterObjectMethod("Vec3", "float &getY()", asMETHOD(Vec3, getY), asCALL_THISCALL); assert(r >= 0); + r = engine->RegisterObjectMethod("Vec3", "float &getZ()", asMETHOD(Vec3, getZ), asCALL_THISCALL); assert(r >= 0); } }