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