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