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