]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_damage.qc
make headshots work on turrets
[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                 {
819                         if(targ.classname == "player")
820                         {
821                                 // HEAD SHOT:
822                                 // find height of hit on player axis
823                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
824                                 vector headmins, headmaxs, org;
825                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
826                                 headmins = org + '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);
827                                 headmaxs = org + '0.7 0 0' * targ.maxs_x + '0 0.7 0' * targ.maxs_y + '0 0 1' * targ.maxs_z;
828                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
829                                 {
830                                         deathtype |= HITTYPE_HEADSHOT;
831                                 }
832                         }
833                         else if(targ.classname == "turret_head")
834                         {
835                                 deathtype |= HITTYPE_HEADSHOT;
836                         }
837                         if(deathtype & HITTYPE_HEADSHOT)
838                                 damage *= 1 + damage_headshotbonus;
839
840                         if(targ.classname == "player")
841                         {
842                                 if(IsDifferentTeam(targ, attacker))
843                                 {
844                                         if(damage > 0)
845                                         {
846                                                 if(targ.BUTTON_CHAT)
847                                                         attacker.typehitsound += 1;
848                                                 else
849                                                         attacker.hitsound += 1;
850
851                                                 damage_goodhits += 1;
852                                                 damage_gooddamage += damage;
853
854                                                 if not(DEATH_ISSPECIAL(deathtype))
855                                                 {
856                                                         if(!g_minstagib)
857                                                         if(IsFlying(targ))
858                                                                 yoda = 1;
859
860                                                         if(g_minstagib)
861                                                         if(targ.items & IT_STRENGTH)
862                                                                 yoda = 1;
863
864                                                         if(deathtype & HITTYPE_HEADSHOT)
865                                                                 headshot = 1;
866                                                 }
867                                         }
868                                 }
869                                 else
870                                 {
871                                         if(deathtype != DEATH_FIRE)
872                                                 attacker.typehitsound += 1;
873                                         if(mirrordamage > 0)
874                                                 if(time > attacker.teamkill_complain)
875                                                 {
876                                                         attacker.teamkill_complain = time + 5;
877                                                         attacker.teamkill_soundtime = time + 0.4;
878                                                         attacker.teamkill_soundsource = targ;
879                                                 }
880                                 }
881                         }
882                 }
883         }
884
885         // apply push
886         if (self.damageforcescale)
887         if (vlen(force))
888         {
889                 self.velocity = self.velocity + self.damageforcescale * force;
890                 self.flags &~= FL_ONGROUND;
891                 UpdateCSQCProjectile(self);
892         }
893         // apply damage
894         if (damage != 0 || (self.damageforcescale && vlen(force)))
895         if (self.event_damage)
896                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
897         self = oldself;
898
899         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
900         {
901                 // Savage: vampire mode
902                 if (g_vampire)
903                 if (!g_minstagib)
904                 if (time > self.spawnshieldtime)
905                 {
906                         attacker.health += damage;
907                 }
908                 if(g_runematch)
909                 {
910                         if (attacker.runes & RUNE_VAMPIRE)
911                         {
912                         // apply vampire rune
913                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
914                                 {
915                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
916                                         attacker.health = bound(
917                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
918                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
919                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
920                                 }
921                                 else
922                                 {
923                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
924                                         attacker.health = bound(
925                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
926                                                                                         // empathy won't let you gain health in the same way...
927                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
928                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
929                                         }
930                         }
931                         // apply empathy curse
932                         else if (attacker.runes & CURSE_EMPATHY)
933                         {
934                                 attacker.health = bound(
935                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
936                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
937                                         attacker.health);
938                         }
939                 }
940         }
941
942         // apply mirror damage if any
943         if(mirrordamage > 0 || mirrorforce > 0)
944         {
945                 attacker = attacker_save;
946                 if(g_minstagib)
947                         if(mirrordamage > 0)
948                         {
949                                 // just lose extra LIVES, don't kill the player for mirror damage
950                                 if(attacker.armorvalue > 0)
951                                 {
952                                         attacker.armorvalue = attacker.armorvalue - 1;
953                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
954                                         attacker.hitsound += 1;
955                                 }
956                                 mirrordamage = 0;
957                         }
958                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
959                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
960         }
961 }
962
963 vector NearestPointOnBox(entity box, vector org)
964 {
965         vector m1, m2, nearest;
966
967         m1 = box.mins + box.origin;
968         m2 = box.maxs + box.origin;
969
970         nearest_x = bound(m1_x, org_x, m2_x);
971         nearest_y = bound(m1_y, org_y, m2_y);
972         nearest_z = bound(m1_z, org_z, m2_z);
973
974         return nearest;
975 }
976
977 .float actual_damage[WEP_COUNT]; //amount of damage done
978 .float max_damage[WEP_COUNT]; //the maximum damage of the weapon
979
980 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(actual_damage);
981 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(max_damage);
982
983 void Damage_RecordDamage(entity attacker, float deathtype, float damage)
984 {
985         float weaponid;
986         weaponid = DEATH_WEAPONOF(deathtype);
987         if not(inWarmupStage)
988         if(weaponid)
989         if(clienttype(attacker) == CLIENTTYPE_REAL)
990         {
991                 // Track damage done and update the stat to be sent later in g_world.qc
992                 attacker.actual_damage[weaponid] += damage;
993                 attacker.damage_hits = weaponid + 64 * rint(attacker.actual_damage[weaponid]);
994         }
995 }
996
997 float RadiusDamage_running;
998 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
999 // Returns total damage applies to creatures
1000 {
1001         entity  targ;
1002         float   finaldmg;
1003         float   power;
1004         vector  blastorigin;
1005         vector  force;
1006         vector  diff;
1007         vector  center;
1008         vector  nearest;
1009         float   total_damage_to_creatures;
1010         entity  next;
1011         float   tfloordmg;
1012         float   tfloorforce;
1013
1014         float stat_damagedone;
1015         float stat_maxdamage;
1016
1017         if(RadiusDamage_running)
1018         {
1019                 string save;
1020                 print("RadiusDamage called recursively!\n");
1021                 print("Expect stuff to go HORRIBLY wrong.\n");
1022                 print("Causing a stack trace...\n");
1023                 save = cvar_string("prvm_backtraceforwarnings");
1024                 cvar_set("prvm_backtraceforwarnings", "1");
1025                 fclose(-1); // calls VM_Warning
1026                 cvar_set("prvm_backtraceforwarnings", save);
1027                 return 0;
1028         }
1029
1030         RadiusDamage_running = 1;
1031
1032         tfloordmg = cvar("g_throughfloor_damage");
1033         tfloorforce = cvar("g_throughfloor_force");
1034
1035         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1036         total_damage_to_creatures = 0;
1037
1038         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1039         {
1040                 force = inflictor.velocity;
1041                 if(vlen(force) == 0)
1042                         force = '0 0 -1';
1043                 else
1044                         force = normalize(force);
1045                 if(forceintensity >= 0)
1046                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, attacker);
1047                 else
1048                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, attacker);
1049         }
1050
1051         stat_damagedone = 0;
1052         stat_maxdamage = 0;
1053
1054         targ = findradius (blastorigin, rad);
1055         while (targ)
1056         {
1057                 next = targ.chain;
1058                 if (targ != inflictor)
1059                         if (ignore != targ) if(targ.takedamage)
1060                         {
1061                                 // LordHavoc: measure distance to nearest point on target (not origin)
1062                                 // (this guarentees 100% damage on a touch impact)
1063                                 nearest = NearestPointOnBox(targ, blastorigin);
1064                                 diff = nearest - blastorigin;
1065                                 // round up a little on the damage to ensure full damage on impacts
1066                                 // and turn the distance into a fraction of the radius
1067                                 power = 1 - ((vlen (diff) - 2) / rad);
1068                                 //bprint(" ");
1069                                 //bprint(ftos(power));
1070                                 //if (targ == attacker)
1071                                 //      print(ftos(power), "\n");
1072                                 if (power > 0)
1073                                 {
1074                                         if (power > 1)
1075                                                 power = 1;
1076                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1077                                         if (finaldmg > 0)
1078                                         {
1079                                                 local float a;
1080                                                 local float c;
1081                                                 local float hits;
1082                                                 local float total;
1083                                                 local float hitratio;
1084                                                 local vector hitloc;
1085                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1086                                                 // if it's a player, use the view origin as reference
1087                                                 if (targ.classname == "player")
1088                                                         center = targ.origin + targ.view_ofs;
1089                                                 force = normalize(center - blastorigin);
1090                                                 force = force * (finaldmg / coredamage) * forceintensity;
1091                                                 // test line of sight to multiple positions on box,
1092                                                 // and do damage if any of them hit
1093                                                 hits = 0;
1094                                                 if (targ.classname == "player")
1095                                                         total = ceil(bound(1, finaldmg, 50));
1096                                                 else
1097                                                         total = ceil(bound(1, finaldmg/10, 5));
1098                                                 hitloc = nearest;
1099                                                 c = 0;
1100                                                 while (c < total)
1101                                                 {
1102                                                         traceline(blastorigin, nearest, MOVE_NOMONSTERS, inflictor);
1103                                                         if (trace_fraction == 1 || trace_ent == targ)
1104                                                         {
1105                                                                 hits = hits + 1;
1106                                                                 if (hits > 1)
1107                                                                         hitloc = hitloc + nearest;
1108                                                                 else
1109                                                                         hitloc = nearest;
1110                                                         }
1111                                                         nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1112                                                         nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1113                                                         nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1114                                                         c = c + 1;
1115                                                 }
1116                                                 nearest = hitloc * (1 / max(1, hits));
1117                                                 hitratio = (hits / total);
1118                                                 a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1119                                                 finaldmg = finaldmg * a;
1120                                                 a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1121                                                 force = force * a;
1122                                                 //if (targ == attacker)
1123                                                 //{
1124                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1125                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1126                                                 //      print(" (", ftos(a), ")\n");
1127                                                 //}
1128                                                 if(hits || tfloordmg || tfloorforce)
1129                                                 {
1130                                                         if(targ.iscreature)
1131                                                         {
1132                                                                 total_damage_to_creatures += finaldmg;
1133
1134                                                                 if(targ.flags & FL_CLIENT)
1135                                                                 if(targ.deadflag == DEAD_NO)
1136                                                                 if(targ != attacker)
1137                                                                 if(!teamplay || targ.team != attacker.team)
1138                                                                 {
1139                                                                         stat_damagedone += finaldmg;
1140                                                                         stat_maxdamage += coredamage;
1141                                                                 }
1142                                                         }
1143
1144                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1145                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1146                                                         else
1147                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1148                                                 }
1149                                         }
1150                                 }
1151                         }
1152                 targ = next;
1153         }
1154
1155         RadiusDamage_running = 0;
1156
1157         Damage_RecordDamage(attacker, deathtype, min(stat_maxdamage, stat_damagedone));
1158
1159         return total_damage_to_creatures;
1160 }
1161
1162 .float fire_damagepersec;
1163 .float fire_endtime;
1164 .float fire_deathtype;
1165 .entity fire_owner;
1166 .float fire_hitsound;
1167 .entity fire_burner;
1168
1169 void fireburner_think();
1170
1171 float Fire_IsBurning(entity e)
1172 {
1173         return (time < e.fire_endtime);
1174 }
1175
1176 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1177 {
1178         float dps;
1179         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1180
1181         if(e.classname == "player")
1182         {
1183                 if(e.deadflag)
1184                         return -1;
1185         }
1186         else
1187         {
1188                 if(!e.fire_burner)
1189                 {
1190                         // print("adding a fire burner to ", e.classname, "\n");
1191                         e.fire_burner = spawn();
1192                         e.fire_burner.classname = "fireburner";
1193                         e.fire_burner.think = fireburner_think;
1194                         e.fire_burner.nextthink = time;
1195                         e.fire_burner.owner = e;
1196                 }
1197         }
1198
1199         t = max(t, 0.1);
1200         dps = d / t;
1201         if(Fire_IsBurning(e))
1202         {
1203                 mintime = e.fire_endtime - time;
1204                 maxtime = max(mintime, t);
1205
1206                 mindps = e.fire_damagepersec;
1207                 maxdps = max(mindps, dps);
1208
1209                 if(maxtime > mintime || maxdps > mindps)
1210                 {
1211                         mindamage = mindps * mintime;
1212                         maxdamage = mindamage + d;
1213
1214                         // interval [mintime, maxtime] * [mindps, maxdps]
1215                         // intersected with
1216                         // [mindamage, maxdamage]
1217                         // maximum of this!
1218
1219                         if(maxdamage >= maxtime * maxdps)
1220                         {
1221                                 totaltime = maxtime;
1222                                 totaldamage = maxtime * maxdps;
1223
1224                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1225                         }
1226                         else
1227                         {
1228                                 // maxdamage is inside the interval!
1229                                 // first, try to use mindps; only if this fails, increase dps as needed
1230                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1231                                 totaldamage = maxdamage;
1232                                 // can totaldamage / totaltime be >= maxdps?
1233                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1234                                 // we know maxdamage < maxtime * maxdps
1235                                 // so it cannot be
1236
1237                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1238                         }
1239
1240                         // total conditions for increasing:
1241                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1242                         // however:
1243                         //     if maxtime = mintime, maxdps = mindps
1244                         // then:
1245                         //     maxdamage = mindamage + d
1246                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1247                         // so the last condition is not needed
1248
1249                         e.fire_damagepersec = totaldamage / totaltime;
1250                         e.fire_endtime = time + totaltime;
1251                         if(totaldamage > 1.2 * mindamage)
1252                         {
1253                                 e.fire_deathtype = dt;
1254                                 if(e.fire_owner != o)
1255                                 {
1256                                         e.fire_owner = o;
1257                                         e.fire_hitsound = FALSE;
1258                                 }
1259                         }
1260                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1261                 }
1262                 else
1263                         return 0;
1264         }
1265         else
1266         {
1267                 e.fire_damagepersec = dps;
1268                 e.fire_endtime = time + t;
1269                 e.fire_deathtype = dt;
1270                 e.fire_owner = o;
1271                 e.fire_hitsound = FALSE;
1272                 return d;
1273         }
1274 }
1275
1276 void Fire_ApplyDamage(entity e)
1277 {
1278         float t, d, hi, ty;
1279
1280         if not(Fire_IsBurning(e))
1281                 return;
1282
1283         // water and slime stop fire
1284         if(e.waterlevel)
1285         if(e.watertype != CONTENT_LAVA)
1286                 e.fire_endtime = 0;
1287
1288         t = min(frametime, e.fire_endtime - time);
1289         d = e.fire_damagepersec * t;
1290
1291         hi = e.fire_owner.hitsound;
1292         ty = e.fire_owner.typehitsound;
1293         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1294         if(e.fire_hitsound && e.fire_owner)
1295         {
1296                 e.fire_owner.hitsound = hi;
1297                 e.fire_owner.typehitsound = ty;
1298         }
1299         e.fire_hitsound = TRUE;
1300
1301         Damage_RecordDamage(e.fire_owner, e.fire_deathtype, d);
1302
1303         if not(IS_INDEPENDENT_PLAYER(e))
1304         FOR_EACH_PLAYER(other) if(e != other)
1305         {
1306                 if(other.classname == "player")
1307                 if(other.deadflag == DEAD_NO)
1308                 if not(IS_INDEPENDENT_PLAYER(other))
1309                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1310                 {
1311                         t = cvar("g_balance_firetransfer_time") * (e.fire_endtime - time);
1312                         d = cvar("g_balance_firetransfer_damage") * e.fire_damagepersec * t;
1313                         Fire_AddDamage(other, e, d, t, DEATH_FIRE);
1314                 }
1315         }
1316 }
1317
1318 void Fire_ApplyEffect(entity e)
1319 {
1320         if(Fire_IsBurning(e))
1321                 e.effects |= EF_FLAME;
1322         else
1323                 e.effects &~= EF_FLAME;
1324 }
1325
1326 void fireburner_think()
1327 {
1328         // for players, this is done in the regular loop
1329         if(wasfreed(self.owner))
1330         {
1331                 remove(self);
1332                 return;
1333         }
1334         Fire_ApplyEffect(self.owner);
1335         if(!Fire_IsBurning(self.owner))
1336         {
1337                 self.owner.fire_burner = world;
1338                 remove(self);
1339                 return;
1340         }
1341         Fire_ApplyDamage(self.owner);
1342         self.nextthink = time;
1343 }