[Change] Stage one of updating lua API to use metatables.
This commit is contained in:
parent
9c8ab865ab
commit
2c5d3433b5
@ -32,17 +32,19 @@ end
|
||||
|
||||
-- Create the mission.
|
||||
function create()
|
||||
local landed = space.getLanded()
|
||||
|
||||
-- Target destination.
|
||||
local i = 0
|
||||
repeat
|
||||
planet = space.getPlanet(misn.factions())
|
||||
i = i + 1
|
||||
until planet ~= space.landName() or i > 10
|
||||
until planet ~= landed or i > 10
|
||||
-- Infinite loop protection.
|
||||
if i > 10 then
|
||||
misn.finish(false)
|
||||
end
|
||||
system = space.getSystem(planet)
|
||||
system = space.getSystem(planet:name())
|
||||
misn.setMarker(system) -- Mark the system.
|
||||
misn_dist = space.jumpDist(system)
|
||||
|
||||
@ -52,11 +54,11 @@ function create()
|
||||
misn_type = "Cargo"
|
||||
misn_faction = rnd.int(2)
|
||||
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.
|
||||
misn_type = "Rush"
|
||||
misn_faction = rnd.int(5)
|
||||
misn.setTitle(string.format(title[11], planet))
|
||||
misn.setTitle(string.format(title[11], planet:name()))
|
||||
else -- People delivery :P
|
||||
misn_type = "People"
|
||||
misn.faction = rnd.int(1)
|
||||
@ -70,7 +72,7 @@ function create()
|
||||
carg_type = "Pilgrims"
|
||||
end
|
||||
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
|
||||
|
||||
-- More mission specifics.
|
||||
@ -91,21 +93,21 @@ function create()
|
||||
end
|
||||
|
||||
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)) +
|
||||
carg_mass * (150+rnd.int(75)) +
|
||||
rnd.int(1500)
|
||||
elseif misn_type == "Rush" then
|
||||
misn_time = time.get() + time.units(2) +
|
||||
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,
|
||||
time.str(misn_time), time.str(misn_time-time.get())))
|
||||
reward = misn_dist * carg_mass * (450+rnd.int(250)) +
|
||||
carg_mass * (250+rnd.int(125)) +
|
||||
rnd.int(2500)
|
||||
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)
|
||||
end
|
||||
misn.setReward(string.format(misn_reward, reward))
|
||||
@ -139,7 +141,8 @@ end
|
||||
|
||||
-- Land hook.
|
||||
function land()
|
||||
if space.landName() == planet then
|
||||
local landed = space.getLanded()
|
||||
if landed == planet then
|
||||
if player.rmCargo(carg_id) then
|
||||
player.pay( reward )
|
||||
tk.msg(finish_title, string.format( finish_msg, carg_type))
|
||||
@ -168,7 +171,7 @@ function timeup()
|
||||
player.msg(misn_time_msg)
|
||||
misn.finish(false)
|
||||
end
|
||||
misn.setDesc(string.format( misn_desc[21], planet, system,
|
||||
misn.setDesc(string.format( misn_desc[21], planet:name(), system,
|
||||
carg_mass, carg_type,
|
||||
time.str(misn_time), time.str(misn_time-time.get())))
|
||||
end
|
||||
|
@ -20,17 +20,19 @@ You aren't too sure of what to make of your encounter with the Empire, only time
|
||||
end
|
||||
|
||||
function create()
|
||||
local landed = space.getLanded()
|
||||
|
||||
-- Target destination.
|
||||
local i = 0
|
||||
repeat
|
||||
dest = space.getPlanet(misn.factions())
|
||||
i = i+1
|
||||
until dest ~= space.landName() or i > 10
|
||||
until dest ~= landed or i > 10
|
||||
-- Infinate loop protection.
|
||||
if i > 10 then
|
||||
misn.finish(false)
|
||||
end
|
||||
system = space.getSystem(dest)
|
||||
system = space.getSystem(dest:name())
|
||||
misn.setMarker(system)
|
||||
|
||||
-- Intro text.
|
||||
@ -43,10 +45,10 @@ function create()
|
||||
reward = 3000
|
||||
misn.setTitle(misn_title)
|
||||
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.
|
||||
tk.msg(title[2], string.format( text[3], dest))
|
||||
tk.msg(title[2], string.format( text[3], dest:name()))
|
||||
|
||||
-- Set up the goal.
|
||||
parcels = player.addCargo("Parcels", 0)
|
||||
@ -55,11 +57,12 @@ function create()
|
||||
end
|
||||
|
||||
function land()
|
||||
if space.landName() == dest then
|
||||
local landed = space.getLanded()
|
||||
if landed == dest then
|
||||
if player.rmCargo(parcels) then
|
||||
player.pay(reward)
|
||||
-- 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)
|
||||
player.modFaction("Empire", 3)
|
||||
misn.finish(true)
|
||||
|
@ -31,7 +31,7 @@ function create()
|
||||
misn_stage = 0
|
||||
misn_nearby = "Coriolis"
|
||||
misn_target = "Dune"
|
||||
misn_base = "Omega Station"
|
||||
misn_base = space.getPlanet("Omega Station")
|
||||
misn_base_sys = "NGC-7291"
|
||||
misn.setMarker(misn_nearby) -- Not exact target.
|
||||
|
||||
@ -41,7 +41,7 @@ function create()
|
||||
misn.setDesc(string.format(misn_desc[1],misn_nearby))
|
||||
|
||||
-- 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.land("land")
|
||||
@ -64,14 +64,14 @@ function enter()
|
||||
|
||||
-- update mission.
|
||||
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.setMarker(misn_base_sys) -- Now we mark return to base.
|
||||
end
|
||||
end
|
||||
|
||||
function land()
|
||||
planet = space.landName()
|
||||
planet = space.getLanded()
|
||||
|
||||
if misn_stage == 1 and planet == misn_base then
|
||||
tk.msg(title[3], string.format(text[3],misn_target))
|
||||
|
@ -26,7 +26,8 @@ function create()
|
||||
|
||||
misn_stage = 0
|
||||
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.
|
||||
misn.setTitle(misn_title)
|
||||
@ -49,16 +50,16 @@ function enter()
|
||||
-- Visited enough systems.
|
||||
if misn_stage == 0 and systems_visited >= 2 then
|
||||
misn.setDesc(string.format(misn_desc[2],
|
||||
misn_base, space.getSystem(misn_base)))
|
||||
misn_base:name(), space.getSystem(misn_base)))
|
||||
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")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function land()
|
||||
planet = space.landName()
|
||||
planet = space.getLanded()
|
||||
|
||||
if misn_stage == 1 and planet == misn_base then
|
||||
tk.msg(title[3], text[3])
|
||||
|
@ -29,30 +29,30 @@ function create()
|
||||
|
||||
misn_stage = 0
|
||||
systems_visited = 0 -- Number of Collective systems visited.
|
||||
misn_base = "Omega Station"
|
||||
misn_target = "Eiroik"
|
||||
misn.setMarker(space.getSystem(misn_target))
|
||||
misn_base, misn_base_sys = space.getPlanet("Omega Station")
|
||||
misn_target, misn_target_sys = space.getPlanet("Eiroik")
|
||||
misn.setMarker(misn_target_sys)
|
||||
|
||||
-- Mission details.
|
||||
misn.setTitle(misn_title)
|
||||
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[3]. misn_target, space_getSystem(misn_target)))
|
||||
tk.msg(title[1], string.format(text[2], misn_target:name()))
|
||||
tk.msg(title[1], string.format(text[3]. misn_target:name(), misn_target_sys))
|
||||
|
||||
hook.enter("land")
|
||||
end
|
||||
end
|
||||
|
||||
function land()
|
||||
planet = space.landName()
|
||||
planet = space.getLanded()
|
||||
|
||||
-- First mission part is landing on the planet.
|
||||
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.setMarker(space.getSystem(misn_base))
|
||||
misn.setMarker(misn_base_sys)
|
||||
|
||||
-- Return bit.
|
||||
elseif misn_stage == 1 and planet == misn_base then
|
||||
|
@ -27,22 +27,22 @@ end
|
||||
function create()
|
||||
-- Target destination.
|
||||
local i = 0
|
||||
local landed = space.getLanded()
|
||||
repeat
|
||||
planet = space.getPlanet(misn.factions())
|
||||
planet, system = space.getPlanet(misn.factions())
|
||||
i = i + 1
|
||||
until planet ~= space.landName() or i > 10
|
||||
until planet ~= landed or i > 10
|
||||
-- Infinite loop protection.
|
||||
if i > 10 then
|
||||
misn.finish(false)
|
||||
end
|
||||
system = space.getSystem( planet )
|
||||
misn.setMarker(system) -- Set system marker.
|
||||
misn_dist = space.jumpDist(system)
|
||||
|
||||
-- Mission generics.
|
||||
misn_type = "Cargo"
|
||||
i = rnd.int(1)
|
||||
misn.setTitle(string.format(title[i+1], planet))
|
||||
misn.setTitle(string.format(title[i+1], planet:name()))
|
||||
|
||||
-- More mission specifics.
|
||||
carg_mass = rnd.int(10, 30)
|
||||
@ -61,7 +61,7 @@ function create()
|
||||
|
||||
misn_time = time.get() + time.units(5) +
|
||||
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())))
|
||||
reward = misn_dist * carg_mass * (500+rnd.int(250)) +
|
||||
carg_mass * (250+rnd.int(150)) +
|
||||
@ -87,7 +87,8 @@ end
|
||||
|
||||
-- Land hook.
|
||||
function land()
|
||||
if space.landName() == planet then
|
||||
local landed = space.getLanded()
|
||||
if landed == planet then
|
||||
if player.rmCargo(carg_id) then
|
||||
player.pay(reward)
|
||||
tk.msg(finish_title, string.format(finish_msg, carg_type))
|
||||
@ -118,7 +119,7 @@ function timeup()
|
||||
player.msg(miss_timeup)
|
||||
misn.finish(false)
|
||||
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())))
|
||||
end
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 423 KiB After Width: | Height: | Size: 423 KiB |
@ -14,8 +14,8 @@ function choose(str)
|
||||
music.play()
|
||||
|
||||
elseif str == "land" then
|
||||
planet = space.landName()
|
||||
class = space.planetClass(planet)
|
||||
planet = space.getLanded()
|
||||
class = planet:class()
|
||||
|
||||
if class == "M" then
|
||||
mus = "agriculture"
|
||||
@ -24,7 +24,7 @@ function choose(str)
|
||||
elseif class == "P" then
|
||||
mus = "snow"
|
||||
else
|
||||
if space.planetServices(planet) > 0 then
|
||||
if planet:services() > 0 then
|
||||
mus = "cosmostation"
|
||||
else
|
||||
mus = "agriculture"
|
||||
|
189
src/llua.c
189
src/llua.c
@ -23,27 +23,6 @@ static const luaL_reg lephisto_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 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. */
|
||||
static int time_get(lua_State* L);
|
||||
static int time_str(lua_State* L);
|
||||
@ -166,12 +145,6 @@ int lua_loadLephisto(lua_State* L) {
|
||||
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);
|
||||
@ -195,168 +168,6 @@ static int lephisto_lang(lua_State* L) {
|
||||
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. -- */
|
||||
static int time_get(lua_State* L) {
|
||||
lua_pushnumber(L, ltime_get());
|
||||
|
300
src/llua_space.c
Normal file
300
src/llua_space.c
Normal 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
6
src/llua_space.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "lua.h"
|
||||
|
||||
/* Load the space library. */
|
||||
int lua_loadSpace(lua_State* L, int readonly);
|
||||
|
@ -114,9 +114,10 @@ static int mission_init(Mission* mission, MissionData* misn, int load) {
|
||||
/* Load the file. */
|
||||
buf = pack_readfile(DATA, misn->lua, &bufsize);
|
||||
if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
|
||||
ERR("Error loading mission file: %s", misn->lua);
|
||||
ERR("%s", lua_tostring(mission->L, -1));
|
||||
WARN("Most likely Lua file has improper syntax, please check");
|
||||
ERR("Error loading mission file: %s\n"
|
||||
"%s\n"
|
||||
"Most likely Lua file has improper syntax, please check",
|
||||
misn->lua, lua_tostring(mission->L, -1));
|
||||
return -1;
|
||||
}
|
||||
free(buf);
|
||||
|
Loading…
Reference in New Issue
Block a user