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