Merge remote-tracking branch 'origin/master' into terencehill/ca_arena_mutators
[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
51 .float dmg_team;
52 .float teamkill_complain;
53 .float teamkill_soundtime;
54 .entity teamkill_soundsource;
55 .entity pusher;
56 .float istypefrag;
57 .float taunt_soundtime;
58
59
60 float IsDifferentTeam(entity a, entity b)
61 {
62         if(teamplay)
63         {
64                 if(a.team == b.team)
65                         return 0;
66         }
67         else
68         {
69                 if(a == b)
70                         return 0;
71         }
72         return 1;
73 }
74
75 float IsFlying(entity a)
76 {
77         if(a.flags & FL_ONGROUND)
78                 return 0;
79         if(a.waterlevel >= WATERLEVEL_SWIMMING)
80                 return 0;
81         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
82         if(trace_fraction < 1)
83                 return 0;
84         return 1;
85 }
86
87 void UpdateFrags(entity player, float f)
88 {
89         PlayerTeamScore_AddScore(player, f);
90 }
91
92 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
93 void W_SwitchWeapon_Force(entity e, float w);
94 entity GiveFrags_randomweapons;
95 void GiveFrags (entity attacker, entity targ, float f, float deathtype)
96 {
97         // TODO route through PlayerScores instead
98         if(gameover) return;
99
100         if(f < 0)
101         {
102                 if(targ == attacker)
103                 {
104                         // suicide
105                         PlayerScore_Add(attacker, SP_SUICIDES, 1);
106                 }
107                 else
108                 {
109                         // teamkill
110                         PlayerScore_Add(attacker, SP_KILLS, -1); // or maybe add a teamkills field?
111                 }
112         }
113         else
114         {
115                 // regular frag
116                 PlayerScore_Add(attacker, SP_KILLS, 1);
117                 if(targ.playerid)
118                         PlayerStats_Event(attacker, sprintf("kills-%d", targ.playerid), 1);
119         }
120
121         PlayerScore_Add(targ, SP_DEATHS, 1);
122
123         if(targ != attacker) // not for suicides
124         if(g_weaponarena_random)
125         {
126                 // after a frag, exchange the current weapon (or the culprit, if detectable) by a new random weapon
127                 float culprit;
128                 culprit = DEATH_WEAPONOF(deathtype);
129                 if(!culprit)
130                         culprit = attacker.weapon;
131                 else if(!WEPSET_CONTAINS_EW(attacker, culprit))
132                         culprit = attacker.weapon;
133
134                 if(g_weaponarena_random_with_laser && culprit == WEP_LASER)
135                 {
136                         // no exchange
137                 }
138                 else
139                 {
140                         if(!GiveFrags_randomweapons)
141                         {
142                                 GiveFrags_randomweapons = spawn();
143                                 GiveFrags_randomweapons.classname = "GiveFrags_randomweapons";
144                         }
145
146                         if(inWarmupStage)
147                                 WEPSET_COPY_EA(GiveFrags_randomweapons, warmup_start_weapons);
148                         else
149                                 WEPSET_COPY_EA(GiveFrags_randomweapons, start_weapons);
150
151                         // all others (including the culprit): remove
152                         WEPSET_ANDNOT_EE(GiveFrags_randomweapons, attacker);
153                         WEPSET_ANDNOT_EW(GiveFrags_randomweapons, culprit);
154
155                         // among the remaining ones, choose one by random
156                         W_RandomWeapons(GiveFrags_randomweapons, 1);
157
158                         if(!WEPSET_EMPTY_E(GiveFrags_randomweapons))
159                         {
160                                 WEPSET_OR_EE(attacker, GiveFrags_randomweapons);
161                                 WEPSET_ANDNOT_EW(attacker, culprit);
162                         }
163                 }
164
165                 // after a frag, choose another random weapon set
166                 if not(WEPSET_CONTAINS_EW(attacker, attacker.weapon))
167                         W_SwitchWeapon_Force(attacker, w_getbestweapon(attacker));
168         }
169
170         // FIXME fix the mess this is (we have REAL points now!)
171         entity oldself;
172         oldself = self;
173         self = attacker;
174         frag_attacker = attacker;
175         frag_target = targ;
176         frag_score = f;
177         if(MUTATOR_CALLHOOK(GiveFragsForKill))
178         {
179                 f = frag_score;
180                 self = oldself;
181         }
182         else
183         {
184                 self = oldself;
185                 if(g_lms)
186                 {
187                         // remove a life
188                         float tl;
189                         tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
190                         if(tl < lms_lowest_lives)
191                                 lms_lowest_lives = tl;
192                         if(tl <= 0)
193                         {
194                                 if(!lms_next_place)
195                                         lms_next_place = player_count;
196                                 else
197                                         lms_next_place = min(lms_next_place, player_count);
198                                 PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
199                                 --lms_next_place;
200                         }
201                         f = 0;
202                 }
203         }
204
205         attacker.totalfrags += f;
206
207         if(f)
208                 UpdateFrags(attacker, f);
209 }
210
211 string AppendItemcodes(string s, entity player)
212 {
213         float w;
214         w = player.weapon;
215         //if(w == 0)
216         //      w = player.switchweapon;
217         if(w == 0)
218                 w = player.cnt; // previous weapon!
219         s = strcat(s, ftos(w));
220         if(time < player.strength_finished)
221                 s = strcat(s, "S");
222         if(time < player.invincible_finished)
223                 s = strcat(s, "I");
224         if(player.flagcarried != world)
225                 s = strcat(s, "F");
226         if(player.BUTTON_CHAT)
227                 s = strcat(s, "T");
228         if(player.kh_next)
229                 s = strcat(s, "K");
230         return s;
231 }
232
233 void LogDeath(string mode, float deathtype, entity killer, entity killed)
234 {
235         string s;
236         if(!autocvar_sv_eventlog)
237                 return;
238         s = strcat(":kill:", mode);
239         s = strcat(s, ":", ftos(killer.playerid));
240         s = strcat(s, ":", ftos(killed.playerid));
241         s = strcat(s, ":type=", Deathtype_Name(deathtype));
242         s = strcat(s, ":items=");
243         s = AppendItemcodes(s, killer);
244         if(killed != killer)
245         {
246                 s = strcat(s, ":victimitems=");
247                 s = AppendItemcodes(s, killed);
248         }
249         GameLogEcho(s);
250 }
251
252 void Obituary_SpecialDeath(
253         entity notif_target,
254         float murder,
255         float deathtype,
256         string s1, string s2, string s3,
257         float f1, float f2, float f3)
258 {
259         if(DEATH_ISSPECIAL(deathtype))
260         {
261                 entity deathent = deathtypes[(deathtype - DT_FIRST) - 1];
262                 if not(deathent) { backtrace("Obituary_SpecialDeath: Could not find deathtype entity!\n"); return; }
263
264                 if(murder)
265                 {
266                         if(deathent.death_msgmurder)
267                         {
268                                 Send_Notification_WOVA(
269                                         NOTIF_ONE,
270                                         notif_target,
271                                         MSG_MULTI,
272                                         deathent.death_msgmurder.nent_id,
273                                         s1, s2, s3, "",
274                                         f1, f2, f3, 0
275                                 );
276                                 Send_Notification_WOVA(
277                                         NOTIF_ALL_EXCEPT,
278                                         notif_target,
279                                         MSG_INFO,
280                                         deathent.death_msgmurder.nent_msginfo.nent_id,
281                                         s1, s2, s3, "",
282                                         f1, f2, f3, 0
283                                 );
284                         }
285                 }
286                 else
287                 {
288                         if(deathent.death_msgself)
289                         {
290                                 Send_Notification_WOVA(
291                                         NOTIF_ONE,
292                                         notif_target,
293                                         MSG_MULTI,
294                                         deathent.death_msgself.nent_id,
295                                         s1, s2, s3, "",
296                                         f1, f2, f3, 0
297                                 );
298                                 Send_Notification_WOVA(
299                                         NOTIF_ALL_EXCEPT,
300                                         notif_target,
301                                         MSG_INFO,
302                                         deathent.death_msgself.nent_msginfo.nent_id,
303                                         s1, s2, s3, "",
304                                         f1, f2, f3, 0
305                                 );
306                         }
307                 }
308         }
309         else { backtrace("Obituary_SpecialDeath called without a special deathtype?\n"); return; }
310 }
311
312 float w_deathtype;
313 float Obituary_WeaponDeath(
314         entity notif_target,
315         float murder,
316         float deathtype,
317         string s1, string s2, string s3,
318         float f1, float f2)
319 {
320         float death_weapon = DEATH_WEAPONOF(deathtype);
321         if(death_weapon)
322         {
323                 w_deathtype = deathtype;
324                 float death_message = weapon_action(death_weapon, ((murder) ? WR_KILLMESSAGE : WR_SUICIDEMESSAGE));
325                 w_deathtype = FALSE;
326
327                 if(death_message)
328                 {
329                         Send_Notification_WOVA(
330                                 NOTIF_ONE,
331                                 notif_target,
332                                 MSG_MULTI,
333                                 death_message,
334                                 s1, s2, s3, "",
335                                 f1, f2, 0, 0
336                         );
337                         Send_Notification_WOVA(
338                                 NOTIF_ALL_EXCEPT,
339                                 notif_target,
340                                 MSG_INFO,
341                                 msg_multi_notifs[death_message - 1].nent_msginfo.nent_id,
342                                 s1, s2, s3, "",
343                                 f1, f2, 0, 0
344                         );
345                 }
346                 else
347                 {
348                         dprint(sprintf(
349                                 "Obituary_WeaponDeath(): ^1Deathtype ^7(%d)^1 has no notification for weapon %d!\n",
350                                 deathtype,
351                                 death_weapon
352                         ));
353                 }
354
355                 return TRUE;
356         }
357         return FALSE;
358 }
359
360 void Obituary(entity attacker, entity inflictor, entity targ, float deathtype)
361 {
362         // Sanity check
363         if not(IS_PLAYER(targ)) { backtrace("Obituary called on non-player?!\n"); return; }
364
365         // Declarations
366         float notif_firstblood = FALSE;
367         float kill_count_to_attacker, kill_count_to_target;
368
369         // Set final information for the death
370         targ.death_origin = targ.origin;
371         if(targ != attacker) { targ.killer_origin = attacker.origin; }
372         string deathlocation = (autocvar_notification_server_allows_location ? NearestLocation(targ.death_origin) : "");
373
374         #ifdef NOTIFICATIONS_DEBUG
375         Debug_Notification(
376                 sprintf(
377                         "Obituary(%s, %s, %s, %s = %d);\n",
378                         attacker.netname,
379                         inflictor.netname,
380                         targ.netname,
381                         Deathtype_Name(deathtype),
382                         deathtype
383                 )
384         );
385         #endif
386         
387         // =======
388         // SUICIDE
389         // =======
390         if(targ == attacker)
391         {
392                 if(DEATH_ISSPECIAL(deathtype))
393                 {
394                         if(deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
395                         {
396                                 Obituary_SpecialDeath(targ, FALSE, deathtype, targ.netname, deathlocation, "", targ.team, 0, 0);
397                         }
398                         else
399                         {
400                                 switch(deathtype)
401                                 {
402                                         case DEATH_MIRRORDAMAGE:
403                                         {
404                                                 Obituary_SpecialDeath(targ, FALSE, deathtype, targ.netname, deathlocation, "", targ.killcount, 0, 0);
405                                                 break;
406                                         }
407                                         
408                                         default:
409                                         {
410                                                 Obituary_SpecialDeath(targ, FALSE, deathtype, targ.netname, deathlocation, "", targ.killcount, 0, 0);
411                                                 break;
412                                         }
413                                 }
414                         }
415                 }
416                 else if not(Obituary_WeaponDeath(targ, FALSE, deathtype, targ.netname, deathlocation, "", targ.killcount, 0))
417                 {
418                         backtrace("SUICIDE: what the hell happened here?\n");
419                         return;
420                 }
421                 LogDeath("suicide", deathtype, targ, targ);
422                 GiveFrags(attacker, targ, -1, deathtype);
423         }
424
425         // ======
426         // MURDER
427         // ======
428         else if(IS_PLAYER(attacker))
429         {
430                 if(!IsDifferentTeam(attacker, targ))
431                 {
432                         LogDeath("tk", deathtype, attacker, targ);
433                         GiveFrags(attacker, targ, -1, deathtype);
434
435                         attacker.killcount = 0;
436                         
437                         Send_Notification(NOTIF_ONE, attacker, MSG_CENTER, CENTER_DEATH_TEAMKILL_FRAG, targ.netname);
438                         Send_Notification(NOTIF_ONE, targ, MSG_CENTER, CENTER_DEATH_TEAMKILL_FRAGGED, attacker.netname);
439                         Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(targ.team, INFO_DEATH_TEAMKILL_), targ.netname, attacker.netname, deathlocation, targ.killcount);
440
441                         // In this case, the death message will ALWAYS be "foo was betrayed by bar"
442                         // No need for specific death/weapon messages...
443                 }
444                 else
445                 {
446                         LogDeath("frag", deathtype, attacker, targ);
447                         GiveFrags(attacker, targ, 1, deathtype);
448
449                         attacker.taunt_soundtime = time + 1;
450                         attacker.killcount = attacker.killcount + 1;
451
452                         #define SPREE_ITEM(counta,countb,center,normal,gentle) \
453                                 case counta: \
454                                 { \
455                                         Send_Notification(NOTIF_ONE, attacker, MSG_ANNCE, ANNCE_KILLSTREAK_##countb); \
456                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_##counta, 1); \
457                                         break; \
458                                 }
459                         switch(attacker.killcount)
460                         {
461                                 KILL_SPREE_LIST
462                                 default: break;
463                         }
464                         #undef SPREE_ITEM
465
466                         if(!checkrules_firstblood)
467                         {
468                                 checkrules_firstblood = TRUE;
469                                 notif_firstblood = TRUE; // modify the current messages so that they too show firstblood information
470                                 PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD, 1);
471                                 PlayerStats_Event(targ, PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM, 1);
472
473                                 // tell spree_inf and spree_cen that this is a first-blood and first-victim event
474                                 kill_count_to_attacker = -1;
475                                 kill_count_to_target = -2;
476                         }
477                         else
478                         {
479                                 kill_count_to_attacker = attacker.killcount;
480                                 kill_count_to_target = 0;
481                         }
482
483                         float verbose_allowed = (autocvar_notification_server_allows_frag_verbose && ((autocvar_notification_server_allows_frag_verbose == 2) || inWarmupStage));
484                         if(targ.istypefrag)
485                         {
486                                 if(attacker.FRAG_VERBOSE && verbose_allowed)
487                                         Send_Notification(NOTIF_ONE, attacker, MSG_CENTER, CENTER_DEATH_MURDER_TYPEFRAG_VERBOSE, targ.netname, kill_count_to_attacker, (IS_BOT_CLIENT(targ) ? NO_MSG : targ.ping));
488                                 else
489                                         Send_Notification(NOTIF_ONE, attacker, MSG_CENTER, CENTER_DEATH_MURDER_TYPEFRAG, targ.netname, kill_count_to_attacker);
490
491                                 if(targ.FRAG_VERBOSE && verbose_allowed)
492                                         Send_Notification(NOTIF_ONE, targ, MSG_CENTER, CENTER_DEATH_MURDER_TYPEFRAGGED_VERBOSE, attacker.netname, kill_count_to_target, attacker.health, attacker.armorvalue, (IS_BOT_CLIENT(attacker) ? NO_MSG : attacker.ping));
493                                 else
494                                         Send_Notification(NOTIF_ONE, targ, MSG_CENTER, CENTER_DEATH_MURDER_TYPEFRAGGED, attacker.netname, kill_count_to_target);
495                         }
496                         else
497                         {
498                                 if(attacker.FRAG_VERBOSE && verbose_allowed)
499                                         Send_Notification(NOTIF_ONE, attacker, MSG_CENTER, CENTER_DEATH_MURDER_FRAG_VERBOSE, targ.netname, kill_count_to_attacker, (IS_BOT_CLIENT(targ) ? NO_MSG : targ.ping));
500                                 else
501                                         Send_Notification(NOTIF_ONE, attacker, MSG_CENTER, CENTER_DEATH_MURDER_FRAG, targ.netname, kill_count_to_attacker);
502
503                                 if(targ.FRAG_VERBOSE && verbose_allowed)
504                                         Send_Notification(NOTIF_ONE, targ, MSG_CENTER, CENTER_DEATH_MURDER_FRAGGED_VERBOSE, attacker.netname, kill_count_to_target, attacker.health, attacker.armorvalue, (IS_BOT_CLIENT(attacker) ? NO_MSG : attacker.ping));
505                                 else
506                                         Send_Notification(NOTIF_ONE, targ, MSG_CENTER, CENTER_DEATH_MURDER_FRAGGED, attacker.netname, kill_count_to_target);
507                         }
508
509                         if not(Obituary_WeaponDeath(targ, TRUE, deathtype, targ.netname, attacker.netname, deathlocation, targ.killcount, kill_count_to_attacker))
510                                 Obituary_SpecialDeath(targ, TRUE, deathtype, targ.netname, attacker.netname, deathlocation, targ.killcount, kill_count_to_attacker, 0);
511                 }
512         }
513
514         // =============
515         // ACCIDENT/TRAP
516         // =============
517         else
518         {
519                 switch(deathtype)
520                 {
521                         // For now, we're just forcing HURTTRIGGER to behave as "DEATH_VOID" and giving it no special options...
522                         // Later on you will only be able to make custom messages using DEATH_CUSTOM,
523                         // and there will be a REAL DEATH_VOID implementation which mappers will use.
524                         /*case DEATH_HURTTRIGGER:
525                         {
526                                 s1 = targ.netname;
527                                 s2 = inflictor.message;
528                                 if(strstrofs(s2, "%", 0) < 0) { s2 = strcat("%s ", s2); }
529                                 break;
530                         }*/
531
532                         case DEATH_CUSTOM:
533                         {
534                                 Obituary_SpecialDeath(targ, FALSE, deathtype,
535                                         targ.netname,
536                                         ((strstrofs(deathmessage, "%", 0) < 0) ? strcat("%s ", deathmessage) : deathmessage),
537                                         deathlocation,
538                                         targ.killcount,
539                                         0,
540                                         0);
541                                 break;
542                         }
543                         
544                         default:
545                         {
546                                 Obituary_SpecialDeath(targ, FALSE, deathtype, targ.netname, deathlocation, "", targ.killcount, 0, 0);
547                                 break;
548                         }
549                 }
550
551                 LogDeath("accident", deathtype, targ, targ);
552                 GiveFrags(targ, targ, -1, deathtype);
553
554                 if(PlayerScore_Add(targ, SP_SCORE, 0) == -5)
555                 {
556                         Send_Notification(NOTIF_ONE, targ, MSG_ANNCE, ANNCE_ACHIEVEMENT_BOTLIKE);
557                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
558                 }
559         }
560
561         // reset target kill count
562         if(targ.killcount) { targ.killcount = 0; }
563 }
564
565 // these are updated by each Damage call for use in button triggering and such
566 entity damage_targ;
567 entity damage_inflictor;
568 entity damage_attacker;
569
570 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
571 {
572         float mirrordamage;
573         float mirrorforce;
574         float complainteamdamage = 0; 
575         entity attacker_save;
576         mirrordamage = 0;
577         mirrorforce = 0;
578
579         if (gameover || targ.killcount == -666)
580                 return;
581
582         entity oldself;
583         oldself = self;
584         self = targ;
585         damage_targ = targ;
586         damage_inflictor = inflictor;
587         damage_attacker = attacker;
588                 attacker_save = attacker;
589
590         if(targ.classname == "player")
591                 if(targ.hook)
592                         if(targ.hook.aiment)
593                                 if(targ.hook.aiment == attacker)
594                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
595
596         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
597         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
598         {
599                 if(targ.classname == "player")
600                         if not(IsDifferentTeam(targ, attacker))
601                         {
602                                 self = oldself;
603                                 return;
604                         }
605         }
606
607         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
608         {
609                 // These are ALWAYS lethal
610                 // No damage modification here
611                 // Instead, prepare the victim for his death...
612                 targ.armorvalue = 0;
613                 targ.spawnshieldtime = 0;
614                 targ.health = 0.9; // this is < 1
615                 targ.flags -= targ.flags & FL_GODMODE;
616                 damage = 100000;
617         }
618         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
619         {
620                 // no processing
621         }
622         else
623         {
624                 /*
625                 skill based bot damage? gtfo. (tZork)
626                 if (targ.classname == "player")
627                 if (attacker.classname == "player")
628                 if (!targ.isbot)
629                 if (attacker.isbot)
630                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
631         */
632         
633                 // nullify damage if teamplay is on
634                 if(deathtype != DEATH_TELEFRAG)
635                 if(attacker.classname == "player")
636                 {
637                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
638                         {
639                                 damage = 0;
640                                 force = '0 0 0';
641                         }
642                         else if(!IsDifferentTeam(attacker, targ))
643                         {
644                                 if(autocvar_teamplay_mode == 1)
645                                         damage = 0;
646                                 else if(attacker != targ)
647                                 {
648                                         if(autocvar_teamplay_mode == 3)
649                                                 damage = 0;
650                                         else if(autocvar_teamplay_mode == 4)
651                                         {
652                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
653                                                 {
654                                                         attacker.dmg_team = attacker.dmg_team + damage;
655                                                         complainteamdamage = attacker.dmg_team - autocvar_g_teamdamage_threshold;
656                                                         if(complainteamdamage > 0 && !g_ca) // FIXME why is g_ca ruled out here? Why not just g_mirrordamage 0 on CA servers?
657                                                                 mirrordamage = autocvar_g_mirrordamage * complainteamdamage;
658                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
659                                                         if(g_minstagib)
660                                                         {
661                                                                 if(autocvar_g_friendlyfire == 0)
662                                                                         damage = 0;
663                                                         }
664                                                         else if(g_ca)
665                                                                 damage = 0;
666                                                         else
667                                                                 damage = autocvar_g_friendlyfire * damage;
668                                                         // mirrordamage will be used LATER
669
670                                                         if(autocvar_g_mirrordamage_virtual)
671                                                         {
672                                                                 vector v  = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
673                                                                 attacker.dmg_take += v_x;
674                                                                 attacker.dmg_save += v_y;
675                                                                 attacker.dmg_inflictor = inflictor;
676                                                                 mirrordamage = v_z; // = 0, to make fteqcc stfu
677                                                                 mirrorforce = 0;
678                                                         }
679
680                                                         if(autocvar_g_friendlyfire_virtual)
681                                                         {
682                                                                 vector v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
683                                                                 targ.dmg_take += v_x;
684                                                                 targ.dmg_save += v_y;
685                                                                 targ.dmg_inflictor = inflictor;
686                                                                 damage = 0;
687                                                                 if(!autocvar_g_friendlyfire_virtual_force)
688                                                                         force = '0 0 0';
689                                                         }
690                                                 }
691                                                 else
692                                                         damage = 0;
693                                         }
694                                 }
695                         }
696                 }
697
698                 if(targ.classname == "player")
699                 if(attacker.classname == "player")
700                 if(attacker != targ)
701                 {
702                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
703                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
704                 }
705
706                 if(targ.classname == "player")
707                 if (g_minstagib)
708                 {
709                         if ((deathtype == DEATH_FALL)  ||
710                                 (deathtype == DEATH_DROWN) ||
711                                 (deathtype == DEATH_SLIME) ||
712                                 (deathtype == DEATH_LAVA)  ||
713                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
714                         {
715                                 self = oldself;
716                                 return;
717                         }
718                         if(damage > 0)
719                             damage = 10000;
720                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
721                         {
722                                 targ.armorvalue -= 1;
723                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
724                                 damage = 0;
725                                 targ.hitsound += 1;
726                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
727                         }
728                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
729                         {
730                                 damage = 0;
731                                 mirrordamage = 0;
732                                 complainteamdamage = 0;
733                                 if (targ != attacker)
734                                 {
735                                         if ((targ.health >= 1) && (targ.classname == "player"))
736                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
737                                         force = '0 0 0';
738                                         // keep mirrorforce
739                                         attacker = targ;
740                                 }
741                         }
742                 }
743
744                 if not(DEATH_ISSPECIAL(deathtype))
745                 {
746                         damage *= g_weapondamagefactor;
747                         mirrordamage *= g_weapondamagefactor;
748                         complainteamdamage *= g_weapondamagefactor;
749                         force = force * g_weaponforcefactor;
750                         mirrorforce *= g_weaponforcefactor;
751                 }
752                 
753                 // should this be changed at all? If so, in what way?
754                 frag_attacker = attacker;
755                 frag_target = targ;
756                 frag_damage = damage;
757                 frag_force = force;
758         frag_deathtype = deathtype;
759                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
760                 damage = frag_damage;
761                 force = frag_force;
762                 
763                 // apply strength multiplier
764                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
765                 {
766                         if(targ == attacker)
767                         {
768                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
769                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
770                         }
771                         else
772                         {
773                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
774                                 force = force * autocvar_g_balance_powerup_strength_force;
775                         }
776                 }
777
778                 // apply invincibility multiplier
779                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
780                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
781
782                 if (targ == attacker)
783                 {
784                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
785                                 damage = 0;
786                         else
787                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
788                 }
789
790                 // count the damage
791                 if(attacker)
792                 if(!targ.deadflag)
793                 if(targ.takedamage == DAMAGE_AIM)
794                 if(targ != attacker)
795                 {
796                         entity victim;
797                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
798                                 victim = targ.owner;
799                         else
800                                 victim = targ;
801
802                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
803                         {
804                                 if(IsDifferentTeam(victim, attacker))
805                                 {
806                                         if(damage > 0)
807                                         {
808                                                 if(deathtype != DEATH_FIRE)
809                                                 {
810                                                         if(victim.BUTTON_CHAT)
811                                                                 attacker.typehitsound += 1;
812                                                         else
813                                                                 attacker.hitsound += 1;
814                                                 }
815
816                                                 damage_goodhits += 1;
817                                                 damage_gooddamage += damage;
818
819                                                 if not(DEATH_ISSPECIAL(deathtype))
820                                                 {
821                                                         if(targ.classname == "player") // don't do this for vehicles
822                                                         if(!g_minstagib)
823                                                         if(IsFlying(victim))
824                                                                 yoda = 1;
825
826                                                         if(g_minstagib)
827                                                         if(victim.items & IT_STRENGTH)
828                                                                 yoda = 1;
829                                                 }
830                                         }
831                                 }
832                                 else
833                                 {
834                                         if(deathtype != DEATH_FIRE)
835                                         {
836                                                 attacker.typehitsound += 1;
837                                         }
838                                         if(complainteamdamage > 0)
839                                                 if(time > attacker.teamkill_complain)
840                                                 {
841                                                         attacker.teamkill_complain = time + 5;
842                                                         attacker.teamkill_soundtime = time + 0.4;
843                                                         attacker.teamkill_soundsource = targ;
844                                                 }
845                                 }
846                         }
847                 }
848         }
849
850         // apply push
851         if (self.damageforcescale)
852         if (vlen(force))
853         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
854         {
855                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
856                 if(self.movetype == MOVETYPE_PHYSICS)
857                 {
858                         entity farcent;
859                         farcent = spawn();
860                         farcent.classname = "farce";
861                         farcent.enemy = self;
862                         farcent.movedir = farce * 10;
863                         if(self.mass)
864                                 farcent.movedir = farcent.movedir * self.mass;
865                         farcent.origin = hitloc;
866                         farcent.forcetype = FORCETYPE_FORCEATPOS;
867                         farcent.nextthink = time + 0.1;
868                         farcent.think = SUB_Remove;
869                 }
870                 else
871                         self.velocity = self.velocity + farce;
872                 self.flags &~= FL_ONGROUND;
873                 UpdateCSQCProjectile(self);
874         }
875         // apply damage
876         if (damage != 0 || (self.damageforcescale && vlen(force)))
877         if (self.event_damage)
878                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
879         self = oldself;
880
881         // apply mirror damage if any
882         if(mirrordamage > 0 || mirrorforce > 0)
883         {
884                 attacker = attacker_save;
885                 if(g_minstagib)
886                 if(mirrordamage > 0)
887                 {
888                         // just lose extra LIVES, don't kill the player for mirror damage
889                         if(attacker.armorvalue > 0)
890                         {
891                                 attacker.armorvalue = attacker.armorvalue - 1;
892                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
893                                 attacker.hitsound += 1;
894                         }
895                         mirrordamage = 0;
896                 }
897
898                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
899                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
900         }
901 }
902
903 float RadiusDamage_running;
904 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
905         // Returns total damage applies to creatures
906 {
907         entity  targ;
908         vector  blastorigin;
909         vector  force;
910         float   total_damage_to_creatures;
911         entity  next;
912         float   tfloordmg;
913         float   tfloorforce;
914
915         float stat_damagedone;
916
917         if(RadiusDamage_running)
918         {
919                 backtrace("RadiusDamage called recursively! Expect stuff to go HORRIBLY wrong.");
920                 return 0;
921         }
922
923         RadiusDamage_running = 1;
924
925         tfloordmg = autocvar_g_throughfloor_damage;
926         tfloorforce = autocvar_g_throughfloor_force;
927
928         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
929         total_damage_to_creatures = 0;
930
931         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
932                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
933                 {
934                         force = inflictor.velocity;
935                         if(vlen(force) == 0)
936                                 force = '0 0 -1';
937                         else
938                                 force = normalize(force);
939                         if(forceintensity >= 0)
940                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
941                         else
942                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
943                 }
944
945         stat_damagedone = 0;
946
947         targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
948         while (targ)
949         {
950                 next = targ.chain;
951                 if (targ != inflictor)
952                         if (ignore != targ) if(targ.takedamage)
953                         {
954                                 vector nearest;
955                                 vector diff;
956                                 float power;
957
958                                 // LordHavoc: measure distance to nearest point on target (not origin)
959                                 // (this guarentees 100% damage on a touch impact)
960                                 nearest = targ.WarpZone_findradius_nearest;
961                                 diff = targ.WarpZone_findradius_dist;
962                                 // round up a little on the damage to ensure full damage on impacts
963                                 // and turn the distance into a fraction of the radius
964                                 power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
965                                 //bprint(" ");
966                                 //bprint(ftos(power));
967                                 //if (targ == attacker)
968                                 //      print(ftos(power), "\n");
969                                 if (power > 0)
970                                 {
971                                         float finaldmg;
972                                         if (power > 1)
973                                                 power = 1;
974                                         finaldmg = coredamage * power + edgedamage * (1 - power);
975                                         if (finaldmg > 0)
976                                         {
977                                                 float a;
978                                                 float c;
979                                                 vector hitloc;
980                                                 vector myblastorigin;
981                                                 vector center;
982
983                                                 myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
984
985                                                 // if it's a player, use the view origin as reference
986                                                 center = CENTER_OR_VIEWOFS(targ);
987
988                                                 force = normalize(center - myblastorigin);
989                                                 force = force * (finaldmg / coredamage) * forceintensity;
990                                                 hitloc = nearest;
991
992                                                 if(targ != directhitentity)
993                                                 {
994                                                         float hits;
995                                                         float total;
996                                                         float hitratio;
997                                                         float mininv_f, mininv_d;
998
999                                                         // test line of sight to multiple positions on box,
1000                                                         // and do damage if any of them hit
1001                                                         hits = 0;
1002
1003                                                         // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1004                                                         // so for a given max stddev:
1005                                                         // n = (1 / (2 * max stddev of hitratio))^2
1006
1007                                                         mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1008                                                         mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1009
1010                                                         if(autocvar_g_throughfloor_debug)
1011                                                                 print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1012
1013                                                         total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1014
1015                                                         if(autocvar_g_throughfloor_debug)
1016                                                                 print(sprintf(" steps=%f", total));
1017
1018                                                         if (targ.classname == "player")
1019                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1020                                                         else
1021                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1022
1023                                                         if(autocvar_g_throughfloor_debug)
1024                                                                 print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1025
1026                                                         for(c = 0; c < total; ++c)
1027                                                         {
1028                                                                 //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1029                                                                 WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1030                                                                 if (trace_fraction == 1 || trace_ent == targ)
1031                                                                 {
1032                                                                         ++hits;
1033                                                                         if (hits > 1)
1034                                                                                 hitloc = hitloc + nearest;
1035                                                                         else
1036                                                                                 hitloc = nearest;
1037                                                                 }
1038                                                                 nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1039                                                                 nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1040                                                                 nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1041                                                         }
1042
1043                                                         nearest = hitloc * (1 / max(1, hits));
1044                                                         hitratio = (hits / total);
1045                                                         a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1046                                                         finaldmg = finaldmg * a;
1047                                                         a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1048                                                         force = force * a;
1049
1050                                                         if(autocvar_g_throughfloor_debug)
1051                                                                 print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1052                                                 }
1053
1054                                                 // laser force adjustments :P
1055                                                 if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1056                                                 {
1057                                                         if (targ == attacker)
1058                                                         {
1059                                                                 vector vel;
1060
1061                                                                 float force_zscale;
1062                                                                 float force_velocitybiasramp;
1063                                                                 float force_velocitybias;
1064
1065                                                                 force_velocitybiasramp = autocvar_sv_maxspeed;
1066                                                                 if(deathtype & HITTYPE_SECONDARY)
1067                                                                 {
1068                                                                         force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1069                                                                         force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1070                                                                 }
1071                                                                 else
1072                                                                 {
1073                                                                         force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1074                                                                         force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1075                                                                 }
1076
1077                                                                 vel = targ.velocity;
1078                                                                 vel_z = 0;
1079                                                                 vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1080                                                                 force =
1081                                                                         vlen(force)
1082                                                                         *
1083                                                                         normalize(normalize(force) + vel);
1084
1085                                                                 force_z *= force_zscale;
1086                                                         }
1087                                                         else
1088                                                         {
1089                                                                 if(deathtype & HITTYPE_SECONDARY)
1090                                                                 {
1091                                                                         force *= autocvar_g_balance_laser_secondary_force_other_scale;
1092                                                                 }
1093                                                                 else
1094                                                                 {
1095                                                                         force *= autocvar_g_balance_laser_primary_force_other_scale;
1096                                                                 }
1097                                                         }
1098                                                 }
1099
1100                                                 //if (targ == attacker)
1101                                                 //{
1102                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1103                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1104                                                 //      print(" (", ftos(a), ")\n");
1105                                                 //}
1106                                                 if(finaldmg || vlen(force))
1107                                                 {
1108                                                         if(targ.iscreature)
1109                                                         {
1110                                                                 total_damage_to_creatures += finaldmg;
1111
1112                                                                 if(accuracy_isgooddamage(attacker, targ))
1113                                                                         stat_damagedone += finaldmg;
1114                                                         }
1115
1116                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1117                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1118                                                         else
1119                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1120                                                 }
1121                                         }
1122                                 }
1123                         }
1124                 targ = next;
1125         }
1126
1127         RadiusDamage_running = 0;
1128
1129         if(!DEATH_ISSPECIAL(deathtype))
1130                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1131
1132         return total_damage_to_creatures;
1133 }
1134
1135 .float fire_damagepersec;
1136 .float fire_endtime;
1137 .float fire_deathtype;
1138 .entity fire_owner;
1139 .float fire_hitsound;
1140 .entity fire_burner;
1141
1142 void fireburner_think();
1143
1144 float Fire_IsBurning(entity e)
1145 {
1146         return (time < e.fire_endtime);
1147 }
1148
1149 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1150 {
1151         float dps;
1152         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1153
1154         if(e.classname == "player")
1155         {
1156                 if(e.deadflag)
1157                         return -1;
1158         }
1159         else
1160         {
1161                 if(!e.fire_burner)
1162                 {
1163                         // print("adding a fire burner to ", e.classname, "\n");
1164                         e.fire_burner = spawn();
1165                         e.fire_burner.classname = "fireburner";
1166                         e.fire_burner.think = fireburner_think;
1167                         e.fire_burner.nextthink = time;
1168                         e.fire_burner.owner = e;
1169                 }
1170         }
1171
1172         t = max(t, 0.1);
1173         dps = d / t;
1174         if(Fire_IsBurning(e))
1175         {
1176                 mintime = e.fire_endtime - time;
1177                 maxtime = max(mintime, t);
1178
1179                 mindps = e.fire_damagepersec;
1180                 maxdps = max(mindps, dps);
1181
1182                 if(maxtime > mintime || maxdps > mindps)
1183                 {
1184                         // Constraints:
1185                         
1186                         // damage we have right now
1187                         mindamage = mindps * mintime;
1188
1189                         // damage we want to get
1190                         maxdamage = mindamage + d;
1191
1192                         // but we can't exceed maxtime * maxdps!
1193                         totaldamage = min(maxdamage, maxtime * maxdps);
1194
1195                         // LEMMA:
1196                         // Look at:
1197                         // totaldamage = min(mindamage + d, maxtime * maxdps)
1198                         // We see:
1199                         // totaldamage <= maxtime * maxdps
1200                         // ==> totaldamage / maxdps <= maxtime.
1201                         // We also see:
1202                         // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
1203                         //                     >= min(mintime, maxtime)
1204                         // ==> totaldamage / maxdps >= mintime.
1205
1206                         /*
1207                         // how long do we damage then?
1208                         // at least as long as before
1209                         // but, never exceed maxdps
1210                         totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
1211                         */
1212
1213                         // alternate:
1214                         // at most as long as maximum allowed
1215                         // but, never below mindps
1216                         totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
1217
1218                         // assuming t > mintime, dps > mindps:
1219                         // we get d = t * dps = maxtime * maxdps
1220                         // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
1221                         // totaldamage / maxdps = maxtime
1222                         // totaldamage / mindps > totaldamage / maxdps = maxtime
1223                         // FROM THIS:
1224                         // a) totaltime = max(mintime, maxtime) = maxtime
1225                         // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
1226
1227                         // assuming t <= mintime:
1228                         // we get maxtime = mintime
1229                         // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
1230                         // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
1231
1232                         // assuming dps <= mindps:
1233                         // we get mindps = maxdps.
1234                         // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
1235                         // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
1236                         // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
1237
1238                         e.fire_damagepersec = totaldamage / totaltime;
1239                         e.fire_endtime = time + totaltime;
1240                         if(totaldamage > 1.2 * mindamage)
1241                         {
1242                                 e.fire_deathtype = dt;
1243                                 if(e.fire_owner != o)
1244                                 {
1245                                         e.fire_owner = o;
1246                                         e.fire_hitsound = FALSE;
1247                                 }
1248                         }
1249                         if(accuracy_isgooddamage(o, e))
1250                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1251                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1252                 }
1253                 else
1254                         return 0;
1255         }
1256         else
1257         {
1258                 e.fire_damagepersec = dps;
1259                 e.fire_endtime = time + t;
1260                 e.fire_deathtype = dt;
1261                 e.fire_owner = o;
1262                 e.fire_hitsound = FALSE;
1263                 if(accuracy_isgooddamage(o, e))
1264                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1265                 return d;
1266         }
1267 }
1268
1269 void Fire_ApplyDamage(entity e)
1270 {
1271         float t, d, hi, ty;
1272         entity o;
1273
1274         if not(Fire_IsBurning(e))
1275                 return;
1276
1277         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1278         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1279                 o = e.fire_owner;
1280
1281         // water and slime stop fire
1282         if(e.waterlevel)
1283         if(e.watertype != CONTENT_LAVA)
1284                 e.fire_endtime = 0;
1285
1286         // ice stops fire
1287         if(e.freezetag_frozen)
1288                 e.fire_endtime = 0;
1289
1290         t = min(frametime, e.fire_endtime - time);
1291         d = e.fire_damagepersec * t;
1292
1293         hi = e.fire_owner.hitsound;
1294         ty = e.fire_owner.typehitsound;
1295         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1296         if(e.fire_hitsound && e.fire_owner)
1297         {
1298                 e.fire_owner.hitsound = hi;
1299                 e.fire_owner.typehitsound = ty;
1300         }
1301         e.fire_hitsound = TRUE;
1302
1303         if not(IS_INDEPENDENT_PLAYER(e))
1304         FOR_EACH_PLAYER(other) if(e != other)
1305         {
1306                 if(other.classname == "player")
1307                 if(other.deadflag == DEAD_NO)
1308                 if not(IS_INDEPENDENT_PLAYER(other))
1309                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1310                 {
1311                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1312                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1313                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1314                 }
1315         }
1316 }
1317
1318 void Fire_ApplyEffect(entity e)
1319 {
1320         if(Fire_IsBurning(e))
1321                 e.effects |= EF_FLAME;
1322         else
1323                 e.effects &~= EF_FLAME;
1324 }
1325
1326 void fireburner_think()
1327 {
1328         // for players, this is done in the regular loop
1329         if(wasfreed(self.owner))
1330         {
1331                 remove(self);
1332                 return;
1333         }
1334         Fire_ApplyEffect(self.owner);
1335         if(!Fire_IsBurning(self.owner))
1336         {
1337                 self.owner.fire_burner = world;
1338                 remove(self);
1339                 return;
1340         }
1341         Fire_ApplyDamage(self.owner);
1342         self.nextthink = time;
1343 }