From b66c6f7ecf7312eafdfdd1d76ec31ebdd9c9b87f Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 17 Mar 2013 19:28:45 +0000
Subject: [PATCH] [Add] Attempted to get modifications actually working.
 Everything is brokened. Don't even attempt to play..

---
 scripts/ai/merchant.lua |  2 +-
 src/ai.c                | 14 +++----
 src/outfit.c            |  1 +
 src/pilot.c             | 83 +++++++++++++++++++++++++++++++++--------
 src/pilot.h             |  3 ++
 src/player.c            |  4 +-
 6 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua
index f52d7c1..9a253d1 100644
--- a/scripts/ai/merchant.lua
+++ b/scripts/ai/merchant.lua
@@ -49,7 +49,7 @@ function create()
 	elseif num == 1 then
 		cargo = "Ore"
 	end
-	ai.setCargo(cargo, ai.rnd(0, ai.cargofree()))
+	ai.setcargo(cargo, ai.rnd(0, ai.cargofree()))
 end
 
 -- Runs away.
diff --git a/src/ai.c b/src/ai.c
index ab0b4f1..b43ec1d 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -350,8 +350,8 @@ void ai_think(Pilot* pilot) {
 
   cur_pilot->solid->dir_vel = 0.;
   if(pilot_turn) // Set the turning velocity.
-    cur_pilot->solid->dir_vel -= cur_pilot->ship->turn * pilot_turn;
-  vect_pset(&cur_pilot->solid->force, cur_pilot->ship->thrust * pilot_acc,
+    cur_pilot->solid->dir_vel -= cur_pilot->turn * pilot_turn;
+  vect_pset(&cur_pilot->solid->force, cur_pilot->thrust * pilot_acc,
 				cur_pilot->solid->dir);
   
   // Fire weapons if needs be.
@@ -553,16 +553,16 @@ static int ai_getpos(lua_State* L) {
 // 
 // Braking vel ==> 0 = v - a*dt
 // Add turn around time (to initial velocity) :
-// ==> 180.*360./cur_pilot->ship->turn
+// ==> 180.*360./cur_pilot->turn
 // Add it to general euler equation x = v*t + 0.5 * a * t^2
 // Have fun.
 // ========================================================
 static int ai_minbrakedist(lua_State* L) {
   double time = VMOD(cur_pilot->solid->vel) /
-        (cur_pilot->ship->thrust / cur_pilot->solid->mass);
+        (cur_pilot->thrust / cur_pilot->solid->mass);
 
-  double dist = VMOD(cur_pilot->solid->vel)*(time + 180./cur_pilot->ship->turn)-
-        0.5 * (cur_pilot->ship->thrust / cur_pilot->solid->mass)*time*time;
+  double dist = VMOD(cur_pilot->solid->vel)*(time + 180./cur_pilot->turn)-
+        0.5 * (cur_pilot->thrust / cur_pilot->solid->mass)*time*time;
 
   lua_pushnumber(L, dist); // return
   return 1;
@@ -585,7 +585,7 @@ static int ai_exists(lua_State* L) {
 
 // Are we at max velocity?
 static int ai_ismaxvel(lua_State* L) {
-  lua_pushboolean(L, VMOD(cur_pilot->solid->vel) == cur_pilot->ship->speed);
+  lua_pushboolean(L,(VMOD(cur_pilot->solid->vel)>cur_pilot->speed-MIN_VEL_ERR));
   return 1;
 }
 
diff --git a/src/outfit.c b/src/outfit.c
index 8da21f0..653f7bc 100644
--- a/src/outfit.c
+++ b/src/outfit.c
@@ -90,6 +90,7 @@ int outfit_isTurret(const Outfit* o) {
 				(o->type == OUTFIT_TYPE_TURRET_BEAM));
 }
 
+// Return 1 if o is a modification.
 int outfit_isMod(const Outfit* o) {
 	return (o->type == OUTFIT_TYPE_MODIFICATION);
 }
diff --git a/src/pilot.c b/src/pilot.c
index 41df44e..be70cf2 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -44,6 +44,7 @@ static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t);
 static void pilot_update(Pilot* pilot, const double dt);
 static void pilot_hyperspace(Pilot* pilot);
 void pilot_render(Pilot* pilot);
+static void pilot_calcStats(Pilot* pilot);
 static void pilot_free(Pilot* p);
 static Fleet* fleet_parse(const xmlNodePtr parent);
 static void pilot_dead(Pilot* p);
@@ -128,7 +129,7 @@ double pilot_face(Pilot* p, const float dir) {
 
   p->solid->dir_vel = 0.;
   if(turn)
-    p->solid->dir_vel -= p->ship->turn * turn;
+    p->solid->dir_vel -= p->turn * turn;
 
   return diff;
 }
@@ -358,11 +359,11 @@ static void pilot_update(Pilot* pilot, const double dt) {
   }
   // We are still alive.
   else if(pilot->armour < pilot->armour_max) {
-    pilot->armour += pilot->ship->armour_regen*dt;
-		pilot->energy += pilot->ship->energy_regen*dt;
+    pilot->armour += pilot->armour_regen*dt;
+		pilot->energy += pilot->energy_regen*dt;
 	} else {
-    pilot->shield += pilot->ship->shield_regen*dt;
-		pilot->energy += pilot->ship->energy_regen*dt;
+    pilot->shield += pilot->shield_regen*dt;
+		pilot->energy += pilot->energy_regen*dt;
 	}
 
   if(pilot->armour > pilot->armour_max) pilot->armour = pilot->armour_max;
@@ -376,7 +377,7 @@ static void pilot_update(Pilot* pilot, const double dt) {
   
   if(!pilot_isFlag(pilot, PILOT_HYPERSPACE))
     // Should not go faster.
-		limit_speeddt(&pilot->solid->vel, pilot->ship->speed, dt);
+		limit_speeddt(&pilot->solid->vel, pilot->speed, dt);
 }
 
 // Pilot is getting ready or is in, hyperspace.
@@ -392,7 +393,7 @@ static void pilot_hyperspace(Pilot* p) {
         pilot_setFlag(p, PILOT_DELETE); // Set flag to delete pilot.
       return;
     }
-    vect_pset(&p->solid->force, p->ship->thrust * 3., p->solid->dir);
+    vect_pset(&p->solid->force, p->thrust * 3., p->solid->dir);
   }
   else if(pilot_isFlag(p, PILOT_HYP_BEGIN)) {
     if(SDL_GetTicks() > p->ptimer) {
@@ -407,7 +408,7 @@ static void pilot_hyperspace(Pilot* p) {
       diff = pilot_face(p, VANGLE(p->solid->vel) + M_PI);
     
     	if(ABS(diff) < MAX_DIR_ERR) // Brake.
-      	vect_pset(&p->solid->force, p->ship->thrust, p->solid->dir);
+      	vect_pset(&p->solid->force, p->thrust, p->solid->dir);
     } else {
       vectnull(&p->solid->force); // Stop accelerating.
 			
@@ -439,6 +440,7 @@ int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 				q -= pilot->outfits[i].quantity - outfit->max;
 				pilot->outfits[i].quantity = outfit->max;
 			}
+			//pilot_calcStats(pilot);
 			return q;
 		}
 	
@@ -460,6 +462,7 @@ int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 	// Hack due to realloc possibility.
 	pilot_setSecondary(pilot, s);
 
+	pilot_calcStats(pilot);
 	return q;
 }
 
@@ -496,6 +499,55 @@ int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
 	return 0;
 }
 
+// Recalculate the pilot's stats based on her outfits.
+static void pilot_calcStats(Pilot* pilot) {
+	int i;
+	double q;
+	Outfit* o;
+	double ac, sc, ec; // Temp health coeficients to set.
+
+	// -- Set up the basic stuff.
+	// Movement.
+	pilot->thrust = pilot->ship->thrust;
+	pilot->turn 	= pilot->ship->turn;
+	pilot->speed 	= pilot->ship->speed;
+	// Health.
+	ac = pilot->armour 	/ pilot->armour_max;
+	sc = pilot->shield 	/ pilot->shield_max;
+	ec = pilot->energy 	/ pilot->energy_max;
+	pilot->armour_max 	= pilot->ship->armour;
+	pilot->shield_max 	= pilot->ship->shield;
+	pilot->energy_max 	= pilot->ship->energy;
+	pilot->armour_regen = pilot->ship->armour_regen;
+	pilot->shield_regen = pilot->ship->shield_regen;
+	pilot->energy_regen = pilot->ship->energy_regen;
+
+	// Now add outfit changes.
+	for(i = 0; i < pilot->noutfits; i++)
+		if(outfit_isMod(pilot->outfits[i].outfit)) {
+			q = (double) pilot->outfits[i].quantity;
+			o = pilot->outfits[i].outfit;
+
+			// Movement.
+			pilot->thrust += o->u.mod.thrust 	* q;
+			pilot->turn 	+= o->u.mod.turn 		* q;
+			pilot->speed	+= o->u.mod.speed 	* q;
+			// Health.
+			pilot->armour_max 	+= o->u.mod.armour 				* q;
+			pilot->armour_regen += o->u.mod.armour_regen 	* q;
+			pilot->shield_max 	+= o->u.mod.shield 				* q;
+			pilot->shield_regen += o->u.mod.shield_regen 	* q;
+			pilot->energy_max 	+= o->u.mod.energy				* q;
+			pilot->energy_regen += o->u.mod.energy_regen 	* q;
+
+		}
+
+	// Give the pilot her health proportion back.
+	pilot->armour = ac * pilot->armour_max;
+	pilot->shield = sc * pilot->shield_max;
+	pilot->energy = ec * pilot->energy_max;
+}
+
 // Try to add quantity of cargo to pilot, return quantity actually added.
 int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) {
 	int i, q;
@@ -581,14 +633,6 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction,
   // Solid.
   pilot->solid = solid_create(ship->mass, dir, pos, vel);
 
-  // Max shields armour.
-  pilot->armour_max   = ship->armour;
-  pilot->shield_max   = ship->shield;
-  pilot->energy_max   = ship->energy;
-  pilot->armour       = pilot->armour_max;
-  pilot->shield       = pilot->shield_max;
-  pilot->energy       = pilot->energy_max;
-
   // Initially idle.
   pilot->task = NULL;
 
@@ -610,6 +654,13 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction,
     }
   }
 
+	// Set the pilot stats based on her ship and outfits.
+	// Hack to have full armour/shield/energy.
+	pilot->armour = pilot->armour_max = 1.;
+	pilot->shield = pilot->shield_max = 1.;
+	pilot->energy = pilot->energy_max = 1.;
+	pilot_calcStats(pilot);
+
 	// Cargo.
 	pilot->credits = 0;
 	pilot->commodities = NULL;
diff --git a/src/pilot.h b/src/pilot.h
index 5a45cc9..60d5085 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -66,9 +66,12 @@ typedef struct Pilot_ {
   Solid* solid;   // Associated solid (physics).
 	int tsx, tsy;		// Current sprite, calculated on update.
 
+	double thrust, turn, speed;
+
   // Current health.
   double armour, shield, energy;
   double armour_max, shield_max, energy_max;
+	double armour_regen, shield_regen, energy_regen;
   double fuel; // Used only for jumps. TODO: make it do something.
 
   void (*think)(struct Pilot_*); // AI thinking for the pilot.
diff --git a/src/player.c b/src/player.c
index 7a4fe9d..67b2f33 100644
--- a/src/player.c
+++ b/src/player.c
@@ -900,14 +900,14 @@ void player_think(Pilot* player) {
   else {
     player->solid->dir_vel = 0.;
     if(player_turn)
-      player->solid->dir_vel -= player->ship->turn * player_turn;
+      player->solid->dir_vel -= player->turn * player_turn;
   }
 
   if(player_isFlag(PLAYER_PRIMARY)) pilot_shoot(player, player_target, 0);
   if(player_isFlag(PLAYER_SECONDARY)) // Needs a target.
     pilot_shoot(player, player_target, 1);
 
-  vect_pset(&player->solid->force, player->ship->thrust * player_acc,
+  vect_pset(&player->solid->force, player->thrust * player_acc,
 				player->solid->dir);
 
 	// Set the listener stuff.