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