| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Silvan Nellen |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include "script_trigger.h" |
|---|
| 18 | #include "script.h" |
|---|
| 19 | |
|---|
| 20 | #include "state.h" |
|---|
| 21 | #include "debug.h" |
|---|
| 22 | ObjectListDefinition(ScriptTrigger); |
|---|
| 23 | |
|---|
| 24 | // CREATE_SCRIPTABLE_CLASS(ScriptTrigger, |
|---|
| 25 | // // Coordinates |
|---|
| 26 | // addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
|---|
| 27 | // ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
|---|
| 28 | // ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
|---|
| 29 | // ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
|---|
| 30 | // //Properties |
|---|
| 31 | // ->addMethod("setName", Executor1<BaseObject, lua_State*, const std::string&>(&BaseObject::setName)) |
|---|
| 32 | // ->addMethod("setTarget", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTarget)) |
|---|
| 33 | // ->addMethod("setTriggerParent", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setTriggerParent)) |
|---|
| 34 | // ->addMethod("setTriggerRemains", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setTriggerRemains)) |
|---|
| 35 | // ->addMethod("setActiveOnCreation", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setActiveOnCreation)) |
|---|
| 36 | // ->addMethod("setInvert", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setInvert)) |
|---|
| 37 | // ->addMethod("setRadius", Executor1<ScriptTrigger, lua_State*, float>(&ScriptTrigger::setRadius)) |
|---|
| 38 | // ->addMethod("setScript", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setScript)) |
|---|
| 39 | // ->addMethod("setFunction", Executor1<ScriptTrigger, lua_State*, const std::string&>(&ScriptTrigger::setFunction)) |
|---|
| 40 | // ->addMethod("setDebugDraw", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setDebugDraw)) |
|---|
| 41 | // ->addMethod("setAddToScript", Executor1<ScriptTrigger, lua_State*, bool>(&ScriptTrigger::setAddToScript)) |
|---|
| 42 | // ); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Constructs a new ScriptTrigger. |
|---|
| 47 | * @param root the xml element to load the parameters from. |
|---|
| 48 | * |
|---|
| 49 | */ |
|---|
| 50 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
|---|
| 51 | { |
|---|
| 52 | this->registerObject(this, ScriptTrigger::_objectList); |
|---|
| 53 | this->toList(OM_COMMON); |
|---|
| 54 | |
|---|
| 55 | returnCount = 1; |
|---|
| 56 | scriptFinished = false; |
|---|
| 57 | doDebugDraw = false; |
|---|
| 58 | |
|---|
| 59 | scriptCalled = false; |
|---|
| 60 | scriptIsOk = false; |
|---|
| 61 | executionStopped = false; // true when something goes wrong and the trigger has to be stopped |
|---|
| 62 | addToScript = false; |
|---|
| 63 | this->activeOnCreation = false; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | if(root != NULL) |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | loadParams(root); |
|---|
| 70 | |
|---|
| 71 | if(addToScript && scriptIsOk) |
|---|
| 72 | { |
|---|
| 73 | script->addObject( "ScriptTrigger", this->getName()); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Deletes the ScriptTrigger. |
|---|
| 82 | * |
|---|
| 83 | */ |
|---|
| 84 | ScriptTrigger::~ScriptTrigger() |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Reads the values from the tml element and sets them. |
|---|
| 91 | * @param root the xml element to load the parameters from. |
|---|
| 92 | * |
|---|
| 93 | */ |
|---|
| 94 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | WorldEntity ::loadParams(root); |
|---|
| 98 | |
|---|
| 99 | LoadParam(root, "file", this, ScriptTrigger, setScript) |
|---|
| 100 | .describe("the fileName of the script, that should be triggered by this script trigger") |
|---|
| 101 | .defaultValues(""); |
|---|
| 102 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
|---|
| 103 | .describe("the function of the script, that should be triggered by this script trigger") |
|---|
| 104 | .defaultValues(""); |
|---|
| 105 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
|---|
| 106 | .describe("where this script trigger should be located") |
|---|
| 107 | .defaultValues(""); |
|---|
| 108 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
|---|
| 109 | .describe("the delay after which the funtion sould be triggered") |
|---|
| 110 | .defaultValues(0); |
|---|
| 111 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
|---|
| 112 | .describe("The name of the parent as it is in the *.oxw file") |
|---|
| 113 | .defaultValues(""); |
|---|
| 114 | LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) |
|---|
| 115 | .describe("") |
|---|
| 116 | .defaultValues(false); |
|---|
| 117 | LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript) |
|---|
| 118 | .describe("True if this scripttrigger should be aviable in the script") |
|---|
| 119 | .defaultValues(false); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Sets the parent of the trigger. |
|---|
| 125 | * @param parent The parent. |
|---|
| 126 | */ |
|---|
| 127 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
|---|
| 128 | { |
|---|
| 129 | WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent); |
|---|
| 130 | |
|---|
| 131 | if (parentEntity != NULL) |
|---|
| 132 | { |
|---|
| 133 | this->setParent(parentEntity); |
|---|
| 134 | this->setParentMode(PNODE_MOVEMENT); |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | { |
|---|
| 138 | PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName()); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | void ScriptTrigger::tick(float timestep) |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | void ScriptTrigger::executeAction(float timestep) |
|---|
| 149 | { |
|---|
| 150 | if(executionStopped && scriptIsOk) // If the script has been loaded correctly but something is wrong with the settings of the trigger |
|---|
| 151 | { |
|---|
| 152 | PRINT(1)("ERROR SCRTIPTTRIGGER: Something went wrong while executing %s in %s . Execution stopped! \n", functionName.c_str(), script->getFileName().c_str()); |
|---|
| 153 | return; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if(scriptIsOk) |
|---|
| 157 | { |
|---|
| 158 | //testScriptingFramework(); |
|---|
| 159 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
|---|
| 160 | { |
|---|
| 161 | PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
|---|
| 162 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
|---|
| 163 | return; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (! (script->pushParam( timestep, this->functionName)) ) |
|---|
| 167 | { |
|---|
| 168 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
|---|
| 169 | return; |
|---|
| 170 | } |
|---|
| 171 | if( !(script->executeFunction()) ) |
|---|
| 172 | { |
|---|
| 173 | PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
|---|
| 174 | executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again. |
|---|
| 175 | return; |
|---|
| 176 | } |
|---|
| 177 | scriptFinished = script->getReturnedBool(); |
|---|
| 178 | } |
|---|
| 179 | else |
|---|
| 180 | printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n"); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | void ScriptTrigger::setScript(const std::string& file) |
|---|
| 187 | { |
|---|
| 188 | ScriptManager* scriptManager = State::getScriptManager(); |
|---|
| 189 | if (scriptManager != NULL) |
|---|
| 190 | { |
|---|
| 191 | |
|---|
| 192 | script = scriptManager->getScriptByFile(file); |
|---|
| 193 | if(script != NULL) |
|---|
| 194 | { |
|---|
| 195 | scriptIsOk = true; |
|---|
| 196 | } |
|---|
| 197 | else |
|---|
| 198 | printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str()); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /* |
|---|
| 203 | void ScriptTrigger::testScriptingFramework() |
|---|
| 204 | { |
|---|
| 205 | std::string file("lunartest2.lua"); |
|---|
| 206 | //get script |
|---|
| 207 | Script* script = State::getScriptManager()->getScriptByFile(file); |
|---|
| 208 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
|---|
| 209 | |
|---|
| 210 | //execute a function |
|---|
| 211 | printf("----------- main -----------\n"); |
|---|
| 212 | std::string main("main"); |
|---|
| 213 | if( script->selectFunction(main,3)) |
|---|
| 214 | printf("function %s selected\n",main.c_str()); |
|---|
| 215 | |
|---|
| 216 | script->pushParam(3.14159,main); |
|---|
| 217 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
|---|
| 218 | script->executeFunction(); |
|---|
| 219 | |
|---|
| 220 | int ret = script->getReturnedInt(); |
|---|
| 221 | printf("main returned %i\n",ret); |
|---|
| 222 | |
|---|
| 223 | if(script->getReturnedBool()) |
|---|
| 224 | printf("main returned true\n"); |
|---|
| 225 | else |
|---|
| 226 | printf("main returned false\n"); |
|---|
| 227 | |
|---|
| 228 | float retf = script->getReturnedFloat(); |
|---|
| 229 | printf("main returned %f\n",retf); |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
|---|
| 233 | //execute a 2nd function |
|---|
| 234 | printf("----------- test -----------\n"); |
|---|
| 235 | std::string test("test"); |
|---|
| 236 | if( script->selectFunction(test,0)) |
|---|
| 237 | printf("function %s selected\n",test.c_str()); |
|---|
| 238 | |
|---|
| 239 | script->executeFunction(); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
|---|
| 243 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
|---|
| 244 | |
|---|
| 245 | }*/ |
|---|