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