From 856ea04d8dceb3b1c5f9c743ecd19fe9b798773c Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 16 May 2013 15:18:12 +0100
Subject: [PATCH] [Add] Prompt before overwriting pilot's save.

---
 src/lephisto.c |  2 +-
 src/lfile.c    | 26 ++++++++++++++++++++++++++
 src/lfile.h    |  1 +
 src/player.c   | 15 +++++++++++++++
 src/space.c    |  2 ++
 5 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/lephisto.c b/src/lephisto.c
index 03cbd80..7c4ae45 100644
--- a/src/lephisto.c
+++ b/src/lephisto.c
@@ -241,7 +241,7 @@ void main_loop(void) {
   fps_control(); // Everyone loves fps control..
   if(toolkit) toolkit_update(); // To simulate key repetition.
   if(!menu_isOpen(MENU_MAIN)) {
-    if(!paused && !toolkit) update_all(); // Update game.
+    if(!paused) update_all(); // Update game.
     render_all();
   }
   if(toolkit) toolkit_render();
diff --git a/src/lfile.c b/src/lfile.c
index 1c97626..298a28a 100644
--- a/src/lfile.c
+++ b/src/lfile.c
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <stdarg.h>
 #ifdef LINUX
 #include <stdlib.h>
 #include <sys/types.h>
@@ -51,6 +52,31 @@ int lfile_dirMakeExist(char* path) {
   return 0;
 }
 
+// Check if a file exists.
+int lfile_fileExists(char* path, ...) {
+  char file[PATH_MAX], name[PATH_MAX];
+  va_list ap;
+  size_t l;
+
+  l = 0;
+  if(path == NULL) return -1;
+  else { // Get the message.
+    va_start(ap, path);
+    vsnprintf(name, PATH_MAX-l, path, ap);
+    l = strlen(name);
+    va_end(ap);
+  }
+
+  snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), name);
+#ifdef LINUX
+  struct stat buf;
+
+  if(stat(file, &buf)==0) // Stat worked, file must exist.
+    return 1;
+#endif
+  return 0;
+}
+
 // List all the files in a dir (besides . and ..).
 char** lfile_readDir(int* lfiles, char* path) {
   char file[PATH_MAX];
diff --git a/src/lfile.h b/src/lfile.h
index f2d1444..d6de045 100644
--- a/src/lfile.h
+++ b/src/lfile.h
@@ -2,5 +2,6 @@
 
 char*   lfile_basePath(void);
 int     lfile_dirMakeExist(char* path);
+int     lfile_fileExists(char* path, ...);
 char**  lfile_readDir(int* lfiles, char* path);
 
diff --git a/src/player.c b/src/player.c
index 4b00a64..401e9cc 100644
--- a/src/player.c
+++ b/src/player.c
@@ -150,7 +150,9 @@ int player_save(xmlTextWriterPtr writer);
 
 // Prompt player name.
 void player_new(void) {
+  int r;
   // Let's not seg fault due to a lack of environment.
+  player_flags = 0;
   player_setFlag(PLAYER_DESTROYED);
   vectnull(&player_cam);
   gl_bindCamera(&player_cam);
@@ -163,6 +165,17 @@ void player_new(void) {
 
   player_name = dialogue_input("Player Name", 3, 20,
                                "Please tell me your name:");
+
+  if(lfile_fileExists("saves/%s.ls", player_name)) {
+    r = dialogue_YesNo("Overwrite",
+        "You already have a pilot named %s. Overwrite?", player_name);
+    if(r == 0) {
+      // Nupe.
+      player_new();
+      return;
+    }
+  }
+
   player_newMake();
 }
 
@@ -473,6 +486,8 @@ void player_render(void) {
   glColour* c;
   glFont* f;
 
+  if(player == NULL) return;
+
   if(player_isFlag(PLAYER_DESTROYED) || pilot_isFlag(player, PILOT_DEAD)) {
     if(player_isFlag(PLAYER_DESTROYED)) {
       if(!toolkit && (SDL_GetTicks() > player_timer))
diff --git a/src/space.c b/src/space.c
index bcbdcca..ae429ea 100644
--- a/src/space.c
+++ b/src/space.c
@@ -501,6 +501,8 @@ void space_update(const double dt) {
 
   (void)dt; // Don't need it right now.
 
+  if(cur_system == NULL) return; // Can't update a null system.
+
   t = SDL_GetTicks();
 
   if(cur_system->nfleets == 0)