DEADSOFTWARE

b540223a46028b5beaddaa4265a6722987b8f8aa
[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];
70 static void *telepsnd;
72 int lt_time;
73 int lt_type;
74 int lt_side;
75 int lt_ypos;
76 static int lt_force;
77 static void *ltnsnd[2];
79 int g_trans;
80 static int g_transt;
82 static void set_trans(int st) {
83 switch(g_st) {
84 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
85 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
86 g_st=st;return;
87 }
88 switch(g_st=st) {
89 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
90 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
91 return;
92 }
93 g_trans=1;g_transt=0;
94 }
96 void G_savegame (FILE* h) {
97 myfwrite8(_2pl, h);
98 myfwrite8(g_dm, h);
99 myfwrite8(g_exit, h);
100 myfwrite8(g_map, h);
101 myfwrite32(g_time, h);
102 myfwrite32(dm_pl1p, h);
103 myfwrite32(dm_pl2p, h);
104 myfwrite32(dm_pnum, h);
105 int i = 0;
106 while (i < dm_pnum) {
107 myfwrite32(dm_pos[i].x, h);
108 myfwrite32(dm_pos[i].y, h);
109 myfwrite8(dm_pos[i].d, h);
110 i += 1;
112 myfwrite8(cheat, h);
113 myfwrite(g_music, 8, 1, h);
116 void G_loadgame (FILE* h) {
117 _2pl = myfread8(h);
118 g_dm = myfread8(h);
119 g_exit = myfread8(h);
120 g_map = myfread8(h);
121 g_time = myfread32(h);
122 dm_pl1p = myfread32(h);
123 dm_pl2p = myfread32(h);
124 dm_pnum = myfread32(h);
125 int i = 0;
126 while (i < dm_pnum) {
127 dm_pos[i].x = myfread32(h);
128 dm_pos[i].y = myfread32(h);
129 dm_pos[i].d = myfread8(h);
130 i += 1;
132 cheat = myfread8(h);
133 myfread(g_music, 8, 1, h);
134 F_loadmus(g_music);
137 int G_load (FILE *h) {
138 switch (blk.t) {
139 case MB_MUSIC:
140 myfread(g_music, 8, 1, h);
141 if (music_random) {
142 F_randmus(g_music);
144 F_loadmus(g_music);
145 return 1;
147 return 0;
150 void load_game (int n) {
151 F_freemus();
152 W_init();
153 F_loadgame(n);
154 set_trans(GS_GAME);
155 pl1.drawst=0xFF;
156 if(_2pl) pl2.drawst=0xFF;
157 BM_remapfld();
158 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
159 BM_mark(&pl1.o,BM_PLR1);
160 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
161 MN_mark();
162 S_startmusic(music_time);
165 void G_start (void) {
166 char s[8];
168 F_freemus();
169 sprintf(s,"MAP%02u",(word)g_map);
170 F_loadmap(s);
171 set_trans(GS_GAME);
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 ltnsnd[0]=Z_getsnd("THUND1");
199 ltnsnd[1]=Z_getsnd("THUND2");
200 DOT_alloc();
201 SMK_alloc();
202 FX_alloc();
203 WP_alloc();
204 IT_alloc();
205 SW_alloc();
206 PL_alloc();
207 MN_alloc();
208 Z_initst();
209 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
210 logo("\n");
211 GM_init();
212 pl1.color=0x70;
213 pl2.color=0x60;
214 g_trans=0;
217 static int G_beg_video (void) {
218 /*
219 switch(g_map) {
220 case 3: return A8_start("FALL");
221 case 4: return A8_start("KORIDOR");
222 case 5: return A8_start("SKULL");
223 case 6: return A8_start("TORCHES");
224 case 7: return A8_start("CACO");
225 case 8: return A8_start("DARTS");
226 case 9: return A8_start("FISH");
227 case 10: return A8_start("TRAP");
228 case 11: return A8_start("JAIL");
229 case 12: return A8_start("MMON1");
230 case 13: return A8_start("TOWER");
231 case 14: return A8_start("SAPOG");
232 case 15: return A8_start("SWITCH");
233 case 16: return A8_start("ACCEL");
234 case 17: return A8_start("MEAT");
235 case 18: return A8_start("LEGION");
236 case 19: return A8_start("CLOUDS");
238 */
239 return 0;
243 static int G_end_video (void) {
244 /*
245 switch(g_map) {
246 case 1: return A8_start("TRUBA");
247 case 10: return A8_start("GOTCHA");
249 */
250 return 0;
253 void G_act (void) {
254 static byte pcnt=0;
255 /*
256 if(g_trans) {
257 if(g_transt==0) {
258 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
259 V_setscr(fx_scr2);
260 transdraw=1;G_draw();transdraw=0;
261 V_setscr(scrbuf);
263 FX_trans1(g_transt*2);
264 V_copytoscr(0,320,0,200);
265 if(++g_transt>32) {
266 g_trans=0;
268 return;
269 g_trans=0;
271 */ g_trans=0;
274 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
275 if (!A8_nextframe() || lastkey == KEY_ESCAPE) {
276 if (lastkey == KEY_ESCAPE) lastkey = KEY_UNKNOWN;
277 A8_close();
278 if(g_st==GS_BVIDEO) G_start();
279 else goto inter;
281 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
282 return;
283 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
284 if(!A8_nextframe()) {
285 switch(g_st) {
286 case GS_ENDANIM: g_st=GS_DARKEN;break;
287 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
288 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
290 return;
292 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
293 return;
294 }else if(g_st==GS_DARKEN) {
295 g_st=GS_END2ANIM;A8_start("CREDITS");
296 return;
299 if(GM_act()) return;
303 switch(g_st) {
304 case GS_TITLE: case GS_ENDSCR:
306 return;
307 case GS_INTER:
308 #ifdef DEMO
309 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
310 set_trans(GS_TITLE);
312 #else
313 if (I_pressed(KEY_SPACE) || I_pressed(KEY_RETURN) || I_pressed(KEY_KP_ENTER)) {
314 if (!G_beg_video()) {
315 G_start();
316 } else {
317 g_st = GS_BVIDEO;
318 F_freemus();
321 #endif
322 return;
326 if(sky_type==2) {
327 if(lt_time>LT_DELAY || lt_force) {
328 if(!(rand()&31) || lt_force) {
329 lt_force=0;
330 lt_time=-LT_HITTIME;
331 lt_type=rand()%2;
332 lt_side=rand()&1;
333 lt_ypos=rand()&31;
334 Z_sound(ltnsnd[rand()&1],128);
336 }else ++lt_time;
338 ++g_time;
339 pl1.hit=0;pl1.hito=-3;
340 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
341 G_code();
343 IT_act();
344 SW_act();
345 if(_2pl) {
346 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
347 else {PL_act(&pl2);PL_act(&pl1);}
348 pcnt^=1;
349 }else PL_act(&pl1);
350 MN_act();
351 if(fld_need_remap) BM_remapfld();
352 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
353 BM_mark(&pl1.o,BM_PLR1);
354 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
355 MN_mark();
356 WP_act();
357 DOT_act();
358 SMK_act();
359 FX_act();
360 if(_2pl) {
361 PL_damage(&pl1);PL_damage(&pl2);
362 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
363 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
364 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
365 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
366 }else{
367 PL_damage(&pl1);
368 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
369 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
371 if(g_exit==1) {
373 if(G_end_video()) {
374 F_freemus();
375 g_st=GS_EVIDEO;
376 return;
379 inter:
380 switch(g_map) {
381 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
382 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
383 default: ++g_map;set_trans(GS_INTER);break;
385 F_freemus();
386 if(g_st==GS_INTER) {
387 F_loadmus("INTERMUS");
388 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
389 S_startmusic(0);
390 }else if(g_exit==2) {
391 switch(g_map) {
392 case 31: g_map=32;set_trans(GS_INTER);break;
393 case 32: g_map=16;set_trans(GS_INTER);break;
394 default: g_map=31;set_trans(GS_INTER);break;
396 F_freemus();
397 F_loadmus("INTERMUS");
398 S_startmusic(0);
401 #ifdef DEMO
402 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
403 #endif
406 void G_respawn_player (player_t *p) {
407 int i;
409 if(dm_pnum==2) {
410 if(p==&pl1) i=dm_pl1p^=1;
411 else i=dm_pl2p^=1;
412 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
413 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
414 return;
416 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
417 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
418 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
419 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);