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