DEADSOFTWARE

portability: use only english strings for debug
[flatwaifu.git] / src / files.c
index f7037cf435be55a4cee9f25d344a48ee0389f3ef..e470830be662a62216d3daac74cda57eec157d4a 100644 (file)
@@ -1,30 +1,27 @@
-/*
-   Copyright (C) Prikol Software 1996-1997
-   Copyright (C) Aleksey Volynskov 1996-1997
-   Copyright (C) <ARembo@gmail.com> 2011
-
-   This file is part of the Doom2D:Rembo project.
-
-   Doom2D:Rembo is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License version 2 as
-   published by the Free Software Foundation.
-
-   Doom2D:Rembo is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, see <http://www.gnu.org/licenses/> or
-   write to the Free Software Foundation, Inc.,
-   51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
-*/
+/* Copyright (C) 1996-1997 Aleksey Volynskov
+ * Copyright (C) 2011 Rambo
+ * Copyright (C) 2020 SovietPony
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License ONLY.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 #include "glob.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/stat.h>
+#ifdef UNIX
+#  include <sys/stat.h>
+#endif
 #include "files.h"
 #include "map.h"
 #include "my.h"
 #include "switch.h"
 #include "weapons.h"
 #include "error.h"
+#include "cp866.h"
 
 typedef struct {
   byte n, i, v, d;
 } dmv;
 
 int d_start, d_end;
-byte savname[7][24];
-byte savok[7];
 mwad_t wad[MAX_WAD];
-map_block_t blk;
+char wads[MAX_WADS][__MAX_PATH];
+FILE* wadh[MAX_WADS];
 
 static byte seq[255];
 static byte seqn;
@@ -61,107 +58,23 @@ static int m_start, m_end;
 static int s_start, s_end;
 static int wad_num;
 
-static char wads[MAX_WADS][__MAX_PATH];
-static FILE* wadh[MAX_WADS];
-
 static char f_drive[__MAX_DRIVE];
 static char f_dir[__MAX_DIR];
 static char f_name[__MAX_FNAME];
 static char f_ext[__MAX_EXT];
 
 void F_startup (void) {
-  logo("F_startup: настройка файловой системы\n");
+  logo("F_startup: setup file system\n");
   memset(wads,0,sizeof(wads));
 }
 
-static char *getsavfpname (int n, int ro) {
-  static char fn[]="savgame0.dat";
-  fn[7]=n+'0';
-#ifndef WIN32
-  static char p[100];
-  char *e = getenv("HOME");
-  strncpy(p,e,60);
-  strcat(p,"/.doom2d-rembo");
-  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) {
-  int i;
-  FILE *h;
-  short ver;
-  char *p;
-  for (i = 0; i < 7; ++i) {
-    p = getsavfpname(i, 1);
-    memset(savname[i], 0, 24);
-    savok[i] = 0;
-    h = fopen(p, "rb");
-    if (h != NULL) {
-      ver = -1;
-      myfread(savname[i], 24, 1, h);
-      ver = myfread16(h);
-      savname[i][23] = 0;
-      savok[i] = (ver == 3) ? 1 : 0;
-      fclose(h);
-    }
-  }
-}
-
-void F_savegame (int n, char *s) {
-  char *p = getsavfpname(n, 0);
-  FILE *h = fopen(p, "wb");
-  if (h != NULL) {
-    myfwrite(s, 24, 1, h); // slot name
-    myfwrite16(3, h); // version
-    G_savegame(h);
-    W_savegame(h);
-    DOT_savegame(h);
-    SMK_savegame(h);
-    FX_savegame(h);
-    IT_savegame(h);
-    MN_savegame(h);
-    PL_savegame(h);
-    SW_savegame(h);
-    WP_savegame(h);
-    fclose(h);
-  }
-}
-
-void F_loadgame (int n) {
-  short ver;
-  char *p = getsavfpname(n, 1);
-  FILE *h = fopen(p, "rb");
-  if (h != NULL) {
-    fseek(h, 24, SEEK_SET); // skip name
-    ver = myfread16(h); // version
-    if (ver == 3) {
-      G_loadgame(h);
-      W_loadgame(h);
-      DOT_loadgame(h);
-      SMK_loadgame(h);
-      FX_loadgame(h);
-      IT_loadgame(h);
-      MN_loadgame(h);
-      PL_loadgame(h);
-      SW_loadgame(h);
-      WP_loadgame(h);
-    }
-    fclose(h);
-  }
-}
-
 void F_addwad (const char *fn) {
   int i;
 
   for(i=0;i<MAX_WADS;++i) if(wads[i][0]==0) {
     strcpy(wads[i],fn);return;
   }
-  ERR_failinit("Не могу добавить WAD %s",fn);
+  ERR_failinit("Unable to add WAD %s",fn);
 }
 
 static int myfilelength (FILE *h) {
@@ -180,20 +93,20 @@ void F_initwads (void) {
   int n, o;
   wad_t w;
 
-  logo("F_initwads: подключение WAD-файлов\n");
+  logo("F_initwads: use WAD-files\n");
   for (i = 0; i < MAX_WAD; ++i) {
     wad[i].n[0] = 0;
   }
 
-  logo("   подключается  %s\n", wads[0]);
+  logo("   add %s\n", wads[0]);
   if ((wadh[0] = h = fopen(wads[0], "rb")) == NULL) {
-    ERR_failinit("Не могу открыть файл: %s", wads[0]);
+    ERR_failinit("Unable to open file: %s", wads[0]);
   }
 
   s[0] = '\0';
   myfread(s, 1, 4, h);
   if (strncmp(s, "IWAD", 4) != 0 && strncmp(s, "PWAD", 4) != 0) {
-    ERR_failinit("Нет подписи IWAD или PWAD (1)");
+    ERR_failinit("Not IWAD or PWAD (1)");
   }
 
   p = 0; // wad number
@@ -205,7 +118,7 @@ void F_initwads (void) {
     w.l = myfread32(h); // len
     myfread(w.n, 1, 8, h); // name
     if (p >= MAX_WAD) {
-      ERR_failinit("Слишком много элементов WAD'а");
+      ERR_failinit("Too many entries in the WAD");
     }
     wad[p].o = w.o;
     wad[p].l = w.l;
@@ -218,14 +131,14 @@ void F_initwads (void) {
 
   for (i = 1; i < MAX_WADS; ++i) {
     if (wads[i][0] != 0) {
-      logo("  подключается %s\n", wads[i]);
+      logo("  add %s\n", wads[i]);
       if ((wadh[i] = h = fopen(wads[i], "rb")) == NULL) {
-        ERR_failinit("Не могу открыть файл2:  %s", wads[i]);
+        ERR_failinit("Unable to open file (2):  %s", wads[i]);
       }
       mysplitpath(wads[i], f_drive, f_dir, f_name, f_ext);
-      if (strcasecmp(f_ext, ".lmp") == 0) {
+      if (cp866_strcasecmp(f_ext, ".lmp") == 0) {
         for (k = 0; k < MAX_WAD; ++k) {
-          if (strncasecmp(wad[k].n, f_name, 8) == 0) {
+          if (cp866_strncasecmp(wad[k].n, f_name, 8) == 0) {
             wad[k].o = 0;
             wad[k].l = myfilelength(h);
             wad[k].f = i;
@@ -233,7 +146,7 @@ void F_initwads (void) {
           }
           if (k >= MAX_WAD) {
             if (p >= MAX_WAD) {
-              ERR_failinit("Слишком много элементов WAD'а");
+              ERR_failinit("Too many entries in the WAD");
             }
             memset(wad[p].n, 0, 8);
             strncpy(wad[p].n, f_name, 8);
@@ -247,7 +160,7 @@ void F_initwads (void) {
         s[0] = '\0';
         myfread(s, 1, 4, h);
         if (strncmp(s, "IWAD", 4) != 0 && strncmp(s, "PWAD", 4) != 0) {
-          ERR_failinit("Нет подписи IWAD или PWAD (2)");
+          ERR_failinit("Not IWAD or PWAD (2)");
         }
         n = myfread32(h); // num
         o = myfread32(h); // offset
@@ -257,7 +170,7 @@ void F_initwads (void) {
           w.o = myfread32(h); // len
           myfread(w.n, 1, 8, h); // name
           for (k = 0; k < MAX_WAD; ++k) {
-            if (strncasecmp(wad[k].n, w.n, 8) == 0) {
+            if (cp866_strncasecmp(wad[k].n, w.n, 8) == 0) {
               wad[k].o = w.o;
               wad[k].l = w.l;
               wad[k].f = i;
@@ -265,7 +178,7 @@ void F_initwads (void) {
             }
             if (k >= MAX_WAD) {
               if (p >= MAX_WAD) {
-                ERR_failinit("Слишком много элементов WAD'а");
+                ERR_failinit("Too many entries in the WAD");
               }
               memcpy(wad[p].n, w.n, 8);
               wad[p].o = w.o;
@@ -303,10 +216,10 @@ void F_loadres (int r, void *p, dword o, dword l) {
   oo=ftell(fh=wadh[wad[r].f]);
 
   if(fseek(fh,wad[r].o+o,SEEK_SET)!=0)
-    ERR_fatal("Ошибка при чтении файла");
+    ERR_fatal("Error while file reading");
 
   if((dword)myfreadc(p,1,l,fh)!=l)
-    ERR_fatal("Ошибка при загрузке ресурса %.8s",wad[r].n);
+    ERR_fatal("Error while loading resource %.8s", wad[r].n);
   
   fseek(fh,oo,SEEK_SET);
   
@@ -318,7 +231,7 @@ void F_saveres(int r, void *p, dword o, dword l) {
   FILE* fh = wadh[wad[r].f];
   int oo = ftell(fh);
   if (fseek(fh, wad[r].o + o, SEEK_SET) != 0) {
-    ERR_fatal("Ошибка при чтении файла");
+    ERR_fatal("Error while reading file");
   }
   myfwrite(p, l, 1, fh);
   fseek(fh, oo, SEEK_SET);
@@ -329,7 +242,7 @@ void F_saveres(int r, void *p, dword o, dword l) {
 int F_findres (const char n[8]) {
   int i;
   for (i = 0; i < wad_num; i++) {
-    if (strncasecmp(wad[i].n, n, 8) == 0) {
+    if (cp866_strncasecmp(wad[i].n, n, 8) == 0) {
       return i;
     }
   }
@@ -340,7 +253,7 @@ int F_findres (const char n[8]) {
 int F_getresid (const char n[8]) {
   int i = F_findres(n);
   if (i == -1) {
-    ERR_fatal("F_getresid: ресурс %.8s не найден", n);
+    ERR_fatal("F_getresid: resource %.8s not found", n);
   }
   return i;
 }
@@ -350,21 +263,24 @@ void F_getresname (char n[8], int r) {
 }
 
 // get sprite id
-int F_getsprid (const char n[4], int s, int d) {
+int F_getsprid (const char n[4], int s, int d, char *dir) {
   int i;
-  byte a,b;
-
-  s+='A';d+='0';
-  for(i=s_start+1;i<s_end;++i)
-    if(strncasecmp(wad[i].n,n,4)==0 && (wad[i].n[4]==s || wad[i].n[6]==s)) {
-      if(wad[i].n[4]==s) a=wad[i].n[5]; else a=0;
-      if(wad[i].n[6]==s) b=wad[i].n[7]; else b=0;
-      if(a=='0') return i;
-      if(b=='0') return(i|0x8000);
-      if(a==d) return i;
-      if(b==d) return(i|0x8000);
+  byte a, b;
+  s += 'A';
+  d += '0';
+  for (i = s_start + 1; i < s_end; i++) {
+    if (cp866_strncasecmp(wad[i].n, n, 4) == 0 && (wad[i].n[4] == s || wad[i].n[6] == s)) {
+      a = wad[i].n[4] == s ? wad[i].n[5] : 0;
+      b = wad[i].n[6] == s ? wad[i].n[7] : 0;
+      if (a == '0' || b == '0' || a == d || b == d) {
+        if (dir != NULL) {
+          *dir = a != '0' && b == '0' || a != d && b == d;
+        }
+        return i;
+      }
     }
-  ERR_fatal("F_getsprid: изображение %.4s%c%c не найдено",n,(byte)s,(byte)d);
+  }
+  ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
   return -1;
 }
 
@@ -379,12 +295,12 @@ void F_nextmus (char *s) {
   for(++i;;++i) {
     if(i>=m_end) i=m_start+1;
 
-    if (strcasecmp(wad[i].n,"MENU") == 0 ||
-        strcasecmp(wad[i].n,"INTERMUS") == 0 ||
-        strcasecmp(wad[i].n,"\x8a\x8e\x8d\x85\x96\x0") == 0)
+    if (cp866_strcasecmp(wad[i].n,"MENU") == 0 ||
+        cp866_strcasecmp(wad[i].n,"INTERMUS") == 0 ||
+        cp866_strcasecmp(wad[i].n,"\x8a\x8e\x8d\x85\x96\x0") == 0)
         continue;
 
-    if(strncasecmp(wad[i].n,"DMI",3)!=0) break;
+    if(cp866_strncasecmp(wad[i].n,"DMI",3)!=0) break;
   }
   memcpy(s,wad[i].n,8);
 }
@@ -434,44 +350,6 @@ void F_readstrz (FILE* h,char *s,int m) {
 }
 */
 
-void F_loadmap (char n[8]) {
-  int r, o;
-  FILE *h;
-  map_header_t hdr;
-  W_init();
-  r = F_getresid(n);
-  h = wadh[wad[r].f];
-  fseek(h, wad[r].o, SEEK_SET);
-  myfread(hdr.id, 8, 1, h);
-  hdr.ver = myfread16(h);
-  if (memcmp(hdr.id, "Doom2D\x1A", 8) != 0) {
-    ERR_fatal("%.8s не является уровнем", n);
-  }
-  for(;;) {
-    blk.t = myfread16(h);
-    blk.st = myfread16(h);
-    blk.sz = myfread32(h);
-    if(blk.t == MB_END) {
-      break;
-    }
-    if(blk.t == MB_COMMENT) {
-      fseek(h, blk.sz, SEEK_CUR);
-      continue;
-    }
-    o = ftell(h) + blk.sz;
-    if(!G_load(h)) {
-      if(!W_load(h)) {
-        if(!IT_load(h)) {
-          if(!SW_load(h)) {
-            ERR_fatal("Неизвестный блок %d(%d) в уровне %.8s", blk.t, blk.st, n);
-          }
-        }
-      }
-    }
-    fseek(h, o, SEEK_SET);
-  }
-}
-
 /*void F_freemus(void) {
 
   int i;