Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/script.cc @ 9298

Last change on this file since 9298 was 9298, checked in by bensch, 18 years ago

orxonox/trunk: merged the branche scripting back here.

merged with command:
svn merge -r9239:HEAD https://svn.orxonox.net/orxonox/branches/scripting .
no conflicts

File size: 10.6 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: Benjamin Grauer
14*/
15
16#include "script.h"
17#include "script_class.h"
18#include "luaincl.h"
19#include "debug.h"
20#include "util/loading/resource_manager.h"
21
22#include "loading/load_param.h"
23#include "parser/tinyxml/tinyxml.h"
24
25#include "class_list.h"
26CREATE_SCRIPTABLE_CLASS(Script, CL_SCRIPT,
27                    addMethod("addObject", ExecutorLua2<Script,const std::string&, const std::string& >(&Script::addObject))
28                    ->addMethod("registerClass", ExecutorLua1<Script,const std::string&>(&Script::registerClass))
29                    ->addMethod("selectFunction", ExecutorLua2ret<Script, bool, const std::string&, int >(&Script::selectFunction))
30                    ->addMethod("executeFunction", ExecutorLua0ret<Script,bool >(&Script::executeFunction))
31                     );
32
33Script::Script(const TiXmlElement* root)
34{
35  this->setClassID(CL_SCRIPT, "Script");
36
37  returnCount = argumentCount = 0;
38
39  luaState = lua_open();
40
41  luaopen_base(luaState);
42  luaopen_table(luaState);
43  luaopen_io(luaState);
44  luaopen_string(luaState);
45  luaopen_math(luaState);
46  luaopen_debug(luaState);
47  if (root != NULL)
48    this->loadParams(root);
49}
50
51
52Script::Script(const std::string& filename)
53{
54  this->setClassID(CL_SCRIPT, "Script");
55
56  returnCount = argumentCount = 0;
57
58  luaState = lua_open();
59
60  luaopen_base(luaState);
61  luaopen_table(luaState);
62  luaopen_io(luaState);
63  luaopen_string(luaState);
64  luaopen_math(luaState);
65  luaopen_debug(luaState);
66 
67  this->loadFile(filename);
68
69}
70
71Script::~Script()
72{
73  lua_setgcthreshold(luaState, 0);  // collected garbage
74  lua_close(luaState);
75}
76
77
78void Script::loadParams(const TiXmlElement* root)
79{
80  //printf(("Loading params for %p \n",this);
81  BaseObject::loadParams(root);
82
83 LOAD_PARAM_START_CYCLE(root, object);
84  {
85    LoadParam_CYCLE(object, "object", this, Script, addObject)
86       .describe("The name of an object that is needed by a script");
87  }
88 LOAD_PARAM_END_CYCLE(object);
89
90
91  LoadParam(root, "file", this, Script, loadFileNoRet)
92      .describe("the fileName of the script, that should be loaded into this world")
93      .defaultValues("");
94}
95
96
97
98bool Script::loadFile(const std::string& filename)
99 {
100   std::string filedest(ResourceManager::getInstance()->getDataDir());
101   filedest += "scripts/" + filename;
102   
103   this->addThisScript();
104   this->registerStandartClasses();
105   
106   if(currentFile.length() != 0)
107   {
108     printf("SCRIPT %s : ERROR: Could not load %s because an other file is already loaded: %s\n",currentFile.c_str(),filename.c_str(), currentFile.c_str());
109     return false;
110    }
111
112   int error = luaL_loadfile (luaState, filedest.c_str());
113
114   if(error == 0)
115   {
116     currentFile = filename;
117     error = lua_pcall(luaState, 0, 0, 0);
118
119     if(error == 0)
120     {
121      return true;
122     }
123     else
124     {
125       printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str());
126       reportError(error);
127     }
128
129   }
130   else
131   {
132     printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str());
133     reportError(error);
134   }
135
136   return false;
137 }
138
139
140 void Script::addObject(const std::string& className, const std::string& objectName)
141 {
142   //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str());
143
144   BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);
145  // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass);
146   WorldObject tmpObj;
147   if (scriptClass != NULL)
148   {
149     tmpObj.type = className;
150     if( !classIsRegistered(className) )
151     {
152     static_cast<ScriptClass*>(scriptClass)->registerClass(this);
153     }
154
155     BaseObject* object = ClassList::getObject(objectName, className);
156    // printf(("%s is at %p \n",objectName.c_str(),object);
157     if (object != NULL && !objectIsAdded(objectName))
158     {
159        static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false);
160        tmpObj.name = objectName;
161        registeredObjects.push_back(tmpObj);
162     }
163   }
164 }
165
166
167
168
169 bool Script::executeFile()
170 {
171   PRINT(2)("WARNING: script.executeFile is not implemented yet\n");
172   /*if(currentFile.length() != 0)
173   {
174    int error = lua_pcall(luaState, 0, 0, 0);
175    if( error == 0)
176      return true;
177     else
178      {
179       reportError(error);
180       return false;
181      }
182 }*/
183   return false;
184 }
185
186 bool Script::selectFunction(const std::string& functionName, int retCount)
187 {
188   if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected
189   {
190   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
191   lua_gettable(luaState, LUA_GLOBALSINDEX);
192
193   if(lua_isfunction( luaState , -1))
194   {
195     returnCount = retCount;
196     argumentCount = 0;
197     currentFunction = functionName;
198     return true;
199   }
200   else
201    return false;
202   }
203   else
204     printf("SCRIPT %s : ERROR: There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFile.c_str(),currentFunction.c_str());
205   return false;
206 }
207
208 //return number of returned values
209 bool Script::executeFunction()
210 {
211   if(currentFunction.length() != 0 )
212   {
213    int error = lua_pcall(luaState, argumentCount, returnCount,0);
214    if(error != 0)
215    {
216      printf("SCRIPT %s : ERROR: Failed to execute function %s: \n",currentFile.c_str(),currentFunction.c_str());
217     reportError(error);
218     //clean up
219     currentFunction.assign("");
220     argumentCount = returnCount = 0;
221     return false;
222    }
223    else
224    {
225      currentFunction.assign("");//a function gets unusable after beeing called for the first time
226      argumentCount = 0;
227      return true;
228    }
229   }
230   else
231     printf("SCRIPT %s : ERROR: no function selected.\n",currentFile.c_str());
232
233   return false;
234 }
235
236
237 //overload this function to add different types
238 bool Script::pushParam(int param, std::string& toFunction)
239 {
240   if(currentFunction == toFunction)
241   {
242     lua_pushnumber(luaState, (lua_Number) param);
243     argumentCount++;
244     return true;
245   }
246   else
247   {
248     printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str());
249    return false;
250   }
251
252 }
253
254
255 bool Script::pushParam(float param, std::string& toFunction)
256 {
257   if(currentFunction.compare(toFunction) == 0)
258   {
259     lua_pushnumber(luaState,(lua_Number) param);
260     argumentCount++;
261     return true;
262   }
263   else
264   {
265     printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str());
266     return false;
267   }
268
269 }
270
271 bool Script::pushParam(double param, std::string& toFunction)
272 {
273   if(currentFunction.compare(toFunction) == 0)
274   {
275     lua_pushnumber(luaState,(lua_Number) param);
276     argumentCount++;
277     return true;
278   }
279   else
280   {
281     printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str());
282     return false;
283   }
284
285 }
286
287 int Script::getReturnedInt()
288 {
289   int returnValue = 0;
290   if(returnCount > 0)
291   {
292     if(lua_isnumber(luaState, -1*returnCount))
293     {
294       returnValue = (int)lua_tonumber(luaState, -1*returnCount);
295       lua_remove(luaState,-1*returnCount);
296       returnCount--;
297       
298     }
299   }
300   return returnValue;
301 }
302
303
304 bool Script::getReturnedBool()
305 {
306   bool returnValue = false;
307   if(returnCount > 0)
308   {
309     if(lua_isboolean(luaState, -1*returnCount))
310     {
311       returnValue = (bool)lua_toboolean(luaState, -1*returnCount);
312       lua_remove(luaState,-1*returnCount);
313       returnCount--;
314     }
315     else
316       printf("SCRIPT %s : ERROR: Trying to retreive non bolean value\n",this->currentFile.c_str());
317   }
318   return returnValue;
319 }
320
321float Script::getReturnedFloat()
322 {
323   float returnValue = 0.0f;
324   if(returnCount > 0)
325   {
326     if(lua_isnumber(luaState, -1*returnCount))
327     {
328       returnValue = (float)lua_tonumber(luaState, -1*returnCount);
329       lua_remove(luaState,-1*returnCount);
330       returnCount--;
331     }
332   }
333   return returnValue;
334 }
335
336 void Script::getReturnedString(std::string& string)
337 {
338   const char* returnValue = "";
339   if(returnCount > 0)
340   {
341     if(lua_isstring(luaState, -1*returnCount))
342     {
343       returnValue = lua_tostring(luaState, -1*returnCount);
344       lua_remove(luaState,-1*returnCount);
345       returnCount--;
346     }
347   }
348  string.assign(returnValue);
349 }
350
351
352void Script::addThisScript()
353{
354  BaseObject* scriptClass = ClassList::getObject("Script", CL_SCRIPT_CLASS);
355   if (scriptClass != NULL)
356   {
357     static_cast<ScriptClass*>(scriptClass)->registerClass(this);
358     static_cast<ScriptClass*>(scriptClass)->insertObject(this, this,"thisscript", false);
359   }
360}
361
362 int Script::reportError(int error)
363 {
364 if(error != 0)
365 {
366  const char *msg = lua_tostring(luaState, -1);
367  if (msg == NULL) msg = "(error with no message)\n";
368  printf("ERROR: %s\n", msg);
369  lua_pop(luaState, 1);
370 }
371  return error;
372 }
373
374 bool Script::registerStandartClasses()
375 {
376   bool success = false;
377   
378   //this->registerClass(std::string("Vector"));
379    this->registerClass("ScriptTrigger");
380  //  this->registerClass("AttractorMine");
381
382   return success;
383 }
384 
385 
386 void Script::registerClass( const std::string& className)
387 {
388   BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);
389   //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass);
390   WorldObject tmpObj;
391   if (scriptClass != NULL)
392   {
393     tmpObj.type = className;
394     if( !classIsRegistered(className) )
395     {
396       static_cast<ScriptClass*>(scriptClass)->registerClass(this);
397       tmpObj.name = "";
398       registeredObjects.push_back(tmpObj);
399       return;
400     }
401   }
402 
403 }
404
405 bool Script::classIsRegistered(const std::string& type)
406 {
407   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
408   {
409     if( (*it).type == type)
410     {
411       return true;
412     }
413   }
414   return false;
415 }
416
417
418
419 bool Script::objectIsAdded(const std::string& name)
420 {
421   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
422   {
423     if( (*it).name == name)
424     {
425       return true;
426     }
427   }
428   return false;
429
430
431 }
Note: See TracBrowser for help on using the repository browser.