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