[Add] Improved space.getPlanet() mission lua.

This commit is contained in:
Allanis 2013-04-04 21:24:25 +01:00
parent 64f929c1d3
commit 7f973d7fe8
3 changed files with 51 additions and 15 deletions

View File

@ -209,10 +209,17 @@ static int space_getPlanet(lua_State* L) {
if(lua_gettop(L) == 0) { if(lua_gettop(L) == 0) {
// Get random planet. // Get random planet.
lua_pushstring(L, space_getRndPlanet());
return 1;
}
else if(lua_isnumber(L, -1) || lua_istable(L, -1)) {
// Faction table or single.
if(lua_isnumber(L, -1)) {
// Faction is just a number.
i = lua_tonumber(L, -1);
planets = space_getFactionPlanet(&nplanets, &i, 1);
} }
else if(lua_istable(L, -1)) { else if(lua_istable(L, -1)) {
// Get planet of faction in table.
// Load up the table. // Load up the table.
lua_pushnil(L); lua_pushnil(L);
nfactions = (int) lua_gettop(L); nfactions = (int) lua_gettop(L);
@ -226,7 +233,7 @@ static int space_getPlanet(lua_State* L) {
// Get the planets. // Get the planets.
planets = space_getFactionPlanet(&nplanets, factions, nfactions); planets = space_getFactionPlanet(&nplanets, factions, nfactions);
free(factions); free(factions);
}
// Choose random planet. // Choose random planet.
if(nplanets == 0) { if(nplanets == 0) {
// No suitable planets. // No suitable planets.

View File

@ -226,6 +226,34 @@ char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions) {
return tmp; return tmp;
} }
// Return the name of a random planet.
char* space_getRndPlanet(void) {
int i, j;
char** tmp;
int ntmp;
int mtmp;
char* res;
ntmp = 0;
mtmp = 25;
tmp = malloc(sizeof(char)*mtmp);
for(i = 0; i < systems_nstack; i++)
for(j = 0; j < systems_stack[i].nplanets; j++) {
ntmp++;
if(ntmp > mtmp) {
mtmp += 25;
tmp = realloc(tmp, sizeof(char*) * mtmp);
}
tmp[ntmp-1] = systems_stack[i].planets[j].name;
}
res = tmp[RNG(0, ntmp-1)];
free(tmp);
return res;
}
// Basically used for spawning fleets. // Basically used for spawning fleets.
void space_update(const double dt) { void space_update(const double dt) {
unsigned int t; unsigned int t;

View File

@ -109,5 +109,6 @@ void space_update(const double dt);
int space_canHyperspace(Pilot* p); int space_canHyperspace(Pilot* p);
int space_hyperspace(Pilot* p); int space_hyperspace(Pilot* p);
char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions); char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions);
char* space_getRndPlanet(void);
extern char* stardate; extern char* stardate;