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