DEADSOFTWARE

4ae9e11fdd949e4f9584b3bf6b51bc6b78d05bbb
[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 "keyb.h"
30 #include "sound.h"
31 #include "view.h"
32 #include "bmap.h"
33 #include "fx.h"
34 #include "switch.h"
35 #include "weapons.h"
36 #include "items.h"
37 #include "dots.h"
38 #include "smoke.h"
39 #include "player.h"
40 #include "monster.h"
41 #include "menu.h"
42 #include "misc.h"
43 #include "map.h"
44 #include "my.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 byte transdraw=0;
62 void FX_trans1(int t);
63 extern unsigned char fx_scr1[64000],fx_scr2[64000];
65 extern short lastkey;
68 extern int hit_xv,hit_yv;
70 //void setgamma(int);
72 extern int PL_JUMP;
74 extern map_block_t blk;
76 extern byte cheat;
78 byte _2pl=0,g_dm=0,g_st=GS_TITLE,g_exit=0,g_map=1,_warp=0;
79 char g_music[8]="MENU";
80 byte _net=0;
81 int g_sttm=1092;
82 dword g_time;
83 int dm_pnum,dm_pl1p,dm_pl2p;
84 pos_t dm_pos[100];
86 static void *telepsnd;
87 void *scrnh[3];
88 void *cd_scr;
90 extern int sky_type;
91 void *ltn[2][2];
92 int lt_time,lt_type,lt_side,lt_ypos,lt_force;
93 void *ltnsnd[2];
95 int g_trans=0,g_transt;
97 static void set_trans(int st) {
98 switch(g_st) {
99 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
100 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
101 g_st=st;return;
103 switch(g_st=st) {
104 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
105 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
106 return;
108 g_trans=1;g_transt=0;
111 void G_savegame(FILE* h) {
112 myfwrite8(_2pl, h);
113 myfwrite8(g_dm, h);
114 myfwrite8(g_exit, h);
115 myfwrite8(g_map, h);
116 myfwrite32(g_time, h);
117 myfwrite32(dm_pl1p, h);
118 myfwrite32(dm_pl2p, h);
119 myfwrite32(dm_pnum, h);
120 int i = 0;
121 while (i < dm_pnum) {
122 myfwrite32(dm_pos[i].x, h);
123 myfwrite32(dm_pos[i].y, h);
124 myfwrite8(dm_pos[i].d, h);
125 i += 1;
127 myfwrite8(cheat, h);
128 myfwrite(g_music, 8, 1, h);
131 void G_loadgame(FILE* h) {
132 _2pl = myfread8(h);
133 g_dm = myfread8(h);
134 g_exit = myfread8(h);
135 g_map = myfread8(h);
136 g_time = myfread32(h);
137 dm_pl1p = myfread32(h);
138 dm_pl2p = myfread32(h);
139 dm_pnum = myfread32(h);
140 int i = 0;
141 while (i < dm_pnum) {
142 dm_pos[i].x = myfread32(h);
143 dm_pos[i].y = myfread32(h);
144 dm_pos[i].d = myfread8(h);
145 i += 1;
147 cheat = myfread8(h);
148 myfread(g_music, 8, 1, h);
149 F_loadmus(g_music);
152 int G_load (FILE *h) {
153 switch (blk.t) {
154 case MB_MUSIC:
155 myfread(g_music, 8, 1, h);
156 if (music_random) {
157 F_randmus(g_music);
159 F_loadmus(g_music);
160 return 1;
162 return 0;
165 void load_game(int n) {
166 F_freemus();
167 W_init();
168 F_loadgame(n);
169 set_trans(GS_GAME);
170 pl1.drawst=0xFF;
171 if(_2pl) pl2.drawst=0xFF;
172 BM_remapfld();
173 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
174 BM_mark(&pl1.o,BM_PLR1);
175 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
176 MN_mark();
177 S_startmusic(music_time);
180 void G_start(void) {
181 char s[8];
183 F_freemus();
184 sprintf(s,"MAP%02u",(word)g_map);
185 F_loadmap(s);
186 set_trans(GS_GAME);
187 pl1.drawst=0xFF;
188 if(_2pl) pl2.drawst=0xFF;
189 g_exit=0;
190 itm_rtime=(g_dm)?1092:0;
191 p_immortal=0;PL_JUMP=10;
192 g_time=0;
193 lt_time=1000;
194 lt_force=1;
195 if(!_2pl) pl1.lives=3;
196 BM_remapfld();
197 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
198 BM_mark(&pl1.o,BM_PLR1);
199 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
200 MN_mark();
201 S_startmusic(music_time);
204 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
206 void G_init(void) {
207 int i,j;
208 char s[9];
210 logo("G_init: настройка ресурсов игры ");
211 logo_gas(5,GGAS_TOTAL);
212 telepsnd=Z_getsnd("TELEPT");
213 ltnsnd[0]=Z_getsnd("THUND1");
214 ltnsnd[1]=Z_getsnd("THUND2");
215 DOT_alloc();
216 SMK_alloc();
217 FX_alloc();
218 WP_alloc();
219 IT_alloc();
220 SW_alloc();
221 PL_alloc();
222 MN_alloc();
223 Z_initst();
224 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
225 logo("\n");
226 GM_init();
227 pl1.color=0x70;
228 pl2.color=0x60;
229 g_trans=0;
232 int G_beg_video(void) {
233 /*
234 switch(g_map) {
235 case 3: return A8_start("FALL");
236 case 4: return A8_start("KORIDOR");
237 case 5: return A8_start("SKULL");
238 case 6: return A8_start("TORCHES");
239 case 7: return A8_start("CACO");
240 case 8: return A8_start("DARTS");
241 case 9: return A8_start("FISH");
242 case 10: return A8_start("TRAP");
243 case 11: return A8_start("JAIL");
244 case 12: return A8_start("MMON1");
245 case 13: return A8_start("TOWER");
246 case 14: return A8_start("SAPOG");
247 case 15: return A8_start("SWITCH");
248 case 16: return A8_start("ACCEL");
249 case 17: return A8_start("MEAT");
250 case 18: return A8_start("LEGION");
251 case 19: return A8_start("CLOUDS");
253 */
254 return 0;
258 int G_end_video(void) {
259 /*
260 switch(g_map) {
261 case 1: return A8_start("TRUBA");
262 case 10: return A8_start("GOTCHA");
264 */
265 return 0;
269 void G_act(void) {
270 static byte pcnt=0;
271 /*
272 if(g_trans) {
273 if(g_transt==0) {
274 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
275 V_setscr(fx_scr2);
276 transdraw=1;G_draw();transdraw=0;
277 V_setscr(scrbuf);
279 FX_trans1(g_transt*2);
280 V_copytoscr(0,320,0,200);
281 if(++g_transt>32) {
282 g_trans=0;
284 return;
285 g_trans=0;
287 */ g_trans=0;
290 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
291 if(!A8_nextframe() || lastkey==SDLK_ESCAPE) {
292 if(lastkey==SDLK_ESCAPE) lastkey=0;
293 A8_close();
294 if(g_st==GS_BVIDEO) G_start();
295 else goto inter;
297 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
298 return;
299 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
300 if(!A8_nextframe()) {
301 switch(g_st) {
302 case GS_ENDANIM: g_st=GS_DARKEN;break;
303 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
304 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
305 }g_sttm=0;return;
307 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
308 return;
309 }else if(g_st==GS_DARKEN) {
310 g_st=GS_END2ANIM;A8_start("CREDITS");
311 return;
314 if(GM_act()) return;
318 switch(g_st) {
319 case GS_TITLE: case GS_ENDSCR:
321 return;
322 case GS_INTER:
323 #ifdef DEMO
324 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
325 set_trans(GS_TITLE);
327 #else
328 if(keys[SDLK_SPACE] || keys[SDLK_RETURN] || keys[SDLK_KP_ENTER])//if(keys[0x39] || keys[0x1C] || keys[0x9C])
329 if(!G_beg_video()) G_start(); else {
330 g_st=GS_BVIDEO;F_freemus();
332 #endif
333 return;
337 if(sky_type==2) {
338 if(lt_time>LT_DELAY || lt_force) {
339 if(!(rand()&31) || lt_force) {
340 lt_force=0;
341 lt_time=-LT_HITTIME;
342 lt_type=rand()%2;
343 lt_side=rand()&1;
344 lt_ypos=rand()&31;
345 Z_sound(ltnsnd[rand()&1],128);
347 }else ++lt_time;
349 ++g_time;
350 pl1.hit=0;pl1.hito=-3;
351 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
352 G_code();
354 IT_act();
355 SW_act();
356 if(_2pl) {
357 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
358 else {PL_act(&pl2);PL_act(&pl1);}
359 pcnt^=1;
360 }else PL_act(&pl1);
361 MN_act();
362 if(fld_need_remap) BM_remapfld();
363 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
364 BM_mark(&pl1.o,BM_PLR1);
365 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
366 MN_mark();
367 WP_act();
368 DOT_act();
369 SMK_act();
370 FX_act();
371 if(_2pl) {
372 PL_damage(&pl1);PL_damage(&pl2);
373 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
374 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
375 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
376 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
377 }else{
378 PL_damage(&pl1);
379 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
380 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
382 if(g_exit==1) {
384 if(G_end_video()) {
385 F_freemus();
386 g_st=GS_EVIDEO;
387 return;
390 inter:
391 switch(g_map) {
392 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
393 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
394 default: ++g_map;set_trans(GS_INTER);break;
396 F_freemus();
397 if(g_st==GS_INTER) {
398 F_loadmus("INTERMUS");
399 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
400 S_startmusic(0);
401 }else if(g_exit==2) {
402 switch(g_map) {
403 case 31: g_map=32;set_trans(GS_INTER);break;
404 case 32: g_map=16;set_trans(GS_INTER);break;
405 default: g_map=31;set_trans(GS_INTER);break;
407 F_freemus();
408 F_loadmus("INTERMUS");
409 S_startmusic(0);
412 #ifdef DEMO
413 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
414 #endif
417 void G_respawn_player(player_t *p) {
418 int i;
420 if(dm_pnum==2) {
421 if(p==&pl1) i=dm_pl1p^=1;
422 else i=dm_pl2p^=1;
423 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
424 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
425 return;
427 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
428 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
429 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
430 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);