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