[Add] Introduced concept of mission cargo being un-tradable.
This commit is contained in:
parent
7f973d7fe8
commit
8172d0f21f
@ -32,7 +32,7 @@ function accept()
|
||||
"Your ship is too full, You need to make room for %d more tons if you want to be able to accept the mission.",
|
||||
carg_mass-player.freeCargo()))
|
||||
elseif misn.accept() then -- Able to accept the mission, hooks BREAK after accepting.
|
||||
player.addCargo(carg_type, carg_mass)
|
||||
carg_id = player.addCargo(carg_type, carg_mass)
|
||||
tk.msg("Mission Accepted",
|
||||
string.format("The workers load the %d tons of %s onto your ship.",
|
||||
carg_mass, carg_type))
|
||||
@ -44,7 +44,7 @@ end
|
||||
|
||||
function land()
|
||||
if space.landName() == planet then
|
||||
player.rmCargo(carg_type, carg_mass)
|
||||
player.rmCargo(carg_id)
|
||||
player.pay(misn_reward)
|
||||
tk.msg("Mission Accomplished",
|
||||
string.format("The workers unload the %s at the docks.", carg_type))
|
||||
|
@ -274,27 +274,23 @@ static int player_addCargo(lua_State* L) {
|
||||
if(lua_isnumber(L, -1)) quantity = (int)lua_tonumber(L, -1);
|
||||
else return 0;
|
||||
|
||||
ret = pilot_addCargo(player, cargo, quantity);
|
||||
ret = pilot_addMissionCargo(player, cargo, quantity);
|
||||
|
||||
lua_pushnumber(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int player_rmCargo(lua_State* L) {
|
||||
Commodity* cargo;
|
||||
int quantity, ret;
|
||||
unsigned int id;
|
||||
|
||||
MIN_ARGS(1);
|
||||
|
||||
MIN_ARGS(2);
|
||||
|
||||
if(lua_isstring(L, -2)) cargo = commodity_get((char*) lua_tostring(L, -2));
|
||||
else return 0;
|
||||
if(lua_isnumber(L, -1)) quantity = (int) lua_tonumber(L, -1);
|
||||
if(lua_isnumber(L, -1)) id = (unsigned int) lua_tonumber(L, -1);
|
||||
else return 0;
|
||||
|
||||
ret = pilot_rmCargo(player, cargo, quantity);
|
||||
pilot_rmMissionCargo(player, id);
|
||||
|
||||
lua_pushnumber(L, ret);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int player_pay(lua_State* L) {
|
||||
|
47
src/pilot.c
47
src/pilot.c
@ -19,6 +19,9 @@
|
||||
// Stack of pilot id's to assure uniqueness.
|
||||
static unsigned int pilot_id = PLAYER_ID;
|
||||
|
||||
// id for special mission cargo.
|
||||
static unsigned int mission_cargo_id = 0;
|
||||
|
||||
// Stack of pilots - yes, they come in stacks now.
|
||||
Pilot** pilot_stack = NULL; // Not static, it is used in player.c and weapon.c and ai.c
|
||||
int pilots = 0;
|
||||
@ -614,7 +617,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
|
||||
q = quantity;
|
||||
for(i = 0; i < pilot->ncommodities; i++)
|
||||
if(pilot->commodities[i].commodity == cargo) {
|
||||
if(!pilot->commodities[i].id && (pilot->commodities[i].commodity == cargo)) {
|
||||
if(pilot->cargo_free < quantity)
|
||||
q = pilot->cargo_free;
|
||||
pilot->commodities[i].quantity += q;
|
||||
@ -628,6 +631,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
pilot->commodities[pilot->ncommodities].commodity = cargo;
|
||||
if(pilot->cargo_free < quantity)
|
||||
q = pilot->cargo_free;
|
||||
pilot->commodities[pilot->ncommodities].id = 0;
|
||||
pilot->commodities[pilot->ncommodities].quantity = q;
|
||||
pilot->cargo_free -= q;
|
||||
pilot->ncommodities++;
|
||||
@ -635,6 +639,45 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
return q;
|
||||
}
|
||||
|
||||
unsigned int pilot_addMissionCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
int q;
|
||||
q = quantity;
|
||||
|
||||
pilot->commodities = realloc(pilot->commodities,
|
||||
sizeof(PilotCommodity) * (pilot->ncommodities+1));
|
||||
pilot->commodities[pilot->ncommodities].commodity = cargo;
|
||||
if(pilot->cargo_free < quantity)
|
||||
q = pilot->cargo_free;
|
||||
pilot->commodities[pilot->ncommodities].id = ++mission_cargo_id;
|
||||
pilot->commodities[pilot->ncommodities].quantity = q;
|
||||
pilot->cargo_free -= q;
|
||||
pilot->ncommodities++;
|
||||
|
||||
return pilot->commodities[pilot->ncommodities-1].id;
|
||||
}
|
||||
|
||||
void pilot_rmMissionCargo(Pilot* pilot, unsigned int cargo_id) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < pilot->ncommodities; i++)
|
||||
if(pilot->commodities[i].id == cargo_id)
|
||||
break;
|
||||
|
||||
if(i >= pilot->ncommodities) {
|
||||
DEBUG("Mission Cargo id '%d' not found on pilot '%s'", cargo_id, pilot->name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove cargo.
|
||||
pilot->cargo_free += pilot->commodities[i].quantity;
|
||||
memmove(pilot->commodities+i, pilot->commodities+i+1,
|
||||
sizeof(PilotCommodity) * (pilot->ncommodities-i-1));
|
||||
|
||||
pilot->ncommodities--;
|
||||
pilot->commodities = realloc(pilot->commodities,
|
||||
sizeof(PilotCommodity) * pilot->ncommodities);
|
||||
}
|
||||
|
||||
// Try to get rid of quantity cargo from pilot,
|
||||
// return quantity actually removed.
|
||||
int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
@ -642,7 +685,7 @@ int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) {
|
||||
|
||||
q = quantity;
|
||||
for(i = 0; i < pilot->ncommodities; i++)
|
||||
if(pilot->commodities[i].commodity == cargo) {
|
||||
if(!pilot->commodities[i].id && (pilot->commodities[i].commodity == cargo)) {
|
||||
if(quantity >= pilot->commodities[i].quantity) {
|
||||
q = pilot->commodities[i].quantity;
|
||||
|
||||
|
@ -50,9 +50,11 @@ typedef struct PilotOutfit_ {
|
||||
unsigned int timer; // Used to store last used weapon time.
|
||||
} PilotOutfit;
|
||||
|
||||
// Pilot commodity.
|
||||
typedef struct PilotCommodity_ {
|
||||
Commodity* commodity;
|
||||
int quantity;
|
||||
unsigned int id; // Special mission id for cargo.
|
||||
} PilotCommodity;
|
||||
|
||||
// Primary pilot structure.
|
||||
@ -135,13 +137,19 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
|
||||
void pilot_setSecondary(Pilot* p, const char* secondary);
|
||||
void pilot_setAmmo(Pilot* p);
|
||||
double pilot_face(Pilot* p, const float dir);
|
||||
// Outfits.
|
||||
int pilot_freeSpace(Pilot* p); // Pilot space.
|
||||
int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity);
|
||||
int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity);
|
||||
char* pilot_getOutfits(Pilot* pilot);
|
||||
// Normal cargo.
|
||||
int pilot_freeCargo(Pilot* p); // Cargo space.
|
||||
int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity);
|
||||
int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity);
|
||||
// Mission cargo - Not to be confused with normal cargo.
|
||||
unsigned int pilot_addMissionCargo(Pilot* pilot, Commodity* cargo, int quantity);
|
||||
void pilot_rmMissionCargo(Pilot* pilot, unsigned int cargo_id);
|
||||
|
||||
|
||||
// Creation.
|
||||
void pilot_init(Pilot* dest, Ship* ship, char* name, int faction,
|
||||
|
@ -381,7 +381,8 @@ int player_cargoOwned(const char* commodityname) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < player->ncommodities; i++)
|
||||
if(strcmp(commodityname, player->commodities[i].commodity->name)==0)
|
||||
if(!player->commodities[i].id &&
|
||||
strcmp(commodityname, player->commodities[i].commodity->name)==0)
|
||||
return player->commodities[i].quantity;
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user