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