DEADSOFTWARE

save: move save code to separated file
[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"
51 #include "save.h"
53 #define LT_DELAY 8
54 #define LT_HITTIME 6
56 #define GETIME 1092
58 byte transdraw;
59 byte _2pl;
60 byte g_dm;
61 byte g_st = GS_TITLE;
62 byte g_exit;
63 byte g_map = 1;
64 char g_music[8] = "MENU";
65 dword g_time;
66 int dm_pnum;
67 int dm_pl1p;
68 int dm_pl2p;
69 pos_t dm_pos[100];
70 byte cheat;
72 static void *telepsnd;
74 int lt_time;
75 int lt_type;
76 int lt_side;
77 int lt_ypos;
78 static int lt_force;
79 static void *ltnsnd[2];
81 int g_trans;
82 static int g_transt;
84 static void set_trans(int st) {
85 switch(g_st) {
86 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
87 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
88 g_st=st;return;
89 }
90 switch(g_st=st) {
91 case GS_ENDANIM: case GS_END2ANIM: case GS_DARKEN:
92 case GS_BVIDEO: case GS_EVIDEO: case GS_END3ANIM:
93 return;
94 }
95 g_trans=1;g_transt=0;
96 }
98 int G_load (FILE *h) {
99 switch (blk.t) {
100 case MB_MUSIC:
101 myfread(g_music, 8, 1, h);
102 if (music_random) {
103 F_randmus(g_music);
105 F_loadmus(g_music);
106 return 1;
108 return 0;
111 void load_game (int n) {
112 F_freemus();
113 W_init();
114 F_loadgame(n);
115 set_trans(GS_GAME);
116 pl1.drawst=0xFF;
117 if(_2pl) pl2.drawst=0xFF;
118 BM_remapfld();
119 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
120 BM_mark(&pl1.o,BM_PLR1);
121 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
122 MN_mark();
123 S_startmusic(music_time);
126 void G_start (void) {
127 char s[8];
129 F_freemus();
130 sprintf(s,"MAP%02u",(word)g_map);
131 F_loadmap(s);
132 set_trans(GS_GAME);
133 pl1.drawst=0xFF;
134 if(_2pl) pl2.drawst=0xFF;
135 g_exit=0;
136 itm_rtime=(g_dm)?1092:0;
137 p_immortal=0;PL_JUMP=10;
138 g_time=0;
139 lt_time=1000;
140 lt_force=1;
141 if(!_2pl) pl1.lives=3;
142 BM_remapfld();
143 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
144 BM_mark(&pl1.o,BM_PLR1);
145 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
146 MN_mark();
147 S_startmusic(music_time);
150 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
152 void G_init (void) {
153 int i,j;
154 char s[9];
156 logo("G_init: настройка ресурсов игры ");
157 logo_gas(5,GGAS_TOTAL);
158 telepsnd=Z_getsnd("TELEPT");
159 ltnsnd[0]=Z_getsnd("THUND1");
160 ltnsnd[1]=Z_getsnd("THUND2");
161 DOT_alloc();
162 SMK_alloc();
163 FX_alloc();
164 WP_alloc();
165 IT_alloc();
166 SW_alloc();
167 PL_alloc();
168 MN_alloc();
169 Z_initst();
170 logo_gas(GGAS_TOTAL,GGAS_TOTAL);
171 logo("\n");
172 GM_init();
173 pl1.color=0x70;
174 pl2.color=0x60;
175 g_trans=0;
178 static int G_beg_video (void) {
179 /*
180 switch(g_map) {
181 case 3: return A8_start("FALL");
182 case 4: return A8_start("KORIDOR");
183 case 5: return A8_start("SKULL");
184 case 6: return A8_start("TORCHES");
185 case 7: return A8_start("CACO");
186 case 8: return A8_start("DARTS");
187 case 9: return A8_start("FISH");
188 case 10: return A8_start("TRAP");
189 case 11: return A8_start("JAIL");
190 case 12: return A8_start("MMON1");
191 case 13: return A8_start("TOWER");
192 case 14: return A8_start("SAPOG");
193 case 15: return A8_start("SWITCH");
194 case 16: return A8_start("ACCEL");
195 case 17: return A8_start("MEAT");
196 case 18: return A8_start("LEGION");
197 case 19: return A8_start("CLOUDS");
199 */
200 return 0;
204 static int G_end_video (void) {
205 /*
206 switch(g_map) {
207 case 1: return A8_start("TRUBA");
208 case 10: return A8_start("GOTCHA");
210 */
211 return 0;
214 void G_act (void) {
215 static byte pcnt=0;
216 /*
217 if(g_trans) {
218 if(g_transt==0) {
219 V_setscr(NULL);memcpy(fx_scr1,scra,64000);
220 V_setscr(fx_scr2);
221 transdraw=1;G_draw();transdraw=0;
222 V_setscr(scrbuf);
224 FX_trans1(g_transt*2);
225 V_copytoscr(0,320,0,200);
226 if(++g_transt>32) {
227 g_trans=0;
229 return;
230 g_trans=0;
232 */ g_trans=0;
235 if(g_st==GS_BVIDEO || g_st==GS_EVIDEO) {
236 if (!A8_nextframe() || lastkey == KEY_ESCAPE) {
237 if (lastkey == KEY_ESCAPE) lastkey = KEY_UNKNOWN;
238 A8_close();
239 if(g_st==GS_BVIDEO) G_start();
240 else goto inter;
242 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
243 return;
244 }else if(g_st==GS_ENDANIM || g_st==GS_END2ANIM || g_st==GS_END3ANIM) {
245 if(!A8_nextframe()) {
246 switch(g_st) {
247 case GS_ENDANIM: g_st=GS_DARKEN;break;
248 case GS_END2ANIM: g_st=GS_END3ANIM;A8_start("KONEC");break;
249 case GS_END3ANIM: g_st=GS_ENDSCR;lastkey=0;break;
251 return;
253 // V_copytoscr(0,SCRW,0,SCRH);//V_copytoscr(0,320,0,200);
254 return;
255 }else if(g_st==GS_DARKEN) {
256 g_st=GS_END2ANIM;A8_start("CREDITS");
257 return;
260 if(GM_act()) return;
264 switch(g_st) {
265 case GS_TITLE: case GS_ENDSCR:
267 return;
268 case GS_INTER:
269 #ifdef DEMO
270 if(keys[0x39] || keys[0x1C] || keys[0x9C]) {
271 set_trans(GS_TITLE);
273 #else
274 if (I_pressed(KEY_SPACE) || I_pressed(KEY_RETURN) || I_pressed(KEY_KP_ENTER)) {
275 if (!G_beg_video()) {
276 G_start();
277 } else {
278 g_st = GS_BVIDEO;
279 F_freemus();
282 #endif
283 return;
287 if(sky_type==2) {
288 if(lt_time>LT_DELAY || lt_force) {
289 if(!(rand()&31) || lt_force) {
290 lt_force=0;
291 lt_time=-LT_HITTIME;
292 lt_type=rand()%2;
293 lt_side=rand()&1;
294 lt_ypos=rand()&31;
295 Z_sound(ltnsnd[rand()&1],128);
297 }else ++lt_time;
299 ++g_time;
300 pl1.hit=0;pl1.hito=-3;
301 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
302 G_code();
304 IT_act();
305 SW_act();
306 if(_2pl) {
307 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
308 else {PL_act(&pl2);PL_act(&pl1);}
309 pcnt^=1;
310 }else PL_act(&pl1);
311 MN_act();
312 if(fld_need_remap) BM_remapfld();
313 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
314 BM_mark(&pl1.o,BM_PLR1);
315 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
316 MN_mark();
317 WP_act();
318 DOT_act();
319 SMK_act();
320 FX_act();
321 if(_2pl) {
322 PL_damage(&pl1);PL_damage(&pl2);
323 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
324 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
325 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
326 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
327 }else{
328 PL_damage(&pl1);
329 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
330 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
332 if(g_exit==1) {
334 if(G_end_video()) {
335 F_freemus();
336 g_st=GS_EVIDEO;
337 return;
340 inter:
341 switch(g_map) {
342 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
343 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
344 default: ++g_map;set_trans(GS_INTER);break;
346 F_freemus();
347 if(g_st==GS_INTER) {
348 F_loadmus("INTERMUS");
349 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
350 S_startmusic(0);
351 }else if(g_exit==2) {
352 switch(g_map) {
353 case 31: g_map=32;set_trans(GS_INTER);break;
354 case 32: g_map=16;set_trans(GS_INTER);break;
355 default: g_map=31;set_trans(GS_INTER);break;
357 F_freemus();
358 F_loadmus("INTERMUS");
359 S_startmusic(0);
362 #ifdef DEMO
363 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
364 #endif
367 void G_respawn_player (player_t *p) {
368 int i;
370 if(dm_pnum==2) {
371 if(p==&pl1) i=dm_pl1p^=1;
372 else i=dm_pl2p^=1;
373 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
374 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
375 return;
377 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
378 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
379 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
380 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);