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