From 2a572b713a8bd0a694484a209a9a8266cb2ba8e6 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 10 Mar 2013 16:07:38 +0000
Subject: [PATCH] [Fix] Enforce outfit selling/buying with limits.

---
 dat/outfit.xml |  8 ++++----
 src/land.c     |  4 +++-
 src/pilot.c    | 26 ++++++++++++++++++++------
 3 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/dat/outfit.xml b/dat/outfit.xml
index 5f8e404..9e336c6 100644
--- a/dat/outfit.xml
+++ b/dat/outfit.xml
@@ -71,9 +71,9 @@
 			<spfx>ExpM</spfx>
       <duration>5</duration>
 			<lockon>1</lockon>
-      <thrust>1200</thrust>
+      <thrust>1600</thrust>
       <turn>200</turn>
-      <speed>600</speed>
+      <speed>800</speed>
       <damage>
         <armour>25</armour>
         <shield>20</shield>
@@ -107,9 +107,9 @@
 			<spfx>ExpM</spfx>
       <duration>7</duration>
 			<lockon>0.5</lockon>
-      <thrust>1200</thrust>
+      <thrust>1300</thrust>
       <turn>200</turn>
-      <speed>600</speed>
+      <speed>650</speed>
       <damage>
         <armour>23</armour>
         <shield>18</shield>
diff --git a/src/land.c b/src/land.c
index 5f3c9dc..af43b4e 100644
--- a/src/land.c
+++ b/src/land.c
@@ -1,3 +1,4 @@
+#include "lephisto.h"
 #include "log.h"
 #include "toolkit.h"
 #include "player.h"
@@ -212,7 +213,8 @@ static void outfits_buy(char* str) {
 		return;
 	}
 
-	player_credits -= outfit->price * pilot_addOutfit(player, outfit, q);
+	player_credits -= outfit->price * pilot_addOutfit(player, outfit,
+				MIN(q, outfit->max));
 	outfits_update(NULL);
 }
 
diff --git a/src/pilot.c b/src/pilot.c
index 6a8cf80..6b1e7b1 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -408,19 +408,31 @@ static void pilot_hyperspace(Pilot* p) {
 }
 
 int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
-	int i;
+	int i, q;
 	char* s;
 
+	q = quantity;
+
 	for(i = 0; i < pilot->noutfits; i++)
 		if(strcmp(outfit->name, pilot->outfits[i].outfit->name)==0) {
 			pilot->outfits[i].quantity += quantity;
-			return quantity;
+			// Can't be over max.
+			if(pilot->outfits[i].quantity > outfit->max) {
+				q -= pilot->outfits[i].quantity - outfit->max;
+				pilot->outfits[i].quantity = outfit->max;
+			}
+			return q;
 		}
 	
 	s = (pilot->secondary) ? pilot->secondary->outfit->name : NULL;
 	pilot->outfits = realloc(pilot->outfits, (pilot->noutfits+1)*sizeof(PilotOutfit));
 	pilot->outfits[pilot->noutfits].outfit 		= outfit;
 	pilot->outfits[pilot->noutfits].quantity 	= quantity;
+	// Can't be over max.
+	if(pilot->outfits[pilot->noutfits].quantity > outfit->max) {
+		q -= pilot->outfits[pilot->noutfits].quantity - outfit->max;
+		pilot->outfits[i].quantity = outfit->max;
+	}
 	pilot->outfits[pilot->noutfits].timer 		= 0;
 
 	if(outfit_isTurret(outfit))
@@ -430,20 +442,22 @@ int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 	// Hack due to realloc possibility.
 	pilot_setSecondary(pilot, s);
 
-	return quantity;
+	return q;
 }
 
 // Remove an outfit from the pilot.
 int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
-	int i;
+	int i, q;
 	char* s;
 
+	q = quantity;
+
 	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) {
 				// We didn't actually remove the full amount.
-				quantity -= pilot->outfits[i].quantity;
+				q += pilot->outfits[i].quantity;
 				// Hack in case it reallocs - Can happen even when shrinking.
 				s = (pilot->secondary) ? pilot->secondary->outfit->name : NULL;
 
@@ -456,7 +470,7 @@ int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 
 				pilot_setSecondary(pilot, s);
 			}
-			return quantity;
+			return q;
 		}
 	WARN("Failure attempting to remove %d '%s' from pilot '%s'",
 				quantity, outfit->name, pilot->name);