Merge remote-tracking branch 'origin/master' into samual/spawn_weapons
[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 cantbe, entity mustbe, 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 (((cantbe != targ) && !mustbe) || (mustbe == targ))
1036                 if (targ.takedamage)
1037                 {
1038                         vector nearest;
1039                         vector diff;
1040                         float power;
1041
1042                         // LordHavoc: measure distance to nearest point on target (not origin)
1043                         // (this guarentees 100% damage on a touch impact)
1044                         nearest = targ.WarpZone_findradius_nearest;
1045                         diff = targ.WarpZone_findradius_dist;
1046                         // round up a little on the damage to ensure full damage on impacts
1047                         // and turn the distance into a fraction of the radius
1048                         power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
1049                         //bprint(" ");
1050                         //bprint(ftos(power));
1051                         //if (targ == attacker)
1052                         //      print(ftos(power), "\n");
1053                         if (power > 0)
1054                         {
1055                                 float finaldmg;
1056                                 if (power > 1)
1057                                         power = 1;
1058                                 finaldmg = coredamage * power + edgedamage * (1 - power);
1059                                 if (finaldmg > 0)
1060                                 {
1061                                         float a;
1062                                         float c;
1063                                         vector hitloc;
1064                                         vector myblastorigin;
1065                                         vector center;
1066
1067                                         myblastorigin = WarpZone_TransformOrigin(targ, inflictororigin);
1068
1069                                         // if it's a player, use the view origin as reference
1070                                         if (targ.classname == "player")
1071                                                 center = targ.origin + targ.view_ofs;
1072                                         else
1073                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1074
1075                                         force = normalize(center - myblastorigin);
1076                                         force = force * (finaldmg / coredamage) * forceintensity;
1077                                         hitloc = nearest;
1078
1079                                         if(targ != directhitentity)
1080                                         {
1081                                                 float hits;
1082                                                 float total;
1083                                                 float hitratio;
1084                                                 float mininv_f, mininv_d;
1085
1086                                                 // test line of sight to multiple positions on box,
1087                                                 // and do damage if any of them hit
1088                                                 hits = 0;
1089
1090                                                 // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1091                                                 // so for a given max stddev:
1092                                                 // n = (1 / (2 * max stddev of hitratio))^2
1093
1094                                                 mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1095                                                 mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1096
1097                                                 if(autocvar_g_throughfloor_debug)
1098                                                         print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1099
1100                                                 total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1101
1102                                                 if(autocvar_g_throughfloor_debug)
1103                                                         print(sprintf(" steps=%f", total));
1104
1105                                                 if (targ.classname == "player")
1106                                                         total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1107                                                 else
1108                                                         total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1109
1110                                                 if(autocvar_g_throughfloor_debug)
1111                                                         print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1112
1113                                                 for(c = 0; c < total; ++c)
1114                                                 {
1115                                                         //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1116                                                         WarpZone_TraceLine(inflictororigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1117                                                         if (trace_fraction == 1 || trace_ent == targ)
1118                                                         {
1119                                                                 ++hits;
1120                                                                 if (hits > 1)
1121                                                                         hitloc = hitloc + nearest;
1122                                                                 else
1123                                                                         hitloc = nearest;
1124                                                         }
1125                                                         nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1126                                                         nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1127                                                         nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1128                                                 }
1129
1130                                                 nearest = hitloc * (1 / max(1, hits));
1131                                                 hitratio = (hits / total);
1132                                                 a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1133                                                 finaldmg = finaldmg * a;
1134                                                 a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1135                                                 force = force * a;
1136
1137                                                 if(autocvar_g_throughfloor_debug)
1138                                                         print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1139                                         }
1140
1141                                         // laser force adjustments :P
1142                                         if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1143                                         {
1144                                                 if (targ == attacker)
1145                                                 {
1146                                                         vector vel;
1147
1148                                                         float force_zscale;
1149                                                         float force_velocitybiasramp;
1150                                                         float force_velocitybias;
1151
1152                                                         force_velocitybiasramp = autocvar_sv_maxspeed;
1153                                                         if(deathtype & HITTYPE_SECONDARY)
1154                                                         {
1155                                                                 force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1156                                                                 force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1157                                                         }
1158                                                         else
1159                                                         {
1160                                                                 force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1161                                                                 force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1162                                                         }
1163
1164                                                         vel = targ.velocity;
1165                                                         vel_z = 0;
1166                                                         vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1167                                                         force =
1168                                                                 vlen(force)
1169                                                                 *
1170                                                                 normalize(normalize(force) + vel);
1171
1172                                                         force_z *= force_zscale;
1173                                                 }
1174                                                 else
1175                                                 {
1176                                                         if(deathtype & HITTYPE_SECONDARY)
1177                                                         {
1178                                                                 force *= autocvar_g_balance_laser_secondary_force_other_scale;
1179                                                         }
1180                                                         else
1181                                                         {
1182                                                                 force *= autocvar_g_balance_laser_primary_force_other_scale;
1183                                                         }
1184                                                 }
1185                                         }
1186
1187                                         //if (targ == attacker)
1188                                         //{
1189                                         //      print("hits ", ftos(hits), " / ", ftos(total));
1190                                         //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1191                                         //      print(" (", ftos(a), ")\n");
1192                                         //}
1193                                         if(finaldmg || vlen(force))
1194                                         {
1195                                                 if(targ.iscreature)
1196                                                 {
1197                                                         total_damage_to_creatures += finaldmg;
1198
1199                                                         if(accuracy_isgooddamage(attacker, targ))
1200                                                                 stat_damagedone += finaldmg;
1201                                                 }
1202
1203                                                 if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1204                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1205                                                 else
1206                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1207                                         }
1208                                 }
1209                         }
1210                 }
1211                 targ = next;
1212         }
1213
1214         RadiusDamage_running = 0;
1215
1216         if(!DEATH_ISSPECIAL(deathtype))
1217                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1218
1219         return total_damage_to_creatures;
1220 }
1221
1222 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity cantbe, entity mustbe, float forceintensity, float deathtype, entity directhitentity)
1223 {
1224         return RadiusDamageForSource (inflictor, (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5), inflictor.velocity, attacker, coredamage, edgedamage, rad, cantbe, mustbe, FALSE, forceintensity, deathtype, directhitentity);
1225 }
1226
1227 .float fire_damagepersec;
1228 .float fire_endtime;
1229 .float fire_deathtype;
1230 .entity fire_owner;
1231 .float fire_hitsound;
1232 .entity fire_burner;
1233
1234 void fireburner_think();
1235
1236 float Fire_IsBurning(entity e)
1237 {
1238         return (time < e.fire_endtime);
1239 }
1240
1241 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1242 {
1243         float dps;
1244         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1245
1246         if(e.classname == "player")
1247         {
1248                 if(e.deadflag)
1249                         return -1;
1250         }
1251         else
1252         {
1253                 if(!e.fire_burner)
1254                 {
1255                         // print("adding a fire burner to ", e.classname, "\n");
1256                         e.fire_burner = spawn();
1257                         e.fire_burner.classname = "fireburner";
1258                         e.fire_burner.think = fireburner_think;
1259                         e.fire_burner.nextthink = time;
1260                         e.fire_burner.owner = e;
1261                 }
1262         }
1263
1264         t = max(t, 0.1);
1265         dps = d / t;
1266         if(Fire_IsBurning(e))
1267         {
1268                 mintime = e.fire_endtime - time;
1269                 maxtime = max(mintime, t);
1270
1271                 mindps = e.fire_damagepersec;
1272                 maxdps = max(mindps, dps);
1273
1274                 if(maxtime > mintime || maxdps > mindps)
1275                 {
1276                         // Constraints:
1277                         
1278                         // damage we have right now
1279                         mindamage = mindps * mintime;
1280
1281                         // damage we want to get
1282                         maxdamage = mindamage + d;
1283
1284                         // but we can't exceed maxtime * maxdps!
1285                         totaldamage = min(maxdamage, maxtime * maxdps);
1286
1287                         // LEMMA:
1288                         // Look at:
1289                         // totaldamage = min(mindamage + d, maxtime * maxdps)
1290                         // We see:
1291                         // totaldamage <= maxtime * maxdps
1292                         // ==> totaldamage / maxdps <= maxtime.
1293                         // We also see:
1294                         // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
1295                         //                     >= min(mintime, maxtime)
1296                         // ==> totaldamage / maxdps >= mintime.
1297
1298                         /*
1299                         // how long do we damage then?
1300                         // at least as long as before
1301                         // but, never exceed maxdps
1302                         totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
1303                         */
1304
1305                         // alternate:
1306                         // at most as long as maximum allowed
1307                         // but, never below mindps
1308                         totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
1309
1310                         // assuming t > mintime, dps > mindps:
1311                         // we get d = t * dps = maxtime * maxdps
1312                         // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
1313                         // totaldamage / maxdps = maxtime
1314                         // totaldamage / mindps > totaldamage / maxdps = maxtime
1315                         // FROM THIS:
1316                         // a) totaltime = max(mintime, maxtime) = maxtime
1317                         // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
1318
1319                         // assuming t <= mintime:
1320                         // we get maxtime = mintime
1321                         // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
1322                         // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
1323
1324                         // assuming dps <= mindps:
1325                         // we get mindps = maxdps.
1326                         // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
1327                         // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
1328                         // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
1329
1330                         e.fire_damagepersec = totaldamage / totaltime;
1331                         e.fire_endtime = time + totaltime;
1332                         if(totaldamage > 1.2 * mindamage)
1333                         {
1334                                 e.fire_deathtype = dt;
1335                                 if(e.fire_owner != o)
1336                                 {
1337                                         e.fire_owner = o;
1338                                         e.fire_hitsound = FALSE;
1339                                 }
1340                         }
1341                         if(accuracy_isgooddamage(o, e))
1342                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1343                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1344                 }
1345                 else
1346                         return 0;
1347         }
1348         else
1349         {
1350                 e.fire_damagepersec = dps;
1351                 e.fire_endtime = time + t;
1352                 e.fire_deathtype = dt;
1353                 e.fire_owner = o;
1354                 e.fire_hitsound = FALSE;
1355                 if(accuracy_isgooddamage(o, e))
1356                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1357                 return d;
1358         }
1359 }
1360
1361 void Fire_ApplyDamage(entity e)
1362 {
1363         float t, d, hi, ty;
1364         entity o;
1365
1366         if not(Fire_IsBurning(e))
1367                 return;
1368
1369         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1370         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1371                 o = e.fire_owner;
1372
1373         // water and slime stop fire
1374         if(e.waterlevel)
1375         if(e.watertype != CONTENT_LAVA)
1376                 e.fire_endtime = 0;
1377
1378         // ice stops fire
1379         if(e.freezetag_frozen)
1380                 e.fire_endtime = 0;
1381
1382         t = min(frametime, e.fire_endtime - time);
1383         d = e.fire_damagepersec * t;
1384
1385         hi = e.fire_owner.hitsound;
1386         ty = e.fire_owner.typehitsound;
1387         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1388         if(e.fire_hitsound && e.fire_owner)
1389         {
1390                 e.fire_owner.hitsound = hi;
1391                 e.fire_owner.typehitsound = ty;
1392         }
1393         e.fire_hitsound = TRUE;
1394
1395         if not(IS_INDEPENDENT_PLAYER(e))
1396         FOR_EACH_PLAYER(other) if(e != other)
1397         {
1398                 if(other.classname == "player")
1399                 if(other.deadflag == DEAD_NO)
1400                 if not(IS_INDEPENDENT_PLAYER(other))
1401                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1402                 {
1403                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1404                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1405                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1406                 }
1407         }
1408 }
1409
1410 void Fire_ApplyEffect(entity e)
1411 {
1412         if(Fire_IsBurning(e))
1413                 e.effects |= EF_FLAME;
1414         else
1415                 e.effects &~= EF_FLAME;
1416 }
1417
1418 void fireburner_think()
1419 {
1420         // for players, this is done in the regular loop
1421         if(wasfreed(self.owner))
1422         {
1423                 remove(self);
1424                 return;
1425         }
1426         Fire_ApplyEffect(self.owner);
1427         if(!Fire_IsBurning(self.owner))
1428         {
1429                 self.owner.fire_burner = world;
1430                 remove(self);
1431                 return;
1432         }
1433         Fire_ApplyDamage(self.owner);
1434         self.nextthink = time;
1435 }