[Change] Began cleaning lua wrappers up a bit.
This commit is contained in:
parent
331cc86c98
commit
e57c6768b7
297
src/llua.c
Normal file
297
src/llua.c
Normal file
@ -0,0 +1,297 @@
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "lephisto.h"
|
||||
#include "rng.h"
|
||||
#include "ltime.h"
|
||||
#include "toolkit.h"
|
||||
#include "space.h"
|
||||
#include "land.h"
|
||||
#include "lluadef.h"
|
||||
#include "llua.h"
|
||||
|
||||
// -- Libraries. --
|
||||
|
||||
// Lephisto.
|
||||
static int lephisto_lang(lua_State* L);
|
||||
static const luaL_reg lephisto_methods[] = {
|
||||
{ "lang", lephisto_lang },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Space.
|
||||
static int space_getPlanet(lua_State* L);
|
||||
static int space_getSystem(lua_State* L);
|
||||
static int space_landName(lua_State* L);
|
||||
static int space_systemName(lua_State* L);
|
||||
static int space_jumpDist(lua_State* L);
|
||||
static const luaL_reg space_methods[] = {
|
||||
{ "getPlanet", space_getPlanet },
|
||||
{ "getSystem", space_getSystem },
|
||||
{ "landName", space_landName },
|
||||
{ "system", space_systemName },
|
||||
{ "jumpDist", space_jumpDist },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Time.
|
||||
static int time_get(lua_State* L);
|
||||
static int time_str(lua_State* L);
|
||||
static int time_units(lua_State* L);
|
||||
static const luaL_reg time_methods[] = {
|
||||
{ "get", time_get },
|
||||
{ "str", time_str },
|
||||
{ "units", time_units },
|
||||
{0, 0}
|
||||
};
|
||||
|
||||
// RND.
|
||||
static int rnd_int(lua_State*L);
|
||||
static const luaL_reg rnd_methods[] = {
|
||||
{ "int", rnd_int },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Toolkit.
|
||||
static int tk_msg(lua_State* L);
|
||||
static int tk_yesno(lua_State* L);
|
||||
static int tk_input(lua_State* L);
|
||||
static const luaL_reg tk_methods[] = {
|
||||
{ "msg", tk_msg },
|
||||
{ "yesno", tk_yesno },
|
||||
{ "input", tk_input },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Individual libraries.
|
||||
int lua_loadLephisto(lua_State* L) {
|
||||
luaL_register(L, "lephisto", lephisto_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadSpace(lua_State* L, int readonly) {
|
||||
(void)readonly;
|
||||
luaL_register(L, "space", space_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadTime(lua_State* L, int readonly) {
|
||||
(void)readonly;
|
||||
luaL_register(L, "time", time_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadRnd(lua_State* L) {
|
||||
luaL_register(L, "rnd", rnd_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadTk(lua_State* L) {
|
||||
luaL_register(L, "tk", tk_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -- Lephisto. --
|
||||
static int lephisto_lang(lua_State* L) {
|
||||
// TODO: multilanguage stuff.
|
||||
lua_pushstring(L, "en");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Space. --
|
||||
static int space_getPlanet(lua_State* L) {
|
||||
int i;
|
||||
int *factions;
|
||||
int nfactions;
|
||||
char** planets;
|
||||
int nplanets;
|
||||
char* rndplanet;
|
||||
|
||||
if(lua_gettop(L) == 0) {
|
||||
// Get random planet.
|
||||
lua_pushstring(L, space_getRndPlanet());
|
||||
return 1;
|
||||
}
|
||||
else if(lua_isnumber(L, -1)) {
|
||||
i = lua_tonumber(L, -1);
|
||||
planets = space_getFactionPlanet(&nplanets, &i, 1);
|
||||
}
|
||||
else if(lua_isstring(L, -1)) {
|
||||
i = faction_get((char*) lua_tostring(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);
|
||||
}
|
||||
else return 0; // Nothing useful.
|
||||
|
||||
// Choose random planet.
|
||||
if(nplanets == 0) {
|
||||
// No suitable planet.
|
||||
free(planets);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rndplanet = planets[RNG(0, nplanets-1)];
|
||||
free(planets);
|
||||
|
||||
lua_pushstring(L, rndplanet);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_getSystem(lua_State* L) {
|
||||
LLUA_MIN_ARGS(1);
|
||||
char* planetname, *system;
|
||||
|
||||
if(lua_isstring(L, -1)) planetname = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
system = planet_getSystem(planetname);
|
||||
lua_pushstring(L, system);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_landName(lua_State* L) {
|
||||
if(landed) {
|
||||
lua_pushstring(L, land_planet->name);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int space_systemName(lua_State* L) {
|
||||
lua_pushstring(L, cur_system->name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_jumpDist(lua_State* L) {
|
||||
LLUA_MIN_ARGS(1);
|
||||
StarSystem** s;
|
||||
int jumps;
|
||||
char* start, *goal;
|
||||
|
||||
if(lua_isstring(L, -1))
|
||||
start = (char*)lua_tostring(L, -1);
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
if((lua_gettop(L) > 1) && lua_isstring(L, -2))
|
||||
goal = (char*) lua_tostring(L, -2);
|
||||
else
|
||||
goal = cur_system->name;
|
||||
|
||||
s = system_getJumpPath(&jumps, start, goal);
|
||||
free(s);
|
||||
|
||||
lua_pushnumber(L, jumps);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Time. --
|
||||
static int time_get(lua_State* L) {
|
||||
lua_pushnumber(L, ltime_get());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int time_str(lua_State* L) {
|
||||
char* lt;
|
||||
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1)))
|
||||
lt = ltime_pretty((unsigned int) lua_tonumber(L, -1));
|
||||
else
|
||||
lt = ltime_pretty(ltime_get());
|
||||
lua_pushstring(L, lt);
|
||||
free(lt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int time_units(lua_State* L) {
|
||||
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1)))
|
||||
lua_pushnumber(L, (unsigned int) lua_tonumber(L, -1) * LTIME_UNIT_LENGTH);
|
||||
else
|
||||
lua_pushnumber(L, LTIME_UNIT_LENGTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- RND. --
|
||||
static int rnd_int(lua_State* L) {
|
||||
int o;
|
||||
|
||||
o = lua_gettop(L);
|
||||
|
||||
if(o == 0) lua_pushnumber(L, RNGF()); // Random double o <= x <= 1.
|
||||
else if(o == 1) {
|
||||
// Random int 0 <= x <= param.
|
||||
if(lua_isnumber(L, -1))
|
||||
lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, -1)));
|
||||
else return 0;
|
||||
}
|
||||
else if(o >= 2) {
|
||||
// Random int param 1 <= x <= param 2.
|
||||
if(lua_isnumber(L, -1) && lua_isnumber(L, -2))
|
||||
lua_pushnumber(L, RNG((int)lua_tonumber(L, -2), (int)lua_tonumber(L, -1)));
|
||||
else return 0;
|
||||
}
|
||||
else return 0;
|
||||
|
||||
// Unless it's returned 0 already it'll always return param.
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Toolkit. --
|
||||
|
||||
static int tk_msg(lua_State* L) {
|
||||
char* title, *str;
|
||||
LLUA_MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
dialogue_msg(title, str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tk_yesno(lua_State* L) {
|
||||
int ret;
|
||||
char* title, *str;
|
||||
LLUA_MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
ret = dialogue_YesNo(title, str);
|
||||
lua_pushboolean(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tk_input(lua_State* L) {
|
||||
char* title, *str;
|
||||
int min, max;
|
||||
LLUA_MIN_ARGS(4);
|
||||
|
||||
if(lua_isstring(L, -4)) title = (char*) lua_tostring(L, -4);
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -3)) min = (int) lua_tonumber(L, -3);
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -2)) max = (int) lua_tonumber(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
dialogue_input(title, min, max, str);
|
||||
return 0;
|
||||
}
|
||||
|
10
src/llua.h
Normal file
10
src/llua.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "lua.h"
|
||||
|
||||
// Individual libraries.
|
||||
int lua_loadLephisto(lua_State* L); // Always read only.
|
||||
int lua_loadSpace(lua_State* L, int readonly);
|
||||
int lua_loadTime(lua_State* L, int readonly);
|
||||
int lua_loadRnd(lua_State* L); // Always read only.
|
||||
int lua_loadTk(lua_State* L); // Always read only.
|
||||
|
16
src/lluadef.h
Normal file
16
src/lluadef.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#define LLUA_DEBUG(str, args...) \
|
||||
(fprintf(stdout, "Lua: "str"\n", ## args))
|
||||
|
||||
#define LLUA_INVALID_PARAMETER() { \
|
||||
LLUA_DEBUG("[%s] Invalid parameter", __func__); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define LLUA_MIN_ARGS(n) \
|
||||
if(lua_gettop(L) < n) { \
|
||||
LLUA_DEBUG("[%s] Too few arguments", __func__); \
|
||||
return 0; \
|
||||
}
|
||||
|
380
src/misn_lua.c
380
src/misn_lua.c
@ -6,6 +6,8 @@
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
|
||||
#include "llua.h"
|
||||
#include "lluadef.h"
|
||||
#include "hook.h"
|
||||
#include "mission.h"
|
||||
#include "log.h"
|
||||
@ -20,20 +22,6 @@
|
||||
#include "xml.h"
|
||||
#include "misn_lua.h"
|
||||
|
||||
#define MISN_DEBUG(str, args...) \
|
||||
(fprintf(stdout, "Mission '%s': "str"\n", cur_mission->data->name, ## args))
|
||||
|
||||
#define MISN_INVALID_PARAMETER() { \
|
||||
MISN_DEBUG("Invalid parameter."); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define MIN_ARGS(n) \
|
||||
if(lua_gettop(L) < n) { \
|
||||
MISN_DEBUG("[%s] Too few arguments", __func__); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
// Similar to lua vars, but with less variety.
|
||||
#define MISN_VAR_NIL 0
|
||||
#define MISN_VAR_NUM 1
|
||||
@ -64,13 +52,6 @@ static unsigned int hook_generic(lua_State* L, char* stack);
|
||||
|
||||
// -- Libraries. --
|
||||
|
||||
// Lephisto.
|
||||
static int lephisto_lang(lua_State* L);
|
||||
static const luaL_reg lephisto_methods[] = {
|
||||
{ "lang", lephisto_lang },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Mission.
|
||||
static int misn_setTitle(lua_State* L);
|
||||
static int misn_setDesc(lua_State* L);
|
||||
@ -105,32 +86,6 @@ static const luaL_reg var_cond_methods[] = {
|
||||
{0, 0 }
|
||||
};
|
||||
|
||||
// Space.
|
||||
static int space_getPlanet(lua_State* L);
|
||||
static int space_getSystem(lua_State* L);
|
||||
static int space_landName(lua_State* L);
|
||||
static int space_systemName(lua_State* L);
|
||||
static int space_jumpDist(lua_State* L);
|
||||
static const luaL_reg space_methods[] = {
|
||||
{ "getPlanet", space_getPlanet },
|
||||
{ "getSystem", space_getSystem },
|
||||
{ "landName", space_landName },
|
||||
{ "system", space_systemName },
|
||||
{ "jumpDist", space_jumpDist },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Time.
|
||||
static int time_get(lua_State* L);
|
||||
static int time_str(lua_State* L);
|
||||
static int time_units(lua_State* L);
|
||||
static const luaL_reg time_methods[] = {
|
||||
{ "get", time_get },
|
||||
{ "str", time_str },
|
||||
{ "units", time_units },
|
||||
{0, 0}
|
||||
};
|
||||
|
||||
// Player.
|
||||
static int player_getname(lua_State* L);
|
||||
static int player_shipname(lua_State* L);
|
||||
@ -154,24 +109,6 @@ static const luaL_reg player_methods[] = {
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// RND.
|
||||
static int rnd_int(lua_State*L);
|
||||
static const luaL_reg rnd_methods[] = {
|
||||
{ "int", rnd_int },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Toolkit.
|
||||
static int tk_msg(lua_State* L);
|
||||
static int tk_yesno(lua_State* L);
|
||||
static int tk_input(lua_State* L);
|
||||
static const luaL_reg tk_methods[] = {
|
||||
{ "msg", tk_msg },
|
||||
{ "yesno", tk_yesno },
|
||||
{ "input", tk_input },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
// Hooks.
|
||||
static int hook_land(lua_State* L);
|
||||
static int hook_takeoff(lua_State* L);
|
||||
@ -198,25 +135,54 @@ static const luaL_reg pilot_methods[] = {
|
||||
|
||||
// Register all the libaries.
|
||||
int misn_loadLibs(lua_State* L) {
|
||||
luaL_register(L, "lephisto", lephisto_methods);
|
||||
luaL_register(L, "misn", misn_methods);
|
||||
luaL_register(L, "var", var_methods);
|
||||
luaL_register(L, "space", space_methods);
|
||||
luaL_register(L, "time", time_methods);
|
||||
luaL_register(L, "player", player_methods);
|
||||
luaL_register(L, "rnd", rnd_methods);
|
||||
luaL_register(L, "tk", tk_methods);
|
||||
luaL_register(L, "hook", hook_methods);
|
||||
luaL_register(L, "pilot", pilot_methods);
|
||||
lua_loadLephisto(L);
|
||||
lua_loadMisn(L);
|
||||
lua_loadVar(L, 0);
|
||||
lua_loadSpace(L, 0);
|
||||
lua_loadTime(L, 0);
|
||||
lua_loadPlayer(L);
|
||||
lua_loadRnd(L);
|
||||
lua_loadTk(L);
|
||||
lua_loadHook(L);
|
||||
lua_loadPilot(L);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int misn_loadCondLibs(lua_State* L) {
|
||||
luaL_register(L, "time", time_methods);
|
||||
lua_loadTime(L, 1);
|
||||
lua_loadVar(L, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Individual libarary loading.
|
||||
int lua_loadMisn(lua_State* L) {
|
||||
luaL_register(L, "misn", misn_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadVar(lua_State* L, int readonly) {
|
||||
if(readonly == 0)
|
||||
luaL_register(L, "var", var_methods);
|
||||
else
|
||||
luaL_register(L, "var", var_cond_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadPlayer(lua_State* L) {
|
||||
luaL_register(L, "player", player_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadHook(lua_State* L) {
|
||||
luaL_register(L, "hook", hook_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lua_loadPilot(lua_State* L) {
|
||||
luaL_register(L, "pilot", pilot_methods);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Run a mission function.
|
||||
//
|
||||
// -1 on error, 1 on misn.finish() call and 0 normally.
|
||||
@ -358,17 +324,10 @@ static int var_add(misn_var* new_var) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -- Lephisto. --
|
||||
static int lephisto_lang(lua_State* L) {
|
||||
// TODO: multilanguage stuff.
|
||||
lua_pushstring(L, "en");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Mission. --
|
||||
|
||||
static int misn_setTitle(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
if(lua_isstring(L, -1)) {
|
||||
if(cur_mission->title)
|
||||
// Cleanup the old title.
|
||||
@ -379,7 +338,7 @@ static int misn_setTitle(lua_State* L) {
|
||||
}
|
||||
|
||||
static int misn_setDesc(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
if(lua_isstring(L, -1)) {
|
||||
if(cur_mission->desc)
|
||||
// Cleanup the old description.
|
||||
@ -390,7 +349,7 @@ static int misn_setDesc(lua_State* L) {
|
||||
}
|
||||
|
||||
static int misn_setReward(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
if(lua_isstring(L, -1)) {
|
||||
if(cur_mission->reward)
|
||||
// Cleanup the old reward.
|
||||
@ -469,13 +428,13 @@ int var_checkflag(char* str) {
|
||||
}
|
||||
|
||||
static int var_peek(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
int i;
|
||||
char* str;
|
||||
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else {
|
||||
MISN_DEBUG("Trying to peek a var with non-string name");
|
||||
LLUA_DEBUG("Trying to peek a var with non-string name");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -502,13 +461,13 @@ static int var_peek(lua_State* L) {
|
||||
}
|
||||
|
||||
static int var_pop(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
int i;
|
||||
char* str;
|
||||
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else {
|
||||
MISN_DEBUG("Trying to pop a var with non-string name");
|
||||
LLUA_DEBUG("Trying to pop a var with non-string name");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -519,18 +478,18 @@ static int var_pop(lua_State* L) {
|
||||
var_stack--;
|
||||
return 0;
|
||||
}
|
||||
MISN_DEBUG("Var '%s' not found in stack", str);
|
||||
LLUA_DEBUG("Var '%s' not found in stack", str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int var_push(lua_State* L) {
|
||||
MIN_ARGS(2);
|
||||
LLUA_MIN_ARGS(2);
|
||||
char* str;
|
||||
misn_var var;
|
||||
|
||||
if(lua_isstring(L, -2)) str = (char*) lua_tostring(L, -2);
|
||||
else {
|
||||
MISN_DEBUG("Trying to push a var with non-string name");
|
||||
LLUA_DEBUG("Trying to push a var with non-string name");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -551,7 +510,7 @@ static int var_push(lua_State* L) {
|
||||
var.type = MISN_VAR_STR;
|
||||
var.d.str = strdup((char*)lua_tostring(L, -1));
|
||||
} else {
|
||||
MISN_DEBUG("Trying to push a var of invalid data type to stack");
|
||||
LLUA_DEBUG("Trying to push a var of invalid data type to stack");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -590,130 +549,6 @@ void var_cleanup(void) {
|
||||
var_mstack = 0;
|
||||
}
|
||||
|
||||
// -- Space. --
|
||||
static int space_getPlanet(lua_State* L) {
|
||||
int i;
|
||||
int *factions;
|
||||
int nfactions;
|
||||
char** planets;
|
||||
int nplanets;
|
||||
char* rndplanet;
|
||||
|
||||
if(lua_gettop(L) == 0) {
|
||||
// Get random planet.
|
||||
lua_pushstring(L, space_getRndPlanet());
|
||||
return 1;
|
||||
}
|
||||
else if(lua_isnumber(L, -1)) {
|
||||
i = lua_tonumber(L, -1);
|
||||
planets = space_getFactionPlanet(&nplanets, &i, 1);
|
||||
}
|
||||
else if(lua_isstring(L, -1)) {
|
||||
i = faction_get((char*) lua_tostring(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);
|
||||
}
|
||||
else return 0; // Nothing useful.
|
||||
|
||||
// Choose random planet.
|
||||
if(nplanets == 0) {
|
||||
// No suitable planet.
|
||||
free(planets);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rndplanet = planets[RNG(0, nplanets-1)];
|
||||
free(planets);
|
||||
|
||||
lua_pushstring(L, rndplanet);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_getSystem(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
char* planetname, *system;
|
||||
|
||||
if(lua_isstring(L, -1)) planetname = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
system = planet_getSystem(planetname);
|
||||
lua_pushstring(L, system);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_landName(lua_State* L) {
|
||||
if(landed) {
|
||||
lua_pushstring(L, land_planet->name);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int space_systemName(lua_State* L) {
|
||||
lua_pushstring(L, cur_system->name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int space_jumpDist(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
StarSystem** s;
|
||||
int jumps;
|
||||
char* start, *goal;
|
||||
|
||||
if(lua_isstring(L, -1))
|
||||
start = (char*)lua_tostring(L, -1);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
|
||||
if((lua_gettop(L) > 1) && lua_isstring(L, -2))
|
||||
goal = (char*) lua_tostring(L, -2);
|
||||
else
|
||||
goal = cur_system->name;
|
||||
|
||||
s = system_getJumpPath(&jumps, start, goal);
|
||||
free(s);
|
||||
|
||||
lua_pushnumber(L, jumps);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Time. --
|
||||
static int time_get(lua_State* L) {
|
||||
lua_pushnumber(L, ltime_get());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int time_str(lua_State* L) {
|
||||
char* lt;
|
||||
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1)))
|
||||
lt = ltime_pretty((unsigned int) lua_tonumber(L, -1));
|
||||
else
|
||||
lt = ltime_pretty(ltime_get());
|
||||
lua_pushstring(L, lt);
|
||||
free(lt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int time_units(lua_State* L) {
|
||||
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1)))
|
||||
lua_pushnumber(L, (unsigned int) lua_tonumber(L, -1) * LTIME_UNIT_LENGTH);
|
||||
else
|
||||
lua_pushnumber(L, LTIME_UNIT_LENGTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Player. --
|
||||
|
||||
static int player_getname(lua_State* L) {
|
||||
@ -735,7 +570,7 @@ static int player_addCargo(lua_State* L) {
|
||||
Commodity* cargo;
|
||||
int quantity, ret;
|
||||
|
||||
MIN_ARGS(2);
|
||||
LLUA_MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) cargo = commodity_get((char*) lua_tostring(L, -2));
|
||||
else return 0;
|
||||
@ -753,7 +588,7 @@ static int player_rmCargo(lua_State* L) {
|
||||
int ret;
|
||||
unsigned int id;
|
||||
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
|
||||
if(lua_isnumber(L, -1)) id = (unsigned int) lua_tonumber(L, -1);
|
||||
else return 0;
|
||||
@ -768,7 +603,7 @@ static int player_rmCargo(lua_State* L) {
|
||||
static int player_pay(lua_State* L) {
|
||||
int money;
|
||||
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
|
||||
if(lua_isnumber(L, -1)) money = (int) lua_tonumber(L, -1);
|
||||
else return 0;
|
||||
@ -779,7 +614,7 @@ static int player_pay(lua_State* L) {
|
||||
}
|
||||
|
||||
static int player_msg(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
char* str;
|
||||
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
@ -790,14 +625,14 @@ static int player_msg(lua_State* L) {
|
||||
}
|
||||
|
||||
static int player_modFaction(lua_State* L) {
|
||||
MIN_ARGS(2);
|
||||
LLUA_MIN_ARGS(2);
|
||||
int f, mod;
|
||||
|
||||
if(lua_isstring(L, -2)) f = faction_get(lua_tostring(L, -2));
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
if(lua_isnumber(L, -1)) mod = (int)lua_tonumber(L, -1);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
faction_modPlayer(f, mod);
|
||||
|
||||
@ -805,95 +640,22 @@ static int player_modFaction(lua_State* L) {
|
||||
}
|
||||
|
||||
static int player_getFaction(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
int f;
|
||||
|
||||
if(lua_isstring(L, -1)) f = faction_get(lua_tostring(L, -1));
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
lua_pushnumber(L, faction_getPlayer(f));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- RND. --
|
||||
static int rnd_int(lua_State* L) {
|
||||
int o;
|
||||
|
||||
o = lua_gettop(L);
|
||||
|
||||
if(o == 0) lua_pushnumber(L, RNGF()); // Random double o <= x <= 1.
|
||||
else if(o == 1) {
|
||||
// Random int 0 <= x <= param.
|
||||
if(lua_isnumber(L, -1))
|
||||
lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, -1)));
|
||||
else return 0;
|
||||
}
|
||||
else if(o >= 2) {
|
||||
// Random int param 1 <= x <= param 2.
|
||||
if(lua_isnumber(L, -1) && lua_isnumber(L, -2))
|
||||
lua_pushnumber(L, RNG((int)lua_tonumber(L, -2), (int)lua_tonumber(L, -1)));
|
||||
else return 0;
|
||||
}
|
||||
else return 0;
|
||||
|
||||
// Unless it's returned 0 already it'll always return param.
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -- Toolkit. --
|
||||
|
||||
static int tk_msg(lua_State* L) {
|
||||
char* title, *str;
|
||||
MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
dialogue_msg(title, str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tk_yesno(lua_State* L) {
|
||||
int ret;
|
||||
char* title, *str;
|
||||
MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
ret = dialogue_YesNo(title, str);
|
||||
lua_pushboolean(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tk_input(lua_State* L) {
|
||||
char* title, *str;
|
||||
int min, max;
|
||||
MIN_ARGS(4);
|
||||
|
||||
if(lua_isstring(L, -4)) title = (char*) lua_tostring(L, -4);
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -3)) min = (int) lua_tonumber(L, -3);
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -2)) max = (int) lua_tonumber(L, -2);
|
||||
else return 0;
|
||||
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1);
|
||||
else return 0;
|
||||
|
||||
dialogue_input(title, min, max, str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -- HOOK --
|
||||
static unsigned int hook_generic(lua_State* L, char* stack) {
|
||||
int i;
|
||||
char* func;
|
||||
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
|
||||
// Make sure mission is a player mission.
|
||||
for(i = 0; i < MISSION_MAX; i++)
|
||||
@ -934,11 +696,11 @@ static int hook_enter(lua_State* L) {
|
||||
}
|
||||
|
||||
static int hook_pilotDeath(lua_State* L) {
|
||||
MIN_ARGS(2);
|
||||
LLUA_MIN_ARGS(2);
|
||||
unsigned int h, p;
|
||||
|
||||
if(lua_isnumber(L, -2)) p = (unsigned int) lua_tonumber(L, -2);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
h = hook_generic(L, "death"); // We won't actually call the death stack directly.
|
||||
pilot_addHook(pilot_get(p), PILOT_HOOK_DEATH, h);
|
||||
@ -948,7 +710,7 @@ static int hook_pilotDeath(lua_State* L) {
|
||||
|
||||
// -- Pilot. --
|
||||
static int pilot_addFleet(lua_State* L) {
|
||||
MIN_ARGS(1);
|
||||
LLUA_MIN_ARGS(1);
|
||||
Fleet* flt;
|
||||
char* fltname;
|
||||
int i, j;
|
||||
@ -957,12 +719,12 @@ static int pilot_addFleet(lua_State* L) {
|
||||
Vec2 vv, vp, vn;
|
||||
|
||||
if(lua_isstring(L, -1)) fltname = (char*) lua_tostring(L, -1);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
// Pull the fleet.
|
||||
flt = fleet_get(fltname);
|
||||
if(flt == NULL) {
|
||||
MISN_DEBUG("Fleet not found!");
|
||||
LLUA_DEBUG("Fleet not found!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1000,15 +762,15 @@ static int pilot_addFleet(lua_State* L) {
|
||||
}
|
||||
|
||||
static int pilot_rename(lua_State* L) {
|
||||
MIN_ARGS(2);
|
||||
LLUA_MIN_ARGS(2);
|
||||
char* name;
|
||||
unsigned int id;
|
||||
Pilot* p;
|
||||
|
||||
if(lua_isnumber(L, -2)) id = (unsigned int) lua_tonumber(L, -2);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
if(lua_isstring(L, -1)) name = (char*) lua_tostring(L, -1);
|
||||
else MISN_INVALID_PARAMETER();
|
||||
else LLUA_INVALID_PARAMETER();
|
||||
|
||||
p = pilot_get(id);
|
||||
free(p->name);
|
||||
|
@ -10,3 +10,10 @@ void var_cleanup(void);
|
||||
int misn_loadLibs(lua_State* L);
|
||||
int misn_loadCondLibs(lua_State* L); // Safe read only stuff.
|
||||
|
||||
// Individual library stuff.
|
||||
int lua_loadMisn(lua_State* L);
|
||||
int lua_loadVar(lua_State* L, int readonly);
|
||||
int lua_loadPlayer(lua_State* L);
|
||||
int lua_loadHook(lua_State* L);
|
||||
int lua_loadPilot(lua_State* L);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user