From 6ec1a2a407c255ab74d0d382667c8e7643e01e52 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 16 May 2013 21:47:04 +0100
Subject: [PATCH] [Add] Hellz yeah!! You can refuel when low now.

---
 src/land.c    | 36 ++++++++++++++++++++++++++++++++++++
 src/toolkit.c |  1 +
 2 files changed, 37 insertions(+)

diff --git a/src/land.c b/src/land.c
index 50e5f51..9d80c1f 100644
--- a/src/land.c
+++ b/src/land.c
@@ -107,6 +107,9 @@ static void misn_close(char* str);
 static void misn_accept(char* str);
 static void misn_genList(int first);
 static void misn_update(char* str);
+// Refuel.
+static int refuel_price(void);
+static void spaceport_refuel(char* str);
 
 // The local market.
 static void commodity_exchange(void) {
@@ -923,8 +926,30 @@ static void misn_update(char* str) {
   window_enableButton(secondary_wid, "btnAcceptMission");
 }
 
+// Return how much it will cost to refuel the player.
+static int refuel_price(void) {
+  return (player->fuel_max - player->fuel)*3;
+}
+
+// Refuel the player.
+static void spaceport_refuel(char* str) {
+  (void)str;
+
+  if(player->credits < refuel_price()) {
+    dialogue_alert("Need more Scred",
+        "You seem to not have enough Scred to refuel your ship");
+    return;
+  }
+  
+  player->credits -= refuel_price();
+  player->fuel = player->fuel_max;
+  window_destroyWidget(land_wid, "btnRefuel");
+}
+
 // Land the player.
 void land(Planet* p) {
+  char buf[32], cred[16];
+
   if(landed) return;
 
   // Change music.
@@ -957,6 +982,7 @@ void land(Planet* p) {
                      BUTTON_WIDTH, BUTTON_HEIGHT, "btnOutfits",
                      "Outfits", (void(*)(char*))outfits);
 
+  // Third column.
   if(planet_hasService(land_planet, PLANET_SERVICE_BASIC)) {
     window_addButton(land_wid, 20, 20,
                      BUTTON_WIDTH, BUTTON_HEIGHT, "btnNews",
@@ -965,6 +991,16 @@ void land(Planet* p) {
     window_addButton(land_wid, 20, 20 + BUTTON_HEIGHT + 20,
                      BUTTON_WIDTH, BUTTON_HEIGHT, "btnBar",
                      "Spaceport Bar", (void(*)(char*))spaceport_bar);
+
+    if(player->fuel < player->fuel_max) {
+      credits2str(cred, refuel_price(), 2);
+      snprintf(buf, 32, "Refuel %s", cred);
+      window_addButton(land_wid, -20, 20+2*(BUTTON_HEIGHT+20),
+          BUTTON_WIDTH, BUTTON_HEIGHT, "btnRefuel",
+          buf, spaceport_refuel);
+      if(player->credits < refuel_price()) // Not enough memory.
+        window_disableButton(land_wid, "btnRefuel");
+    }
   }
 
 
diff --git a/src/toolkit.c b/src/toolkit.c
index 57b8d01..6f43e75 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -581,6 +581,7 @@ void window_destroyWidget(unsigned int wid, const char* wgtname) {
     return;
   }
 
+  if(w->focus == i) w->focus = -1;
   widget_cleanup(&w->widgets[i]);
   if(i < w->nwidgets-1)
     // This isn't the last widget.