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