]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_damage.qc
moved gibbing from server to client
[divverent/nexuiz.git] / data / qcsrc / server / g_damage.qc
1 .float dmg;
2 .float dmg_edge;
3 .float dmg_force;
4 .float dmg_radius;
5
6 float Damage_DamageInfo_SendEntity(entity to, float sf)
7 {
8         WriteByte(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
9         WriteShort(MSG_ENTITY, self.projectiledeathtype);
10         WriteCoord(MSG_ENTITY, floor(self.origin_x));
11         WriteCoord(MSG_ENTITY, floor(self.origin_y));
12         WriteCoord(MSG_ENTITY, floor(self.origin_z));
13         WriteByte(MSG_ENTITY, bound(1, self.dmg, 255));
14         WriteByte(MSG_ENTITY, bound(0, self.dmg_radius, 255));
15         WriteByte(MSG_ENTITY, bound(1, self.dmg_edge, 255));
16         WriteShort(MSG_ENTITY, self.oldorigin_x);
17         return TRUE;
18 }
19
20 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, float deathtype)
21 {
22         // TODO maybe call this from non-edgedamage too?
23         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
24
25         entity e;
26
27         e = spawn();
28         e.classname = "damageinfo";
29         setorigin(e, org);
30         setmodel(e, "null");
31         e.think = SUB_Remove;
32         e.nextthink = time + 0.2;
33         e.projectiledeathtype = deathtype;
34         e.dmg = coredamage;
35         e.dmg_edge = edgedamage;
36         e.dmg_radius = rad;
37         e.dmg_force = vlen(force);
38         e.velocity = force;
39
40         e.oldorigin_x = compressShortVector(e.velocity);
41
42         e.SendEntity = Damage_DamageInfo_SendEntity;
43 }
44
45 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
46
47 float checkrules_firstblood;
48
49 float yoda;
50 float damage_goodhits;
51 float damage_gooddamage;
52 float headshot;
53 float damage_headshotbonus; // bonus multiplier for head shots, set to 0 after use
54
55 .float dmg_team;
56 .float teamkill_complain;
57 .float teamkill_soundtime;
58 .entity teamkill_soundsource;
59 .entity pusher;
60 .float taunt_soundtime;
61
62
63 float IsDifferentTeam(entity a, entity b)
64 {
65         if(teams_matter)
66         {
67                 if(a.team == b.team)
68                         return 0;
69         }
70         else
71         {
72                 if(a == b)
73                         return 0;
74         }
75         return 1;
76 }
77
78 float IsFlying(entity a)
79 {
80         if(a.flags & FL_ONGROUND)
81                 return 0;
82         if(a.waterlevel >= 2)
83                 return 0;
84         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
85         if(trace_fraction < 1)
86                 return 0;
87         return 1;
88 }
89
90 void UpdateFrags(entity player, float f)
91 {
92         PlayerTeamScore_AddScore(player, f);
93 }
94
95 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
96 void GiveFrags (entity attacker, entity targ, float f)
97 {
98         // TODO route through PlayerScores instead
99         if(gameover) return;
100
101         if(f < 0)
102         {
103                 if(targ == attacker)
104                 {
105                         // suicide
106                         PlayerScore_Add(attacker, SP_SUICIDES, 1);
107                 }
108                 else
109                 {
110                         // teamkill
111                         PlayerScore_Add(attacker, SP_KILLS, -1); // or maybe add a teamkills field?
112                 }
113         }
114         else
115         {
116                 // regular frag
117                 PlayerScore_Add(attacker, SP_KILLS, 1);
118         }
119
120         PlayerScore_Add(targ, SP_DEATHS, 1);
121
122         if(g_arena)
123                 if(cvar("g_arena_roundbased"))
124                         return;
125
126         // FIXME fix the mess this is (we have REAL points now!)
127         if(g_runematch)
128         {
129                 f = RunematchHandleFrags(attacker, targ, f);
130         }
131         else if(g_keyhunt)
132         {
133                 f = kh_HandleFrags(attacker, targ, f);
134         }
135         else if(g_lms)
136         {
137                 // remove a life
138                 float tl;
139                 tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
140                 if(tl < lms_lowest_lives)
141                         lms_lowest_lives = tl;
142                 if(tl <= 0)
143                 {
144                         if(!lms_next_place)
145                                 lms_next_place = player_count;
146                         PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
147                         --lms_next_place;
148                 }
149                 f = 0;
150         }
151         else if(g_ctf)
152         {
153                 if(g_ctf_ignore_frags)
154                         f = 0;
155         }
156
157         attacker.totalfrags += f;
158
159         if(f)
160                 UpdateFrags(attacker, f);
161 }
162
163 string AppendItemcodes(string s, entity player)
164 {
165         float w;
166         w = player.weapon;
167         //if(w == 0)
168         //      w = player.switchweapon;
169         if(w == 0)
170                 w = player.cnt; // previous weapon!
171         s = strcat(s, ftos(w));
172         if(time < player.strength_finished)
173                 s = strcat(s, "S");
174         if(time < player.invincible_finished)
175                 s = strcat(s, "I");
176         if(player.flagcarried != world)
177                 s = strcat(s, "F");
178         if(player.BUTTON_CHAT)
179                 s = strcat(s, "T");
180         if(player.kh_next)
181                 s = strcat(s, "K");
182         if(player.runes)
183                 s = strcat(s, "|", ftos(player.runes));
184         return s;
185 }
186
187 void LogDeath(string mode, float deathtype, entity killer, entity killed)
188 {
189         string s;
190         if(!cvar("sv_eventlog"))
191                 return;
192         s = strcat(":kill:", mode);
193         s = strcat(s, ":", ftos(killer.playerid));
194         s = strcat(s, ":", ftos(killed.playerid));
195         s = strcat(s, ":type=", ftos(deathtype));
196         s = strcat(s, ":items=");
197         s = AppendItemcodes(s, killer);
198         if(killed != killer)
199         {
200                 s = strcat(s, ":victimitems=");
201                 s = AppendItemcodes(s, killed);
202         }
203         GameLogEcho(s);
204 }
205
206 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
207 {
208         string  s, a;
209         float p, w;
210
211         if (targ.classname == "player" || targ.classname == "corpse")
212         {
213                 if (targ.classname == "corpse")
214                         s = "A corpse";
215                 else
216                         s = targ.netname;
217                 a = attacker.netname;
218
219                 if (targ == attacker)
220                 {
221                         if (deathtype == DEATH_TEAMCHANGE) {
222                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", ColoredTeamName(targ.team)));
223                         } else if (deathtype == DEATH_AUTOTEAMCHANGE) {
224                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(targ.team)));
225                                 return;
226                         } else if (deathtype == DEATH_CAMP) {
227                                 if(sv_gentle)
228                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
229                                 else
230                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
231                         } else if (deathtype == DEATH_NOAMMO) {
232                                 if(sv_gentle)
233                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
234                                 else
235                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
236                         } else if (deathtype == DEATH_ROT) {
237                                 if(sv_gentle)
238                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
239                                 else
240                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
241                         } else if (deathtype == DEATH_MIRRORDAMAGE) {
242                                 if(sv_gentle)
243                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
244                                 else
245                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
246                         } else {
247                                 if(sv_gentle)
248                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
249                                 else
250                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
251                         }
252
253                         if(sv_gentle) {
254                                 if (deathtype == DEATH_CAMP)
255                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
256                                 else if (deathtype == DEATH_MIRRORDAMAGE)
257                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
258                                 else
259                                         bprint ("^1",s, "^1 will be reinserted into the game due to his own actions\n");
260
261                                 if(deathtype != DEATH_TEAMCHANGE)
262                                 {
263                                         LogDeath("suicide", deathtype, targ, targ);
264                                         GiveFrags(attacker, targ, -1);
265                                 }
266                                 if (targ.killcount > 2)
267                                         bprint ("^1",s,"^1 faded after a ",ftos(targ.killcount)," point spree\n");
268                         } else {
269                                 w = DEATH_WEAPONOF(deathtype);
270                                 if(WEP_VALID(w))
271                                 {
272                                         w_deathtypestring = "couldn't resist the urge to self-destruct";
273                                         w_deathtype = deathtype;
274                                         weapon_action(w, WR_SUICIDEMESSAGE);
275                                         bprint("^1", s, "^1 ", w_deathtypestring, "\n");
276                                 }
277                                 else if (deathtype == DEATH_KILL)
278                                         bprint ("^1",s, "^1 couldn't take it anymore\n");
279                                 else if (deathtype == DEATH_ROT)
280                                         bprint ("^1",s, "^1 died\n");
281                                 else if (deathtype == DEATH_NOAMMO)
282                                         bprint ("^7",s, "^7 committed suicide. What's the point of living without ammo?\n");
283                                 else if (deathtype == DEATH_CAMP)
284                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
285                                 else if (deathtype == DEATH_MIRRORDAMAGE)
286                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
287                                 else if (deathtype != DEATH_TEAMCHANGE)
288                                         bprint ("^1",s, "^1 couldn't resist the urge to self-destruct\n");
289
290                                 if(deathtype != DEATH_TEAMCHANGE)
291                                 {
292                                         LogDeath("suicide", deathtype, targ, targ);
293                                         GiveFrags(attacker, targ, -1);
294                                 }
295                                 if (targ.killcount > 2)
296                                         bprint ("^1",s,"^1 ended it all after a ",ftos(targ.killcount)," kill spree\n");
297                         }
298                 }
299                 else if (attacker.classname == "player" || attacker.classname == "gib")
300                 {
301                         if(teamplay && attacker.team == targ.team)
302                         {
303                                 if(sv_gentle) {
304                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against a team mate!"));
305                                         bprint ("^1", a, "^1 took action against a team mate\n");
306                                 } else {
307                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s, ", a team mate!"));
308                                         bprint ("^1", a, "^1 mows down a team mate\n");
309                                 }
310                                 GiveFrags(attacker, targ, -1);
311                                 if (targ.killcount > 2) {
312                                         if(sv_gentle)
313                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," scoring spree was ended by a team mate!\n");
314                                         else
315                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," kill spree was ended by a team mate!\n");
316                                 }
317                                 if (attacker.killcount > 2) {
318                                         if(sv_gentle)
319                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," scoring spree by going against a team mate\n");
320                                         else
321                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," kill spree by killing a team mate\n");
322                                 }
323                                 attacker.killcount = 0;
324
325                                 LogDeath("tk", deathtype, attacker, targ);
326                         }
327                         else
328                         {
329                                 string blood_message, victim_message;
330                                 if (!checkrules_firstblood)
331                                 {
332                                         checkrules_firstblood = TRUE;
333                                         if(sv_gentle)
334                                         {
335                                                 bprint("^1",a, "^1 was the first to score", "\n");
336                                                 blood_message = "^1First point\n";
337                                                 //victim_message = "^1First victim\n";  // or First casualty
338                                         }
339                                         else
340                                         {
341                                                 bprint("^1",a, "^1 drew first blood", "\n");
342                                                 blood_message = "^1First blood\n";
343                                                 victim_message = "^1First victim\n";  // or First casualty
344                                         }
345                                 }
346
347                                 if(sv_gentle > 0) {
348                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s));
349                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, a,"^1 scored against you ^7"));
350                                 } else {
351                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You fragged ^7", s));
352                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were fragged by ^7", a));
353                                         attacker.taunt_soundtime = time + 1;
354                                 }
355
356                                 if(sv_gentle) {
357                                         bprint ("^1",s, "^1 needs a restart thanks to ", a, "\n");
358                                 } else {
359                                         w = DEATH_WEAPONOF(deathtype);
360                                         if(WEP_VALID(w))
361                                         {
362                                                 w_deathtypestring = "was blasted by";
363                                                 w_deathtype = deathtype;
364                                                 weapon_action(w, WR_KILLMESSAGE);
365                                                 p = strstrofs(w_deathtypestring, "#", 0);
366                                                 if(p < 0)
367                                                         bprint("^1", s, "^1 ", w_deathtypestring, " ", a, "\n");
368                                                 else
369                                                         bprint("^1", s, "^1 ", substring(w_deathtypestring, 0, p), a, "^1", substring(w_deathtypestring, p+1, strlen(w_deathtypestring) - (p+1)), "\n");
370                                         }
371                                         else if (deathtype == DEATH_TELEFRAG)
372                                                 bprint ("^1",s, "^1 was telefragged by ", a, "\n");
373                                         else if (deathtype == DEATH_DROWN)
374                                                 bprint ("^1",s, "^1 was drowned by ", a, "\n");
375                                         else if (deathtype == DEATH_SLIME)
376                                                 bprint ("^1",s, "^1 was slimed by ", a, "\n");
377                                         else if (deathtype == DEATH_LAVA)
378                                                 bprint ("^1",s, "^1 was cooked by ", a, "\n");
379                                         else if (deathtype == DEATH_FALL)
380                                                 bprint ("^1",s, "^1 was grounded by ", a, "\n");
381                                         else if (deathtype == DEATH_SHOOTING_STAR)
382                                                 bprint ("^1",s, "^1 was shot into space by ", a, "\n");
383                                         else if (deathtype == DEATH_SWAMP)
384                                                 bprint ("^1",s, "^1 was conserved by ", a, "\n");
385                                         else if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
386                                         {
387                                                 p = strstrofs(inflictor.message2, "#", 0);
388                                                 if(p < 0)
389                                                         bprint("^1", s, "^1 ", inflictor.message2, " ", a, "\n");
390                                                 else
391                                                         bprint("^1", s, "^1 ", substring(inflictor.message2, 0, p), a, "^1", substring(inflictor.message2, p+1, strlen(inflictor.message2) - (p+1)), "\n");
392                                         }
393                                         else if(deathtype == DEATH_TURRET)
394                                                 bprint ("^1",s, "^1 was pushed into the line of fire by ^1", a, "\n");
395                                         else if(deathtype == DEATH_TOUCHEXPLODE)
396                                                 bprint ("^1",s, "^1 was pushed into an accident by ^1", a, "\n");
397                                         else
398                                                 bprint ("^1",s, "^1 was fragged by ", a, "\n");
399                                 }
400
401                                 if(g_ctf && targ.flagcarried)
402                                 {
403                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
404                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
405                                         GiveFrags(attacker, targ, 0); // for logging
406                                 }
407                                 else
408                                         GiveFrags(attacker, targ, 1);
409
410                                 if (targ.killcount > 2) {
411                                         if(sv_gentle)
412                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " scoring spree was ended by ", a, "\n");
413                                         else
414                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
415                                 }
416
417                                 attacker.killcount = attacker.killcount + 1;
418
419                                 if (attacker.killcount > 2) {
420                                         if(sv_gentle)
421                                                 bprint ("^1",a,"^1 made ",ftos(attacker.killcount)," scores in a row\n");
422                                         else
423                                                 bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
424                                 }
425
426                                 LogDeath("frag", deathtype, attacker, targ);
427
428                                 if (attacker.killcount == 3)
429                                 {
430                                         if(sv_gentle) {
431                                                 bprint (a,"^7 made a ^1TRIPLE SCORE\n");
432                                         } else {
433                                                 bprint (a,"^7 made a ^1TRIPLE FRAG\n");
434                                                 announce(attacker, "announcer/male/03kills.wav");
435                                         }
436                                 }
437                                 else if (attacker.killcount == 5)
438                                 {
439                                         if(sv_gentle) {
440                                                 bprint (a,"^7 unleashes ^1SCORING RAGE\n");
441                                         } else {
442                                                 bprint (a,"^7 unleashes ^1RAGE\n");
443                                                 announce(attacker, "announcer/male/05kills.wav");
444                                         }
445                                 }
446                                 else if (attacker.killcount == 10)
447                                 {
448                                         if(sv_gentle) {
449                                                 bprint (a,"^7 made ^1TEN SCORES IN A ROW!\n");
450                                         } else {
451                                                 bprint (a,"^7 starts the ^1MASSACRE!\n");
452                                                 announce(attacker, "announcer/male/10kills.wav");
453                                         }
454                                 }
455                                 else if (attacker.killcount == 15)
456                                 {
457                                         if(sv_gentle) {
458                                                 bprint (a,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
459                                         } else {
460                                                 bprint (a,"^7 executes ^1MAYHEM!\n");
461                                                 announce(attacker, "announcer/male/15kills.wav");
462                                         }
463                                 }
464                                 else if (attacker.killcount == 20)
465                                 {
466                                         if(sv_gentle) {
467                                                 bprint (a,"^7 made ^1TWENTY SCORES IN A ROW!\n");
468                                         } else {
469                                                 bprint (a,"^7 is a ^1BERSERKER!\n");
470                                                 announce(attacker, "announcer/male/20kills.wav");
471                                         }
472                                 }
473                                 else if (attacker.killcount == 25)
474                                 {
475                                         if(sv_gentle) {
476                                                 bprint (a,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
477                                         } else {
478                                                 bprint (a,"^7 inflicts ^1CARNAGE!\n");
479                                                 announce(attacker, "announcer/male/25kills.wav");
480                                         }
481                                 }
482                                 else if (attacker.killcount == 30)
483                                 {
484                                         if(sv_gentle) {
485                                                 bprint (a,"^7 made ^1THIRTY SCORES IN A ROW!\n");
486                                         } else {
487                                                 bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
488                                                 announce(attacker, "announcer/male/30kills.wav");
489                                         }
490                                 }
491                         }
492                 }
493                 else
494                 {
495                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
496                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
497                                 bprint ("^1",s, "^1 ", inflictor.message, "\n");
498                         else if (deathtype == DEATH_DROWN)
499                                 if(sv_gentle)
500                                         bprint ("^1",s, "^1 was in the water for too long\n");
501                                 else
502                                         bprint ("^1",s, "^1 drowned\n");
503                         else if (deathtype == DEATH_SLIME)
504                                 bprint ("^1",s, "^1 was slimed\n");
505                         else if (deathtype == DEATH_LAVA)
506                                 if(sv_gentle)
507                                         bprint ("^1",s, "^1 found a hot place\n");
508                                 else
509                                         bprint ("^1",s, "^1 turned into hot slag\n");
510                         else if (deathtype == DEATH_FALL)
511                                 if(sv_gentle)
512                                         bprint ("^1",s, "^1 tested gravity (and it worked)\n");
513                                 else
514                                         bprint ("^1",s, "^1 hit the ground with a crunch\n");
515                         else if (deathtype == DEATH_SHOOTING_STAR)
516                                 bprint ("^1",s, "^1 became a shooting star\n");
517                         else if (deathtype == DEATH_SWAMP)
518                                 if(sv_gentle)
519                                         bprint ("^1",s, "^1 discovered a swamp\n");
520                                 else
521                                         bprint ("^1",s, "^1 is now conserved for centuries to come\n");
522                         else if(deathtype == DEATH_TURRET)
523                                 bprint ("^1",s, "^1 was mowed down by a turret \n");
524                         else if(deathtype == DEATH_TOUCHEXPLODE)
525                                 bprint ("^1",s, "^1 died in an accident\n");
526                         else
527                                 if(sv_gentle)
528                                         bprint ("^1",s, "^1 needs a restart\n");
529                                 else
530                                         bprint ("^1",s, "^1 died\n");
531                         GiveFrags(targ, targ, -1);
532                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
533                                 announce(targ, "announcer/male/botlike.wav");
534                         }
535
536                         if (targ.killcount > 2)
537                                 if(sv_gentle)
538                                         bprint ("^1",s,"^1 needs a restart after a ",ftos(targ.killcount)," scoring spree\n");
539                                 else
540                                         bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
541
542                         LogDeath("accident", deathtype, targ, targ);
543                 }
544                 targ.death_origin = targ.origin;
545                 if(targ != attacker)
546                         targ.killer_origin = attacker.origin;
547                 // FIXME: this should go in PutClientInServer
548                 if (targ.killcount)
549                         targ.killcount = 0;
550         }
551 }
552
553 // these are updated by each Damage call for use in button triggering and such
554 entity damage_targ;
555 entity damage_inflictor;
556 entity damage_attacker;
557
558 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
559 {
560         float mirrordamage;
561         float mirrorforce;
562         float teamdamage0;
563         entity attacker_save;
564         mirrordamage = 0;
565         mirrorforce = 0;
566
567         if (gameover || targ.killcount == -666)
568                 return;
569
570         local entity oldself;
571         oldself = self;
572         self = targ;
573         damage_targ = targ;
574         damage_inflictor = inflictor;
575         damage_attacker = attacker;
576                 attacker_save = attacker;
577         
578         if(targ.classname == "player")
579                 if(targ.hook)
580                         if(targ.hook.aiment)
581                                 if(targ.hook.aiment == attacker)
582                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
583
584         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
585         if(DEATH_ISWEAPON(deathtype, WEP_HOOK))
586         {
587                 if(targ.classname == "player")
588                         if not(IsDifferentTeam(targ, attacker))
589                         {
590                                 self = oldself;
591                                 return;
592                         }
593         }
594
595         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
596         {
597                 // These are ALWAYS lethal
598                 // No damage modification here
599                 // Instead, prepare the victim for his death...
600                 targ.armorvalue = 0;
601                 targ.spawnshieldtime = 0;
602                 targ.health = 0.9; // this is < 1
603                 targ.flags -= targ.flags & FL_GODMODE;
604                 damage = 100000;
605         }
606         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
607         {
608                 // no processing
609         }
610         else
611         {
612                 if (targ.classname == "player")
613                 if (attacker.classname == "player")
614                 if (!targ.isbot)
615                 if (attacker.isbot)
616                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
617
618                 // nullify damage if teamplay is on
619                 if(deathtype != DEATH_TELEFRAG)
620                 if(attacker.classname == "player")
621                 {
622                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
623                         {
624                                 damage = 0;
625                                 force = '0 0 0';
626                         }
627                         else if(attacker.team == targ.team)
628                         {
629                                 if(teamplay == 1)
630                                         damage = 0;
631                                 else if(attacker != targ)
632                                 {
633                                         if(teamplay == 3)
634                                                 damage = 0;
635                                         else if(teamplay == 4)
636                                         {
637                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
638                                                 {
639                                                         teamdamage0 = max(attacker.dmg_team, cvar("g_teamdamage_threshold"));
640                                                         attacker.dmg_team = attacker.dmg_team + damage;
641                                                         if(attacker.dmg_team > teamdamage0)
642                                                                 mirrordamage = cvar("g_mirrordamage") * (attacker.dmg_team - teamdamage0);
643                                                         mirrorforce = cvar("g_mirrordamage") * vlen(force);
644                                                         if(g_minstagib)
645                                                         {
646                                                                 if(cvar("g_friendlyfire") == 0)
647                                                                         damage = 0;
648                                                         }
649                                                         else
650                                                                 damage = cvar("g_friendlyfire") * damage;
651                                                         // mirrordamage will be used LATER
652                                                 }
653                                                 else
654                                                         damage = 0;
655                                         }
656                                 }
657                         }
658                 }
659
660                 if(targ.classname == "player")
661                 if(attacker.classname == "player")
662                 if(attacker != targ)
663                 {
664                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
665                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
666                 }
667
668                 if (g_minstagib)
669                 {
670                         if ((deathtype == DEATH_FALL)  ||
671                                 (deathtype == DEATH_DROWN) ||
672                                 (deathtype == DEATH_SLIME) ||
673                                 (deathtype == DEATH_LAVA))
674                         {
675                                 self = oldself;
676                                 return;
677                         }
678                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
679                         {
680                                 targ.armorvalue -= 1;
681                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(targ.armorvalue)));
682                                 damage = 0;
683                                 targ.hitsound += 1;
684                         }
685                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
686                         {
687                                 damage = 0;
688                                 if (targ != attacker)
689                                 {
690                                         if (targ.classname == "player")
691                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "Secondary fire inflicts no damage!"));
692                                         damage = 0;
693                                         mirrordamage = 0;
694                                         force = '0 0 0';
695                                         // keep mirrorforce
696                                         attacker = targ;
697                                 }
698                         }
699                 }
700
701                 // apply strength multiplier
702                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
703                 {
704                         if(targ == attacker)
705                         {
706                                 damage = damage * cvar("g_balance_powerup_strength_selfdamage");
707                                 force = force * cvar("g_balance_powerup_strength_selfforce");
708                         }
709                         else
710                         {
711                                 damage = damage * cvar("g_balance_powerup_strength_damage");
712                                 force = force * cvar("g_balance_powerup_strength_force");
713                         }
714                 }
715
716                 // apply invincibility multiplier
717                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
718                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
719
720                 if (targ == attacker)
721                         damage = damage * cvar("g_balance_selfdamagepercent");  // Partial damage if the attacker hits himself
722
723                 // CTF: reduce damage/force
724                 if(g_ctf)
725                 if(targ == attacker)
726                 if(targ.flagcarried)
727                 {
728                         damage = damage * cvar("g_ctf_flagcarrier_selfdamage");
729                         force = force * cvar("g_ctf_flagcarrier_selfforce");
730                 }
731
732                 if(g_runematch)
733                 {
734                         // apply strength rune
735                         if (attacker.runes & RUNE_STRENGTH)
736                         {
737                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
738                                 {
739                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
740                                         force = force * cvar("g_balance_rune_strength_combo_force");
741                                 }
742                                 else
743                                 {
744                                         damage = damage * cvar("g_balance_rune_strength_damage");
745                                         force = force * cvar("g_balance_rune_strength_force");
746                                 }
747                         }
748                         else if (attacker.runes & CURSE_WEAK)
749                         {
750                                 damage = damage * cvar("g_balance_curse_weak_damage");
751                                 force = force * cvar("g_balance_curse_weak_force");
752                         }
753
754                         // apply defense rune
755                         if (targ.runes & RUNE_DEFENSE)
756                         {
757                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
758                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
759                                 else
760                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
761                         }
762                         else if (targ.runes & CURSE_VULNER)
763                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
764                 }
765
766                 // count the damage
767                 if(attacker)
768                 if(!targ.deadflag)
769                 if(targ.takedamage == DAMAGE_AIM)
770                 if(targ != attacker)
771                 if(targ.classname == "player")
772                 {
773                         if(IsDifferentTeam(targ, attacker))
774                         {
775                                 if(damage > 0)
776                                 {
777                                         if(targ.BUTTON_CHAT)
778                                                 attacker.typehitsound += 1;
779                                         else
780                                                 attacker.hitsound += 1;
781
782                                         damage_goodhits += 1;
783                                         damage_gooddamage += damage;
784
785                                         if not(DEATH_ISSPECIAL(deathtype))
786                                         {
787                                                 if(!g_minstagib)
788                                                 if(IsFlying(targ))
789                                                         yoda = 1;
790
791                                                 if(g_minstagib)
792                                                 if(targ.items & IT_STRENGTH)
793                                                         yoda = 1;
794
795                                                 // HEAD SHOT:
796                                                 // find height of hit on player axis
797                                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
798                                                 vector headmins, headmaxs, org;
799                                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
800                                                 headmins = org + '0.7 0 0' * targ.mins_x + '0 0.7 0' * targ.mins_y + '0 0 1' * targ.view_ofs_z;
801                                                 headmaxs = org + '0.7 0 0' * targ.maxs_x + '0 0.7 0' * targ.maxs_y + '0 0 1' * targ.maxs_z;
802                                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
803                                                 {
804                                                         damage *= 1 + damage_headshotbonus;
805                                                         headshot = 1;
806                                                         deathtype |= HITTYPE_HEADSHOT;
807                                                 }
808                                         }
809                                 }
810                         }
811                         else
812                         {
813                                 attacker.typehitsound += 1;
814                                 if(mirrordamage > 0)
815                                         if(time > attacker.teamkill_complain)
816                                         {
817                                                 attacker.teamkill_complain = time + 5;
818                                                 attacker.teamkill_soundtime = time + 0.4;
819                                                 attacker.teamkill_soundsource = targ;
820                                         }
821                         }
822                 }
823         }
824
825         // apply push
826         if (self.damageforcescale)
827         if (vlen(force))
828         {
829                 self.velocity = self.velocity + self.damageforcescale * force;
830                 self.flags = self.flags - (self.flags & FL_ONGROUND);
831                 UpdateCSQCProjectile(self);
832         }
833         // apply damage
834         if (damage != 0)
835         if (self.event_damage)
836                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
837         self = oldself;
838
839         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
840         {
841                 // Savage: vampire mode
842                 if (g_vampire)
843                 if (!g_minstagib)
844                 if (time > self.spawnshieldtime)
845                 {
846                         attacker.health += damage;
847                 }
848                 if(g_runematch)
849                 {
850                         if (attacker.runes & RUNE_VAMPIRE)
851                         {
852                         // apply vampire rune
853                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
854                                 {
855                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
856                                         attacker.health = bound(
857                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
858                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
859                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
860                                 }
861                                 else
862                                 {
863                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
864                                         attacker.health = bound(
865                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
866                                                                                         // empathy won't let you gain health in the same way...
867                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
868                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
869                                         }
870                         }
871                         // apply empathy curse
872                         else if (attacker.runes & CURSE_EMPATHY)
873                         {
874                                 attacker.health = bound(
875                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
876                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
877                                         attacker.health);
878                         }
879                 }
880         }
881
882         // apply mirror damage if any
883         if(mirrordamage > 0 || mirrorforce > 0)
884         {
885                 attacker = attacker_save;
886                 if(g_minstagib)
887                         if(mirrordamage > 0)
888                         {
889                                 // just lose extra LIVES, don't kill the player for mirror damage
890                                 if(attacker.armorvalue > 0)
891                                 {
892                                         attacker.armorvalue = attacker.armorvalue - 1;
893                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
894                                         attacker.hitsound += 1;
895                                 }
896                                 mirrordamage = 0;
897                         }
898                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
899                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
900         }
901 }
902
903 vector NearestPointOnBox(entity box, vector org)
904 {
905         vector m1, m2, nearest;
906
907         m1 = box.mins + box.origin;
908         m2 = box.maxs + box.origin;
909
910         nearest_x = bound(m1_x, org_x, m2_x);
911         nearest_y = bound(m1_y, org_y, m2_y);
912         nearest_z = bound(m1_z, org_z, m2_z);
913
914         return nearest;
915 }
916
917 float RadiusDamage_running;
918 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
919 // Returns total damage applies to creatures
920 {
921         entity  targ;
922         float   finaldmg;
923         float   power;
924         vector  blastorigin;
925         vector  force;
926         vector  diff;
927         vector  center;
928         vector  nearest;
929         float   total_damage_to_creatures;
930         entity  next;
931
932         if(RadiusDamage_running)
933         {
934                 string save;
935                 print("RadiusDamage called recursively!\n");
936                 print("Expect stuff to go HORRIBLY wrong.\n");
937                 print("Causing a stack trace...\n");
938                 save = cvar_string("prvm_backtraceforwarnings");
939                 cvar_set("prvm_backtraceforwarnings", "1");
940                 fclose(-1); // calls VM_Warning
941                 cvar_set("prvm_backtraceforwarnings", save);
942                 return 0;
943         }
944
945
946         RadiusDamage_running = 1;
947
948         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
949         total_damage_to_creatures = 0;
950
951         if(deathtype == WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE) // only send gravity bomb damage once
952                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * '0 0 1', deathtype);
953
954         targ = findradius (blastorigin, rad);
955         while (targ)
956         {
957                 next = targ.chain;
958                 if (targ != inflictor)
959                         if (ignore != targ)
960                         {
961                                 // LordHavoc: measure distance to nearest point on target (not origin)
962                                 // (this guarentees 100% damage on a touch impact)
963                                 nearest = NearestPointOnBox(targ, blastorigin);
964                                 diff = nearest - blastorigin;
965                                 // round up a little on the damage to ensure full damage on impacts
966                                 // and turn the distance into a fraction of the radius
967                                 power = 1 - ((vlen (diff) - 2) / rad);
968                                 //bprint(" ");
969                                 //bprint(ftos(power));
970                                 if (power > 0)
971                                 {
972                                         if (power > 1)
973                                                 power = 1;
974                                         finaldmg = coredamage * power + edgedamage * (1 - power);
975                                         if (finaldmg > 0)
976                                         {
977                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
978                                                 // if it's a player, use the view origin as reference
979                                                 if (targ.classname == "player")
980                                                         center = targ.origin + targ.view_ofs;
981                                                 force = normalize(center - blastorigin) * (finaldmg / coredamage) * forceintensity;
982                                                 // test line of sight to multiple positions on box,
983                                                 // and do damage if any of them hit
984                                                 local float c;
985                                                 c = ceil(finaldmg / 10);
986                                                 if (c > 20)
987                                                         c = 20;
988                                                 while (c > 0)
989                                                 {
990                                                         c = c - 1;
991                                                         traceline(blastorigin, nearest, TRUE, inflictor);
992                                                         if (trace_fraction == 1 || trace_ent == targ
993                                                             || cvar("g_throughfloor"))
994                                                         {
995                                                                 if(targ.iscreature)
996                                                                         total_damage_to_creatures += finaldmg;
997                                                                 if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
998                                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
999                                                                 else
1000                                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1001                                                                 break;
1002                                                         }
1003                                                         nearest_x = targ.mins_x + random() * targ.size_x;
1004                                                         nearest_y = targ.mins_y + random() * targ.size_y;
1005                                                         nearest_z = targ.mins_z + random() * targ.size_z;
1006                                                 }
1007                                         }
1008                                 }
1009                         }
1010                 targ = next;
1011         }
1012
1013         RadiusDamage_running = 0;
1014
1015         return total_damage_to_creatures;
1016 }