c1fb0c9a62463f35f3b9a0c202e04427f1448508
[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 they 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 their 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 they 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 themself\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                                 if(sv_gentle > 0) {
373                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You scored against ^7", s, GetAdvancedDeathReports(targ)));
374                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, a,"^1 scored against you ^7", GetAdvancedDeathReports(attacker)));
375                                 } else {
376                                         if((cvar("sv_fragmessage_information_typefrag")) && (targ.BUTTON_CHAT)) {
377                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^1You typefragged ^7", s, GetAdvancedDeathReports(targ)));
378                                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were typefragged by ^7", a, GetAdvancedDeathReports(attacker)));
379                                         } else { 
380                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You fragged ^7", s, GetAdvancedDeathReports(targ)));
381                                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were fragged by ^7", a, GetAdvancedDeathReports(attacker)));
382                                         }
383                                         attacker.taunt_soundtime = time + 1;
384                                 }
385
386                                 if(sv_gentle) {
387                                         bprint ("^1",s, "^1 needs a restart thanks to ", a, "\n");
388                                 } else {
389                                         w = DEATH_WEAPONOF(deathtype);
390                                         if(WEP_VALID(w))
391                                         {
392                                                 w_deathtypestring = "was blasted by";
393                                                 w_deathtype = deathtype;
394                                                 weapon_action(w, WR_KILLMESSAGE);
395                                                 p = strstrofs(w_deathtypestring, "#", 0);
396                                                 if(p < 0)
397                                                         bprint("^1", s, "^1 ", w_deathtypestring, " ", a, "\n");
398                                                 else
399                                                         bprint("^1", s, "^1 ", substring(w_deathtypestring, 0, p), a, "^1", substring(w_deathtypestring, p+1, strlen(w_deathtypestring) - (p+1)), "\n");
400                                         }
401                                         else if (deathtype == DEATH_TELEFRAG)
402                                                 bprint ("^1",s, "^1 was telefragged by ", a, "\n");
403                                         else if (deathtype == DEATH_DROWN)
404                                                 bprint ("^1",s, "^1 was drowned by ", a, "\n");
405                                         else if (deathtype == DEATH_SLIME)
406                                                 bprint ("^1",s, "^1 was slimed by ", a, "\n");
407                                         else if (deathtype == DEATH_LAVA)
408                                                 bprint ("^1",s, "^1 was cooked by ", a, "\n");
409                                         else if (deathtype == DEATH_FALL)
410                                                 bprint ("^1",s, "^1 was grounded by ", a, "\n");
411                                         else if (deathtype == DEATH_SHOOTING_STAR)
412                                                 bprint ("^1",s, "^1 was shot into space by ", a, "\n");
413                                         else if (deathtype == DEATH_SWAMP)
414                                                 bprint ("^1",s, "^1 was conserved by ", a, "\n");
415                                         else if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
416                                         {
417                                                 p = strstrofs(inflictor.message2, "#", 0);
418                                                 if(p < 0)
419                                                         bprint("^1", s, "^1 ", inflictor.message2, " ", a, "\n");
420                                                 else
421                                                         bprint("^1", s, "^1 ", substring(inflictor.message2, 0, p), a, "^1", substring(inflictor.message2, p+1, strlen(inflictor.message2) - (p+1)), "\n");
422                                         }
423                                         else if(deathtype == DEATH_TURRET)
424                                                 bprint ("^1",s, "^1 was pushed into the line of fire by ^1", a, "\n");
425                                         else if(deathtype == DEATH_TOUCHEXPLODE)
426                                                 bprint ("^1",s, "^1 was pushed into an accident by ^1", a, "\n");
427                                         else if(deathtype == DEATH_CHEAT)
428                                                 bprint ("^1",s, "^1 was unfairly eliminated by ^1", a, "\n");
429                                         else if (deathtype == DEATH_FIRE)
430                                                 bprint ("^1",s, "^1 was burnt to death by ^1", a, "\n");
431                                         else
432                                                 bprint ("^1",s, "^1 was fragged by ", a, "\n");
433                                 }
434
435                                 if(g_ctf && targ.flagcarried)
436                                 {
437                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
438                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
439                                         GiveFrags(attacker, targ, 0); // for logging
440                                 }
441                                 else
442                                         GiveFrags(attacker, targ, 1);
443
444                                 if (targ.killcount > 2) {
445                                         if(sv_gentle)
446                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " scoring spree was ended by ", a, "\n");
447                                         else
448                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
449                                 }
450
451                                 attacker.killcount = attacker.killcount + 1;
452
453                                 if (attacker.killcount > 2) {
454                                         if(sv_gentle)
455                                                 bprint ("^1",a,"^1 made ",ftos(attacker.killcount)," scores in a row\n");
456                                         else
457                                                 bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
458                                 }
459
460                                 LogDeath("frag", deathtype, attacker, targ);
461
462                                 if (attacker.killcount == 3)
463                                 {
464                                         if(sv_gentle) {
465                                                 bprint (a,"^7 made a ^1TRIPLE SCORE\n");
466                                         } else {
467                                                 bprint (a,"^7 made a ^1TRIPLE FRAG\n");
468                                                 announce(attacker, "announcer/male/03kills.wav");
469                                         }
470                                 }
471                                 else if (attacker.killcount == 5)
472                                 {
473                                         if(sv_gentle) {
474                                                 bprint (a,"^7 unleashes ^1SCORING RAGE\n");
475                                         } else {
476                                                 bprint (a,"^7 unleashes ^1RAGE\n");
477                                                 announce(attacker, "announcer/male/05kills.wav");
478                                         }
479                                 }
480                                 else if (attacker.killcount == 10)
481                                 {
482                                         if(sv_gentle) {
483                                                 bprint (a,"^7 made ^1TEN SCORES IN A ROW!\n");
484                                         } else {
485                                                 bprint (a,"^7 starts the ^1MASSACRE!\n");
486                                                 announce(attacker, "announcer/male/10kills.wav");
487                                         }
488                                 }
489                                 else if (attacker.killcount == 15)
490                                 {
491                                         if(sv_gentle) {
492                                                 bprint (a,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
493                                         } else {
494                                                 bprint (a,"^7 executes ^1MAYHEM!\n");
495                                                 announce(attacker, "announcer/male/15kills.wav");
496                                         }
497                                 }
498                                 else if (attacker.killcount == 20)
499                                 {
500                                         if(sv_gentle) {
501                                                 bprint (a,"^7 made ^1TWENTY SCORES IN A ROW!\n");
502                                         } else {
503                                                 bprint (a,"^7 is a ^1BERSERKER!\n");
504                                                 announce(attacker, "announcer/male/20kills.wav");
505                                         }
506                                 }
507                                 else if (attacker.killcount == 25)
508                                 {
509                                         if(sv_gentle) {
510                                                 bprint (a,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
511                                         } else {
512                                                 bprint (a,"^7 inflicts ^1CARNAGE!\n");
513                                                 announce(attacker, "announcer/male/25kills.wav");
514                                         }
515                                 }
516                                 else if (attacker.killcount == 30)
517                                 {
518                                         if(sv_gentle) {
519                                                 bprint (a,"^7 made ^1THIRTY SCORES IN A ROW!\n");
520                                         } else {
521                                                 bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
522                                                 announce(attacker, "announcer/male/30kills.wav");
523                                         }
524                                 }
525                         }
526                 }
527                 else
528                 {
529                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
530                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
531                                 bprint ("^1",s, "^1 ", inflictor.message, "\n");
532                         else if (deathtype == DEATH_DROWN)
533                                 if(sv_gentle)
534                                         bprint ("^1",s, "^1 was in the water for too long\n");
535                                 else
536                                         bprint ("^1",s, "^1 drowned\n");
537                         else if (deathtype == DEATH_SLIME)
538                                 bprint ("^1",s, "^1 was slimed\n");
539                         else if (deathtype == DEATH_LAVA)
540                                 if(sv_gentle)
541                                         bprint ("^1",s, "^1 found a hot place\n");
542                                 else
543                                         bprint ("^1",s, "^1 turned into hot slag\n");
544                         else if (deathtype == DEATH_FALL)
545                                 if(sv_gentle)
546                                         bprint ("^1",s, "^1 tested gravity (and it worked)\n");
547                                 else
548                                         bprint ("^1",s, "^1 hit the ground with a crunch\n");
549                         else if (deathtype == DEATH_SHOOTING_STAR)
550                                 bprint ("^1",s, "^1 became a shooting star\n");
551                         else if (deathtype == DEATH_SWAMP)
552                                 if(sv_gentle)
553                                         bprint ("^1",s, "^1 discovered a swamp\n");
554                                 else
555                                         bprint ("^1",s, "^1 is now conserved for centuries to come\n");
556                         else if(deathtype == DEATH_TURRET)
557                                 bprint ("^1",s, "^1 was mowed down by a turret \n");
558                         else if(deathtype == DEATH_TOUCHEXPLODE)
559                                 bprint ("^1",s, "^1 died in an accident\n");
560                         else if(deathtype == DEATH_CHEAT)
561                                 bprint ("^1",s, "^1 was unfairly eliminated\n");
562                         else if(deathtype == DEATH_FIRE)
563                                 if(sv_gentle)
564                                         bprint ("^1",s, "^1 felt a little hot\n");
565                                 else
566                                         bprint ("^1",s, "^1 burnt to death\n");
567                         else
568                                 if(sv_gentle)
569                                         bprint ("^1",s, "^1 needs a restart\n");
570                                 else
571                                         bprint ("^1",s, "^1 died\n");
572                         GiveFrags(targ, targ, -1);
573                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
574                                 announce(targ, "announcer/male/botlike.wav");
575                         }
576
577                         if (targ.killcount > 2)
578                                 if(sv_gentle)
579                                         bprint ("^1",s,"^1 needs a restart after a ",ftos(targ.killcount)," scoring spree\n");
580                                 else
581                                         bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
582
583                         LogDeath("accident", deathtype, targ, targ);
584                 }
585                 targ.death_origin = targ.origin;
586                 if(targ != attacker)
587                         targ.killer_origin = attacker.origin;
588                 // FIXME: this should go in PutClientInServer
589                 if (targ.killcount)
590                         targ.killcount = 0;
591         }
592 }
593
594 // these are updated by each Damage call for use in button triggering and such
595 entity damage_targ;
596 entity damage_inflictor;
597 entity damage_attacker;
598
599 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
600 {
601         float mirrordamage;
602         float mirrorforce;
603         float teamdamage0;
604         entity attacker_save;
605         mirrordamage = 0;
606         mirrorforce = 0;
607
608         if (gameover || targ.killcount == -666)
609                 return;
610
611         local entity oldself;
612         oldself = self;
613         self = targ;
614         damage_targ = targ;
615         damage_inflictor = inflictor;
616         damage_attacker = attacker;
617                 attacker_save = attacker;
618
619         if(targ.classname == "player")
620                 if(targ.hook)
621                         if(targ.hook.aiment)
622                                 if(targ.hook.aiment == attacker)
623                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
624
625         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
626         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
627         {
628                 if(targ.classname == "player")
629                         if not(IsDifferentTeam(targ, attacker))
630                         {
631                                 self = oldself;
632                                 return;
633                         }
634         }
635
636         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
637         {
638                 // These are ALWAYS lethal
639                 // No damage modification here
640                 // Instead, prepare the victim for his death...
641                 targ.armorvalue = 0;
642                 targ.spawnshieldtime = 0;
643                 targ.health = 0.9; // this is < 1
644                 targ.flags -= targ.flags & FL_GODMODE;
645                 damage = 100000;
646         }
647         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
648         {
649                 // no processing
650         }
651         else
652         {
653                 if (targ.classname == "player")
654                 if (attacker.classname == "player")
655                 if (!targ.isbot)
656                 if (attacker.isbot)
657                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
658
659                 // nullify damage if teamplay is on
660                 if(deathtype != DEATH_TELEFRAG)
661                 if(attacker.classname == "player")
662                 {
663                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
664                         {
665                                 damage = 0;
666                                 force = '0 0 0';
667                         }
668                         else if(attacker.team == targ.team)
669                         {
670                                 if(teamplay == 1)
671                                         damage = 0;
672                                 else if(attacker != targ)
673                                 {
674                                         if(teamplay == 3)
675                                                 damage = 0;
676                                         else if(teamplay == 4)
677                                         {
678                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
679                                                 {
680                                                         teamdamage0 = max(attacker.dmg_team, cvar("g_teamdamage_threshold"));
681                                                         attacker.dmg_team = attacker.dmg_team + damage;
682                                                         if(attacker.dmg_team > teamdamage0)
683                                                                 mirrordamage = cvar("g_mirrordamage") * (attacker.dmg_team - teamdamage0);
684                                                         mirrorforce = cvar("g_mirrordamage") * vlen(force);
685                                                         if(g_minstagib)
686                                                         {
687                                                                 if(cvar("g_friendlyfire") == 0)
688                                                                         damage = 0;
689                                                         }
690                                                         else
691                                                                 damage = cvar("g_friendlyfire") * damage;
692                                                         // mirrordamage will be used LATER
693                                                 }
694                                                 else
695                                                         damage = 0;
696                                         }
697                                 }
698                         }
699                 }
700
701                 if(targ.classname == "player")
702                 if(attacker.classname == "player")
703                 if(attacker != targ)
704                 {
705                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
706                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
707                 }
708
709                 if(targ.classname == "player")
710                 if (g_minstagib)
711                 {
712                         if ((deathtype == DEATH_FALL)  ||
713                                 (deathtype == DEATH_DROWN) ||
714                                 (deathtype == DEATH_SLIME) ||
715                                 (deathtype == DEATH_LAVA))
716                         {
717                                 self = oldself;
718                                 return;
719                         }
720                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
721                         {
722                                 targ.armorvalue -= 1;
723                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(targ.armorvalue)));
724                                 damage = 0;
725                                 targ.hitsound += 1;
726                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
727                         }
728                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
729                         {
730                                 damage = 0;
731                                 if (targ != attacker)
732                                 {
733                                         if (targ.classname == "player")
734                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "Secondary fire inflicts no damage!"));
735                                         damage = 0;
736                                         mirrordamage = 0;
737                                         force = '0 0 0';
738                                         // keep mirrorforce
739                                         attacker = targ;
740                                 }
741                         }
742                 }
743
744                 if not(DEATH_ISSPECIAL(deathtype))
745                 {
746                         damage *= g_weapondamagefactor;
747                         mirrordamage *= g_weapondamagefactor;
748                         force = force * g_weaponforcefactor;
749                         mirrorforce *= g_weaponforcefactor;
750                 }
751
752                 // apply strength multiplier
753                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
754                 {
755                         if(targ == attacker)
756                         {
757                                 damage = damage * cvar("g_balance_powerup_strength_selfdamage");
758                                 force = force * cvar("g_balance_powerup_strength_selfforce");
759                         }
760                         else
761                         {
762                                 damage = damage * cvar("g_balance_powerup_strength_damage");
763                                 force = force * cvar("g_balance_powerup_strength_force");
764                         }
765                 }
766
767                 // apply invincibility multiplier
768                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
769                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
770
771                 if (targ == attacker)
772                         damage = damage * cvar("g_balance_selfdamagepercent");  // Partial damage if the attacker hits himself
773
774                 // CTF: reduce damage/force
775                 if(g_ctf)
776                 if(targ == attacker)
777                 if(targ.flagcarried)
778                 {
779                         damage = damage * cvar("g_ctf_flagcarrier_selfdamage");
780                         force = force * cvar("g_ctf_flagcarrier_selfforce");
781                 }
782
783                 if(g_runematch)
784                 {
785                         // apply strength rune
786                         if (attacker.runes & RUNE_STRENGTH)
787                         {
788                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
789                                 {
790                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
791                                         force = force * cvar("g_balance_rune_strength_combo_force");
792                                 }
793                                 else
794                                 {
795                                         damage = damage * cvar("g_balance_rune_strength_damage");
796                                         force = force * cvar("g_balance_rune_strength_force");
797                                 }
798                         }
799                         else if (attacker.runes & CURSE_WEAK)
800                         {
801                                 damage = damage * cvar("g_balance_curse_weak_damage");
802                                 force = force * cvar("g_balance_curse_weak_force");
803                         }
804
805                         // apply defense rune
806                         if (targ.runes & RUNE_DEFENSE)
807                         {
808                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
809                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
810                                 else
811                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
812                         }
813                         else if (targ.runes & CURSE_VULNER)
814                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
815                 }
816
817                 // count the damage
818                 if(attacker)
819                 if(!targ.deadflag)
820                 if(targ.takedamage == DAMAGE_AIM)
821                 if(targ != attacker)
822                 {
823                         if(targ.classname == "player")
824                         {
825                                 // HEAD SHOT:
826                                 // find height of hit on player axis
827                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
828                                 vector headmins, headmaxs, org;
829                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
830                                 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);
831                                 headmaxs = org + '0.6 0 0' * targ.maxs_x + '0 0.6 0' * targ.maxs_y + '0 0 1' * targ.maxs_z;
832                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
833                                 {
834                                         deathtype |= HITTYPE_HEADSHOT;
835                                 }
836                         }
837                         else if(targ.classname == "turret_head")
838                         {
839                                 deathtype |= HITTYPE_HEADSHOT;
840                         }
841                         if(deathtype & HITTYPE_HEADSHOT)
842                                 damage *= 1 + damage_headshotbonus;
843
844                         if(targ.classname == "player")
845                         {
846                                 if(IsDifferentTeam(targ, attacker))
847                                 {
848                                         if(damage > 0)
849                                         {
850                                                 if(targ.BUTTON_CHAT)
851                                                         attacker.typehitsound += 1;
852                                                 else
853                                                         attacker.hitsound += 1;
854
855                                                 damage_goodhits += 1;
856                                                 damage_gooddamage += damage;
857
858                                                 if not(DEATH_ISSPECIAL(deathtype))
859                                                 {
860                                                         if(!g_minstagib)
861                                                         if(IsFlying(targ))
862                                                                 yoda = 1;
863
864                                                         if(g_minstagib)
865                                                         if(targ.items & IT_STRENGTH)
866                                                                 yoda = 1;
867
868                                                         if(deathtype & HITTYPE_HEADSHOT)
869                                                                 headshot = 1;
870                                                 }
871                                         }
872                                 }
873                                 else
874                                 {
875                                         if(deathtype != DEATH_FIRE)
876                                                 attacker.typehitsound += 1;
877                                         if(mirrordamage > 0)
878                                                 if(time > attacker.teamkill_complain)
879                                                 {
880                                                         attacker.teamkill_complain = time + 5;
881                                                         attacker.teamkill_soundtime = time + 0.4;
882                                                         attacker.teamkill_soundsource = targ;
883                                                 }
884                                 }
885                         }
886                 }
887         }
888
889         // apply push
890         if (self.damageforcescale)
891         if (vlen(force))
892         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
893         {
894                 self.velocity = self.velocity + self.damageforcescale * force;
895                 self.flags &~= FL_ONGROUND;
896                 UpdateCSQCProjectile(self);
897         }
898         // apply damage
899         if (damage != 0 || (self.damageforcescale && vlen(force)))
900         if (self.event_damage)
901                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
902         self = oldself;
903
904         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
905         {
906                 // Savage: vampire mode
907                 if (g_vampire)
908                 if (!g_minstagib)
909                 if (time >= self.spawnshieldtime)
910                 {
911                         attacker.health += damage;
912                 }
913                 if(g_runematch)
914                 {
915                         if (attacker.runes & RUNE_VAMPIRE)
916                         {
917                         // apply vampire rune
918                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
919                                 {
920                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
921                                         attacker.health = bound(
922                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
923                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
924                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
925                                 }
926                                 else
927                                 {
928                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
929                                         attacker.health = bound(
930                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
931                                                                                         // empathy won't let you gain health in the same way...
932                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
933                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
934                                         }
935                         }
936                         // apply empathy curse
937                         else if (attacker.runes & CURSE_EMPATHY)
938                         {
939                                 attacker.health = bound(
940                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
941                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
942                                         attacker.health);
943                         }
944                 }
945         }
946
947         // apply mirror damage if any
948         if(mirrordamage > 0 || mirrorforce > 0)
949         {
950                 attacker = attacker_save;
951                 if(g_minstagib)
952                         if(mirrordamage > 0)
953                         {
954                                 // just lose extra LIVES, don't kill the player for mirror damage
955                                 if(attacker.armorvalue > 0)
956                                 {
957                                         attacker.armorvalue = attacker.armorvalue - 1;
958                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
959                                         attacker.hitsound += 1;
960                                 }
961                                 mirrordamage = 0;
962                         }
963                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
964                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
965         }
966 }
967
968 vector NearestPointOnBox(entity box, vector org)
969 {
970         vector m1, m2, nearest;
971
972         m1 = box.mins + box.origin;
973         m2 = box.maxs + box.origin;
974
975         nearest_x = bound(m1_x, org_x, m2_x);
976         nearest_y = bound(m1_y, org_y, m2_y);
977         nearest_z = bound(m1_z, org_z, m2_z);
978
979         return nearest;
980 }
981
982 .float actual_damage[WEP_COUNT]; //amount of damage done
983 .float max_damage[WEP_COUNT]; //the maximum damage of the weapon
984
985 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(actual_damage);
986 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(max_damage);
987
988 void Damage_RecordDamage(entity attacker, float deathtype, float damage)
989 {
990         float weaponid;
991         weaponid = DEATH_WEAPONOF(deathtype);
992         if not(inWarmupStage)
993         if(weaponid)
994         if(clienttype(attacker) == CLIENTTYPE_REAL)
995         {
996                 // Track damage done and update the stat to be sent later in g_world.qc
997                 attacker.actual_damage[weaponid] += damage;
998                 attacker.damage_hits = weaponid + 64 * rint(attacker.actual_damage[weaponid]);
999         }
1000 }
1001
1002 float RadiusDamage_running;
1003 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
1004 // Returns total damage applies to creatures
1005 {
1006         entity  targ;
1007         float   finaldmg;
1008         float   power;
1009         vector  blastorigin;
1010         vector  force;
1011         vector  diff;
1012         vector  center;
1013         vector  nearest;
1014         float   total_damage_to_creatures;
1015         entity  next;
1016         float   tfloordmg;
1017         float   tfloorforce;
1018
1019         float stat_damagedone;
1020         float stat_maxdamage;
1021
1022         if(RadiusDamage_running)
1023         {
1024                 string save;
1025                 print("RadiusDamage called recursively!\n");
1026                 print("Expect stuff to go HORRIBLY wrong.\n");
1027                 print("Causing a stack trace...\n");
1028                 save = cvar_string("prvm_backtraceforwarnings");
1029                 cvar_set("prvm_backtraceforwarnings", "1");
1030                 fclose(-1); // calls VM_Warning
1031                 cvar_set("prvm_backtraceforwarnings", save);
1032                 return 0;
1033         }
1034
1035         RadiusDamage_running = 1;
1036
1037         tfloordmg = cvar("g_throughfloor_damage");
1038         tfloorforce = cvar("g_throughfloor_force");
1039
1040         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1041         total_damage_to_creatures = 0;
1042
1043         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1044         {
1045                 force = inflictor.velocity;
1046                 if(vlen(force) == 0)
1047                         force = '0 0 -1';
1048                 else
1049                         force = normalize(force);
1050                 if(forceintensity >= 0)
1051                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, attacker);
1052                 else
1053                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, attacker);
1054         }
1055
1056         stat_damagedone = 0;
1057         stat_maxdamage = 0;
1058
1059         targ = findradius (blastorigin, rad);
1060         while (targ)
1061         {
1062                 next = targ.chain;
1063                 if (targ != inflictor)
1064                         if (ignore != targ) if(targ.takedamage)
1065                         {
1066                                 // LordHavoc: measure distance to nearest point on target (not origin)
1067                                 // (this guarentees 100% damage on a touch impact)
1068                                 nearest = NearestPointOnBox(targ, blastorigin);
1069                                 diff = nearest - blastorigin;
1070                                 // round up a little on the damage to ensure full damage on impacts
1071                                 // and turn the distance into a fraction of the radius
1072                                 power = 1 - ((vlen (diff) - 2) / rad);
1073                                 //bprint(" ");
1074                                 //bprint(ftos(power));
1075                                 //if (targ == attacker)
1076                                 //      print(ftos(power), "\n");
1077                                 if (power > 0)
1078                                 {
1079                                         if (power > 1)
1080                                                 power = 1;
1081                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1082                                         if (finaldmg > 0)
1083                                         {
1084                                                 local float a;
1085                                                 local float c;
1086                                                 local float hits;
1087                                                 local float total;
1088                                                 local float hitratio;
1089                                                 local vector hitloc;
1090                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1091                                                 // if it's a player, use the view origin as reference
1092                                                 if (targ.classname == "player")
1093                                                         center = targ.origin + targ.view_ofs;
1094                                                 force = normalize(center - blastorigin);
1095                                                 force = force * (finaldmg / coredamage) * forceintensity;
1096                                                 // test line of sight to multiple positions on box,
1097                                                 // and do damage if any of them hit
1098                                                 hits = 0;
1099                                                 if (targ.classname == "player")
1100                                                         total = ceil(bound(1, finaldmg, 50));
1101                                                 else
1102                                                         total = ceil(bound(1, finaldmg/10, 5));
1103                                                 hitloc = nearest;
1104                                                 c = 0;
1105                                                 while (c < total)
1106                                                 {
1107                                                         traceline(blastorigin, nearest, MOVE_NOMONSTERS, inflictor);
1108                                                         if (trace_fraction == 1 || trace_ent == targ)
1109                                                         {
1110                                                                 hits = hits + 1;
1111                                                                 if (hits > 1)
1112                                                                         hitloc = hitloc + nearest;
1113                                                                 else
1114                                                                         hitloc = nearest;
1115                                                         }
1116                                                         nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1117                                                         nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1118                                                         nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1119                                                         c = c + 1;
1120                                                 }
1121                                                 nearest = hitloc * (1 / max(1, hits));
1122                                                 hitratio = (hits / total);
1123                                                 a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1124                                                 finaldmg = finaldmg * a;
1125                                                 a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1126                                                 force = force * a;
1127                                                 //if (targ == attacker)
1128                                                 //{
1129                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1130                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1131                                                 //      print(" (", ftos(a), ")\n");
1132                                                 //}
1133                                                 if(hits || tfloordmg || tfloorforce)
1134                                                 {
1135                                                         if(targ.iscreature)
1136                                                         {
1137                                                                 total_damage_to_creatures += finaldmg;
1138
1139                                                                 if(targ.flags & FL_CLIENT)
1140                                                                 if(targ.deadflag == DEAD_NO)
1141                                                                 if(targ != attacker)
1142                                                                 if(!teamplay || targ.team != attacker.team)
1143                                                                 {
1144                                                                         stat_damagedone += finaldmg;
1145                                                                         stat_maxdamage += coredamage;
1146                                                                 }
1147                                                         }
1148
1149                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1150                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1151                                                         else
1152                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1153                                                 }
1154                                         }
1155                                 }
1156                         }
1157                 targ = next;
1158         }
1159
1160         RadiusDamage_running = 0;
1161
1162         Damage_RecordDamage(attacker, deathtype, min(stat_maxdamage, stat_damagedone));
1163
1164         return total_damage_to_creatures;
1165 }
1166
1167 .float fire_damagepersec;
1168 .float fire_endtime;
1169 .float fire_deathtype;
1170 .entity fire_owner;
1171 .float fire_hitsound;
1172 .entity fire_burner;
1173
1174 void fireburner_think();
1175
1176 float Fire_IsBurning(entity e)
1177 {
1178         return (time < e.fire_endtime);
1179 }
1180
1181 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1182 {
1183         float dps;
1184         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1185
1186         if(e.classname == "player")
1187         {
1188                 if(e.deadflag)
1189                         return -1;
1190         }
1191         else
1192         {
1193                 if(!e.fire_burner)
1194                 {
1195                         // print("adding a fire burner to ", e.classname, "\n");
1196                         e.fire_burner = spawn();
1197                         e.fire_burner.classname = "fireburner";
1198                         e.fire_burner.think = fireburner_think;
1199                         e.fire_burner.nextthink = time;
1200                         e.fire_burner.owner = e;
1201                 }
1202         }
1203
1204         t = max(t, 0.1);
1205         dps = d / t;
1206         if(Fire_IsBurning(e))
1207         {
1208                 mintime = e.fire_endtime - time;
1209                 maxtime = max(mintime, t);
1210
1211                 mindps = e.fire_damagepersec;
1212                 maxdps = max(mindps, dps);
1213
1214                 if(maxtime > mintime || maxdps > mindps)
1215                 {
1216                         mindamage = mindps * mintime;
1217                         maxdamage = mindamage + d;
1218
1219                         // interval [mintime, maxtime] * [mindps, maxdps]
1220                         // intersected with
1221                         // [mindamage, maxdamage]
1222                         // maximum of this!
1223
1224                         if(maxdamage >= maxtime * maxdps)
1225                         {
1226                                 totaltime = maxtime;
1227                                 totaldamage = maxtime * maxdps;
1228
1229                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1230                         }
1231                         else
1232                         {
1233                                 // maxdamage is inside the interval!
1234                                 // first, try to use mindps; only if this fails, increase dps as needed
1235                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1236                                 totaldamage = maxdamage;
1237                                 // can totaldamage / totaltime be >= maxdps?
1238                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1239                                 // we know maxdamage < maxtime * maxdps
1240                                 // so it cannot be
1241
1242                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1243                         }
1244
1245                         // total conditions for increasing:
1246                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1247                         // however:
1248                         //     if maxtime = mintime, maxdps = mindps
1249                         // then:
1250                         //     maxdamage = mindamage + d
1251                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1252                         // so the last condition is not needed
1253
1254                         e.fire_damagepersec = totaldamage / totaltime;
1255                         e.fire_endtime = time + totaltime;
1256                         if(totaldamage > 1.2 * mindamage)
1257                         {
1258                                 e.fire_deathtype = dt;
1259                                 if(e.fire_owner != o)
1260                                 {
1261                                         e.fire_owner = o;
1262                                         e.fire_hitsound = FALSE;
1263                                 }
1264                         }
1265                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1266                 }
1267                 else
1268                         return 0;
1269         }
1270         else
1271         {
1272                 e.fire_damagepersec = dps;
1273                 e.fire_endtime = time + t;
1274                 e.fire_deathtype = dt;
1275                 e.fire_owner = o;
1276                 e.fire_hitsound = FALSE;
1277                 return d;
1278         }
1279 }
1280
1281 void Fire_ApplyDamage(entity e)
1282 {
1283         float t, d, hi, ty;
1284
1285         if not(Fire_IsBurning(e))
1286                 return;
1287
1288         // water and slime stop fire
1289         if(e.waterlevel)
1290         if(e.watertype != CONTENT_LAVA)
1291                 e.fire_endtime = 0;
1292
1293         t = min(frametime, e.fire_endtime - time);
1294         d = e.fire_damagepersec * t;
1295
1296         hi = e.fire_owner.hitsound;
1297         ty = e.fire_owner.typehitsound;
1298         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1299         if(e.fire_hitsound && e.fire_owner)
1300         {
1301                 e.fire_owner.hitsound = hi;
1302                 e.fire_owner.typehitsound = ty;
1303         }
1304         e.fire_hitsound = TRUE;
1305
1306         Damage_RecordDamage(e.fire_owner, e.fire_deathtype, d);
1307
1308         if not(IS_INDEPENDENT_PLAYER(e))
1309         FOR_EACH_PLAYER(other) if(e != other)
1310         {
1311                 if(other.classname == "player")
1312                 if(other.deadflag == DEAD_NO)
1313                 if not(IS_INDEPENDENT_PLAYER(other))
1314                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1315                 {
1316                         t = cvar("g_balance_firetransfer_time") * (e.fire_endtime - time);
1317                         d = cvar("g_balance_firetransfer_damage") * e.fire_damagepersec * t;
1318                         Fire_AddDamage(other, e, d, t, DEATH_FIRE);
1319                 }
1320         }
1321 }
1322
1323 void Fire_ApplyEffect(entity e)
1324 {
1325         if(Fire_IsBurning(e))
1326                 e.effects |= EF_FLAME;
1327         else
1328                 e.effects &~= EF_FLAME;
1329 }
1330
1331 void fireburner_think()
1332 {
1333         // for players, this is done in the regular loop
1334         if(wasfreed(self.owner))
1335         {
1336                 remove(self);
1337                 return;
1338         }
1339         Fire_ApplyEffect(self.owner);
1340         if(!Fire_IsBurning(self.owner))
1341         {
1342                 self.owner.fire_burner = world;
1343                 remove(self);
1344                 return;
1345         }
1346         Fire_ApplyDamage(self.owner);
1347         self.nextthink = time;
1348 }