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
Line 
1#include "script.h"
2#include "script_class.h"
3#include "luaincl.h"
4
5
6#include "loading/load_param.h"
7#include "parser/tinyxml/tinyxml.h"
8
9#include "class_list.h"
10
11Script::Script(const TiXmlElement* root)
12{
13  this->setClassID(CL_SCRIPT, "Script");
14
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);
25  if (root != NULL)
26    this->loadParams(root);
27}
28
29
30Script::~Script()
31{
32  lua_setgcthreshold(luaState, 0);  // collected garbage
33  lua_close(luaState);
34}
35
36
37void Script::loadParams(const TiXmlElement* root)
38{
39  BaseObject::loadParams(root);
40
41  LOAD_PARAM_START_CYCLE(root, object);
42  {
43    LoadParam_CYCLE(object, "object", this, Script, addObject)
44        .describe("The name of an object that is needed by a script");
45  }
46  LOAD_PARAM_END_CYCLE(object);
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)
57 {
58
59   if(currentFile.length() != 0)
60   {
61     printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str());
62     return false;
63    }
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     {
78       printf("ERROR while loading file %s: ",filename.c_str());
79       reportError(error);
80     }
81
82   }
83   else
84   {
85     printf("ERROR while loading file %s: ",filename.c_str());
86     reportError(error);
87   }
88
89   return false;
90 }
91
92
93 void Script::addObject(const std::string& className, const std::string& objectName)
94 {
95   printf("Script: I am about to add %s of class %s\n",objectName.c_str(),className.c_str());
96   
97   BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);
98   WorldObject tmpObj;
99   if (scriptClass != NULL)
100   {
101     tmpObj.type = className;
102     if( !classIsRegistered(className) )
103     {
104     static_cast<ScriptClass*>(scriptClass)->registerClass(this);
105     }
106
107     BaseObject* object = ClassList::getObject(objectName, className);
108     if (object != NULL && !objectIsAdded(objectName))
109     {
110        static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false);
111        tmpObj.name = objectName;
112        registeredObjects.push_back(tmpObj);
113     }
114   }
115 }
116
117
118
119
120 bool Script::executeFile()
121 {
122   printf("WARNING: script.executeFile is not implemented yet");
123   /*if(currentFile.length() != 0)
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      }
133 }*/
134   return false;
135 }
136
137 bool Script::selectFunction(std::string& functionName, int retCount)
138 {
139   if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected
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
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;
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    {
167     printf("ERROR while executing function %s: \n",currentFunction.c_str());
168     reportError(error);
169     //clean up
170     currentFunction.assign(""); 
171     argumentCount = returnCount = 0;
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");
183   
184   return false;
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
284 void Script::getReturnedString(std::string& string)
285 {
286   const char* returnValue;
287   if(returnCount > 0)
288   {
289     if(lua_isstring(luaState, -1))
290     {
291       returnValue = lua_tostring(luaState, -1);
292       returnCount--;
293       lua_pop(luaState,1);
294     }
295   }
296  string.assign(returnValue);
297 }
298
299 int Script::reportError(int error)
300 {
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);
307 }
308  return error;
309 }
310
311
312 bool Script::classIsRegistered(const std::string& type)
313 {
314   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
315   {
316     if( (*it).type == type)
317     {
318       return true;
319     }
320   }
321   return false;
322 }
323
324
325
326 bool Script::objectIsAdded(const std::string& name)
327 {
328   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
329   {
330     if( (*it).name == name)
331     {
332       return true;
333     }
334   }
335   return false;
336
337
338 }
Note: See TracBrowser for help on using the repository browser.