From a33bb0624a985f1ec3cd5f8530dc6b05d1859ac7 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 8 Mar 2013 00:10:45 +0000 Subject: [PATCH] [Add] Selling of outfits, also improved purchasing of outfits. --- src/board.c | 2 +- src/economy.c | 12 ++++++----- src/land.c | 44 ++++++++++++++++++++++++++++++++------ src/pilot.c | 59 ++++++++++++++++++++++++++++++++++++++++++++------- src/pilot.h | 2 ++ src/player.c | 52 +++++++++++++++++++++++++++++++-------------- src/player.h | 6 ++++-- 7 files changed, 139 insertions(+), 38 deletions(-) diff --git a/src/board.c b/src/board.c index 3a57456..2e72f95 100644 --- a/src/board.c +++ b/src/board.c @@ -102,7 +102,7 @@ static void board_stealCreds(char* str) { return; } - credits += board_credits; + player_credits += board_credits; board_credits = 0; board_update(); // Update the lack of credits. player_message("You manage to steal the ship's Scred"); diff --git a/src/economy.c b/src/economy.c index c8a576c..ee3f7d0 100644 --- a/src/economy.c +++ b/src/economy.c @@ -5,12 +5,14 @@ // Convert credits to a usable string for displaying. // str must have 10 characters allocated. void credits2str(char* str, unsigned int credits, int decimals) { - if(credits >= 1000000000) - snprintf(str, 10, "%.*fB", decimals, (double)credits / 1000000000.); + if(decimals < 0) + snprintf(str, 32, "%d", credits); + else if(credits >= 1000000000) + snprintf(str, 16, "%.*fB", decimals, (double)credits / 1000000000.); else if(credits >= 1000000) - snprintf(str, 10, "%*fM", decimals, (double)credits / 1000000.); + snprintf(str, 16, "%*fM", decimals, (double)credits / 1000000.); else if(credits >= 1000) - snprintf(str, 10, "%.*fK", decimals, (double)credits / 1000.); - else snprintf(str, 10, "%d", credits); + snprintf(str, 16, "%.*fK", decimals, (double)credits / 1000.); + else snprintf(str, 16, "%d", credits); } diff --git a/src/land.c b/src/land.c index 1fa2603..50a2ce2 100644 --- a/src/land.c +++ b/src/land.c @@ -41,6 +41,9 @@ static int land_wid = 0; // Primary land window. static int secondary_wid = 0; // For the second opened land window (We can only have 2 max). static Planet* planet = NULL; +// Extern. +extern unsigned int player_credits; + // Commodity excahnge. static void commodity_exchange(void); static void commodity_exchange_close(char* str); @@ -49,6 +52,7 @@ static void outfits(void); static void outfits_close(char* str); static void outfits_update(char* str); static void outfits_buy(char* str); +static void outfits_sell(char* str); // Shipyard. static void shipyard(void); static void shipyard_close(char* str); @@ -104,15 +108,18 @@ static void outfits(void) { "Buy", outfits_buy); window_addButton(secondary_wid, -40-BUTTON_WIDTH, 40+BUTTON_HEIGHT, - BUTTON_WIDTH, BUTTON_HEIGHT, "btnInfoOutfit", - "Info", NULL); + BUTTON_WIDTH, BUTTON_HEIGHT, "btnSellOutfit", + "Sell", outfits_sell); window_addText(secondary_wid, 40+200+40, -80, 80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole, "Name:\n" "Type:\n" + "Owned:\n" + "Space taken:\n" "\n" - "Price:\n"); + "Price:\n" + "Money:\n"); window_addText(secondary_wid, 40+200+40+80, -80, 250, 96, 0, "txtDDesc", &gl_smallFont, &cBlack, NULL); @@ -140,21 +147,31 @@ static void outfits_update(char* str) { (void)str; char* outfitname; Outfit* outfit; - char buf[80], buf2[32]; + char buf[80], buf2[32], buf3[32]; outfitname = toolkit_getList(secondary_wid, "lstOutfits"); outfit = outfit_get(outfitname); window_modifyText(secondary_wid, "txtDescription", outfit->description); - credits2str(buf2, outfit->price, 0); + credits2str(buf2, outfit->price, -1); + credits2str(buf3, player_credits, 2); snprintf(buf, 80, "%s\n" "%s\n" + "%d\n" "\n" + "%d\n" + "%d\n" + "\n" + "%s Scred\n" "%s SCred\n", outfit->name, outfit_getType(outfit), - buf2); + player_outfitOwned(outfitname), + outfit->mass, + player_freeSpace(), + buf2, + buf3); window_modifyText(secondary_wid, "txtDDesc", buf); } @@ -171,6 +188,21 @@ static void outfits_buy(char* str) { q = 1; // Q should be dependant on MOD keys. TODO pilot_addOutfit(player, outfit, q); + outfits_update(NULL); +} + +static void outfits_sell(char* str) { + (void)str; + char* outfitname; + Outfit* outfit; + int q; + + outfitname = toolkit_getList(secondary_wid, "lstOutfits"); + outfit = outfit_get(outfitname); + q = 1; + + pilot_rmOutfit(player, outfit, q); + outfits_update(NULL); } static void shipyard(void) { diff --git a/src/pilot.c b/src/pilot.c index 6c30383..7de142b 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -25,7 +25,7 @@ int pilots = 0; static int mpilots = 0; extern Pilot* player; -extern unsigned int combat_rating; +extern unsigned int combat_crating; // Stack of fleets. static Fleet* fleet_stack = NULL; @@ -218,7 +218,7 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, if(!pilot_isFlag(p, PILOT_DEAD)) { pilot_dead(p); // Adjust the combat rating based on pilot mass. - if(shooter == PLAYER_ID) combat_rating += MAX(1, p->ship->mass/50); + if(shooter == PLAYER_ID) combat_crating += MAX(1, p->ship->mass/50); } } @@ -237,6 +237,28 @@ void pilot_dead(Pilot* p) { pilot_setFlag(p, PILOT_DEAD); } +void pilot_setSecondary(Pilot* p, const char* secondary) { + int i; + + if(secondary == NULL) { + p->secondary = NULL; + p->ammo = NULL; + return; + } + + for(i = 0; i < p->noutfits; i++) { + if(strcmp(secondary, p->outfits[i].outfit->name)==0) { + p->secondary = &p->outfits[i];; + pilot_setAmmo(p); + return; + } + } + WARN("Attempted to set pilot '%s' secondary weapon to non-existing '%s'", + p->name, secondary); + p->secondary = NULL; + p->ammo = NULL; +} + // Set the pilot's ammo based on their secondary weapon. void pilot_setAmmo(Pilot* p) { int i; @@ -402,13 +424,34 @@ void pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) { pilot_setFlag(pilot, PILOT_HASTURRET); // Hack due to realloc possibility. - if(s) { - for(i = 0; i < pilot->noutfits; i++) - if(strcmp(s, pilot->outfits[i].outfit->name)==0) { - pilot->secondary = &pilot->outfits[i]; - break; + pilot_setSecondary(pilot, s); +} + +// Remove an outfit from the pilot. +void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) { + int i; + char* s; + + for(i = 0; i < pilot->noutfits; i++) + if(strcmp(outfit->name, pilot->outfits[i].outfit->name)==0) { + pilot->outfits[i].quantity -= quantity; + if(pilot->outfits[i].quantity <= 0) { + // Hack in case it reallocs - Can happen even when shrinking. + s = (pilot->secondary) ? pilot->secondary->outfit->name : NULL; + + // Remove the outfit. + memmove(pilot->outfits+i, pilot->outfits+i+1, + sizeof(PilotOutfit)*(pilot->noutfits-i)); + pilot->noutfits--; + pilot->outfits = realloc(pilot->outfits, + sizeof(PilotOutfit)*(pilot->noutfits)); + + pilot_setSecondary(pilot, s); } - } + return; + } + WARN("Failure attempting to remove %d '%s' from pilot '%s'", + quantity, outfit->name, pilot->name); } // ==Init pilot.=========================================== diff --git a/src/pilot.h b/src/pilot.h index 6edaa20..3be9a29 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -115,9 +115,11 @@ Fleet* fleet_get(const char* name); void pilot_shoot(Pilot* p, const unsigned int target, const int secondary); void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, const double damage_shield, const double damage_armour); +void pilot_setSecondary(Pilot* p, const char* secondary); void pilot_setAmmo(Pilot* p); double pilot_face(Pilot* p, const float dir); void pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity); +void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity); // Creation. void pilot_init(Pilot* dest, Ship* ship, char* name, Faction* faction, AI_Profile* ai, diff --git a/src/player.c b/src/player.c index 397b4a6..2e9ba38 100644 --- a/src/player.c +++ b/src/player.c @@ -27,10 +27,10 @@ // Player stuff. Pilot* player = NULL; // extern in pilot.h // Player global properties. -char* player_name = NULL; // Player name. -unsigned int credits = 0; // Ze monies. -unsigned int combat_rating = 0; // Ze rating. -unsigned int player_flags = 0; // Player flags. +char* player_name = NULL; // Player name. +unsigned int player_credits = 0; // Ze monies. +unsigned int combat_crating = 0; // Ze rating. +unsigned int player_flags = 0; // Player flags. // Input.c double player_turn = 0.; // Turn velocity from input. double player_acc = 0.; // Accel velocity from input. @@ -155,8 +155,8 @@ void player_new(void) { else if(xml_isNode(tmp, "y")) y = xml_getFloat(tmp); } while((tmp = tmp->next)); } - else if(xml_isNode(cur, "combat_rating")) - combat_rating = xml_getInt(cur); + else if(xml_isNode(cur, "combat_crating")) + combat_crating = xml_getInt(cur); } while((cur = cur->next)); } }while((node = node->next)); @@ -166,7 +166,7 @@ void player_new(void) { xmlCleanupParser(); // Money. - credits = RNG(l, h); + player_credits = RNG(l, h); player_newShip(ship); space_init(system); @@ -233,15 +233,35 @@ static char* player_ratings[] = { }; const char* player_rating(void) { - if(combat_rating == 0) return player_ratings[0]; - else if(combat_rating < 50) return player_ratings[1]; - else if(combat_rating < 200) return player_ratings[2]; - else if(combat_rating < 500) return player_ratings[3]; - else if(combat_rating < 1000) return player_ratings[4]; - else if(combat_rating < 2500) return player_ratings[5]; - else if(combat_rating < 10000) return player_ratings[6]; - else return player_ratings[7]; + if(combat_crating == 0) return player_ratings[0]; + else if(combat_crating < 50) return player_ratings[1]; + else if(combat_crating < 200) return player_ratings[2]; + else if(combat_crating < 500) return player_ratings[3]; + else if(combat_crating < 1000) return player_ratings[4]; + else if(combat_crating < 2500) return player_ratings[5]; + else if(combat_crating < 10000) return player_ratings[6]; + else return player_ratings[7]; +} +// Return how much space the player has remaining. +int player_freeSpace(void) { + int i, s; + s = player->ship->cap_weapon; + for(i = 0; i < player->noutfits; i++) + s -= player->outfits[i].quantity * player->outfits[i].outfit->mass; + + return s; +} + +// Return amount of outfits the player owns. +int player_outfitOwned(const char* outfitname) { + int i; + + for(i = 0; i < player->noutfits; i++) + if(strcmp(outfitname, player->outfits[i].outfit->name)==0) + return player->outfits[i].quantity; + + return 0; } // Render the background player stuff, namely planet target @@ -458,7 +478,7 @@ void player_render(void) { gl_print(NULL, gui.misc.x + 10, gui.misc.y - 10 - gl_defFont.h, &cConsole, "SCred:"); - credits2str(str, credits, 2); + credits2str(str, player_credits, 2); i = gl_printWidth(&gl_smallFont, "%s", str); gl_print(&gl_smallFont, gui.misc.x + gui.misc.w - 10 - i, diff --git a/src/player.h b/src/player.h index b01a2e2..6972ac9 100644 --- a/src/player.h +++ b/src/player.h @@ -18,8 +18,8 @@ extern Pilot* pilot; extern char* player_name; extern unsigned int player_flags; -extern unsigned int credits; -extern unsigned int combat_rating; +extern unsigned int player_credits; +extern unsigned int combat_crating; // Enums. typedef enum RadarShape_ { RADAR_RECT, RADAR_CIRCLE } RadarShape; // For render functions. @@ -39,6 +39,8 @@ void player_message(const char* fmt, ...); void player_clear(void); void player_warp(const double x, const double y); const char* player_rating(void); +int player_freeSpace(void); +int player_outfitOwned(const char* outfitname); // Keybind actions. void player_setRadarRel(int mod);