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