Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

helicopter scripted

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