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