DEADSOFTWARE

files: add F_getsprid description
[flatwaifu.git] / src / files.c
index deac9e3e2898da9726eeb48a4bc84d8d36b59ec8..913a5eea0358792653067faeab92a248f74eee7a 100644 (file)
 #include <assert.h>
 #include "files.h"
 #include "error.h"
-#include "cp866.h"
 
 #include "map.h" // MAP_load
+#include "save.h" // SAVE_getname
+
+#ifdef UNIX
+#  include <sys/stat.h>
+#endif
 
 #include "common/streams.h"
 #include "common/files.h"
 #include "common/wadres.h"
+#include "common/cp866.h"
 
 int d_start, d_end;
+
+char savname[SAVE_MAX][SAVE_MAXLEN];
+char savok[SAVE_MAX];
+
 static int m_start, m_end;
 static int s_start, s_end;
 
-void F_startup (void) {
-  logo("F_startup: setup file system\n");
-}
-
 void F_addwad (const char *fn) {
   static int i = 0;
   static FILE_Stream wadh[MAX_WADS];
@@ -60,9 +65,6 @@ void F_initwads (void) {
   if (!WADRES_rehash()) {
     ERR_failinit("F_initwads: failed rehash");
   }
-}
-
-void F_allocres (void) {
   d_start = F_getresid("D_START");
   d_end = F_getresid("D_END");
   m_start = F_getresid("M_START");
@@ -71,10 +73,6 @@ void F_allocres (void) {
   s_end = F_getresid("S_END");
 }
 
-void F_loadres (int r, void *p) {
-  WADRES_getdata(r, p);
-}
-
 int F_findres (const char n[8]) {
   return WADRES_find(n);
 }
@@ -91,6 +89,22 @@ void F_getresname (char n[8], int r) {
   WADRES_getname(r, n);
 }
 
+// Get sprite resource id.
+// Sprite name has following format:
+//  (nnnn)('A'+s)('0'+d)[('A'+s)('0'+d)]
+//  Letter means animation frame
+//    A for first, B for second...
+//  Number means direction
+//    0 = front
+//    1 = left
+//    2 = right
+//  Optional part means that this file can be used for differnt frame/direction.
+//  Note that if found FRONT direction for this frame than it UNCONDITIONALLY used.
+//  Note that search performed between markers S_START and S_END in order as paced in wad.
+//  int n[4]  -- sprite name
+//  int s     -- sprite frame
+//  int d     -- sprite direction
+//  char *dir -- out flag "alternative used" (
 int F_getsprid (const char n[4], int s, int d, char *dir) {
   s += 'A';
   d += '0';
@@ -117,6 +131,7 @@ int F_getreslen (int r) {
   return WADRES_getsize(r);
 }
 
+/*
 void F_nextmus (char *s) {
   int i = F_findres(s);
   if (i <= m_start || i >= m_end) {
@@ -145,6 +160,7 @@ void F_randmus (char *s) {
     F_nextmus(s);
   }
 }
+*/
 
 void F_loadmap (char n[8]) {
   int id = F_getresid(n);
@@ -159,3 +175,57 @@ void F_loadmap (char n[8]) {
     ERR_fatal("Failed to load map: resource %.8s not found", n);
   }
 }
+
+static char *getsavfpname (int n, int ro) {
+  static char fn[] = "savgame0.dat";
+  static char p[100];
+  fn[7] = n + '0';
+#ifdef UNIX
+  char *e = getenv("HOME");
+  strncpy(p, e, 60);
+  strcat(p, "/.flatwaifu");
+  if (!ro) {
+    mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+  }
+  strcat(p, "/");
+  strcat(p, fn);
+#else
+  strcpy(p, fn);
+#endif
+  return p;
+}
+
+void F_getsavnames (void) {
+  FILE_Stream rd;
+  for (int i = 0; i < SAVE_MAX; ++i) {
+    savok[i] = 0;
+    char *p = getsavfpname(i, 1);
+    if (FILE_Open(&rd, p, "rb")) {
+      savok[i] = SAVE_getname(&rd.base, savname[i]);
+      FILE_Close(&rd);
+    }
+    if (!savok[i]) {
+      memset(savname[i], 0, 24);
+    } else {
+      savname[i][23] = 0;
+    }
+  }
+}
+
+void F_loadgame (int n) {
+  FILE_Stream rd;
+  char *p = getsavfpname(n, 1);
+  if (FILE_Open(&rd, p, "rb")) {
+    SAVE_load(&rd.base);
+    FILE_Close(&rd);
+  }
+}
+
+void F_savegame (int n, char *s) {
+  FILE_Stream wr;
+  char *p = getsavfpname(n, 0);
+  if (FILE_Open(&wr, p, "wb")) {
+    SAVE_save(&wr.base, s);
+    FILE_Close(&wr);
+  }
+}