Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8675 was 8675, checked in by snellen, 19 years ago

copied movechopper.lua into src

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