27#include "nlua_camera.h"
28#include "nlua_commodity.h"
29#include "nlua_faction.h"
31#include "nlua_music.h"
33#include "nlua_system.h"
39#include "player_fleet.h"
155 if ( lua_isnil(
naevL, -1 ) ) {
187 misnptr = lua_touserdata( L, -1 );
188 misn = misnptr ? *misnptr : NULL;
202 nlua_setenv(
naevL, misn->
env,
"__misn" );
205 nlua_getenv(
naevL, misn->
env, func );
219 int ret, misn_delete;
228 ret = nlua_pcall( env, nargs, 0 );
231 nlua_getenv(
naevL, env,
"__misn" );
237 ( lua_isstring(
naevL, -1 ) ) ? lua_tostring(
naevL, -1 ) : NULL;
238 if ( ( err == NULL ) || ( strcmp( err, NLUA_DONE ) != 0 ) ) {
239 WARN( _(
"Mission '%s' -> '%s': %s" ), cur_mission->
data->
name, func,
240 ( err ) ? err : _(
"unknown error" ) );
248 nlua_getenv(
naevL, env,
"__misn_delete" );
249 misn_delete = lua_toboolean(
naevL, -1 );
265 else if ( !isaccepted && cur_mission->
accepted )
279 const char *str = luaL_checkstring( L, 1 );
281 free( cur_mission->
title );
282 cur_mission->
title = strdup( str );
296 const char *str = luaL_checkstring( L, 1 );
298 free( cur_mission->
desc );
299 cur_mission->
desc = strdup( str );
312 free( cur_mission->
reward );
314 if ( lua_isnumber( L, 1 ) ) {
315 char buf[ECON_CRED_STRLEN];
317 CLAMP( CREDITS_MIN, CREDITS_MAX,
318 (credits_t)round( luaL_checknumber( L, 1 ) ) );
320 cur_mission->
reward = strdup( buf );
322 const char *str = luaL_checkstring( L, 1 );
323 cur_mission->
reward = strdup( str );
339 cur_mission->
distance = luaL_checklong( L, 1 );
355 cur_mission->
illegal = lua_toboolean( L, 1 );
369 if ( lua_isnoneornil( L, 1 ) )
394 int id, issys, objid;
396 MissionMarkerType type;
407 stype = luaL_optstring( L, 2,
"high" );
410 if ( strcmp( stype,
"computer" ) == 0 )
411 type = SPOBMARKER_COMPUTER;
412 else if ( strcmp( stype,
"low" ) == 0 )
413 type = SPOBMARKER_LOW;
414 else if ( strcmp( stype,
"high" ) == 0 )
415 type = SPOBMARKER_HIGH;
416 else if ( strcmp( stype,
"plot" ) == 0 )
417 type = SPOBMARKER_PLOT;
419 return NLUA_ERROR( L, _(
"Unknown marker type: %s" ), stype );
423 type = mission_markerTypeSpobToSystem( type );
434 lua_pushnumber( L,
id );
449 int id, objid, issys;
454 id = luaL_checkinteger( L, 1 );
468 if (
id == cur_mission->
markers[i].
id ) {
469 marker = &cur_mission->
markers[i];
473 if ( marker == NULL )
474 return NLUA_ERROR( L, _(
"Mission does not have a marker with id '%d'" ),
479 marker->
type = mission_markerTypeSpobToSystem( marker->
type );
481 marker->
type = mission_markerTypeSystemToSpob( marker->
type );
482 marker->
objid = objid;
505 if ( lua_isnoneornil( L, 1 ) ) {
513 id = luaL_checkinteger( L, 1 );
518 if (
id == cur_mission->
markers[i].
id ) {
519 marker = &cur_mission->
markers[i];
523 if ( marker == NULL ) {
556 const char *name, *desc;
564 free( cur_mission->
npc );
565 cur_mission->
npc = NULL;
571 if ( lua_gettop( L ) == 0 )
575 name = luaL_checkstring( L, 1 );
577 desc = luaL_checkstring( L, 3 );
580 cur_mission->
npc = strdup( name );
581 cur_mission->
npc_desc = strdup( desc );
604 lua_rawseti( L, -2, i + 1 );
620 Mission *new_misn, *cur_mission;
638 new_misn = calloc( 1,
sizeof(
Mission ) );
645 return NLUA_ERROR( L, _(
"Mission already accepted!" ) );
648 *new_misn = *cur_mission;
649 memset( cur_mission, 0,
sizeof(
Mission ) );
650 cur_mission->
env = LUA_NOREF;
652 cur_mission = new_misn;
656 misnptr = lua_newuserdata( L,
sizeof(
Mission *) );
657 *misnptr = cur_mission;
658 nlua_setenv( L, cur_mission->
env,
"__misn" );
661 lua_pushboolean( L, !ret );
675 int b = lua_toboolean( L, 1 );
678 lua_pushboolean( L, 1 );
679 nlua_setenv( L, cur_mission->
env,
"__misn_delete" );
684 lua_pushstring( L, NLUA_DONE );
709 quantity = luaL_checkint( L, 2 );
717 lua_pushnumber( L, ret );
733 id = luaL_checklong( L, 1 );
738 lua_pushboolean( L, 0 );
747 lua_pushboolean( L, !ret );
763 id = luaL_checklong( L, 1 );
767 lua_pushboolean( L, 0 );
776 lua_pushboolean( L, !ret );
801 NLUA_WARN( L, _(
"Can't create an OSD on an unaccepted mission!" ) );
806 title = luaL_checkstring( L, 1 );
807 luaL_checktype( L, 2, LUA_TTABLE );
808 nitems = lua_objlen( L, 2 );
811 if ( cur_mission->
osd != 0 ) {
812 osd_destroy( cur_mission->
osd );
813 cur_mission->
osd = 0;
817 items = calloc( nitems,
sizeof(
char * ) );
820 for (
int i = 0; i < nitems; i++ ) {
821 lua_pushnumber( L, i + 1 );
822 lua_gettable( L, 2 );
823 if ( !lua_isstring( L, -1 ) ) {
825 luaL_typerror( L, -1,
"string" );
828 items[i] = strdup( lua_tostring( L, -1 ) );
833 cur_mission->
osd = osd_create( title, nitems, (
const char **)items,
834 misn_osdGetPriority( cur_mission ),
835 misn_osdGetHide( cur_mission ) );
839 for (
int i = 0; i < nitems; i++ )
855 if ( cur_mission->
osd != 0 ) {
856 osd_destroy( cur_mission->
osd );
857 cur_mission->
osd = 0;
876 n = luaL_checkint( L, 1 );
881 if ( cur_mission->
osd != 0 )
882 osd_active( cur_mission->
osd, n );
896 if ( cur_mission->
osd == 0 )
899 int active = osd_getActive( cur_mission->
osd );
903 lua_pushinteger( L, active + 1 );
920 if ( cur_mission->
osd == 0 )
923 lua_pushstring( L, osd_getTitle( cur_mission->
osd ) );
925 items = osd_getItems( cur_mission->
osd );
926 for (
int i = 0; i <
array_size( items ); i++ ) {
927 lua_pushstring( L, items[i] );
928 lua_rawseti( L, -2, i + 1 );
930 lua_pushinteger( L, osd_getActive( cur_mission->
osd ) );
957 const char *func, *name, *desc;
962 func = luaL_checkstring( L, 1 );
963 name = luaL_checkstring( L, 2 );
965 desc = luaL_checkstring( L, 4 );
968 priority = luaL_optinteger( L, 5, 5 );
969 if ( !lua_isnoneornil( L, 6 ) )
985 lua_pushnumber( L,
id );
1001 unsigned int id = luaL_checklong( L, 1 );
1009 return NLUA_ERROR( L, _(
"Invalid NPC ID!" ) );
1047 inclusive = lua_toboolean( L, 2 );
1051 return NLUA_ERROR( L, _(
"Mission trying to claim but already has." ) );
1056 if ( lua_istable( L, 1 ) ) {
1059 while ( lua_next( L, 1 ) != 0 ) {
1062 else if ( lua_isstring( L, -1 ) )
1068 else if ( lua_isstring( L, 1 ) )
1071 NLUA_INVALID_PARAMETER( L, 1 );
1076 lua_pushboolean( L, 0 );
1081 cur_mission->
claims = claim;
1083 lua_pushboolean( L, 1 );
1087void misn_pushMissionData( lua_State *L,
const MissionData *md )
1093 lua_pushstring( L, md->
name );
1094 lua_setfield( L, -2,
"name" );
1096 lua_pushstring( L, mission_availabilityStr( ma->
loc ) );
1097 lua_setfield( L, -2,
"loc" );
1099 lua_pushinteger( L, ma->
chance );
1100 lua_setfield( L, -2,
"chance" );
1102 if ( ma->
spob != NULL ) {
1103 lua_pushstring( L, ma->
spob );
1104 lua_setfield( L, -2,
"spob" );
1107 if ( ma->
system != NULL ) {
1108 lua_pushstring( L, ma->
system );
1109 lua_setfield( L, -2,
"system" );
1113 lua_pushstring( L, ma->
chapter );
1114 lua_setfield( L, -2,
"chapter" );
1119 if ( ma->
cond != NULL ) {
1120 lua_pushstring( L, ma->
cond );
1121 lua_setfield( L, -2,
"cond" );
1124 if ( ma->
done != NULL ) {
1125 lua_pushstring( L, ma->
done );
1126 lua_setfield( L, -2,
"done" );
1129 lua_pushinteger( L, ma->
priority );
1130 lua_setfield( L, -2,
"priority" );
1132 if ( mis_isFlag( md, MISSION_UNIQUE ) ) {
1133 lua_pushboolean( L, 1 );
1134 lua_setfield( L, -2,
"unique" );
1139 lua_pushstring( L, md->
tags[t] );
1140 lua_pushboolean( L, 1 );
1141 lua_rawset( L, -3 );
1143 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.
void misn_regen(void)
Regenerates the mission list.
static void misn_accept(unsigned int wid, const char *str)
Accepts the selected mission.
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.
LuaFaction luaL_validfaction(lua_State *L, int ind)
Gets faction (or faction name) at index, raising an error if type isn't a valid faction.
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_computerRefresh(lua_State *L)
Refreshes the mission computer offerings.
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_setDistance(lua_State *L)
Sets the distance to the mission's goal. Used for sorting missions in the mission computer....
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_setFaction(lua_State *L)
Sets the faction giver of the mission. Used mainly as metadata for sorting and organizing missions in...
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_setIllegal(lua_State *L)
Marks whether or not a mission should be considered illegal in the current system....
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.
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.
unsigned int pfleet_cargoMissionAdd(const Commodity *com, int q)
Adds some mission cargo to the player's fleet.
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.