Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptimprovements/src/world_entities/script_triggers/script_trigger.cc @ 10606

Last change on this file since 10606 was 10606, checked in by snellen, 17 years ago

moved scripttrigger to the subfolder script_triggers/ in world_entities

File size: 9.7 KB
Line 
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"
22ObjectListDefinition(ScriptTrigger);
23
24CREATE_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 */
50ScriptTrigger::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  executionStopped = false; // true when something goes wrong and the trigger has to be stopped
63  triggerRemains = true;
64  addToScript = false;
65  this->activeOnCreation = false;
66  target = NULL;
67
68  if(root != NULL)
69  {
70
71    loadParams(root);
72
73    if(addToScript && scriptIsOk)
74    {
75      script->addObject( "ScriptTrigger", this->getName());
76    }
77
78  }
79
80}
81
82/**
83 * Deletes the ScriptTrigger.
84 *
85 */
86ScriptTrigger::~ScriptTrigger()
87{
88
89}
90
91/**
92 * Reads the values from the tml element and sets them.
93 * @param root the xml element to load the parameters from.
94 *
95 */
96void ScriptTrigger::loadParams(const TiXmlElement* root)
97{
98
99  WorldEntity ::loadParams(root);
100
101  LoadParam(root, "file", this, ScriptTrigger, setScript)
102      .describe("the fileName of the script, that should be triggered by this script trigger")
103      .defaultValues("");
104  LoadParam(root, "function", this, ScriptTrigger, setFunction)
105      .describe("the function of the script, that should be triggered by this script trigger")
106      .defaultValues("");
107  LoadParam(root, "abs-coor", this, ScriptTrigger, setAbsCoor)
108      .describe("where this script trigger should be located")
109      .defaultValues("");
110  LoadParam(root, "radius", this, ScriptTrigger, setRadius)
111      .describe("the fileName of the script, that should be triggered by this script trigger")
112      .defaultValues(0);
113  LoadParam(root, "delay", this, ScriptTrigger, setDelay)
114      .describe("the delay after which the funtion sould be triggered")
115      .defaultValues(0);
116  LoadParam(root, "worldentity", this, ScriptTrigger, setTarget)
117      .describe("The name of the target as it is in the *.oxw file")
118      .defaultValues("");
119  LoadParam(root, "triggerparent", this, ScriptTrigger, setTriggerParent)
120      .describe("The name of the parent as it is in the *.oxw file")
121      .defaultValues("");
122  LoadParam(root, "invert", this, ScriptTrigger, setInvert)
123      .describe("")
124      .defaultValues(false);
125  LoadParam(root, "triggerRemains", this, ScriptTrigger, setTriggerRemains)
126      .describe("")
127      .defaultValues(true);
128  LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw)
129      .describe("")
130      .defaultValues(false);
131  LoadParam(root, "addtoscript", this, ScriptTrigger, setAddToScript)
132      .describe("True if this scripttrigger should be aviable in the script")
133      .defaultValues(false);
134}
135
136
137/**
138 * 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.
139 * @param target The worldentity that the script supervises.
140 */
141void ScriptTrigger::setTarget(const std::string& target)
142{
143
144  WorldEntity* targetEntity = WorldEntity::objectList().getObject(target);
145  if (targetEntity != NULL)
146  {
147    this->setTarget(targetEntity);
148  }
149  else
150  {
151    PRINTF(2)("ERROR SCRTIPTTRIGGER : Target %s for %s::%s does not Exist\n", target.c_str(), this->getClassCName(), this->getCName());
152  }
153}
154
155/**
156 * Sets the parent of the trigger.
157 * @param parent The parrent.
158 */
159void ScriptTrigger::setTriggerParent(const std::string& parent)
160{
161  WorldEntity* parentEntity = WorldEntity::objectList().getObject(parent);
162
163  if (parentEntity != NULL)
164  {
165    this->setParent(parentEntity);
166    this->setParentMode(PNODE_MOVEMENT);
167  }
168  else
169  {
170    PRINTF(2)("ERROR SCRTIPTTRIGGER : Parent %s for %s::%s does not Exist\n", parent.c_str(), this->getClassCName(), this->getCName());
171  }
172}
173
174void ScriptTrigger::tick(float timestep)
175{
176  if(scriptFinished) return;
177
178  if(activeOnCreation)
179   {
180     executeAction(timestep);
181     return;
182   }
183
184 if(triggerRemains && scriptCalled)
185  {
186    executeAction(timestep);
187    return;
188  }
189
190  if( this->target != NULL)
191  {
192    if( !invert && this->distance(target) < radius)
193    {
194    //printf("Distance is %f \n", this->distance(target));
195    executeAction(timestep);
196    scriptCalled = true;
197    return;
198
199    }
200    else if( invert && this->distance(target) > radius)
201    {
202      executeAction(timestep);
203      scriptCalled = true;
204      return;
205    }
206  }
207}
208
209
210void ScriptTrigger::executeAction(float timestep)
211{
212  if(executionStopped && scriptIsOk) // If the script has been loaded correctly but something is wrong with the settings of the trigger
213  {
214   PRINT(1)("ERROR SCRTIPTTRIGGER: Something went wrong while executing %s in %s . Execution stopped! \n", functionName.c_str(), script->getFileName().c_str());
215   return;
216  }
217
218  if(scriptIsOk)
219  {
220       //testScriptingFramework();
221    if(!(script->selectFunction(this->functionName,returnCount)) )
222     {
223      PRINT(1)("ERROR SCRTIPTTRIGGER : Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
224      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
225      return;
226     }
227
228    if (! (script->pushParam( timestep, this->functionName)) )
229     {
230      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
231      return;
232     }
233    if( !(script->executeFunction()) )
234     {
235      PRINT(1)("ERROR SCRTIPTTRIGGER : Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());
236      executionStopped = true; //Since the triggersettings won't change on runtime, it makes no sense to call the function again.
237      return;
238     }
239    scriptFinished = script->getReturnedBool();
240  }
241  else
242  printf("ERROR SCRTIPTTRIGGER : Script could not be executed !\n");
243
244
245}
246
247
248void ScriptTrigger::setScript(const std::string& file)
249{
250  ScriptManager* scriptManager = State::getScriptManager();
251  if (scriptManager != NULL)
252  {
253
254    script = scriptManager->getScriptByFile(file);
255    if(script != NULL)
256    {
257      scriptIsOk = true;
258    }
259    else
260    printf("ERROR SCRTIPTTRIGGER : Could not find the wrapperobject of %s , the script won't be executed ! \n", file.c_str());
261  }
262}
263
264/*
265 void ScriptTrigger::testScriptingFramework()
266{
267   std::string file("lunartest2.lua");
268   //get script
269   Script* script = State::getScriptManager()->getScriptByFile(file);
270   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
271
272      //execute a function
273   printf("----------- main -----------\n");
274   std::string main("main");
275   if( script->selectFunction(main,3))
276     printf("function %s selected\n",main.c_str());
277
278   script->pushParam(3.14159,main);
279   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
280   script->executeFunction();
281
282   int ret = script->getReturnedInt();
283   printf("main returned %i\n",ret);
284
285   if(script->getReturnedBool())
286     printf("main returned true\n");
287   else
288     printf("main returned false\n");
289
290   float retf = script->getReturnedFloat();
291   printf("main returned %f\n",retf);
292
293
294   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
295      //execute a 2nd function
296   printf("----------- test -----------\n");
297   std::string test("test");
298   if( script->selectFunction(test,0))
299     printf("function %s selected\n",test.c_str());
300
301   script->executeFunction();
302
303
304      //if(argc>1) lua_dofile(script.getLuaState(), argv[1]);
305   printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState()));
306
307}*/
Note: See TracBrowser for help on using the repository browser.