Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/script.cc @ 8289

Last change on this file since 8289 was 8289, checked in by snellen, 18 years ago

added some debug output, script gets called

File size: 7.2 KB
RevLine 
[8075]1#include "script.h"
[8202]2#include "script_class.h"
3#include "luaincl.h"
[8197]4
[8202]5
[8193]6#include "loading/load_param.h"
[8197]7#include "parser/tinyxml/tinyxml.h"
[8075]8
[8197]9#include "class_list.h"
[8075]10
[8193]11Script::Script(const TiXmlElement* root)
[8075]12{
[8193]13  this->setClassID(CL_SCRIPT, "Script");
[8231]14
[8075]15  returnCount = argumentCount = 0;
16
17  luaState = lua_open();
18
19  luaopen_base(luaState);
20  luaopen_table(luaState);
21  luaopen_io(luaState);
22  luaopen_string(luaState);
23  luaopen_math(luaState);
24  luaopen_debug(luaState);
[8193]25  if (root != NULL)
26    this->loadParams(root);
[8075]27}
28
29
30Script::~Script()
31{
32  lua_setgcthreshold(luaState, 0);  // collected garbage
33  lua_close(luaState);
34}
35
36
[8193]37void Script::loadParams(const TiXmlElement* root)
38{
[8199]39  BaseObject::loadParams(root);
[8231]40
[8197]41  LOAD_PARAM_START_CYCLE(root, object);
[8193]42  {
[8197]43    LoadParam_CYCLE(object, "object", this, Script, addObject)
[8193]44        .describe("The name of an object that is needed by a script");
45  }
[8197]46  LOAD_PARAM_END_CYCLE(object);
[8193]47
48
49  LoadParam(root, "file", this, Script, loadFileNoRet)
50      .describe("the fileName of the script, that should be loaded into this world")
51      .defaultValues("");
52}
53
54
55
56bool Script::loadFile(const std::string& filename)
[8075]57 {
58
59   if(currentFile.length() != 0)
[8131]60   {
[8075]61     printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str());
[8085]62     return false;
63    }
[8075]64
65   int error = luaL_loadfile (luaState, filename.c_str());
66
67   if(error == 0)
68   {
69     error = lua_pcall(luaState, 0, 0, 0);
70
71     if(error == 0)
72     {
73      currentFile = filename;
74      return true;
75     }
76     else
77     {
[8289]78       printf("ERROR while loading file %s: ",filename.c_str());
79       reportError(error);
[8075]80     }
81
82   }
83   else
84   {
[8289]85     printf("ERROR while loading file %s: ",filename.c_str());
[8075]86     reportError(error);
87   }
88
89   return false;
90 }
91
[8231]92
[8193]93 void Script::addObject(const std::string& className, const std::string& objectName)
94 {
[8289]95   printf("Script: I am about to add %s of class %s\n",objectName.c_str(),className.c_str());
96   
[8202]97   BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);
[8207]98   WorldObject tmpObj;
[8197]99   if (scriptClass != NULL)
100   {
[8231]101     tmpObj.type = className;
[8206]102     if( !classIsRegistered(className) )
[8207]103     {
[8202]104     static_cast<ScriptClass*>(scriptClass)->registerClass(this);
[8207]105     }
[8231]106
[8197]107     BaseObject* object = ClassList::getObject(objectName, className);
[8206]108     if (object != NULL && !objectIsAdded(objectName))
[8197]109     {
[8202]110        static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false);
[8207]111        tmpObj.name = objectName;
112        registeredObjects.push_back(tmpObj);
[8197]113     }
114   }
[8193]115 }
116
[8231]117
118
119
[8075]120 bool Script::executeFile()
121 {
[8231]122   printf("WARNING: script.executeFile is not implemented yet");
123   /*if(currentFile.length() != 0)
[8075]124   {
125    int error = lua_pcall(luaState, 0, 0, 0);
126    if( error == 0)
127      return true;
128     else
129      {
130       reportError(error);
131       return false;
132      }
[8231]133 }*/
[8075]134   return false;
135 }
136
137 bool Script::selectFunction(std::string& functionName, int retCount)
138 {
[8085]139   if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected
[8075]140   {
141   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
142   lua_gettable(luaState, LUA_GLOBALSINDEX);
143
144   if(lua_isfunction( luaState , -1))
145   {
146     returnCount = retCount;
147     argumentCount = 0;
148     currentFunction = functionName;
149     return true;
150   }
151   else
152    return false;
153   }
154   else
[8085]155     printf("There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFunction.c_str());
156   return false;
[8075]157 }
158
159 //return number of returned values
160 bool Script::executeFunction()
161 {
162   if(currentFunction.length() != 0 )
163   {
164    int error = lua_pcall(luaState, argumentCount, returnCount,0);
165    if(error != 0)
166    {
[8289]167     printf("ERROR while executing function %s: \n",currentFunction.c_str());
[8075]168     reportError(error);
[8289]169     //clean up
170     currentFunction.assign(""); 
171     argumentCount = returnCount = 0;
[8075]172     return false;
173    }
174    else
175    {
176      currentFunction.assign("");//a function gets unusable after beeing called for the first time
177      argumentCount = 0;
178      return true;
179    }
180   }
181   else
182     printf("Error: no function selected.\n");
[8289]183   
184   return false;
[8075]185 }
186
187
188 //overload this function to add different types
189 bool Script::pushParam(int param, std::string& toFunction)
190 {
191   if(currentFunction.compare(toFunction) == 0)
192   {
193     lua_pushnumber(luaState, (lua_Number) param);
194     argumentCount++;
195    return true;
196   }
197   else
198   {
199    printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
200    return false;
201   }
202
203 }
204
205
206 bool Script::pushParam(float param, std::string& toFunction)
207 {
208   if(currentFunction.compare(toFunction) == 0)
209   {
210     lua_pushnumber(luaState,(lua_Number) param);
211     argumentCount++;
212     return true;
213   }
214   else
215   {
216     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
217     return false;
218   }
219
220 }
221
222 bool Script::pushParam(double param, std::string& toFunction)
223 {
224   if(currentFunction.compare(toFunction) == 0)
225   {
226     lua_pushnumber(luaState,(lua_Number) param);
227     argumentCount++;
228     return true;
229   }
230   else
231   {
232     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
233     return false;
234   }
235
236 }
237
238 int Script::getReturnedInt()
239 {
240   int returnValue;
241   if(returnCount > 0)
242   {
243     if(lua_isnumber(luaState, -1))
244     {
245       returnValue = (int)lua_tonumber(luaState, -1);
246       returnCount--;
247       lua_pop(luaState,1);
248     }
249   }
250   return returnValue;
251 }
252
253
254 bool Script::getReturnedBool()
255 {
256   bool returnValue;
257   if(returnCount > 0)
258   {
259     if(lua_isboolean(luaState, -1))
260     {
261       returnValue = (bool)lua_toboolean(luaState, -1);
262       returnCount--;
263       lua_pop(luaState,1);
264     }
265   }
266   return returnValue;
267 }
268
269float Script::getReturnedFloat()
270 {
271   float returnValue;
272   if(returnCount > 0)
273   {
274     if(lua_isnumber(luaState, -1))
275     {
276       returnValue = (float)lua_tonumber(luaState, -1);
277       returnCount--;
278       lua_pop(luaState,1);
279     }
280   }
281   return returnValue;
282 }
283
[8131]284 void Script::getReturnedString(std::string& string)
285 {
[8134]286   const char* returnValue;
[8131]287   if(returnCount > 0)
288   {
[8133]289     if(lua_isstring(luaState, -1))
[8131]290     {
291       returnValue = lua_tostring(luaState, -1);
292       returnCount--;
293       lua_pop(luaState,1);
294     }
295   }
296  string.assign(returnValue);
297 }
298
[8075]299 int Script::reportError(int error)
300 {
[8085]301 if(error != 0)
302 {
303  const char *msg = lua_tostring(luaState, -1);
304  if (msg == NULL) msg = "(error with no message)";
305  fprintf(stderr, "ERROR: %s\n", msg);
306  lua_pop(luaState, 1);
[8289]307 }
[8085]308  return error;
[8075]309 }
310
[8231]311
[8206]312 bool Script::classIsRegistered(const std::string& type)
313 {
[8207]314   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
[8206]315   {
316     if( (*it).type == type)
317     {
318       return true;
319     }
320   }
321   return false;
322 }
[8231]323
324
325
[8206]326 bool Script::objectIsAdded(const std::string& name)
327 {
[8207]328   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
[8206]329   {
330     if( (*it).name == name)
331     {
332       return true;
333     }
334   }
335   return false;
[8231]336
337
[8206]338 }
Note: See TracBrowser for help on using the repository browser.