static int l_csys_bodies(lua_State *L) { CustomSystem *cs = l_csys_check(L, 1); CustomSystemBody **primary_ptr = l_csb_check_ptr(L, 2); int primary_type = (*primary_ptr)->type; luaL_checktype(L, 3, LUA_TTABLE); if ((primary_type < SystemBody::TYPE_STAR_MIN || primary_type > SystemBody::TYPE_STAR_MAX) && primary_type != SystemBody::TYPE_GRAVPOINT) return luaL_error(L, "first body does not have a valid star type"); if (primary_type != cs->primaryType[0] && primary_type != SystemBody::TYPE_GRAVPOINT) return luaL_error(L, "first body type does not match the system's primary star type"); lua_pushvalue(L, 3); _add_children_to_sbody(L, *primary_ptr); lua_pop(L, 1); cs->sBody = *primary_ptr; *primary_ptr = 0; if (cs->sBody) { unsigned star_count = count_stars(cs->sBody); if (star_count != cs->numStars) return luaL_error(L, "expected %u star(s) in system %s, but found %u (did you forget star types in CustomSystem:new?)", cs->numStars, cs->name.c_str(), star_count); // XXX Someday, we should check the other star types as well, but we do not use them anyway now. } lua_settop(L, 1); return 1; }
static void _add_children_to_sbody(lua_State* L, CustomSBody* sbody, OOLUA::Lua_table children) { int i=1; while (1) { CustomSBody *kid; if (!children.safe_at(i++, kid)) break; if (kid == NULL) { luaL_error(L, "invalid element (must be CustomSBody or table of CustomSBody)\n" "invalid element is child of CustomSBody '%s'", sbody->name.c_str()); } while (1) { OOLUA::Lua_table sub; if (!children.safe_at(i, sub)) break; if (!sub.valid()) break; _add_children_to_sbody(L, kid, sub); i++; continue; } sbody->children.push_back(*kid); } }
void CustomSystem::l_bodies(lua_State* L, CustomSBody& primary_star, OOLUA::Lua_table children) { if ( primary_star.type < SBody::TYPE_STAR_MIN || primary_star.type > SBody::TYPE_STAR_MAX ) luaL_error(L, "first body does not have a valid star type"); if ( primary_star.type != primaryType[0] ) luaL_error(L, "first body is not of same type as system primary star"); _add_children_to_sbody(L, &primary_star, children); sBody = primary_star; }
static void _add_children_to_sbody(lua_State *L, CustomSystemBody *sbody) { lua_checkstack(L, 5); // grow the stack if necessary LUA_DEBUG_START(L); int i = 0; while (true) { // first there's a body lua_rawgeti(L, -1, ++i); if (lua_isnil(L, -1)) { lua_pop(L, 1); break; } LUA_DEBUG_CHECK(L, 1); CustomSystemBody **csptr = l_csb_check_ptr(L, -1); CustomSystemBody *kid = *csptr; *csptr = 0; lua_pop(L, 1); LUA_DEBUG_CHECK(L, 0); // then there are any number of sub-tables containing direct children while (true) { lua_rawgeti(L, -1, i+1); LUA_DEBUG_CHECK(L, 1); if (!lua_istable(L, -1)) break; _add_children_to_sbody(L, kid); lua_pop(L, 1); LUA_DEBUG_CHECK(L, 0); ++i; } lua_pop(L, 1); LUA_DEBUG_CHECK(L, 0); //Output("add-children-to-body adding %s to %s\n", kid->name.c_str(), sbody->name.c_str()); sbody->children.push_back(kid); } //Output("add-children-to-body done for %s\n", sbody->name.c_str()); LUA_DEBUG_END(L, 0); }
static int l_csys_bodies(lua_State *L) { CustomSystem *cs = l_csys_check(L, 1); CustomSystemBody **primary_ptr = l_csb_check_ptr(L, 2); int primary_type = (*primary_ptr)->type; luaL_checktype(L, 3, LUA_TTABLE); if (primary_type < SystemBody::TYPE_STAR_MIN || primary_type > SystemBody::TYPE_STAR_MAX) return luaL_error(L, "first body does not have a valid star type"); if (primary_type != cs->primaryType[0]) return luaL_error(L, "first body type does not match the system's primary star type"); lua_pushvalue(L, 3); _add_children_to_sbody(L, *primary_ptr); lua_pop(L, 1); cs->sBody = *primary_ptr; *primary_ptr = 0; lua_settop(L, 1); return 1; }