DEADSOFTWARE

704ca271b9f3c40f6fae889dec624f7ad6533f64
[flatwaifu.git] / src / files.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #ifdef UNIX
24 # include <sys/stat.h>
25 #endif
26 #include "files.h"
27 #include "map.h"
28 #include "my.h"
29 #include "game.h"
30 #include "view.h"
31 #include "dots.h"
32 #include "smoke.h"
33 #include "fx.h"
34 #include "items.h"
35 #include "monster.h"
36 #include "player.h"
37 #include "switch.h"
38 #include "weapons.h"
39 #include "error.h"
40 #include "cp866.h"
42 #include "common/streams.h"
43 #include "common/files.h"
44 #include "common/wadres.h"
46 int d_start, d_end;
47 static int m_start, m_end;
48 static int s_start, s_end;
50 void F_startup (void) {
51 logo("F_startup: setup file system\n");
52 }
54 void F_addwad (const char *fn) {
55 static int i = 0;
56 static FILE_Reader wadh[MAX_WADS];
57 if (i < MAX_WADS) {
58 if (FILE_OpenReader(&wadh[i], fn)) {
59 if (WADRES_addwad(&wadh[i].base)) {
60 i += 1;
61 } else {
62 ERR_failinit("Invalid WAD %s", fn);
63 }
64 } else {
65 ERR_failinit("Unable to add WAD %s", fn);
66 }
67 } else {
68 ERR_failinit("Too many wads");
69 }
70 }
72 void F_initwads (void) {
73 if (!WADRES_rehash()) {
74 ERR_failinit("F_initwads: failed rehash");
75 }
76 }
78 void F_allocres (void) {
79 d_start = F_getresid("D_START");
80 d_end = F_getresid("D_END");
81 m_start = F_getresid("M_START");
82 m_end = F_getresid("M_END");
83 s_start = F_getresid("S_START");
84 s_end = F_getresid("S_END");
85 }
87 void F_loadres (int r, void *p) {
88 WADRES_getdata(r, p);
89 }
91 int F_findres (const char n[8]) {
92 return WADRES_find(n);
93 }
95 int F_getresid (const char n[8]) {
96 int i = F_findres(n);
97 if (i == -1) {
98 ERR_fatal("F_getresid: resource %.8s not found", n);
99 }
100 return i;
103 void F_getresname (char n[8], int r) {
104 WADRES_getname(r, n);
107 int F_getsprid (const char n[4], int s, int d, char *dir) {
108 s += 'A';
109 d += '0';
110 for (int i = s_start + 1; i < s_end; i++) {
111 char wn[8];
112 byte a, b;
113 WADRES_getname(i, wn);
114 if (cp866_strncasecmp(wn, n, 4) == 0 && (wn[4] == s || wn[6] == s)) {
115 a = wn[4] == s ? wn[5] : 0;
116 b = wn[6] == s ? wn[7] : 0;
117 if (a == '0' || b == '0' || a == d || b == d) {
118 if (dir != NULL) {
119 *dir = (a != '0' && b == '0') || (a != d && b == d);
121 return i;
125 ERR_fatal("F_getsprid: image %.4s%c%c not found", n, s, d);
126 return -1;
129 int F_getreslen (int r) {
130 return WADRES_getsize(r);
133 void F_nextmus (char *s) {
134 int i = F_findres(s);
135 if (i <= m_start || i >= m_end) {
136 i = m_start;
138 for (++i; ; ++i) {
139 if (i >= m_end) {
140 i = m_start + 1;
142 WADRES_getname(i, s);
143 if (cp866_strcasecmp(s, "MENU") == 0 ||
144 cp866_strcasecmp(s, "INTERMUS") == 0 ||
145 cp866_strcasecmp(s, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
146 continue;
148 if (cp866_strncasecmp(s, "DMI", 3) != 0) {
149 break;
154 void F_randmus (char *s) {
155 int i;
156 int n = myrand(10);
157 for (i = 0; i < n; i++) {
158 F_nextmus(s);
162 // reads bytes from file until CR
163 void F_readstr (FILE* h, char *s, int m) {
164 int i = 0;
165 size_t len = 0;
166 static char c = 0;
167 while (i < m) {
168 c = 13;
169 len = myfreadc(&c, 1, 1, h);
170 if (len == 0 || c == 13 || c == 10) {
171 break;
173 s[i] = c;
174 i++;
176 s[i] = 0;
179 void F_loadmap (char n[8]) {
180 int id = F_getresid(n);
181 if (id != -1) {
182 Reader *r = WADRES_getbasereader(id);
183 long offset = WADRES_getoffset(id);
184 r->setpos(r, offset);
185 if (!MAP_load(r)) {
186 ERR_fatal("Failed to load map");
188 } else {
189 ERR_fatal("Failed to load map: resource %.8s not found", n);