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