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