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