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