Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the script_engine branche back here
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/script_engine . -r8284:HEAD
no conflicts

File size: 7.6 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.compare(toFunction) == 0)
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   }
270   return returnValue;
271 }
272
273float Script::getReturnedFloat()
274 {
275   float returnValue = 0.0f;
276   if(returnCount > 0)
277   {
278     if(lua_isnumber(luaState, -1*returnCount))
279     {
280       returnValue = (float)lua_tonumber(luaState, -1*returnCount);
281       lua_remove(luaState,-1*returnCount);
282       returnCount--;
283     }
284   }
285   return returnValue;
286 }
287
288 void Script::getReturnedString(std::string& string)
289 {
290   const char* returnValue = "";
291   if(returnCount > 0)
292   {
293     if(lua_isstring(luaState, -1*returnCount))
294     {
295       returnValue = lua_tostring(luaState, -1*returnCount);
296       lua_remove(luaState,-1*returnCount);
297       returnCount--;
298     }
299   }
300  string.assign(returnValue);
301 }
302
303 int Script::reportError(int error)
304 {
305 if(error != 0)
306 {
307  const char *msg = lua_tostring(luaState, -1);
308  if (msg == NULL) msg = "(error with no message)";
309  fprintf(stderr, "ERROR: %s\n", msg);
310  lua_pop(luaState, 1);
311 }
312  return error;
313 }
314
315
316 bool Script::classIsRegistered(const std::string& type)
317 {
318   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
319   {
320     if( (*it).type == type)
321     {
322       return true;
323     }
324   }
325   return false;
326 }
327
328
329
330 bool Script::objectIsAdded(const std::string& name)
331 {
332   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
333   {
334     if( (*it).name == name)
335     {
336       return true;
337     }
338   }
339   return false;
340
341
342 }
Note: See TracBrowser for help on using the repository browser.