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