DEADSOFTWARE

ppc: fix saves and more map loading improvements
[flatwaifu.git] / src / view.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include <string.h>
25 #include <stdlib.h>
26 #include "vga.h"
27 #include "memory.h"
28 #include "files.h"
29 #include "error.h"
30 #include "view.h"
31 #include "dots.h"
32 #include "smoke.h"
33 #include "weapons.h"
34 #include "items.h"
35 #include "switch.h"
36 #include "fx.h"
37 #include "player.h"
38 #include "monster.h"
39 #include "misc.h"
40 #include "map.h"
41 #include "sound.h"
43 #define ANIT 5
45 /*
46 #define WD 200
47 #define HT 98
49 #define MAXX (FLDW*CELW-WD/2)
50 #define MAXY (FLDH*CELH-HT/2)
51 */
53 int WD; //
54 int HT; //
56 extern map_block_t blk;
58 extern byte clrmap[256*12];
59 void V_remap_rect(int,int,int,int,byte *);
61 byte w_horiz=ON;
62 void *horiz=NULL;//static void *horiz=NULL;
63 int w_o,w_x,w_y,sky_type=1;
64 void *walp[256];
65 dword walf[256];
66 int walh[256];
67 byte walswp[256];
68 byte walani[256];
69 int anih[ANIT][5];
70 byte anic[ANIT];
71 byte fldb[FLDH][FLDW];
72 byte fldf[FLDH][FLDW];
73 byte fld[FLDH][FLDW];
75 extern int lt_time,lt_type,lt_side,lt_ypos;
76 extern void *ltn[2][2];
78 static void getname(int n,char *s) {
79 if(walh[n]==-1) {memset(s,0,8);return;}
80 if(walh[n]==-2) {
81 memcpy(s,"_WATER_",8);s[7]=(byte)walp[n]-1+'0';
82 return;
83 }
84 F_getresname(s,walh[n]&0x7FFF);
85 }
87 static short getani(char *n) {
88 if(strncasecmp(n,"WALL22_1",8)==0) return 1;
89 if(strncasecmp(n,"WALL58_1",8)==0) return 2;
90 if(strncasecmp(n,"W73A_1",8)==0) return 3;
91 if(strncasecmp(n,"RP2_1",8)==0) return 4;
92 return 0;
93 }
95 void W_savegame(FILE* h) {
96 char s[8];
97 int i;
98 myfwrite32(sky_type, h);
99 for(i = 1; i < 256; ++i) {
100 getname(i, s);
101 myfwrite(s, 8, 1, h);
103 for (i = 0; i < 256; i++) {
104 myfwrite32(walf[i], h);
106 for (i = 0; i < 256; i++) {
107 myfwrite8(walswp[i], h);
109 myfwrite(fldb, FLDW*FLDH, 1, h);
110 myfwrite(fld, FLDW*FLDH, 1, h);
111 myfwrite(fldf, FLDW*FLDH, 1, h);
114 void W_loadgame(FILE* h) {
115 char s[8];
116 int i;
117 myfread32(&sky_type, h);
118 for (i = 1; i < 256; ++i) {
119 walani[i]=0;
120 myfread(s, 8, 1, h);
121 if (!s[0]) {
122 walh[i] = -1;
123 walp[i] = NULL;
124 } else {
125 walani[i] = getani(s);
126 if (strncasecmp(s, "_WATER_", 7) == 0) {
127 walh[i] = -2;
128 walp[i] = (void*)(s[7] - '0' + 1);
129 } else {
130 walh[i] = F_getresid(s);
131 walp[i] = V_getvgaimg(walh[i]);
135 for (i = 0; i < 256; i++) {
136 myfread32(&walf[i], h);
137 if (i > 0 && walf[i] & 1) {
138 walh[i] |= 0x8000;
141 for (i = 0; i < 256; i++) {
142 myfread8(&walswp[i], h);
144 myfread(fldb, FLDW*FLDH, 1, h);
145 myfread(fld, FLDW*FLDH, 1, h);
146 myfread(fldf, FLDW*FLDH, 1, h);
147 strcpy(s, "RSKY1");
148 s[4] = '0' + sky_type;
149 M_unlock(horiz);
150 horiz = V_loadvgaimg(s);
153 void W_adjust(void) {
154 int MAXX =(FLDW*CELW-WD/2);//
155 int MAXY =(FLDH*CELH-HT/2);//
157 if(w_x<WD/2) w_x=WD/2;
158 if(w_y<HT/2) w_y=HT/2;
159 if(w_x>MAXX) w_x=MAXX;
160 if(w_y>MAXY) w_y=MAXY;
163 void W_draw(void) {
164 W_adjust();
165 V_setrect(0,WD,w_o+1,HT);
166 if(w_horiz) {
169 //V_pic(127-(word)(w_x-WD/2)*56U/(word)(MAXX-WD/2),w_o+123-(word)(w_y-HT/2)*28U/(word)(MAXY-HT/2),horiz);
170 vgaimg *img = (vgaimg*)horiz;
171 int x=0;
172 int d=0;
173 do {
174 int y=w_o;
175 d &= ~2;
176 do {
177 V_rotspr(x,y,img,d);
178 y+=img->h;
179 d^=2;
180 } while (y<HT+w_o);
181 x+=img->w;
182 d^=1;
183 } while (x<WD);
187 if(sky_type==2) if(lt_time<0) {
188 if(!lt_side) V_spr(0,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
189 else V_spr2(WD-1,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
191 }else V_clr(0,WD,w_o+1,HT,0x97);
192 Z_drawfld((byte *)fldb, 1);
193 DOT_draw();
194 IT_draw();
195 PL_draw(&pl1);
196 if(_2pl) PL_draw(&pl2);
197 MN_draw();
198 WP_draw();
199 SMK_draw();
200 FX_draw();
201 Z_drawfld((byte *)fldf, 0);
202 if(sky_type==2)
203 if(lt_time==-4 || lt_time==-2)
204 V_remap_rect(0,WD,w_o+1,HT,clrmap+256*11);
208 void W_init(void) {
209 int i,j;
210 static char *anm[ANIT-1][5]={
211 {"WALL22_1","WALL23_1","WALL23_2",NULL,NULL},
212 {"WALL58_1","WALL58_2","WALL58_3",NULL,NULL},
213 {"W73A_1","W73A_2",NULL,NULL,NULL},
214 {"RP2_1","RP2_2","RP2_3","RP2_4",NULL}
215 };
217 for(i=1;i<ANIT;++i) {
218 for(j=0;anm[i-1][j];++j)
219 anih[i][j]=F_getresid(anm[i-1][j]);
220 for(;j<5;++j) anih[i][j]=-1;
222 memset(anic,0,sizeof(anic));
223 DOT_init();
224 SMK_init();
225 FX_init();
226 WP_init();
227 IT_init();
228 SW_init();
229 PL_init();
230 MN_init();
231 M_unlock(horiz);
232 horiz=V_loadvgaimg("RSKY1");
234 free_chunks();
237 void W_act(void) {
238 int i,a;
240 if(g_time%3!=0) return;
241 for(i=1;i<256;++i) if((a=walani[i])!=0) {
242 if(anih[a][++anic[a]]==-1) anic[a]=0;
243 walp[i]=V_getvgaimg(anih[a][anic[a]]);
247 /*
248 static void unpack(void *buf,int len,void *obuf) {
249 byte *p,*o;
250 int l,n;
252 for(p=(byte*)buf,o=(byte*)obuf,l=len;l;++p,--l) if(*p==255) {
253 n=*((word*)(++p));memset(o,*(p+=2),n);o+=n;l-=3;
254 }else *(o++)=*p;
256 */
258 static void unpack(void *buf, int len, void *obuf) {
259 int i = 0;
260 int j = 0;
261 unsigned char *p = buf;
262 unsigned char *q = obuf;
263 while (i < len) {
264 int id = p[i];
265 int step = 1;
266 i += 1;
267 if (id == 0xff) {
268 step = p[i] | p[i + 1] << 8;
269 id = p[i + 2];
270 i += 3;
272 memset(&q[j], id, step);
273 j += step;
277 int W_load (FILE *h) {
278 int i, j, k, g;
279 void *p, *buf;
280 wall_t w;
281 switch (blk.t) {
282 case MB_WALLNAMES:
283 for (i = 0; i < 256; ++i) {
284 walh[i] = -1;
285 walswp[i] = i;
286 walani[i] = 0;
288 for (i = 1; i < 256 && blk.sz > 0; ++i, blk.sz -= 9) {
289 myfread(w.n, 8, 1, h);
290 myfread8(&w.t, h);
291 if (strncasecmp(w.n, "_WATER_", 7) == 0) {
292 walp[i] = (void*)(w.n[7] - '0' + 1);
293 walh[i] = -2;
294 } else {
295 walh[i] = F_getresid(w.n);
296 walp[i] = V_getvgaimg(walh[i]);
297 if (w.n[0] == 'S' && w.n[1] == 'W' && w.n[4] == '_') {
298 walswp[i] = 0;
300 walf[i] = w.t ? 1 : 0;
301 if (w.t) {
302 walh[i] |= 0x8000;
304 if (strncasecmp(w.n, "VTRAP01", 8) == 0) {
305 walf[i] |= 2;
307 walani[i] = getani(w.n);
310 for (j = i, i = 1; i < 256 && j < 256; ++i) {
311 if (walswp[i] == 0) {
312 F_getresname(w.n, walh[i] & 0x7FFF);
313 w.n[5] ^= 1;
314 g = F_getresid(w.n) | (walh[i] & 0x8000);
315 k = 1;
316 while (k < 256 && walh[k] != g) {
317 k += 1;
319 if(k >= 256) {
320 k = j;
321 j += 1;
322 walh[k] = g;
323 walp[k] = V_getvgaimg(g);
324 walf[k] = g & 0x8000 ? 1 : 0;
326 walswp[i] = k;
327 walswp[k] = i;
330 return 1;
331 case MB_BACK:
332 p = fldb;
333 goto unp;
334 case MB_WTYPE:
335 p = fld;
336 goto unp;
337 case MB_FRONT:
338 p = fldf;
339 unp:
340 switch (blk.st) {
341 case 0:
342 myfread(p, FLDW * FLDH, 1, h);
343 break;
344 case 1:
345 buf = malloc(blk.sz);
346 if(buf == NULL) {
347 ERR_fatal("Не хватает памяти");
349 myfread(buf, blk.sz, 1, h);
350 unpack(buf, blk.sz, p);
351 free(buf);
352 break;
353 default:
354 return 0;
356 return 1;
357 case MB_SKY:
358 myfread16(&sky_type, h);
359 strcpy(w.n, "RSKY1");
360 w.n[4] = '0' + sky_type;
361 M_unlock(horiz);
362 horiz = V_loadvgaimg(w.n);
363 return 1;
365 return 0;