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