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