Debian Codegraph @

code/game/g_combat.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, 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.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 // g_combat.c
24
25 #include "g_local.h"
26
27
28 /*
29 ============
30 ScorePlum
31 ============
32 */
33 void ScorePlum( gentity_t *ent, vec3_t origin, int score ) {
34 gentity_t *plum;
35
36 plum = G_TempEntity( origin, EV_SCOREPLUM );
37 // only send this temp entity to a single client
38 plum->r.svFlags |= SVF_SINGLECLIENT;
39 plum->r.singleClient = ent->s.number;
40 //
41 plum->s.otherEntityNum = ent->s.number;
42 plum->s.time = score;
43 }
44
45 /*
46 ============
47 AddScore
48
49 Adds score to both the client and his team
50 ============
51 */
52 void AddScore( gentity_t *ent, vec3_t origin, int score ) {
53 if ( !ent->client ) {
54 return;
55 }
56 // no scoring during pre-match warmup
57 if ( level.warmupTime ) {
58 return;
59 }
60 // show score plum
61 ScorePlum(ent, origin, score);
62 //
63 ent->client->ps.persistant[PERS_SCORE] += score;
64 if ( g_gametype.integer == GT_TEAM )
65 level.teamScores[ ent->client->ps.persistant[PERS_TEAM] ] += score;
66 CalculateRanks();
67 }
68
69 /*
70 =================
71 TossClientItems
72
73 Toss the weapon and powerups for the killed player
74 =================
75 */
76 void TossClientItems( gentity_t *self ) {
77 gitem_t *item;
78 int weapon;
79 float angle;
80 int i;
81 gentity_t *drop;
82
83 // drop the weapon if not a gauntlet or machinegun
84 weapon = self->s.weapon;
85
86 // make a special check to see if they are changing to a new
87 // weapon that isn't the mg or gauntlet. Without this, a client
88 // can pick up a weapon, be killed, and not drop the weapon because
89 // their weapon change hasn't completed yet and they are still holding the MG.
90 if ( weapon == WP_MACHINEGUN || weapon == WP_GRAPPLING_HOOK ) {
91 if ( self->client->ps.weaponstate == WEAPON_DROPPING ) {
92 weapon = self->client->pers.cmd.weapon;
93 }
94 if ( !( self->client->ps.stats[STAT_WEAPONS] & ( 1 << weapon ) ) ) {
95 weapon = WP_NONE;
96 }
97 }
98
99 if ( weapon > WP_MACHINEGUN && weapon != WP_GRAPPLING_HOOK &&
100 self->client->ps.ammo[ weapon ] ) {
101 // find the item type for this weapon
102 item = BG_FindItemForWeapon( weapon );
103
104 // spawn the item
105 Drop_Item( self, item, 0 );
106 }
107
108 // drop all the powerups if not in teamplay
109 if ( g_gametype.integer != GT_TEAM ) {
110 angle = 45;
111 for ( i = 1 ; i < PW_NUM_POWERUPS ; i++ ) {
112 if ( self->client->ps.powerups[ i ] > level.time ) {
113 item = BG_FindItemForPowerup( i );
114 if ( !item ) {
115 continue;
116 }
117 drop = Drop_Item( self, item, angle );
118 // decide how many seconds it has left
119 drop->count = ( self->client->ps.powerups[ i ] - level.time ) / 1000;
120 if ( drop->count < 1 ) {
121 drop->count = 1;
122 }
123 angle += 45;
124 }
125 }
126 }
127 }
128
129 #ifdef MISSIONPACK
130
131 /*
132 =================
133 TossClientCubes
134 =================
135 */
136 extern gentity_t *neutralObelisk;
137
138 void TossClientCubes( gentity_t *self ) {
139 gitem_t *item;
140 gentity_t *drop;
141 vec3_t velocity;
142 vec3_t angles;
143 vec3_t origin;
144
145 self->client->ps.generic1 = 0;
146
147 // this should never happen but we should never
148 // get the server to crash due to skull being spawned in
149 if (!G_EntitiesFree()) {
150 return;
151 }
152
153 if( self->client->sess.sessionTeam == TEAM_RED ) {
154 item = BG_FindItem( "Red Cube" );
155 }
156 else {
157 item = BG_FindItem( "Blue Cube" );
158 }
159
160 angles[YAW] = (float)(level.time % 360);
161 angles[PITCH] = 0; // always forward
162 angles[ROLL] = 0;
163
164 AngleVectors( angles, velocity, NULL, NULL );
165 VectorScale( velocity, 150, velocity );
166 velocity[2] += 200 + crandom() * 50;
167
168 if( neutralObelisk ) {
169 VectorCopy( neutralObelisk->s.pos.trBase, origin );
170 origin[2] += 44;
171 } else {
172 VectorClear( origin ) ;
173 }
174
175 drop = LaunchItem( item, origin, velocity );
176
177 drop->nextthink = level.time + g_cubeTimeout.integer * 1000;
178 drop->think = G_FreeEntity;
179 drop->spawnflags = self->client->sess.sessionTeam;
180 }
181
182
183 /*
184 =================
185 TossClientPersistantPowerups
186 =================
187 */
188 void TossClientPersistantPowerups( gentity_t *ent ) {
189 gentity_t *powerup;
190
191 if( !ent->client ) {
192 return;
193 }
194
195 if( !ent->client->persistantPowerup ) {
196 return;
197 }
198
199 powerup = ent->client->persistantPowerup;
200
201 powerup->r.svFlags &= ~SVF_NOCLIENT;
202 powerup->s.eFlags &= ~EF_NODRAW;
203 powerup->r.contents = CONTENTS_TRIGGER;
204 trap_LinkEntity( powerup );
205
206 ent->client->ps.stats[STAT_PERSISTANT_POWERUP] = 0;
207 ent->client->persistantPowerup = NULL;
208 }
209 #endif
210
211
212 /*
213 ==================
214 LookAtKiller
215 ==================
216 */
217 void LookAtKiller( gentity_t *self, gentity_t *inflictor, gentity_t *attacker ) {
218 vec3_t dir;
219
220 if ( attacker && attacker != self ) {
221 VectorSubtract (attacker->s.pos.trBase, self->s.pos.trBase, dir);
222 } else if ( inflictor && inflictor != self ) {
223 VectorSubtract (inflictor->s.pos.trBase, self->s.pos.trBase, dir);
224 } else {
225 self->client->ps.stats[STAT_DEAD_YAW] = self->s.angles[YAW];
226 return;
227 }
228
229 self->client->ps.stats[STAT_DEAD_YAW] = vectoyaw ( dir );
230 }
231
232 /*
233 ==================
234 GibEntity
235 ==================
236 */
237 void GibEntity( gentity_t *self, int killer ) {
238 gentity_t *ent;
239 int i;
240
241 //if this entity still has kamikaze
242 if (self->s.eFlags & EF_KAMIKAZE) {
243 // check if there is a kamikaze timer around for this owner
244 for (i = 0; i < level.num_entities; i++) {
245 ent = &g_entities[i];
246 if (!ent->inuse)
247 continue;
248 if (ent->activator != self)
249 continue;
250 if (strcmp(ent->classname, "kamikaze timer"))
251 continue;
252 G_FreeEntity(ent);
253 break;
254 }
255 }
256 G_AddEvent( self, EV_GIB_PLAYER, killer );
257 self->takedamage = qfalse;
258 self->s.eType = ET_INVISIBLE;
259 self->r.contents = 0;
260 }
261
262 /*
263 ==================
264 body_die
265 ==================
266 */
267 void body_die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int meansOfDeath ) {
268 if ( self->health > GIB_HEALTH ) {
269 return;
270 }
271 if ( !g_blood.integer ) {
272 self->health = GIB_HEALTH+1;
273 return;
274 }
275
276 GibEntity( self, 0 );
277 }
278
279
280 // these are just for logging, the client prints its own messages
281 char *modNames[] = {
282 "MOD_UNKNOWN",
283 "MOD_SHOTGUN",
284 "MOD_GAUNTLET",
285 "MOD_MACHINEGUN",
286 "MOD_GRENADE",
287 "MOD_GRENADE_SPLASH",
288 "MOD_ROCKET",
289 "MOD_ROCKET_SPLASH",
290 "MOD_PLASMA",
291 "MOD_PLASMA_SPLASH",
292 "MOD_RAILGUN",
293 "MOD_LIGHTNING",
294 "MOD_BFG",
295 "MOD_BFG_SPLASH",
296 "MOD_WATER",
297 "MOD_SLIME",
298 "MOD_LAVA",
299 "MOD_CRUSH",
300 "MOD_TELEFRAG",
301 "MOD_FALLING",
302 "MOD_SUICIDE",
303 "MOD_TARGET_LASER",
304 "MOD_TRIGGER_HURT",
305 #ifdef MISSIONPACK
306 "MOD_NAIL",
307 "MOD_CHAINGUN",
308 "MOD_PROXIMITY_MINE",
309 "MOD_KAMIKAZE",
310 "MOD_JUICED",
311 #endif
312 "MOD_GRAPPLE"
313 };
314
315 #ifdef MISSIONPACK
316 /*
317 ==================
318 Kamikaze_DeathActivate
319 ==================
320 */
321 void Kamikaze_DeathActivate( gentity_t *ent ) {
322 G_StartKamikaze(ent);
323 G_FreeEntity(ent);
324 }
325
326 /*
327 ==================
328 Kamikaze_DeathTimer
329 ==================
330 */
331 void Kamikaze_DeathTimer( gentity_t *self ) {
332 gentity_t *ent;
333
334 ent = G_Spawn();
335 ent->classname = "kamikaze timer";
336 VectorCopy(self->s.pos.trBase, ent->s.pos.trBase);
337 ent->r.svFlags |= SVF_NOCLIENT;
338 ent->think = Kamikaze_DeathActivate;
339 ent->nextthink = level.time + 5 * 1000;
340
341 ent->activator = self;
342 }
343
344 #endif
345
346 /*
347 ==================
348 CheckAlmostCapture
349 ==================
350 */
351 void CheckAlmostCapture( gentity_t *self, gentity_t *attacker ) {
352 gentity_t *ent;
353 vec3_t dir;
354 char *classname;
355
356 // if this player was carrying a flag
357 if ( self->client->ps.powerups[PW_REDFLAG] ||
358 self->client->ps.powerups[PW_BLUEFLAG] ||
359 self->client->ps.powerups[PW_NEUTRALFLAG] ) {
360 // get the goal flag this player should have been going for
361 if ( g_gametype.integer == GT_CTF ) {
362 if ( self->client->sess.sessionTeam == TEAM_BLUE ) {
363 classname = "team_CTF_blueflag";
364 }
365 else {
366 classname = "team_CTF_redflag";
367 }
368 }
369 else {
370 if ( self->client->sess.sessionTeam == TEAM_BLUE ) {
371 classname = "team_CTF_redflag";
372 }
373 else {
374 classname = "team_CTF_blueflag";
375 }
376 }
377 ent = NULL;
378 do
379 {
380 ent = G_Find(ent, FOFS(classname), classname);
381 } while (ent && (ent->flags & FL_DROPPED_ITEM));
382 // if we found the destination flag and it's not picked up
383 if (ent && !(ent->r.svFlags & SVF_NOCLIENT) ) {
384 // if the player was *very* close
385 VectorSubtract( self->client->ps.origin, ent->s.origin, dir );
386 if ( VectorLength(dir) < 200 ) {
387 self->client->ps.persistant[PERS_PLAYEREVENTS] ^= PLAYEREVENT_HOLYSHIT;
388 if ( attacker->client ) {
389 attacker->client->ps.persistant[PERS_PLAYEREVENTS] ^= PLAYEREVENT_HOLYSHIT;
390 }
391 }
392 }
393 }
394 }
395
396 /*
397 ==================
398 CheckAlmostScored
399 ==================
400 */
401 void CheckAlmostScored( gentity_t *self, gentity_t *attacker ) {
402 gentity_t *ent;
403 vec3_t dir;
404 char *classname;
405
406 // if the player was carrying cubes
407 if ( self->client->ps.generic1 ) {
408 if ( self->client->sess.sessionTeam == TEAM_BLUE ) {
409 classname = "team_redobelisk";
410 }
411 else {
412 classname = "team_blueobelisk";
413 }
414 ent = G_Find(NULL, FOFS(classname), classname);
415 // if we found the destination obelisk
416 if ( ent ) {
417 // if the player was *very* close
418 VectorSubtract( self->client->ps.origin, ent->s.origin, dir );
419 if ( VectorLength(dir) < 200 ) {
420 self->client->ps.persistant[PERS_PLAYEREVENTS] ^= PLAYEREVENT_HOLYSHIT;
421 if ( attacker->client ) {
422 attacker->client->ps.persistant[PERS_PLAYEREVENTS] ^= PLAYEREVENT_HOLYSHIT;
423 }
424 }
425 }
426 }
427 }
428
429 /*
430 ==================
431 player_die
432 ==================
433 */
434 void player_die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int meansOfDeath ) {
435 gentity_t *ent;
436 int anim;
437 int contents;
438 int killer;
439 int i;
440 char *killerName, *obit;
441
442 if ( self->client->ps.pm_type == PM_DEAD ) {
443 return;
444 }
445
446 if ( level.intermissiontime ) {
447 return;
448 }
449
450 // check for an almost capture
451 CheckAlmostCapture( self, attacker );
452 // check for a player that almost brought in cubes
453 CheckAlmostScored( self, attacker );
454
455 if (self->client && self->client->hook) {
456 Weapon_HookFree(self->client->hook);
457 }
458 #ifdef MISSIONPACK
459 if ((self->client->ps.eFlags & EF_TICKING) && self->activator) {
460 self->client->ps.eFlags &= ~EF_TICKING;
461 self->activator->think = G_FreeEntity;
462 self->activator->nextthink = level.time;
463 }
464 #endif
465 self->client->ps.pm_type = PM_DEAD;
466
467 if ( attacker ) {
468 killer = attacker->s.number;
469 if ( attacker->client ) {
470 killerName = attacker->client->pers.netname;
471 } else {
472 killerName = "<non-client>";
473 }
474 } else {
475 killer = ENTITYNUM_WORLD;
476 killerName = "<world>";
477 }
478
479 if ( killer < 0 || killer >= MAX_CLIENTS ) {
480 killer = ENTITYNUM_WORLD;
481 killerName = "<world>";
482 }
483
484 if ( meansOfDeath < 0 || meansOfDeath >= ARRAY_LEN( modNames ) ) {
485 obit = "<bad obituary>";
486 } else {
487 obit = modNames[meansOfDeath];
488 }
489
490 G_LogPrintf("Kill: %i %i %i: %s killed %s by %s\n",
491 killer, self->s.number, meansOfDeath, killerName,
492 self->client->pers.netname, obit );
493
494 // broadcast the death event to everyone
495 ent = G_TempEntity( self->r.currentOrigin, EV_OBITUARY );
496 ent->s.eventParm = meansOfDeath;
497 ent->s.otherEntityNum = self->s.number;
498 ent->s.otherEntityNum2 = killer;
499 ent->r.svFlags = SVF_BROADCAST; // send to everyone
500
501 self->enemy = attacker;
502
503 self->client->ps.persistant[PERS_KILLED]++;
504
505 if (attacker && attacker->client) {
506 attacker->client->lastkilled_client = self->s.number;
507
508 if ( attacker == self || OnSameTeam (self, attacker ) ) {
509 AddScore( attacker, self->r.currentOrigin, -1 );
510 } else {
511 AddScore( attacker, self->r.currentOrigin, 1 );
512
513 if( meansOfDeath == MOD_GAUNTLET ) {
514
515 // play humiliation on player
516 attacker->client->ps.persistant[PERS_GAUNTLET_FRAG_COUNT]++;
517
518 // add the sprite over the player's head
519 attacker->client->ps.eFlags &= ~(EF_AWARD_IMPRESSIVE | EF_AWARD_EXCELLENT | EF_AWARD_GAUNTLET | EF_AWARD_ASSIST | EF_AWARD_DEFEND | EF_AWARD_CAP );
520 attacker->client->ps.eFlags |= EF_AWARD_GAUNTLET;
521 attacker->client->rewardTime = level.time + REWARD_SPRITE_TIME;
522
523 // also play humiliation on target
524 self->client->ps.persistant[PERS_PLAYEREVENTS] ^= PLAYEREVENT_GAUNTLETREWARD;
525 }
526
527 // check for two kills in a short amount of time
528 // if this is close enough to the last kill, give a reward sound
529 if ( level.time - attacker->client->lastKillTime < CARNAGE_REWARD_TIME ) {
530 // play excellent on player
531 attacker->client->ps.persistant[PERS_EXCELLENT_COUNT]++;
532
533 // add the sprite over the player's head
534 attacker->client->ps.eFlags &= ~(EF_AWARD_IMPRESSIVE | EF_AWARD_EXCELLENT | EF_AWARD_GAUNTLET | EF_AWARD_ASSIST | EF_AWARD_DEFEND | EF_AWARD_CAP );
535 attacker->client->ps.eFlags |= EF_AWARD_EXCELLENT;
536 attacker->client->rewardTime = level.time + REWARD_SPRITE_TIME;
537 }
538 attacker->client->lastKillTime = level.time;
539
540 }
541 } else {
542 AddScore( self, self->r.currentOrigin, -1 );
543 }
544
545 // Add team bonuses
546 Team_FragBonuses(self, inflictor, attacker);
547
548 // if I committed suicide, the flag does not fall, it returns.
549 if (meansOfDeath == MOD_SUICIDE) {
550 if ( self->client->ps.powerups[PW_NEUTRALFLAG] ) { // only happens in One Flag CTF
551 Team_ReturnFlag( TEAM_FREE );
552 self->client->ps.powerups[PW_NEUTRALFLAG] = 0;
553 }
554 else if ( self->client->ps.powerups[PW_REDFLAG] ) { // only happens in standard CTF
555 Team_ReturnFlag( TEAM_RED );
556 self->client->ps.powerups[PW_REDFLAG] = 0;
557 }
558 else if ( self->client->ps.powerups[PW_BLUEFLAG] ) { // only happens in standard CTF
559 Team_ReturnFlag( TEAM_BLUE );
560 self->client->ps.powerups[PW_BLUEFLAG] = 0;
561 }
562 }
563
564 TossClientItems( self );
565 #ifdef MISSIONPACK
566 TossClientPersistantPowerups( self );
567 if( g_gametype.integer == GT_HARVESTER ) {
568 TossClientCubes( self );
569 }
570 #endif
571
572 Cmd_Score_f( self ); // show scores
573 // send updated scores to any clients that are following this one,
574 // or they would get stale scoreboards
575 for ( i = 0 ; i < level.maxclients ; i++ ) {
576 gclient_t *client;
577
578 client = &level.clients[i];
579 if ( client->pers.connected != CON_CONNECTED ) {
580 continue;
581 }
582 if ( client->sess.sessionTeam != TEAM_SPECTATOR ) {
583 continue;
584 }
585 if ( client->sess.spectatorClient == self->s.number ) {
586 Cmd_Score_f( g_entities + i );
587 }
588 }
589
590 self->takedamage = qtrue; // can still be gibbed
591
592 self->s.weapon = WP_NONE;
593 self->s.powerups = 0;
594 self->r.contents = CONTENTS_CORPSE;
595
596 self->s.angles[0] = 0;
597 self->s.angles[2] = 0;
598 LookAtKiller (self, inflictor, attacker);
599
600 VectorCopy( self->s.angles, self->client->ps.viewangles );
601
602 self->s.loopSound = 0;
603
604 self->r.maxs[2] = DEAD_HEIGHT;
605
606 // don't allow respawn until the death anim is done
607 // g_forcerespawn may force spawning at some later time
608 self->client->respawnTime = level.time + 1700;
609
610 // remove powerups
611 memset( self->client->ps.powerups, 0, sizeof(self->client->ps.powerups) );
612
613 // never gib in a nodrop
614 contents = trap_PointContents( self->r.currentOrigin, -1 );
615
616 if ( (self->health <= GIB_HEALTH && !(contents & CONTENTS_NODROP) && g_blood.integer) || meansOfDeath == MOD_SUICIDE) {
617 // gib death
618 GibEntity( self, killer );
619 } else {
620 // normal death
621 static int lastDeath;
622
623 switch ( lastDeath ) {
624 case 0:
625 anim = BOTH_DEATH1;
626 break;
627 case 1:
628 anim = BOTH_DEATH2;
629 break;
630 case 2:
631 default:
632 anim = BOTH_DEATH3;
633 break;
634 }
635
636 // for the no-blood option, we need to prevent the health
637 // from going to gib level
638 if ( self->health <= GIB_HEALTH ) {
639 self->health = GIB_HEALTH+1;
640 }
641
642 self->client->ps.legsAnim =
643 ( ( self->client->ps.legsAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | anim;
644 self->client->ps.torsoAnim =
645 ( ( self->client->ps.torsoAnim & ANIM_TOGGLEBIT ) ^ ANIM_TOGGLEBIT ) | anim;
646
647 G_AddEvent( self, EV_DEATH1 + lastDeath, killer );
648
649 // the body can still be gibbed
650 self->die = body_die;
651
652 // globally cycle through the different death animations
653 lastDeath = ( lastDeath + 1 ) % 3;
654
655 #ifdef MISSIONPACK
656 if (self->s.eFlags & EF_KAMIKAZE) {
657 Kamikaze_DeathTimer( self );
658 }
659 #endif
660 }
661
662 trap_LinkEntity (self);
663
664 }
665
666
667 /*
668 ================
669 CheckArmor
670 ================
671 */
672 int CheckArmor (gentity_t *ent, int damage, int dflags)
673 {
674 gclient_t *client;
675 int save;
676 int count;
677
678 if (!damage)
679 return 0;
680
681 client = ent->client;
682
683 if (!client)
684 return 0;
685
686 if (dflags & DAMAGE_NO_ARMOR)
687 return 0;
688
689 // armor
690 count = client->ps.stats[STAT_ARMOR];
691 save = ceil( damage * ARMOR_PROTECTION );
692 if (save >= count)
693 save = count;
694
695 if (!save)
696 return 0;
697
698 client->ps.stats[STAT_ARMOR] -= save;
699
700 return save;
701 }
702
703 /*
704 ================
705 RaySphereIntersections
706 ================
707 */
708 int RaySphereIntersections( vec3_t origin, float radius, vec3_t point, vec3_t dir, vec3_t intersections[2] ) {
709 float b, c, d, t;
710
711 // | origin - (point + t * dir) | = radius
712 // a = dir[0]^2 + dir[1]^2 + dir[2]^2;
713 // b = 2 * (dir[0] * (point[0] - origin[0]) + dir[1] * (point[1] - origin[1]) + dir[2] * (point[2] - origin[2]));
714 // c = (point[0] - origin[0])^2 + (point[1] - origin[1])^2 + (point[2] - origin[2])^2 - radius^2;
715
716 // normalize dir so a = 1
717 VectorNormalize(dir);
718 b = 2 * (dir[0] * (point[0] - origin[0]) + dir[1] * (point[1] - origin[1]) + dir[2] * (point[2] - origin[2]));
719 c = (point[0] - origin[0]) * (point[0] - origin[0]) +
720 (point[1] - origin[1]) * (point[1] - origin[1]) +
721 (point[2] - origin[2]) * (point[2] - origin[2]) -
722 radius * radius;
723
724 d = b * b - 4 * c;
725 if (d > 0) {
726 t = (- b + sqrt(d)) / 2;
727 VectorMA(point, t, dir, intersections[0]);
728 t = (- b - sqrt(d)) / 2;
729 VectorMA(point, t, dir, intersections[1]);
730 return 2;
731 }
732 else if (d == 0) {
733 t = (- b ) / 2;
734 VectorMA(point, t, dir, intersections[0]);
735 return 1;
736 }
737 return 0;
738 }
739
740 #ifdef MISSIONPACK
741 /*
742 ================
743 G_InvulnerabilityEffect
744 ================
745 */
746 int G_InvulnerabilityEffect( gentity_t *targ, vec3_t dir, vec3_t point, vec3_t impactpoint, vec3_t bouncedir ) {
747 gentity_t *impact;
748 vec3_t intersections[2], vec;
749 int n;
750
751 if ( !targ->client ) {
752 return qfalse;
753 }
754 VectorCopy(dir, vec);
755 VectorInverse(vec);
756 n = RaySphereIntersections( targ->client->ps.origin, INVUL_RADIUS, point, vec, intersections);
757 if (n > 0) {
758 impact = G_TempEntity( targ->client->ps.origin, EV_INVUL_IMPACT );
759 VectorSubtract(intersections[0], targ->client->ps.origin, vec);
760 vectoangles(vec, impact->s.angles);
761 impact->s.angles[0] += 90;
762 if (impact->s.angles[0] > 360)
763 impact->s.angles[0] -= 360;
764 if ( impactpoint ) {
765 VectorCopy( intersections[0], impactpoint );
766 }
767 if ( bouncedir ) {
768 VectorCopy( vec, bouncedir );
769 VectorNormalize( bouncedir );
770 }
771 return qtrue;
772 }
773 else {
774 return qfalse;
775 }
776 }
777 #endif
778 /*
779 ============
780 G_Damage
781
782 targ entity that is being damaged
783 inflictor entity that is causing the damage
784 attacker entity that caused the inflictor to damage targ
785 example: targ=monster, inflictor=rocket, attacker=player
786
787 dir direction of the attack for knockback
788 point point at which the damage is being inflicted, used for headshots
789 damage amount of damage being inflicted
790 knockback force to be applied against targ as a result of the damage
791
792 inflictor, attacker, dir, and point can be NULL for environmental effects
793
794 dflags these flags are used to control how T_Damage works
795 DAMAGE_RADIUS damage was indirect (from a nearby explosion)
796 DAMAGE_NO_ARMOR armor does not protect from this damage
797 DAMAGE_NO_KNOCKBACK do not affect velocity, just view angles
798 DAMAGE_NO_PROTECTION kills godmode, armor, everything
799 ============
800 */
801
802 void G_Damage( gentity_t *targ, gentity_t *inflictor, gentity_t *attacker,
803 vec3_t dir, vec3_t point, int damage, int dflags, int mod ) {
804 gclient_t *client;
805 int take;
806 int asave;
807 int knockback;
808 int max;
809 #ifdef MISSIONPACK
810 vec3_t bouncedir, impactpoint;
811 #endif
812
813 if (!targ->takedamage) {
814 return;
815 }
816
817 // the intermission has already been qualified for, so don't
818 // allow any extra scoring
819 if ( level.intermissionQueued ) {
820 return;
821 }
822 #ifdef MISSIONPACK
823 if ( targ->client && mod != MOD_JUICED) {
824 if ( targ->client->invulnerabilityTime > level.time) {
825 if ( dir && point ) {
826 G_InvulnerabilityEffect( targ, dir, point, impactpoint, bouncedir );
827 }
828 return;
829 }
830 }
831 #endif
832 if ( !inflictor ) {
833 inflictor = &g_entities[ENTITYNUM_WORLD];
834 }
835 if ( !attacker ) {
836 attacker = &g_entities[ENTITYNUM_WORLD];
837 }
838
839 // shootable doors / buttons don't actually have any health
840 if ( targ->s.eType == ET_MOVER ) {
841 if ( targ->use && targ->moverState == MOVER_POS1 ) {
842 targ->use( targ, inflictor, attacker );
843 }
844 return;
845 }
846 #ifdef MISSIONPACK
847 if( g_gametype.integer == GT_OBELISK && CheckObeliskAttack( targ, attacker ) ) {
848 return;
849 }
850 #endif
851 // reduce damage by the attacker's handicap value
852 // unless they are rocket jumping
853 if ( attacker->client && attacker != targ ) {
854 max = attacker->client->ps.stats[STAT_MAX_HEALTH];
855 #ifdef MISSIONPACK
856 if( bg_itemlist[attacker->client->ps.stats[STAT_PERSISTANT_POWERUP]].giTag == PW_GUARD ) {
857 max /= 2;
858 }
859 #endif
860 damage = damage * max / 100;
861 }
862
863 client = targ->client;
864
865 if ( client ) {
866 if ( client->noclip ) {
867 return;
868 }
869 }
870
871 if ( !dir ) {
872 dflags |= DAMAGE_NO_KNOCKBACK;
873 } else {
874 VectorNormalize(dir);
875 }
876
877 knockback = damage;
878 if ( knockback > 200 ) {
879 knockback = 200;
880 }
881 if ( targ->flags & FL_NO_KNOCKBACK ) {
882 knockback = 0;
883 }
884 if ( dflags & DAMAGE_NO_KNOCKBACK ) {
885 knockback = 0;
886 }
887
888 // figure momentum add, even if the damage won't be taken
889 if ( knockback && targ->client ) {
890 vec3_t kvel;
891 float mass;
892
893 mass = 200;
894
895 VectorScale (dir, g_knockback.value * (float)knockback / mass, kvel);
896 VectorAdd (targ->client->ps.velocity, kvel, targ->client->ps.velocity);
897
898 // set the timer so that the other client can't cancel
899 // out the movement immediately
900 if ( !targ->client->ps.pm_time ) {
901 int t;
902
903 t = knockback * 2;
904 if ( t < 50 ) {
905 t = 50;
906 }
907 if ( t > 200 ) {
908 t = 200;
909 }
910 targ->client->ps.pm_time = t;
911 targ->client->ps.pm_flags |= PMF_TIME_KNOCKBACK;
912 }
913 }
914
915 // check for completely getting out of the damage
916 if ( !(dflags & DAMAGE_NO_PROTECTION) ) {
917
918 // if TF_NO_FRIENDLY_FIRE is set, don't do damage to the target
919 // if the attacker was on the same team
920 #ifdef MISSIONPACK
921 if ( mod != MOD_JUICED && targ != attacker && !(dflags & DAMAGE_NO_TEAM_PROTECTION) && OnSameTeam (targ, attacker) ) {
922 #else
923 if ( targ != attacker && OnSameTeam (targ, attacker) ) {
924 #endif
925 if ( !g_friendlyFire.integer ) {
926 return;
927 }
928 }
929 #ifdef MISSIONPACK
930 if (mod == MOD_PROXIMITY_MINE) {
931 if (inflictor && inflictor->parent && OnSameTeam(targ, inflictor->parent)) {
932 return;
933 }
934 if (targ == attacker) {
935 return;
936 }
937 }
938 #endif
939
940 // check for godmode
941 if ( targ->flags & FL_GODMODE ) {
942 return;
943 }
944 }
945
946 // battlesuit protects from all radius damage (but takes knockback)
947 // and protects 50% against all damage
948 if ( client && client->ps.powerups[PW_BATTLESUIT] ) {
949 G_AddEvent( targ, EV_POWERUP_BATTLESUIT, 0 );
950 if ( ( dflags & DAMAGE_RADIUS ) || ( mod == MOD_FALLING ) ) {
951 return;
952 }
953 damage *= 0.5;
954 }
955
956 // add to the attacker's hit counter (if the target isn't a general entity like a prox mine)
957 if ( attacker->client && client
958 && targ != attacker && targ->health > 0
959 && targ->s.eType != ET_MISSILE
960 && targ->s.eType != ET_GENERAL) {
961 if ( OnSameTeam( targ, attacker ) ) {
962 attacker->client->ps.persistant[PERS_HITS]--;
963 } else {
964 attacker->client->ps.persistant[PERS_HITS]++;
965 }
966 attacker->client->ps.persistant[PERS_ATTACKEE_ARMOR] = (targ->health<<8)|(client->ps.stats[STAT_ARMOR]);
967 }
968
969 // always give half damage if hurting self
970 // calculated after knockback, so rocket jumping works
971 if ( targ == attacker) {
972 damage *= 0.5;
973 }
974
975 if ( damage < 1 ) {
976 damage = 1;
977 }
978 take = damage;
979
980 // save some from armor
981 asave = CheckArmor (targ, take, dflags);
982 take -= asave;
983
984 if ( g_debugDamage.integer ) {
985 G_Printf( "%i: client:%i health:%i damage:%i armor:%i\n", level.time, targ->s.number,
986 targ->health, take, asave );
987 }
988
989 // add to the damage inflicted on a player this frame
990 // the total will be turned into screen blends and view angle kicks
991 // at the end of the frame
992 if ( client ) {
993 if ( attacker ) {
994 client->ps.persistant[PERS_ATTACKER] = attacker->s.number;
995 } else {
996 client->ps.persistant[PERS_ATTACKER] = ENTITYNUM_WORLD;
997 }
998 client->damage_armor += asave;
999 client->damage_blood += take;
1000 client->damage_knockback += knockback;
1001 if ( dir ) {
1002 VectorCopy ( dir, client->damage_from );
1003 client->damage_fromWorld = qfalse;
1004 } else {
1005 VectorCopy ( targ->r.currentOrigin, client->damage_from );
1006 client->damage_fromWorld = qtrue;
1007 }
1008 }
1009
1010 // See if it's the player hurting the emeny flag carrier
1011 #ifdef MISSIONPACK
1012 if( g_gametype.integer == GT_CTF || g_gametype.integer == GT_1FCTF ) {
1013 #else
1014 if( g_gametype.integer == GT_CTF) {
1015 #endif
1016 Team_CheckHurtCarrier(targ, attacker);
1017 }
1018
1019 if (targ->client) {
1020 // set the last client who damaged the target
1021 targ->client->lasthurt_client = attacker->s.number;
1022 targ->client->lasthurt_mod = mod;
1023 }
1024
1025 // do the damage
1026 if (take) {
1027 targ->health = targ->health - take;
1028 if ( targ->client ) {
1029 targ->client->ps.stats[STAT_HEALTH] = targ->health;
1030 }
1031
1032 if ( targ->health <= 0 ) {
1033 if ( client )
1034 targ->flags |= FL_NO_KNOCKBACK;
1035
1036 if (targ->health < -999)
1037 targ->health = -999;
1038
1039 targ->enemy = attacker;
1040 targ->die (targ, inflictor, attacker, take, mod);
1041 return;
1042 } else if ( targ->pain ) {
1043 targ->pain (targ, attacker, take);
1044 }
1045 }
1046
1047 }
1048
1049
1050 /*
1051 ============
1052 CanDamage
1053
1054 Returns qtrue if the inflictor can directly damage the target. Used for
1055 explosions and melee attacks.
1056 ============
1057 */
1058 qboolean CanDamage (gentity_t *targ, vec3_t origin) {
1059 vec3_t dest;
1060 trace_t tr;
1061 vec3_t midpoint;
1062 vec3_t offsetmins = {-15, -15, -15};
1063 vec3_t offsetmaxs = {15, 15, 15};
1064
1065 // use the midpoint of the bounds instead of the origin, because
1066 // bmodels may have their origin is 0,0,0
1067 VectorAdd (targ->r.absmin, targ->r.absmax, midpoint);
1068 VectorScale (midpoint, 0.5, midpoint);
1069
1070 VectorCopy(midpoint, dest);
1071 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1072
1073 if (tr.fraction == 1.0 || tr.entityNum == targ->s.number)
1074 return qtrue;
1075
1076 // this should probably check in the plane of projection,
1077 // rather than in world coordinate
1078 VectorCopy(midpoint, dest);
1079 dest[0] += offsetmaxs[0];
1080 dest[1] += offsetmaxs[1];
1081 dest[2] += offsetmaxs[2];
1082 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1083
1084 if (tr.fraction == 1.0)
1085 return qtrue;
1086
1087 VectorCopy(midpoint, dest);
1088 dest[0] += offsetmaxs[0];
1089 dest[1] += offsetmins[1];
1090 dest[2] += offsetmaxs[2];
1091 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1092
1093 if (tr.fraction == 1.0)
1094 return qtrue;
1095
1096 VectorCopy(midpoint, dest);
1097 dest[0] += offsetmins[0];
1098 dest[1] += offsetmaxs[1];
1099 dest[2] += offsetmaxs[2];
1100 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1101
1102 if (tr.fraction == 1.0)
1103 return qtrue;
1104
1105 VectorCopy(midpoint, dest);
1106 dest[0] += offsetmins[0];
1107 dest[1] += offsetmins[1];
1108 dest[2] += offsetmaxs[2];
1109 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1110
1111 if (tr.fraction == 1.0)
1112 return qtrue;
1113
1114 VectorCopy(midpoint, dest);
1115 dest[0] += offsetmaxs[0];
1116 dest[1] += offsetmaxs[1];
1117 dest[2] += offsetmins[2];
1118 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1119
1120 if (tr.fraction == 1.0)
1121 return qtrue;
1122
1123 VectorCopy(midpoint, dest);
1124 dest[0] += offsetmaxs[0];
1125 dest[1] += offsetmins[1];
1126 dest[2] += offsetmins[2];
1127 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1128
1129 if (tr.fraction == 1.0)
1130 return qtrue;
1131
1132 VectorCopy(midpoint, dest);
1133 dest[0] += offsetmins[0];
1134 dest[1] += offsetmaxs[1];
1135 dest[2] += offsetmins[2];
1136 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1137
1138 if (tr.fraction == 1.0)
1139 return qtrue;
1140
1141 VectorCopy(midpoint, dest);
1142 dest[0] += offsetmins[0];
1143 dest[1] += offsetmins[1];
1144 dest[2] += offsetmins[2];
1145 trap_Trace(&tr, origin, vec3_origin, vec3_origin, dest, ENTITYNUM_NONE, MASK_SOLID);
1146
1147 if (tr.fraction == 1.0)
1148 return qtrue;
1149
1150 return qfalse;
1151 }
1152
1153
1154 /*
1155 ============
1156 G_RadiusDamage
1157 ============
1158 */
1159 qboolean G_RadiusDamage ( vec3_t origin, gentity_t *attacker, float damage, float radius,
1160 gentity_t *ignore, int mod) {
1161 float points, dist;
1162 gentity_t *ent;
1163 int entityList[MAX_GENTITIES];
1164 int numListedEntities;
1165 vec3_t mins, maxs;
1166 vec3_t v;
1167 vec3_t dir;
1168 int i, e;
1169 qboolean hitClient = qfalse;
1170
1171 if ( radius < 1 ) {
1172 radius = 1;
1173 }
1174
1175 for ( i = 0 ; i < 3 ; i++ ) {
1176 mins[i] = origin[i] - radius;
1177 maxs[i] = origin[i] + radius;
1178 }
1179
1180 numListedEntities = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
1181
1182 for ( e = 0 ; e < numListedEntities ; e++ ) {
1183 ent = &g_entities[entityList[ e ]];
1184
1185 if (ent == ignore)
1186 continue;
1187 if (!ent->takedamage)
1188 continue;
1189
1190 // find the distance from the edge of the bounding box
1191 for ( i = 0 ; i < 3 ; i++ ) {
1192 if ( origin[i] < ent->r.absmin[i] ) {
1193 v[i] = ent->r.absmin[i] - origin[i];
1194 } else if ( origin[i] > ent->r.absmax[i] ) {
1195 v[i] = origin[i] - ent->r.absmax[i];
1196 } else {
1197 v[i] = 0;
1198 }
1199 }
1200
1201 dist = VectorLength( v );
1202 if ( dist >= radius ) {
1203 continue;
1204 }
1205
1206 points = damage * ( 1.0 - dist / radius );
1207
1208 if( CanDamage (ent, origin) ) {
1209 if( LogAccuracyHit( ent, attacker ) ) {
1210 hitClient = qtrue;
1211 }
1212 VectorSubtract (ent->r.currentOrigin, origin, dir);
1213 // push the center of mass higher than the origin so players
1214 // get knocked into the air more
1215 dir[2] += 24;
1216 G_Damage (ent, NULL, attacker, dir, origin, (int)points, DAMAGE_RADIUS, mod);
1217 }
1218 }
1219
1220 return hitClient;
1221 }