From 585d1c50169c267966aa98db6d01e912583cd63c Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Thu, 30 Apr 2015 19:37:11 -0400 Subject: [PATCH] Scripting cleanup, remove hardcoded stuff in favor of more generic approaches --- src/physics/physics.cpp | 26 +++++++++----------- src/scriptengine/script_engine.cpp | 38 +++++++++-------------------- src/scriptengine/script_engine.hpp | 9 ------- src/scriptengine/script_gui.cpp | 25 +++++++++++++++++++ src/scriptengine/script_physics.cpp | 17 ------------- src/scriptengine/script_track.cpp | 12 --------- src/tracks/track.cpp | 4 +-- 7 files changed, 50 insertions(+), 81 deletions(-) diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 7935282f3..056044f7d 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -171,9 +171,9 @@ void Physics::update(float dt) Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); int kartid1 = p->getUserPointer(0)->getPointerKart()->getWorldKartId(); int kartid2 = p->getUserPointer(1)->getPointerKart()->getWorldKartId(); + // TODO: do not use globals this way, pass directly as function paramters Scripting::Physics::setCollision(kartid1,kartid2); - Scripting::Physics::setCollisionType("KartKart"); - script_engine->runScript("collisions"); + script_engine->runScript("onKartKartCollision"); continue; } // if kart-kart collision @@ -183,16 +183,15 @@ void Physics::update(float dt) // ------------------------- Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::Physics::setCollision(0, 0); - Scripting::Physics::setCollisionType("KartObject"); //object as in physical object - Scripting::Physics::setCollision - ( + //Scripting::Physics::setCollisionType("KartObject"); //object as in physical object + Scripting::Physics::setCollision( p->getUserPointer(0)->getPointerPhysicalObject()->getID(), "kart" - ); - script_engine->runScript("collisions"); + ); + script_engine->runScript("onKartObjectCollision"); PhysicalObject *obj = p->getUserPointer(0) ->getPointerPhysicalObject(); - if(obj->isCrashReset()) + if (obj->isCrashReset()) { AbstractKart *kart = p->getUserPointer(1)->getPointerKart(); new RescueAnimation(kart); @@ -219,7 +218,7 @@ void Physics::update(float dt) continue; } - if(p->getUserPointer(0)->is(UserPointer::UP_ANIMATION)) + if (p->getUserPointer(0)->is(UserPointer::UP_ANIMATION)) { // Kart hits animation ThreeDAnimation *anim=p->getUserPointer(0)->getPointerAnimation(); @@ -257,13 +256,12 @@ void Physics::update(float dt) // ------------------------------- Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); Scripting::Physics::setCollision(0,0); //TODO : support item types etc - Scripting::Physics::setCollisionType("ItemObject"); - Scripting::Physics::setCollision - ( + //Scripting::Physics::setCollisionType("ItemObject"); + Scripting::Physics::setCollision( p->getUserPointer(1)->getPointerPhysicalObject()->getID(), "item" - ); - script_engine->runScript("collisions"); + ); + script_engine->runScript("onItemObjectCollision"); p->getUserPointer(0)->getPointerFlyable() ->hit(NULL, p->getUserPointer(1)->getPointerPhysicalObject()); PhysicalObject* obj = p->getUserPointer(1)->getPointerPhysicalObject(); diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 35efa63e5..13aa1ac42 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -85,10 +85,9 @@ std::string getScript(std::string scriptName) { std::string script_dir = file_manager->getAsset(FileManager::SCRIPT, ""); script_dir += World::getWorld()->getTrack()->getIdent() + "/"; - if (scriptName != "update" && scriptName != "collisions" && scriptName != "start") - scriptName = "triggers"; - script_dir += scriptName + ".as"; + // TODO: allow splitting in multiple files + script_dir += "scripting.as"; FILE *f = fopen(script_dir.c_str(), "rb"); if (f == NULL) { @@ -134,7 +133,8 @@ void ScriptEngine::runScript(std::string scriptName) asIScriptFunction *func; - if (m_script_cache.find(scriptName) == m_script_cache.end()) + auto cached_script = m_script_cache.find(scriptName); + if (cached_script == m_script_cache.end()) { // Compile the script code Log::debug("Scripting", "Compiling script '%s' (was not in cache)", scriptName.c_str()); @@ -142,7 +142,7 @@ void ScriptEngine::runScript(std::string scriptName) if (r < 0) { Log::debug("Scripting", "Script '%s' is not available", scriptName.c_str()); - m_script_cache[scriptName] = NULL; // remember that this script is unavaiable + m_script_cache[scriptName] = NULL; // remember that this script is unavailable ctx->Release(); return; } @@ -150,27 +150,12 @@ void ScriptEngine::runScript(std::string scriptName) // Find the function for the function we want to execute. //This is how you call a normal function with arguments //asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)"); - if (scriptName == "collisions") - { - func = Scripting::Physics::registerScriptCallbacks(m_engine); - } - else if (scriptName == "update") - { - func = Scripting::Track::registerUpdateScriptCallbacks(m_engine); - } - else if (scriptName == "start") - { - func = Scripting::Track::registerStartScriptCallbacks(m_engine); - } - else - { - //trigger type can have different names - func = Scripting::Track::registerScriptCallbacks(m_engine, scriptName); - } - + func = Scripting::Track::registerScriptCallbacks(m_engine, scriptName); + if (func == NULL) { - Log::warn("Scripting", "The required function was not found : %s", scriptName.c_str()); + Log::debug("Scripting", "Scripting function was not found : %s", scriptName.c_str()); + m_script_cache[scriptName] = NULL; // remember that this script is unavailable ctx->Release(); return; } @@ -181,9 +166,8 @@ void ScriptEngine::runScript(std::string scriptName) } else { - //Script present in cache - // TODO: clear when done - func = m_script_cache[scriptName]; + // Script present in cache + func = cached_script->second; } if (func == NULL) diff --git a/src/scriptengine/script_engine.hpp b/src/scriptengine/script_engine.hpp index 2affef764..67b145b38 100644 --- a/src/scriptengine/script_engine.hpp +++ b/src/scriptengine/script_engine.hpp @@ -30,10 +30,7 @@ namespace Scripting namespace Physics { void registerScriptFunctions(asIScriptEngine *engine); - asIScriptFunction* - registerScriptCallbacks(asIScriptEngine *engine); void setCollision(int collider1,int collider2); - void setCollisionType(std::string collisionType); void setCollision(std::string collider1, std::string collider2); } @@ -50,12 +47,6 @@ namespace Scripting asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine, std::string scriptName); - - asIScriptFunction* - registerUpdateScriptCallbacks(asIScriptEngine *engine); - - asIScriptFunction* - registerStartScriptCallbacks(asIScriptEngine *engine); } class ScriptEngine diff --git a/src/scriptengine/script_gui.cpp b/src/scriptengine/script_gui.cpp index 62ccc2d02..60118cd71 100644 --- a/src/scriptengine/script_gui.cpp +++ b/src/scriptengine/script_gui.cpp @@ -174,6 +174,24 @@ namespace Scripting new(gen->GetAddressOfReturnLocation()) std::string(StringUtils::wide_to_utf8(out.c_str())); } + void scriptLogInfo(asIScriptGeneric *gen) + { + std::string input = *(std::string*)gen->GetArgAddress(0); + Log::info("Script", "%s", input.c_str()); + } + + void scriptLogWarning(asIScriptGeneric *gen) + { + std::string input = *(std::string*)gen->GetArgAddress(0); + Log::warn("Script", "%s", input.c_str()); + } + + void scriptLogError(asIScriptGeneric *gen) + { + std::string input = *(std::string*)gen->GetArgAddress(0); + Log::error("Script", "%s", input.c_str()); + } + void registerScriptFunctions(asIScriptEngine *engine) { int r; // of type asERetCodes @@ -185,13 +203,20 @@ namespace Scripting 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); } void registerScriptEnums(asIScriptEngine *engine) { + engine->SetDefaultNamespace("GUI"); engine->RegisterEnum("PA"); engine->RegisterEnumValue("PA", "STEER_LEFT", PA_STEER_LEFT); engine->RegisterEnumValue("PA", "STEER_RIGHT", PA_STEER_RIGHT); diff --git a/src/scriptengine/script_physics.cpp b/src/scriptengine/script_physics.cpp index 2dfabf7b4..893f32624 100644 --- a/src/scriptengine/script_physics.cpp +++ b/src/scriptengine/script_physics.cpp @@ -51,11 +51,6 @@ namespace Scripting void *pointer = &m_collider1; gen->SetReturnObject(pointer); } - void getCollisionType(asIScriptGeneric *gen) - { - void *pointer = &m_collisionType; - gen->SetReturnObject(pointer); - } //Callbacks from Physics Engine, for collisions void setCollision(int collider1,int collider2) @@ -68,24 +63,12 @@ namespace Scripting m_collider1 = collider1; m_collider2 = collider2; } - void setCollisionType(std::string collisionType) - { - m_collisionType = collisionType; - } - asIScriptFunction* registerScriptCallbacks(asIScriptEngine *engine) - { - asIScriptFunction *func; - std::string function_name = "void on" + m_collisionType + "Collision()"; - func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str()); - return func; - } void registerScriptFunctions(asIScriptEngine *engine) { int r; engine->SetDefaultNamespace("Physics"); r = engine->RegisterGlobalFunction("uint getCollidingKart1()", asFUNCTION(getCollidingKart1), asCALL_GENERIC); assert( r >= 0 ); r = engine->RegisterGlobalFunction("uint getCollidingKart2()", asFUNCTION(getCollidingKart2), asCALL_GENERIC); assert( r >= 0 ); - r = engine->RegisterGlobalFunction("string getCollisionType()", asFUNCTION(getCollisionType), asCALL_GENERIC); assert(r >= 0); r = engine->RegisterGlobalFunction("string getCollidingID()", asFUNCTION(getCollidingID), asCALL_GENERIC); assert(r >= 0); r = engine->RegisterGlobalFunction("string createExplosion(Vec3 &in)", asFUNCTION(createExplosion), asCALL_GENERIC); assert(r >= 0); } diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index 3b95f39af..090bc9e18 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -46,18 +46,6 @@ namespace Scripting func = engine->GetModule(0)->GetFunctionByDecl(function_name.c_str()); return func; } - asIScriptFunction* registerStartScriptCallbacks(asIScriptEngine *engine) - { - asIScriptFunction *func; - func = engine->GetModule(0)->GetFunctionByDecl("void onStart()"); - return func; - } - asIScriptFunction* registerUpdateScriptCallbacks(asIScriptEngine *engine) - { - asIScriptFunction *func; - func = engine->GetModule(0)->GetFunctionByDecl("void onUpdate()"); - return func; - } /* void disableAnimation(std::string *name, void *memory) { diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index 3645fe53a..49fe9b9bd 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -1486,7 +1486,7 @@ void Track::update(float dt) if (!m_startup_run) // first time running update = good point to run startup script { Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runScript("start"); + script_engine->runScript("onStart"); m_startup_run = true; } m_track_object_manager->update(dt); @@ -1498,7 +1498,7 @@ void Track::update(float dt) CheckManager::get()->update(dt); ItemManager::get()->update(dt); Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine(); - script_engine->runScript("update"); + script_engine->runScript("onUpdate"); } // update // ----------------------------------------------------------------------------