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