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