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