[Change] Stage two of the lua API conversion, needs some heavy testing.

This commit is contained in:
Allanis 2013-08-25 19:34:27 +01:00
parent 2c5d3433b5
commit 7ebeadc73c
10 changed files with 162 additions and 99 deletions

View File

@ -46,7 +46,7 @@
<unique>1</unique> <unique>1</unique>
</flags> </flags>
<avail> <avail>
<cond>var.peek("es_cargo") == true and space.jumpDist("NGC-7291") &lt; 5 and player.getFaction("Empire") &gt; 5 and player.getRating() &gt; 10</cond> <cond>var.peek("es_cargo") == true and space.getSystem("NGC-7291"):jumpDist() &lt; 5 and player.getFaction("Empire") &gt; 5 and player.getRating() &gt; 10</cond>
<chance>60</chance> <chance>60</chance>
<location>Bar</location> <location>Bar</location>
<faction>Empire</faction> <faction>Empire</faction>

View File

@ -32,21 +32,20 @@ end
-- Create the mission. -- Create the mission.
function create() function create()
local landed = space.getLanded() local landed = space.getPlanet() -- Get landed planet.
-- Target destination. -- Target destination.
local i = 0 local i = 0
repeat repeat
planet = space.getPlanet(misn.factions()) planet, system = space.getPlanet(misn.factions())
i = i + 1 i = i + 1
until planet ~= landed 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:name())
misn.setMarker(system) -- Mark the system. misn.setMarker(system) -- Mark the system.
misn_dist = space.jumpDist(system) misn_dist = system:jumpDist()
-- Mission generics. -- Mission generics.
i = rnd.int(6) i = rnd.int(6)
@ -93,21 +92,21 @@ function create()
end end
if misn_type == "Cargo" then if misn_type == "Cargo" then
misn.setDesc(string.format( misn_desc[1], planet:name(), system, carg_mass, carg_type)) misn.setDesc(string.format( misn_desc[1], planet:name(), system:name(), 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:name(), system, misn.setDesc(string.format( misn_desc[11], planet:name(), system:name(),
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:name(), system)) misn.setDesc(string.format(misn_desc[21], carg_type, planet:name(), system:name()))
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))
@ -141,7 +140,7 @@ end
-- Land hook. -- Land hook.
function land() function land()
local landed = space.getLanded() local landed = space.getPlanet()
if landed == planet then if landed == planet then
if player.rmCargo(carg_id) then if player.rmCargo(carg_id) then
player.pay( reward ) player.pay( reward )
@ -171,7 +170,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:name(), system, misn.setDesc(string.format( misn_desc[21], planet:name(), system:name(),
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,19 +20,18 @@ 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() local landed = space.getPlanet()
-- Target destination. -- Target destination.
local i = 0 local i = 0
repeat repeat
dest = space.getPlanet(misn.factions()) dest,system = space.getPlanet(misn.factions())
i = i+1 i = i+1
until dest ~= landed 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:name())
misn.setMarker(system) misn.setMarker(system)
-- Intro text. -- Intro text.
@ -45,7 +44,7 @@ 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:name(), system)) misn.setDesc(string.format(misn_desc, dest:name(), system:name()))
-- Flavour text and mini-briefing. -- Flavour text and mini-briefing.
tk.msg(title[2], string.format( text[3], dest:name())) tk.msg(title[2], string.format( text[3], dest:name()))
@ -57,7 +56,7 @@ function create()
end end
function land() function land()
local landed = space.getLanded() local landed = space.getPlanet()
if landed == dest then if landed == dest then
if player.rmCargo(parcels) then if player.rmCargo(parcels) then
player.pay(reward) player.pay(reward)

View File

@ -29,19 +29,20 @@ function create()
misn.accept() misn.accept()
misn_stage = 0 misn_stage = 0
misn_nearby = "Coriolis" misn_nearby = space.getSystem("Coriolis")
misn_target = "Dune" misn_target = space.getSystem("Dune")
misn_base = space.getPlanet("Omega Station") misn_base,misn_base_sys = 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.
-- 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_nearby)) misn.setDesc(string.format(misn_desc[1],misn_nearby:name()))
-- Flavour text and mini-briefing. -- Flavour text and mini-briefing.
tk.msg(title[2], string.format(text[2], misn_nearby, misn_base:name(), misn_base_sys)) tk.msg(title[2], string.format(text[2], misn_nearby:name(),
misn_base:name(), misn_base_sys:name()))
hook.enter("enter") hook.enter("enter")
hook.land("land") hook.land("land")
@ -49,10 +50,10 @@ function create()
end end
function enter() function enter()
sys = space.system() sys = space.getSystem()
-- Additional fleets. -- Additional fleets.
if sys == "NGC-7291" then -- Increase action for realism. if sys:name() == "NGC-7291" then -- Increase action for realism.
pilot.add("Empire Sml Defense") pilot.add("Empire Sml Defense")
pilot.add("Collective Sml Swarm") pilot.add("Collective Sml Swarm")
elseif sys == misn_target then elseif sys == misn_target then
@ -64,17 +65,17 @@ 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:name() ,misn_base_sys)) misn.setDesc(string.format(misn_desc[2], misn_base:name() ,misn_base_sys:name()))
misn_stage = 1 misn_stage = 1
misn.setMarker(misn_base_sys) -- Now we mark return to base. misn.setMarker(misn_base_sys:name()) -- Now we mark return to base.
end end
end end
function land() function land()
planet = space.getLanded() planet = space.getPlanet()
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:name()))
player.modFaction("Empire", 5) player.modFaction("Empire", 5)
misn.finish(true) misn.finish(true)
end end

View File

@ -26,8 +26,7 @@ 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 = space.getPlanet("Omega Station") misn_base,misn_base_sys = space.getPlanet("Omega Station")
misn_base_sys = space.getSystem(misn_base:name())
-- Mission details. -- Mission details.
misn.setTitle(misn_title) misn.setTitle(misn_title)
@ -41,7 +40,8 @@ function create()
end end
function enter() function enter()
factions = space.faction() local sys = space.getSystem()
local factions = sys:faction()
-- Increment system visited count. -- Increment system visited count.
if misn_stage == 0 and factions["Collective"] then if misn_stage == 0 and factions["Collective"] then
@ -50,7 +50,7 @@ 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:name(), space.getSystem(misn_base))) misn_base:name(), misn_base_sys:name()))
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.
hook.land("land") hook.land("land")
@ -59,7 +59,7 @@ function enter()
end end
function land() function land()
planet = space.getLanded() local planet = space.getPlanet()
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

@ -36,17 +36,17 @@ function create()
-- 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:name(), misn_target_sys)) misn.setDesc(string.format(misn_desc[1], misn_target:name(), misn_target_sys:name()))
tk.msg(title[1], string.format(text[2], misn_target:name())) 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)) tk.msg(title[1], string.format(text[3]. misn_target:name(), misn_target_sys:name()))
hook.enter("land") hook.enter("land")
end end
end end
function land() function land()
planet = space.getLanded() planet = space.getPlanet()
-- 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

View File

@ -27,7 +27,7 @@ end
function create() function create()
-- Target destination. -- Target destination.
local i = 0 local i = 0
local landed = space.getLanded() local landed = space.getPlanet()
repeat repeat
planet, system = space.getPlanet(misn.factions()) planet, system = space.getPlanet(misn.factions())
i = i + 1 i = i + 1
@ -37,7 +37,7 @@ function create()
misn.finish(false) misn.finish(false)
end end
misn.setMarker(system) -- Set system marker. misn.setMarker(system) -- Set system marker.
misn_dist = space.jumpDist(system) misn_dist = system:jumpDist()
-- Mission generics. -- Mission generics.
misn_type = "Cargo" misn_type = "Cargo"
@ -61,7 +61,8 @@ 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:name(),, system, misn.setDesc(string.format( misn_desc, carg_mass, carg_type,
planet:name(),, system:name(),
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 +88,7 @@ end
-- Land hook. -- Land hook.
function land() function land()
local landed = space.getLanded() local landed = space.getPlanet()
if landed == planet then if landed == planet then
if player.rmCargo(carg_id) then if player.rmCargo(carg_id) then
player.pay(reward) player.pay(reward)
@ -119,7 +120,8 @@ 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:name(), system, misn.setDesc(string.format( misn_desc, carg_mass, carg_type,
planet:name(), system:name(),
time.str(misn_time), time.str(misn_time-time.get()))) time.str(misn_time), time.str(misn_time-time.get())))
end end

View File

@ -14,7 +14,7 @@ function choose(str)
music.play() music.play()
elseif str == "land" then elseif str == "land" then
planet = space.getLanded() planet = space.getPlanet()
class = planet:class() class = planet:class()
if class == "M" then if class == "M" then

View File

@ -9,9 +9,6 @@
#include "map.h" #include "map.h"
#include "llua_space.h" #include "llua_space.h"
#define PLANET_METATABLE "Planet"
#define SYSTEM_METATABLE "System"
/* Lua wrappers. */ /* Lua wrappers. */
typedef struct LuaPlanet_ { typedef struct LuaPlanet_ {
Planet* p; Planet* p;
@ -22,24 +19,20 @@ typedef struct LuaSystem_ {
} LuaSystem; } LuaSystem;
static int planetL_createmetatable(lua_State* L); static int planetL_createmetatable(lua_State* L);
static int systemL_createmetatable(lua_State* L);
/* Space. */ /* Space. */
static int planetL_get(lua_State* L); static int planetL_get(lua_State* L);
static int space_getSystem(lua_State* L); static int systemL_get(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[] = { static const luaL_reg space_methods[] = {
{ "getPlanet", planetL_get }, { "getPlanet", planetL_get },
{ "getSystem", space_getSystem }, { "getSystem", systemL_get },
{ "getLanded", spaceL_getLanded },
{ "system", space_systemName },
{ "jumpDist", space_jumpDist },
{ "faction", space_faction },
{ 0, 0 } { 0, 0 }
}; };
/* Internal planet methods. */
static LuaPlanet* lua_toplanet(lua_State* L, int ind);
static LuaPlanet* lua_pushplanet(lua_State* L, LuaPlanet planet);
/* Planet metatable methods. */ /* Planet metatable methods. */
static int planetL_eq(lua_State* L); static int planetL_eq(lua_State* L);
static int planetL_name(lua_State* L); static int planetL_name(lua_State* L);
@ -56,6 +49,23 @@ static const luaL_reg planet_methods[] = {
{ 0, 0 } { 0, 0 }
}; };
/* Internal system methods. */
static LuaSystem* lua_tosystem(lua_State* L, int ind);
static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys);
/* System metatable methods. */
static int systemL_eq(lua_State* L);
static int systemL_name(lua_State* L);
static int systemL_faction(lua_State* L);
static int systemL_jumpdistance(lua_State* L);
static const luaL_reg system_methods[] = {
{ "__eq", systemL_eq },
{ "__tostring", systemL_name },
{ "name", systemL_name },
{ "faction", systemL_faction },
{ "jumpDist", systemL_jumpdistance },
{ 0, 0 }
};
/* Load the space library. */ /* Load the space library. */
int lua_loadSpace(lua_State* L, int readonly) { int lua_loadSpace(lua_State* L, int readonly) {
(void)readonly; (void)readonly;
@ -65,6 +75,7 @@ int lua_loadSpace(lua_State* L, int readonly) {
/* Register the metatables. */ /* Register the metatables. */
planetL_createmetatable(L); planetL_createmetatable(L);
systemL_createmetatable(L);
return 0; return 0;
} }
@ -86,6 +97,21 @@ static int planetL_createmetatable(lua_State* L) {
return 0; /* No error. */ return 0; /* No error. */
} }
/* Register the system metatable. */
static int systemL_createmetatable(lua_State* L) {
/* Create the metatable. */
luaL_newmetatable(L, SYSTEM_METATABLE);
/* Create the access table. */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
/* Register the values. */
luaL_register(L, NULL, system_methods);
return 0; /* No error. */
}
/* -- PLANET -- */ /* -- PLANET -- */
/* Get planet at index. */ /* Get planet at index. */
@ -117,13 +143,21 @@ static int planetL_get(lua_State* L) {
int nplanets; int nplanets;
char* rndplanet; char* rndplanet;
LuaPlanet planet; LuaPlanet planet;
LuaSystem sys;
rndplanet = NULL; rndplanet = NULL;
nplanets = 0; nplanets = 0;
/* Get a random planet. */ /* Get the landed planet. */
if(lua_gettop(L) == 0) { if(lua_gettop(L) == 0) {
rndplanet = space_getRndPlanet(); if(land_planet != NULL) {
planet.p = land_planet;
lua_pushplanet(L, planet);
sys.s = system_get(planet_getSystem(land_planet->name));
lua_pushsystem(L, sys);
return 2;
}
return 0; /* Not landed. */
} }
/* Get a planet by faction */ /* Get a planet by faction */
@ -172,7 +206,8 @@ static int planetL_get(lua_State* L) {
/* Push the planet. */ /* Push the planet. */
planet.p = planet_get(rndplanet); /* The real planet. */ planet.p = planet_get(rndplanet); /* The real planet. */
lua_pushplanet(L, planet); lua_pushplanet(L, planet);
lua_pushstring(L, planet_getSystem(rndplanet)); sys.s = system_get(planet_getSystem(rndplanet));
lua_pushsystem(L, sys);
return 2; return 2;
} }
@ -200,7 +235,7 @@ static int planetL_name(lua_State* L) {
static int planetL_faction(lua_State* L) { static int planetL_faction(lua_State* L) {
LuaPlanet* p; LuaPlanet* p;
p = lua_toplanet(L, 1); p = lua_toplanet(L, 1);
lua_pushnumber(L, p->p->faction); lua_pushstring(L, faction_name(p->p->faction));
return 1; return 1;
} }
@ -215,6 +250,7 @@ static int planetL_class(lua_State* L) {
return 1; return 1;
} }
/* Get planet services. */
static int planetL_services(lua_State* L) { static int planetL_services(lua_State* L) {
LuaPlanet* p; LuaPlanet* p;
p = lua_toplanet(L, 1); p = lua_toplanet(L, 1);
@ -222,44 +258,92 @@ static int planetL_services(lua_State* L) {
return 1; return 1;
} }
/* -- SYSTEM -- */
/* Get system at index. */
static LuaSystem* lua_tosystem(lua_State* L, int ind) {
if(lua_isuserdata(L, ind)) {
return (LuaSystem*) lua_touserdata(L,ind);
}
luaL_typerror(L, ind, SYSTEM_METATABLE);
return NULL;
}
/* Pushes a system on the stack. */
static LuaSystem* lua_pushsystem(lua_State* L, LuaSystem sys) {
LuaSystem* s;
s = (LuaSystem*) lua_newuserdata(L, sizeof(LuaSystem));
*s = sys;
luaL_getmetatable(L, SYSTEM_METATABLE);
lua_setmetatable(L, -2);
return s;
}
/* Get a system. */ /* Get a system. */
static int space_getSystem(lua_State* L) { static int systemL_get(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
LuaSystem sys;
char* planetname, *sysname; char* planetname, *sysname;
if(lua_isstring(L, 1)) planetname = (char*)lua_tostring(L, 1); if(lua_isstring(L, 1)) planetname = (char*)lua_tostring(L, 1);
else if(lua_isuserdata(L, 1)) {}
else LLUA_INVALID_PARAMETER(); else LLUA_INVALID_PARAMETER();
/* Return the system. */
sysname = planet_getSystem(planetname); sysname = planet_getSystem(planetname);
lua_pushstring(L, sysname); sys.s = system_get(sysname);
lua_pushsystem(L, sys);
return 1; return 1;
} }
static int spaceL_getLanded(lua_State* L) { /* Check system for equality. */
LuaPlanet planet; static int systemL_eq(lua_State* L) {
LuaSystem* a, *b;
a = lua_tosystem(L, 1);
b = lua_tosystem(L, 2);
if(a->s == b->s)
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
if(landed) {
planet.p = land_planet;
lua_pushplanet(L, planet);
return 1; return 1;
}
/* Return the systems name. */
static int systemL_name(lua_State* L) {
LuaSystem* sys;
sys = lua_tosystem(L, 1);
lua_pushstring(L, sys->s->name);
return 1;
}
/* Get system factions. */
static int systemL_faction(lua_State* L) {
int i;
LuaSystem* sys;
sys = lua_tosystem(L, 1);
/* Return result in table. */
lua_newtable(L);
for(i = 0; i < sys->s->nplanets; i++) {
lua_pushboolean(L, 1); /* Value. */
lua_setfield(L, -2, faction_name(sys->s->planets[i].faction)); /* Key. */
/* Allows syntax foo = space.faction("foo"); if foo["bar"] then ... end */
} }
return 0;
}
static int space_systemName(lua_State* L) {
lua_pushstring(L, cur_system->name);
return 1; return 1;
} }
static int space_jumpDist(lua_State* L) { /* Get jump distance for current system, or to another. */
static int systemL_jumpdistance(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
LuaSystem* sys;
StarSystem** s; StarSystem** s;
int jumps; int jumps;
char* start, *goal; char* start, *goal;
if(lua_isstring(L, 1)) sys = lua_tosystem(L, 1);
start = (char*)lua_tostring(L, 1); start = sys->s->name;
else LLUA_INVALID_PARAMETER();
if((lua_gettop(L) > 1) && lua_isstring(L, 2)) if((lua_gettop(L) > 1) && lua_isstring(L, 2))
goal = (char*)lua_tostring(L, 2); goal = (char*)lua_tostring(L, 2);
@ -273,28 +357,3 @@ static int space_jumpDist(lua_State* L) {
return 1; 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;
}

View File

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