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