27#include "nlua_audio.h"
29#include "nlua_camera.h"
30#include "nlua_commodity.h"
31#include "nlua_faction.h"
33#include "nlua_music.h"
35#include "nlua_player.h"
36#include "nlua_system.h"
153 if (lua_isnil( naevL, -1 )) {
184 nlua_getenv( L, __NLUA_CURENV,
"__misn" );
185 misnptr = lua_touserdata( L, -1 );
186 misn = misnptr ? *misnptr : NULL;
198 misnptr = lua_newuserdata( naevL,
sizeof(
Mission*) );
200 nlua_setenv( naevL, misn->
env,
"__misn" );
203 nlua_getenv( naevL, misn->
env, func );
217 int ret, misn_delete;
226 ret = nlua_pcall(env, nargs, 0);
229 nlua_getenv(naevL, env,
"__misn");
230 cur_mission = *(
Mission**) lua_touserdata(naevL, -1);
234 const char* err = (lua_isstring(naevL,-1)) ? lua_tostring(naevL,-1) : NULL;
235 if ((err==NULL) || (strcmp(err,NLUA_DONE)!=0)) {
236 WARN(_(
"Mission '%s' -> '%s': %s"),
237 cur_mission->
data->
name, func, (err) ? err : _(
"unknown error"));
246 nlua_getenv(naevL, env,
"__misn_delete");
247 misn_delete = lua_toboolean(naevL,-1);
263 else if (!isaccepted && cur_mission->
accepted)
277 const char *str = luaL_checkstring(L,1);
279 free(cur_mission->
title);
280 cur_mission->
title = strdup(str);
294 const char *str = luaL_checkstring(L,1);
296 free(cur_mission->
desc);
297 cur_mission->
desc = strdup(str);
309 free(cur_mission->
reward);
311 if (lua_isnumber(L,1)) {
312 char buf[ECON_CRED_STRLEN];
313 cur_mission->
reward_value =
CLAMP( CREDITS_MIN, CREDITS_MAX, (credits_t)round(luaL_checknumber(L,1)) );
315 cur_mission->
reward = strdup(buf);
318 const char *str = luaL_checkstring(L,1);
319 cur_mission->
reward = strdup(str);
342 int id, issys, objid;
344 MissionMarkerType type;
356 stype = luaL_optstring( L, 2,
"high" );
359 if (strcmp(stype,
"computer")==0)
360 type = SPOBMARKER_COMPUTER;
361 else if (strcmp(stype,
"low")==0)
362 type = SPOBMARKER_LOW;
363 else if (strcmp(stype,
"high")==0)
364 type = SPOBMARKER_HIGH;
365 else if (strcmp(stype,
"plot")==0)
366 type = SPOBMARKER_PLOT;
368 return NLUA_ERROR(L, _(
"Unknown marker type: %s"), stype);
372 type = mission_markerTypeSpobToSystem( type );
383 lua_pushnumber( L,
id );
398 int id, objid, issys;
403 id = luaL_checkinteger( L, 1 );
419 marker = &cur_mission->
markers[i];
424 return NLUA_ERROR( L, _(
"Mission does not have a marker with id '%d'"),
id );
428 marker->
type = mission_markerTypeSpobToSystem( marker->
type );
430 marker->
type = mission_markerTypeSystemToSpob( marker->
type );
431 marker->
objid = objid;
453 if (lua_isnoneornil(L,1)) {
460 id = luaL_checkinteger( L, 1 );
466 marker = &cur_mission->
markers[i];
470 if (marker == NULL) {
502 const char *name, *desc;
510 free(cur_mission->
npc);
511 cur_mission->
npc = NULL;
517 if (lua_gettop(L) == 0)
521 name = luaL_checkstring(L,1);
523 desc = luaL_checkstring(L,3);
526 cur_mission->
npc = strdup(name);
527 cur_mission->
npc_desc = strdup(desc);
549 lua_rawseti(L,-2,i+1);
564 Mission *new_misn, *cur_mission;
581 new_misn = calloc( 1,
sizeof(
Mission) );
588 return NLUA_ERROR(L, _(
"Mission already accepted!"));
591 *new_misn = *cur_mission;
592 memset( cur_mission, 0,
sizeof(
Mission) );
593 cur_mission->
env = LUA_NOREF;
595 cur_mission = new_misn;
599 misnptr = lua_newuserdata( L,
sizeof(
Mission*) );
600 *misnptr = cur_mission;
601 nlua_setenv( L, cur_mission->
env,
"__misn" );
604 lua_pushboolean(L,!ret);
619 int b = lua_toboolean(L,1);
622 lua_pushboolean( L, 1 );
623 nlua_setenv( L, cur_mission->
env,
"__misn_delete" );
628 lua_pushstring(L, NLUA_DONE);
653 quantity = luaL_checkint(L,2);
661 lua_pushnumber(L, ret);
677 id = luaL_checklong(L,1);
681 lua_pushboolean(L,0);
690 lua_pushboolean(L,!ret);
706 id = luaL_checklong(L,1);
710 lua_pushboolean(L,0);
719 lua_pushboolean(L,!ret);
744 WARN(_(
"Can't create an OSD on an unaccepted mission!"));
749 title = luaL_checkstring(L,1);
750 luaL_checktype(L,2,LUA_TTABLE);
751 nitems = lua_objlen(L,2);
754 if (cur_mission->
osd != 0) {
755 osd_destroy( cur_mission->
osd );
756 cur_mission->
osd = 0;
760 items = calloc( nitems,
sizeof(
char *) );
763 for (
int i=0; i<nitems; i++) {
764 lua_pushnumber(L,i+1);
766 if (!lua_isstring(L,-1)) {
768 luaL_typerror(L, -1,
"string");
771 items[i] = strdup( lua_tostring(L, -1) );
776 cur_mission->
osd = osd_create( title, nitems, (
const char**) items,
781 for (
int i=0; i<nitems; i++)
797 if (cur_mission->
osd != 0) {
798 osd_destroy( cur_mission->
osd );
799 cur_mission->
osd = 0;
818 n = luaL_checkint(L,1);
823 if (cur_mission->
osd != 0)
824 osd_active( cur_mission->
osd, n );
838 char **items = osd_getItems(cur_mission->
osd);
839 int active = osd_getActive(cur_mission->
osd);
841 if (!items || active < 0) {
846 lua_pushstring(L, items[active]);
863 if (cur_mission->
osd == 0)
866 lua_pushstring( L, osd_getTitle(cur_mission->
osd) );
868 items = osd_getItems(cur_mission->
osd);
870 lua_pushstring( L, items[i] );
871 lua_rawseti( L, -2, i+1 );
873 lua_pushinteger( L, osd_getActive(cur_mission->
osd) );
895 const char *func, *name, *desc;
900 func = luaL_checkstring(L, 1);
901 name = luaL_checkstring(L, 2);
903 desc = luaL_checkstring(L, 4);
906 priority = luaL_optinteger(L,5,5);
907 if (!lua_isnoneornil(L,6))
915 id =
npc_add_mission( cur_mission->
id, func, name, priority, portrait, desc, bg );
922 lua_pushnumber( L,
id );
938 unsigned int id = luaL_checklong(L, 1);
946 return NLUA_ERROR(L, _(
"Invalid NPC ID!"));
977 inclusive = lua_toboolean(L,2);
981 return NLUA_ERROR(L, _(
"Mission trying to claim but already has."));
986 if (lua_istable(L,1)) {
989 while (lua_next(L, 1) != 0) {
992 else if (lua_isstring(L,-1))
999 else if (lua_isstring(L, 1))
1002 NLUA_INVALID_PARAMETER(L,1);
1007 lua_pushboolean(L,0);
1012 cur_mission->
claims = claim;
1014 lua_pushboolean(L,1);
1018void misn_pushMissionData( lua_State *L,
const MissionData *md )
1024 lua_pushstring( L, md->
name );
1025 lua_setfield(L,-2,
"name");
1027 lua_pushstring( L, mission_availabilityStr(ma->
loc) );
1028 lua_setfield(L,-2,
"loc");
1030 lua_pushinteger( L, ma->
chance );
1031 lua_setfield(L,-2,
"chance");
1033 if (ma->
spob != NULL) {
1034 lua_pushstring( L, ma->
spob );
1035 lua_setfield(L,-2,
"spob");
1038 if (ma->
system != NULL) {
1039 lua_pushstring( L, ma->
system );
1040 lua_setfield(L,-2,
"system");
1044 lua_pushstring( L, ma->
chapter );
1045 lua_setfield(L,-2,
"chapter");
1050 if (ma->
cond != NULL) {
1051 lua_pushstring( L, ma->
cond );
1052 lua_setfield(L,-2,
"cond");
1055 if (ma->
done != NULL) {
1056 lua_pushstring( L, ma->
done );
1057 lua_setfield(L,-2,
"done");
1060 lua_pushinteger( L, ma->
priority );
1061 lua_setfield(L,-2,
"priority");
1063 if (mis_isFlag(md,MISSION_UNIQUE)) {
1064 lua_pushboolean( L, 1 );
1065 lua_setfield(L,-2,
"unique");
1070 lua_pushstring( L, md->
tags[t] );
1071 lua_pushboolean( L, 1 );
1072 lua_rawset( L, -3 );
1074 lua_setfield(L,-2,
"tags");
Provides macros to work with dynamic arrays.
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
int claim_test(const Claim_t *claim)
Tests to see if a system claim would have collisions.
void claim_destroy(Claim_t *claim)
Destroys a system claim.
int claim_addStr(Claim_t *claim, const char *str)
Adds a string claim to a claim.
int claim_addSys(Claim_t *claim, int ss_id)
Adds a claim to a system claim.
void claim_activate(Claim_t *claim)
Activates a claim on a system.
int claim_isNull(const Claim_t *claim)
See if a claim actually contains data.
Claim_t * claim_create(int exclusive)
Creates a system claim.
void credits2str(char *str, credits_t credits, int decimals)
Converts credits to a usable string for displaying.
void bar_regen(void)
Regenerates the bar list.
Mission ** player_missions
void mission_cleanup(Mission *misn)
Cleans up a mission.
int mission_addMarker(Mission *misn, int id, int objid, MissionMarkerType type)
Adds a system marker to a mission.
int mission_linkCargo(Mission *misn, unsigned int cargo_id)
Links cargo to the mission for posterior cleanup.
void mission_sysMark(void)
Marks all active systems that need marking.
void mission_shift(int pos)
Puts the specified mission at the end of the player_missions array.
int mission_getID(const char *name)
Gets id from mission name.
int mission_unlinkCargo(Mission *misn, unsigned int cargo_id)
Unlinks cargo from the mission, removes it from the player.
Header file with generic functions and naev-specifics.
int nlua_loadStandard(nlua_env env)
Loads the standard Naev Lua API.
int nlua_loadBackground(nlua_env env)
Loads the graphics library.
int nlua_loadCamera(nlua_env env)
Loads the camera library.
Commodity * luaL_validcommodity(lua_State *L, int ind)
Makes sure the commodity is valid or raises a Lua error.
LuaFaction * lua_pushfaction(lua_State *L, LuaFaction faction)
Pushes a faction on the stack.
int nlua_loadHook(nlua_env env)
Loads the hook Lua library.
static int misn_factions(lua_State *L)
Gets the factions the mission is available for.
Mission * misn_getFromLua(lua_State *L)
Gets the mission that's being currently run in Lua.
static const luaL_Reg misn_methods[]
static int misn_npcRm(lua_State *L)
Removes an NPC.
static int misn_osdGet(lua_State *L)
Gets the current mission OSD information.
static int misn_osdActive(lua_State *L)
Sets active in mission OSD.
int misn_tryRun(Mission *misn, const char *func)
Tries to run a mission, but doesn't err if it fails.
static int misn_osdDestroy(lua_State *L)
Destroys the mission OSD.
void misn_runStart(Mission *misn, const char *func)
Sets up the mission to run misn_runFunc.
static int misn_finish(lua_State *L)
Finishes the mission.
static int misn_markerMove(lua_State *L)
Moves a marker to a new system.
static int misn_osdCreate(lua_State *L)
Creates a mission OSD.
static int misn_cargoRm(lua_State *L)
Removes the mission cargo.
static int misn_setTitle(lua_State *L)
Mission Lua bindings.
static int misn_claim(lua_State *L)
Tries to claim systems or strings.
int misn_runFunc(const Mission *misn, const char *func, int nargs)
Runs a mission set up with misn_runStart.
static int misn_markerAdd(lua_State *L)
Adds a new marker.
static int misn_osdGetActiveItem(lua_State *L)
Gets the active OSD element.
static int misn_setReward(lua_State *L)
Sets the current mission reward description.
static int misn_accept(lua_State *L)
Attempts to accept the mission.
static int misn_setDesc(lua_State *L)
Sets the current mission description.
static int misn_cargoJet(lua_State *L)
Jettisons the mission cargo.
static int misn_markerRm(lua_State *L)
Removes a mission system marker.
static int misn_npcAdd(lua_State *L)
Adds an NPC.
int nlua_loadMisn(nlua_env env)
Loads the mission Lua library.
static int misn_cargoAdd(lua_State *L)
Adds some mission cargo to the player. They cannot sell it nor get rid of it unless they abandons the...
int misn_loadLibs(nlua_env env)
Registers all the mission libraries.
static int misn_setNPC(lua_State *L)
Sets the current mission NPC.
int misn_run(Mission *misn, const char *func)
Runs a mission function.
int nlua_loadMusic(nlua_env env)
Music Lua module.
LuaSpob luaL_checkspob(lua_State *L, int ind)
Gets spob at index raising an error if isn't a spob.
int lua_isspob(lua_State *L, int ind)
Checks to see if ind is a spob.
LuaSystem luaL_checksystem(lua_State *L, int ind)
Gets system at index raising an error if type doesn't match.
StarSystem * luaL_validsystem(lua_State *L, int ind)
Gets system (or system name) at index raising an error if type doesn't match.
LuaSystem lua_tosystem(lua_State *L, int ind)
Lua system module.
int lua_issystem(lua_State *L, int ind)
Checks to see if ind is a system.
glTexture * luaL_validtex(lua_State *L, int ind, const char *searchpath)
Gets texture directly or from a filename (string) at index or raises error if there is no texture at ...
int nlua_loadTex(nlua_env env)
Loads the texture library.
int nlua_loadTk(nlua_env env)
Loads the Toolkit Lua library.
unsigned int npc_add_mission(unsigned int mid, const char *func, const char *name, int priority, glTexture *portrait, const char *desc, glTexture *background)
Adds a mission NPC to the mission computer.
int npc_rm_mission(unsigned int id, unsigned int mid)
removes a mission NPC.
void gl_freeTexture(glTexture *texture)
Frees a texture.
unsigned int pilot_addMissionCargo(Pilot *pilot, const Commodity *cargo, int quantity)
Adds special mission cargo, can't sell it and such.
int pilot_rmMissionCargo(Pilot *pilot, unsigned int cargo_id, int jettison)
Removes special mission cargo based on id.
void player_missionFinished(int id)
Marks a mission as completed.
int system_index(const StarSystem *sys)
Gets the index of a star system.
Defines the availability of a mission.
Represents an active mission.
Abstraction for rendering sprite sheets.