1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
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.
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.
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/>.
26 #include "map.h" // MAP_load
27 #include "save.h" // SAVE_getname
30 # include <sys/stat.h>
33 #include "common/streams.h"
34 #include "common/files.h"
35 #include "common/wadres.h"
36 #include "common/cp866.h"
40 char savname
[SAVE_MAX
][SAVE_MAXLEN
];
43 static int m_start
, m_end
;
44 static int s_start
, s_end
;
46 void F_startup (void) {
47 logo("F_startup: setup file system\n");
50 void F_addwad (const char *fn
) {
52 static FILE_Stream wadh
[MAX_WADS
];
54 if (FILE_Open(&wadh
[i
], fn
, "rb")) {
55 if (WADRES_addwad(&wadh
[i
].base
)) {
58 ERR_failinit("Invalid WAD %s", fn
);
61 ERR_failinit("Unable to add WAD %s", fn
);
64 ERR_failinit("Too many wads");
68 void F_initwads (void) {
69 if (!WADRES_rehash()) {
70 ERR_failinit("F_initwads: failed rehash");
74 void F_allocres (void) {
75 d_start
= F_getresid("D_START");
76 d_end
= F_getresid("D_END");
77 m_start
= F_getresid("M_START");
78 m_end
= F_getresid("M_END");
79 s_start
= F_getresid("S_START");
80 s_end
= F_getresid("S_END");
83 void F_loadres (int r
, void *p
) {
87 int F_findres (const char n
[8]) {
88 return WADRES_find(n
);
91 int F_getresid (const char n
[8]) {
94 ERR_fatal("F_getresid: resource %.8s not found", n
);
99 void F_getresname (char n
[8], int r
) {
100 WADRES_getname(r
, n
);
103 int F_getsprid (const char n
[4], int s
, int d
, char *dir
) {
106 for (int i
= s_start
+ 1; i
< s_end
; i
++) {
109 WADRES_getname(i
, wn
);
110 if (cp866_strncasecmp(wn
, n
, 4) == 0 && (wn
[4] == s
|| wn
[6] == s
)) {
111 a
= wn
[4] == s
? wn
[5] : 0;
112 b
= wn
[6] == s
? wn
[7] : 0;
113 if (a
== '0' || b
== '0' || a
== d
|| b
== d
) {
115 *dir
= (a
!= '0' && b
== '0') || (a
!= d
&& b
== d
);
121 ERR_fatal("F_getsprid: image %.4s%c%c not found", n
, s
, d
);
125 int F_getreslen (int r
) {
126 return WADRES_getsize(r
);
129 void F_nextmus (char *s
) {
130 int i
= F_findres(s
);
131 if (i
<= m_start
|| i
>= m_end
) {
138 WADRES_getname(i
, s
);
139 if (cp866_strcasecmp(s
, "MENU") == 0 ||
140 cp866_strcasecmp(s
, "INTERMUS") == 0 ||
141 cp866_strcasecmp(s
, "\x8a\x8e\x8d\x85\x96\x0") == 0) {
144 if (cp866_strncasecmp(s
, "DMI", 3) != 0) {
150 void F_randmus (char *s
) {
153 for (i
= 0; i
< n
; i
++) {
158 void F_loadmap (char n
[8]) {
159 int id
= F_getresid(n
);
161 Stream
*r
= WADRES_getbasereader(id
);
162 long offset
= WADRES_getoffset(id
);
163 stream_setpos(r
, offset
);
165 ERR_fatal("Failed to load map");
168 ERR_fatal("Failed to load map: resource %.8s not found", n
);
172 static char *getsavfpname (int n
, int ro
) {
173 static char fn
[] = "savgame0.dat";
177 char *e
= getenv("HOME");
179 strcat(p
, "/.flatwaifu");
181 mkdir(p
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
191 void F_getsavnames (void) {
193 for (int i
= 0; i
< SAVE_MAX
; ++i
) {
195 char *p
= getsavfpname(i
, 1);
196 if (FILE_Open(&rd
, p
, "rb")) {
197 savok
[i
] = SAVE_getname(&rd
.base
, savname
[i
]);
201 memset(savname
[i
], 0, 24);
208 void F_loadgame (int n
) {
210 char *p
= getsavfpname(n
, 1);
211 if (FILE_Open(&rd
, p
, "rb")) {
217 void F_savegame (int n
, char *s
) {
219 char *p
= getsavfpname(n
, 0);
220 if (FILE_Open(&wr
, p
, "wb")) {
221 SAVE_save(&wr
.base
, s
);