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