[8711] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: Benjamin Grauer |
---|
| 14 | */ |
---|
| 15 | |
---|
[8075] | 16 | #include "script.h" |
---|
[8202] | 17 | #include "script_class.h" |
---|
| 18 | #include "luaincl.h" |
---|
[9298] | 19 | #include "debug.h" |
---|
[8202] | 20 | |
---|
[9869] | 21 | #include "loading/resource_manager.h" |
---|
[8193] | 22 | #include "loading/load_param.h" |
---|
[8197] | 23 | #include "parser/tinyxml/tinyxml.h" |
---|
[8075] | 24 | |
---|
[9869] | 25 | ObjectListDefinition(Script); |
---|
| 26 | |
---|
[8075] | 27 | |
---|
[8193] | 28 | Script::Script(const TiXmlElement* root) |
---|
[8075] | 29 | { |
---|
[9869] | 30 | this->registerObject(this, Script::_objectList); |
---|
[8231] | 31 | |
---|
[8075] | 32 | returnCount = argumentCount = 0; |
---|
| 33 | |
---|
| 34 | luaState = lua_open(); |
---|
| 35 | |
---|
| 36 | luaopen_base(luaState); |
---|
| 37 | luaopen_table(luaState); |
---|
| 38 | luaopen_io(luaState); |
---|
| 39 | luaopen_string(luaState); |
---|
| 40 | luaopen_math(luaState); |
---|
| 41 | luaopen_debug(luaState); |
---|
[8193] | 42 | if (root != NULL) |
---|
| 43 | this->loadParams(root); |
---|
[8075] | 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
[9298] | 47 | Script::Script(const std::string& filename) |
---|
| 48 | { |
---|
[9869] | 49 | this->registerObject(this, Script::_objectList); |
---|
[9298] | 50 | |
---|
| 51 | returnCount = argumentCount = 0; |
---|
| 52 | |
---|
| 53 | luaState = lua_open(); |
---|
| 54 | |
---|
| 55 | luaopen_base(luaState); |
---|
| 56 | luaopen_table(luaState); |
---|
| 57 | luaopen_io(luaState); |
---|
| 58 | luaopen_string(luaState); |
---|
| 59 | luaopen_math(luaState); |
---|
| 60 | luaopen_debug(luaState); |
---|
[9869] | 61 | |
---|
[9298] | 62 | this->loadFile(filename); |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
[8075] | 66 | Script::~Script() |
---|
| 67 | { |
---|
| 68 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
| 69 | lua_close(luaState); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[8193] | 73 | void Script::loadParams(const TiXmlElement* root) |
---|
| 74 | { |
---|
[9298] | 75 | //printf(("Loading params for %p \n",this); |
---|
[8199] | 76 | BaseObject::loadParams(root); |
---|
[8231] | 77 | |
---|
[9298] | 78 | LOAD_PARAM_START_CYCLE(root, object); |
---|
[8193] | 79 | { |
---|
[8197] | 80 | LoadParam_CYCLE(object, "object", this, Script, addObject) |
---|
[9298] | 81 | .describe("The name of an object that is needed by a script"); |
---|
[8193] | 82 | } |
---|
[9298] | 83 | LOAD_PARAM_END_CYCLE(object); |
---|
[8193] | 84 | |
---|
| 85 | |
---|
| 86 | LoadParam(root, "file", this, Script, loadFileNoRet) |
---|
| 87 | .describe("the fileName of the script, that should be loaded into this world") |
---|
| 88 | .defaultValues(""); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | bool Script::loadFile(const std::string& filename) |
---|
[8075] | 94 | { |
---|
[9869] | 95 | std::string filedest(Resources::ResourceManager::getInstance()->mainGlobalPath().name()); |
---|
| 96 | filedest += "/scripts/" + filename; |
---|
| 97 | |
---|
[9061] | 98 | this->addThisScript(); |
---|
| 99 | this->registerStandartClasses(); |
---|
[9869] | 100 | |
---|
[8075] | 101 | if(currentFile.length() != 0) |
---|
[8131] | 102 | { |
---|
[9298] | 103 | printf("SCRIPT %s : ERROR: Could not load %s because an other file is already loaded: %s\n",currentFile.c_str(),filename.c_str(), currentFile.c_str()); |
---|
[8085] | 104 | return false; |
---|
| 105 | } |
---|
[8075] | 106 | |
---|
[8711] | 107 | int error = luaL_loadfile (luaState, filedest.c_str()); |
---|
[8075] | 108 | |
---|
| 109 | if(error == 0) |
---|
| 110 | { |
---|
[9235] | 111 | currentFile = filename; |
---|
[8075] | 112 | error = lua_pcall(luaState, 0, 0, 0); |
---|
| 113 | |
---|
| 114 | if(error == 0) |
---|
| 115 | { |
---|
| 116 | return true; |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
[9298] | 120 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
[8408] | 121 | reportError(error); |
---|
[8075] | 122 | } |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | { |
---|
[9298] | 127 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
[8075] | 128 | reportError(error); |
---|
| 129 | } |
---|
| 130 | |
---|
[10319] | 131 | printf("SCRIPT : ERROR: while loading file %s \n",filename.c_str()); |
---|
[8075] | 132 | return false; |
---|
| 133 | } |
---|
| 134 | |
---|
[8231] | 135 | |
---|
[8193] | 136 | void Script::addObject(const std::string& className, const std::string& objectName) |
---|
| 137 | { |
---|
[9298] | 138 | //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str()); |
---|
[8408] | 139 | |
---|
[9869] | 140 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
[9298] | 141 | // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
[8207] | 142 | WorldObject tmpObj; |
---|
[8197] | 143 | if (scriptClass != NULL) |
---|
| 144 | { |
---|
[8231] | 145 | tmpObj.type = className; |
---|
[8206] | 146 | if( !classIsRegistered(className) ) |
---|
[8207] | 147 | { |
---|
[9869] | 148 | scriptClass->registerClass(this); |
---|
[8207] | 149 | } |
---|
[8231] | 150 | |
---|
[9869] | 151 | BaseObject* object = ObjectListBase::getBaseObject(className, objectName); |
---|
[9298] | 152 | // printf(("%s is at %p \n",objectName.c_str(),object); |
---|
[8206] | 153 | if (object != NULL && !objectIsAdded(objectName)) |
---|
[8197] | 154 | { |
---|
[9869] | 155 | scriptClass->insertObject(this, object, false); |
---|
[8207] | 156 | tmpObj.name = objectName; |
---|
| 157 | registeredObjects.push_back(tmpObj); |
---|
[8197] | 158 | } |
---|
| 159 | } |
---|
[8193] | 160 | } |
---|
| 161 | |
---|
[8231] | 162 | |
---|
| 163 | |
---|
| 164 | |
---|
[8075] | 165 | bool Script::executeFile() |
---|
| 166 | { |
---|
[9869] | 167 | PRINTF(2)("script.executeFile is not implemented yet\n"); |
---|
[8231] | 168 | /*if(currentFile.length() != 0) |
---|
[8075] | 169 | { |
---|
| 170 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
| 171 | if( error == 0) |
---|
| 172 | return true; |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | reportError(error); |
---|
| 176 | return false; |
---|
| 177 | } |
---|
[8231] | 178 | }*/ |
---|
[8075] | 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
[9061] | 182 | bool Script::selectFunction(const std::string& functionName, int retCount) |
---|
[8075] | 183 | { |
---|
[8085] | 184 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
[8075] | 185 | { |
---|
| 186 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
| 187 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
| 188 | |
---|
| 189 | if(lua_isfunction( luaState , -1)) |
---|
| 190 | { |
---|
| 191 | returnCount = retCount; |
---|
| 192 | argumentCount = 0; |
---|
| 193 | currentFunction = functionName; |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | return false; |
---|
| 198 | } |
---|
| 199 | else |
---|
[9298] | 200 | printf("SCRIPT %s : ERROR: There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFile.c_str(),currentFunction.c_str()); |
---|
[8085] | 201 | return false; |
---|
[8075] | 202 | } |
---|
| 203 | |
---|
| 204 | //return number of returned values |
---|
| 205 | bool Script::executeFunction() |
---|
| 206 | { |
---|
| 207 | if(currentFunction.length() != 0 ) |
---|
| 208 | { |
---|
| 209 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
| 210 | if(error != 0) |
---|
| 211 | { |
---|
[9869] | 212 | PRINTF(1)("Script '%s' : Failed to execute function '%s': \n",currentFile.c_str(),currentFunction.c_str()); |
---|
[8075] | 213 | reportError(error); |
---|
[8408] | 214 | //clean up |
---|
| 215 | currentFunction.assign(""); |
---|
| 216 | argumentCount = returnCount = 0; |
---|
[8075] | 217 | return false; |
---|
| 218 | } |
---|
| 219 | else |
---|
| 220 | { |
---|
| 221 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
| 222 | argumentCount = 0; |
---|
| 223 | return true; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | else |
---|
[9869] | 227 | PRINTF(1)("SCRIPT '%s' : no function selected.\n",currentFile.c_str()); |
---|
[8408] | 228 | |
---|
| 229 | return false; |
---|
[8075] | 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | //overload this function to add different types |
---|
| 234 | bool Script::pushParam(int param, std::string& toFunction) |
---|
| 235 | { |
---|
[8711] | 236 | if(currentFunction == toFunction) |
---|
[8075] | 237 | { |
---|
| 238 | lua_pushnumber(luaState, (lua_Number) param); |
---|
| 239 | argumentCount++; |
---|
[8711] | 240 | return true; |
---|
[8075] | 241 | } |
---|
| 242 | else |
---|
| 243 | { |
---|
[9298] | 244 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 245 | return false; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | bool Script::pushParam(float param, std::string& toFunction) |
---|
| 252 | { |
---|
| 253 | if(currentFunction.compare(toFunction) == 0) |
---|
| 254 | { |
---|
| 255 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 256 | argumentCount++; |
---|
| 257 | return true; |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
[9298] | 261 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 262 | return false; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | bool Script::pushParam(double param, std::string& toFunction) |
---|
| 268 | { |
---|
| 269 | if(currentFunction.compare(toFunction) == 0) |
---|
| 270 | { |
---|
| 271 | lua_pushnumber(luaState,(lua_Number) param); |
---|
| 272 | argumentCount++; |
---|
| 273 | return true; |
---|
| 274 | } |
---|
| 275 | else |
---|
| 276 | { |
---|
[9298] | 277 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
[8075] | 278 | return false; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | int Script::getReturnedInt() |
---|
| 284 | { |
---|
[8408] | 285 | int returnValue = 0; |
---|
[8075] | 286 | if(returnCount > 0) |
---|
| 287 | { |
---|
[8408] | 288 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 289 | { |
---|
[8408] | 290 | returnValue = (int)lua_tonumber(luaState, -1*returnCount); |
---|
| 291 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 292 | returnCount--; |
---|
[9869] | 293 | |
---|
[8075] | 294 | } |
---|
| 295 | } |
---|
| 296 | return returnValue; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | bool Script::getReturnedBool() |
---|
| 301 | { |
---|
[8408] | 302 | bool returnValue = false; |
---|
[8075] | 303 | if(returnCount > 0) |
---|
| 304 | { |
---|
[8408] | 305 | if(lua_isboolean(luaState, -1*returnCount)) |
---|
[8075] | 306 | { |
---|
[8408] | 307 | returnValue = (bool)lua_toboolean(luaState, -1*returnCount); |
---|
| 308 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 309 | returnCount--; |
---|
| 310 | } |
---|
[8711] | 311 | else |
---|
[9298] | 312 | printf("SCRIPT %s : ERROR: Trying to retreive non bolean value\n",this->currentFile.c_str()); |
---|
[8075] | 313 | } |
---|
| 314 | return returnValue; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | float Script::getReturnedFloat() |
---|
| 318 | { |
---|
[8408] | 319 | float returnValue = 0.0f; |
---|
[8075] | 320 | if(returnCount > 0) |
---|
| 321 | { |
---|
[8408] | 322 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
[8075] | 323 | { |
---|
[8408] | 324 | returnValue = (float)lua_tonumber(luaState, -1*returnCount); |
---|
| 325 | lua_remove(luaState,-1*returnCount); |
---|
[8075] | 326 | returnCount--; |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | return returnValue; |
---|
| 330 | } |
---|
| 331 | |
---|
[8131] | 332 | void Script::getReturnedString(std::string& string) |
---|
| 333 | { |
---|
[8408] | 334 | const char* returnValue = ""; |
---|
[8131] | 335 | if(returnCount > 0) |
---|
| 336 | { |
---|
[8408] | 337 | if(lua_isstring(luaState, -1*returnCount)) |
---|
[8131] | 338 | { |
---|
[8408] | 339 | returnValue = lua_tostring(luaState, -1*returnCount); |
---|
| 340 | lua_remove(luaState,-1*returnCount); |
---|
[8131] | 341 | returnCount--; |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | string.assign(returnValue); |
---|
| 345 | } |
---|
| 346 | |
---|
[9003] | 347 | |
---|
| 348 | void Script::addThisScript() |
---|
| 349 | { |
---|
[9869] | 350 | ScriptClass* scriptClass = ScriptClass::objectList().getObject("Script"); |
---|
| 351 | |
---|
| 352 | if (scriptClass != NULL) |
---|
[9003] | 353 | { |
---|
[9869] | 354 | scriptClass->registerClass(this); |
---|
| 355 | scriptClass->insertObject(this, this,"thisscript", false); |
---|
[9003] | 356 | } |
---|
| 357 | } |
---|
| 358 | |
---|
[8075] | 359 | int Script::reportError(int error) |
---|
| 360 | { |
---|
[8085] | 361 | if(error != 0) |
---|
| 362 | { |
---|
| 363 | const char *msg = lua_tostring(luaState, -1); |
---|
[9298] | 364 | if (msg == NULL) msg = "(error with no message)\n"; |
---|
| 365 | printf("ERROR: %s\n", msg); |
---|
[8085] | 366 | lua_pop(luaState, 1); |
---|
[8408] | 367 | } |
---|
[8085] | 368 | return error; |
---|
[8075] | 369 | } |
---|
| 370 | |
---|
[8711] | 371 | bool Script::registerStandartClasses() |
---|
| 372 | { |
---|
| 373 | bool success = false; |
---|
[9869] | 374 | |
---|
[9235] | 375 | //this->registerClass(std::string("Vector")); |
---|
| 376 | this->registerClass("ScriptTrigger"); |
---|
| 377 | // this->registerClass("AttractorMine"); |
---|
[9061] | 378 | |
---|
[8711] | 379 | return success; |
---|
| 380 | } |
---|
[9869] | 381 | |
---|
| 382 | |
---|
[9235] | 383 | void Script::registerClass( const std::string& className) |
---|
[8711] | 384 | { |
---|
[9869] | 385 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
[9298] | 386 | //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
[9869] | 387 | |
---|
[8711] | 388 | WorldObject tmpObj; |
---|
| 389 | if (scriptClass != NULL) |
---|
| 390 | { |
---|
| 391 | tmpObj.type = className; |
---|
| 392 | if( !classIsRegistered(className) ) |
---|
| 393 | { |
---|
| 394 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
| 395 | tmpObj.name = ""; |
---|
| 396 | registeredObjects.push_back(tmpObj); |
---|
[9235] | 397 | return; |
---|
[8711] | 398 | } |
---|
| 399 | } |
---|
[9869] | 400 | |
---|
[8711] | 401 | } |
---|
[8231] | 402 | |
---|
[8206] | 403 | bool Script::classIsRegistered(const std::string& type) |
---|
| 404 | { |
---|
[8207] | 405 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 406 | { |
---|
| 407 | if( (*it).type == type) |
---|
| 408 | { |
---|
| 409 | return true; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | return false; |
---|
| 413 | } |
---|
[8231] | 414 | |
---|
| 415 | |
---|
| 416 | |
---|
[8206] | 417 | bool Script::objectIsAdded(const std::string& name) |
---|
| 418 | { |
---|
[8207] | 419 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
[8206] | 420 | { |
---|
| 421 | if( (*it).name == name) |
---|
| 422 | { |
---|
| 423 | return true; |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | return false; |
---|
[8231] | 427 | |
---|
| 428 | |
---|
[8206] | 429 | } |
---|