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 | radius = 10; |
---|
56 | returnCount = 1; |
---|
57 | scriptFinished = false; |
---|
58 | doDebugDraw = false; |
---|
59 | invert = false; |
---|
60 | scriptCalled = false; |
---|
61 | scriptIsOk = false; |
---|
62 | triggerRemains = false; |
---|
63 | addToScript = false; |
---|
64 | this->activeOnCreation = false; |
---|
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, "radius", this, ScriptTrigger, setRadius) |
---|
109 | .describe("the fileName of the script, that should be triggered by this script trigger") |
---|
110 | .defaultValues(0); |
---|
111 | LoadParam(root, "delay", this, ScriptTrigger, setDelay) |
---|
112 | .describe("the delay after which the funtion sould be triggered") |
---|
113 | .defaultValues(0); |
---|
114 | LoadParam(root, "worldentity", this, ScriptTrigger, setTarget) |
---|
115 | .describe("The name of the target as it is in the *.oxw file") |
---|
116 | .defaultValues(""); |
---|
117 | LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent) |
---|
118 | .describe("The name of the parent as it is in the *.oxw file") |
---|
119 | .defaultValues(""); |
---|
120 | LoadParam(root, "invert", this, ScriptTrigger, setInvert) |
---|
121 | .describe("") |
---|
122 | .defaultValues(false); |
---|
123 | LoadParam(root, "triggerRemains", this, ScriptTrigger, setTriggerRemains) |
---|
124 | .describe("") |
---|
125 | .defaultValues(true); |
---|
126 | LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) |
---|
127 | .describe("") |
---|
128 | .defaultValues(false); |
---|
129 | LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript) |
---|
130 | .describe("True if this scripttrigger should be aviable in the script") |
---|
131 | .defaultValues(false); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * 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. |
---|
137 | * @param target The worldentity that the script supervises. |
---|
138 | */ |
---|
139 | void ScriptTrigger::setTarget(const std::string& target) |
---|
140 | { |
---|
141 | |
---|
142 | WorldEntity* targetEntity = WorldEntity::objectList().getObject(target); |
---|
143 | if (targetEntity != NULL) |
---|
144 | { |
---|
145 | this->setTarget(targetEntity); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | PRINTF(2)("Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName()); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Sets the parent of the trigger. |
---|
155 | * @param parent The parrent. |
---|
156 | */ |
---|
157 | void ScriptTrigger::setTriggerParent(const std::string& parent) |
---|
158 | { |
---|
159 | WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent); |
---|
160 | |
---|
161 | if (parentEntity != NULL) |
---|
162 | { |
---|
163 | this->setParent(parentEntity); |
---|
164 | this->setParentMode(PNODE_MOVEMENT); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | PRINTF(2)("Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName()); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | void ScriptTrigger::tick(float timestep) |
---|
173 | { |
---|
174 | if(scriptFinished) return; |
---|
175 | |
---|
176 | if(activeOnCreation) |
---|
177 | { |
---|
178 | executeAction(timestep); |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | if(triggerRemains && scriptCalled) |
---|
183 | { |
---|
184 | executeAction(timestep); |
---|
185 | return; |
---|
186 | } |
---|
187 | |
---|
188 | if( !invert && this->distance(target) < radius) |
---|
189 | { |
---|
190 | //printf("Distance is %f \n", this->distance(target)); |
---|
191 | executeAction(timestep); |
---|
192 | scriptCalled = true; |
---|
193 | return; |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | if( invert && this->distance(target) > radius) |
---|
198 | { |
---|
199 | executeAction(timestep); |
---|
200 | scriptCalled = true; |
---|
201 | return; |
---|
202 | } |
---|
203 | //else |
---|
204 | //printf("SCRIPTTRIGGER: target out of range\n"); |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | void ScriptTrigger::executeAction(float timestep) |
---|
210 | { |
---|
211 | if(scriptIsOk) |
---|
212 | { |
---|
213 | //testScriptingFramework(); |
---|
214 | if(!(script->selectFunction(this->functionName,returnCount)) ) |
---|
215 | PRINT(1)("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
216 | |
---|
217 | script->pushParam( timestep, this->functionName); |
---|
218 | |
---|
219 | if( !(script->executeFunction()) ) |
---|
220 | PRINT(1)("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); |
---|
221 | |
---|
222 | scriptFinished = script->getReturnedBool(); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | void ScriptTrigger::setScript(const std::string& file) |
---|
230 | { |
---|
231 | ScriptManager* scriptManager = State::getScriptManager(); |
---|
232 | if (scriptManager != NULL) |
---|
233 | { |
---|
234 | |
---|
235 | script = scriptManager->getScriptByFile(file); |
---|
236 | if(script != NULL) |
---|
237 | { |
---|
238 | scriptIsOk = true; |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /* |
---|
244 | void ScriptTrigger::testScriptingFramework() |
---|
245 | { |
---|
246 | std::string file("lunartest2.lua"); |
---|
247 | //get script |
---|
248 | Script* script = State::getScriptManager()->getScriptByFile(file); |
---|
249 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
250 | |
---|
251 | //execute a function |
---|
252 | printf("----------- main -----------\n"); |
---|
253 | std::string main("main"); |
---|
254 | if( script->selectFunction(main,3)) |
---|
255 | printf("function %s selected\n",main.c_str()); |
---|
256 | |
---|
257 | script->pushParam(3.14159,main); |
---|
258 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
259 | script->executeFunction(); |
---|
260 | |
---|
261 | int ret = script->getReturnedInt(); |
---|
262 | printf("main returned %i\n",ret); |
---|
263 | |
---|
264 | if(script->getReturnedBool()) |
---|
265 | printf("main returned true\n"); |
---|
266 | else |
---|
267 | printf("main returned false\n"); |
---|
268 | |
---|
269 | float retf = script->getReturnedFloat(); |
---|
270 | printf("main returned %f\n",retf); |
---|
271 | |
---|
272 | |
---|
273 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
274 | //execute a 2nd function |
---|
275 | printf("----------- test -----------\n"); |
---|
276 | std::string test("test"); |
---|
277 | if( script->selectFunction(test,0)) |
---|
278 | printf("function %s selected\n",test.c_str()); |
---|
279 | |
---|
280 | script->executeFunction(); |
---|
281 | |
---|
282 | |
---|
283 | //if(argc>1) lua_dofile(script.getLuaState(), argv[1]); |
---|
284 | printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); |
---|
285 | |
---|
286 | }*/ |
---|