DEADSOFTWARE

Add missed stdlib include
[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 walp[i]=M_lock(walh[i]=F_getresid(s));
123 myfread(walf,1,sizeof(walf),h);
124 for(i=1;i<256;++i) if(walf[i]&1) walh[i]|=0x8000;
125 myfread(walswp,1,sizeof(walswp),h);
126 myfread(fldb,1,FLDW*FLDH,h);
127 myfread(fld,1,FLDW*FLDH,h);
128 myfread(fldf,1,FLDW*FLDH,h);
129 strcpy(s,"RSKY1");s[4]=sky_type+'0';
130 M_unlock(horiz);
131 horiz=M_lock(F_getresid(s));
134 void W_adjust(void) {
135 int MAXX =(FLDW*CELW-WD/2);//
136 int MAXY =(FLDH*CELH-HT/2);//
138 if(w_x<WD/2) w_x=WD/2;
139 if(w_y<HT/2) w_y=HT/2;
140 if(w_x>MAXX) w_x=MAXX;
141 if(w_y>MAXY) w_y=MAXY;
144 void W_draw(void) {
145 W_adjust();
146 V_setrect(0,WD,w_o+1,HT);
147 if(w_horiz) {
150 //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);
151 vgaimg *img = (vgaimg*)horiz;
152 int x=0;
153 int d=0;
154 do {
155 int y=w_o;
156 d &= ~2;
157 do {
158 V_rotspr(x,y,img,d);
159 y+=img->h;
160 d^=2;
161 } while (y<HT+w_o);
162 x+=img->w;
163 d^=1;
164 } while (x<WD);
168 if(sky_type==2) if(lt_time<0) {
169 if(!lt_side) V_spr(0,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
170 else V_spr2(WD-1,w_o+lt_ypos,ltn[lt_type][(lt_time<-5)?0:1]);
172 }else V_clr(0,WD,w_o+1,HT,0x97);
173 Z_drawfld((byte *)fldb, 1);
174 DOT_draw();
175 IT_draw();
176 PL_draw(&pl1);
177 if(_2pl) PL_draw(&pl2);
178 MN_draw();
179 WP_draw();
180 SMK_draw();
181 FX_draw();
182 Z_drawfld((byte *)fldf, 0);
183 if(sky_type==2)
184 if(lt_time==-4 || lt_time==-2)
185 V_remap_rect(0,WD,w_o+1,HT,clrmap+256*11);
189 void W_init(void) {
190 int i,j;
191 static char *anm[ANIT-1][5]={
192 {"WALL22_1","WALL23_1","WALL23_2",NULL,NULL},
193 {"WALL58_1","WALL58_2","WALL58_3",NULL,NULL},
194 {"W73A_1","W73A_2",NULL,NULL,NULL},
195 {"RP2_1","RP2_2","RP2_3","RP2_4",NULL}
196 };
198 for(i=1;i<ANIT;++i) {
199 for(j=0;anm[i-1][j];++j)
200 anih[i][j]=F_getresid(anm[i-1][j]);
201 for(;j<5;++j) anih[i][j]=-1;
203 memset(anic,0,sizeof(anic));
204 DOT_init();
205 SMK_init();
206 FX_init();
207 WP_init();
208 IT_init();
209 SW_init();
210 PL_init();
211 MN_init();
212 M_unlock(horiz);
213 horiz=M_lock(F_getresid("RSKY1"));
215 free_chunks();
218 void W_act(void) {
219 int i,a;
221 if(g_time%3!=0) return;
222 for(i=1;i<256;++i) if((a=walani[i])!=0) {
223 if(anih[a][++anic[a]]==-1) anic[a]=0;
224 walp[i]=M_lock(anih[a][anic[a]]);
228 static void unpack(void *buf,int len,void *obuf) {
229 byte *p,*o;
230 int l,n;
232 for(p=(byte*)buf,o=(byte*)obuf,l=len;l;++p,--l) if(*p==255) {
233 n=*((word*)(++p));memset(o,*(p+=2),n);o+=n;l-=3;
234 }else *(o++)=*p;
237 int W_load(FILE* h) {
238 int i,j,k,g;
239 static wall_t w;
240 void *p,*buf;
242 switch(blk.t) {
243 case MB_WALLNAMES:
244 for(i=0;i<256;++i) {walh[i]=-1;walswp[i]=i;walani[i]=0;}
245 for(i=1;i<256 && blk.sz>0;++i,blk.sz-=sizeof(w)) {
246 myfread(&w,1,sizeof(w),h);
248 if(strncasecmp(w.n,"_WATER_",7)==0) //if(memicmp(w.n,"_WATER_",7)==0)
249 {walp[i]=(void*)(w.n[7]-'0'+1);walh[i]=-2;continue;}
250 walp[i]=M_lock(walh[i]=F_getresid(w.n));
251 if(w.n[0]=='S' && w.n[1]=='W' && w.n[4]=='_') walswp[i]=0;
252 walf[i]=(w.t)?1:0;if(w.t) walh[i]|=0x8000;
254 if(strncasecmp(w.n,"VTRAP01",8)==0) walf[i]|=2; //if(memicmp(w.n,"VTRAP01",8)==0) walf[i]|=2;
255 walani[i]=getani(w.n);
257 for(j=i,i=1;i<256;++i) if(walswp[i]==0) {
258 if(j>=256) break;
259 F_getresname(w.n,walh[i]&0x7FFF);
260 w.n[5]^=1;
261 g=F_getresid(w.n)|(walh[i]&0x8000);
262 for(k=1;k<256;++k) if(walh[k]==g) break;
263 if(k>=256) {
264 walh[k=j++]=g;walp[k]=M_lock(g);
265 walf[k]=(g&0x8000)?1:0;
267 walswp[i]=k;walswp[k]=i;
269 return 1;
270 case MB_BACK: p=fldb;goto unp;
271 case MB_WTYPE: p=fld;goto unp;
272 case MB_FRONT: p=fldf;
273 unp: switch(blk.st) {
274 case 0: myfread(p,1,FLDW*FLDH,h);break;
275 case 1:
276 if(!(buf=malloc(blk.sz)))
277 ERR_fatal("Не хватает памяти");
278 myfread(buf,1,blk.sz,h);
279 unpack(buf,blk.sz,p);free(buf);break;
280 default: return 0;
281 }return 1;
282 case MB_SKY:
283 sky_type=0;myfread(&sky_type,1,2,h);
284 strcpy(w.n,"RSKY1");w.n[4]=sky_type+'0';
285 M_unlock(horiz);
286 horiz=M_lock(F_getresid(w.n));
287 return 1;
288 }return 0;