DEADSOFTWARE

headers describes that c-files implements
[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"
45 #include "game.h"
46 #include "config.h"
47 #include "music.h"
48 #include "a8.h"
49 #include "error.h"
51 #include <SDL_keysym.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];
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==SDLK_ESCAPE) {
277 if(lastkey==SDLK_ESCAPE) lastkey=0;
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(keys[SDLK_SPACE] || keys[SDLK_RETURN] || keys[SDLK_KP_ENTER])//if(keys[0x39] || keys[0x1C] || keys[0x9C])
315 if(!G_beg_video()) G_start(); else {
316 g_st=GS_BVIDEO;F_freemus();
318 #endif
319 return;
323 if(sky_type==2) {
324 if(lt_time>LT_DELAY || lt_force) {
325 if(!(rand()&31) || lt_force) {
326 lt_force=0;
327 lt_time=-LT_HITTIME;
328 lt_type=rand()%2;
329 lt_side=rand()&1;
330 lt_ypos=rand()&31;
331 Z_sound(ltnsnd[rand()&1],128);
333 }else ++lt_time;
335 ++g_time;
336 pl1.hit=0;pl1.hito=-3;
337 if(_2pl) {pl2.hit=0;pl2.hito=-3;}
338 G_code();
340 IT_act();
341 SW_act();
342 if(_2pl) {
343 if(pcnt) {PL_act(&pl1);PL_act(&pl2);}
344 else {PL_act(&pl2);PL_act(&pl1);}
345 pcnt^=1;
346 }else PL_act(&pl1);
347 MN_act();
348 if(fld_need_remap) BM_remapfld();
349 BM_clear(BM_PLR1|BM_PLR2|BM_MONSTER);
350 BM_mark(&pl1.o,BM_PLR1);
351 if(_2pl) BM_mark(&pl2.o,BM_PLR2);
352 MN_mark();
353 WP_act();
354 DOT_act();
355 SMK_act();
356 FX_act();
357 if(_2pl) {
358 PL_damage(&pl1);PL_damage(&pl2);
359 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
360 if(!(pl2.f&PLF_PNSND) && pl2.pain) PL_cry(&pl2);
361 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
362 if((pl2.pain-=5) < 0) {pl2.pain=0;pl2.f&=(0xFFFF-PLF_PNSND);}
363 }else{
364 PL_damage(&pl1);
365 if(!(pl1.f&PLF_PNSND) && pl1.pain) PL_cry(&pl1);
366 if((pl1.pain-=5) < 0) {pl1.pain=0;pl1.f&=(0xFFFF-PLF_PNSND);}
368 if(g_exit==1) {
370 if(G_end_video()) {
371 F_freemus();
372 g_st=GS_EVIDEO;
373 return;
376 inter:
377 switch(g_map) {
378 case 19: g_st=GS_ENDANIM;A8_start("FINAL");break;
379 case 31: case 32: g_map=16;set_trans(GS_INTER);break;
380 default: ++g_map;set_trans(GS_INTER);break;
382 F_freemus();
383 if(g_st==GS_INTER) {
384 F_loadmus("INTERMUS");
385 }else {F_loadmus("\x8a\x8e\x8d\x85\x96\x0");if(mus_vol>0) {S_volumemusic(128);} }
386 S_startmusic(0);
387 }else if(g_exit==2) {
388 switch(g_map) {
389 case 31: g_map=32;set_trans(GS_INTER);break;
390 case 32: g_map=16;set_trans(GS_INTER);break;
391 default: g_map=31;set_trans(GS_INTER);break;
393 F_freemus();
394 F_loadmus("INTERMUS");
395 S_startmusic(0);
398 #ifdef DEMO
399 if(g_dm && g_time>10920) {set_trans(GS_INTER);}
400 #endif
403 void G_respawn_player (player_t *p) {
404 int i;
406 if(dm_pnum==2) {
407 if(p==&pl1) i=dm_pl1p^=1;
408 else i=dm_pl2p^=1;
409 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
410 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);
411 return;
413 do{i=myrand(dm_pnum);}while(i==dm_pl1p || i==dm_pl2p);
414 p->o.x=dm_pos[i].x;p->o.y=dm_pos[i].y;p->d=dm_pos[i].d;
415 if(p==&pl1) dm_pl1p=i; else dm_pl2p=i;
416 FX_tfog(dm_pos[i].x,dm_pos[i].y);Z_sound(telepsnd,128);