DEADSOFTWARE

fe52751f86674a1d244603716d1c25764feeb4a1
[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 byte transdraw=0;
63 void FX_trans1(int t);
64 extern unsigned char fx_scr1[64000],fx_scr2[64000];
66 extern short lastkey;
69 extern int hit_xv,hit_yv;
71 extern vgapal std_pal;
72 void setgamma(int);
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;
83 char g_music[8]="MENU";
84 byte _net=0;
85 int g_sttm=1092;
86 dword g_time;
87 int dm_pnum,dm_pl1p,dm_pl2p;
88 pos_t dm_pos[100];
90 static void *telepsnd;
91 void *scrnh[3];
92 void *cd_scr;
94 extern int sky_type;
95 void *ltn[2][2];
96 int lt_time,lt_type,lt_side,lt_ypos,lt_force;
97 void *ltnsnd[2];
99 int g_trans=0,g_transt;
101 static void set_trans(int st) {
102 switch(g_st) {
103 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
104 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
105 g_st=st;return;
107 switch(g_st=st) {
108 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
109 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
110 return;
112 g_trans=1;g_transt=0;
115 void G_savegame(FILE* h) {
116 myfwrite8(_2pl, h);
117 myfwrite8(g_dm, h);
118 myfwrite8(g_exit, h);
119 myfwrite8(g_map, h);
120 myfwrite32(g_time, h);
121 myfwrite32(dm_pl1p, h);
122 myfwrite32(dm_pl2p, h);
123 myfwrite32(dm_pnum, h);
124 int i = 0;
125 while (i < dm_pnum) {
126 myfwrite32(dm_pos[i].x, h);
127 myfwrite32(dm_pos[i].y, h);
128 myfwrite8(dm_pos[i].d, h);
129 i += 1;
131 myfwrite8(cheat, h);
132 myfwrite(g_music, 8, 1, h);
135 void G_loadgame(FILE* h) {
136 _2pl = myfread8(h);
137 g_dm = myfread8(h);
138 g_exit = myfread8(h);
139 g_map = myfread8(h);
140 g_time = myfread32(h);
141 dm_pl1p = myfread32(h);
142 dm_pl2p = myfread32(h);
143 dm_pnum = myfread32(h);
144 int i = 0;
145 while (i < dm_pnum) {
146 dm_pos[i].x = myfread32(h);
147 dm_pos[i].y = myfread32(h);
148 dm_pos[i].d = myfread8(h);
149 i += 1;
151 cheat = myfread8(h);
152 myfread(g_music, 8, 1, h);
153 F_loadmus(g_music);
156 int G_load (FILE *h) {
157 switch (blk.t) {
158 case MB_MUSIC:
159 myfread(g_music, 8, 1, h);
160 if (music_random) {
161 F_randmus(g_music);
163 F_loadmus(g_music);
164 return 1;
166 return 0;
169 void load_game(int n) {
170 F_freemus();
171 W_init();
172 F_loadgame(n);
173 set_trans(GS_GAME);
174 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
175 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
176 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();}
177 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
178 V_setscr(scrbuf);
179 pl1.drawst=0xFF;
180 if(_2pl) pl2.drawst=0xFF;
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 void G_start(void) {
190 char s[8];
192 F_freemus();
193 sprintf(s,"MAP%02u",(word)g_map);
194 F_loadmap(s);
195 set_trans(GS_GAME);
196 V_setscr((g_trans)?fx_scr2:scrbuf);V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
197 V_clr(0,SCRW,0,SCRH,0);//V_clr(0,320,0,200,0);
198 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();}
199 else {w_o=0;Z_clrst();}//else {w_o=50;Z_clrst();}
200 V_setscr(scrbuf);
201 pl1.drawst=0xFF;
202 if(_2pl) pl2.drawst=0xFF;
203 g_exit=0;
204 itm_rtime=(g_dm)?1092:0;
205 p_immortal=0;PL_JUMP=10;
206 g_time=0;
207 lt_time=1000;
208 lt_force=1;
209 if(!_2pl) pl1.lives=3;
210 BM_remapfld();
211 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
212 BM_mark(&pl1.o,BM_PLR1);
213 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
214 MN_mark();
215 S_startmusic(music_time);
218 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
220 void G_init(void) {
221 int i,j;
222 char s[9];
224 logo("G_init: настройка ресурсов игры ");
225 logo_gas(5,GGAS_TOTAL);
226 telepsnd=Z_getsnd("TELEPT");
227 scrnh[0]=V_loadvgaimg("TITLEPIC");
228 scrnh[1]=V_loadvgaimg("INTERPIC");
229 scrnh[2]=V_loadvgaimg("ENDPIC");
230 cd_scr=V_loadvgaimg("CD1PIC");
231 for(i=0;i<2;++i) {
232 sprintf(s,"LTN%c",i+'1');
233 for(j=0;j<2;++j)
234 ltn[i][j]=Z_getspr(s,j,0,NULL);
236 ltnsnd[0]=Z_getsnd("THUND1");
237 ltnsnd[1]=Z_getsnd("THUND2");
238 DOT_alloc();
239 SMK_alloc();
240 FX_alloc();
241 WP_alloc();
242 IT_alloc();
243 SW_alloc();
244 PL_alloc();
245 MN_alloc();
246 Z_initst();
247 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
248 logo("\n");
249 GM_init();
250 pl1.color=0x70;
251 pl2.color=0x60;
252 g_trans=0;
255 int G_beg_video(void) {
256 /*
257 switch(g_map) {
258 case 3: return A8_start("FALL");
259 case 4: return A8_start("KORIDOR");
260 case 5: return A8_start("SKULL");
261 case 6: return A8_start("TORCHES");
262 case 7: return A8_start("CACO");
263 case 8: return A8_start("DARTS");
264 case 9: return A8_start("FISH");
265 case 10: return A8_start("TRAP");
266 case 11: return A8_start("JAIL");
267 case 12: return A8_start("MMON1");
268 case 13: return A8_start("TOWER");
269 case 14: return A8_start("SAPOG");
270 case 15: return A8_start("SWITCH");
271 case 16: return A8_start("ACCEL");
272 case 17: return A8_start("MEAT");
273 case 18: return A8_start("LEGION");
274 case 19: return A8_start("CLOUDS");
276 */
277 return 0;
281 int G_end_video(void) {
282 /*
283 switch(g_map) {
284 case 1: return A8_start("TRUBA");
285 case 10: return A8_start("GOTCHA");
287 */
288 return 0;
292 void G_act(void) {
293 static byte pcnt=0;
294 /*
295 if(g_trans) {
296 if(g_transt==0) {
297 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
298 V_setscr(fx_scr2);
299 transdraw=1;G_draw();transdraw=0;
300 V_setscr(scrbuf);
302 FX_trans1(g_transt*2);
303 V_copytoscr(0,320,0,200);
304 if(++g_transt>32) {
305 g_trans=0;
307 return;
308 g_trans=0;
310 */ g_trans=0;
313 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
314 if(!A8_nextframe() || lastkey==SDLK_ESCAPE) {
315 if(lastkey==SDLK_ESCAPE) lastkey=0;
316 A8_close();
317 if(g_st==GS_BVIDEO) G_start();
318 else goto inter;
320 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
321 return;
322 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
323 if(!A8_nextframe()) {
324 switch(g_st) {
325 case GS_ENDANIM: g_st=GS_DARKEN;break;
326 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
327 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
328 }g_sttm=0;return;
330 V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
331 return;
332 }else if(g_st==GS_DARKEN) {
333 g_st=GS_END2ANIM;A8_start("CREDITS");
334 return;
337 if(GM_act()) return;
341 switch(g_st) {
342 case GS_TITLE: case GS_ENDSCR:
344 return;
345 case GS_INTER:
346 #ifdef DEMO
347 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
348 set_trans(GS_TITLE);
350 #else
351 if(keys[SDLK_SPACE] || keys[SDLK_RETURN] || keys[SDLK_KP_ENTER])//if(keys[0x39] || keys[0x1C] || keys[0x9C])
352 if(!G_beg_video()) G_start(); else {
353 g_st=GS_BVIDEO;F_freemus();
355 #endif
356 return;
360 if(sky_type==2) {
361 if(lt_time>LT_DELAY || lt_force) {
362 if(!(rand()&31) || lt_force) {
363 lt_force=0;
364 lt_time=-LT_HITTIME;
365 lt_type=rand()%2;
366 lt_side=rand()&1;
367 lt_ypos=rand()&31;
368 Z_sound(ltnsnd[rand()&1],128);
370 }else ++lt_time;
372 ++g_time;
373 pl1.hit=0;pl1.hito=-3;
374 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
375 G_code();
377 W_act();
378 IT_act();
379 SW_act();
380 if(_2pl) {
381 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
382 else {PL_act(&pl2);PL_act(&pl1);}
383 pcnt^=1;
384 }else PL_act(&pl1);
385 MN_act();
386 if(fld_need_remap) BM_remapfld();
387 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
388 BM_mark(&pl1.o,BM_PLR1);
389 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
390 MN_mark();
391 WP_act();
392 DOT_act();
393 SMK_act();
394 FX_act();
395 if(_2pl) {
396 PL_damage(&pl1);PL_damage(&pl2);
397 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
398 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
399 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
400 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
401 }else{
402 PL_damage(&pl1);
403 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
404 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
406 if(g_exit==1) {
408 if(G_end_video()) {
409 F_freemus();
410 g_st=GS_EVIDEO;
411 return;
414 inter:
415 switch(g_map) {
416 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
417 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
418 default: ++g_map;set_trans(GS_INTER);break;
420 F_freemus();
421 if(g_st==GS_INTER) {
422 F_loadmus("INTERMUS");
423 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
424 S_startmusic(0);
425 }else if(g_exit==2) {
426 switch(g_map) {
427 case 31: g_map=32;set_trans(GS_INTER);break;
428 case 32: g_map=16;set_trans(GS_INTER);break;
429 default: g_map=31;set_trans(GS_INTER);break;
431 F_freemus();
432 F_loadmus("INTERMUS");
433 S_startmusic(0);
436 #ifdef DEMO
437 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
438 #endif
441 void G_respawn_player(player_t *p) {
442 int i;
444 if(dm_pnum==2) {
445 if(p==&pl1) i=dm_pl1p^=1;
446 else i=dm_pl2p^=1;
447 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
448 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
449 return;
451 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
452 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
453 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
454 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);