[Add] Track cargo missions so they get cleaned on abort.
This commit is contained in:
parent
8df3171916
commit
e152d40fb2
@ -300,6 +300,7 @@ static int player_addCargo(lua_State* L) {
|
||||
else return 0;
|
||||
|
||||
ret = pilot_addMissionCargo(player, cargo, quantity);
|
||||
mission_linkCargo(cur_mission, ret);
|
||||
|
||||
lua_pushnumber(L, ret);
|
||||
return 1;
|
||||
@ -315,6 +316,7 @@ static int player_rmCargo(lua_State* L) {
|
||||
else return 0;
|
||||
|
||||
ret = pilot_rmMissionCargo(player, id);
|
||||
mission_unlinkCargo(cur_mission, id);
|
||||
|
||||
lua_pushboolean(L, !ret);
|
||||
return 0;
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "pack.h"
|
||||
#include "xml.h"
|
||||
#include "faction.h"
|
||||
#include "player.h"
|
||||
#include "mission.h"
|
||||
|
||||
#define XML_MISSION_ID "Missions" // XML section identifier.
|
||||
@ -54,6 +55,8 @@ static int mission_init(Mission* mission, MissionData* misn) {
|
||||
mission->title = NULL;
|
||||
mission->desc = NULL;
|
||||
mission->reward = NULL;
|
||||
mission->cargo = NULL;
|
||||
mission->ncargo = 0;
|
||||
|
||||
// Init lua.
|
||||
mission->L = luaL_newstate();
|
||||
@ -111,12 +114,48 @@ void missions_bar(int faction, char* planet, char* system) {
|
||||
}
|
||||
}
|
||||
|
||||
// Links cargo to the mission for posterior cleanup.
|
||||
void mission_linkCargo(Mission* misn, unsigned int cargo_id) {
|
||||
misn->ncargo++;
|
||||
misn->cargo = realloc(misn->cargo, sizeof(unsigned int) * misn->ncargo);
|
||||
misn->cargo[misn->ncargo-1] = cargo_id;
|
||||
}
|
||||
|
||||
// Unlink cargo from the mission, removes it from the player.
|
||||
void mission_unlinkCargo(Mission* misn, unsigned int cargo_id) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < misn->ncargo; i++)
|
||||
if(misn->cargo[i] == cargo_id)
|
||||
break;
|
||||
|
||||
if(i >= misn->cargo) {
|
||||
DEBUG("Mission '%s' attempting to unlink in existant cargo %d.",
|
||||
misn->title, cargo_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// Shrink cargo size - No need to realloc.
|
||||
memmove(&misn->cargo[i], &misn->cargo[i+1],
|
||||
sizeof(unsigned int) * (misn->ncargo-i-1));
|
||||
|
||||
misn->ncargo--;
|
||||
player_rmMissionCargo(cargo_id);
|
||||
}
|
||||
|
||||
// Clean up a mission.
|
||||
void mission_cleanup(Mission* misn) {
|
||||
int i;
|
||||
if(misn->id) hook_rmParent(misn->id); // Remove existing hooks.
|
||||
if(misn->title) free(misn->title);
|
||||
if(misn->desc) free(misn->desc);
|
||||
if(misn->reward) free(misn->reward);
|
||||
if(misn->cargo) {
|
||||
for(i = 0; i < misn->ncargo; i++)
|
||||
mission_unlinkCargo(misn, misn->cargo[i]);
|
||||
free(misn->cargo);
|
||||
misn->ncargo = 0;
|
||||
}
|
||||
if(misn->L) lua_close(misn->L);
|
||||
memset(misn, 0, sizeof(Mission));
|
||||
}
|
||||
|
@ -49,7 +49,11 @@ typedef struct Mission_ {
|
||||
char* desc; // Description of the mission.
|
||||
char* reward; // Rewards - in text.
|
||||
|
||||
lua_State* L;
|
||||
// Mission cargo give to the player - Need to cleanup.
|
||||
unsigned int* cargo;
|
||||
int ncargo;
|
||||
|
||||
lua_State* L; // The state of the running lua code.
|
||||
} Mission;
|
||||
|
||||
#define MISSION_MAX 6 // No sense in the player having unlimited missions..
|
||||
@ -61,6 +65,10 @@ Mission* missions_computer(int* n, int faction, char* planet, char* system);
|
||||
void mission_accept(Mission* mission);
|
||||
void mission_bar(int faction, char* planet, char* system);
|
||||
|
||||
// Cargo stuff.
|
||||
void mission_linkCargo(Mission* misn, unsigned int cargo_id);
|
||||
void mission_unlinkCargo(Mission* misn, unsigned int cargo_id);
|
||||
|
||||
// Load/Quit.
|
||||
int missions_load(void);
|
||||
void mission_cleanup(Mission* misn);
|
||||
|
10
src/player.c
10
src/player.c
@ -388,6 +388,16 @@ int player_cargoOwned(const char* commodityname) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void player_rmMissionCargo(unsigned int cargo_id) {
|
||||
int i;
|
||||
|
||||
if(!pilot_rmMissionCargo(player, cargo_id)) return; // Already done.
|
||||
|
||||
for(i = 0; i < player_nstack; i++)
|
||||
if(!pilot_rmMissionCargo(player_stack[i], cargo_id))
|
||||
return; // Success.
|
||||
}
|
||||
|
||||
// Render the background player stuff, namely planet target
|
||||
void player_renderBG(void) {
|
||||
double x, y;
|
||||
|
@ -45,8 +45,10 @@ void player_message(const char* fmt, ...);
|
||||
void player_clear(void);
|
||||
void player_warp(const double x, const double y);
|
||||
const char* player_rating(void);
|
||||
// Cargo.
|
||||
int player_outfitOwned(const char* outfitname);
|
||||
int player_cargoOwned(const char* commodityname);
|
||||
void player_rmMissionCargo(unsigned int cargo_id);
|
||||
|
||||
// Pilot ships.
|
||||
char** player_ships(int* nships);
|
||||
|
Loading…
Reference in New Issue
Block a user