/** * @brief Gets the ships sold at a planet. * * @luatparam Planet p Planet to get ships sold at. * @luatreturn {Ship,...} An ordered table containing all the ships sold (empty if none sold). * @luafunc shipsSold( p ) */ static int planetL_shipsSold( lua_State *L ) { Planet *p; int i, n; Ship **s; /* Get result and tech. */ p = luaL_validplanet(L,1); s = tech_getShip( p->tech, &n ); /* Push results in a table. */ lua_newtable(L); for (i=0; i<n; i++) { lua_pushnumber(L,i+1); /* index, starts with 1 */ lua_pushship(L,s[i]); /* value = LuaShip */ lua_rawset(L,-3); /* store the value in the table */ } return 1; }
/** * @brief Unpersists Lua data. * * @param L State to unperisist data into. * @param parent Node containing all the Lua persisted data. * @return 0 on success. */ static int nxml_unpersistDataNode( lua_State *L, xmlNodePtr parent ) { LuaPlanet p; LuaSystem s; LuaFaction f; LuaShip sh; LuaTime lt; Planet *pnt; StarSystem *ss; xmlNodePtr node; char *name, *type, *buf, *num; int keynum; node = parent->xmlChildrenNode; do { if (xml_isNode(node,"data")) { /* Get general info. */ xmlr_attr(node,"name",name); xmlr_attr(node,"type",type); /* Check to see if key is a number. */ xmlr_attr(node,"keynum",num); if (num != NULL) { keynum = 1; lua_pushnumber(L, atof(name)); free(num); } else lua_pushstring(L, name); /* handle data types */ /* Recursive tables. */ if (strcmp(type,"table")==0) { xmlr_attr(node,"name",buf); /* Create new table. */ lua_newtable(L); /* Save data. */ nxml_unpersistDataNode(L,node); /* Set table. */ free(buf); } else if (strcmp(type,"number")==0) lua_pushnumber(L,xml_getFloat(node)); else if (strcmp(type,"bool")==0) lua_pushboolean(L,xml_getInt(node)); else if (strcmp(type,"string")==0) lua_pushstring(L,xml_get(node)); else if (strcmp(type,"planet")==0) { pnt = planet_get(xml_get(node)); if (pnt != NULL) { p.id = planet_index(pnt); lua_pushplanet(L,p); } else WARN("Failed to load unexistent planet '%s'", xml_get(node)); } else if (strcmp(type,"system")==0) { ss = system_get(xml_get(node)); if (ss != NULL) { s.id = system_index( ss ); lua_pushsystem(L,s); } else WARN("Failed to load unexistent system '%s'", xml_get(node)); } else if (strcmp(type,"faction")==0) { f.f = faction_get(xml_get(node)); lua_pushfaction(L,f); } else if (strcmp(type,"ship")==0) { sh.ship = ship_get(xml_get(node)); lua_pushship(L,sh); } else if (strcmp(type,"time")==0) { lt.t = xml_getLong(node); lua_pushtime(L,lt); } else { WARN("Unknown lua data type!"); lua_pop(L,1); return -1; } /* Set field. */ lua_settable(L, -3); /* cleanup */ free(type); free(name); } } while (xml_nextNode(node)); return 0; }