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