From 34a6be43dfd7368fbea63b1ce364079565b8ad25 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 24 Feb 2013 15:40:34 +0000
Subject: [PATCH] [Add] Merchant vessels are now able to go to hyperspace.
 [Change] Overpowered merchant ship a little for testing purposes.

---
 dat/ship.xml            |  2 +-
 dat/start.xml           |  2 +-
 scripts/ai/merchant.lua | 15 +++++++++++++--
 src/ai.c                | 29 ++++++++++++++++-------------
 src/pilot.c             |  8 +++++---
 src/player.c            |  8 +++++---
 src/sound.c             |  2 +-
 src/space.c             |  2 --
 8 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/dat/ship.xml b/dat/ship.xml
index 3839e62..3fc2722 100644
--- a/dat/ship.xml
+++ b/dat/ship.xml
@@ -25,7 +25,7 @@
 			<cap_cargo>25</cap_cargo>
 		</characteristics>
 		<outfits>
-			<outfit quantity='1'>Laser</outfit>
+			<outfit quantity='3'>Laser</outfit>
 		</outfits>
   </ship>
   <ship name = "Leapard">
diff --git a/dat/start.xml b/dat/start.xml
index e3dd9c2..f034a85 100644
--- a/dat/start.xml
+++ b/dat/start.xml
@@ -1,7 +1,7 @@
 <Start>
   <name>Dark Tides</name>
   <player>
-    <ship>Lancer</ship>
+    <ship>Merchant Ship</ship>
     <credits>
       <low>500</low>
       <high>1500</high>
diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua
index 46c2403..681f74a 100644
--- a/scripts/ai/merchant.lua
+++ b/scripts/ai/merchant.lua
@@ -3,7 +3,10 @@ control_rate = 2
 
 -- Required "control" function.
 function control()
-  if ai.taskname() == "none" then
+	task = ai.taskname()
+	if task == "hyperspace" then
+		ai.hyperspace() -- Try to go to hyperspace.
+	elseif task == "none" then
     planet = ai.rndplanet()
     ai.pushtask(0, "go", planet)
   end
@@ -62,7 +65,15 @@ end
 --Waits.
 function land()
   if ai.timeup(0) then
-    ai.pushtask(0, "runaway", player)
+    ai.pushtask(0, "hyperspace")
   end
 end
 
+-- Go to hyperspace YEAAAHHH!!
+function hyperspace()
+	dir = ai.face(-1) -- Face away from (0,0)
+	if(dir < 10) then 
+		ai.accel()
+	end
+end
+
diff --git a/src/ai.c b/src/ai.c
index 5237bba..1e18aa4 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -588,22 +588,25 @@ static int ai_face(lua_State* L) {
   MIN_ARGS(1);
   Vec2* v; // Grab the position to face.
   Pilot* p;
-  
-  if(lua_isnumber(L,1)) {
-   p = pilot_get((unsigned int)lua_tonumber(L,1));
-   if(p == NULL) return 0; // Make sure pilot is valid.
-   v = &p->solid->pos;
+  double mod, diff;
+	int invert = 0;
+	int n = -2;
+	
+	if(lua_isnumber(L, 1))
+		n = (int)lua_tonumber(L, 1);
+	
+	if(n >= 0) {
+		p = pilot_get(n);
+   	if(p == NULL) return 0; // Make sure pilot is valid.
+   	v = &p->solid->pos;
   }
   else if(lua_islightuserdata(L,1)) v = (Vec2*)lua_topointer(L,1);
 
-  double mod = -10;
-  if(lua_gettop(L) > 1 && lua_isnumber(L,2))
-    switch((int)lua_tonumber(L,2)) {
-      case 0: break;
-      case 1: mod *= -1; break;
-      case 2: break;
-    }
-  double diff = angle_diff(cur_pilot->solid->dir, vect_angle(&cur_pilot->solid->pos, v));
+  mod = -10;
+  if(lua_gettop(L) > 1 && lua_isnumber(L,2)) invert = (int)lua_tonumber(L,2);
+	if(invert) mod *= -1;
+	diff = angle_diff(cur_pilot->solid->dir,
+				(n==-1) ? VANGLE(cur_pilot->solid->pos) : vect_angle(&cur_pilot->solid->pos, v));
   
   pilot_turn = mod*diff;
 
diff --git a/src/pilot.c b/src/pilot.c
index e06bcbe..833aebb 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -1,7 +1,6 @@
 #include <string.h>
 #include <math.h>
 #include <stdlib.h>
-#include <assert.h> // We don't need this?
 
 #include "lephisto.h"
 #include "log.h"
@@ -280,13 +279,13 @@ static void pilot_hyperspace(Pilot* p) {
     double diff;
     
     if(VMOD(p->solid->vel) > MIN_VEL_ERR) {
-      diff = pilot_face(p, VANGLE(player->solid->vel) + M_PI);
+      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);
     } else {
       vectnull(&p->solid->force); // Stop accelerating.
-      diff = pilot_face(p, VANGLE(player->solid->pos));
+      diff = pilot_face(p, VANGLE(p->solid->pos));
 
       if(ABS(diff) < MAX_DIR_ERR) {
         // We should prepare for the jump now.
@@ -419,6 +418,9 @@ void pilot_destroy(Pilot* p) {
   for(i = 0; i < pilots; i++)
     if(pilot_stack[i] == p)
       break;
+	
+	pilots--;
+
   while(i < pilots) {
     pilot_stack[i] = pilot_stack[i+1];
     i++;
diff --git a/src/player.c b/src/player.c
index 26f933f..9cfdf1d 100644
--- a/src/player.c
+++ b/src/player.c
@@ -250,9 +250,11 @@ void player_render(void) {
   glFont* f;
 
   // Render the player target graphics.
-  if(player_target != PLAYER_ID) {
-    p = pilot_get(player_target);
-
+  if(player_target != PLAYER_ID) p = pilot_get(player_target);
+	else p = NULL;
+	if(p == NULL) player_target = PLAYER_ID; // No more pilot target.
+	else {
+		// There is still a pilot target.
     if(pilot_isDisabled(p)) c = &cInert;
     else if(pilot_isFlag(p, PILOT_HOSTILE)) c = &cHostile;
     else c = &cNeutral;
diff --git a/src/sound.c b/src/sound.c
index 21e39c9..2c598e0 100644
--- a/src/sound.c
+++ b/src/sound.c
@@ -52,7 +52,7 @@ static int nvoice_stack = 0;
 static int mvoice_stack = 0;
 
 // Volume.
-static ALfloat svolume = 0.5;
+static ALfloat svolume = 0.3;
 
 static int sound_makeList(void);
 static int sound_load(ALuint* buffer, char* filename);
diff --git a/src/space.c b/src/space.c
index dcb1ed0..63319c5 100644
--- a/src/space.c
+++ b/src/space.c
@@ -167,8 +167,6 @@ int space_canHyperspace(Pilot* p) {
 // Hyperspace, returns 0 if entering hyperspace, or the distance if not.
 int space_hyperspace(Pilot* p) {
   if(!space_canHyperspace(p)) return -1;
-  // Too fast.
-  //if(VMOD(p->solid->vel) > MAX_HYPERSPACE_VEL) return -2;
 
   // Pilot is now going to get automatically ready for hyperspace.
   pilot_setFlag(p, PILOT_HYP_PREP);