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