[Add] Improved space.getPlanet() mission lua.
This commit is contained in:
parent
64f929c1d3
commit
7f973d7fe8
@ -209,24 +209,31 @@ static int space_getPlanet(lua_State* L) {
|
||||
|
||||
if(lua_gettop(L) == 0) {
|
||||
// Get random planet.
|
||||
lua_pushstring(L, space_getRndPlanet());
|
||||
return 1;
|
||||
}
|
||||
else if(lua_istable(L, -1)) {
|
||||
// Get planet of faction in table.
|
||||
|
||||
// Load up the table.
|
||||
lua_pushnil(L);
|
||||
nfactions = (int) lua_gettop(L);
|
||||
factions = malloc(sizeof(int) * nfactions);
|
||||
i = 0;
|
||||
while(lua_next(L, -2) != 0) {
|
||||
factions[i++] = (int) lua_tonumber(L, -1);
|
||||
lua_pop(L, 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)) {
|
||||
// Load up the table.
|
||||
lua_pushnil(L);
|
||||
nfactions = (int) lua_gettop(L);
|
||||
factions = malloc(sizeof(int) * nfactions);
|
||||
i = 0;
|
||||
while(lua_next(L, -2) != 0) {
|
||||
factions[i++] = (int) lua_tonumber(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
// Get the planets.
|
||||
planets = space_getFactionPlanet(&nplanets, factions, nfactions);
|
||||
free(factions);
|
||||
|
||||
// Get the planets.
|
||||
planets = space_getFactionPlanet(&nplanets, factions, nfactions);
|
||||
free(factions);
|
||||
}
|
||||
// Choose random planet.
|
||||
if(nplanets == 0) {
|
||||
// No suitable planets.
|
||||
|
28
src/space.c
28
src/space.c
@ -226,6 +226,34 @@ char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions) {
|
||||
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.
|
||||
void space_update(const double dt) {
|
||||
unsigned int t;
|
||||
|
@ -109,5 +109,6 @@ void space_update(const double dt);
|
||||
int space_canHyperspace(Pilot* p);
|
||||
int space_hyperspace(Pilot* p);
|
||||
char** space_getFactionPlanet(int* nplanets, int* factions, int nfactions);
|
||||
char* space_getRndPlanet(void);
|
||||
extern char* stardate;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user