]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
more uninitialized local use fixes
[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 || !WEPSET_CONTAINS_EW(attacker, culprit))
144                         culprit = attacker.weapon;
145
146                 if(g_weaponarena_random_with_laser && culprit == WEP_LASER)
147                 {
148                         // no exchange
149                 }
150                 else
151                 {
152                         if(!GiveFrags_randomweapons)
153                         {
154                                 GiveFrags_randomweapons = spawn();
155                                 GiveFrags_randomweapons.classname = "GiveFrags_randomweapons";
156                         }
157
158                         if(inWarmupStage)
159                                 WEPSET_COPY_EA(GiveFrags_randomweapons, warmup_start_weapons);
160                         else
161                                 WEPSET_COPY_EA(GiveFrags_randomweapons, start_weapons);
162
163                         // all others (including the culprit): remove
164                         WEPSET_ANDNOT_EE(GiveFrags_randomweapons, attacker);
165                         WEPSET_ANDNOT_EW(GiveFrags_randomweapons, culprit);
166
167                         // among the remaining ones, choose one by random
168                         W_RandomWeapons(GiveFrags_randomweapons, 1);
169
170                         if(!WEPSET_EMPTY_E(GiveFrags_randomweapons))
171                         {
172                                 WEPSET_OR_EE(attacker, GiveFrags_randomweapons);
173                                 WEPSET_ANDNOT_EW(attacker, culprit);
174                         }
175                 }
176
177                 // after a frag, choose another random weapon set
178                 if not(WEPSET_CONTAINS_EW(attacker, attacker.weapon))
179                         W_SwitchWeapon_Force(attacker, w_getbestweapon(attacker));
180         }
181
182         // FIXME fix the mess this is (we have REAL points now!)
183         entity oldself;
184         oldself = self;
185         self = attacker;
186         frag_attacker = attacker;
187         frag_target = targ;
188         frag_score = f;
189         if(MUTATOR_CALLHOOK(GiveFragsForKill))
190         {
191                 f = frag_score;
192                 self = oldself;
193         }
194         else
195         {
196                 self = oldself;
197                 if(g_runematch)
198                 {
199                         f = RunematchHandleFrags(attacker, targ, f);
200                 }
201                 else if(g_lms)
202                 {
203                         // remove a life
204                         float tl;
205                         tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
206                         if(tl < lms_lowest_lives)
207                                 lms_lowest_lives = tl;
208                         if(tl <= 0)
209                         {
210                                 if(!lms_next_place)
211                                         lms_next_place = player_count;
212                                 else
213                                         lms_next_place = min(lms_next_place, player_count);
214                                 PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
215                                 --lms_next_place;
216                         }
217                         f = 0;
218                 }
219                 else if(g_ctf)
220                 {
221                         if(g_ctf_ignore_frags)
222                                 f = 0;
223                 }
224         }
225
226         attacker.totalfrags += f;
227
228         if(f)
229                 UpdateFrags(attacker, f);
230 }
231
232 string Obituary_ExtraFragInfo(entity player) // Extra fragmessage information
233 {
234         string health_output = string_null;
235         string ping_output = string_null;
236         string handicap_output = string_null;
237         string output = string_null;
238
239         if(autocvar_sv_fraginfo && ((autocvar_sv_fraginfo == 2) || inWarmupStage))
240         {
241                 // health/armor of attacker (person who killed you)
242                 if(autocvar_sv_fraginfo_stats && (player.health >= 1))
243                         health_output = strcat("^7(Health ^1", ftos(rint(player.health)), "^7 / Armor ^2", ftos(rint(player.armorvalue)), "^7)");
244                 
245                 // ping display
246                 if(autocvar_sv_fraginfo_ping)
247                         ping_output = ((clienttype(player) == CLIENTTYPE_BOT) ? "^2Bot" : strcat("Ping ", ((player.ping >= 150) ? "^1" : "^2"), ftos(rint(player.ping)), "ms"));
248                         
249                 // handicap display 
250                 if(autocvar_sv_fraginfo_handicap) 
251                 {
252                         if(autocvar_sv_fraginfo_handicap == 2)  
253                                 handicap_output = strcat(output, strcat("Handicap ^2", ((player.cvar_cl_handicap <= 1) ? "Off" : ftos(rint(player.cvar_cl_handicap)))));
254                         else if(player.cvar_cl_handicap) // with _handicap 1, only show this if there actually is a handicap enabled.   
255                                 handicap_output = strcat("Handicap ^2", ftos(rint(player.cvar_cl_handicap)));
256                 }
257                 
258                 // format the string
259                 output = strcat(health_output, (health_output ? ((ping_output || handicap_output) ? " ^7(" : "") : ((ping_output || handicap_output) ? "^7(" : "")), 
260                         ping_output, (handicap_output ? "^7 / " : ""), 
261                         handicap_output, ((ping_output || handicap_output) ? "^7)" : ""));
262                 
263                 // add new line to the beginning if there is a message
264                 if(output) { output = strcat("\n", output); }
265         }
266         
267         return output;
268 }
269
270 string AppendItemcodes(string s, entity player)
271 {
272         float w;
273         w = player.weapon;
274         //if(w == 0)
275         //      w = player.switchweapon;
276         if(w == 0)
277                 w = player.cnt; // previous weapon!
278         s = strcat(s, ftos(w));
279         if(time < player.strength_finished)
280                 s = strcat(s, "S");
281         if(time < player.invincible_finished)
282                 s = strcat(s, "I");
283         if(player.flagcarried != world)
284                 s = strcat(s, "F");
285         if(player.BUTTON_CHAT)
286                 s = strcat(s, "T");
287         if(player.kh_next)
288                 s = strcat(s, "K");
289         if(player.runes)
290                 s = strcat(s, "|", ftos(player.runes));
291         return s;
292 }
293
294 void LogDeath(string mode, float deathtype, entity killer, entity killed)
295 {
296         string s;
297         if(!autocvar_sv_eventlog)
298                 return;
299         s = strcat(":kill:", mode);
300         s = strcat(s, ":", ftos(killer.playerid));
301         s = strcat(s, ":", ftos(killed.playerid));
302         s = strcat(s, ":type=", ftos(deathtype));
303         s = strcat(s, ":items=");
304         s = AppendItemcodes(s, killer);
305         if(killed != killer)
306         {
307                 s = strcat(s, ":victimitems=");
308                 s = AppendItemcodes(s, killed);
309         }
310         GameLogEcho(s);
311 }
312
313 void Send_KillNotification (string s1, string s2, string s3, float msg, float type)
314 {
315         WriteByte(MSG_ALL, SVC_TEMPENTITY);
316         WriteByte(MSG_ALL, TE_CSQC_KILLNOTIFY);
317         WriteString(MSG_ALL, s1);
318         WriteString(MSG_ALL, s2);
319         WriteString(MSG_ALL, s3);
320         WriteShort(MSG_ALL, msg);
321         WriteByte(MSG_ALL, type);
322 }
323
324 // Function is used to send a generic centerprint whose content CSQC gets to decide (gentle version or not in the below cases)
325 void Send_CSQC_KillCenterprint(entity e, string s1, string s2, float msg, float type)
326 {
327         if (clienttype(e) == CLIENTTYPE_REAL)
328         {
329                 msg_entity = e;
330                 WRITESPECTATABLE_MSG_ONE({
331                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
332                         WriteByte(MSG_ONE, TE_CSQC_KILLCENTERPRINT);
333                         WriteString(MSG_ONE, s1);
334                         WriteString(MSG_ONE, s2);
335                         WriteShort(MSG_ONE, msg);
336                         WriteByte(MSG_ONE, type);
337                 });
338         }
339 }
340
341 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
342 {
343         string  s, a, msg;
344         float w, type;
345
346         if (targ.classname == "player")
347         {
348                 s = targ.netname;
349                 a = attacker.netname;
350
351                 if (targ == attacker) // suicides
352                 {
353                         if (deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
354                                 msg = ColoredTeamName(targ.team); // TODO: check if needed?
355                         else
356                                 msg = "";
357             if(!g_cts) // no "killed your own dumb self" message in CTS
358                 Send_CSQC_KillCenterprint(targ, msg, "", deathtype, MSG_SUICIDE);
359
360                         if(deathtype != DEATH_TEAMCHANGE && deathtype != DEATH_QUIET)
361                         {
362                                 LogDeath("suicide", deathtype, targ, targ);
363                                 GiveFrags(attacker, targ, -1, deathtype);
364                         }
365
366                         if (targ.killcount > 2)
367                                 msg = ftos(targ.killcount);
368                         else
369                                 msg = "";
370                         if(teamplay && deathtype == DEATH_MIRRORDAMAGE)
371                         {
372                                 if(attacker.team == COLOR_TEAM1)
373                                         deathtype = KILL_TEAM_RED;
374                                 else
375                                         deathtype = KILL_TEAM_BLUE;
376                         }
377
378                         Send_KillNotification(s, msg, ftos(w), deathtype, MSG_SUICIDE);
379                 }
380                 else if (attacker.classname == "player")
381                 {
382                         if(!IsDifferentTeam(attacker, targ))
383                         {
384                                 if(attacker.team == COLOR_TEAM1)
385                                         type = KILL_TEAM_RED;
386                                 else
387                                         type = KILL_TEAM_BLUE;
388
389                                 GiveFrags(attacker, targ, -1, deathtype);
390
391                                 Send_CSQC_KillCenterprint(attacker, s, "", type, MSG_KILL);
392
393                                 if (targ.killcount > 2)
394                                         msg = ftos(targ.killcount);
395                                 else
396                                         msg = "";
397
398                                 if (attacker.killcount > 2) {
399                                         msg = ftos(attacker.killcount);
400                                         type = KILL_TEAM_SPREE;
401                                 }
402                                 Send_KillNotification(a, s, msg, type, MSG_KILL);
403
404                                 attacker.killcount = 0;
405
406                                 LogDeath("tk", deathtype, attacker, targ);
407                         }
408                         else
409                         {
410                                 if (!checkrules_firstblood)
411                                 {
412                                         checkrules_firstblood = TRUE;
413                                         Send_KillNotification(a, "", "", KILL_FIRST_BLOOD, MSG_KILL);
414                                         // TODO: make these print a newline if they dont
415                                         Send_CSQC_KillCenterprint(attacker, "", "", KILL_FIRST_BLOOD, MSG_KILL);
416                                         Send_CSQC_KillCenterprint(targ, "", "", KILL_FIRST_VICTIM, MSG_KILL);
417                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD, 1);
418                                         PlayerStats_Event(targ, PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM, 1);
419                                 }
420
421                                 if(targ.BUTTON_CHAT) {
422                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_TYPEFRAG, MSG_KILL);
423                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_TYPEFRAGGED, MSG_KILL);
424                                 } else {
425                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_FRAG, MSG_KILL);
426                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_FRAGGED, MSG_KILL);
427                                 }
428
429                                 attacker.taunt_soundtime = time + 1;
430
431                                 // TODO: fix this?
432                                 if (deathtype == DEATH_CUSTOM)
433                                         msg = deathmessage;
434                                 else
435                                         msg = inflictor.message2;
436
437                                 if(strstrofs(msg, "%", 0) < 0)
438                                         msg = strcat("%s ", msg, " by %s");
439
440                                 Send_KillNotification(a, s, msg, deathtype, MSG_KILL);
441
442                                 if(g_ctf && targ.flagcarried)
443                                 {
444                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
445                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
446                                         GiveFrags(attacker, targ, 0, deathtype); // for logging
447                                 }
448                                 else
449                                         GiveFrags(attacker, targ, 1, deathtype);
450
451                                 if (targ.killcount > 2) {
452                                         Send_KillNotification(s, ftos(targ.killcount), a, KILL_END_SPREE, MSG_SPREE);
453                                 }
454
455                                 attacker.killcount = attacker.killcount + 1;
456
457                                 if (attacker.killcount > 2) {
458                                         Send_KillNotification(a, ftos(attacker.killcount), "", KILL_SPREE, MSG_SPREE);
459                                 }
460                                 else if (attacker.killcount == 3)
461                                 {
462                                         Send_KillNotification(a, "", "", KILL_SPREE_3, MSG_SPREE);
463                                         AnnounceTo(attacker, "03kills");
464                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3, 1);
465                                 }
466                                 else if (attacker.killcount == 5)
467                                 {
468                                         Send_KillNotification(a, "", "", KILL_SPREE_5, MSG_SPREE);
469                                         AnnounceTo(attacker, "05kills");
470                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5, 1);
471                                 }
472                                 else if (attacker.killcount == 10)
473                                 {
474                                         Send_KillNotification(a, "", "", KILL_SPREE_10, MSG_SPREE);
475                                         AnnounceTo(attacker, "10kills");
476                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10, 1);
477                                 }
478                                 else if (attacker.killcount == 15)
479                                 {
480                                         Send_KillNotification(a, "", "", KILL_SPREE_15, MSG_SPREE);
481                                         AnnounceTo(attacker, "15kills");
482                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15, 1);
483                                 }
484                                 else if (attacker.killcount == 20)
485                                 {
486                                         Send_KillNotification(a, "", "", KILL_SPREE_20, MSG_SPREE);
487                                         AnnounceTo(attacker, "20kills");
488                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20, 1);
489                                 }
490                                 else if (attacker.killcount == 25)
491                                 {
492                                         Send_KillNotification(a, "", "", KILL_SPREE_25, MSG_SPREE);
493                                         AnnounceTo(attacker, "25kills");
494                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25, 1);
495                                 }
496                                 else if (attacker.killcount == 30)
497                                 {
498                                         Send_KillNotification(a, "", "", KILL_SPREE_30, MSG_SPREE);
499                                         AnnounceTo(attacker, "30kills");
500                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30, 1);
501                                 }
502                                 LogDeath("frag", deathtype, attacker, targ);
503                         }
504                 }
505                 else
506                 {
507                         Send_CSQC_KillCenterprint(targ, "", "", deathtype, MSG_KILL_ACTION);
508                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
509                                 msg = inflictor.message;
510                         else if (deathtype == DEATH_CUSTOM)
511                                 msg = deathmessage;
512                         if(strstrofs(msg, "%", 0) < 0)
513                                 msg = strcat("%s ", msg);
514
515                         GiveFrags(targ, targ, -1, deathtype);
516                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
517                                 AnnounceTo(targ, "botlike");
518                                 PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
519                         }
520                         Send_KillNotification(s, msg, "", deathtype, MSG_KILL_ACTION);
521
522                         if (targ.killcount > 2)
523                                 Send_KillNotification(s, ftos(targ.killcount), "", 0, MSG_KILL_ACTION_SPREE);
524
525                         LogDeath("accident", deathtype, targ, targ);
526                 }
527
528                 targ.death_origin = targ.origin;
529                 if(targ != attacker)
530                         targ.killer_origin = attacker.origin;
531
532                 // FIXME: this should go in PutClientInServer
533                 if (targ.killcount)
534                         targ.killcount = 0;
535         }
536 }
537
538 // these are updated by each Damage call for use in button triggering and such
539 entity damage_targ;
540 entity damage_inflictor;
541 entity damage_attacker;
542
543 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
544 {
545         float mirrordamage;
546         float mirrorforce;
547         float teamdamage0;
548         entity attacker_save;
549         mirrordamage = 0;
550         mirrorforce = 0;
551
552         if (gameover || targ.killcount == -666)
553                 return;
554
555         entity oldself;
556         oldself = self;
557         self = targ;
558         damage_targ = targ;
559         damage_inflictor = inflictor;
560         damage_attacker = attacker;
561                 attacker_save = attacker;
562
563         if(targ.classname == "player")
564                 if(targ.hook)
565                         if(targ.hook.aiment)
566                                 if(targ.hook.aiment == attacker)
567                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
568
569         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
570         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
571         {
572                 if(targ.classname == "player")
573                         if not(IsDifferentTeam(targ, attacker))
574                         {
575                                 self = oldself;
576                                 return;
577                         }
578         }
579
580         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE || deathtype == DEATH_QUIET)
581         {
582                 // These are ALWAYS lethal
583                 // No damage modification here
584                 // Instead, prepare the victim for his death...
585                 targ.armorvalue = 0;
586                 targ.spawnshieldtime = 0;
587                 targ.health = 0.9; // this is < 1
588                 targ.flags -= targ.flags & FL_GODMODE;
589                 damage = 100000;
590         }
591         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
592         {
593                 // no processing
594         }
595         else
596         {
597                 /*
598                 skill based bot damage? gtfo. (tZork)
599                 if (targ.classname == "player")
600                 if (attacker.classname == "player")
601                 if (!targ.isbot)
602                 if (attacker.isbot)
603                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
604         */
605         
606                 // nullify damage if teamplay is on
607                 if(deathtype != DEATH_TELEFRAG)
608                 if(attacker.classname == "player")
609                 {
610                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
611                         {
612                                 damage = 0;
613                                 force = '0 0 0';
614                         }
615                         else if(!IsDifferentTeam(attacker, targ))
616                         {
617                                 if(autocvar_teamplay_mode == 1)
618                                         damage = 0;
619                                 else if(attacker != targ)
620                                 {
621                                         if(autocvar_teamplay_mode == 3)
622                                                 damage = 0;
623                                         else if(autocvar_teamplay_mode == 4)
624                                         {
625                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
626                                                 {
627                                                         teamdamage0 = max(attacker.dmg_team, autocvar_g_teamdamage_threshold);
628                                                         attacker.dmg_team = attacker.dmg_team + damage;
629                                                         if(attacker.dmg_team > teamdamage0 && !g_ca)
630                                                                 mirrordamage = autocvar_g_mirrordamage * (attacker.dmg_team - teamdamage0);
631                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
632                                                         if(g_minstagib)
633                                                         {
634                                                                 if(autocvar_g_friendlyfire == 0)
635                                                                         damage = 0;
636                                                         }
637                                                         else if(g_ca)
638                                                                 damage = 0;
639                                                         else
640                                                                 damage = autocvar_g_friendlyfire * damage;
641                                                         // mirrordamage will be used LATER
642
643                                                         if(autocvar_g_mirrordamage_virtual)
644                                                         {
645                                                                 vector v;
646                                                                 v = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
647                                                                 v_z = 0; // fteqcc sucks
648                                                                 attacker.dmg_take += v_x;
649                                                                 attacker.dmg_save += v_y;
650                                                                 attacker.dmg_inflictor = inflictor;
651                                                                 mirrordamage = 0;
652                                                                 mirrorforce = 0;
653                                                         }
654
655                                                         if(autocvar_g_friendlyfire_virtual)
656                                                         {
657                                                                 vector v;
658                                                                 v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
659                                                                 v_z = 0; // fteqcc sucks
660                                                                 targ.dmg_take += v_x;
661                                                                 targ.dmg_save += v_y;
662                                                                 targ.dmg_inflictor = inflictor;
663                                                                 damage = 0;
664                                                                 if(!autocvar_g_friendlyfire_virtual_force)
665                                                                         force = '0 0 0';
666                                                         }
667                                                 }
668                                                 else
669                                                         damage = 0;
670                                         }
671                                 }
672                         }
673                 }
674
675                 if(targ.classname == "player")
676                 if(attacker.classname == "player")
677                 if(attacker != targ)
678                 {
679                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
680                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
681                 }
682
683                 if(targ.classname == "player")
684                 if (g_minstagib)
685                 {
686                         if ((deathtype == DEATH_FALL)  ||
687                                 (deathtype == DEATH_DROWN) ||
688                                 (deathtype == DEATH_SLIME) ||
689                                 (deathtype == DEATH_LAVA)  ||
690                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
691                         {
692                                 self = oldself;
693                                 return;
694                         }
695                         if(damage > 0)
696                             damage = 10000;
697                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
698                         {
699                                 targ.armorvalue -= 1;
700                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
701                                 damage = 0;
702                                 targ.hitsound += 1;
703                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
704                         }
705                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
706                         {
707                                 damage = 0;
708                                 mirrordamage = 0;
709                                 if (targ != attacker)
710                                 {
711                                         if ((targ.health >= 1) && (targ.classname == "player"))
712                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
713                                         force = '0 0 0';
714                                         // keep mirrorforce
715                                         attacker = targ;
716                                 }
717                         }
718                 }
719
720                 if not(DEATH_ISSPECIAL(deathtype))
721                 {
722                         damage *= g_weapondamagefactor;
723                         mirrordamage *= g_weapondamagefactor;
724                         force = force * g_weaponforcefactor;
725                         mirrorforce *= g_weaponforcefactor;
726                 }
727                 
728                 // should this be changed at all? If so, in what way?
729                 frag_attacker = attacker;
730                 frag_target = targ;
731                 frag_damage = damage;
732                 frag_force = force;
733         frag_deathtype = deathtype;
734                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
735                 damage = frag_damage;
736                 force = frag_force;
737                 
738                 // apply strength multiplier
739                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
740                 {
741                         if(targ == attacker)
742                         {
743                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
744                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
745                         }
746                         else
747                         {
748                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
749                                 force = force * autocvar_g_balance_powerup_strength_force;
750                         }
751                 }
752
753                 // apply invincibility multiplier
754                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
755                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
756
757                 if (targ == attacker)
758                 {
759                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
760                                 damage = 0;
761                         else
762                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
763                 }
764
765                 // CTF: reduce damage/force
766                 if(g_ctf)
767                 if(targ == attacker)
768                 if(targ.flagcarried)
769                 {
770                         damage = damage * autocvar_g_ctf_flagcarrier_selfdamage;
771                         force = force * autocvar_g_ctf_flagcarrier_selfforce;
772                 }
773
774                 if(g_runematch)
775                 {
776                         // apply strength rune
777                         if (attacker.runes & RUNE_STRENGTH)
778                         {
779                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
780                                 {
781                                         damage = damage * autocvar_g_balance_rune_strength_combo_damage;
782                                         force = force * autocvar_g_balance_rune_strength_combo_force;
783                                 }
784                                 else
785                                 {
786                                         damage = damage * autocvar_g_balance_rune_strength_damage;
787                                         force = force * autocvar_g_balance_rune_strength_force;
788                                 }
789                         }
790                         else if (attacker.runes & CURSE_WEAK)
791                         {
792                                 damage = damage * autocvar_g_balance_curse_weak_damage;
793                                 force = force * autocvar_g_balance_curse_weak_force;
794                         }
795
796                         // apply defense rune
797                         if (targ.runes & RUNE_DEFENSE)
798                         {
799                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
800                                         damage = damage * autocvar_g_balance_rune_defense_combo_takedamage;
801                                 else
802                                         damage = damage * autocvar_g_balance_rune_defense_takedamage;
803                         }
804                         else if (targ.runes & CURSE_VULNER)
805                                 damage = damage * autocvar_g_balance_curse_vulner_takedamage;
806                 }
807
808                 // count the damage
809                 if(attacker)
810                 if(!targ.deadflag)
811                 if(targ.takedamage == DAMAGE_AIM)
812                 if(targ != attacker)
813                 {
814                         if(damage_headshotbonus)
815                         {
816                                 if(targ.classname == "player")
817                                 {
818                                         // HEAD SHOT:
819                                         // find height of hit on player axis
820                                         // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
821                                         vector headmins, headmaxs, org;
822                                         org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
823                                         headmins = org + GetHeadshotMins(targ);
824                                         headmaxs = org + GetHeadshotMaxs(targ);
825                                         if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
826                                         {
827                                                 deathtype |= HITTYPE_HEADSHOT;
828                                         }
829                                 }
830                                 else if(targ.classname == "turret_head")
831                                 {
832                                         deathtype |= HITTYPE_HEADSHOT;
833                                 }
834                                 if(deathtype & HITTYPE_HEADSHOT)
835                                         if(damage_headshotbonus > 0)
836                                                 damage *= 1 + damage_headshotbonus;
837                         }
838
839                         entity victim;
840                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
841                                 victim = targ.owner;
842                         else
843                                 victim = targ;
844
845                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
846                         {
847                                 if(IsDifferentTeam(victim, attacker))
848                                 {
849                                         if(damage > 0)
850                                         {
851                                                 if(deathtype != DEATH_FIRE)
852                                                 {
853                                                         if(victim.BUTTON_CHAT)
854                                                                 attacker.typehitsound += 1;
855                                                         else
856                                                                 attacker.hitsound += 1;
857                                                 }
858
859                                                 damage_goodhits += 1;
860                                                 damage_gooddamage += damage;
861
862                                                 if not(DEATH_ISSPECIAL(deathtype))
863                                                 {
864                                                         if(targ.classname == "player") // don't do this for vehicles
865                                                         if(!g_minstagib)
866                                                         if(IsFlying(victim))
867                                                                 yoda = 1;
868
869                                                         if(g_minstagib)
870                                                         if(victim.items & IT_STRENGTH)
871                                                                 yoda = 1;
872
873                                                         if(deathtype & HITTYPE_HEADSHOT)
874                                                                 headshot = 1;
875                                                 }
876                                                 if(g_ca)
877                                                         PlayerScore_Add(attacker, SP_SCORE, damage * autocvar_g_ca_damage2score_multiplier);
878                                         }
879                                 }
880                                 else
881                                 {
882                                         if(deathtype != DEATH_FIRE)
883                                         {
884                                                 attacker.typehitsound += 1;
885                                         }
886                                         if(mirrordamage > 0)
887                                                 if(time > attacker.teamkill_complain)
888                                                 {
889                                                         attacker.teamkill_complain = time + 5;
890                                                         attacker.teamkill_soundtime = time + 0.4;
891                                                         attacker.teamkill_soundsource = targ;
892                                                 }
893                                 }
894                         }
895                 }
896         }
897
898         // apply push
899         if (self.damageforcescale)
900         if (vlen(force))
901         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
902         {
903                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
904                 if(self.movetype == MOVETYPE_PHYSICS)
905                 {
906                         entity farcent;
907                         farcent = spawn();
908                         farcent.classname = "farce";
909                         farcent.enemy = self;
910                         farcent.movedir = farce * 10;
911                         if(self.mass)
912                                 farcent.movedir = farcent.movedir * self.mass;
913                         farcent.origin = hitloc;
914                         farcent.forcetype = FORCETYPE_FORCEATPOS;
915                         farcent.nextthink = time + 0.1;
916                         farcent.think = SUB_Remove;
917                 }
918                 else
919                         self.velocity = self.velocity + farce;
920                 self.flags &~= FL_ONGROUND;
921                 UpdateCSQCProjectile(self);
922         }
923         // apply damage
924         if (damage != 0 || (self.damageforcescale && vlen(force)))
925         if (self.event_damage)
926                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
927         self = oldself;
928
929         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
930         {
931                 if(g_runematch)
932                 {
933                         if (attacker.runes & RUNE_VAMPIRE)
934                         {
935                         // apply vampire rune
936                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
937                                 {
938                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb;
939                                         attacker.health = bound(
940                                                 autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 40
941                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb,
942                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
943                                 }
944                                 else
945                                 {
946                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_absorb;
947                                         attacker.health = bound(
948                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
949                                                                                         // empathy won't let you gain health in the same way...
950                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_absorb,
951                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
952                                         }
953                         }
954                         // apply empathy curse
955                         else if (attacker.runes & CURSE_EMPATHY)
956                         {
957                                 attacker.health = bound(
958                                         autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 20
959                                         attacker.health + damage * autocvar_g_balance_curse_empathy_takedamage,
960                                         attacker.health);
961                         }
962                 }
963         }
964
965         // apply mirror damage if any
966         if(mirrordamage > 0 || mirrorforce > 0)
967         {
968                 attacker = attacker_save;
969                 if(g_minstagib)
970                 if(mirrordamage > 0)
971                 {
972                         // just lose extra LIVES, don't kill the player for mirror damage
973                         if(attacker.armorvalue > 0)
974                         {
975                                 attacker.armorvalue = attacker.armorvalue - 1;
976                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
977                                 attacker.hitsound += 1;
978                         }
979                         mirrordamage = 0;
980                 }
981
982                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
983                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
984         }
985 }
986
987 float RadiusDamage_running;
988 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
989         // Returns total damage applies to creatures
990 {
991         entity  targ;
992         vector  blastorigin;
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         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1013         total_damage_to_creatures = 0;
1014
1015         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1016                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1017                 {
1018                         force = inflictor.velocity;
1019                         if(vlen(force) == 0)
1020                                 force = '0 0 -1';
1021                         else
1022                                 force = normalize(force);
1023                         if(forceintensity >= 0)
1024                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
1025                         else
1026                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
1027                 }
1028
1029         stat_damagedone = 0;
1030
1031         targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
1032         while (targ)
1033         {
1034                 next = targ.chain;
1035                 if (targ != inflictor)
1036                         if (ignore != targ) 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, blastorigin);
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(blastorigin, 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 fire_damagepersec;
1223 .float fire_endtime;
1224 .float fire_deathtype;
1225 .entity fire_owner;
1226 .float fire_hitsound;
1227 .entity fire_burner;
1228
1229 void fireburner_think();
1230
1231 float Fire_IsBurning(entity e)
1232 {
1233         return (time < e.fire_endtime);
1234 }
1235
1236 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1237 {
1238         float dps;
1239         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1240
1241         if(e.classname == "player")
1242         {
1243                 if(e.deadflag)
1244                         return -1;
1245         }
1246         else
1247         {
1248                 if(!e.fire_burner)
1249                 {
1250                         // print("adding a fire burner to ", e.classname, "\n");
1251                         e.fire_burner = spawn();
1252                         e.fire_burner.classname = "fireburner";
1253                         e.fire_burner.think = fireburner_think;
1254                         e.fire_burner.nextthink = time;
1255                         e.fire_burner.owner = e;
1256                 }
1257         }
1258
1259         t = max(t, 0.1);
1260         dps = d / t;
1261         if(Fire_IsBurning(e))
1262         {
1263                 mintime = e.fire_endtime - time;
1264                 maxtime = max(mintime, t);
1265
1266                 mindps = e.fire_damagepersec;
1267                 maxdps = max(mindps, dps);
1268
1269                 if(maxtime > mintime || maxdps > mindps)
1270                 {
1271                         mindamage = mindps * mintime;
1272                         maxdamage = mindamage + d;
1273
1274                         // interval [mintime, maxtime] * [mindps, maxdps]
1275                         // intersected with
1276                         // [mindamage, maxdamage]
1277                         // maximum of this!
1278
1279                         if(maxdamage >= maxtime * maxdps)
1280                         {
1281                                 totaltime = maxtime;
1282                                 totaldamage = maxtime * maxdps;
1283
1284                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1285                         }
1286                         else
1287                         {
1288                                 // maxdamage is inside the interval!
1289                                 // first, try to use mindps; only if this fails, increase dps as needed
1290                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1291                                 totaldamage = maxdamage;
1292                                 // can totaldamage / totaltime be >= maxdps?
1293                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1294                                 // we know maxdamage < maxtime * maxdps
1295                                 // so it cannot be
1296
1297                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1298                         }
1299
1300                         // total conditions for increasing:
1301                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1302                         // however:
1303                         //     if maxtime = mintime, maxdps = mindps
1304                         // then:
1305                         //     maxdamage = mindamage + d
1306                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1307                         // so the last condition is not needed
1308
1309                         e.fire_damagepersec = totaldamage / totaltime;
1310                         e.fire_endtime = time + totaltime;
1311                         if(totaldamage > 1.2 * mindamage)
1312                         {
1313                                 e.fire_deathtype = dt;
1314                                 if(e.fire_owner != o)
1315                                 {
1316                                         e.fire_owner = o;
1317                                         e.fire_hitsound = FALSE;
1318                                 }
1319                         }
1320                         if(accuracy_isgooddamage(o, e))
1321                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1322                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1323                 }
1324                 else
1325                         return 0;
1326         }
1327         else
1328         {
1329                 e.fire_damagepersec = dps;
1330                 e.fire_endtime = time + t;
1331                 e.fire_deathtype = dt;
1332                 e.fire_owner = o;
1333                 e.fire_hitsound = FALSE;
1334                 if(accuracy_isgooddamage(o, e))
1335                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1336                 return d;
1337         }
1338 }
1339
1340 void Fire_ApplyDamage(entity e)
1341 {
1342         float t, d, hi, ty;
1343         entity o;
1344
1345         if not(Fire_IsBurning(e))
1346                 return;
1347
1348         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1349         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1350                 o = e.fire_owner;
1351
1352         // water and slime stop fire
1353         if(e.waterlevel)
1354         if(e.watertype != CONTENT_LAVA)
1355                 e.fire_endtime = 0;
1356
1357         // ice stops fire
1358         if(e.freezetag_frozen)
1359                 e.fire_endtime = 0;
1360
1361         t = min(frametime, e.fire_endtime - time);
1362         d = e.fire_damagepersec * t;
1363
1364         hi = e.fire_owner.hitsound;
1365         ty = e.fire_owner.typehitsound;
1366         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1367         if(e.fire_hitsound && e.fire_owner)
1368         {
1369                 e.fire_owner.hitsound = hi;
1370                 e.fire_owner.typehitsound = ty;
1371         }
1372         e.fire_hitsound = TRUE;
1373
1374         if not(IS_INDEPENDENT_PLAYER(e))
1375         FOR_EACH_PLAYER(other) if(e != other)
1376         {
1377                 if(other.classname == "player")
1378                 if(other.deadflag == DEAD_NO)
1379                 if not(IS_INDEPENDENT_PLAYER(other))
1380                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1381                 {
1382                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1383                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1384                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1385                 }
1386         }
1387 }
1388
1389 void Fire_ApplyEffect(entity e)
1390 {
1391         if(Fire_IsBurning(e))
1392                 e.effects |= EF_FLAME;
1393         else
1394                 e.effects &~= EF_FLAME;
1395 }
1396
1397 void fireburner_think()
1398 {
1399         // for players, this is done in the regular loop
1400         if(wasfreed(self.owner))
1401         {
1402                 remove(self);
1403                 return;
1404         }
1405         Fire_ApplyEffect(self.owner);
1406         if(!Fire_IsBurning(self.owner))
1407         {
1408                 self.owner.fire_burner = world;
1409                 remove(self);
1410                 return;
1411         }
1412         Fire_ApplyDamage(self.owner);
1413         self.nextthink = time;
1414 }