[8246] | 1 | //for testing |
---|
| 2 | #include "luaincl.h" |
---|
[8171] | 3 | #include "script_trigger.h" |
---|
[8202] | 4 | #include "class_list.h" |
---|
| 5 | #include "script.h" |
---|
[8171] | 6 | |
---|
[8239] | 7 | #include "state.h" |
---|
[8246] | 8 | |
---|
| 9 | |
---|
| 10 | |
---|
[8202] | 11 | ScriptTrigger::ScriptTrigger(const TiXmlElement* root) |
---|
[8171] | 12 | { |
---|
[8202] | 13 | scriptCalled = false; |
---|
[8211] | 14 | scriptIsOk = false; |
---|
[8202] | 15 | loadParams(root); |
---|
| 16 | |
---|
| 17 | |
---|
[8171] | 18 | } |
---|
| 19 | |
---|
| 20 | ScriptTrigger::~ScriptTrigger() |
---|
| 21 | { |
---|
| 22 | |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | void ScriptTrigger::loadParams(const TiXmlElement* root) |
---|
| 26 | { |
---|
[8207] | 27 | if(root != NULL) |
---|
| 28 | { |
---|
[8212] | 29 | WorldEntity ::loadParams(root); |
---|
[8211] | 30 | |
---|
[8212] | 31 | LoadParam(root, "file", this, ScriptTrigger, setScript) |
---|
[8171] | 32 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
| 33 | .defaultValues(""); |
---|
| 34 | LoadParam(root, "function", this, ScriptTrigger, setFunction) |
---|
| 35 | .describe("the function of the script, that should be triggered by this script trigger") |
---|
| 36 | .defaultValues(""); |
---|
[8205] | 37 | LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor) |
---|
[8178] | 38 | .describe("where this script trigger should be located") |
---|
[8171] | 39 | .defaultValues(""); |
---|
| 40 | LoadParam(root, "radius", this, ScriptTrigger, setRadius) |
---|
| 41 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
[8172] | 42 | .defaultValues(0); |
---|
| 43 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
| 44 | .describe("the delay after which the funtion sould be triggered") |
---|
| 45 | .defaultValues(0); |
---|
[8205] | 46 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
[8178] | 47 | .describe("The name of the target as it is in the *.oxw file") |
---|
| 48 | .defaultValues(""); |
---|
[8205] | 49 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
[8208] | 50 | .describe("The name of the parent as it is in the *.oxw file") |
---|
[8178] | 51 | .defaultValues(""); |
---|
[8259] | 52 | LoadParam(root, "callonce", this, ScriptTrigger, setCallOnce) |
---|
| 53 | .describe("True if the script shoul only be called once") |
---|
| 54 | .defaultValues(""); |
---|
[8207] | 55 | } |
---|
[8211] | 56 | |
---|
[8171] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
[8199] | 60 | void ScriptTrigger::setTarget(const std::string& target) |
---|
| 61 | { |
---|
| 62 | BaseObject* targetEntity = ClassList::getObject(target, CL_WORLD_ENTITY); |
---|
[8211] | 63 | |
---|
[8199] | 64 | if (targetEntity != NULL) |
---|
| 65 | { |
---|
| 66 | this->setTarget(dynamic_cast<WorldEntity*>(targetEntity)); |
---|
| 67 | } |
---|
| 68 | else |
---|
| 69 | { |
---|
[8202] | 70 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassName(), this->getName()); |
---|
[8199] | 71 | } |
---|
| 72 | } |
---|
[8171] | 73 | |
---|
[8205] | 74 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
| 75 | { |
---|
| 76 | BaseObject* parentEntity = ClassList::getObject(parent, CL_WORLD_ENTITY); |
---|
[8211] | 77 | |
---|
[8205] | 78 | if (parentEntity != NULL) |
---|
| 79 | { |
---|
| 80 | this->setParent(dynamic_cast<WorldEntity*>(parentEntity)); |
---|
[8207] | 81 | this->setParentMode(PNODE_MOVEMENT); |
---|
[8205] | 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassName(), this->getName()); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[8171] | 89 | void ScriptTrigger::tick(float timestep) |
---|
| 90 | { |
---|
[8265] | 91 | printf("SCRIPTTRIGGER: tick called\n"); |
---|
[8171] | 92 | if((this->getAbsDirV()-target->getAbsDirV()).len() < radius) |
---|
| 93 | { |
---|
[8178] | 94 | if(!callOnce) |
---|
[8171] | 95 | { |
---|
| 96 | executeAction(); |
---|
| 97 | } |
---|
[8178] | 98 | else if(callOnce && !scriptCalled) |
---|
[8171] | 99 | { |
---|
| 100 | executeAction(); |
---|
| 101 | scriptCalled = true; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | void ScriptTrigger::executeAction() |
---|
| 109 | { |
---|
[8211] | 110 | if(scriptIsOk) |
---|
[8207] | 111 | { |
---|
[8259] | 112 | testScriptingFramework(); |
---|
[8171] | 113 | if(!(script->selectFunction(this->functionName,0)) ) |
---|
[8212] | 114 | printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[8171] | 115 | if( !(script->executeFunction()) ) |
---|
[8212] | 116 | printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
[8207] | 117 | } |
---|
[8171] | 118 | } |
---|
[8211] | 119 | |
---|
| 120 | |
---|
| 121 | void ScriptTrigger::setScript(const std::string& file) |
---|
| 122 | { |
---|
[8239] | 123 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
| 124 | if (scriptManager != NULL) |
---|
| 125 | { |
---|
| 126 | script = scriptManager->getScriptByFile(file); |
---|
| 127 | if(script != NULL) |
---|
| 128 | scriptIsOk = true; |
---|
| 129 | } |
---|
[8214] | 130 | } |
---|
[8243] | 131 | |
---|
| 132 | |
---|
| 133 | void ScriptTrigger::testScriptingFramework() |
---|
| 134 | { |
---|
| 135 | std::string file("lunartest2.lua"); |
---|
| 136 | // Script script; |
---|
[8246] | 137 | Script* script = State::getScriptManager()->getScriptByFile(file); |
---|
[8243] | 138 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
| 139 | |
---|
| 140 | // Register classes with the script |
---|
| 141 | // Lunar<Account>::Register(&script); |
---|
| 142 | //Lunar<Object>::Register(&script); |
---|
| 143 | |
---|
| 144 | //Object* obj= new Object(); |
---|
| 145 | //Account a; |
---|
| 146 | //Account *b = new Account(30); |
---|
| 147 | |
---|
| 148 | // Add instances to the script |
---|
| 149 | //Lunar<Object>::insertObject(&script,obj,"Obj",true); |
---|
| 150 | //Lunar<Account>::insertObject(&script,&a,"a",false); |
---|
| 151 | //Lunar<Account>::insertObject(&script,b,"b",true); |
---|
| 152 | //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
| 153 | |
---|
| 154 | //Load a file |
---|
| 155 | //std::string file("lunartest2.lua"); |
---|
| 156 | |
---|
| 157 | //if(script.loadFile(file)) |
---|
| 158 | //printf("File %s succefully loaded\n", file.c_str()); |
---|
| 159 | //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
| 160 | |
---|
| 161 | //execute a function |
---|
| 162 | printf("----------- main -----------\n"); |
---|
| 163 | std::string main("main"); |
---|
| 164 | if( script->selectFunction(main,3)) |
---|
| 165 | printf("function %s selected\n",main.c_str()); |
---|
| 166 | |
---|
| 167 | script->pushParam(3.14159,main); |
---|
| 168 | script->executeFunction(); |
---|
| 169 | |
---|
| 170 | float retf = script->getReturnedFloat(); |
---|
| 171 | printf("main returned %f\n",retf); |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | if(script->getReturnedBool()) |
---|
| 175 | printf("main returned true\n"); |
---|
| 176 | else |
---|
| 177 | printf("main returned false\n"); |
---|
| 178 | |
---|
| 179 | int ret = script->getReturnedInt(); |
---|
| 180 | printf("main returned %i\n",ret); |
---|
| 181 | |
---|
| 182 | //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); |
---|
| 183 | //execute a 2nd function |
---|
| 184 | printf("----------- test -----------\n"); |
---|
| 185 | std::string test("test"); |
---|
| 186 | if( script->selectFunction(test,0)) |
---|
| 187 | printf("function %s selected\n",test.c_str()); |
---|
| 188 | |
---|
| 189 | script->executeFunction(); |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
---|
| 193 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
| 194 | |
---|
| 195 | } |
---|