DEADSOFTWARE

eba57b827eb251a2447bf4872c6c8b22a2113c1e
[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 "sound.h"
30 #include "view.h"
31 #include "bmap.h"
32 #include "fx.h"
33 #include "switch.h"
34 #include "weapons.h"
35 #include "items.h"
36 #include "dots.h"
37 #include "smoke.h"
38 #include "player.h"
39 #include "monster.h"
40 #include "menu.h"
41 #include "misc.h"
42 #include "map.h"
43 #include "my.h"
44 #include "game.h"
45 #include "config.h"
46 #include "music.h"
47 #include "a8.h"
48 #include "error.h"
49 #include "input.h"
52 #define LT_DELAY 8
53 #define LT_HITTIME 6
55 #define GETIME 1092
57 byte transdraw;
58 byte _2pl;
59 byte g_dm;
60 byte g_st = GS_TITLE;
61 byte g_exit;
62 byte g_map = 1;
63 char g_music[8] = "MENU";
64 dword g_time;
65 int dm_pnum;
66 int dm_pl1p;
67 int dm_pl2p;
68 pos_t dm_pos[100];
69 byte cheat;
71 static void *telepsnd;
73 int lt_time;
74 int lt_type;
75 int lt_side;
76 int lt_ypos;
77 static int lt_force;
78 static void *ltnsnd[2];
80 int g_trans;
81 static int g_transt;
83 static void set_trans(int st) {
84 switch(g_st) {
85 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
86 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
87 g_st=st;return;
88 }
89 switch(g_st=st) {
90 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
91 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
92 return;
93 }
94 g_trans=1;g_transt=0;
95 }
97 void G_savegame (FILE* h) {
98 myfwrite8(_2pl, h);
99 myfwrite8(g_dm, h);
100 myfwrite8(g_exit, h);
101 myfwrite8(g_map, h);
102 myfwrite32(g_time, h);
103 myfwrite32(dm_pl1p, h);
104 myfwrite32(dm_pl2p, h);
105 myfwrite32(dm_pnum, h);
106 int i = 0;
107 while (i < dm_pnum) {
108 myfwrite32(dm_pos[i].x, h);
109 myfwrite32(dm_pos[i].y, h);
110 myfwrite8(dm_pos[i].d, h);
111 i += 1;
113 myfwrite8(cheat, h);
114 myfwrite(g_music, 8, 1, h);
117 void G_loadgame (FILE* h) {
118 _2pl = myfread8(h);
119 g_dm = myfread8(h);
120 g_exit = myfread8(h);
121 g_map = myfread8(h);
122 g_time = myfread32(h);
123 dm_pl1p = myfread32(h);
124 dm_pl2p = myfread32(h);
125 dm_pnum = myfread32(h);
126 int i = 0;
127 while (i < dm_pnum) {
128 dm_pos[i].x = myfread32(h);
129 dm_pos[i].y = myfread32(h);
130 dm_pos[i].d = myfread8(h);
131 i += 1;
133 cheat = myfread8(h);
134 myfread(g_music, 8, 1, h);
135 F_loadmus(g_music);
138 int G_load (FILE *h) {
139 switch (blk.t) {
140 case MB_MUSIC:
141 myfread(g_music, 8, 1, h);
142 if (music_random) {
143 F_randmus(g_music);
145 F_loadmus(g_music);
146 return 1;
148 return 0;
151 void load_game (int n) {
152 F_freemus();
153 W_init();
154 F_loadgame(n);
155 set_trans(GS_GAME);
156 pl1.drawst=0xFF;
157 if(_2pl) pl2.drawst=0xFF;
158 BM_remapfld();
159 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
160 BM_mark(&pl1.o,BM_PLR1);
161 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
162 MN_mark();
163 S_startmusic(music_time);
166 void G_start (void) {
167 char s[8];
169 F_freemus();
170 sprintf(s,"MAP%02u",(word)g_map);
171 F_loadmap(s);
172 set_trans(GS_GAME);
173 pl1.drawst=0xFF;
174 if(_2pl) pl2.drawst=0xFF;
175 g_exit=0;
176 itm_rtime=(g_dm)?1092:0;
177 p_immortal=0;PL_JUMP=10;
178 g_time=0;
179 lt_time=1000;
180 lt_force=1;
181 if(!_2pl) pl1.lives=3;
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 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
192 void G_init (void) {
193 int i,j;
194 char s[9];
196 logo("G_init: настройка ресурсов игры ");
197 logo_gas(5,GGAS_TOTAL);
198 telepsnd=Z_getsnd("TELEPT");
199 ltnsnd[0]=Z_getsnd("THUND1");
200 ltnsnd[1]=Z_getsnd("THUND2");
201 DOT_alloc();
202 SMK_alloc();
203 FX_alloc();
204 WP_alloc();
205 IT_alloc();
206 SW_alloc();
207 PL_alloc();
208 MN_alloc();
209 Z_initst();
210 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
211 logo("\n");
212 GM_init();
213 pl1.color=0x70;
214 pl2.color=0x60;
215 g_trans=0;
218 static int G_beg_video (void) {
219 /*
220 switch(g_map) {
221 case 3: return A8_start("FALL");
222 case 4: return A8_start("KORIDOR");
223 case 5: return A8_start("SKULL");
224 case 6: return A8_start("TORCHES");
225 case 7: return A8_start("CACO");
226 case 8: return A8_start("DARTS");
227 case 9: return A8_start("FISH");
228 case 10: return A8_start("TRAP");
229 case 11: return A8_start("JAIL");
230 case 12: return A8_start("MMON1");
231 case 13: return A8_start("TOWER");
232 case 14: return A8_start("SAPOG");
233 case 15: return A8_start("SWITCH");
234 case 16: return A8_start("ACCEL");
235 case 17: return A8_start("MEAT");
236 case 18: return A8_start("LEGION");
237 case 19: return A8_start("CLOUDS");
239 */
240 return 0;
244 static int G_end_video (void) {
245 /*
246 switch(g_map) {
247 case 1: return A8_start("TRUBA");
248 case 10: return A8_start("GOTCHA");
250 */
251 return 0;
254 void G_act (void) {
255 static byte pcnt=0;
256 /*
257 if(g_trans) {
258 if(g_transt==0) {
259 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
260 V_setscr(fx_scr2);
261 transdraw=1;G_draw();transdraw=0;
262 V_setscr(scrbuf);
264 FX_trans1(g_transt*2);
265 V_copytoscr(0,320,0,200);
266 if(++g_transt>32) {
267 g_trans=0;
269 return;
270 g_trans=0;
272 */ g_trans=0;
275 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
276 if (!A8_nextframe() || lastkey == KEY_ESCAPE) {
277 if (lastkey == KEY_ESCAPE) lastkey = KEY_UNKNOWN;
278 A8_close();
279 if(g_st==GS_BVIDEO) G_start();
280 else goto inter;
282 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
283 return;
284 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
285 if(!A8_nextframe()) {
286 switch(g_st) {
287 case GS_ENDANIM: g_st=GS_DARKEN;break;
288 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
289 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
291 return;
293 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
294 return;
295 }else if(g_st==GS_DARKEN) {
296 g_st=GS_END2ANIM;A8_start("CREDITS");
297 return;
300 if(GM_act()) return;
304 switch(g_st) {
305 case GS_TITLE: case GS_ENDSCR:
307 return;
308 case GS_INTER:
309 #ifdef DEMO
310 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
311 set_trans(GS_TITLE);
313 #else
314 if (I_pressed(KEY_SPACE) || I_pressed(KEY_RETURN) || I_pressed(KEY_KP_ENTER)) {
315 if (!G_beg_video()) {
316 G_start();
317 } else {
318 g_st = GS_BVIDEO;
319 F_freemus();
322 #endif
323 return;
327 if(sky_type==2) {
328 if(lt_time>LT_DELAY || lt_force) {
329 if(!(rand()&31) || lt_force) {
330 lt_force=0;
331 lt_time=-LT_HITTIME;
332 lt_type=rand()%2;
333 lt_side=rand()&1;
334 lt_ypos=rand()&31;
335 Z_sound(ltnsnd[rand()&1],128);
337 }else ++lt_time;
339 ++g_time;
340 pl1.hit=0;pl1.hito=-3;
341 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
342 G_code();
344 IT_act();
345 SW_act();
346 if(_2pl) {
347 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
348 else {PL_act(&pl2);PL_act(&pl1);}
349 pcnt^=1;
350 }else PL_act(&pl1);
351 MN_act();
352 if(fld_need_remap) BM_remapfld();
353 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
354 BM_mark(&pl1.o,BM_PLR1);
355 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
356 MN_mark();
357 WP_act();
358 DOT_act();
359 SMK_act();
360 FX_act();
361 if(_2pl) {
362 PL_damage(&pl1);PL_damage(&pl2);
363 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
364 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
365 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
366 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
367 }else{
368 PL_damage(&pl1);
369 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
370 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
372 if(g_exit==1) {
374 if(G_end_video()) {
375 F_freemus();
376 g_st=GS_EVIDEO;
377 return;
380 inter:
381 switch(g_map) {
382 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
383 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
384 default: ++g_map;set_trans(GS_INTER);break;
386 F_freemus();
387 if(g_st==GS_INTER) {
388 F_loadmus("INTERMUS");
389 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
390 S_startmusic(0);
391 }else if(g_exit==2) {
392 switch(g_map) {
393 case 31: g_map=32;set_trans(GS_INTER);break;
394 case 32: g_map=16;set_trans(GS_INTER);break;
395 default: g_map=31;set_trans(GS_INTER);break;
397 F_freemus();
398 F_loadmus("INTERMUS");
399 S_startmusic(0);
402 #ifdef DEMO
403 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
404 #endif
407 void G_respawn_player (player_t *p) {
408 int i;
410 if(dm_pnum==2) {
411 if(p==&pl1) i=dm_pl1p^=1;
412 else i=dm_pl2p^=1;
413 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
414 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
415 return;
417 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
418 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
419 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
420 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);