From eeb09fdf050559d784d81aa4ddc443074c69ab43 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Fri, 8 Mar 2013 13:54:30 +0000
Subject: [PATCH] [Add] Make $$ from selling outfits.

---
 src/land.c   | 16 ++++++++++++----
 src/pilot.c  | 14 ++++++++++----
 src/pilot.h  |  4 ++--
 src/player.c | 12 ++++++------
 src/player.h |  4 ++--
 5 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/src/land.c b/src/land.c
index 2270f09..c5a1edf 100644
--- a/src/land.c
+++ b/src/land.c
@@ -41,9 +41,6 @@ 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);
@@ -192,6 +189,11 @@ static void outfits_buy(char* str) {
 					outfit->mass - player_freeSpace());
 		return;
 	}
+	else if(player_outfitOwned(outfitname) >= outfit->max) {
+		// Already has too many.
+		toolkit_alert("You can only carry %d of this outfit.", outfit->max);
+		return;
+	}
 
 	pilot_addOutfit(player, outfit, q);
 	outfits_update(NULL);
@@ -207,7 +209,13 @@ static void outfits_sell(char* str) {
 	outfit = outfit_get(outfitname);
 	q = 1;
 
-	pilot_rmOutfit(player, outfit, q);
+	if(player_outfitOwned(outfitname) <= 0) {
+		// No outfits to sell.
+		toolkit_alert("You can't sell something you don't have!");
+		return;
+	}
+
+	player_credits += outfit->price * pilot_rmOutfit(player, outfit, q);
 	outfits_update(NULL);
 }
 
diff --git a/src/pilot.c b/src/pilot.c
index 7de142b..92b08f7 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -403,14 +403,14 @@ static void pilot_hyperspace(Pilot* p) {
   }
 }
 
-void pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
+int pilot_addOutfit(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;
-			return;
+			return quantity;
 		}
 	
 	s = (pilot->secondary) ? pilot->secondary->outfit->name : NULL;
@@ -425,10 +425,12 @@ void pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 	
 	// Hack due to realloc possibility.
 	pilot_setSecondary(pilot, s);
+
+	return quantity;
 }
 
 // Remove an outfit from the pilot.
-void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
+int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 	int i;
 	char* s;
 
@@ -436,6 +438,8 @@ void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 		if(strcmp(outfit->name, pilot->outfits[i].outfit->name)==0) {
 			pilot->outfits[i].quantity -= quantity;
 			if(pilot->outfits[i].quantity <= 0) {
+				// We didn't actually remove the full amount.
+				quantity -= pilot->outfits[i].quantity;
 				// Hack in case it reallocs - Can happen even when shrinking.
 				s = (pilot->secondary) ? pilot->secondary->outfit->name : NULL;
 
@@ -448,10 +452,12 @@ void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 
 				pilot_setSecondary(pilot, s);
 			}
-			return;
+			return quantity;
 		}
 	WARN("Failure attempting to remove %d '%s' from pilot '%s'",
 				quantity, outfit->name, pilot->name);
+	
+	return 0;
 }
 
 // ==Init pilot.===========================================
diff --git a/src/pilot.h b/src/pilot.h
index 3be9a29..c4b3cda 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -118,8 +118,8 @@ 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);
-void pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity);
-void pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity);
+int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity);
+int 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 2e9ba38..8384f3d 100644
--- a/src/player.c
+++ b/src/player.c
@@ -27,13 +27,13 @@
 // Player stuff.
 Pilot* player = NULL; // extern in pilot.h
 // Player global properties.
-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.
+char* player_name = NULL; 			// Player name.
+int player_credits = 0; 				// Ze monies.
+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.
+double player_turn = 0.; 				// Turn velocity from input.
+double player_acc  = 0.; 				// Accel velocity from input.
 unsigned int player_target = PLAYER_ID; // Targetted pilot.
 static int planet_target = -1; // Targetted planet.
 // Internal.
diff --git a/src/player.h b/src/player.h
index 6972ac9..7441469 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 player_credits;
-extern unsigned int combat_crating;
+extern int player_credits;
+extern int combat_crating;
 
 // Enums.
 typedef enum RadarShape_ { RADAR_RECT, RADAR_CIRCLE } RadarShape; // For render functions.