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