[Change] Stage one of updating lua API to use metatables.

This commit is contained in:
Allanis 2013-08-25 16:20:41 +01:00
parent 9c8ab865ab
commit 2c5d3433b5
12 changed files with 361 additions and 235 deletions

View File

@ -32,17 +32,19 @@ end
-- Create the mission. -- Create the mission.
function create() function create()
local landed = space.getLanded()
-- Target destination. -- Target destination.
local i = 0 local i = 0
repeat repeat
planet = space.getPlanet(misn.factions()) planet = space.getPlanet(misn.factions())
i = i + 1 i = i + 1
until planet ~= space.landName() or i > 10 until planet ~= landed or i > 10
-- Infinite loop protection. -- Infinite loop protection.
if i > 10 then if i > 10 then
misn.finish(false) misn.finish(false)
end end
system = space.getSystem(planet) system = space.getSystem(planet:name())
misn.setMarker(system) -- Mark the system. misn.setMarker(system) -- Mark the system.
misn_dist = space.jumpDist(system) misn_dist = space.jumpDist(system)
@ -52,11 +54,11 @@ function create()
misn_type = "Cargo" misn_type = "Cargo"
misn_faction = rnd.int(2) misn_faction = rnd.int(2)
i = rnd.int(3) i = rnd.int(3)
misn.setTitle(string.format(title[i+1], planet)) misn.setTitle(string.format(title[i+1], planet:name()))
elseif i < 6 then-- Rush delivery. elseif i < 6 then-- Rush delivery.
misn_type = "Rush" misn_type = "Rush"
misn_faction = rnd.int(5) misn_faction = rnd.int(5)
misn.setTitle(string.format(title[11], planet)) misn.setTitle(string.format(title[11], planet:name()))
else -- People delivery :P else -- People delivery :P
misn_type = "People" misn_type = "People"
misn.faction = rnd.int(1) misn.faction = rnd.int(1)
@ -70,7 +72,7 @@ function create()
carg_type = "Pilgrims" carg_type = "Pilgrims"
end end
i = rnd.int(1) i = rnd.int(1)
misn_setTitle(string.format(title[i+21], carg_type, planet)) misn_setTitle(string.format(title[i+21], carg_type, planet:name()))
end end
-- More mission specifics. -- More mission specifics.
@ -91,21 +93,21 @@ function create()
end end
if misn_type == "Cargo" then if misn_type == "Cargo" then
misn.setDesc(string.format( misn_desc[1], planet, system, carg_mass, carg_type)) misn.setDesc(string.format( misn_desc[1], planet:name(), system, carg_mass, carg_type))
reward = misn_dist * carg_mass * (250+rnd.int(150)) + reward = misn_dist * carg_mass * (250+rnd.int(150)) +
carg_mass * (150+rnd.int(75)) + carg_mass * (150+rnd.int(75)) +
rnd.int(1500) rnd.int(1500)
elseif misn_type == "Rush" then elseif misn_type == "Rush" then
misn_time = time.get() + time.units(2) + misn_time = time.get() + time.units(2) +
rnd.int(time.units(2), time.units(4)) * misn_dist rnd.int(time.units(2), time.units(4)) * misn_dist
misn.setDesc(string.format( misn_desc[11], planet, system, misn.setDesc(string.format( misn_desc[11], planet:name(), system,
carg_mass, carg_type, carg_mass, carg_type,
time.str(misn_time), time.str(misn_time-time.get()))) time.str(misn_time), time.str(misn_time-time.get())))
reward = misn_dist * carg_mass * (450+rnd.int(250)) + reward = misn_dist * carg_mass * (450+rnd.int(250)) +
carg_mass * (250+rnd.int(125)) + carg_mass * (250+rnd.int(125)) +
rnd.int(2500) rnd.int(2500)
else -- People. else -- People.
misn.setDesc(string.format(misn_desc[21], carg_type, planet, system)) misn.setDesc(string.format(misn_desc[21], carg_type, planet:name(), system))
reward = misn_dist * (1000 + rnd.int(500)) + rnd.int(2000) reward = misn_dist * (1000 + rnd.int(500)) + rnd.int(2000)
end end
misn.setReward(string.format(misn_reward, reward)) misn.setReward(string.format(misn_reward, reward))
@ -139,7 +141,8 @@ end
-- Land hook. -- Land hook.
function land() function land()
if space.landName() == planet then local landed = space.getLanded()
if landed == planet then
if player.rmCargo(carg_id) then if player.rmCargo(carg_id) then
player.pay( reward ) player.pay( reward )
tk.msg(finish_title, string.format( finish_msg, carg_type)) tk.msg(finish_title, string.format( finish_msg, carg_type))
@ -168,7 +171,7 @@ function timeup()
player.msg(misn_time_msg) player.msg(misn_time_msg)
misn.finish(false) misn.finish(false)
end end
misn.setDesc(string.format( misn_desc[21], planet, system, misn.setDesc(string.format( misn_desc[21], planet:name(), system,
carg_mass, carg_type, carg_mass, carg_type,
time.str(misn_time), time.str(misn_time-time.get()))) time.str(misn_time), time.str(misn_time-time.get())))
end end

View File

@ -20,17 +20,19 @@ You aren't too sure of what to make of your encounter with the Empire, only time
end end
function create() function create()
local landed = space.getLanded()
-- Target destination. -- Target destination.
local i = 0 local i = 0
repeat repeat
dest = space.getPlanet(misn.factions()) dest = space.getPlanet(misn.factions())
i = i+1 i = i+1
until dest ~= space.landName() or i > 10 until dest ~= landed or i > 10
-- Infinate loop protection. -- Infinate loop protection.
if i > 10 then if i > 10 then
misn.finish(false) misn.finish(false)
end end
system = space.getSystem(dest) system = space.getSystem(dest:name())
misn.setMarker(system) misn.setMarker(system)
-- Intro text. -- Intro text.
@ -43,10 +45,10 @@ function create()
reward = 3000 reward = 3000
misn.setTitle(misn_title) misn.setTitle(misn_title)
misn.setReward(string.format(misn_reward, reward)) misn.setReward(string.format(misn_reward, reward))
misn.setDesc(string.format(misn_desc,dest, system)) misn.setDesc(string.format(misn_desc, dest:name(), system))
-- Flavour text and mini-briefing. -- Flavour text and mini-briefing.
tk.msg(title[2], string.format( text[3], dest)) tk.msg(title[2], string.format( text[3], dest:name()))
-- Set up the goal. -- Set up the goal.
parcels = player.addCargo("Parcels", 0) parcels = player.addCargo("Parcels", 0)
@ -55,11 +57,12 @@ function create()
end end
function land() function land()
if space.landName() == dest then local landed = space.getLanded()
if landed == dest then
if player.rmCargo(parcels) then if player.rmCargo(parcels) then
player.pay(reward) player.pay(reward)
-- More flavour text. -- More flavour text.
tk.msg(title[3], string.format(text[4], dest)) tk.msg(title[3], string.format(text[4], dest:name()))
var.push("es_cargo", true) var.push("es_cargo", true)
player.modFaction("Empire", 3) player.modFaction("Empire", 3)
misn.finish(true) misn.finish(true)

View File

@ -31,7 +31,7 @@ function create()
misn_stage = 0 misn_stage = 0
misn_nearby = "Coriolis" misn_nearby = "Coriolis"
misn_target = "Dune" misn_target = "Dune"
misn_base = "Omega Station" misn_base = space.getPlanet("Omega Station")
misn_base_sys = "NGC-7291" misn_base_sys = "NGC-7291"
misn.setMarker(misn_nearby) -- Not exact target. misn.setMarker(misn_nearby) -- Not exact target.
@ -41,7 +41,7 @@ function create()
misn.setDesc(string.format(misn_desc[1],misn_nearby)) misn.setDesc(string.format(misn_desc[1],misn_nearby))
-- Flavour text and mini-briefing. -- Flavour text and mini-briefing.
tk.msg(title[2], string.format(text[2], misn_nearby, misn_base, misn_base_sys)) tk.msg(title[2], string.format(text[2], misn_nearby, misn_base:name(), misn_base_sys))
hook.enter("enter") hook.enter("enter")
hook.land("land") hook.land("land")
@ -64,14 +64,14 @@ function enter()
-- update mission. -- update mission.
if misn_stage == 0 and sys == misn_target then if misn_stage == 0 and sys == misn_target then
misn.setDesc(string.format(misn_desc[2],misn_base,misn_base_sys)) misn.setDesc(string.format(misn_desc[2], misn_base:name() ,misn_base_sys))
misn_stage = 1 misn_stage = 1
misn.setMarker(misn_base_sys) -- Now we mark return to base. misn.setMarker(misn_base_sys) -- Now we mark return to base.
end end
end end
function land() function land()
planet = space.landName() planet = space.getLanded()
if misn_stage == 1 and planet == misn_base then if misn_stage == 1 and planet == misn_base then
tk.msg(title[3], string.format(text[3],misn_target)) tk.msg(title[3], string.format(text[3],misn_target))

View File

@ -26,7 +26,8 @@ function create()
misn_stage = 0 misn_stage = 0
systems_visited = 0 -- Number of Collective systems visited. systems_visited = 0 -- Number of Collective systems visited.
misn_base = "Omega Station" misn_base = space.getPlanet("Omega Station")
misn_base_sys = space.getSystem(misn_base:name())
-- Mission details. -- Mission details.
misn.setTitle(misn_title) misn.setTitle(misn_title)
@ -49,16 +50,16 @@ function enter()
-- Visited enough systems. -- Visited enough systems.
if misn_stage == 0 and systems_visited >= 2 then if misn_stage == 0 and systems_visited >= 2 then
misn.setDesc(string.format(misn_desc[2], misn.setDesc(string.format(misn_desc[2],
misn_base, space.getSystem(misn_base))) misn_base:name(), space.getSystem(misn_base)))
misn_stage = 1 misn_stage = 1
misn.setMarker(space.getsystem(misn_base)) -- Now we mark return to base. misn.setMarker(misn_base_sys) -- Now we mark return to base.
hook.land("land") hook.land("land")
end end
end end
end end
function land() function land()
planet = space.landName() planet = space.getLanded()
if misn_stage == 1 and planet == misn_base then if misn_stage == 1 and planet == misn_base then
tk.msg(title[3], text[3]) tk.msg(title[3], text[3])

View File

@ -29,30 +29,30 @@ function create()
misn_stage = 0 misn_stage = 0
systems_visited = 0 -- Number of Collective systems visited. systems_visited = 0 -- Number of Collective systems visited.
misn_base = "Omega Station" misn_base, misn_base_sys = space.getPlanet("Omega Station")
misn_target = "Eiroik" misn_target, misn_target_sys = space.getPlanet("Eiroik")
misn.setMarker(space.getSystem(misn_target)) misn.setMarker(misn_target_sys)
-- Mission details. -- Mission details.
misn.setTitle(misn_title) misn.setTitle(misn_title)
misn.setReward(misn_reward) misn.setReward(misn_reward)
misn.setDesc(string.format(misn_desc[1], misn_target, space.getSystem(misn_target))) misn.setDesc(string.format(misn_desc[1], misn_target:name(), misn_target_sys))
tk.msg(title[1], string.format(text[2], misn_target)) tk.msg(title[1], string.format(text[2], misn_target:name()))
tk.msg(title[1], string.format(text[3]. misn_target, space_getSystem(misn_target))) tk.msg(title[1], string.format(text[3]. misn_target:name(), misn_target_sys))
hook.enter("land") hook.enter("land")
end end
end end
function land() function land()
planet = space.landName() planet = space.getLanded()
-- First mission part is landing on the planet. -- First mission part is landing on the planet.
if misn_stage == 0 and planet == misn_target then if misn_stage == 0 and planet == misn_target then
tk.msg(title[1], string.format(text[4], misn_target)) tk.msg(title[1], string.format(text[4], misn_target:name()))
misn_stage = 1 misn_stage = 1
misn.setMarker(space.getSystem(misn_base)) misn.setMarker(misn_base_sys)
-- Return bit. -- Return bit.
elseif misn_stage == 1 and planet == misn_base then elseif misn_stage == 1 and planet == misn_base then

View File

@ -27,22 +27,22 @@ end
function create() function create()
-- Target destination. -- Target destination.
local i = 0 local i = 0
local landed = space.getLanded()
repeat repeat
planet = space.getPlanet(misn.factions()) planet, system = space.getPlanet(misn.factions())
i = i + 1 i = i + 1
until planet ~= space.landName() or i > 10 until planet ~= landed or i > 10
-- Infinite loop protection. -- Infinite loop protection.
if i > 10 then if i > 10 then
misn.finish(false) misn.finish(false)
end end
system = space.getSystem( planet )
misn.setMarker(system) -- Set system marker. misn.setMarker(system) -- Set system marker.
misn_dist = space.jumpDist(system) misn_dist = space.jumpDist(system)
-- Mission generics. -- Mission generics.
misn_type = "Cargo" misn_type = "Cargo"
i = rnd.int(1) i = rnd.int(1)
misn.setTitle(string.format(title[i+1], planet)) misn.setTitle(string.format(title[i+1], planet:name()))
-- More mission specifics. -- More mission specifics.
carg_mass = rnd.int(10, 30) carg_mass = rnd.int(10, 30)
@ -61,7 +61,7 @@ function create()
misn_time = time.get() + time.units(5) + misn_time = time.get() + time.units(5) +
rnd.int(time.units(5), time.units(8)) * misn_dist rnd.int(time.units(5), time.units(8)) * misn_dist
misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet, system, misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet:name(),, system,
time.str(misn_time), time.str(misn_time-time.get()))) time.str(misn_time), time.str(misn_time-time.get())))
reward = misn_dist * carg_mass * (500+rnd.int(250)) + reward = misn_dist * carg_mass * (500+rnd.int(250)) +
carg_mass * (250+rnd.int(150)) + carg_mass * (250+rnd.int(150)) +
@ -87,7 +87,8 @@ end
-- Land hook. -- Land hook.
function land() function land()
if space.landName() == planet then local landed = space.getLanded()
if landed == planet then
if player.rmCargo(carg_id) then if player.rmCargo(carg_id) then
player.pay(reward) player.pay(reward)
tk.msg(finish_title, string.format(finish_msg, carg_type)) tk.msg(finish_title, string.format(finish_msg, carg_type))
@ -118,7 +119,7 @@ function timeup()
player.msg(miss_timeup) player.msg(miss_timeup)
misn.finish(false) misn.finish(false)
end end
misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet, system, misn.setDesc(string.format( misn_desc, carg_mass, carg_type, planet:name(), system,
time.str(misn_time), time.str(misn_time-time.get()))) time.str(misn_time), time.str(misn_time-time.get())))
end end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 423 KiB

After

Width:  |  Height:  |  Size: 423 KiB

View File

@ -14,8 +14,8 @@ function choose(str)
music.play() music.play()
elseif str == "land" then elseif str == "land" then
planet = space.landName() planet = space.getLanded()
class = space.planetClass(planet) class = planet:class()
if class == "M" then if class == "M" then
mus = "agriculture" mus = "agriculture"
@ -24,7 +24,7 @@ function choose(str)
elseif class == "P" then elseif class == "P" then
mus = "snow" mus = "snow"
else else
if space.planetServices(planet) > 0 then if planet:services() > 0 then
mus = "cosmostation" mus = "cosmostation"
else else
mus = "agriculture" mus = "agriculture"

View File

@ -23,27 +23,6 @@ static const luaL_reg lephisto_methods[] = {
{ 0, 0 } { 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 int space_faction(lua_State* L);
static int space_planetClass(lua_State* L);
static int space_planetServices(lua_State* L);
static const luaL_reg space_methods[] = {
{ "getPlanet", space_getPlanet },
{ "getSystem", space_getSystem },
{ "landName", space_landName },
{ "system", space_systemName },
{ "jumpDist", space_jumpDist },
{ "spaceFaction", space_faction },
{ "planetClass", space_planetClass },
{ "planetServices", space_planetServices },
{ 0, 0 }
};
/* Time. */ /* Time. */
static int time_get(lua_State* L); static int time_get(lua_State* L);
static int time_str(lua_State* L); static int time_str(lua_State* L);
@ -166,12 +145,6 @@ int lua_loadLephisto(lua_State* L) {
return 0; 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) { int lua_loadTime(lua_State* L, int readonly) {
(void)readonly; (void)readonly;
luaL_register(L, "time", time_methods); luaL_register(L, "time", time_methods);
@ -195,168 +168,6 @@ static int lephisto_lang(lua_State* L) {
return 1; 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, *sysname;
if(lua_isstring(L, 1)) planetname = (char*) lua_tostring(L, 1);
else return 0;
sysname = planet_getSystem(planetname);
lua_pushstring(L, sysname);
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 = map_getJumpPath(&jumps, start, goal, 1);
free(s);
lua_pushnumber(L, jumps);
return 1;
}
static int space_faction(lua_State* L) {
int i;
StarSystem* s;
/* Get the system. */
if(lua_isstring(L, 2))
s = system_get(lua_tostring(L, 1));
else s = cur_system;
/* Check if valid. */
if(s == NULL) {
LLUA_DEBUG("Invalid system!");
return 0;
}
/* Return the result in table. */
lua_newtable(L);
for(i = 0; i < s->nplanets; i++) {
lua_pushboolean(L, 1); /* Value. */
lua_setfield(L, -2, faction_name(s->planets[i].faction)); /* Key. */
/* Allows syntax foo = space.faction("foo"); if foo["bar"] then ... end */
}
return 1;
}
static int space_planetClass(lua_State* L) {
Planet* p;
char buf[2];
LLUA_MIN_ARGS(1);
/* Get planet. */
if(lua_isstring(L, 1))
p = planet_get((char*)lua_tostring(L,1));
else
LLUA_INVALID_PARAMETER();
if(p == NULL) return 0;
/* Return the class. */
buf[0] = planet_getClass(p);
buf[1] = '\0';
lua_pushstring(L, buf);
return 1;
}
static int space_planetServices(lua_State* L) {
Planet* p;
LLUA_MIN_ARGS(1);
/* Get planet. */
if(lua_isstring(L, 1))
p = planet_get((char*)lua_tostring(L,1));
else
LLUA_INVALID_PARAMETER();
if(p == NULL) return 0;
lua_pushnumber(L, p->services);
return 1;
}
/* -- Time. -- */ /* -- Time. -- */
static int time_get(lua_State* L) { static int time_get(lua_State* L) {
lua_pushnumber(L, ltime_get()); lua_pushnumber(L, ltime_get());

300
src/llua_space.c Normal file
View File

@ -0,0 +1,300 @@
#include "lauxlib.h"
#include "log.h"
#include "lephisto.h"
#include "rng.h"
#include "space.h"
#include "land.h"
#include "lluadef.h"
#include "map.h"
#include "llua_space.h"
#define PLANET_METATABLE "Planet"
#define SYSTEM_METATABLE "System"
/* Lua wrappers. */
typedef struct LuaPlanet_ {
Planet* p;
} LuaPlanet;
typedef struct LuaSystem_ {
StarSystem* s;
} LuaSystem;
static int planetL_createmetatable(lua_State* L);
/* Space. */
static int planetL_get(lua_State* L);
static int space_getSystem(lua_State* L);
static int spaceL_getLanded(lua_State* L);
static int space_systemName(lua_State* L);
static int space_jumpDist(lua_State* L);
static int space_faction(lua_State* L);
static const luaL_reg space_methods[] = {
{ "getPlanet", planetL_get },
{ "getSystem", space_getSystem },
{ "getLanded", spaceL_getLanded },
{ "system", space_systemName },
{ "jumpDist", space_jumpDist },
{ "faction", space_faction },
{ 0, 0 }
};
/* Planet metatable methods. */
static int planetL_eq(lua_State* L);
static int planetL_name(lua_State* L);
static int planetL_faction(lua_State* L);
static int planetL_class(lua_State* L);
static int planetL_services(lua_State* L);
static const luaL_reg planet_methods[] = {
{ "__eq", planetL_eq },
{ "__tostring", planetL_name },
{ "name", planetL_name },
{ "faction", planetL_faction },
{ "class", planetL_class },
{ "services", planetL_services },
{ 0, 0 }
};
/* Load the space library. */
int lua_loadSpace(lua_State* L, int readonly) {
(void)readonly;
/* Register the functions. */
luaL_register(L, "space", space_methods);
/* Register the metatables. */
planetL_createmetatable(L);
return 0;
}
/* -- SPACE -- */
/* Create a planet metatable from a planet and put it on top of the stack. */
static int planetL_createmetatable(lua_State* L) {
/* Create the metatable. */
luaL_newmetatable(L, PLANET_METATABLE);
/* Create the access table. */
lua_pushvalue(L, -1);
lua_setfield(L, -2,"__index");
/* Register the values. */
luaL_register(L, NULL, planet_methods);
return 0; /* No error. */
}
/* -- PLANET -- */
/* Get planet at index. */
static LuaPlanet* lua_toplanet(lua_State* L, int ind) {
if(lua_isuserdata(L, ind)) {
return (LuaPlanet*) lua_touserdata(L, ind);
}
luaL_typerror(L, ind, PLANET_METATABLE);
return NULL;
}
/* Push a planet on the stack. */
static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet) {
LuaPlanet* p;
p = (LuaPlanet*) lua_newuserdata(L, sizeof(LuaPlanet));
*p = planet;
luaL_getmetatable(L, PLANET_METATABLE);
lua_setmetatable(L, -2);
return p;
}
/* get a planet. */
static int planetL_get(lua_State* L) {
int i;
int* factions;
int nfactions;
char** planets;
int nplanets;
char* rndplanet;
LuaPlanet planet;
rndplanet = NULL;
nplanets = 0;
/* Get a random planet. */
if(lua_gettop(L) == 0) {
rndplanet = space_getRndPlanet();
}
/* Get a planet by faction */
else if(lua_isnumber(L, 1)) {
i = lua_tonumber(L, 1);
planets = space_getFactionPlanet(&nplanets, &i, 1);
}
/* Get a planet by name. */
else if(lua_isstring(L, 1)) {
i = faction_get((char*)lua_tostring(L, 1));
planets = space_getFactionPlanet(&nplanets, &i, 1);
}
/* Get a planet from faction list. */
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 LLUA_INVALID_PARAMETER(); /* Bad parameter. */
/* No suitable planet found. */
if((rndplanet == NULL) && (nplanets == 0)) {
free(planets);
return 0;
}
/* Pick random planet. */
else if(rndplanet == NULL) {
rndplanet = planets[RNG(0, nplanets-1)];
free(planets);
}
/* Push the planet. */
planet.p = planet_get(rndplanet); /* The real planet. */
lua_pushplanet(L, planet);
lua_pushstring(L, planet_getSystem(rndplanet));
return 2;
}
/* __eq (equality) metamethod for planets. */
static int planetL_eq(lua_State* L) {
LuaPlanet* a, *b;
a = lua_toplanet(L, 1);
b = lua_toplanet(L, 2);
if(a->p == b->p)
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
return 1;
}
/* Get the planets name. */
static int planetL_name(lua_State* L) {
LuaPlanet* p;
p = lua_toplanet(L, 1);
lua_pushstring(L, p->p->name);
return 1;
}
/* Get the planets faction. */
static int planetL_faction(lua_State* L) {
LuaPlanet* p;
p = lua_toplanet(L, 1);
lua_pushnumber(L, p->p->faction);
return 1;
}
/* Get the planets class. */
static int planetL_class(lua_State* L) {
char buf[2];
LuaPlanet* p;
p = lua_toplanet(L, 1);
buf[0] = planet_getClass(p->p);
buf[1] = '\0';
lua_pushstring(L, buf);
return 1;
}
static int planetL_services(lua_State* L) {
LuaPlanet* p;
p = lua_toplanet(L, 1);
lua_pushnumber(L, p->p->services);
return 1;
}
/* Get a system. */
static int space_getSystem(lua_State* L) {
LLUA_MIN_ARGS(1);
char* planetname, *sysname;
if(lua_isstring(L, 1)) planetname = (char*)lua_tostring(L, 1);
else LLUA_INVALID_PARAMETER();
sysname = planet_getSystem(planetname);
lua_pushstring(L, sysname);
return 1;
}
static int spaceL_getLanded(lua_State* L) {
LuaPlanet planet;
if(landed) {
planet.p = land_planet;
lua_pushplanet(L, planet);
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 = map_getJumpPath(&jumps, start, goal, 1);
free(s);
lua_pushnumber(L, jumps);
return 1;
}
static int space_faction(lua_State* L) {
int i;
StarSystem* s;
/* Get system. */
if(lua_isstring(L, 1))
s = system_get(lua_tostring(L, 1));
else s = cur_system;
/* Check if valid. */
if(s == NULL) {
LLUA_DEBUG("Invalid system!");
return 0;
}
/* Return result in table. */
lua_newtable(L);
for(i = 0; i < s->nplanets; i++) {
lua_pushboolean(L, 1); /* Value. */
lua_setfield(L, -2, faction_name(s->planets[i].faction)); /* Key. */
/* Allows syntax foo = space.faction("foo"); if foo["bar"] then ... end */
}
return 1;
}

6
src/llua_space.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include "lua.h"
/* Load the space library. */
int lua_loadSpace(lua_State* L, int readonly);

View File

@ -114,9 +114,10 @@ static int mission_init(Mission* mission, MissionData* misn, int load) {
/* Load the file. */ /* Load the file. */
buf = pack_readfile(DATA, misn->lua, &bufsize); buf = pack_readfile(DATA, misn->lua, &bufsize);
if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) { if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
ERR("Error loading mission file: %s", misn->lua); ERR("Error loading mission file: %s\n"
ERR("%s", lua_tostring(mission->L, -1)); "%s\n"
WARN("Most likely Lua file has improper syntax, please check"); "Most likely Lua file has improper syntax, please check",
misn->lua, lua_tostring(mission->L, -1));
return -1; return -1;
} }
free(buf); free(buf);