Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptchanges.new/src/world_entities/script_trigger.cc @ 10378

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

added some conclusive errormessages in script.cc and script_trigger.cc

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