[8711] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[8408] | 3 | |
---|
[8711] | 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 | |
---|
[9006] | 16 | |
---|
[8171] | 17 | #include "script_trigger.h" |
---|
[8202] | 18 | #include "class_list.h" |
---|
| 19 | #include "script.h" |
---|
[8171] | 20 | |
---|
[8239] | 21 | #include "state.h" |
---|
[8408] | 22 | |
---|
| 23 | |
---|
[8783] | 24 | CREATE_SCRIPTABLE_CLASS(ScriptTrigger, CL_SCRIPT_TRIGGER, |
---|
[9061] | 25 | // Coordinates |
---|
| 26 | addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor)) |
---|
| 27 | ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX)) |
---|
| 28 | ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY)) |
---|
| 29 | ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ)) |
---|
| 30 | //Properties |
---|
[9193] | 31 | ->addMethod("setName", ExecutorLua1<BaseObject, const std::string&>(&BaseObject::setName)) |
---|
[9061] | 32 | ->addMethod("setTarget", ExecutorLua1<ScriptTrigger, const std::string&>(&ScriptTrigger::setTarget)) |
---|
| 33 | ->addMethod("setTriggerParent", ExecutorLua1<ScriptTrigger, const std::string&>(&ScriptTrigger::setTriggerParent)) |
---|
| 34 | ->addMethod("setTriggerLasts", ExecutorLua1<ScriptTrigger, bool>(&ScriptTrigger::setTriggerLasts)) |
---|
| 35 | ->addMethod("setInvert", ExecutorLua1<ScriptTrigger, bool>(&ScriptTrigger::setInvert)) |
---|
| 36 | ->addMethod("setRadius", ExecutorLua1<ScriptTrigger, float>(&ScriptTrigger::setRadius)) |
---|
| 37 | ->addMethod("setScript", ExecutorLua1<ScriptTrigger, const std::string&>(&ScriptTrigger::setScript)) |
---|
| 38 | ->addMethod("setFunction", ExecutorLua1<ScriptTrigger, const std::string&>(&ScriptTrigger::setFunction)) |
---|
| 39 | ->addMethod("setDebugDraw", ExecutorLua1<ScriptTrigger, bool>(&ScriptTrigger::setDebugDraw)) |
---|
| 40 | ->addMethod("setAddToScript", ExecutorLua1<ScriptTrigger, bool>(&ScriptTrigger::setAddToScript)) |
---|
| 41 | ); |
---|
[8408] | 42 | |
---|
[8783] | 43 | |
---|
[8408] | 44 | /** |
---|
| 45 | * Constructs a new ScriptTrigger. |
---|
| 46 | * @param root the xml element to load the parameters from. |
---|
| 47 | * |
---|
| 48 | */ |
---|
[8202] | 49 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
[8171] | 50 | { |
---|
[8408] | 51 | this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger"); |
---|
| 52 | this->toList(OM_COMMON); |
---|
| 53 | |
---|
[8711] | 54 | returnCount = 1; |
---|
[8894] | 55 | scriptFinished = false; |
---|
[8408] | 56 | doDebugDraw = false; |
---|
[8711] | 57 | invert = false; |
---|
[8202] | 58 | scriptCalled = false; |
---|
[8211] | 59 | scriptIsOk = false; |
---|
[8894] | 60 | triggerLasts = true; |
---|
[8783] | 61 | addToScript = false; |
---|
| 62 | |
---|
| 63 | if(root != NULL) |
---|
| 64 | { |
---|
| 65 | |
---|
[9006] | 66 | loadParams(root); |
---|
[8783] | 67 | |
---|
[9006] | 68 | if(addToScript && scriptIsOk) |
---|
| 69 | { |
---|
| 70 | script->addObject( "ScriptTrigger", this->getName()); |
---|
| 71 | } |
---|
[8783] | 72 | |
---|
| 73 | } |
---|
[8171] | 74 | } |
---|
| 75 | |
---|
[8408] | 76 | /** |
---|
| 77 | * Deletes the ScriptTrigger. |
---|
| 78 | * |
---|
| 79 | */ |
---|
[8171] | 80 | ScriptTrigger::~ScriptTrigger() |
---|
| 81 | { |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | |
---|
[8408] | 85 | /** |
---|
| 86 | * Reads the values from the tml element and sets them. |
---|
| 87 | * @param root the xml element to load the parameters from. |
---|
| 88 | * |
---|
| 89 | */ |
---|
[8171] | 90 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
| 91 | { |
---|
[8783] | 92 | |
---|
[9006] | 93 | WorldEntity ::loadParams(root); |
---|
[8211] | 94 | |
---|
[9006] | 95 | LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
| 96 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
| 97 | .defaultValues(""); |
---|
| 98 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
---|
| 99 | .describe("the function of the script, that should be triggered by this script trigger") |
---|
| 100 | .defaultValues(""); |
---|
| 101 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
| 102 | .describe("where this script trigger should be located") |
---|
| 103 | .defaultValues(""); |
---|
| 104 | LoadParam(root, "radius", this, ScriptTrigger, setRadius) |
---|
| 105 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
| 106 | .defaultValues(0); |
---|
| 107 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
| 108 | .describe("the delay after which the funtion sould be triggered") |
---|
| 109 | .defaultValues(0); |
---|
| 110 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
| 111 | .describe("The name of the target as it is in the *.oxw file") |
---|
| 112 | .defaultValues(""); |
---|
| 113 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
| 114 | .describe("The name of the parent as it is in the *.oxw file") |
---|
| 115 | .defaultValues(""); |
---|
| 116 | LoadParam(root, "invert", this, ScriptTrigger, setInvert) |
---|
| 117 | .describe("") |
---|
[9110] | 118 | .defaultValues(false); |
---|
[9006] | 119 | LoadParam(root, "triggerlasts", this, ScriptTrigger, setTriggerLasts) |
---|
| 120 | .describe("") |
---|
[9110] | 121 | .defaultValues(true); |
---|
[9006] | 122 | LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) |
---|
| 123 | .describe("") |
---|
[9110] | 124 | .defaultValues(false); |
---|
[9006] | 125 | LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript) |
---|
| 126 | .describe("True if this scripttrigger should be aviable in the script") |
---|
[9110] | 127 | .defaultValues(false); |
---|
[8171] | 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[8408] | 131 | /** |
---|
| 132 | * Sets the target(a world entity) of the ScriptTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered. |
---|
| 133 | * @param target The worldentity that the script supervises. |
---|
| 134 | */ |
---|
[8199] | 135 | void ScriptTrigger::setTarget(const std::string& target) |
---|
| 136 | { |
---|
| 137 | BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY); |
---|
[8211] | 138 | |
---|
[8199] | 139 | if (targetEntity != NULL) |
---|
| 140 | { |
---|
| 141 | this->setTarget(dynamic_cast<WorldEntity*>(targetEntity)); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
[8202] | 145 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName()); |
---|
[8199] | 146 | } |
---|
| 147 | } |
---|
[8171] | 148 | |
---|
[8408] | 149 | /** |
---|
| 150 | * Sets the parent of the trigger. |
---|
| 151 | * @param parent The parrent. |
---|
| 152 | */ |
---|
[8205] | 153 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
| 154 | { |
---|
| 155 | BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY); |
---|
[8211] | 156 | |
---|
[8205] | 157 | if (parentEntity != NULL) |
---|
| 158 | { |
---|
| 159 | this->setParent(dynamic_cast<WorldEntity*>(parentEntity)); |
---|
[8207] | 160 | this->setParentMode(PNODE_MOVEMENT); |
---|
[8205] | 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName()); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[8171] | 168 | void ScriptTrigger::tick(float timestep) |
---|
| 169 | { |
---|
[8894] | 170 | if(scriptFinished) return; |
---|
[8408] | 171 | |
---|
[8711] | 172 | if(triggerLasts && scriptCalled) |
---|
| 173 | { |
---|
| 174 | executeAction(timestep); |
---|
| 175 | return; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if( !invert && this->distance(target) < radius) |
---|
[9006] | 179 | { |
---|
[8711] | 180 | executeAction(timestep); |
---|
| 181 | scriptCalled = true; |
---|
| 182 | |
---|
[9006] | 183 | } |
---|
| 184 | else if( invert && this->distance(target) > radius) |
---|
| 185 | { |
---|
| 186 | executeAction(timestep); |
---|
| 187 | scriptCalled = true; |
---|
| 188 | } |
---|
[8408] | 189 | //else |
---|
| 190 | //printf("SCRIPTTRIGGER: target out of range\n"); |
---|
[8171] | 191 | |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
[8711] | 195 | void ScriptTrigger::executeAction(float timestep) |
---|
[8171] | 196 | { |
---|
[8894] | 197 | |
---|
[9006] | 198 | if(scriptIsOk) |
---|
| 199 | { |
---|
[8711] | 200 | //testScriptingFramework(); |
---|
[9006] | 201 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
---|
| 202 | printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[8711] | 203 | |
---|
[9006] | 204 | script->pushParam( timestep, this->functionName); |
---|
[8711] | 205 | |
---|
[9006] | 206 | if( !(script->executeFunction()) ) |
---|
| 207 | printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[8711] | 208 | |
---|
[9006] | 209 | scriptFinished = script->getReturnedBool(); |
---|
| 210 | } |
---|
[8711] | 211 | |
---|
| 212 | |
---|
[8171] | 213 | } |
---|
[8211] | 214 | |
---|
| 215 | |
---|
| 216 | void ScriptTrigger::setScript(const std::string& file) |
---|
| 217 | { |
---|
[8408] | 218 | |
---|
[8239] | 219 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
| 220 | if (scriptManager != NULL) |
---|
| 221 | { |
---|
[8408] | 222 | |
---|
[8239] | 223 | script = scriptManager->getScriptByFile(file); |
---|
| 224 | if(script != NULL) |
---|
[8408] | 225 | { |
---|
[8239] | 226 | scriptIsOk = true; |
---|
[8408] | 227 | } |
---|
[8239] | 228 | } |
---|
[8214] | 229 | } |
---|
[8243] | 230 | |
---|
[8711] | 231 | /* |
---|
[8243] | 232 | void ScriptTrigger::testScriptingFramework() |
---|
[9006] | 233 | { |
---|
[8243] | 234 | std::string file("lunartest2.lua"); |
---|
[8408] | 235 | //get script |
---|
[8246] | 236 | Script* script = State::getScriptManager()->getScriptByFile(file); |
---|
[8243] | 237 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
| 238 | |
---|
| 239 | //execute a function |
---|
| 240 | printf("----------- main -----------\n"); |
---|
| 241 | std::string main("main"); |
---|
| 242 | if( script->selectFunction(main,3)) |
---|
| 243 | printf("function %s selected\n",main.c_str()); |
---|
| 244 | |
---|
| 245 | script->pushParam(3.14159,main); |
---|
[8408] | 246 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
[8243] | 247 | script->executeFunction(); |
---|
| 248 | |
---|
[8408] | 249 | int ret = script->getReturnedInt(); |
---|
| 250 | printf("main returned %i\n",ret); |
---|
[8243] | 251 | |
---|
| 252 | if(script->getReturnedBool()) |
---|
| 253 | printf("main returned true\n"); |
---|
| 254 | else |
---|
| 255 | printf("main returned false\n"); |
---|
| 256 | |
---|
[8408] | 257 | float retf = script->getReturnedFloat(); |
---|
| 258 | printf("main returned %f\n",retf); |
---|
| 259 | |
---|
[8243] | 260 | |
---|
[8408] | 261 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
[8243] | 262 | //execute a 2nd function |
---|
| 263 | printf("----------- test -----------\n"); |
---|
| 264 | std::string test("test"); |
---|
| 265 | if( script->selectFunction(test,0)) |
---|
| 266 | printf("function %s selected\n",test.c_str()); |
---|
| 267 | |
---|
| 268 | script->executeFunction(); |
---|
| 269 | |
---|
| 270 | |
---|
| 271 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
---|
| 272 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
| 273 | |
---|
[8711] | 274 | }*/ |
---|