DEADSOFTWARE

files: move resource manager to system drivers
[flatwaifu.git] / src / player.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include <stdlib.h>
20 #include <string.h>
21 #include "view.h"
22 #include "dots.h"
23 #include "smoke.h"
24 #include "weapons.h"
25 #include "monster.h"
26 #include "fx.h"
27 #include "items.h"
28 #include "switch.h"
29 #include "player.h"
30 #include "misc.h"
31 #include "game.h"
32 #include "input.h"
34 #define PL_RAD 8
35 #define PL_HT 26
37 #define PL_SWUP 4
38 #define PL_FLYUP 4
40 #define PL_AQUA_AIR 1091
42 byte p_immortal;
43 byte p_fly;
45 int PL_JUMP=10;
46 int PL_RUN=8;
48 static int wp_it[11]={0,I_CSAW,0,I_SGUN,I_SGUN2,I_MGUN,I_LAUN,I_PLAS,I_BFG,I_GUN2,0};
50 enum{STAND,GO,DIE,SLOP,DEAD,MESS,OUT,FALL};
52 typedef void fire_f(int,int,int,int,int);
54 player_t pl1;
55 player_t pl2;
56 static int aitime;
57 static void *aisnd[3];
58 static void *pdsnd[5];
60 static void *snd[11];
61 byte plr_goanim[]="BDACDA";
62 byte plr_dieanim[]="HHHHIIIIJJJJKKKKLLLLMMMM";
63 byte plr_slopanim[]="OOPPQQRRSSTTUUVVWW";
65 static int nonz (int a) {
66 return a ? a : 1;
67 }
69 static int firediry(player_t *p) {
70 if(p->f&PLF_UP) return -42;
71 if(p->f&PLF_DOWN) return 19;
72 return 0;
73 }
75 static void fire(player_t *p) {
76 static fire_f *ff[11]={
77 WP_pistol,WP_pistol,WP_pistol,WP_shotgun,WP_dshotgun,
78 WP_mgun,WP_rocket,WP_plasma,WP_bfgshot,WP_shotgun,WP_pistol};
79 static int ft[11]={5,2,6,18,36,2,12,2,0,2,1};
81 if(p->cwpn) return;
82 if(p->wpn==8) {
83 if(!p->fire)
84 if(I_pressed(p->kf) && p->cell>=40)
85 {Z_sound(snd[5],128);p->fire=21;p->cell-=40;p->drawst|=PL_DRAWWPN;return;}
86 else return;
87 if(p->fire==1) p->cwpn=12;
88 else return;
89 }else if(p->wpn==1) {
90 if(!p->csnd) {
91 if(!I_pressed(p->kf)) {Z_sound(snd[7],128);p->csnd=13;return;}
92 }
93 if(I_pressed(p->kf) && !p->fire) {
94 p->fire=2;
95 WP_chainsaw(p->o.x+((p->d)?4:-4),p->o.y,(g_dm)?9:3,p->id);
96 if(!p->csnd) {Z_sound(snd[8],128);p->csnd=29;}
97 }return;
98 }else if(p->fire) return;
99 if(I_pressed(p->kf) || p->wpn==8) {
100 switch(p->wpn) {
101 case 2: case 5:
102 if(!p->ammo) return;
103 --p->ammo;p->drawst|=PL_DRAWWPN;break;
104 case 3: case 9:
105 if(!p->shel) return;
106 --p->shel;p->drawst|=PL_DRAWWPN;break;
107 case 4:
108 if(p->shel<2) return;
109 p->shel-=2;p->drawst|=PL_DRAWWPN;break;
110 case 6:
111 if(!p->rock) return;
112 --p->rock;p->drawst|=PL_DRAWWPN;break;
113 case 7:
114 if(!p->cell) return;
115 --p->cell;p->drawst|=PL_DRAWWPN;break;
116 case 10:
117 if(!p->fuel) return;
118 --p->fuel;p->drawst|=PL_DRAWWPN;break;
120 if(p->wpn==10)
121 WP_ognemet(p->o.x,p->o.y-15,p->o.x+((p->d)?30:-30),p->o.y-15+firediry(p),
122 p->o.xv+p->o.vx,p->o.yv+p->o.vy,p->id);
123 else if(p->wpn>=1) ff[p->wpn] (p->o.x,p->o.y-15,p->o.x+((p->d)?30:-30),
124 p->o.y-15+firediry(p),p->id);
125 else WP_punch(p->o.x+((p->d)?4:-4),p->o.y,3,p->id);
126 p->fire=ft[p->wpn];
127 if(p->wpn>=2) p->f|=PLF_FIRE;
131 static void chgwpn(player_t *p) {
132 if(p->cwpn) return;
133 if(p->fire && p->wpn!=1) return;
134 if(I_pressed(p->kwl)) {
135 do{ if(--p->wpn<0) p->wpn=10; }while(!(p->wpns&(1<<p->wpn)));
136 p->cwpn=3;
137 }else if(I_pressed(p->kwr)) {
138 do{ if(++p->wpn>10) p->wpn=0; }while(!(p->wpns&(1<<p->wpn)));
139 p->cwpn=3;
141 if(p->cwpn) {
142 p->drawst|=PL_DRAWWPN;p->fire=0;
143 if(p->wpn==1) Z_sound(snd[6],128);
147 static void jump(player_t *p,int st) {
148 if(Z_canbreathe(p->o.x,p->o.y,p->o.r,p->o.h)) {
149 if(p->air<PL_AIR) {p->air=PL_AIR;p->drawst|=PL_DRAWAIR;}
150 }else {
151 if(--p->air < -9) {
152 p->air=0;
153 PL_hit(p,10,-3,HIT_WATER);
154 }else if((p->air&31)==0) {
155 FX_bubble(p->o.x,p->o.y-20,0,0,5);
157 p->drawst|=PL_DRAWAIR;
159 if(I_pressed(p->kj)) {
160 if(p_fly) {
161 p->o.yv=-PL_FLYUP;
162 }else{
163 if(Z_canstand(p->o.x,p->o.y,p->o.r)) p->o.yv=-PL_JUMP;
164 else if(st&Z_INWATER) p->o.yv=-PL_SWUP;
169 int PL_isdead (player_t *p) {
170 switch(p->st) {
171 case DEAD: case MESS:
172 case OUT:
173 return 1;
175 return 0;
178 void PL_init (void) {
179 pl1.id = -1;
180 pl2.id = -2;
181 p_immortal = 0;
182 PL_JUMP = 10;
183 PL_RUN = 8;
184 aitime = 0;
187 void PL_alloc(void) {
188 int i,j;
189 static char nm[][6]={
190 "OOF",
191 "PLPAIN",
192 "PLDETH",
193 "SLOP",
194 "PDIEHI",
195 "BFG",
196 "SAWUP",
197 "SAWIDL",
198 "SAWFUL",
199 "SAWHIT",
200 "PLFALL"
201 };
202 char s[6];
203 for(i=0;i<11;++i) snd[i]=Z_getsnd(nm[i]);
204 memcpy(s,"AIx",4);
205 for(i=0;i<3;++i) {
206 s[2]=i+'1';
207 aisnd[i]=Z_getsnd(s);
209 memcpy(s,"PLDTHx",6);
210 for(i=0;i<5;++i) {
211 s[5]=i+'1';
212 pdsnd[i]=Z_getsnd(s);
216 static void PL_restore(player_t *p) {
217 p->o.xv=p->o.yv=p->o.vx=p->o.vy=0;
218 p->o.r=PL_RAD;p->o.h=PL_HT;
219 p->pain=0;
220 p->invl=p->suit=0;
221 switch(p->st) {
222 case DEAD: case MESS: case OUT:
223 case DIE: case SLOP: case FALL:
224 p->life=100;p->armor=0;p->air=PL_AIR;
225 p->wpns=5;
226 p->wpn=2;
227 p->ammo=50;p->fuel=p->shel=p->rock=p->cell=0;
228 p->amul=1;
230 p->st=STAND;
231 p->fire=p->cwpn=p->csnd=0;
232 p->f=0;
233 p->drawst=0xFF;
234 p->looky=0;
235 p->keys=(g_dm)?0x70:0;
238 void PL_reset (void) {
239 pl1.st=pl2.st=DEAD;
240 pl1.frag=pl2.frag=0;
243 void PL_spawn (player_t *p,int x,int y,char d) {
244 PL_restore(p);
245 p->o.x=x;p->o.y=y;p->d=d;
246 p->kills=p->secrets=0;
249 int PL_hit (player_t *p,int d,int o,int t) {
250 if(!d) return 0;
251 switch(p->st) {
252 case DIE: case SLOP:
253 case DEAD: case MESS:
254 case OUT: case FALL:
255 return 0;
257 if(t==HIT_TRAP) {if(!p_immortal) {p->armor=0;p->life=-100;}}
258 else if(t!=HIT_ROCKET && t!=HIT_ELECTRO) {
259 if(p->id==-1) {if(o==-1) return 0;}
260 else if(o==-2) return 0;
262 if(t!=HIT_WATER && t!=HIT_ELECTRO)
263 DOT_blood(p->o.x,p->o.y-15,hit_xv,hit_yv,d*2);
264 else if(t==HIT_WATER) FX_bubble(p->o.x,p->o.y-20,0,0,d/2);
265 if(p_immortal || p->invl) return 1;
266 p->hit+=d;
267 p->hito=o;
268 return 1;
271 void PL_damage (player_t *p) {
272 int i;
274 if(!p->hit && p->life>0) return;
275 switch(p->st) {
276 case DIE: case SLOP:
277 case DEAD: case MESS:
278 case OUT: case FALL:
279 return;
281 i=p->hit*p->life/nonz(p->armor*3/4+p->life);
282 p->pain+=p->hit;
283 p->drawst|=PL_DRAWLIFE|PL_DRAWARMOR;
284 if((p->armor-=p->hit-i)<0) {p->life+=p->armor;p->armor=0;}
285 if((p->life-=i)<=0) {
286 if(p->life>-30) {p->st=DIE;p->s=0;Z_sound(pdsnd[rand()%5],128);}
287 else {p->st=SLOP;p->s=0;Z_sound(snd[3],128);}
288 if(p->amul>1) IT_spawn(p->o.x,p->o.y,I_BPACK);
289 if(!g_dm) {
290 if(p->keys&16) IT_spawn(p->o.x,p->o.y,I_KEYR);
291 if(p->keys&32) IT_spawn(p->o.x,p->o.y,I_KEYG);
292 if(p->keys&64) IT_spawn(p->o.x,p->o.y,I_KEYB);
294 for(i=1,p->wpns>>=1;i<11;++i,p->wpns>>=1)
295 if(i!=2) if(p->wpns&1) IT_spawn(p->o.x,p->o.y,wp_it[i]);
296 p->wpns=5;p->wpn=2;
297 p->f|=PLF_PNSND;
298 p->drawst|=PL_DRAWWPN;
299 if(g_dm && _2pl) {
300 if(p->id==-1) {
301 if(p->hito==-2) {++pl2.kills;++pl2.frag;}
302 else if(p->hito==-1) --pl1.frag;
303 }else{
304 if(p->hito==-1) {++pl1.kills;++pl1.frag;}
305 else if(p->hito==-2) --pl2.frag;
307 pl1.drawst|=PL_DRAWFRAG;
308 if(_2pl) pl2.drawst|=PL_DRAWFRAG;
310 p->life=0;return;
312 return;
315 void PL_cry (player_t *p) {
316 Z_sound(snd[(p->pain>20)?1:0],128);
317 p->f|=PLF_PNSND;
320 int PL_give (player_t *p, int t) {
321 int i;
323 switch(p->st) {
324 case DIE: case SLOP:
325 case DEAD: case MESS: case OUT:
326 return 0;
328 switch(t) {
329 case I_STIM: case I_MEDI:
330 if(p->life>=100) return 0;
331 if((p->life+=((t==I_MEDI)?25:10))>100) p->life=100;
332 p->drawst|=PL_DRAWLIFE;return 1;
333 case I_CLIP:
334 if(p->ammo>=200*p->amul) return 0;
335 if((p->ammo+=10)>200*p->amul) p->ammo=200*p->amul;
336 p->drawst|=PL_DRAWWPN;return 1;
337 case I_AMMO:
338 if(p->ammo>=200*p->amul) return 0;
339 if((p->ammo+=50)>200*p->amul) p->ammo=200*p->amul;
340 p->drawst|=PL_DRAWWPN;return 1;
341 case I_SHEL:
342 if(p->shel>=50*p->amul) return 0;
343 if((p->shel+=4)>50*p->amul) p->shel=50*p->amul;
344 p->drawst|=PL_DRAWWPN;return 1;
345 case I_SBOX:
346 if(p->shel>=50*p->amul) return 0;
347 if((p->shel+=25)>50*p->amul) p->shel=50*p->amul;
348 p->drawst|=PL_DRAWWPN;return 1;
349 case I_ROCKET:
350 if(p->rock>=50*p->amul) return 0;
351 if((++p->rock)>50*p->amul) p->rock=50*p->amul;
352 p->drawst|=PL_DRAWWPN;return 1;
353 case I_RBOX:
354 if(p->rock>=50*p->amul) return 0;
355 if((p->rock+=5)>50*p->amul) p->rock=50*p->amul;
356 p->drawst|=PL_DRAWWPN;return 1;
357 case I_CELL:
358 if(p->cell>=300*p->amul) return 0;
359 if((p->cell+=40)>300*p->amul) p->cell=300*p->amul;
360 p->drawst|=PL_DRAWWPN;return 1;
361 case I_CELP:
362 if(p->cell>=300*p->amul) return 0;
363 if((p->cell+=100)>300*p->amul) p->cell=300*p->amul;
364 p->drawst|=PL_DRAWWPN;return 1;
365 case I_BPACK:
366 if(p->amul==1) {p->amul=2;i=1;} else i=0;
367 i|=PL_give(p,I_CLIP);
368 i|=PL_give(p,I_SHEL);
369 i|=PL_give(p,I_ROCKET);
370 i|=PL_give(p,I_CELL);
371 return i;
372 case I_CSAW:
373 if(!(p->wpns&2)) {p->wpns|=2;p->drawst|=PL_DRAWWPN;return 1;}
374 return 0;
375 case I_GUN2:
376 i=PL_give(p,I_SHEL);
377 if(!(p->wpns&512)) {p->wpns|=512;i=1;}
378 p->drawst|=PL_DRAWWPN;return i;
379 case I_SGUN:
380 i=PL_give(p,I_SHEL);
381 if(!(p->wpns&8)) {p->wpns|=8;i=1;}
382 p->drawst|=PL_DRAWWPN;return i;
383 case I_SGUN2:
384 i=PL_give(p,I_SHEL);
385 if(!(p->wpns&16)) {p->wpns|=16;i=1;}
386 p->drawst|=PL_DRAWWPN;return i;
387 case I_MGUN:
388 i=PL_give(p,I_AMMO);
389 if(!(p->wpns&32)) {p->wpns|=32;i=1;}
390 p->drawst|=PL_DRAWWPN;return i;
391 case I_LAUN:
392 i=PL_give(p,I_ROCKET);
393 i|=PL_give(p,I_ROCKET);
394 if(!(p->wpns&64)) {p->wpns|=64;i=1;}
395 p->drawst|=PL_DRAWWPN;return i;
396 case I_PLAS:
397 i=PL_give(p,I_CELL);
398 if(!(p->wpns&128)) {p->wpns|=128;i=1;}
399 p->drawst|=PL_DRAWWPN;return i;
400 case I_BFG:
401 i=PL_give(p,I_CELL);
402 if(!(p->wpns&256)) {p->wpns|=256;i=1;}
403 p->drawst|=PL_DRAWWPN;return i;
404 case I_ARM1:
405 if(p->armor>=100) return 0;
406 p->armor=100;p->drawst|=PL_DRAWARMOR;return 1;
407 case I_ARM2:
408 if(p->armor>=200) return 0;
409 p->armor=200;p->drawst|=PL_DRAWARMOR;return 1;
410 case I_MEGA:
411 i=0;
412 if(p->life<200) {p->life=200;p->drawst|=PL_DRAWLIFE;i=1;}
413 if(p->armor<200) {p->armor=200;p->drawst|=PL_DRAWARMOR;i=1;}
414 return i;
415 case I_SUPER:
416 if(p->life<200) {p->life=min(p->life+100,200);p->drawst|=PL_DRAWLIFE;return 1;}
417 return 0;
418 case I_INVL:
419 p->invl=PL_POWERUP_TIME;
420 return 1;
421 case I_SUIT:
422 p->suit=PL_POWERUP_TIME;
423 return 1;
424 case I_AQUA:
425 if(p->air >= PL_AQUA_AIR) return 0;
426 p->air=PL_AQUA_AIR;p->drawst|=PL_DRAWAIR;
427 return 1;
428 case I_KEYR:
429 if(p->keys&16) return 0;
430 p->keys|=16;p->drawst|=PL_DRAWKEYS;return 1;
431 case I_KEYG:
432 if(p->keys&32) return 0;
433 p->keys|=32;p->drawst|=PL_DRAWKEYS;return 1;
434 case I_KEYB:
435 if(p->keys&64) return 0;
436 p->keys|=64;p->drawst|=PL_DRAWKEYS;return 1;
437 default:
438 return 0;
442 void PL_act (player_t *p) {
443 int st;
445 if(--aitime<0) aitime=0;
446 SW_press(p->o.x,p->o.y,p->o.r,p->o.h,4|p->keys,p->id);
447 if(!p->suit) if((g_time&15)==0)
448 PL_hit(p,Z_getacid(p->o.x,p->o.y,p->o.r,p->o.h),-3,HIT_SOME);
449 if(p->st!=FALL && p->st!=OUT) {
450 if(((st=Z_moveobj(&p->o))&Z_FALLOUT) && p->o.y>=FLDH*CELH+50) {
451 switch(p->st) {
452 case DEAD: case MESS: case DIE: case SLOP:
453 p->s=5;break;
454 default:
455 p->s=Z_sound(snd[10],128);
456 if(g_dm) --p->frag;
457 }p->st=FALL;
459 }else st=0;
460 if(st&Z_HITWATER) Z_splash(&p->o,PL_RAD+PL_HT);
461 if(p->f&PLF_FIRE) if(p->fire!=2) p->f-=PLF_FIRE;
462 if(I_pressed(p->ku)) {p->f|=PLF_UP;p->looky-=5;}
463 else{
464 p->f&=0xFFFF-PLF_UP;
465 if(I_pressed(p->kd))
466 {p->f|=PLF_DOWN;p->looky+=5;}
467 else {p->f&=0xFFFF-PLF_DOWN;p->looky=Z_dec(p->looky,5);}
469 if(I_pressed(p->kp)) SW_press(p->o.x,p->o.y,p->o.r,p->o.h,1|p->keys,p->id);
470 if(p->fire) --p->fire;
471 if(p->cwpn) --p->cwpn;
472 if(p->csnd) --p->csnd;
473 if(p->invl) --p->invl;
474 if(p->suit) --p->suit;
475 switch(p->st) {
476 case DIE:
477 p->o.h=7;
478 if(!plr_dieanim[++p->s]) {p->st=DEAD;MN_killedp();}
479 p->o.xv=Z_dec(p->o.xv,1);
480 break;
481 case SLOP:
482 p->o.h=6;
483 if(!plr_slopanim[++p->s]) {p->st=MESS;MN_killedp();}
484 p->o.xv=Z_dec(p->o.xv,1);
485 break;
486 case GO:
487 chgwpn(p);fire(p);jump(p,st);
488 if(p_fly)
489 SMK_gas(p->o.x,p->o.y-2,2,3,p->o.xv+p->o.vx,p->o.yv+p->o.vy,128);
490 if((p->s+=abs(p->o.xv)/2) >= 24) p->s%=24;
491 if(!I_pressed(p->kl) && !I_pressed(p->kr)) {
492 if(p->o.xv) p->o.xv=Z_dec(p->o.xv,1);
493 else p->st=STAND;
494 break;
496 if(p->o.xv<PL_RUN && I_pressed(p->kr)) {p->o.xv+=PL_RUN>>3;p->d=1;}
497 else if(PL_RUN>8)
498 SMK_gas(p->o.x,p->o.y-2,2,3,p->o.xv+p->o.vx,p->o.yv+p->o.vy,32);
499 if(p->o.xv>-PL_RUN && I_pressed(p->kl)) {p->o.xv-=PL_RUN>>3;p->d=0;}
500 else if(PL_RUN>8)
501 SMK_gas(p->o.x,p->o.y-2,2,3,p->o.xv+p->o.vx,p->o.yv+p->o.vy,32);
502 break;
503 case STAND:
504 chgwpn(p);fire(p);jump(p,st);
505 if(p_fly)
506 SMK_gas(p->o.x,p->o.y-2,2,3,p->o.xv+p->o.vx,p->o.yv+p->o.vy,128);
507 if(I_pressed(p->kl)) {p->st=GO;p->s=0;p->d=0;}
508 else if(I_pressed(p->kr)) {p->st=GO;p->s=0;p->d=1;}
509 break;
510 case DEAD:
511 case MESS:
512 case OUT:
513 p->o.xv=Z_dec(p->o.xv,1);
514 if(I_pressed(p->ku) || I_pressed(p->kd) || I_pressed(p->kl) || I_pressed(p->kr) ||
515 I_pressed(p->kf) || I_pressed(p->kj) || I_pressed(p->kp) || I_pressed(p->kwl) || I_pressed(p->kwr)) {
516 if(p->st!=OUT) MN_spawn_deadpl(&p->o,p->color,(p->st==MESS)?1:0);
517 PL_restore(p);
518 if(g_dm) {G_respawn_player(p);break;}
519 if(!_2pl) {
520 if(--p->lives==0) {G_start();break;}
521 else{p->o.x=dm_pos[0].x;p->o.y=dm_pos[0].y;p->d=dm_pos[0].d;}
522 p->drawst|=PL_DRAWLIVES;
524 if(p->id==-1)
525 {p->o.x=dm_pos[0].x;p->o.y=dm_pos[0].y;p->d=dm_pos[0].d;}
526 else {p->o.x=dm_pos[1].x;p->o.y=dm_pos[1].y;p->d=dm_pos[1].d;}
527 }break;
528 case FALL:
529 if(--p->s<=0) p->st=OUT;
530 break;
534 static void chk_bfg(player_t *p,int x,int y) {
535 int dx,dy;
537 if(aitime) return;
538 switch(p->st) {
539 case DIE: case SLOP: case FALL:
540 case DEAD: case MESS: case OUT:
541 return;
543 dx=p->o.x-x;dy=p->o.y-p->o.h/2-y;
544 if(dx*dx+dy*dy<=1600) {
545 aitime=Z_sound(aisnd[rand()%3],128)*4;
549 void bfg_fly (int x,int y,int o) {
550 // if(!g_dm) return;
551 if(o!=-1) chk_bfg(&pl1,x,y);
552 if(_2pl) if(o!=-2) chk_bfg(&pl2,x,y);
553 if(o==-1 || o==-2) MN_warning(x-50,y-50,x+50,y+50);