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