DEADSOFTWARE

3bf36772cd60cec019f9d89528746c84613d03c6
[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;
99 myfwrite(&sky_type,1,4,h);
100 for(i=1;i<256;++i) {
101 getname(i,s);myfwrite(s,1,8,h);
103 myfwrite(walf,1,sizeof(walf),h);
104 myfwrite(walswp,1,sizeof(walswp),h);
105 myfwrite(fldb,1,FLDW*FLDH,h);
106 myfwrite(fld,1,FLDW*FLDH,h);
107 myfwrite(fldf,1,FLDW*FLDH,h);
110 void W_loadgame(FILE* h) {
111 char s[8];
112 int i;
114 myfread(&sky_type,1,4,h);
115 for(i=1;i<256;++i) {
116 walani[i]=0;
117 myfread(s,1,8,h);if(!s[0]) {walh[i]=-1;walp[i]=NULL;continue;}
118 walani[i]=getani(s);
119 if(strncasecmp(s,"_WATER_",7)==0) { //if(memicmp(s,"_WATER_",7)==0) {
120 walh[i]=-2;walp[i]=(void*)(s[7]-'0'+1);
121 } else {
122 walh[i]=F_getresid(s);
123 walp[i]=V_getvgaimg(walh[i]);
126 myfread(walf,1,sizeof(walf),h);
127 for(i=1;i<256;++i) if(walf[i]&1) walh[i]|=0x8000;
128 myfread(walswp,1,sizeof(walswp),h);
129 myfread(fldb,1,FLDW*FLDH,h);
130 myfread(fld,1,FLDW*FLDH,h);
131 myfread(fldf,1,FLDW*FLDH,h);
132 strcpy(s,"RSKY1");s[4]=sky_type+'0';
133 M_unlock(horiz);
134 horiz=V_loadvgaimg(s);
137 void W_adjust(void) {
138 int MAXX =(FLDW*CELW-WD/2);//
139 int MAXY =(FLDH*CELH-HT/2);//
141 if(w_x<WD/2) w_x=WD/2;
142 if(w_y<HT/2) w_y=HT/2;
143 if(w_x>MAXX) w_x=MAXX;
144 if(w_y>MAXY) w_y=MAXY;
147 void W_draw(void) {
148 W_adjust();
149 V_setrect(0,WD,w_o+1,HT);
150 if(w_horiz) {
153 //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);
154 vgaimg *img = (vgaimg*)horiz;
155 int x=0;
156 int d=0;
157 do {
158 int y=w_o;
159 d &= ~2;
160 do {
161 V_rotspr(x,y,img,d);
162 y+=img->h;
163 d^=2;
164 } while (y<HT+w_o);
165 x+=img->w;
166 d^=1;
167 } while (x<WD);
171 if(sky_type==2) if(lt_time<0) {
172 if(!lt_side) V_spr(0,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
173 else V_spr2(WD-1,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
175 }else V_clr(0,WD,w_o+1,HT,0x97);
176 Z_drawfld((byte *)fldb, 1);
177 DOT_draw();
178 IT_draw();
179 PL_draw(&pl1);
180 if(_2pl) PL_draw(&pl2);
181 MN_draw();
182 WP_draw();
183 SMK_draw();
184 FX_draw();
185 Z_drawfld((byte *)fldf, 0);
186 if(sky_type==2)
187 if(lt_time==-4 || lt_time==-2)
188 V_remap_rect(0,WD,w_o+1,HT,clrmap+256*11);
192 void W_init(void) {
193 int i,j;
194 static char *anm[ANIT-1][5]={
195 {"WALL22_1","WALL23_1","WALL23_2",NULL,NULL},
196 {"WALL58_1","WALL58_2","WALL58_3",NULL,NULL},
197 {"W73A_1","W73A_2",NULL,NULL,NULL},
198 {"RP2_1","RP2_2","RP2_3","RP2_4",NULL}
199 };
201 for(i=1;i<ANIT;++i) {
202 for(j=0;anm[i-1][j];++j)
203 anih[i][j]=F_getresid(anm[i-1][j]);
204 for(;j<5;++j) anih[i][j]=-1;
206 memset(anic,0,sizeof(anic));
207 DOT_init();
208 SMK_init();
209 FX_init();
210 WP_init();
211 IT_init();
212 SW_init();
213 PL_init();
214 MN_init();
215 M_unlock(horiz);
216 horiz=V_loadvgaimg("RSKY1");
218 free_chunks();
221 void W_act(void) {
222 int i,a;
224 if(g_time%3!=0) return;
225 for(i=1;i<256;++i) if((a=walani[i])!=0) {
226 if(anih[a][++anic[a]]==-1) anic[a]=0;
227 walp[i]=V_getvgaimg(anih[a][anic[a]]);
231 /*
232 static void unpack(void *buf,int len,void *obuf) {
233 byte *p,*o;
234 int l,n;
236 for(p=(byte*)buf,o=(byte*)obuf,l=len;l;++p,--l) if(*p==255) {
237 n=*((word*)(++p));memset(o,*(p+=2),n);o+=n;l-=3;
238 }else *(o++)=*p;
240 */
242 static void unpack(void *buf, int len, void *obuf) {
243 int i = 0;
244 int j = 0;
245 unsigned char *p = buf;
246 unsigned char *q = obuf;
247 while (i < len) {
248 int id = p[i];
249 int step = 1;
250 i += 1;
251 if (id == 0xff) {
252 step = p[i] | p[i + 1] << 8;
253 id = p[i + 2];
254 i += 3;
256 memset(&q[j], id, step);
257 j += step;
261 int W_load(FILE* h) {
262 int i,j,k,g;
263 static wall_t w;
264 void *p,*buf;
266 switch(blk.t) {
267 case MB_WALLNAMES:
268 for(i=0;i<256;++i) {walh[i]=-1;walswp[i]=i;walani[i]=0;}
269 for(i=1;i<256 && blk.sz>0;++i,blk.sz-=sizeof(w)) {
270 myfread(&w,1,sizeof(w),h);
272 if(strncasecmp(w.n,"_WATER_",7)==0) //if(memicmp(w.n,"_WATER_",7)==0)
273 {walp[i]=(void*)(w.n[7]-'0'+1);walh[i]=-2;continue;}
275 walh[i]=F_getresid(w.n);
276 walp[i]=V_getvgaimg(walh[i]);
277 if(w.n[0]=='S' && w.n[1]=='W' && w.n[4]=='_') walswp[i]=0;
278 walf[i]=(w.t)?1:0;if(w.t) walh[i]|=0x8000;
280 if(strncasecmp(w.n,"VTRAP01",8)==0) walf[i]|=2; //if(memicmp(w.n,"VTRAP01",8)==0) walf[i]|=2;
281 walani[i]=getani(w.n);
283 for(j=i,i=1;i<256;++i) if(walswp[i]==0) {
284 if(j>=256) break;
285 F_getresname(w.n,walh[i]&0x7FFF);
286 w.n[5]^=1;
287 g=F_getresid(w.n)|(walh[i]&0x8000);
288 for(k=1;k<256;++k) if(walh[k]==g) break;
289 if(k>=256) {
290 walh[k=j++]=g;walp[k]=V_getvgaimg(g);
291 walf[k]=(g&0x8000)?1:0;
293 walswp[i]=k;walswp[k]=i;
295 return 1;
296 case MB_BACK: p=fldb;goto unp;
297 case MB_WTYPE: p=fld;goto unp;
298 case MB_FRONT: p=fldf;
299 unp: switch(blk.st) {
300 case 0: myfread(p,1,FLDW*FLDH,h);break;
301 case 1:
302 if(!(buf=malloc(blk.sz)))
303 ERR_fatal("Не хватает памяти");
304 myfread(buf,1,blk.sz,h);
305 unpack(buf,blk.sz,p);free(buf);break;
306 default: return 0;
307 }return 1;
308 case MB_SKY:
309 sky_type=0;myfread(&sky_type,2,1,h);
310 sky_type = short2host(sky_type);
311 strcpy(w.n,"RSKY1");w.n[4]=sky_type+'0';
312 M_unlock(horiz);
313 horiz=V_loadvgaimg(w.n);
314 return 1;
315 }return 0;