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