[Add] Buy/Sell cargo.

This commit is contained in:
Allanis 2013-03-17 01:01:47 +00:00
parent 8a375a58e1
commit 91e68ee828
3 changed files with 53 additions and 8 deletions

View File

@ -46,6 +46,8 @@ static Planet* planet = NULL;
// Commodity excahnge. // Commodity excahnge.
static void commodity_exchange(void); static void commodity_exchange(void);
static void commodity_exchange_close(char* str); static void commodity_exchange_close(char* str);
static void commodity_buy(char* str);
static void commodity_sell(char* str);
// Outfits. // Outfits.
static void outfits(void); static void outfits(void);
static void outfits_close(char* str); static void outfits_close(char* str);
@ -78,12 +80,20 @@ static void commodity_exchange(void) {
BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodityClose", BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodityClose",
"Close", commodity_exchange_close); "Close", commodity_exchange_close);
window_addButton(secondary_wid, -40-((BUTTON_WIDTH-20)/2), 20*2+BUTTON_HEIGHT,
(BUTTON_WIDTH-20)/2, BUTTON_HEIGHT, "btnCommodityBuy",
"Buy", commodity_buy);
window_addButton(secondary_wid, -20, 20*2+BUTTON_HEIGHT,
(BUTTON_WIDTH-20)/2, BUTTON_HEIGHT, "btnCommoditySell",
"Sell", commodity_buy);
goods = malloc(sizeof(char*)*planet->ncommodities); goods = malloc(sizeof(char*)*planet->ncommodities);
for(i = 0; i < planet->ncommodities; i++) for(i = 0; i < planet->ncommodities; i++)
goods[i] = strdup(planet->commodities[i]->name); goods[i] = strdup(planet->commodities[i]->name);
window_addList(secondary_wid, 20, -40, window_addList(secondary_wid, 20, -40,
100, COMMODITY_HEIGHT-80-BUTTON_HEIGHT, COMMODITY_WIDTH-BUTTON_WIDTH-60, COMMODITY_HEIGHT-80-BUTTON_HEIGHT,
"lstGoods", goods, planet->ncommodities, 0, NULL); "lstGoods", goods, planet->ncommodities, 0, NULL);
} }
@ -92,6 +102,44 @@ static void commodity_exchange_close(char* str) {
window_destroy(secondary_wid); window_destroy(secondary_wid);
} }
static void commodity_buy(char* str) {
(void)str;
char* comname;
Commodity* com;
int q;
q = 10;
comname = toolkit_getList(secondary_wid, "lstGoods");
com = commodity_get(comname);
if(player_credits <= q * com->medium) {
toolkit_alert("Not enough Scred!");
return;
}
else if(player->cargo_free <= 0) {
toolkit_alert("not enough free space!");
return;
}
q = pilot_addCargo(player, com, q);
player_credits -= q * com->medium;
}
static void commodity_sell(char* str) {
(void)str;
char* comname;
Commodity* com;
int q;
q = 10;
comname = toolkit_getList(secondary_wid, "lstGoods");
com = commodity_get(comname);
q = pilot_rmCargo(player, com, q);
player_credits += q * com->medium;
}
static void outfits(void) { static void outfits(void) {
char** outfits; char** outfits;
int noutfits; int noutfits;

View File

@ -500,7 +500,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
// Must add another one. // Must add another one.
pilot->commodities = realloc(pilot->commodities, pilot->commodities = realloc(pilot->commodities,
sizeof(PilotCommodity) * pilot->ncommodities); sizeof(PilotCommodity) * (pilot->ncommodities+1));
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;
@ -519,7 +519,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].commodity == cargo) {
if(quantity > pilot->commodities[i].quantity) { if(quantity >= pilot->commodities[i].quantity) {
q = pilot->commodities[i].quantity; q = pilot->commodities[i].quantity;
// Remove cargo. // Remove cargo.
@ -535,10 +535,7 @@ int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) {
return q; return q;
} }
WARN("Trying to remove %d cargo '%s' from pilot '%s' when it doesn't exist", return 0;
quantity, cargo->name, pilot->name);
return -1;
} }
// ==Init pilot.=========================================== // ==Init pilot.===========================================

View File

@ -493,7 +493,7 @@ void player_render(void) {
// Cargo and co. // Cargo and co.
if(player->ncommodities > 0) { if(player->ncommodities > 0) {
j -= gl_smallFont.h - 5; j -= gl_smallFont.h + 5;
gl_print(&gl_smallFont, gl_print(&gl_smallFont,
gui.misc.x + 8, j, &cConsole, "Cargo"); gui.misc.x + 8, j, &cConsole, "Cargo");