]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
Working on new laser primary attack -- now has no projectile and works with its own...
[xonotic/xonotic-data.pk3dir.git] / 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         WriteByte(MSG_ENTITY, self.species);
18         return TRUE;
19 }
20
21 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, float deathtype, float bloodtype, entity dmgowner)
22 {
23         // TODO maybe call this from non-edgedamage too?
24         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
25
26         entity e;
27
28         if(!sound_allowed(MSG_BROADCAST, dmgowner))
29                 deathtype |= 0x8000;
30
31         e = spawn();
32         setorigin(e, org);
33         e.projectiledeathtype = deathtype;
34         e.dmg = coredamage;
35         e.dmg_edge = edgedamage;
36         e.dmg_radius = rad;
37         e.dmg_force = vlen(force);
38         e.velocity = force;
39         e.oldorigin_x = compressShortVector(e.velocity);
40         e.species = bloodtype;
41
42         Net_LinkEntity(e, FALSE, 0.2, Damage_DamageInfo_SendEntity);
43 }
44
45 float checkrules_firstblood;
46
47 float yoda;
48 float damage_goodhits;
49 float damage_gooddamage;
50 float headshot;
51 float damage_headshotbonus; // bonus multiplier for head shots, set to 0 after use
52
53 .float dmg_team;
54 .float teamkill_complain;
55 .float teamkill_soundtime;
56 .entity teamkill_soundsource;
57 .entity pusher;
58 .float taunt_soundtime;
59
60
61 float IsDifferentTeam(entity a, entity b)
62 {
63         if(teamplay)
64         {
65                 if(a.team == b.team)
66                         return 0;
67         }
68         else
69         {
70                 if(a == b)
71                         return 0;
72         }
73         return 1;
74 }
75
76 float IsFlying(entity a)
77 {
78         if(a.flags & FL_ONGROUND)
79                 return 0;
80         if(a.waterlevel >= WATERLEVEL_SWIMMING)
81                 return 0;
82         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
83         if(trace_fraction < 1)
84                 return 0;
85         return 1;
86 }
87
88 vector GetHeadshotMins(entity targ)
89 {
90         return '-0.5 0 0' * PL_HEAD_x + '0 -0.5 0' * PL_HEAD_y + '0 0 1' * (targ.maxs_z - PL_HEAD_z);
91 }
92 vector GetHeadshotMaxs(entity targ)
93 {
94         return '0.5 0 0' * PL_HEAD_x + '0 0.5 0' * PL_HEAD_y + '0 0 1' * targ.maxs_z;
95 }
96
97 void UpdateFrags(entity player, float f)
98 {
99         PlayerTeamScore_AddScore(player, f);
100 }
101
102 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
103 void W_SwitchWeapon_Force(entity e, float w);
104 entity GiveFrags_randomweapons;
105 void GiveFrags (entity attacker, entity targ, float f, float deathtype)
106 {
107         // TODO route through PlayerScores instead
108         if(gameover) return;
109
110         if(f < 0)
111         {
112                 if(targ == attacker)
113                 {
114                         // suicide
115                         PlayerScore_Add(attacker, SP_SUICIDES, 1);
116                 }
117                 else
118                 {
119                         // teamkill
120                         PlayerScore_Add(attacker, SP_KILLS, -1); // or maybe add a teamkills field?
121                 }
122         }
123         else
124         {
125                 // regular frag
126                 PlayerScore_Add(attacker, SP_KILLS, 1);
127                 if(targ.playerid)
128                         PlayerStats_Event(attacker, sprintf("kills-%d", targ.playerid), 1);
129         }
130
131         PlayerScore_Add(targ, SP_DEATHS, 1);
132
133         if(g_arena || g_ca)
134                 if(autocvar_g_arena_roundbased)
135                         return;
136
137         if(targ != attacker) // not for suicides
138         if(g_weaponarena_random)
139         {
140                 // after a frag, exchange the current weapon (or the culprit, if detectable) by a new random weapon
141                 float culprit;
142                 culprit = DEATH_WEAPONOF(deathtype);
143                 if(!culprit)
144                         culprit = attacker.weapon;
145                 else if(!WEPSET_CONTAINS_EW(attacker, culprit))
146                         culprit = attacker.weapon;
147
148                 if(g_weaponarena_random_with_laser && culprit == WEP_LASER)
149                 {
150                         // no exchange
151                 }
152                 else
153                 {
154                         if(!GiveFrags_randomweapons)
155                         {
156                                 GiveFrags_randomweapons = spawn();
157                                 GiveFrags_randomweapons.classname = "GiveFrags_randomweapons";
158                         }
159
160                         if(inWarmupStage)
161                                 WEPSET_COPY_EA(GiveFrags_randomweapons, warmup_start_weapons);
162                         else
163                                 WEPSET_COPY_EA(GiveFrags_randomweapons, start_weapons);
164
165                         // all others (including the culprit): remove
166                         WEPSET_ANDNOT_EE(GiveFrags_randomweapons, attacker);
167                         WEPSET_ANDNOT_EW(GiveFrags_randomweapons, culprit);
168
169                         // among the remaining ones, choose one by random
170                         W_RandomWeapons(GiveFrags_randomweapons, 1);
171
172                         if(!WEPSET_EMPTY_E(GiveFrags_randomweapons))
173                         {
174                                 WEPSET_OR_EE(attacker, GiveFrags_randomweapons);
175                                 WEPSET_ANDNOT_EW(attacker, culprit);
176                         }
177                 }
178
179                 // after a frag, choose another random weapon set
180                 if not(WEPSET_CONTAINS_EW(attacker, attacker.weapon))
181                         W_SwitchWeapon_Force(attacker, w_getbestweapon(attacker));
182         }
183
184         // FIXME fix the mess this is (we have REAL points now!)
185         entity oldself;
186         oldself = self;
187         self = attacker;
188         frag_attacker = attacker;
189         frag_target = targ;
190         frag_score = f;
191         if(MUTATOR_CALLHOOK(GiveFragsForKill))
192         {
193                 f = frag_score;
194                 self = oldself;
195         }
196         else
197         {
198                 self = oldself;
199                 if(g_runematch)
200                 {
201                         f = RunematchHandleFrags(attacker, targ, f);
202                 }
203                 else if(g_lms)
204                 {
205                         // remove a life
206                         float tl;
207                         tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
208                         if(tl < lms_lowest_lives)
209                                 lms_lowest_lives = tl;
210                         if(tl <= 0)
211                         {
212                                 if(!lms_next_place)
213                                         lms_next_place = player_count;
214                                 else
215                                         lms_next_place = min(lms_next_place, player_count);
216                                 PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
217                                 --lms_next_place;
218                         }
219                         f = 0;
220                 }
221                 else if(g_ctf)
222                 {
223                         if(g_ctf_ignore_frags)
224                                 f = 0;
225                 }
226         }
227
228         attacker.totalfrags += f;
229
230         if(f)
231                 UpdateFrags(attacker, f);
232 }
233
234 string Obituary_ExtraFragInfo(entity player) // Extra fragmessage information
235 {
236         string health_output = string_null;
237         string ping_output = string_null;
238         string handicap_output = string_null;
239         string output = string_null;
240
241         if(autocvar_sv_fraginfo && ((autocvar_sv_fraginfo == 2) || inWarmupStage))
242         {
243                 // health/armor of attacker (person who killed you)
244                 if(autocvar_sv_fraginfo_stats && (player.health >= 1))
245                         health_output = strcat("^7(Health ^1", ftos(rint(player.health)), "^7 / Armor ^2", ftos(rint(player.armorvalue)), "^7)");
246                 
247                 // ping display
248                 if(autocvar_sv_fraginfo_ping)
249                         ping_output = ((clienttype(player) == CLIENTTYPE_BOT) ? "^2Bot" : strcat("Ping ", ((player.ping >= 150) ? "^1" : "^2"), ftos(rint(player.ping)), "ms"));
250                         
251                 // handicap display 
252                 if(autocvar_sv_fraginfo_handicap) 
253                 {
254                         if(autocvar_sv_fraginfo_handicap == 2)  
255                                 handicap_output = strcat(output, strcat("Handicap ^2", ((player.cvar_cl_handicap <= 1) ? "Off" : ftos(rint(player.cvar_cl_handicap)))));
256                         else if(player.cvar_cl_handicap) // with _handicap 1, only show this if there actually is a handicap enabled.   
257                                 handicap_output = strcat("Handicap ^2", ftos(rint(player.cvar_cl_handicap)));
258                 }
259                 
260                 // format the string
261                 output = strcat(health_output, (health_output ? ((ping_output || handicap_output) ? " ^7(" : "") : ((ping_output || handicap_output) ? "^7(" : "")), 
262                         ping_output, (handicap_output ? "^7 / " : ""), 
263                         handicap_output, ((ping_output || handicap_output) ? "^7)" : ""));
264                 
265                 // add new line to the beginning if there is a message
266                 if(output) { output = strcat("\n", output); }
267         }
268         
269         return output;
270 }
271
272 string AppendItemcodes(string s, entity player)
273 {
274         float w;
275         w = player.weapon;
276         //if(w == 0)
277         //      w = player.switchweapon;
278         if(w == 0)
279                 w = player.cnt; // previous weapon!
280         s = strcat(s, ftos(w));
281         if(time < player.strength_finished)
282                 s = strcat(s, "S");
283         if(time < player.invincible_finished)
284                 s = strcat(s, "I");
285         if(player.flagcarried != world)
286                 s = strcat(s, "F");
287         if(player.BUTTON_CHAT)
288                 s = strcat(s, "T");
289         if(player.kh_next)
290                 s = strcat(s, "K");
291         if(player.runes)
292                 s = strcat(s, "|", ftos(player.runes));
293         return s;
294 }
295
296 void LogDeath(string mode, float deathtype, entity killer, entity killed)
297 {
298         string s;
299         if(!autocvar_sv_eventlog)
300                 return;
301         s = strcat(":kill:", mode);
302         s = strcat(s, ":", ftos(killer.playerid));
303         s = strcat(s, ":", ftos(killed.playerid));
304         s = strcat(s, ":type=", ftos(deathtype));
305         s = strcat(s, ":items=");
306         s = AppendItemcodes(s, killer);
307         if(killed != killer)
308         {
309                 s = strcat(s, ":victimitems=");
310                 s = AppendItemcodes(s, killed);
311         }
312         GameLogEcho(s);
313 }
314
315 void Send_KillNotification (string s1, string s2, string s3, float msg, float type)
316 {
317         WriteByte(MSG_ALL, SVC_TEMPENTITY);
318         WriteByte(MSG_ALL, TE_CSQC_KILLNOTIFY);
319         WriteString(MSG_ALL, s1);
320         WriteString(MSG_ALL, s2);
321         WriteString(MSG_ALL, s3);
322         WriteShort(MSG_ALL, msg);
323         WriteByte(MSG_ALL, type);
324 }
325
326 // Function is used to send a generic centerprint whose content CSQC gets to decide (gentle version or not in the below cases)
327 void Send_CSQC_KillCenterprint(entity e, string s1, string s2, float msg, float type)
328 {
329         if (clienttype(e) == CLIENTTYPE_REAL)
330         {
331                 msg_entity = e;
332                 WRITESPECTATABLE_MSG_ONE({
333                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
334                         WriteByte(MSG_ONE, TE_CSQC_KILLCENTERPRINT);
335                         WriteString(MSG_ONE, s1);
336                         WriteString(MSG_ONE, s2);
337                         WriteShort(MSG_ONE, msg);
338                         WriteByte(MSG_ONE, type);
339                 });
340         }
341 }
342
343 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
344 {
345         string  s, a, msg;
346         float w, type;
347
348         if (targ.classname == "player")
349         {
350                 s = targ.netname;
351                 a = attacker.netname;
352
353                 if (targ == attacker) // suicides
354                 {
355                         if (deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
356                                 msg = ColoredTeamName(targ.team); // TODO: check if needed?
357                         else
358                                 msg = "";
359             if(!g_cts) // no "killed your own dumb self" message in CTS
360                 Send_CSQC_KillCenterprint(targ, msg, "", deathtype, MSG_SUICIDE);
361
362                         if(deathtype != DEATH_TEAMCHANGE && deathtype != DEATH_QUIET)
363                         {
364                                 LogDeath("suicide", deathtype, targ, targ);
365                                 GiveFrags(attacker, targ, -1, deathtype);
366                         }
367
368                         if (targ.killcount > 2)
369                                 msg = ftos(targ.killcount);
370                         else
371                                 msg = "";
372                         if(teamplay && deathtype == DEATH_MIRRORDAMAGE)
373                         {
374                                 if(attacker.team == COLOR_TEAM1)
375                                         deathtype = KILL_TEAM_RED;
376                                 else
377                                         deathtype = KILL_TEAM_BLUE;
378                         }
379
380                         Send_KillNotification(s, msg, ftos(w), deathtype, MSG_SUICIDE);
381                 }
382                 else if (attacker.classname == "player")
383                 {
384                         if(!IsDifferentTeam(attacker, targ))
385                         {
386                                 if(attacker.team == COLOR_TEAM1)
387                                         type = KILL_TEAM_RED;
388                                 else
389                                         type = KILL_TEAM_BLUE;
390
391                                 GiveFrags(attacker, targ, -1, deathtype);
392
393                                 Send_CSQC_KillCenterprint(attacker, s, "", type, MSG_KILL);
394
395                                 if (targ.killcount > 2)
396                                         msg = ftos(targ.killcount);
397                                 else
398                                         msg = "";
399
400                                 if (attacker.killcount > 2) {
401                                         msg = ftos(attacker.killcount);
402                                         type = KILL_TEAM_SPREE;
403                                 }
404                                 Send_KillNotification(a, s, msg, type, MSG_KILL);
405
406                                 attacker.killcount = 0;
407
408                                 LogDeath("tk", deathtype, attacker, targ);
409                         }
410                         else
411                         {
412                                 if (!checkrules_firstblood)
413                                 {
414                                         checkrules_firstblood = TRUE;
415                                         Send_KillNotification(a, "", "", KILL_FIRST_BLOOD, MSG_KILL);
416                                         // TODO: make these print a newline if they dont
417                                         Send_CSQC_KillCenterprint(attacker, "", "", KILL_FIRST_BLOOD, MSG_KILL);
418                                         Send_CSQC_KillCenterprint(targ, "", "", KILL_FIRST_VICTIM, MSG_KILL);
419                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD, 1);
420                                         PlayerStats_Event(targ, PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM, 1);
421                                 }
422
423                                 if(targ.BUTTON_CHAT) {
424                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_TYPEFRAG, MSG_KILL);
425                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_TYPEFRAGGED, MSG_KILL);
426                                 } else {
427                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_FRAG, MSG_KILL);
428                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_FRAGGED, MSG_KILL);
429                                 }
430
431                                 attacker.taunt_soundtime = time + 1;
432
433                                 if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
434                                         msg = inflictor.message2;
435                                 else if (deathtype == DEATH_CUSTOM)
436                                         msg = deathmessage;
437                                 else
438                                         msg = "";
439
440                                 if(strstrofs(msg, "%", 0) < 0)
441                                         msg = strcat("%s ", msg, " by %s");
442
443                                 Send_KillNotification(a, s, msg, deathtype, MSG_KILL);
444
445                                 if(g_ctf && targ.flagcarried)
446                                 {
447                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
448                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
449                                         GiveFrags(attacker, targ, 0, deathtype); // for logging
450                                 }
451                                 else
452                                         GiveFrags(attacker, targ, 1, deathtype);
453
454                                 if (targ.killcount > 2) {
455                                         Send_KillNotification(s, ftos(targ.killcount), a, KILL_END_SPREE, MSG_SPREE);
456                                 }
457
458                                 attacker.killcount = attacker.killcount + 1;
459
460                                 if (attacker.killcount > 2) {
461                                         Send_KillNotification(a, ftos(attacker.killcount), "", KILL_SPREE, MSG_SPREE);
462                                 }
463                                 else if (attacker.killcount == 3)
464                                 {
465                                         Send_KillNotification(a, "", "", KILL_SPREE_3, MSG_SPREE);
466                                         AnnounceTo(attacker, "03kills");
467                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3, 1);
468                                 }
469                                 else if (attacker.killcount == 5)
470                                 {
471                                         Send_KillNotification(a, "", "", KILL_SPREE_5, MSG_SPREE);
472                                         AnnounceTo(attacker, "05kills");
473                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5, 1);
474                                 }
475                                 else if (attacker.killcount == 10)
476                                 {
477                                         Send_KillNotification(a, "", "", KILL_SPREE_10, MSG_SPREE);
478                                         AnnounceTo(attacker, "10kills");
479                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10, 1);
480                                 }
481                                 else if (attacker.killcount == 15)
482                                 {
483                                         Send_KillNotification(a, "", "", KILL_SPREE_15, MSG_SPREE);
484                                         AnnounceTo(attacker, "15kills");
485                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15, 1);
486                                 }
487                                 else if (attacker.killcount == 20)
488                                 {
489                                         Send_KillNotification(a, "", "", KILL_SPREE_20, MSG_SPREE);
490                                         AnnounceTo(attacker, "20kills");
491                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20, 1);
492                                 }
493                                 else if (attacker.killcount == 25)
494                                 {
495                                         Send_KillNotification(a, "", "", KILL_SPREE_25, MSG_SPREE);
496                                         AnnounceTo(attacker, "25kills");
497                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25, 1);
498                                 }
499                                 else if (attacker.killcount == 30)
500                                 {
501                                         Send_KillNotification(a, "", "", KILL_SPREE_30, MSG_SPREE);
502                                         AnnounceTo(attacker, "30kills");
503                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30, 1);
504                                 }
505                                 LogDeath("frag", deathtype, attacker, targ);
506                         }
507                 }
508                 else
509                 {
510                         Send_CSQC_KillCenterprint(targ, "", "", deathtype, MSG_KILL_ACTION);
511                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
512                                 msg = inflictor.message;
513                         else if (deathtype == DEATH_CUSTOM)
514                                 msg = deathmessage;
515                         else
516                                 msg = "";
517                         if(strstrofs(msg, "%", 0) < 0)
518                                 msg = strcat("%s ", msg);
519
520                         GiveFrags(targ, targ, -1, deathtype);
521                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
522                                 AnnounceTo(targ, "botlike");
523                                 PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
524                         }
525                         Send_KillNotification(s, msg, "", deathtype, MSG_KILL_ACTION);
526
527                         if (targ.killcount > 2)
528                                 Send_KillNotification(s, ftos(targ.killcount), "", 0, MSG_KILL_ACTION_SPREE);
529
530                         LogDeath("accident", deathtype, targ, targ);
531                 }
532
533                 targ.death_origin = targ.origin;
534                 if(targ != attacker)
535                         targ.killer_origin = attacker.origin;
536
537                 // FIXME: this should go in PutClientInServer
538                 if (targ.killcount)
539                         targ.killcount = 0;
540         }
541 }
542
543 // these are updated by each Damage call for use in button triggering and such
544 entity damage_targ;
545 entity damage_inflictor;
546 entity damage_attacker;
547
548 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
549 {
550         float mirrordamage;
551         float mirrorforce;
552         float teamdamage0;
553         entity attacker_save;
554         mirrordamage = 0;
555         mirrorforce = 0;
556
557         if (gameover || targ.killcount == -666)
558                 return;
559
560         entity oldself;
561         oldself = self;
562         self = targ;
563         damage_targ = targ;
564         damage_inflictor = inflictor;
565         damage_attacker = attacker;
566                 attacker_save = attacker;
567
568         if(targ.classname == "player")
569                 if(targ.hook)
570                         if(targ.hook.aiment)
571                                 if(targ.hook.aiment == attacker)
572                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
573
574         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
575         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
576         {
577                 if(targ.classname == "player")
578                         if not(IsDifferentTeam(targ, attacker))
579                         {
580                                 self = oldself;
581                                 return;
582                         }
583         }
584
585         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE || deathtype == DEATH_QUIET)
586         {
587                 // These are ALWAYS lethal
588                 // No damage modification here
589                 // Instead, prepare the victim for his death...
590                 targ.armorvalue = 0;
591                 targ.spawnshieldtime = 0;
592                 targ.health = 0.9; // this is < 1
593                 targ.flags -= targ.flags & FL_GODMODE;
594                 damage = 100000;
595         }
596         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
597         {
598                 // no processing
599         }
600         else
601         {
602                 /*
603                 skill based bot damage? gtfo. (tZork)
604                 if (targ.classname == "player")
605                 if (attacker.classname == "player")
606                 if (!targ.isbot)
607                 if (attacker.isbot)
608                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
609         */
610         
611                 // nullify damage if teamplay is on
612                 if(deathtype != DEATH_TELEFRAG)
613                 if(attacker.classname == "player")
614                 {
615                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
616                         {
617                                 damage = 0;
618                                 force = '0 0 0';
619                         }
620                         else if(!IsDifferentTeam(attacker, targ))
621                         {
622                                 if(autocvar_teamplay_mode == 1)
623                                         damage = 0;
624                                 else if(attacker != targ)
625                                 {
626                                         if(autocvar_teamplay_mode == 3)
627                                                 damage = 0;
628                                         else if(autocvar_teamplay_mode == 4)
629                                         {
630                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
631                                                 {
632                                                         teamdamage0 = max(attacker.dmg_team, autocvar_g_teamdamage_threshold);
633                                                         attacker.dmg_team = attacker.dmg_team + damage;
634                                                         if(attacker.dmg_team > teamdamage0 && !g_ca)
635                                                                 mirrordamage = autocvar_g_mirrordamage * (attacker.dmg_team - teamdamage0);
636                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
637                                                         if(g_minstagib)
638                                                         {
639                                                                 if(autocvar_g_friendlyfire == 0)
640                                                                         damage = 0;
641                                                         }
642                                                         else if(g_ca)
643                                                                 damage = 0;
644                                                         else
645                                                                 damage = autocvar_g_friendlyfire * damage;
646                                                         // mirrordamage will be used LATER
647
648                                                         if(autocvar_g_mirrordamage_virtual)
649                                                         {
650                                                                 vector v = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
651                                                                 attacker.dmg_take += v_x;
652                                                                 attacker.dmg_save += v_y;
653                                                                 attacker.dmg_inflictor = inflictor;
654                                                                 mirrordamage = 0;
655                                                                 mirrorforce = 0;
656                                                         }
657
658                                                         if(autocvar_g_friendlyfire_virtual)
659                                                         {
660                                                                 vector v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
661                                                                 targ.dmg_take += v_x;
662                                                                 targ.dmg_save += v_y;
663                                                                 targ.dmg_inflictor = inflictor;
664                                                                 damage = 0;
665                                                                 if(!autocvar_g_friendlyfire_virtual_force)
666                                                                         force = '0 0 0';
667                                                         }
668                                                 }
669                                                 else
670                                                         damage = 0;
671                                         }
672                                 }
673                         }
674                 }
675
676                 if(targ.classname == "player")
677                 if(attacker.classname == "player")
678                 if(attacker != targ)
679                 {
680                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
681                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
682                 }
683
684                 if(targ.classname == "player")
685                 if (g_minstagib)
686                 {
687                         if ((deathtype == DEATH_FALL)  ||
688                                 (deathtype == DEATH_DROWN) ||
689                                 (deathtype == DEATH_SLIME) ||
690                                 (deathtype == DEATH_LAVA)  ||
691                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
692                         {
693                                 self = oldself;
694                                 return;
695                         }
696                         if(damage > 0)
697                             damage = 10000;
698                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
699                         {
700                                 targ.armorvalue -= 1;
701                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
702                                 damage = 0;
703                                 targ.hitsound += 1;
704                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
705                         }
706                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
707                         {
708                                 damage = 0;
709                                 mirrordamage = 0;
710                                 if (targ != attacker)
711                                 {
712                                         if ((targ.health >= 1) && (targ.classname == "player"))
713                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
714                                         force = '0 0 0';
715                                         // keep mirrorforce
716                                         attacker = targ;
717                                 }
718                         }
719                 }
720
721                 if not(DEATH_ISSPECIAL(deathtype))
722                 {
723                         damage *= g_weapondamagefactor;
724                         mirrordamage *= g_weapondamagefactor;
725                         force = force * g_weaponforcefactor;
726                         mirrorforce *= g_weaponforcefactor;
727                 }
728                 
729                 // should this be changed at all? If so, in what way?
730                 frag_attacker = attacker;
731                 frag_target = targ;
732                 frag_damage = damage;
733                 frag_force = force;
734         frag_deathtype = deathtype;
735                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
736                 damage = frag_damage;
737                 force = frag_force;
738                 
739                 // apply strength multiplier
740                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
741                 {
742                         if(targ == attacker)
743                         {
744                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
745                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
746                         }
747                         else
748                         {
749                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
750                                 force = force * autocvar_g_balance_powerup_strength_force;
751                         }
752                 }
753
754                 // apply invincibility multiplier
755                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
756                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
757
758                 if (targ == attacker)
759                 {
760                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
761                                 damage = 0;
762                         else
763                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
764                 }
765
766                 // CTF: reduce damage/force
767                 if(g_ctf)
768                 if(targ == attacker)
769                 if(targ.flagcarried)
770                 {
771                         damage = damage * autocvar_g_ctf_flagcarrier_selfdamage;
772                         force = force * autocvar_g_ctf_flagcarrier_selfforce;
773                 }
774
775                 if(g_runematch)
776                 {
777                         // apply strength rune
778                         if (attacker.runes & RUNE_STRENGTH)
779                         {
780                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
781                                 {
782                                         damage = damage * autocvar_g_balance_rune_strength_combo_damage;
783                                         force = force * autocvar_g_balance_rune_strength_combo_force;
784                                 }
785                                 else
786                                 {
787                                         damage = damage * autocvar_g_balance_rune_strength_damage;
788                                         force = force * autocvar_g_balance_rune_strength_force;
789                                 }
790                         }
791                         else if (attacker.runes & CURSE_WEAK)
792                         {
793                                 damage = damage * autocvar_g_balance_curse_weak_damage;
794                                 force = force * autocvar_g_balance_curse_weak_force;
795                         }
796
797                         // apply defense rune
798                         if (targ.runes & RUNE_DEFENSE)
799                         {
800                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
801                                         damage = damage * autocvar_g_balance_rune_defense_combo_takedamage;
802                                 else
803                                         damage = damage * autocvar_g_balance_rune_defense_takedamage;
804                         }
805                         else if (targ.runes & CURSE_VULNER)
806                                 damage = damage * autocvar_g_balance_curse_vulner_takedamage;
807                 }
808
809                 // count the damage
810                 if(attacker)
811                 if(!targ.deadflag)
812                 if(targ.takedamage == DAMAGE_AIM)
813                 if(targ != attacker)
814                 {
815                         if(damage_headshotbonus)
816                         {
817                                 if(targ.classname == "player")
818                                 {
819                                         // HEAD SHOT:
820                                         // find height of hit on player axis
821                                         // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
822                                         vector headmins, headmaxs, org;
823                                         org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
824                                         headmins = org + GetHeadshotMins(targ);
825                                         headmaxs = org + GetHeadshotMaxs(targ);
826                                         if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
827                                         {
828                                                 deathtype |= HITTYPE_HEADSHOT;
829                                         }
830                                 }
831                                 else if(targ.classname == "turret_head")
832                                 {
833                                         deathtype |= HITTYPE_HEADSHOT;
834                                 }
835                                 if(deathtype & HITTYPE_HEADSHOT)
836                                         if(damage_headshotbonus > 0)
837                                                 damage *= 1 + damage_headshotbonus;
838                         }
839
840                         entity victim;
841                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
842                                 victim = targ.owner;
843                         else
844                                 victim = targ;
845
846                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
847                         {
848                                 if(IsDifferentTeam(victim, attacker))
849                                 {
850                                         if(damage > 0)
851                                         {
852                                                 if(deathtype != DEATH_FIRE)
853                                                 {
854                                                         if(victim.BUTTON_CHAT)
855                                                                 attacker.typehitsound += 1;
856                                                         else
857                                                                 attacker.hitsound += 1;
858                                                 }
859
860                                                 damage_goodhits += 1;
861                                                 damage_gooddamage += damage;
862
863                                                 if not(DEATH_ISSPECIAL(deathtype))
864                                                 {
865                                                         if(targ.classname == "player") // don't do this for vehicles
866                                                         if(!g_minstagib)
867                                                         if(IsFlying(victim))
868                                                                 yoda = 1;
869
870                                                         if(g_minstagib)
871                                                         if(victim.items & IT_STRENGTH)
872                                                                 yoda = 1;
873
874                                                         if(deathtype & HITTYPE_HEADSHOT)
875                                                                 headshot = 1;
876                                                 }
877                                                 if(g_ca)
878                                                         PlayerScore_Add(attacker, SP_SCORE, damage * autocvar_g_ca_damage2score_multiplier);
879                                         }
880                                 }
881                                 else
882                                 {
883                                         if(deathtype != DEATH_FIRE)
884                                         {
885                                                 attacker.typehitsound += 1;
886                                         }
887                                         if(mirrordamage > 0)
888                                                 if(time > attacker.teamkill_complain)
889                                                 {
890                                                         attacker.teamkill_complain = time + 5;
891                                                         attacker.teamkill_soundtime = time + 0.4;
892                                                         attacker.teamkill_soundsource = targ;
893                                                 }
894                                 }
895                         }
896                 }
897         }
898
899         // apply push
900         if (self.damageforcescale)
901         if (vlen(force))
902         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
903         {
904                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
905                 if(self.movetype == MOVETYPE_PHYSICS)
906                 {
907                         entity farcent;
908                         farcent = spawn();
909                         farcent.classname = "farce";
910                         farcent.enemy = self;
911                         farcent.movedir = farce * 10;
912                         if(self.mass)
913                                 farcent.movedir = farcent.movedir * self.mass;
914                         farcent.origin = hitloc;
915                         farcent.forcetype = FORCETYPE_FORCEATPOS;
916                         farcent.nextthink = time + 0.1;
917                         farcent.think = SUB_Remove;
918                 }
919                 else
920                         self.velocity = self.velocity + farce;
921                 self.flags &~= FL_ONGROUND;
922                 UpdateCSQCProjectile(self);
923         }
924         // apply damage
925         if (damage != 0 || (self.damageforcescale && vlen(force)))
926         if (self.event_damage)
927                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
928         self = oldself;
929
930         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
931         {
932                 if(g_runematch)
933                 {
934                         if (attacker.runes & RUNE_VAMPIRE)
935                         {
936                         // apply vampire rune
937                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
938                                 {
939                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb;
940                                         attacker.health = bound(
941                                                 autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 40
942                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb,
943                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
944                                 }
945                                 else
946                                 {
947                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_absorb;
948                                         attacker.health = bound(
949                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
950                                                                                         // empathy won't let you gain health in the same way...
951                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_absorb,
952                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
953                                         }
954                         }
955                         // apply empathy curse
956                         else if (attacker.runes & CURSE_EMPATHY)
957                         {
958                                 attacker.health = bound(
959                                         autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 20
960                                         attacker.health + damage * autocvar_g_balance_curse_empathy_takedamage,
961                                         attacker.health);
962                         }
963                 }
964         }
965
966         // apply mirror damage if any
967         if(mirrordamage > 0 || mirrorforce > 0)
968         {
969                 attacker = attacker_save;
970                 if(g_minstagib)
971                 if(mirrordamage > 0)
972                 {
973                         // just lose extra LIVES, don't kill the player for mirror damage
974                         if(attacker.armorvalue > 0)
975                         {
976                                 attacker.armorvalue = attacker.armorvalue - 1;
977                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
978                                 attacker.hitsound += 1;
979                         }
980                         mirrordamage = 0;
981                 }
982
983                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
984                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
985         }
986 }
987
988 float RadiusDamage_running;
989 float RadiusDamageForSource (entity inflictor, vector inflictororigin, vector inflictorvelocity, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float inflictorselfdamage, float forceintensity, float deathtype, entity directhitentity)
990         // Returns total damage applies to creatures
991 {
992         entity  targ;
993         vector  force;
994         float   total_damage_to_creatures;
995         entity  next;
996         float   tfloordmg;
997         float   tfloorforce;
998
999         float stat_damagedone;
1000
1001         if(RadiusDamage_running)
1002         {
1003                 backtrace("RadiusDamage called recursively! Expect stuff to go HORRIBLY wrong.");
1004                 return 0;
1005         }
1006
1007         RadiusDamage_running = 1;
1008
1009         tfloordmg = autocvar_g_throughfloor_damage;
1010         tfloorforce = autocvar_g_throughfloor_force;
1011
1012         total_damage_to_creatures = 0;
1013
1014         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1015                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1016                 {
1017                         force = inflictorvelocity;
1018                         if(vlen(force) == 0)
1019                                 force = '0 0 -1';
1020                         else
1021                                 force = normalize(force);
1022                         if(forceintensity >= 0)
1023                                 Damage_DamageInfo(inflictororigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
1024                         else
1025                                 Damage_DamageInfo(inflictororigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
1026                 }
1027
1028         stat_damagedone = 0;
1029
1030         targ = WarpZone_FindRadius (inflictororigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
1031         while (targ)
1032         {
1033                 next = targ.chain;
1034                 if ((targ != inflictor) || inflictorselfdamage)
1035                         if (ignore != targ) if(targ.takedamage)
1036                         {
1037                                 vector nearest;
1038                                 vector diff;
1039                                 float power;
1040
1041                                 // LordHavoc: measure distance to nearest point on target (not origin)
1042                                 // (this guarentees 100% damage on a touch impact)
1043                                 nearest = targ.WarpZone_findradius_nearest;
1044                                 diff = targ.WarpZone_findradius_dist;
1045                                 // round up a little on the damage to ensure full damage on impacts
1046                                 // and turn the distance into a fraction of the radius
1047                                 power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
1048                                 //bprint(" ");
1049                                 //bprint(ftos(power));
1050                                 //if (targ == attacker)
1051                                 //      print(ftos(power), "\n");
1052                                 if (power > 0)
1053                                 {
1054                                         float finaldmg;
1055                                         if (power > 1)
1056                                                 power = 1;
1057                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1058                                         if (finaldmg > 0)
1059                                         {
1060                                                 float a;
1061                                                 float c;
1062                                                 vector hitloc;
1063                                                 vector myblastorigin;
1064                                                 vector center;
1065
1066                                                 myblastorigin = WarpZone_TransformOrigin(targ, inflictororigin);
1067
1068                                                 // if it's a player, use the view origin as reference
1069                                                 if (targ.classname == "player")
1070                                                         center = targ.origin + targ.view_ofs;
1071                                                 else
1072                                                         center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1073
1074                                                 force = normalize(center - myblastorigin);
1075                                                 force = force * (finaldmg / coredamage) * forceintensity;
1076                                                 hitloc = nearest;
1077
1078                                                 if(targ != directhitentity)
1079                                                 {
1080                                                         float hits;
1081                                                         float total;
1082                                                         float hitratio;
1083                                                         float mininv_f, mininv_d;
1084
1085                                                         // test line of sight to multiple positions on box,
1086                                                         // and do damage if any of them hit
1087                                                         hits = 0;
1088
1089                                                         // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1090                                                         // so for a given max stddev:
1091                                                         // n = (1 / (2 * max stddev of hitratio))^2
1092
1093                                                         mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1094                                                         mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1095
1096                                                         if(autocvar_g_throughfloor_debug)
1097                                                                 print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1098
1099                                                         total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1100
1101                                                         if(autocvar_g_throughfloor_debug)
1102                                                                 print(sprintf(" steps=%f", total));
1103
1104                                                         if (targ.classname == "player")
1105                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1106                                                         else
1107                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1108
1109                                                         if(autocvar_g_throughfloor_debug)
1110                                                                 print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1111
1112                                                         for(c = 0; c < total; ++c)
1113                                                         {
1114                                                                 //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1115                                                                 WarpZone_TraceLine(inflictororigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1116                                                                 if (trace_fraction == 1 || trace_ent == targ)
1117                                                                 {
1118                                                                         ++hits;
1119                                                                         if (hits > 1)
1120                                                                                 hitloc = hitloc + nearest;
1121                                                                         else
1122                                                                                 hitloc = nearest;
1123                                                                 }
1124                                                                 nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1125                                                                 nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1126                                                                 nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1127                                                         }
1128
1129                                                         nearest = hitloc * (1 / max(1, hits));
1130                                                         hitratio = (hits / total);
1131                                                         a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1132                                                         finaldmg = finaldmg * a;
1133                                                         a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1134                                                         force = force * a;
1135
1136                                                         if(autocvar_g_throughfloor_debug)
1137                                                                 print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1138                                                 }
1139
1140                                                 // laser force adjustments :P
1141                                                 if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1142                                                 {
1143                                                         if (targ == attacker)
1144                                                         {
1145                                                                 vector vel;
1146
1147                                                                 float force_zscale;
1148                                                                 float force_velocitybiasramp;
1149                                                                 float force_velocitybias;
1150
1151                                                                 force_velocitybiasramp = autocvar_sv_maxspeed;
1152                                                                 if(deathtype & HITTYPE_SECONDARY)
1153                                                                 {
1154                                                                         force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1155                                                                         force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1156                                                                 }
1157                                                                 else
1158                                                                 {
1159                                                                         force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1160                                                                         force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1161                                                                 }
1162
1163                                                                 vel = targ.velocity;
1164                                                                 vel_z = 0;
1165                                                                 vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1166                                                                 force =
1167                                                                         vlen(force)
1168                                                                         *
1169                                                                         normalize(normalize(force) + vel);
1170
1171                                                                 force_z *= force_zscale;
1172                                                         }
1173                                                         else
1174                                                         {
1175                                                                 if(deathtype & HITTYPE_SECONDARY)
1176                                                                 {
1177                                                                         force *= autocvar_g_balance_laser_secondary_force_other_scale;
1178                                                                 }
1179                                                                 else
1180                                                                 {
1181                                                                         force *= autocvar_g_balance_laser_primary_force_other_scale;
1182                                                                 }
1183                                                         }
1184                                                 }
1185
1186                                                 //if (targ == attacker)
1187                                                 //{
1188                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1189                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1190                                                 //      print(" (", ftos(a), ")\n");
1191                                                 //}
1192                                                 if(finaldmg || vlen(force))
1193                                                 {
1194                                                         if(targ.iscreature)
1195                                                         {
1196                                                                 total_damage_to_creatures += finaldmg;
1197
1198                                                                 if(accuracy_isgooddamage(attacker, targ))
1199                                                                         stat_damagedone += finaldmg;
1200                                                         }
1201
1202                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1203                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1204                                                         else
1205                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1206                                                 }
1207                                         }
1208                                 }
1209                         }
1210                 targ = next;
1211         }
1212
1213         RadiusDamage_running = 0;
1214
1215         if(!DEATH_ISSPECIAL(deathtype))
1216                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1217
1218         return total_damage_to_creatures;
1219 }
1220
1221 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
1222 {
1223         return RadiusDamageForSource (inflictor, (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5), inflictor.velocity, attacker, coredamage, edgedamage, rad, ignore, FALSE, forceintensity, deathtype, directhitentity);
1224 }
1225
1226 .float fire_damagepersec;
1227 .float fire_endtime;
1228 .float fire_deathtype;
1229 .entity fire_owner;
1230 .float fire_hitsound;
1231 .entity fire_burner;
1232
1233 void fireburner_think();
1234
1235 float Fire_IsBurning(entity e)
1236 {
1237         return (time < e.fire_endtime);
1238 }
1239
1240 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1241 {
1242         float dps;
1243         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1244
1245         if(e.classname == "player")
1246         {
1247                 if(e.deadflag)
1248                         return -1;
1249         }
1250         else
1251         {
1252                 if(!e.fire_burner)
1253                 {
1254                         // print("adding a fire burner to ", e.classname, "\n");
1255                         e.fire_burner = spawn();
1256                         e.fire_burner.classname = "fireburner";
1257                         e.fire_burner.think = fireburner_think;
1258                         e.fire_burner.nextthink = time;
1259                         e.fire_burner.owner = e;
1260                 }
1261         }
1262
1263         t = max(t, 0.1);
1264         dps = d / t;
1265         if(Fire_IsBurning(e))
1266         {
1267                 mintime = e.fire_endtime - time;
1268                 maxtime = max(mintime, t);
1269
1270                 mindps = e.fire_damagepersec;
1271                 maxdps = max(mindps, dps);
1272
1273                 if(maxtime > mintime || maxdps > mindps)
1274                 {
1275                         mindamage = mindps * mintime;
1276                         maxdamage = mindamage + d;
1277
1278                         // interval [mintime, maxtime] * [mindps, maxdps]
1279                         // intersected with
1280                         // [mindamage, maxdamage]
1281                         // maximum of this!
1282
1283                         if(maxdamage >= maxtime * maxdps)
1284                         {
1285                                 totaltime = maxtime;
1286                                 totaldamage = maxtime * maxdps;
1287
1288                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1289                         }
1290                         else
1291                         {
1292                                 // maxdamage is inside the interval!
1293                                 // first, try to use mindps; only if this fails, increase dps as needed
1294                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1295                                 totaldamage = maxdamage;
1296                                 // can totaldamage / totaltime be >= maxdps?
1297                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1298                                 // we know maxdamage < maxtime * maxdps
1299                                 // so it cannot be
1300
1301                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1302                         }
1303
1304                         // total conditions for increasing:
1305                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1306                         // however:
1307                         //     if maxtime = mintime, maxdps = mindps
1308                         // then:
1309                         //     maxdamage = mindamage + d
1310                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1311                         // so the last condition is not needed
1312
1313                         e.fire_damagepersec = totaldamage / totaltime;
1314                         e.fire_endtime = time + totaltime;
1315                         if(totaldamage > 1.2 * mindamage)
1316                         {
1317                                 e.fire_deathtype = dt;
1318                                 if(e.fire_owner != o)
1319                                 {
1320                                         e.fire_owner = o;
1321                                         e.fire_hitsound = FALSE;
1322                                 }
1323                         }
1324                         if(accuracy_isgooddamage(o, e))
1325                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1326                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1327                 }
1328                 else
1329                         return 0;
1330         }
1331         else
1332         {
1333                 e.fire_damagepersec = dps;
1334                 e.fire_endtime = time + t;
1335                 e.fire_deathtype = dt;
1336                 e.fire_owner = o;
1337                 e.fire_hitsound = FALSE;
1338                 if(accuracy_isgooddamage(o, e))
1339                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1340                 return d;
1341         }
1342 }
1343
1344 void Fire_ApplyDamage(entity e)
1345 {
1346         float t, d, hi, ty;
1347         entity o;
1348
1349         if not(Fire_IsBurning(e))
1350                 return;
1351
1352         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1353         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1354                 o = e.fire_owner;
1355
1356         // water and slime stop fire
1357         if(e.waterlevel)
1358         if(e.watertype != CONTENT_LAVA)
1359                 e.fire_endtime = 0;
1360
1361         // ice stops fire
1362         if(e.freezetag_frozen)
1363                 e.fire_endtime = 0;
1364
1365         t = min(frametime, e.fire_endtime - time);
1366         d = e.fire_damagepersec * t;
1367
1368         hi = e.fire_owner.hitsound;
1369         ty = e.fire_owner.typehitsound;
1370         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1371         if(e.fire_hitsound && e.fire_owner)
1372         {
1373                 e.fire_owner.hitsound = hi;
1374                 e.fire_owner.typehitsound = ty;
1375         }
1376         e.fire_hitsound = TRUE;
1377
1378         if not(IS_INDEPENDENT_PLAYER(e))
1379         FOR_EACH_PLAYER(other) if(e != other)
1380         {
1381                 if(other.classname == "player")
1382                 if(other.deadflag == DEAD_NO)
1383                 if not(IS_INDEPENDENT_PLAYER(other))
1384                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1385                 {
1386                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1387                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1388                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1389                 }
1390         }
1391 }
1392
1393 void Fire_ApplyEffect(entity e)
1394 {
1395         if(Fire_IsBurning(e))
1396                 e.effects |= EF_FLAME;
1397         else
1398                 e.effects &~= EF_FLAME;
1399 }
1400
1401 void fireburner_think()
1402 {
1403         // for players, this is done in the regular loop
1404         if(wasfreed(self.owner))
1405         {
1406                 remove(self);
1407                 return;
1408         }
1409         Fire_ApplyEffect(self.owner);
1410         if(!Fire_IsBurning(self.owner))
1411         {
1412                 self.owner.fire_burner = world;
1413                 remove(self);
1414                 return;
1415         }
1416         Fire_ApplyDamage(self.owner);
1417         self.nextthink = time;
1418 }