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