! Initialize the script orbit. * moduleName is the name of a module that contains the orbit factory * function. The module will be loaded with Lua's require function before * creating the Lua orbit object. * * funcName is the name of some factory function in the specified luaState * that will produce a Lua orbit object from the parameter list. * * The Lua factory function accepts a s
| 57 | * z coordinates. Units for the position are kilometers. |
| 58 | */ |
| 59 | bool |
| 60 | ScriptedOrbit::initialize(const std::string& moduleName, |
| 61 | const std::string& funcName, |
| 62 | Hash* parameters) |
| 63 | { |
| 64 | if (parameters == NULL) |
| 65 | return false; |
| 66 | |
| 67 | luaState = GetScriptedObjectContext(); |
| 68 | if (luaState == NULL) |
| 69 | { |
| 70 | clog << "ScriptedOrbits are currently disabled.\n"; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (!moduleName.empty()) |
| 75 | { |
| 76 | lua_getglobal(luaState, "require"); |
| 77 | if (!lua_isfunction(luaState, -1)) |
| 78 | { |
| 79 | clog << "Cannot load ScriptedOrbit package: 'require' function is unavailable\n"; |
| 80 | lua_pop(luaState, 1); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | lua_pushstring(luaState, moduleName.c_str()); |
| 85 | if (lua_pcall(luaState, 1, 1, 0) != 0) |
| 86 | { |
| 87 | clog << "Failed to load module for ScriptedOrbit: " << lua_tostring(luaState, -1) << "\n"; |
| 88 | lua_pop(luaState, 1); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Get the orbit generator function |
| 94 | lua_getglobal(luaState, funcName.c_str()); |
| 95 | |
| 96 | if (lua_isfunction(luaState, -1) == 0) |
| 97 | { |
| 98 | // No function with the requested name; pop whatever value we |
| 99 | // did receive along with the table of arguments. |
| 100 | lua_pop(luaState, 1); |
| 101 | clog << "No Lua function named " << funcName << " found.\n"; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // Construct the table that we'll pass to the orbit generator function |
| 106 | lua_newtable(luaState); |
| 107 | |
| 108 | // Convert all the parameters in the list to their equivalent Lua |
| 109 | // types and insert them into the table. Presently, only number, string, |
| 110 | // and boolean values are converted. |
| 111 | for (HashIterator iter = parameters->begin(); iter != parameters->end(); |
| 112 | iter++) |
| 113 | { |
| 114 | switch (iter->second->getType()) |
| 115 | { |
| 116 | case Value::NumberType: |
no test coverage detected