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