DEADSOFTWARE

f30b44569d16b3a940904386f4c9ebcc2c1bee71
[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 "vga.h"
26 #include "memory.h"
27 #include "files.h"
28 #include "error.h"
29 #include "view.h"
30 #include "dots.h"
31 #include "smoke.h"
32 #include "weapons.h"
33 #include "items.h"
34 #include "switch.h"
35 #include "fx.h"
36 #include "player.h"
37 #include "monster.h"
38 #include "misc.h"
39 #include "map.h"
40 #include "sound.h"
42 #define ANIT 5
44 /*
45 #define WD 200
46 #define HT 98
48 #define MAXX (FLDW*CELW-WD/2)
49 #define MAXY (FLDH*CELH-HT/2)
50 */
52 int WD; //
53 int HT; //
55 extern map_block_t blk;
57 extern byte clrmap[256*12];
58 void V_remap_rect(int,int,int,int,byte *);
60 byte w_horiz=ON;
61 void *horiz=NULL;//static void *horiz=NULL;
62 int w_o,w_x,w_y,sky_type=1;
63 void *walp[256];
64 dword walf[256];
65 int walh[256];
66 byte walswp[256];
67 byte walani[256];
68 int anih[ANIT][5];
69 byte anic[ANIT];
70 byte fldb[FLDH][FLDW];
71 byte fldf[FLDH][FLDW];
72 byte fld[FLDH][FLDW];
74 extern int lt_time,lt_type,lt_side,lt_ypos;
75 extern void *ltn[2][2];
77 static void getname(int n,char *s) {
78 if(walh[n]==-1) {memset(s,0,8);return;}
79 if(walh[n]==-2) {
80 memcpy(s,"_WATER_",8);s[7]=(byte)walp[n]-1+'0';
81 return;
82 }
83 F_getresname(s,walh[n]&0x7FFF);
84 }
86 static short getani(char *n) {
87 if(strncasecmp(n,"WALL22_1",8)==0) return 1;
88 if(strncasecmp(n,"WALL58_1",8)==0) return 2;
89 if(strncasecmp(n,"W73A_1",8)==0) return 3;
90 if(strncasecmp(n,"RP2_1",8)==0) return 4;
91 return 0;
92 }
94 void W_savegame(FILE* h) {
95 char s[8];
96 int i;
98 myfwrite(&sky_type,1,4,h);
99 for(i=1;i<256;++i) {
100 getname(i,s);myfwrite(s,1,8,h);
102 myfwrite(walf,1,sizeof(walf),h);
103 myfwrite(walswp,1,sizeof(walswp),h);
104 myfwrite(fldb,1,FLDW*FLDH,h);
105 myfwrite(fld,1,FLDW*FLDH,h);
106 myfwrite(fldf,1,FLDW*FLDH,h);
109 void W_loadgame(FILE* h) {
110 char s[8];
111 int i;
113 myfread(&sky_type,1,4,h);
114 for(i=1;i<256;++i) {
115 walani[i]=0;
116 myfread(s,1,8,h);if(!s[0]) {walh[i]=-1;walp[i]=NULL;continue;}
117 walani[i]=getani(s);
118 if(strncasecmp(s,"_WATER_",7)==0) { //if(memicmp(s,"_WATER_",7)==0) {
119 walh[i]=-2;walp[i]=(void*)(s[7]-'0'+1);
120 }else walp[i]=M_lock(walh[i]=F_getresid(s));
122 myfread(walf,1,sizeof(walf),h);
123 for(i=1;i<256;++i) if(walf[i]&1) walh[i]|=0x8000;
124 myfread(walswp,1,sizeof(walswp),h);
125 myfread(fldb,1,FLDW*FLDH,h);
126 myfread(fld,1,FLDW*FLDH,h);
127 myfread(fldf,1,FLDW*FLDH,h);
128 strcpy(s,"RSKY1");s[4]=sky_type+'0';
129 M_unlock(horiz);
130 horiz=M_lock(F_getresid(s));
133 void W_adjust(void) {
134 int MAXX =(FLDW*CELW-WD/2);//
135 int MAXY =(FLDH*CELH-HT/2);//
137 if(w_x<WD/2) w_x=WD/2;
138 if(w_y<HT/2) w_y=HT/2;
139 if(w_x>MAXX) w_x=MAXX;
140 if(w_y>MAXY) w_y=MAXY;
143 void W_draw(void) {
144 W_adjust();
145 V_setrect(0,WD,w_o+1,HT);
146 if(w_horiz) {
149 //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);
150 vgaimg *img = (vgaimg*)horiz;
151 int x=0;
152 int d=0;
153 do {
154 int y=w_o;
155 d &= ~2;
156 do {
157 V_rotspr(x,y,img,d);
158 y+=img->h;
159 d^=2;
160 } while (y<HT+w_o);
161 x+=img->w;
162 d^=1;
163 } while (x<WD);
167 if(sky_type==2) if(lt_time<0) {
168 if(!lt_side) V_spr(0,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
169 else V_spr2(WD-1,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
171 }else V_clr(0,WD,w_o+1,HT,0x97);
172 Z_drawfld((byte *)fldb, 1);
173 DOT_draw();
174 IT_draw();
175 PL_draw(&pl1);
176 if(_2pl) PL_draw(&pl2);
177 MN_draw();
178 WP_draw();
179 SMK_draw();
180 FX_draw();
181 Z_drawfld((byte *)fldf, 0);
182 if(sky_type==2)
183 if(lt_time==-4 || lt_time==-2)
184 V_remap_rect(0,WD,w_o+1,HT,clrmap+256*11);
188 void W_init(void) {
189 int i,j;
190 static char *anm[ANIT-1][5]={
191 {"WALL22_1","WALL23_1","WALL23_2",NULL,NULL},
192 {"WALL58_1","WALL58_2","WALL58_3",NULL,NULL},
193 {"W73A_1","W73A_2",NULL,NULL,NULL},
194 {"RP2_1","RP2_2","RP2_3","RP2_4",NULL}
195 };
197 for(i=1;i<ANIT;++i) {
198 for(j=0;anm[i-1][j];++j)
199 anih[i][j]=F_getresid(anm[i-1][j]);
200 for(;j<5;++j) anih[i][j]=-1;
202 memset(anic,0,sizeof(anic));
203 DOT_init();
204 SMK_init();
205 FX_init();
206 WP_init();
207 IT_init();
208 SW_init();
209 PL_init();
210 MN_init();
211 M_unlock(horiz);
212 horiz=M_lock(F_getresid("RSKY1"));
214 free_chunks();
217 void W_act(void) {
218 int i,a;
220 if(g_time%3!=0) return;
221 for(i=1;i<256;++i) if((a=walani[i])!=0) {
222 if(anih[a][++anic[a]]==-1) anic[a]=0;
223 walp[i]=M_lock(anih[a][anic[a]]);
227 static void unpack(void *buf,int len,void *obuf) {
228 byte *p,*o;
229 int l,n;
231 for(p=(byte*)buf,o=(byte*)obuf,l=len;l;++p,--l) if(*p==255) {
232 n=*((word*)(++p));memset(o,*(p+=2),n);o+=n;l-=3;
233 }else *(o++)=*p;
236 int W_load(FILE* h) {
237 int i,j,k,g;
238 static wall_t w;
239 void *p,*buf;
241 switch(blk.t) {
242 case MB_WALLNAMES:
243 for(i=0;i<256;++i) {walh[i]=-1;walswp[i]=i;walani[i]=0;}
244 for(i=1;i<256 && blk.sz>0;++i,blk.sz-=sizeof(w)) {
245 myfread(&w,1,sizeof(w),h);
247 if(strncasecmp(w.n,"_WATER_",7)==0) //if(memicmp(w.n,"_WATER_",7)==0)
248 {walp[i]=(void*)(w.n[7]-'0'+1);walh[i]=-2;continue;}
249 walp[i]=M_lock(walh[i]=F_getresid(w.n));
250 if(w.n[0]=='S' && w.n[1]=='W' && w.n[4]=='_') walswp[i]=0;
251 walf[i]=(w.t)?1:0;if(w.t) walh[i]|=0x8000;
253 if(strncasecmp(w.n,"VTRAP01",8)==0) walf[i]|=2; //if(memicmp(w.n,"VTRAP01",8)==0) walf[i]|=2;
254 walani[i]=getani(w.n);
256 for(j=i,i=1;i<256;++i) if(walswp[i]==0) {
257 if(j>=256) break;
258 F_getresname(w.n,walh[i]&0x7FFF);
259 w.n[5]^=1;
260 g=F_getresid(w.n)|(walh[i]&0x8000);
261 for(k=1;k<256;++k) if(walh[k]==g) break;
262 if(k>=256) {
263 walh[k=j++]=g;walp[k]=M_lock(g);
264 walf[k]=(g&0x8000)?1:0;
266 walswp[i]=k;walswp[k]=i;
268 return 1;
269 case MB_BACK: p=fldb;goto unp;
270 case MB_WTYPE: p=fld;goto unp;
271 case MB_FRONT: p=fldf;
272 unp: switch(blk.st) {
273 case 0: myfread(p,1,FLDW*FLDH,h);break;
274 case 1:
275 if(!(buf=malloc(blk.sz)))
276 ERR_fatal("Не хватает памяти");
277 myfread(buf,1,blk.sz,h);
278 unpack(buf,blk.sz,p);free(buf);break;
279 default: return 0;
280 }return 1;
281 case MB_SKY:
282 sky_type=0;myfread(&sky_type,1,2,h);
283 strcpy(w.n,"RSKY1");w.n[4]=sky_type+'0';
284 M_unlock(horiz);
285 horiz=M_lock(F_getresid(w.n));
286 return 1;
287 }return 0;