DEADSOFTWARE

fix some warnings
[flatwaifu.git] / src / game.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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "files.h"
28 #include "memory.h"
29 #include "vga.h"
30 #include "keyb.h"
31 #include "sound.h"
32 #include "view.h"
33 #include "bmap.h"
34 #include "fx.h"
35 #include "switch.h"
36 #include "weapons.h"
37 #include "items.h"
38 #include "dots.h"
39 #include "smoke.h"
40 #include "player.h"
41 #include "monster.h"
42 #include "menu.h"
43 #include "misc.h"
44 #include "map.h"
45 #include "my.h"
47 #include <SDL_keysym.h>
49 #define LT_DELAY 8
50 #define LT_HITTIME 6
52 #define GETIME 1092
55 int A8_start(char*);
56 int A8_nextframe(void);
57 void A8_close(void);
60 void FX_trans1(int t);
61 extern unsigned char fx_scr1[64000],fx_scr2[64000];
63 extern short lastkey;
66 extern int hit_xv,hit_yv;
68 extern vgapal std_pal;
69 void setgamma(int);
71 extern int sw_secrets;
73 #define PL_FLASH 90
75 extern int PL_JUMP;
77 extern map_block_t blk;
79 extern byte clrmap[256*12];
81 extern byte cheat;
83 byte _2pl=0,g_dm=0,g_st=GS_TITLE,g_exit=0,g_map=1,_warp=0;
84 char g_music[8]="MENU";
85 byte _net=0;
86 int g_sttm=1092;
87 dword g_time;
88 int dm_pnum,dm_pl1p,dm_pl2p;
89 pos_t dm_pos[100];
91 static void *telepsnd;
92 static void *scrnh[3];
93 void *cd_scr;
95 extern int sky_type;
96 void *ltn[2][2];
97 int lt_time,lt_type,lt_side,lt_ypos,lt_force;
98 void *ltnsnd[2];
100 int g_trans=0,g_transt;
102 static void set_trans(int st) {
103 switch(g_st) {
104 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
105 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
106 g_st=st;return;
108 switch(g_st=st) {
109 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
110 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
111 return;
113 g_trans=1;g_transt=0;
116 void G_savegame(FILE* h) {
117 myfwrite8(_2pl, h);
118 myfwrite8(g_dm, h);
119 myfwrite8(g_exit, h);
120 myfwrite8(g_map, h);
121 myfwrite32(g_time, h);
122 myfwrite32(dm_pl1p, h);
123 myfwrite32(dm_pl2p, h);
124 myfwrite32(dm_pnum, h);
125 int i = 0;
126 while (i < dm_pnum) {
127 myfwrite32(dm_pos[i].x, h);
128 myfwrite32(dm_pos[i].y, h);
129 myfwrite8(dm_pos[i].d, h);
130 i += 1;
132 myfwrite8(cheat, h);
133 myfwrite(g_music, 8, 1, h);
136 void G_loadgame(FILE* h) {
137 _2pl = myfread8(h);
138 g_dm = myfread8(h);
139 g_exit = myfread8(h);
140 g_map = myfread8(h);
141 g_time = myfread32(h);
142 dm_pl1p = myfread32(h);
143 dm_pl2p = myfread32(h);
144 dm_pnum = myfread32(h);
145 int i = 0;
146 while (i < dm_pnum) {
147 dm_pos[i].x = myfread32(h);
148 dm_pos[i].y = myfread32(h);
149 dm_pos[i].d = myfread8(h);
150 i += 1;
152 cheat = myfread8(h);
153 myfread(g_music, 8, 1, h);
154 F_loadmus(g_music);
157 int G_load (FILE *h) {
158 switch (blk.t) {
159 case MB_MUSIC:
160 myfread(g_music, 8, 1, h);
161 if (music_random) {
162 F_randmus(g_music);
164 F_loadmus(g_music);
165 return 1;
167 return 0;
170 void load_game(int n) {
171 F_freemus();
172 W_init();
173 F_loadgame(n);
174 set_trans(GS_GAME);
175 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
176 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
177 if(_2pl) {w_o=0;Z_clrst();w_o=SCRH/2;Z_clrst();}//if(_2pl) {w_o=0;Z_clrst();w_o=100;Z_clrst();}
178 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
179 V_setscr(scrbuf);
180 pl1.drawst=0xFF;
181 if(_2pl) pl2.drawst=0xFF;
182 BM_remapfld();
183 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
184 BM_mark(&pl1.o,BM_PLR1);
185 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
186 MN_mark();
187 S_startmusic(music_time);
190 void G_start(void) {
191 char s[8];
193 F_freemus();
194 sprintf(s,"MAP%02u",(word)g_map);
195 F_loadmap(s);
196 set_trans(GS_GAME);
197 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
198 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
199 if(_2pl) {w_o=0;Z_clrst();w_o=SCRH/2;Z_clrst();}//if(_2pl) {w_o=0;Z_clrst();w_o=100;Z_clrst();}
200 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
201 V_setscr(scrbuf);
202 pl1.drawst=0xFF;
203 if(_2pl) pl2.drawst=0xFF;
204 g_exit=0;
205 itm_rtime=(g_dm)?1092:0;
206 p_immortal=0;PL_JUMP=10;
207 g_time=0;
208 lt_time=1000;
209 lt_force=1;
210 if(!_2pl) pl1.lives=3;
211 BM_remapfld();
212 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
213 BM_mark(&pl1.o,BM_PLR1);
214 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
215 MN_mark();
216 S_startmusic(music_time);
219 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
221 void G_init(void) {
222 int i,j;
223 char s[9];
225 logo("G_init: настройка ресурсов игры ");
226 logo_gas(5,GGAS_TOTAL);
227 telepsnd=Z_getsnd("TELEPT");
228 scrnh[0]=V_loadvgaimg("TITLEPIC");
229 scrnh[1]=V_loadvgaimg("INTERPIC");
230 scrnh[2]=V_loadvgaimg("ENDPIC");
231 cd_scr=V_loadvgaimg("CD1PIC");
232 for(i=0;i<2;++i) {
233 sprintf(s,"LTN%c",i+'1');
234 for(j=0;j<2;++j)
235 ltn[i][j]=Z_getspr(s,j,0,NULL);
237 ltnsnd[0]=Z_getsnd("THUND1");
238 ltnsnd[1]=Z_getsnd("THUND2");
239 DOT_alloc();
240 SMK_alloc();
241 FX_alloc();
242 WP_alloc();
243 IT_alloc();
244 SW_alloc();
245 PL_alloc();
246 MN_alloc();
247 Z_initst();
248 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
249 logo("\n");
250 GM_init();
251 pl1.color=0x70;
252 pl2.color=0x60;
253 g_trans=0;
256 int G_beg_video(void) {
257 /*
258 switch(g_map) {
259 case 3: return A8_start("FALL");
260 case 4: return A8_start("KORIDOR");
261 case 5: return A8_start("SKULL");
262 case 6: return A8_start("TORCHES");
263 case 7: return A8_start("CACO");
264 case 8: return A8_start("DARTS");
265 case 9: return A8_start("FISH");
266 case 10: return A8_start("TRAP");
267 case 11: return A8_start("JAIL");
268 case 12: return A8_start("MMON1");
269 case 13: return A8_start("TOWER");
270 case 14: return A8_start("SAPOG");
271 case 15: return A8_start("SWITCH");
272 case 16: return A8_start("ACCEL");
273 case 17: return A8_start("MEAT");
274 case 18: return A8_start("LEGION");
275 case 19: return A8_start("CLOUDS");
277 */
278 return 0;
282 int G_end_video(void) {
283 /*
284 switch(g_map) {
285 case 1: return A8_start("TRUBA");
286 case 10: return A8_start("GOTCHA");
288 */
289 return 0;
293 static byte transdraw=0;
295 void G_act(void) {
296 static byte pcnt=0;
297 /*
298 if(g_trans) {
299 if(g_transt==0) {
300 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
301 V_setscr(fx_scr2);
302 transdraw=1;G_draw();transdraw=0;
303 V_setscr(scrbuf);
305 FX_trans1(g_transt*2);
306 V_copytoscr(0,320,0,200);
307 if(++g_transt>32) {
308 g_trans=0;
310 return;
311 g_trans=0;
313 */ g_trans=0;
316 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
317 if(!A8_nextframe() || lastkey==SDLK_ESCAPE) {
318 if(lastkey==SDLK_ESCAPE) lastkey=0;
319 A8_close();
320 if(g_st==GS_BVIDEO) G_start();
321 else goto inter;
323 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
324 return;
325 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
326 if(!A8_nextframe()) {
327 switch(g_st) {
328 case GS_ENDANIM: g_st=GS_DARKEN;break;
329 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
330 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
331 }g_sttm=0;return;
333 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
334 return;
335 }else if(g_st==GS_DARKEN) {
336 g_st=GS_END2ANIM;A8_start("CREDITS");
337 return;
340 if(GM_act()) return;
344 switch(g_st) {
345 case GS_TITLE: case GS_ENDSCR:
347 return;
348 case GS_INTER:
349 #ifdef DEMO
350 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
351 set_trans(GS_TITLE);
353 #else
354 if(keys[SDLK_SPACE] || keys[SDLK_RETURN] || keys[SDLK_KP_ENTER])//if(keys[0x39] || keys[0x1C] || keys[0x9C])
355 if(!G_beg_video()) G_start(); else {
356 g_st=GS_BVIDEO;F_freemus();
358 #endif
359 return;
363 if(sky_type==2) {
364 if(lt_time>LT_DELAY || lt_force) {
365 if(!(rand()&31) || lt_force) {
366 lt_force=0;
367 lt_time=-LT_HITTIME;
368 lt_type=rand()%2;
369 lt_side=rand()&1;
370 lt_ypos=rand()&31;
371 Z_sound(ltnsnd[rand()&1],128);
373 }else ++lt_time;
375 ++g_time;
376 pl1.hit=0;pl1.hito=-3;
377 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
378 G_code();
380 W_act();
381 IT_act();
382 SW_act();
383 if(_2pl) {
384 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
385 else {PL_act(&pl2);PL_act(&pl1);}
386 pcnt^=1;
387 }else PL_act(&pl1);
388 MN_act();
389 if(fld_need_remap) BM_remapfld();
390 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
391 BM_mark(&pl1.o,BM_PLR1);
392 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
393 MN_mark();
394 WP_act();
395 DOT_act();
396 SMK_act();
397 FX_act();
398 if(_2pl) {
399 PL_damage(&pl1);PL_damage(&pl2);
400 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
401 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
402 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
403 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
404 }else{
405 PL_damage(&pl1);
406 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
407 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
409 if(g_exit==1) {
411 if(G_end_video()) {
412 F_freemus();
413 g_st=GS_EVIDEO;
414 return;
417 inter:
418 switch(g_map) {
419 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
420 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
421 default: ++g_map;set_trans(GS_INTER);break;
423 F_freemus();
424 if(g_st==GS_INTER) {
425 F_loadmus("INTERMUS");
426 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
427 S_startmusic(0);
428 }else if(g_exit==2) {
429 switch(g_map) {
430 case 31: g_map=32;set_trans(GS_INTER);break;
431 case 32: g_map=16;set_trans(GS_INTER);break;
432 default: g_map=31;set_trans(GS_INTER);break;
434 F_freemus();
435 F_loadmus("INTERMUS");
436 S_startmusic(0);
439 #ifdef DEMO
440 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
441 #endif
444 /*
445 static void drawview(player_t *p) {
446 if(p->looky<-50) p->looky=-50;
447 else if(p->looky>50) p->looky=50;
448 w_x=p->o.x;w_y=p->o.y-12+p->looky;W_draw();PL_drawst(p);
450 */
451 static void drawview(player_t *p) {
452 if(p->looky<-SCRH/4) p->looky=-SCRH/4;
453 else if(p->looky>SCRH/4) p->looky=SCRH/4;
454 w_x=p->o.x;w_y=p->o.y-12+p->looky;
455 W_draw();
456 PL_drawst(p);
459 static int get_pu_st(int t) {
460 if(t>=PL_FLASH) return 1;
461 if((t/9)&1) return 0;
462 return 1;
465 static void pl_info(player_t *p,int y) {
466 dword t;
468 t=p->kills*10920/g_time;
469 Z_gotoxy(25,y);Z_printbf("KILLS");//Z_gotoxy(25,y);Z_printbf("KILLS");
470 Z_gotoxy(25,y+15);Z_printbf("KPM");//Z_gotoxy(25,y+15);Z_printbf("KPM");
471 Z_gotoxy(25,y+30);Z_printbf("SECRETS %u / %u",p->secrets,sw_secrets);//Z_gotoxy(25,y+30);Z_printbf("SECRETS %u / %u",p->secrets,sw_secrets);
472 Z_gotoxy(255,y);Z_printbf("%u",p->kills);//Z_gotoxy(255,y);Z_printbf("%u",p->kills);
473 Z_gotoxy(255,y+15);Z_printbf("%u.%u",t/10,t%10);//Z_gotoxy(255,y+15);Z_printbf("%u.%u",t/10,t%10);
476 void G_draw(void) {
477 int h;
478 word hr,mn,sc;
480 if(g_trans && !transdraw) return;
481 switch(g_st) {
482 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
483 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
484 return;
485 case GS_TITLE:
486 V_center(1);//
487 V_pic(0,0,scrnh[0]);
488 V_center(0);//
489 break;
490 case GS_ENDSCR:
491 V_center(1);//
492 V_clr(0,SCRW,0,SCRH,0);V_pic(0,0,scrnh[2]);//V_clr(0,320,0,200,0);V_pic(0,0,scrnh[2]);
493 V_center(0);//
494 break;
495 case GS_INTER:
496 V_center(1);//
497 V_clr(0,SCRW,0,SCRH,0);//
498 V_pic(0,0,scrnh[1]);
499 Z_gotoxy(60,20);Z_printbf("LEVEL COMPLETE");
500 Z_calc_time(g_time,&hr,&mn,&sc);
501 Z_gotoxy(115,40);Z_printbf("TIME %u:%02u:%02u",hr,mn,sc);
502 h=60;
503 if(_2pl) {
504 Z_gotoxy(80,h);Z_printbf("PLAYER ONE");
505 Z_gotoxy(80,h+70);Z_printbf("PLAYER TWO");
506 h+=SCRH/10;//h+=20;
508 pl_info(&pl1,h);
509 if(_2pl) pl_info(&pl2,h+70);
510 V_center(0);//
511 break;
513 V_center(1);//
514 if(g_st!=GS_GAME) {
515 if(g_trans) return;
516 GM_draw();
517 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
518 return;
520 V_center(0);//
522 if(_2pl) {
523 w_o=0;WD=SCRW-120;HT=SCRH/2-2;drawview(&pl1);//w_o=0;drawview(&pl1);
524 w_o=SCRH/2;WD=SCRW-120;HT=SCRH/2-2;drawview(&pl2);//w_o=100;drawview(&pl2);
525 }else{
526 w_o=0;WD=SCRW-120;HT=SCRH-2;drawview(&pl1);//w_o=50;drawview(&pl1);
528 if(g_trans) return;
529 V_center(1);//
530 if(GM_draw()) {
531 pl1.drawst=pl2.drawst=0xFF;//pl1.drawst=pl2.drawst=0;
532 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
533 return;
535 V_center(0);//
536 if(pl1.invl) h=get_pu_st(pl1.invl)*6;
537 else if(pl1.pain<15) h=0;
538 else if(pl1.pain<35) h=1;
539 else if(pl1.pain<55) h=2;
540 else if(pl1.pain<75) h=3;
541 else if(pl1.pain<95) h=4;
542 else h=5;
543 if(h) V_maptoscr(0,SCRW-120,1,(_2pl)?(SCRH/2-2):(SCRH-2),clrmap+h*256);//if(h) V_maptoscr(0,200,(_2pl)?1:51,98,clrmap+h*256);
544 else V_copytoscr(0,SCRW-120,1,(_2pl)?(SCRH/2-2):(SCRH-2)); //else V_copytoscr(0,200,(_2pl)?1:51,98);
545 if(pl1.drawst) V_copytoscr(SCRW-120,120,0,_2pl?(SCRH/2):SCRH);//if(pl1.drawst) V_copytoscr(200,120,(_2pl)?0:50,100);
546 pl1.drawst=0xFF;//pl1.drawst=0;
547 if(_2pl) {
548 if(pl2.invl) h=get_pu_st(pl2.invl)*6;
549 else if(pl2.pain<15) h=0;
550 else if(pl2.pain<35) h=1;
551 else if(pl2.pain<55) h=2;
552 else if(pl2.pain<75) h=3;
553 else if(pl2.pain<95) h=4;
554 else h=5;
555 if(h) V_maptoscr(0,SCRW-120,SCRH/2+1,SCRH/2-2,clrmap+h*256);//if(h) V_maptoscr(0,200,101,98,clrmap+h*256);
556 else V_copytoscr(0,SCRW-120,SCRH/2+1,SCRH/2-2);//else V_copytoscr(0,200,101,98);
557 if(pl2.drawst) V_copytoscr(SCRW-120,120,SCRH/2,SCRH/2);//if(pl2.drawst) V_copytoscr(200,120,100,100);
558 pl2.drawst=0xFF;//pl2.drawst=0;
562 void G_respawn_player(player_t *p) {
563 int i;
565 if(dm_pnum==2) {
566 if(p==&pl1) i=dm_pl1p^=1;
567 else i=dm_pl2p^=1;
568 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
569 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
570 return;
572 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
573 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
574 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
575 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);