[Add] Introduced concept of mission cargo being un-tradable.

This commit is contained in:
Allanis 2013-04-05 20:19:27 +01:00
parent 7f973d7fe8
commit 8172d0f21f
5 changed files with 64 additions and 16 deletions

View File

@ -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.", "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())) carg_mass-player.freeCargo()))
elseif misn.accept() then -- Able to accept the mission, hooks BREAK after accepting. 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", tk.msg("Mission Accepted",
string.format("The workers load the %d tons of %s onto your ship.", string.format("The workers load the %d tons of %s onto your ship.",
carg_mass, carg_type)) carg_mass, carg_type))
@ -44,7 +44,7 @@ end
function land() function land()
if space.landName() == planet then if space.landName() == planet then
player.rmCargo(carg_type, carg_mass) player.rmCargo(carg_id)
player.pay(misn_reward) player.pay(misn_reward)
tk.msg("Mission Accomplished", tk.msg("Mission Accomplished",
string.format("The workers unload the %s at the docks.", carg_type)) string.format("The workers unload the %s at the docks.", carg_type))

View File

@ -274,27 +274,23 @@ static int player_addCargo(lua_State* L) {
if(lua_isnumber(L, -1)) quantity = (int)lua_tonumber(L, -1); if(lua_isnumber(L, -1)) quantity = (int)lua_tonumber(L, -1);
else return 0; else return 0;
ret = pilot_addCargo(player, cargo, quantity); ret = pilot_addMissionCargo(player, cargo, quantity);
lua_pushnumber(L, ret); lua_pushnumber(L, ret);
return 1; return 1;
} }
static int player_rmCargo(lua_State* L) { static int player_rmCargo(lua_State* L) {
Commodity* cargo; unsigned int id;
int quantity, ret;
MIN_ARGS(2); MIN_ARGS(1);
if(lua_isstring(L, -2)) cargo = commodity_get((char*) lua_tostring(L, -2)); if(lua_isnumber(L, -1)) id = (unsigned int) lua_tonumber(L, -1);
else return 0;
if(lua_isnumber(L, -1)) quantity = (int) lua_tonumber(L, -1);
else return 0; else return 0;
ret = pilot_rmCargo(player, cargo, quantity); pilot_rmMissionCargo(player, id);
lua_pushnumber(L, ret); return 0;
return 1;
} }
static int player_pay(lua_State* L) { static int player_pay(lua_State* L) {

View File

@ -19,6 +19,9 @@
// Stack of pilot id's to assure uniqueness. // Stack of pilot id's to assure uniqueness.
static unsigned int pilot_id = PLAYER_ID; 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. // 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 Pilot** pilot_stack = NULL; // Not static, it is used in player.c and weapon.c and ai.c
int pilots = 0; int pilots = 0;
@ -614,7 +617,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
q = quantity; q = quantity;
for(i = 0; i < pilot->ncommodities; i++) 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) if(pilot->cargo_free < quantity)
q = pilot->cargo_free; q = pilot->cargo_free;
pilot->commodities[i].quantity += q; pilot->commodities[i].quantity += q;
@ -628,6 +631,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
pilot->commodities[pilot->ncommodities].commodity = cargo; pilot->commodities[pilot->ncommodities].commodity = cargo;
if(pilot->cargo_free < quantity) if(pilot->cargo_free < quantity)
q = pilot->cargo_free; q = pilot->cargo_free;
pilot->commodities[pilot->ncommodities].id = 0;
pilot->commodities[pilot->ncommodities].quantity = q; pilot->commodities[pilot->ncommodities].quantity = q;
pilot->cargo_free -= q; pilot->cargo_free -= q;
pilot->ncommodities++; pilot->ncommodities++;
@ -635,6 +639,45 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
return q; 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, // Try to get rid of quantity cargo from pilot,
// return quantity actually removed. // return quantity actually removed.
int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) { int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) {
@ -642,7 +685,7 @@ int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) {
q = quantity; q = quantity;
for(i = 0; i < pilot->ncommodities; i++) 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) { if(quantity >= pilot->commodities[i].quantity) {
q = pilot->commodities[i].quantity; q = pilot->commodities[i].quantity;

View File

@ -50,9 +50,11 @@ typedef struct PilotOutfit_ {
unsigned int timer; // Used to store last used weapon time. unsigned int timer; // Used to store last used weapon time.
} PilotOutfit; } PilotOutfit;
// Pilot commodity.
typedef struct PilotCommodity_ { typedef struct PilotCommodity_ {
Commodity* commodity; Commodity* commodity;
int quantity; int quantity;
unsigned int id; // Special mission id for cargo.
} PilotCommodity; } PilotCommodity;
// Primary pilot structure. // 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_setSecondary(Pilot* p, const char* secondary);
void pilot_setAmmo(Pilot* p); void pilot_setAmmo(Pilot* p);
double pilot_face(Pilot* p, const float dir); double pilot_face(Pilot* p, const float dir);
// Outfits.
int pilot_freeSpace(Pilot* p); // Pilot space. int pilot_freeSpace(Pilot* p); // Pilot space.
int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity); int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity);
int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity); int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity);
char* pilot_getOutfits(Pilot* pilot); char* pilot_getOutfits(Pilot* pilot);
// Normal cargo.
int pilot_freeCargo(Pilot* p); // Cargo space. int pilot_freeCargo(Pilot* p); // Cargo space.
int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity); int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity);
int pilot_rmCargo(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. // Creation.
void pilot_init(Pilot* dest, Ship* ship, char* name, int faction, void pilot_init(Pilot* dest, Ship* ship, char* name, int faction,

View File

@ -381,7 +381,8 @@ int player_cargoOwned(const char* commodityname) {
int i; int i;
for(i = 0; i < player->ncommodities; 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 player->commodities[i].quantity;
return 0; return 0;