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