use less traces for radius damage again, and cap it at 20 (roughly this
[divverent/nexuiz.git] / data / 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 #define DAMAGE_CENTERPRINT_SPACER NEWLINES
45
46 float checkrules_firstblood;
47
48 float yoda;
49 float damage_goodhits;
50 float damage_gooddamage;
51 float headshot;
52 float damage_headshotbonus; // bonus multiplier for head shots, set to 0 after use
53
54 .float dmg_team;
55 .float teamkill_complain;
56 .float teamkill_soundtime;
57 .entity teamkill_soundsource;
58 .entity pusher;
59 .float taunt_soundtime;
60
61
62 float IsDifferentTeam(entity a, entity b)
63 {
64         if(teams_matter)
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 void UpdateFrags(entity player, float f)
90 {
91         PlayerTeamScore_AddScore(player, f);
92 }
93
94 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
95 void W_SwitchWeapon_Force(entity e, float w);
96 void GiveFrags (entity attacker, entity targ, float f)
97 {
98         float w;
99
100         // TODO route through PlayerScores instead
101         if(gameover) return;
102
103         if(f < 0)
104         {
105                 if(targ == attacker)
106                 {
107                         // suicide
108                         PlayerScore_Add(attacker, SP_SUICIDES, 1);
109                 }
110                 else
111                 {
112                         // teamkill
113                         PlayerScore_Add(attacker, SP_KILLS, -1); // or maybe add a teamkills field?
114                 }
115         }
116         else
117         {
118                 // regular frag
119                 PlayerScore_Add(attacker, SP_KILLS, 1);
120         }
121
122         PlayerScore_Add(targ, SP_DEATHS, 1);
123
124         if(g_arena)
125                 if(cvar("g_arena_roundbased"))
126                         return;
127
128         if(targ != attacker) // not for suicides
129         if(g_weaponarena_random)
130         {
131                 // after a frag, choose another random weapon set
132                 if(inWarmupStage)
133                         w = warmup_start_weapons;
134                 else
135                         w = start_weapons;
136
137                 attacker.weapons = randombits(w - (w & W_WeaponBit(attacker.weapon)), g_weaponarena_random, TRUE);
138                 if(attacker.weapons < 0)
139                 {
140                         // error from randombits: no weapon available
141                         // this means we can just give ALL weapons
142                         attacker.weapons = w;
143                 }
144                 if not(attacker.weapons & W_WeaponBit(attacker.weapon))
145                         W_SwitchWeapon_Force(attacker, w_getbestweapon(attacker));
146         }
147
148         // FIXME fix the mess this is (we have REAL points now!)
149         if(g_runematch)
150         {
151                 f = RunematchHandleFrags(attacker, targ, f);
152         }
153         else if(g_keyhunt)
154         {
155                 f = kh_HandleFrags(attacker, targ, f);
156         }
157         else if(g_lms)
158         {
159                 // remove a life
160                 float tl;
161                 tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
162                 if(tl < lms_lowest_lives)
163                         lms_lowest_lives = tl;
164                 if(tl <= 0)
165                 {
166                         if(!lms_next_place)
167                                 lms_next_place = player_count;
168                         PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
169                         --lms_next_place;
170                 }
171                 f = 0;
172         }
173         else if(g_ctf)
174         {
175                 if(g_ctf_ignore_frags)
176                         f = 0;
177         }
178
179         attacker.totalfrags += f;
180
181         if(f)
182                 UpdateFrags(attacker, f);
183 }
184
185 string AppendItemcodes(string s, entity player)
186 {
187         float w;
188         w = player.weapon;
189         //if(w == 0)
190         //      w = player.switchweapon;
191         if(w == 0)
192                 w = player.cnt; // previous weapon!
193         s = strcat(s, ftos(w));
194         if(time < player.strength_finished)
195                 s = strcat(s, "S");
196         if(time < player.invincible_finished)
197                 s = strcat(s, "I");
198         if(player.flagcarried != world)
199                 s = strcat(s, "F");
200         if(player.BUTTON_CHAT)
201                 s = strcat(s, "T");
202         if(player.kh_next)
203                 s = strcat(s, "K");
204         if(player.runes)
205                 s = strcat(s, "|", ftos(player.runes));
206         return s;
207 }
208
209 void LogDeath(string mode, float deathtype, entity killer, entity killed)
210 {
211         string s;
212         if(!cvar("sv_eventlog"))
213                 return;
214         s = strcat(":kill:", mode);
215         s = strcat(s, ":", ftos(killer.playerid));
216         s = strcat(s, ":", ftos(killed.playerid));
217         s = strcat(s, ":type=", ftos(deathtype));
218         s = strcat(s, ":items=");
219         s = AppendItemcodes(s, killer);
220         if(killed != killer)
221         {
222                 s = strcat(s, ":victimitems=");
223                 s = AppendItemcodes(s, killed);
224         }
225         GameLogEcho(s);
226 }
227
228 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
229 {
230         string  s, a;
231         float p, w;
232
233         if (targ.classname == "player" || targ.classname == "corpse")
234         {
235                 if (targ.classname == "corpse")
236                         s = "A corpse";
237                 else
238                         s = targ.netname;
239                 a = attacker.netname;
240
241                 if (targ == attacker)
242                 {
243                         if (deathtype == DEATH_TEAMCHANGE) {
244                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", ColoredTeamName(targ.team)));
245                         } else if (deathtype == DEATH_AUTOTEAMCHANGE) {
246                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(targ.team)));
247                                 return;
248                         } else if (deathtype == DEATH_CAMP) {
249                                 if(sv_gentle)
250                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
251                                 else
252                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
253                         } else if (deathtype == DEATH_NOAMMO) {
254                                 if(sv_gentle)
255                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
256                                 else
257                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
258                         } else if (deathtype == DEATH_ROT) {
259                                 if(sv_gentle)
260                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
261                                 else
262                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
263                         } else if (deathtype == DEATH_MIRRORDAMAGE) {
264                                 if(sv_gentle)
265                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
266                                 else
267                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
268                         } else {
269                                 if(sv_gentle)
270                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
271                                 else
272                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
273                         }
274
275                         if(sv_gentle) {
276                                 if (deathtype == DEATH_CAMP)
277                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
278                                 else if (deathtype == DEATH_MIRRORDAMAGE)
279                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
280                                 else
281                                         bprint ("^1",s, "^1 will be reinserted into the game due to his own actions\n");
282
283                                 if(deathtype != DEATH_TEAMCHANGE)
284                                 {
285                                         LogDeath("suicide", deathtype, targ, targ);
286                                         GiveFrags(attacker, targ, -1);
287                                 }
288                                 if (targ.killcount > 2)
289                                         bprint ("^1",s,"^1 faded after a ",ftos(targ.killcount)," point spree\n");
290                         } else {
291                                 w = DEATH_WEAPONOF(deathtype);
292                                 if(WEP_VALID(w))
293                                 {
294                                         w_deathtypestring = "couldn't resist the urge to self-destruct";
295                                         w_deathtype = deathtype;
296                                         weapon_action(w, WR_SUICIDEMESSAGE);
297                                         bprint("^1", s, "^1 ", w_deathtypestring, "\n");
298                                 }
299                                 else if (deathtype == DEATH_KILL)
300                                         bprint ("^1",s, "^1 couldn't take it anymore\n");
301                                 else if (deathtype == DEATH_ROT)
302                                         bprint ("^1",s, "^1 died\n");
303                                 else if (deathtype == DEATH_NOAMMO)
304                                         bprint ("^7",s, "^7 committed suicide. What's the point of living without ammo?\n");
305                                 else if (deathtype == DEATH_CAMP)
306                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
307                                 else if (deathtype == DEATH_MIRRORDAMAGE)
308                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
309                                 else if (deathtype == DEATH_CHEAT)
310                                         bprint ("^1",s, "^1 unfairly eliminated himself\n");
311                                 else if (deathtype == DEATH_FIRE)
312                                         bprint ("^1",s, "^1 burned to death\n");
313                                 else if (deathtype != DEATH_TEAMCHANGE)
314                                         bprint ("^1",s, "^1 couldn't resist the urge to self-destruct\n");
315
316                                 if(deathtype != DEATH_TEAMCHANGE)
317                                 {
318                                         LogDeath("suicide", deathtype, targ, targ);
319                                         GiveFrags(attacker, targ, -1);
320                                 }
321                                 if (targ.killcount > 2)
322                                         bprint ("^1",s,"^1 ended it all after a ",ftos(targ.killcount)," kill spree\n");
323                         }
324                 }
325                 else if (attacker.classname == "player" || attacker.classname == "gib")
326                 {
327                         if(teams_matter && attacker.team == targ.team)
328                         {
329                                 if(sv_gentle) {
330                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against a team mate!"));
331                                         bprint ("^1", a, "^1 took action against a team mate\n");
332                                 } else {
333                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s, ", a team mate!"));
334                                         bprint ("^1", a, "^1 mows down a team mate\n");
335                                 }
336                                 GiveFrags(attacker, targ, -1);
337                                 if (targ.killcount > 2) {
338                                         if(sv_gentle)
339                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," scoring spree was ended by a team mate!\n");
340                                         else
341                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," kill spree was ended by a team mate!\n");
342                                 }
343                                 if (attacker.killcount > 2) {
344                                         if(sv_gentle)
345                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," scoring spree by going against a team mate\n");
346                                         else
347                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," kill spree by killing a team mate\n");
348                                 }
349                                 attacker.killcount = 0;
350
351                                 LogDeath("tk", deathtype, attacker, targ);
352                         }
353                         else
354                         {
355                                 string blood_message, victim_message;
356                                 if (!checkrules_firstblood)
357                                 {
358                                         checkrules_firstblood = TRUE;
359                                         if(sv_gentle)
360                                         {
361                                                 bprint("^1",a, "^1 was the first to score", "\n");
362                                                 blood_message = "^1First point\n";
363                                                 //victim_message = "^1First victim\n";  // or First casualty
364                                         }
365                                         else
366                                         {
367                                                 bprint("^1",a, "^1 drew first blood", "\n");
368                                                 blood_message = "^1First blood\n";
369                                                 victim_message = "^1First victim\n";  // or First casualty
370                                         }
371                                 }
372
373                                 if(sv_gentle > 0) {
374                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s));
375                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, a,"^1 scored against you ^7"));
376                                 } else {
377                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You fragged ^7", s));
378                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were fragged by ^7", a));
379                                         attacker.taunt_soundtime = time + 1;
380                                 }
381
382                                 if(sv_gentle) {
383                                         bprint ("^1",s, "^1 needs a restart thanks to ", a, "\n");
384                                 } else {
385                                         w = DEATH_WEAPONOF(deathtype);
386                                         if(WEP_VALID(w))
387                                         {
388                                                 w_deathtypestring = "was blasted by";
389                                                 w_deathtype = deathtype;
390                                                 weapon_action(w, WR_KILLMESSAGE);
391                                                 p = strstrofs(w_deathtypestring, "#", 0);
392                                                 if(p < 0)
393                                                         bprint("^1", s, "^1 ", w_deathtypestring, " ", a, "\n");
394                                                 else
395                                                         bprint("^1", s, "^1 ", substring(w_deathtypestring, 0, p), a, "^1", substring(w_deathtypestring, p+1, strlen(w_deathtypestring) - (p+1)), "\n");
396                                         }
397                                         else if (deathtype == DEATH_TELEFRAG)
398                                                 bprint ("^1",s, "^1 was telefragged by ", a, "\n");
399                                         else if (deathtype == DEATH_DROWN)
400                                                 bprint ("^1",s, "^1 was drowned by ", a, "\n");
401                                         else if (deathtype == DEATH_SLIME)
402                                                 bprint ("^1",s, "^1 was slimed by ", a, "\n");
403                                         else if (deathtype == DEATH_LAVA)
404                                                 bprint ("^1",s, "^1 was cooked by ", a, "\n");
405                                         else if (deathtype == DEATH_FALL)
406                                                 bprint ("^1",s, "^1 was grounded by ", a, "\n");
407                                         else if (deathtype == DEATH_SHOOTING_STAR)
408                                                 bprint ("^1",s, "^1 was shot into space by ", a, "\n");
409                                         else if (deathtype == DEATH_SWAMP)
410                                                 bprint ("^1",s, "^1 was conserved by ", a, "\n");
411                                         else if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
412                                         {
413                                                 p = strstrofs(inflictor.message2, "#", 0);
414                                                 if(p < 0)
415                                                         bprint("^1", s, "^1 ", inflictor.message2, " ", a, "\n");
416                                                 else
417                                                         bprint("^1", s, "^1 ", substring(inflictor.message2, 0, p), a, "^1", substring(inflictor.message2, p+1, strlen(inflictor.message2) - (p+1)), "\n");
418                                         }
419                                         else if(deathtype == DEATH_TURRET)
420                                                 bprint ("^1",s, "^1 was pushed into the line of fire by ^1", a, "\n");
421                                         else if(deathtype == DEATH_TOUCHEXPLODE)
422                                                 bprint ("^1",s, "^1 was pushed into an accident by ^1", a, "\n");
423                                         else if(deathtype == DEATH_CHEAT)
424                                                 bprint ("^1",s, "^1 was unfairly eliminated by ^1", a, "\n");
425                                         else if (deathtype == DEATH_FIRE)
426                                                 bprint ("^1",s, "^1 was burnt to death by ^1", a, "\n");
427                                         else
428                                                 bprint ("^1",s, "^1 was fragged by ", a, "\n");
429                                 }
430
431                                 if(g_ctf && targ.flagcarried)
432                                 {
433                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
434                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
435                                         GiveFrags(attacker, targ, 0); // for logging
436                                 }
437                                 else
438                                         GiveFrags(attacker, targ, 1);
439
440                                 if (targ.killcount > 2) {
441                                         if(sv_gentle)
442                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " scoring spree was ended by ", a, "\n");
443                                         else
444                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
445                                 }
446
447                                 attacker.killcount = attacker.killcount + 1;
448
449                                 if (attacker.killcount > 2) {
450                                         if(sv_gentle)
451                                                 bprint ("^1",a,"^1 made ",ftos(attacker.killcount)," scores in a row\n");
452                                         else
453                                                 bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
454                                 }
455
456                                 LogDeath("frag", deathtype, attacker, targ);
457
458                                 if (attacker.killcount == 3)
459                                 {
460                                         if(sv_gentle) {
461                                                 bprint (a,"^7 made a ^1TRIPLE SCORE\n");
462                                         } else {
463                                                 bprint (a,"^7 made a ^1TRIPLE FRAG\n");
464                                                 announce(attacker, "announcer/male/03kills.wav");
465                                         }
466                                 }
467                                 else if (attacker.killcount == 5)
468                                 {
469                                         if(sv_gentle) {
470                                                 bprint (a,"^7 unleashes ^1SCORING RAGE\n");
471                                         } else {
472                                                 bprint (a,"^7 unleashes ^1RAGE\n");
473                                                 announce(attacker, "announcer/male/05kills.wav");
474                                         }
475                                 }
476                                 else if (attacker.killcount == 10)
477                                 {
478                                         if(sv_gentle) {
479                                                 bprint (a,"^7 made ^1TEN SCORES IN A ROW!\n");
480                                         } else {
481                                                 bprint (a,"^7 starts the ^1MASSACRE!\n");
482                                                 announce(attacker, "announcer/male/10kills.wav");
483                                         }
484                                 }
485                                 else if (attacker.killcount == 15)
486                                 {
487                                         if(sv_gentle) {
488                                                 bprint (a,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
489                                         } else {
490                                                 bprint (a,"^7 executes ^1MAYHEM!\n");
491                                                 announce(attacker, "announcer/male/15kills.wav");
492                                         }
493                                 }
494                                 else if (attacker.killcount == 20)
495                                 {
496                                         if(sv_gentle) {
497                                                 bprint (a,"^7 made ^1TWENTY SCORES IN A ROW!\n");
498                                         } else {
499                                                 bprint (a,"^7 is a ^1BERSERKER!\n");
500                                                 announce(attacker, "announcer/male/20kills.wav");
501                                         }
502                                 }
503                                 else if (attacker.killcount == 25)
504                                 {
505                                         if(sv_gentle) {
506                                                 bprint (a,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
507                                         } else {
508                                                 bprint (a,"^7 inflicts ^1CARNAGE!\n");
509                                                 announce(attacker, "announcer/male/25kills.wav");
510                                         }
511                                 }
512                                 else if (attacker.killcount == 30)
513                                 {
514                                         if(sv_gentle) {
515                                                 bprint (a,"^7 made ^1THIRTY SCORES IN A ROW!\n");
516                                         } else {
517                                                 bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
518                                                 announce(attacker, "announcer/male/30kills.wav");
519                                         }
520                                 }
521                         }
522                 }
523                 else
524                 {
525                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
526                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
527                                 bprint ("^1",s, "^1 ", inflictor.message, "\n");
528                         else if (deathtype == DEATH_DROWN)
529                                 if(sv_gentle)
530                                         bprint ("^1",s, "^1 was in the water for too long\n");
531                                 else
532                                         bprint ("^1",s, "^1 drowned\n");
533                         else if (deathtype == DEATH_SLIME)
534                                 bprint ("^1",s, "^1 was slimed\n");
535                         else if (deathtype == DEATH_LAVA)
536                                 if(sv_gentle)
537                                         bprint ("^1",s, "^1 found a hot place\n");
538                                 else
539                                         bprint ("^1",s, "^1 turned into hot slag\n");
540                         else if (deathtype == DEATH_FALL)
541                                 if(sv_gentle)
542                                         bprint ("^1",s, "^1 tested gravity (and it worked)\n");
543                                 else
544                                         bprint ("^1",s, "^1 hit the ground with a crunch\n");
545                         else if (deathtype == DEATH_SHOOTING_STAR)
546                                 bprint ("^1",s, "^1 became a shooting star\n");
547                         else if (deathtype == DEATH_SWAMP)
548                                 if(sv_gentle)
549                                         bprint ("^1",s, "^1 discovered a swamp\n");
550                                 else
551                                         bprint ("^1",s, "^1 is now conserved for centuries to come\n");
552                         else if(deathtype == DEATH_TURRET)
553                                 bprint ("^1",s, "^1 was mowed down by a turret \n");
554                         else if(deathtype == DEATH_TOUCHEXPLODE)
555                                 bprint ("^1",s, "^1 died in an accident\n");
556                         else if(deathtype == DEATH_CHEAT)
557                                 bprint ("^1",s, "^1 was unfairly eliminated\n");
558                         else if(deathtype == DEATH_FIRE)
559                                 if(sv_gentle)
560                                         bprint ("^1",s, "^1 felt a little hot\n");
561                                 else
562                                         bprint ("^1",s, "^1 burnt to death\n");
563                         else
564                                 if(sv_gentle)
565                                         bprint ("^1",s, "^1 needs a restart\n");
566                                 else
567                                         bprint ("^1",s, "^1 died\n");
568                         GiveFrags(targ, targ, -1);
569                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
570                                 announce(targ, "announcer/male/botlike.wav");
571                         }
572
573                         if (targ.killcount > 2)
574                                 if(sv_gentle)
575                                         bprint ("^1",s,"^1 needs a restart after a ",ftos(targ.killcount)," scoring spree\n");
576                                 else
577                                         bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
578
579                         LogDeath("accident", deathtype, targ, targ);
580                 }
581                 targ.death_origin = targ.origin;
582                 if(targ != attacker)
583                         targ.killer_origin = attacker.origin;
584                 // FIXME: this should go in PutClientInServer
585                 if (targ.killcount)
586                         targ.killcount = 0;
587         }
588 }
589
590 // these are updated by each Damage call for use in button triggering and such
591 entity damage_targ;
592 entity damage_inflictor;
593 entity damage_attacker;
594
595 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
596 {
597         float mirrordamage;
598         float mirrorforce;
599         float teamdamage0;
600         entity attacker_save;
601         mirrordamage = 0;
602         mirrorforce = 0;
603
604         if (gameover || targ.killcount == -666)
605                 return;
606
607         local entity oldself;
608         oldself = self;
609         self = targ;
610         damage_targ = targ;
611         damage_inflictor = inflictor;
612         damage_attacker = attacker;
613                 attacker_save = attacker;
614
615         if(targ.classname == "player")
616                 if(targ.hook)
617                         if(targ.hook.aiment)
618                                 if(targ.hook.aiment == attacker)
619                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
620
621         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
622         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
623         {
624                 if(targ.classname == "player")
625                         if not(IsDifferentTeam(targ, attacker))
626                         {
627                                 self = oldself;
628                                 return;
629                         }
630         }
631
632         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
633         {
634                 // These are ALWAYS lethal
635                 // No damage modification here
636                 // Instead, prepare the victim for his death...
637                 targ.armorvalue = 0;
638                 targ.spawnshieldtime = 0;
639                 targ.health = 0.9; // this is < 1
640                 targ.flags -= targ.flags & FL_GODMODE;
641                 damage = 100000;
642         }
643         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
644         {
645                 // no processing
646         }
647         else
648         {
649                 if (targ.classname == "player")
650                 if (attacker.classname == "player")
651                 if (!targ.isbot)
652                 if (attacker.isbot)
653                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
654
655                 // nullify damage if teamplay is on
656                 if(deathtype != DEATH_TELEFRAG)
657                 if(attacker.classname == "player")
658                 {
659                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
660                         {
661                                 damage = 0;
662                                 force = '0 0 0';
663                         }
664                         else if(attacker.team == targ.team)
665                         {
666                                 if(teamplay == 1)
667                                         damage = 0;
668                                 else if(attacker != targ)
669                                 {
670                                         if(teamplay == 3)
671                                                 damage = 0;
672                                         else if(teamplay == 4)
673                                         {
674                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
675                                                 {
676                                                         teamdamage0 = max(attacker.dmg_team, cvar("g_teamdamage_threshold"));
677                                                         attacker.dmg_team = attacker.dmg_team + damage;
678                                                         if(attacker.dmg_team > teamdamage0)
679                                                                 mirrordamage = cvar("g_mirrordamage") * (attacker.dmg_team - teamdamage0);
680                                                         mirrorforce = cvar("g_mirrordamage") * vlen(force);
681                                                         if(g_minstagib)
682                                                         {
683                                                                 if(cvar("g_friendlyfire") == 0)
684                                                                         damage = 0;
685                                                         }
686                                                         else
687                                                                 damage = cvar("g_friendlyfire") * damage;
688                                                         // mirrordamage will be used LATER
689                                                 }
690                                                 else
691                                                         damage = 0;
692                                         }
693                                 }
694                         }
695                 }
696
697                 if(targ.classname == "player")
698                 if(attacker.classname == "player")
699                 if(attacker != targ)
700                 {
701                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
702                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
703                 }
704
705                 if(targ.classname == "player")
706                 if (g_minstagib)
707                 {
708                         if ((deathtype == DEATH_FALL)  ||
709                                 (deathtype == DEATH_DROWN) ||
710                                 (deathtype == DEATH_SLIME) ||
711                                 (deathtype == DEATH_LAVA))
712                         {
713                                 self = oldself;
714                                 return;
715                         }
716                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
717                         {
718                                 targ.armorvalue -= 1;
719                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(targ.armorvalue)));
720                                 damage = 0;
721                                 targ.hitsound += 1;
722                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
723                         }
724                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
725                         {
726                                 damage = 0;
727                                 if (targ != attacker)
728                                 {
729                                         if (targ.classname == "player")
730                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "Secondary fire inflicts no damage!"));
731                                         damage = 0;
732                                         mirrordamage = 0;
733                                         force = '0 0 0';
734                                         // keep mirrorforce
735                                         attacker = targ;
736                                 }
737                         }
738                 }
739
740                 if not(DEATH_ISSPECIAL(deathtype))
741                 {
742                         damage *= g_weapondamagefactor;
743                         mirrordamage *= g_weapondamagefactor;
744                         force = force * g_weaponforcefactor;
745                         mirrorforce *= g_weaponforcefactor;
746                 }
747
748                 // apply strength multiplier
749                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
750                 {
751                         if(targ == attacker)
752                         {
753                                 damage = damage * cvar("g_balance_powerup_strength_selfdamage");
754                                 force = force * cvar("g_balance_powerup_strength_selfforce");
755                         }
756                         else
757                         {
758                                 damage = damage * cvar("g_balance_powerup_strength_damage");
759                                 force = force * cvar("g_balance_powerup_strength_force");
760                         }
761                 }
762
763                 // apply invincibility multiplier
764                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
765                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
766
767                 if (targ == attacker)
768                         damage = damage * cvar("g_balance_selfdamagepercent");  // Partial damage if the attacker hits himself
769
770                 // CTF: reduce damage/force
771                 if(g_ctf)
772                 if(targ == attacker)
773                 if(targ.flagcarried)
774                 {
775                         damage = damage * cvar("g_ctf_flagcarrier_selfdamage");
776                         force = force * cvar("g_ctf_flagcarrier_selfforce");
777                 }
778
779                 if(g_runematch)
780                 {
781                         // apply strength rune
782                         if (attacker.runes & RUNE_STRENGTH)
783                         {
784                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
785                                 {
786                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
787                                         force = force * cvar("g_balance_rune_strength_combo_force");
788                                 }
789                                 else
790                                 {
791                                         damage = damage * cvar("g_balance_rune_strength_damage");
792                                         force = force * cvar("g_balance_rune_strength_force");
793                                 }
794                         }
795                         else if (attacker.runes & CURSE_WEAK)
796                         {
797                                 damage = damage * cvar("g_balance_curse_weak_damage");
798                                 force = force * cvar("g_balance_curse_weak_force");
799                         }
800
801                         // apply defense rune
802                         if (targ.runes & RUNE_DEFENSE)
803                         {
804                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
805                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
806                                 else
807                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
808                         }
809                         else if (targ.runes & CURSE_VULNER)
810                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
811                 }
812
813                 // count the damage
814                 if(attacker)
815                 if(!targ.deadflag)
816                 if(targ.takedamage == DAMAGE_AIM)
817                 if(targ != attacker)
818                 if(targ.classname == "player")
819                 {
820                         if(IsDifferentTeam(targ, attacker))
821                         {
822                                 if(damage > 0)
823                                 {
824                                         if(targ.BUTTON_CHAT)
825                                                 attacker.typehitsound += 1;
826                                         else
827                                                 attacker.hitsound += 1;
828
829                                         damage_goodhits += 1;
830                                         damage_gooddamage += damage;
831
832                                         if not(DEATH_ISSPECIAL(deathtype))
833                                         {
834                                                 if(!g_minstagib)
835                                                 if(IsFlying(targ))
836                                                         yoda = 1;
837
838                                                 if(g_minstagib)
839                                                 if(targ.items & IT_STRENGTH)
840                                                         yoda = 1;
841
842                                                 // HEAD SHOT:
843                                                 // find height of hit on player axis
844                                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
845                                                 vector headmins, headmaxs, org;
846                                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
847                                                 headmins = org + '0.7 0 0' * targ.mins_x + '0 0.7 0' * targ.mins_y + '0 0 1' * (1.5 * targ.view_ofs_z - 0.5 * targ.maxs_z);
848                                                 headmaxs = org + '0.7 0 0' * targ.maxs_x + '0 0.7 0' * targ.maxs_y + '0 0 1' * targ.maxs_z;
849                                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
850                                                 {
851                                                         damage *= 1 + damage_headshotbonus;
852                                                         headshot = 1;
853                                                         deathtype |= HITTYPE_HEADSHOT;
854                                                 }
855                                         }
856                                 }
857                         }
858                         else
859                         {
860                                 if(deathtype != DEATH_FIRE)
861                                         attacker.typehitsound += 1;
862                                 if(mirrordamage > 0)
863                                         if(time > attacker.teamkill_complain)
864                                         {
865                                                 attacker.teamkill_complain = time + 5;
866                                                 attacker.teamkill_soundtime = time + 0.4;
867                                                 attacker.teamkill_soundsource = targ;
868                                         }
869                         }
870                 }
871         }
872
873         // apply push
874         if (self.damageforcescale)
875         if (vlen(force))
876         {
877                 self.velocity = self.velocity + self.damageforcescale * force;
878                 self.flags &~= FL_ONGROUND;
879                 UpdateCSQCProjectile(self);
880         }
881         // apply damage
882         if (damage != 0 || (self.damageforcescale && vlen(force)))
883         if (self.event_damage)
884                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
885         self = oldself;
886
887         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
888         {
889                 // Savage: vampire mode
890                 if (g_vampire)
891                 if (!g_minstagib)
892                 if (time > self.spawnshieldtime)
893                 {
894                         attacker.health += damage;
895                 }
896                 if(g_runematch)
897                 {
898                         if (attacker.runes & RUNE_VAMPIRE)
899                         {
900                         // apply vampire rune
901                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
902                                 {
903                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
904                                         attacker.health = bound(
905                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
906                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
907                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
908                                 }
909                                 else
910                                 {
911                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
912                                         attacker.health = bound(
913                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
914                                                                                         // empathy won't let you gain health in the same way...
915                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
916                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
917                                         }
918                         }
919                         // apply empathy curse
920                         else if (attacker.runes & CURSE_EMPATHY)
921                         {
922                                 attacker.health = bound(
923                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
924                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
925                                         attacker.health);
926                         }
927                 }
928         }
929
930         // apply mirror damage if any
931         if(mirrordamage > 0 || mirrorforce > 0)
932         {
933                 attacker = attacker_save;
934                 if(g_minstagib)
935                         if(mirrordamage > 0)
936                         {
937                                 // just lose extra LIVES, don't kill the player for mirror damage
938                                 if(attacker.armorvalue > 0)
939                                 {
940                                         attacker.armorvalue = attacker.armorvalue - 1;
941                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
942                                         attacker.hitsound += 1;
943                                 }
944                                 mirrordamage = 0;
945                         }
946                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
947                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
948         }
949 }
950
951 vector NearestPointOnBox(entity box, vector org)
952 {
953         vector m1, m2, nearest;
954
955         m1 = box.mins + box.origin;
956         m2 = box.maxs + box.origin;
957
958         nearest_x = bound(m1_x, org_x, m2_x);
959         nearest_y = bound(m1_y, org_y, m2_y);
960         nearest_z = bound(m1_z, org_z, m2_z);
961
962         return nearest;
963 }
964
965 .float actual_damage[WEP_COUNT]; //amount of damage done
966 .float max_damage[WEP_COUNT]; //the maximum damage of the weapon
967
968 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(actual_damage);
969 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(max_damage);
970
971 void Damage_RecordDamage(entity attacker, float deathtype, float damage)
972 {
973         float weaponid;
974         weaponid = DEATH_WEAPONOF(deathtype);
975         if not(inWarmupStage)
976         if(weaponid)
977         if(clienttype(attacker) == CLIENTTYPE_REAL)
978         {
979                 // Track damage done and update the stat to be sent later in g_world.qc
980                 attacker.actual_damage[weaponid] += damage;
981                 attacker.damage_hits = weaponid + 64 * rint(attacker.actual_damage[weaponid]);
982         }
983 }
984
985 float RadiusDamage_running;
986 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
987 // Returns total damage applies to creatures
988 {
989         entity  targ;
990         float   finaldmg;
991         float   power;
992         vector  blastorigin;
993         vector  force;
994         vector  diff;
995         vector  center;
996         vector  nearest;
997         float   total_damage_to_creatures;
998         entity  next;
999         float   tfloordmg;
1000         float   tfloorforce;
1001
1002         float stat_damagedone;
1003         float stat_maxdamage;
1004
1005         if(RadiusDamage_running)
1006         {
1007                 string save;
1008                 print("RadiusDamage called recursively!\n");
1009                 print("Expect stuff to go HORRIBLY wrong.\n");
1010                 print("Causing a stack trace...\n");
1011                 save = cvar_string("prvm_backtraceforwarnings");
1012                 cvar_set("prvm_backtraceforwarnings", "1");
1013                 fclose(-1); // calls VM_Warning
1014                 cvar_set("prvm_backtraceforwarnings", save);
1015                 return 0;
1016         }
1017
1018         RadiusDamage_running = 1;
1019
1020         tfloordmg = cvar("g_throughfloor_damage");
1021         tfloorforce = cvar("g_throughfloor_force");
1022
1023         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1024         total_damage_to_creatures = 0;
1025
1026         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1027         {
1028                 force = inflictor.velocity;
1029                 if(vlen(force) == 0)
1030                         force = '0 0 -1';
1031                 else
1032                         force = normalize(force);
1033                 if(forceintensity >= 0)
1034                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, attacker);
1035                 else
1036                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, attacker);
1037         }
1038
1039         stat_damagedone = 0;
1040         stat_maxdamage = 0;
1041
1042         targ = findradius (blastorigin, rad);
1043         while (targ)
1044         {
1045                 next = targ.chain;
1046                 if (targ != inflictor)
1047                         if (ignore != targ)
1048                         {
1049                                 // LordHavoc: measure distance to nearest point on target (not origin)
1050                                 // (this guarentees 100% damage on a touch impact)
1051                                 nearest = NearestPointOnBox(targ, blastorigin);
1052                                 diff = nearest - blastorigin;
1053                                 // round up a little on the damage to ensure full damage on impacts
1054                                 // and turn the distance into a fraction of the radius
1055                                 power = 1 - ((vlen (diff) - 2) / rad);
1056                                 //bprint(" ");
1057                                 //bprint(ftos(power));
1058                                 //if (targ == attacker)
1059                                 //      print(ftos(power), "\n");
1060                                 if (power > 0)
1061                                 {
1062                                         if (power > 1)
1063                                                 power = 1;
1064                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1065                                         if (finaldmg > 0)
1066                                         {
1067                                                 local float a;
1068                                                 local float c;
1069                                                 local float hits;
1070                                                 local float total;
1071                                                 local float hitratio;
1072                                                 local vector hitloc;
1073                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1074                                                 // if it's a player, use the view origin as reference
1075                                                 if (targ.classname == "player")
1076                                                         center = targ.origin + targ.view_ofs;
1077                                                 force = normalize(center - blastorigin);
1078                                                 force = force * (finaldmg / coredamage) * forceintensity;
1079                                                 // test line of sight to multiple positions on box,
1080                                                 // and do damage if any of them hit
1081                                                 hits = 0;
1082                                                 total = ceil(bound(1, finaldmg*0.2, 20));
1083                                                 hitloc = nearest;
1084                                                 c = 0;
1085                                                 while (c < total)
1086                                                 {
1087                                                         traceline(blastorigin, nearest, MOVE_NOMONSTERS, inflictor);
1088                                                         if (trace_fraction == 1 || trace_ent == targ)
1089                                                         {
1090                                                                 hits = hits + 1;
1091                                                                 if (hits > 1)
1092                                                                         hitloc = hitloc + nearest;
1093                                                                 else
1094                                                                         hitloc = nearest;
1095                                                         }
1096                                                         nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1097                                                         nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1098                                                         nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1099                                                         c = c + 1;
1100                                                 }
1101                                                 nearest = hitloc * (1 / max(1, hits));
1102                                                 hitratio = (hits / total);
1103                                                 a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1104                                                 finaldmg = finaldmg * a;
1105                                                 a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1106                                                 force = force * a;
1107                                                 //if (targ == attacker)
1108                                                 //{
1109                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1110                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1111                                                 //      print(" (", ftos(a), ")\n");
1112                                                 //}
1113                                                 if(hits || tfloordmg || tfloorforce)
1114                                                 {
1115                                                         if(targ.iscreature)
1116                                                         {
1117                                                                 total_damage_to_creatures += finaldmg;
1118
1119                                                                 if(targ.flags & FL_CLIENT)
1120                                                                 if(targ.deadflag == DEAD_NO)
1121                                                                 if(targ != attacker)
1122                                                                 if(!teamplay || targ.team != attacker.team)
1123                                                                 {
1124                                                                         stat_damagedone += finaldmg;
1125                                                                         stat_maxdamage += coredamage;
1126                                                                 }
1127                                                         }
1128
1129                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1130                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1131                                                         else
1132                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1133                                                 }
1134                                         }
1135                                 }
1136                         }
1137                 targ = next;
1138         }
1139
1140         RadiusDamage_running = 0;
1141
1142         Damage_RecordDamage(attacker, deathtype, min(stat_maxdamage, stat_damagedone));
1143
1144         return total_damage_to_creatures;
1145 }
1146
1147 .float fire_damagepersec;
1148 .float fire_endtime;
1149 .float fire_deathtype;
1150 .entity fire_owner;
1151 .float fire_hitsound;
1152 .entity fire_burner;
1153
1154 void fireburner_think();
1155
1156 float Fire_IsBurning(entity e)
1157 {
1158         return (time < e.fire_endtime);
1159 }
1160
1161 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1162 {
1163         float dps;
1164         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1165
1166         if(e.classname == "player")
1167         {
1168                 if(e.deadflag)
1169                         return -1;
1170         }
1171         else
1172         {
1173                 if(!e.fire_burner)
1174                 {
1175                         // print("adding a fire burner to ", e.classname, "\n");
1176                         e.fire_burner = spawn();
1177                         e.fire_burner.classname = "fireburner";
1178                         e.fire_burner.think = fireburner_think;
1179                         e.fire_burner.nextthink = time;
1180                         e.fire_burner.owner = e;
1181                 }
1182         }
1183
1184         t = max(t, 0.1);
1185         dps = d / t;
1186         if(Fire_IsBurning(e))
1187         {
1188                 mintime = e.fire_endtime - time;
1189                 maxtime = max(mintime, t);
1190
1191                 mindps = e.fire_damagepersec;
1192                 maxdps = max(mindps, dps);
1193
1194                 if(maxtime > mintime || maxdps > mindps)
1195                 {
1196                         mindamage = mindps * mintime;
1197                         maxdamage = mindamage + d;
1198
1199                         // interval [mintime, maxtime] * [mindps, maxdps]
1200                         // intersected with
1201                         // [mindamage, maxdamage]
1202                         // maximum of this!
1203
1204                         if(maxdamage >= maxtime * maxdps)
1205                         {
1206                                 totaltime = maxtime;
1207                                 totaldamage = maxtime * maxdps;
1208
1209                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1210                         }
1211                         else
1212                         {
1213                                 // maxdamage is inside the interval!
1214                                 // first, try to use mindps; only if this fails, increase dps as needed
1215                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1216                                 totaldamage = maxdamage;
1217                                 // can totaldamage / totaltime be >= maxdps?
1218                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1219                                 // we know maxdamage < maxtime * maxdps
1220                                 // so it cannot be
1221
1222                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1223                         }
1224
1225                         // total conditions for increasing:
1226                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1227                         // however:
1228                         //     if maxtime = mintime, maxdps = mindps
1229                         // then:
1230                         //     maxdamage = mindamage + d
1231                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1232                         // so the last condition is not needed
1233
1234                         e.fire_damagepersec = totaldamage / totaltime;
1235                         e.fire_endtime = time + totaltime;
1236                         if(totaldamage > 1.2 * mindamage)
1237                         {
1238                                 e.fire_deathtype = dt;
1239                                 if(e.fire_owner != o)
1240                                 {
1241                                         e.fire_owner = o;
1242                                         e.fire_hitsound = FALSE;
1243                                 }
1244                         }
1245                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1246                 }
1247                 else
1248                         return 0;
1249         }
1250         else
1251         {
1252                 e.fire_damagepersec = dps;
1253                 e.fire_endtime = time + t;
1254                 e.fire_deathtype = dt;
1255                 e.fire_owner = o;
1256                 e.fire_hitsound = FALSE;
1257                 return d;
1258         }
1259 }
1260
1261 void Fire_ApplyDamage(entity e)
1262 {
1263         float t, d, hi, ty;
1264
1265         if not(Fire_IsBurning(e))
1266                 return;
1267
1268         // water and slime stop fire
1269         if(e.waterlevel)
1270         if(e.watertype != CONTENT_LAVA)
1271                 e.fire_endtime = 0;
1272
1273         t = min(frametime, e.fire_endtime - time);
1274         d = e.fire_damagepersec * t;
1275
1276         hi = e.fire_owner.hitsound;
1277         ty = e.fire_owner.typehitsound;
1278         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1279         if(e.fire_hitsound && e.fire_owner)
1280         {
1281                 e.fire_owner.hitsound = hi;
1282                 e.fire_owner.typehitsound = ty;
1283         }
1284         e.fire_hitsound = TRUE;
1285
1286         Damage_RecordDamage(e.fire_owner, e.fire_deathtype, d);
1287
1288         if not(IS_INDEPENDENT_PLAYER(e))
1289         FOR_EACH_PLAYER(other) if(e != other)
1290         {
1291                 if(other.classname == "player")
1292                 if(other.deadflag == DEAD_NO)
1293                 if not(IS_INDEPENDENT_PLAYER(other))
1294                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1295                 {
1296                         t = cvar("g_balance_firetransfer_time") * (e.fire_endtime - time);
1297                         d = cvar("g_balance_firetransfer_damage") * e.fire_damagepersec * t;
1298                         Fire_AddDamage(other, e, d, t, DEATH_FIRE);
1299                 }
1300         }
1301 }
1302
1303 void Fire_ApplyEffect(entity e)
1304 {
1305         if(Fire_IsBurning(e))
1306                 e.effects |= EF_FLAME;
1307         else
1308                 e.effects &~= EF_FLAME;
1309 }
1310
1311 void fireburner_think()
1312 {
1313         // for players, this is done in the regular loop
1314         if(wasfreed(self.owner))
1315         {
1316                 remove(self);
1317                 return;
1318         }
1319         Fire_ApplyEffect(self.owner);
1320         if(!Fire_IsBurning(self.owner))
1321         {
1322                 self.owner.fire_burner = world;
1323                 remove(self);
1324                 return;
1325         }
1326         Fire_ApplyDamage(self.owner);
1327         self.nextthink = time;
1328 }