DEADSOFTWARE

1b35f7eb5469c3ed164c92c607d13602fbe1e77e
[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"
46 #include <SDL_keysym.h>
48 #define LT_DELAY 8
49 #define LT_HITTIME 6
51 #define GETIME 1092
54 int A8_start(char*);
55 int A8_nextframe(void);
56 void A8_close(void);
59 void FX_trans1(int t);
60 extern unsigned char fx_scr1[64000],fx_scr2[64000];
62 extern short lastkey;
65 extern int hit_xv,hit_yv;
67 extern vgapal std_pal;
68 void setgamma(int);
70 extern int sw_secrets;
72 #define PL_FLASH 90
74 extern int PL_JUMP;
76 extern map_block_t blk;
78 extern byte clrmap[256*12];
80 extern byte cheat;
82 byte _2pl=0,g_dm=0,g_st=GS_TITLE,g_exit=0,g_map=1,_warp=0,g_music[8]="MENU";
83 byte _net=0;
84 int g_sttm=1092;
85 dword g_time;
86 int dm_pnum,dm_pl1p,dm_pl2p;
87 pos_t dm_pos[100];
89 static void *telepsnd;
90 static void *scrnh[3];
91 void *cd_scr;
93 extern int sky_type;
94 void *ltn[2][2];
95 int lt_time,lt_type,lt_side,lt_ypos,lt_force;
96 void *ltnsnd[2];
98 int g_trans=0,g_transt;
100 static void set_trans(int st) {
101 switch(g_st) {
102 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
103 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
104 g_st=st;return;
106 switch(g_st=st) {
107 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
108 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
109 return;
111 g_trans=1;g_transt=0;
114 void G_savegame(FILE* h) {
115 myfwrite(&_2pl,1,1,h);myfwrite(&g_dm,1,1,h);myfwrite(&g_exit,1,1,h);myfwrite(&g_map,1,1,h);
116 myfwrite(&g_time,1,4,h);myfwrite(&dm_pl1p,1,4,h);myfwrite(&dm_pl2p,1,4,h);
117 myfwrite(&dm_pnum,1,4,h);myfwrite(dm_pos,1,dm_pnum*sizeof(pos_t),h);
118 myfwrite(&cheat,1,1,h);
119 myfwrite(g_music,1,8,h);
122 void G_loadgame(FILE* h) {
123 myfread(&_2pl,1,1,h);myfread(&g_dm,1,1,h);myfread(&g_exit,1,1,h);myfread(&g_map,1,1,h);
124 myfread(&g_time,1,4,h);myfread(&dm_pl1p,1,4,h);myfread(&dm_pl2p,1,4,h);
125 myfread(&dm_pnum,1,4,h);myfread(dm_pos,1,dm_pnum*sizeof(pos_t),h);
126 myfread(&cheat,1,1,h);
127 myfread(g_music,1,8,h);F_loadmus(g_music);
130 int G_load(FILE* h) {
131 switch(blk.t) {
132 case MB_MUSIC:
133 myfread(g_music,1,8,h);
134 if (music_random) F_randmus(g_music);
135 F_loadmus(g_music);
136 return 1;
137 }return 0;
140 void load_game(int n) {
141 F_freemus();
142 W_init();
143 F_loadgame(n);
144 set_trans(GS_GAME);
145 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
146 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
147 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();}
148 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
149 V_setscr(scrbuf);
150 pl1.drawst=0xFF;
151 if(_2pl) pl2.drawst=0xFF;
152 BM_remapfld();
153 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
154 BM_mark(&pl1.o,BM_PLR1);
155 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
156 MN_mark();
157 S_startmusic(music_time);
160 void G_start(void) {
161 char s[8];
163 F_freemus();
164 sprintf(s,"MAP%02u",(word)g_map);
165 F_loadmap(s);
166 set_trans(GS_GAME);
167 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
168 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
169 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();}
170 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
171 V_setscr(scrbuf);
172 pl1.drawst=0xFF;
173 if(_2pl) pl2.drawst=0xFF;
174 g_exit=0;
175 itm_rtime=(g_dm)?1092:0;
176 p_immortal=0;PL_JUMP=10;
177 g_time=0;
178 lt_time=1000;
179 lt_force=1;
180 if(!_2pl) pl1.lives=3;
181 BM_remapfld();
182 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
183 BM_mark(&pl1.o,BM_PLR1);
184 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
185 MN_mark();
186 S_startmusic(music_time);
189 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
191 void G_init(void) {
192 int i,j;
193 char s[9];
195 logo("G_init: настройка ресурсов игры ");
196 logo_gas(5,GGAS_TOTAL);
197 telepsnd=Z_getsnd("TELEPT");
198 scrnh[0]=V_loadvgaimg("TITLEPIC");
199 scrnh[1]=V_loadvgaimg("INTERPIC");
200 scrnh[2]=V_loadvgaimg("ENDPIC");
201 cd_scr=V_loadvgaimg("CD1PIC");
202 for(i=0;i<2;++i) {
203 sprintf(s,"LTN%c",i+'1');
204 for(j=0;j<2;++j)
205 ltn[i][j]=Z_getspr(s,j,0,NULL);
207 ltnsnd[0]=Z_getsnd("THUND1");
208 ltnsnd[1]=Z_getsnd("THUND2");
209 DOT_alloc();
210 SMK_alloc();
211 FX_alloc();
212 WP_alloc();
213 IT_alloc();
214 SW_alloc();
215 PL_alloc();
216 MN_alloc();
217 Z_initst();
218 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
219 logo("\n");
220 GM_init();
221 pl1.color=0x70;
222 pl2.color=0x60;
223 g_trans=0;
226 int G_beg_video(void) {
227 /*
228 switch(g_map) {
229 case 3: return A8_start("FALL");
230 case 4: return A8_start("KORIDOR");
231 case 5: return A8_start("SKULL");
232 case 6: return A8_start("TORCHES");
233 case 7: return A8_start("CACO");
234 case 8: return A8_start("DARTS");
235 case 9: return A8_start("FISH");
236 case 10: return A8_start("TRAP");
237 case 11: return A8_start("JAIL");
238 case 12: return A8_start("MMON1");
239 case 13: return A8_start("TOWER");
240 case 14: return A8_start("SAPOG");
241 case 15: return A8_start("SWITCH");
242 case 16: return A8_start("ACCEL");
243 case 17: return A8_start("MEAT");
244 case 18: return A8_start("LEGION");
245 case 19: return A8_start("CLOUDS");
247 */
248 return 0;
252 int G_end_video(void) {
253 /*
254 switch(g_map) {
255 case 1: return A8_start("TRUBA");
256 case 10: return A8_start("GOTCHA");
258 */
259 return 0;
263 static byte transdraw=0;
265 void G_act(void) {
266 static byte pcnt=0;
267 /*
268 if(g_trans) {
269 if(g_transt==0) {
270 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
271 V_setscr(fx_scr2);
272 transdraw=1;G_draw();transdraw=0;
273 V_setscr(scrbuf);
275 FX_trans1(g_transt*2);
276 V_copytoscr(0,320,0,200);
277 if(++g_transt>32) {
278 g_trans=0;
280 return;
281 g_trans=0;
283 */ g_trans=0;
286 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
287 if(!A8_nextframe() || lastkey==SDLK_ESCAPE) {
288 if(lastkey==SDLK_ESCAPE) lastkey=0;
289 A8_close();
290 if(g_st==GS_BVIDEO) G_start();
291 else goto inter;
293 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
294 return;
295 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
296 if(!A8_nextframe()) {
297 switch(g_st) {
298 case GS_ENDANIM: g_st=GS_DARKEN;break;
299 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
300 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
301 }g_sttm=0;return;
303 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
304 return;
305 }else if(g_st==GS_DARKEN) {
306 g_st=GS_END2ANIM;A8_start("CREDITS");
307 return;
310 if(GM_act()) return;
314 switch(g_st) {
315 case GS_TITLE: case GS_ENDSCR:
317 return;
318 case GS_INTER:
319 #ifdef DEMO
320 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
321 set_trans(GS_TITLE);
323 #else
324 if(keys[SDLK_SPACE] || keys[SDLK_RETURN] || keys[SDLK_KP_ENTER])//if(keys[0x39] || keys[0x1C] || keys[0x9C])
325 if(!G_beg_video()) G_start(); else {
326 g_st=GS_BVIDEO;F_freemus();
328 #endif
329 return;
333 if(sky_type==2) {
334 if(lt_time>LT_DELAY || lt_force) {
335 if(!(rand()&31) || lt_force) {
336 lt_force=0;
337 lt_time=-LT_HITTIME;
338 lt_type=rand()%2;
339 lt_side=rand()&1;
340 lt_ypos=rand()&31;
341 Z_sound(ltnsnd[rand()&1],128);
343 }else ++lt_time;
345 ++g_time;
346 pl1.hit=0;pl1.hito=-3;
347 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
348 G_code();
350 W_act();
351 IT_act();
352 SW_act();
353 if(_2pl) {
354 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
355 else {PL_act(&pl2);PL_act(&pl1);}
356 pcnt^=1;
357 }else PL_act(&pl1);
358 MN_act();
359 if(fld_need_remap) BM_remapfld();
360 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
361 BM_mark(&pl1.o,BM_PLR1);
362 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
363 MN_mark();
364 WP_act();
365 DOT_act();
366 SMK_act();
367 FX_act();
368 if(_2pl) {
369 PL_damage(&pl1);PL_damage(&pl2);
370 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
371 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
372 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
373 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
374 }else{
375 PL_damage(&pl1);
376 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
377 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
379 if(g_exit==1) {
381 if(G_end_video()) {
382 F_freemus();
383 g_st=GS_EVIDEO;
384 return;
387 inter:
388 switch(g_map) {
389 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
390 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
391 default: ++g_map;set_trans(GS_INTER);break;
393 F_freemus();
394 if(g_st==GS_INTER) {
395 F_loadmus("INTERMUS");
396 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
397 S_startmusic(0);
398 }else if(g_exit==2) {
399 switch(g_map) {
400 case 31: g_map=32;set_trans(GS_INTER);break;
401 case 32: g_map=16;set_trans(GS_INTER);break;
402 default: g_map=31;set_trans(GS_INTER);break;
404 F_freemus();
405 F_loadmus("INTERMUS");
406 S_startmusic(0);
409 #ifdef DEMO
410 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
411 #endif
414 /*
415 static void drawview(player_t *p) {
416 if(p->looky<-50) p->looky=-50;
417 else if(p->looky>50) p->looky=50;
418 w_x=p->o.x;w_y=p->o.y-12+p->looky;W_draw();PL_drawst(p);
420 */
421 static void drawview(player_t *p) {
422 if(p->looky<-SCRH/4) p->looky=-SCRH/4;
423 else if(p->looky>SCRH/4) p->looky=SCRH/4;
424 w_x=p->o.x;w_y=p->o.y-12+p->looky;
425 W_draw();
426 PL_drawst(p);
429 static int get_pu_st(int t) {
430 if(t>=PL_FLASH) return 1;
431 if((t/9)&1) return 0;
432 return 1;
435 static void pl_info(player_t *p,int y) {
436 dword t;
438 t=p->kills*10920/g_time;
439 Z_gotoxy(25,y);Z_printbf("KILLS");//Z_gotoxy(25,y);Z_printbf("KILLS");
440 Z_gotoxy(25,y+15);Z_printbf("KPM");//Z_gotoxy(25,y+15);Z_printbf("KPM");
441 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);
442 Z_gotoxy(255,y);Z_printbf("%u",p->kills);//Z_gotoxy(255,y);Z_printbf("%u",p->kills);
443 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);
446 void G_draw(void) {
447 int h;
448 word hr,mn,sc;
450 if(g_trans && !transdraw) return;
451 switch(g_st) {
452 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
453 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
454 return;
455 case GS_TITLE:
456 V_center(1);//
457 V_pic(0,0,scrnh[0]);
458 V_center(0);//
459 break;
460 case GS_ENDSCR:
461 V_center(1);//
462 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]);
463 V_center(0);//
464 break;
465 case GS_INTER:
466 V_center(1);//
467 V_clr(0,SCRW,0,SCRH,0);//
468 V_pic(0,0,scrnh[1]);
469 Z_gotoxy(60,20);Z_printbf("LEVEL COMPLETE");
470 Z_calc_time(g_time,&hr,&mn,&sc);
471 Z_gotoxy(115,40);Z_printbf("TIME %u:%02u:%02u",hr,mn,sc);
472 h=60;
473 if(_2pl) {
474 Z_gotoxy(80,h);Z_printbf("PLAYER ONE");
475 Z_gotoxy(80,h+70);Z_printbf("PLAYER TWO");
476 h+=SCRH/10;//h+=20;
478 pl_info(&pl1,h);
479 if(_2pl) pl_info(&pl2,h+70);
480 V_center(0);//
481 break;
483 V_center(1);//
484 if(g_st!=GS_GAME) {
485 if(g_trans) return;
486 GM_draw();
487 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
488 return;
490 V_center(0);//
492 if(_2pl) {
493 w_o=0;WD=SCRW-120;HT=SCRH/2-2;drawview(&pl1);//w_o=0;drawview(&pl1);
494 w_o=SCRH/2;WD=SCRW-120;HT=SCRH/2-2;drawview(&pl2);//w_o=100;drawview(&pl2);
495 }else{
496 w_o=0;WD=SCRW-120;HT=SCRH-2;drawview(&pl1);//w_o=50;drawview(&pl1);
498 if(g_trans) return;
499 V_center(1);//
500 if(GM_draw()) {
501 pl1.drawst=pl2.drawst=0xFF;//pl1.drawst=pl2.drawst=0;
502 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
503 return;
505 V_center(0);//
506 if(pl1.invl) h=get_pu_st(pl1.invl)*6;
507 else if(pl1.pain<15) h=0;
508 else if(pl1.pain<35) h=1;
509 else if(pl1.pain<55) h=2;
510 else if(pl1.pain<75) h=3;
511 else if(pl1.pain<95) h=4;
512 else h=5;
513 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);
514 else V_copytoscr(0,SCRW-120,1,(_2pl)?(SCRH/2-2):(SCRH-2)); //else V_copytoscr(0,200,(_2pl)?1:51,98);
515 if(pl1.drawst) V_copytoscr(SCRW-120,120,0,_2pl?(SCRH/2):SCRH);//if(pl1.drawst) V_copytoscr(200,120,(_2pl)?0:50,100);
516 pl1.drawst=0xFF;//pl1.drawst=0;
517 if(_2pl) {
518 if(pl2.invl) h=get_pu_st(pl2.invl)*6;
519 else if(pl2.pain<15) h=0;
520 else if(pl2.pain<35) h=1;
521 else if(pl2.pain<55) h=2;
522 else if(pl2.pain<75) h=3;
523 else if(pl2.pain<95) h=4;
524 else h=5;
525 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);
526 else V_copytoscr(0,SCRW-120,SCRH/2+1,SCRH/2-2);//else V_copytoscr(0,200,101,98);
527 if(pl2.drawst) V_copytoscr(SCRW-120,120,SCRH/2,SCRH/2);//if(pl2.drawst) V_copytoscr(200,120,100,100);
528 pl2.drawst=0xFF;//pl2.drawst=0;
532 void G_respawn_player(player_t *p) {
533 int i;
535 if(dm_pnum==2) {
536 if(p==&pl1) i=dm_pl1p^=1;
537 else i=dm_pl2p^=1;
538 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
539 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
540 return;
542 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
543 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
544 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
545 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);