From 73b034d1ec58f5e1cd230d433d24f1e92ccbbeb5 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 1 Jan 2014 21:44:00 +0000
Subject: [PATCH] [Add] Added communication framework allowing you to interact
 with other pilots/planets.

---
 dat/ship.xml | 18 +++++++++---------
 src/comm.c   | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/comm.h   |  4 ++++
 src/input.c  | 11 ++++++++++-
 src/player.c |  6 ++++++
 src/player.h |  1 +
 6 files changed, 84 insertions(+), 10 deletions(-)
 create mode 100644 src/comm.c
 create mode 100644 src/comm.h

diff --git a/dat/ship.xml b/dat/ship.xml
index 8ac79b8..35d509e 100644
--- a/dat/ship.xml
+++ b/dat/ship.xml
@@ -10,22 +10,22 @@
   <tech>7</tech>
   <description>!Used for testing!</description>
   <movement>
-   <thrust>220</thrust>
+   <thrust>320</thrust>
    <turn>135</turn>
    <speed>260</speed>
   </movement>
   <health>
-   <shield>340</shield>
-   <armour>300</armour>
-   <energy>380</energy>
-   <shield_regen>380</shield_regen>
-   <armour_regen>360</armour_regen>
-   <energy_regen>1000</energy_regen>
+   <shield>11340</shield>
+   <armour>11300</armour>
+   <energy>11380</energy>
+   <shield_regen>11380</shield_regen>
+   <armour_regen>11360</armour_regen>
+   <energy_regen>11000</energy_regen>
   </health>
   <characteristics>
-   <crew>4</crew>
+   <crew>10</crew>
    <mass>95</mass>
-   <fuel>1000</fuel>
+   <fuel>1000000</fuel>
    <cap_weapon>800</cap_weapon>
    <cap_cargo>6000</cap_cargo>
   </characteristics>
diff --git a/src/comm.c b/src/comm.c
new file mode 100644
index 0000000..9a6ba11
--- /dev/null
+++ b/src/comm.c
@@ -0,0 +1,54 @@
+/**
+ * @file comm.c
+ *
+ * @brief For communicating with planets/pilots.
+ */
+
+#include "lephisto.h"
+#include "log.h"
+#include "toolkit.h"
+#include "pilot.h"
+#include "comm.h"
+
+#define COMM_WIDTH    320 /**< Communication window width. */
+#define COMM_HEIGHT   240 /**< Communication window height. */
+
+#define BUTTON_WIDTH  90  /**< Button width. */
+#define BUTTON_HEIGHT 30  /*<< Button height. */
+
+static Pilot* comm_pilot = NULL; /**< Pilot currently talking to. */
+static int comm_wid = 0;         /**< Communication window ID. */
+
+static void comm_close(char* str);
+
+/**
+ * @brief Open the communication dialogue with a pilot.
+ *    @param pilot Pilot to communicate with.
+ *    @return 0 on success.
+ */
+int comm_open(unsigned int pilot) {
+  char buf[128];
+
+  /* Get the pilot. */
+  comm_pilot = pilot_get(pilot);
+  if(comm_pilot == NULL)
+    return -1;
+
+  /* Create the window. */
+  snprintf(buf, 128, "Comm - %s", comm_pilot->name);
+  comm_wid = window_create(buf, -1, -1, COMM_WIDTH, COMM_HEIGHT);
+
+  window_addButton(comm_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
+      "btnClose", "Close Channel", comm_close);
+
+  return 0;
+}
+
+static void comm_close(char* str) {
+  (void)str;
+  if(comm_wid > 0) {
+    window_destroy(comm_wid);
+    comm_wid = 0;
+  }
+}
+
diff --git a/src/comm.h b/src/comm.h
new file mode 100644
index 0000000..474b11c
--- /dev/null
+++ b/src/comm.h
@@ -0,0 +1,4 @@
+#pragma once
+
+int comm_open(unsigned int pilot);
+
diff --git a/src/input.c b/src/input.c
index 0de096c..4e8d436 100644
--- a/src/input.c
+++ b/src/input.c
@@ -45,6 +45,8 @@ const char* keybindNames[] = {
   "secondary", "secondary_next",
   /* Space Navigation. */
   "autonav", "target_planet", "land", "thyperspace","starmap", "jump",
+  /* Communication. */
+  "hail",
   /* Misc. */
   "mapzoomin", "mapzoomout", "screenshot", "pause", "menu", "info",
   "end" /* Must terminate at the end. */
@@ -94,7 +96,8 @@ void input_setDefault(void) {
   input_setKeybind("thyperspace",     KEYBIND_KEYBOARD, SDLK_h,           KMOD_NONE,  0);
   input_setKeybind("starmap",         KEYBIND_KEYBOARD, SDLK_m,           KMOD_NONE,  0);
   input_setKeybind("jump",            KEYBIND_KEYBOARD, SDLK_j,           KMOD_NONE,  0);
-
+  /* Communication. */
+  input_setKeybind("hail",            KEYBIND_KEYBOARD, SDLK_y,           KMOD_NONE,  0);
   /* Misc. */
   input_setKeybind("mapzoomin",       KEYBIND_KEYBOARD, SDLK_KP_PLUS,     KMOD_NONE,  0);
   input_setKeybind("mapzoomout",      KEYBIND_KEYBOARD, SDLK_KP_MINUS,    KMOD_NONE,  0);
@@ -362,6 +365,12 @@ static void input_key(int keynum, double value, int kabs) {
       player_jump();
     }
   }
+  /* Communication. */
+  else if(KEY("hail") && INGAME() && NOHYP()) {
+    if(value == KEY_PRESS) {
+      player_hail();
+    }
+  }
   /* Zoom in. */
   else if(KEY("mapzoomin") && INGAME()) {
     if(value == KEY_PRESS) player_setRadarRel(1);
diff --git a/src/player.c b/src/player.c
index 135e5aa..2e1f7e0 100644
--- a/src/player.c
+++ b/src/player.c
@@ -30,6 +30,7 @@
 #include "lfile.h"
 #include "spfx.h"
 #include "unidiff.h"
+#include "comm.h"
 #include "player.h"
 
 #define XML_GUI_ID    "GUIs"    /**< XML section identifier for GUI document. */
@@ -2030,6 +2031,11 @@ void player_screenshot(void) {
   gl_screenshot(filename);
 }
 
+void player_hail(void) {
+  if(player->target != player->id)
+    comm_open(player->target);
+}
+
 /**
  * @fn void player_dead(void)
  *
diff --git a/src/player.h b/src/player.h
index b0c5b99..8d1d636 100644
--- a/src/player.h
+++ b/src/player.h
@@ -88,4 +88,5 @@ void player_accel(double acc);
 void player_accelOver(void);
 void player_startAutonav(void);
 void player_abortAutonav(void);
+void player_hail(void);