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