diff --git a/src/land.c b/src/land.c index b846876..32c9b87 100644 --- a/src/land.c +++ b/src/land.c @@ -46,6 +46,8 @@ static Planet* planet = NULL; // Commodity excahnge. static void commodity_exchange(void); static void commodity_exchange_close(char* str); +static void commodity_buy(char* str); +static void commodity_sell(char* str); // Outfits. static void outfits(void); static void outfits_close(char* str); @@ -78,12 +80,20 @@ static void commodity_exchange(void) { BUTTON_WIDTH, BUTTON_HEIGHT, "btnCommodityClose", "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); for(i = 0; i < planet->ncommodities; i++) goods[i] = strdup(planet->commodities[i]->name); 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); } @@ -92,6 +102,44 @@ static void commodity_exchange_close(char* str) { 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) { char** outfits; int noutfits; diff --git a/src/pilot.c b/src/pilot.c index efc649f..4f00293 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -500,7 +500,7 @@ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) { // Must add another one. pilot->commodities = realloc(pilot->commodities, - sizeof(PilotCommodity) * pilot->ncommodities); + sizeof(PilotCommodity) * (pilot->ncommodities+1)); pilot->commodities[pilot->ncommodities].commodity = cargo; if(pilot->cargo_free < quantity) q = pilot->cargo_free; @@ -519,7 +519,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(quantity > pilot->commodities[i].quantity) { + if(quantity >= pilot->commodities[i].quantity) { q = pilot->commodities[i].quantity; // Remove cargo. @@ -535,10 +535,7 @@ int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity) { return q; } - WARN("Trying to remove %d cargo '%s' from pilot '%s' when it doesn't exist", - quantity, cargo->name, pilot->name); - - return -1; + return 0; } // ==Init pilot.=========================================== diff --git a/src/player.c b/src/player.c index 1e18471..344fd8c 100644 --- a/src/player.c +++ b/src/player.c @@ -493,7 +493,7 @@ void player_render(void) { // Cargo and co. if(player->ncommodities > 0) { - j -= gl_smallFont.h - 5; + j -= gl_smallFont.h + 5; gl_print(&gl_smallFont, gui.misc.x + 8, j, &cConsole, "Cargo");