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