]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
7f7f8f1c54f5aca54169786147aa8dfe71abdad1
[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         WriteByte(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                         //w = DEATH_WEAPONOF(deathtype);
367                         bprint("deathtype: ", ftos(deathtype), "\n");
368                         if(WEP_VALID(w))
369                         {
370                                 deathtype = DEATH_WEAPON;
371                         }
372
373                         // TODO: wut is this?
374                         // givefrags for logging apparently?
375                         if(deathtype != DEATH_TEAMCHANGE && deathtype != DEATH_QUIET)
376                         {
377                                 LogDeath("suicide", deathtype, targ, targ);
378                                 GiveFrags(attacker, targ, -1);
379                         }
380
381                         if (targ.killcount > 2)
382                                 msg = ftos(targ.killcount);
383                         Send_KillNotification(s, msg, ftos(w), deathtype, MSG_SUICIDE);
384                 }
385                 else if (attacker.classname == "player" || attacker.classname == "gib")
386                 {
387                         if(teams_matter && attacker.team == targ.team)
388                         {
389                                 type = KILL_TEAM;
390                                 /*
391                                 if(sv_gentle) {
392                                 // TODO: the centerprint!
393                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against a team mate!"));
394                                         bprint ("^1", a, "^1 took action against a team mate\n");
395                                 } else {
396                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s, ", a team mate!"));
397                                         bprint ("^1", a, "^1 mows down a team mate\n");
398                                 }
399                                 */
400                                 GiveFrags(attacker, targ, -1);
401                                 /*
402                                 if (targ.killcount > 2) {
403                                         if(sv_gentle)
404                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," scoring spree was ended by a team mate!\n");
405                                         else
406                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," kill spree was ended by a team mate!\n");
407                                 }
408                                 if (attacker.killcount > 2) {
409                                         if(sv_gentle)
410                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," scoring spree by going against a team mate\n");
411                                         else
412                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," kill spree by killing a team mate\n");
413                                 }
414                                 */
415
416                                 if (targ.killcount > 2) {
417                                         msg = ftos(targ.killcount);
418                                         a = s;
419                                 }
420                                 if (attacker.killcount > 2) {
421                                         msg = ftos(attacker.killcount);
422                                         type = KILL_TEAM_SPREE;
423                                 }
424
425                                 attacker.killcount = 0;
426
427                                 LogDeath("tk", deathtype, attacker, targ);
428                                 Send_CSQC_Centerprint(attacker, s, MSG_KILL);
429                                 Send_KillNotification(a, msg, "", type, MSG_KILL);
430                         }
431                         else
432                         {
433                                 string blood_message, victim_message;
434                                 if (!checkrules_firstblood)
435                                 {
436                                         checkrules_firstblood = TRUE;
437                                         Send_KillNotification(a, "", "", KILL_FIRST_BLOOD, MSG_KILL);
438                                         //bprint("^1",a, "^1 drew first blood", "\n");
439                                         // TODO: make these print at newline if they dont
440                                         Send_CSQC_Centerprint(attacker, "", KILL_FIRST_BLOOD);
441                                         Send_CSQC_Centerprint(targ, "", KILL_FIRST_VICTIM);
442                                         //blood_message = "^1First blood\n";
443                                         //victim_message = "^1First victim\n";  // or First casualty
444                                 }
445                                 //if(sv_gentle > 0) {
446                                 //      centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You scored against ^7", s, GetAdvancedDeathReports(targ)));
447                                 //      centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, a,"^1 scored against you ^7", GetAdvancedDeathReports(attacker)));
448
449                                 if((cvar("sv_fragmessage_information_typefrag")) && (targ.BUTTON_CHAT)) {
450                                         Send_CSQC_Centerprint(attacker, s, KILL_TYPEFRAG);
451                                         //centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^1You typefragged ^7", s, GetAdvancedDeathReports(targ)));
452                                         Send_CSQC_Centerprint(targ, a, KILL_TYPEFRAGGED);
453                                         //centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were typefragged by ^7", a, GetAdvancedDeathReports(attacker)));
454                                 } else {
455                                         Send_CSQC_Centerprint(attacker, s, KILL_FRAG);
456                                         //centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You fragged ^7", s, GetAdvancedDeathReports(targ)));
457                                         Send_CSQC_Centerprint(targ, a, KILL_FRAGGED);
458                                         //centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were fragged by ^7", a, GetAdvancedDeathReports(attacker)));
459                                 }
460                                 attacker.taunt_soundtime = time + 1;
461
462                                 // TODO: more msg
463                                 //if(sv_gentle) {
464                                 //      bprint ("^1",s, "^1 needs a restart thanks to ", a, "\n");
465                                 //} else {
466                                         /*
467                                         w = DEATH_WEAPONOF(deathtype);
468                                         if(WEP_VALID(w))
469                                         {
470                                                 w_deathtypestring = "was blasted by";
471                                                 w_deathtype = deathtype;
472                                                 weapon_action(w, WR_KILLMESSAGE);
473                                                 p = strstrofs(w_deathtypestring, "#", 0);
474                                                 if(p < 0)
475                                                         bprint("^1", s, "^1 ", w_deathtypestring, " ", a, "\n");
476                                                 else
477                                                         bprint("^1", s, "^1 ", substring(w_deathtypestring, 0, p), a, "^1", substring(w_deathtypestring, p+1, strlen(w_deathtypestring) - (p+1)), "\n");
478                                         }
479                                         else if (deathtype == DEATH_TELEFRAG)
480                                                 bprint ("^1",s, "^1 was telefragged by ", a, "\n");
481                                         else if (deathtype == DEATH_DROWN)
482                                                 bprint ("^1",s, "^1 was drowned by ", a, "\n");
483                                         else if (deathtype == DEATH_SLIME)
484                                                 bprint ("^1",s, "^1 was slimed by ", a, "\n");
485                                         else if (deathtype == DEATH_LAVA)
486                                                 bprint ("^1",s, "^1 was cooked by ", a, "\n");
487                                         else if (deathtype == DEATH_FALL)
488                                                 bprint ("^1",s, "^1 was grounded by ", a, "\n");
489                                         else if (deathtype == DEATH_SHOOTING_STAR)
490                                                 bprint ("^1",s, "^1 was shot into space by ", a, "\n");
491                                         else if (deathtype == DEATH_SWAMP)
492                                                 bprint ("^1",s, "^1 was conserved by ", a, "\n");
493                                         else if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
494                                         {
495                                                 p = strstrofs(inflictor.message2, "#", 0);
496                                                 if(p < 0)
497                                                         bprint("^1", s, "^1 ", inflictor.message2, " ", a, "\n");
498                                                 else
499                                                         bprint("^1", s, "^1 ", substring(inflictor.message2, 0, p), a, "^1", substring(inflictor.message2, p+1, strlen(inflictor.message2) - (p+1)), "\n");
500                                         }
501                                         else if(deathtype == DEATH_SBCRUSH)
502                         bprint ("^1",s, "^1 was crushed by ^1", a, "\n");
503                                         else if(deathtype == DEATH_SBMINIGUN)
504                         bprint ("^1",s, "^1 got shredded by ^1", a, "\n");
505                                         else if(deathtype == DEATH_SBROCKET)
506                         bprint ("^1",s, "^1 was blased to bits by ^1", a, "\n");
507                                         else if(deathtype == DEATH_SBBLOWUP)
508                                         [
509                         bprint ("^1",s, "^1 got cought in the destruction of ^1", a, "'s vehicle\n");
510
511                                         else if(deathtype == DEATH_WAKIGUN)
512                         bprint ("^1",s, "^1 was bolted down by ^1", a, "\n");
513                                         else if(deathtype == DEATH_WAKIROCKET)
514                         bprint ("^1",s, "^1 could find no shelter from ^1", a, "'s rockets\n");
515                                         else if(deathtype == DEATH_WAKIBLOWUP)
516                         bprint ("^1",s, "^1 dies when ^1", a, "'s wakizashi dies.\n");
517
518                                         else if(deathtype == DEATH_TURRET)
519                                                 bprint ("^1",s, "^1 was pushed into the line of fire by ^1", a, "\n");
520                                         else if(deathtype == DEATH_TOUCHEXPLODE)
521                                                 bprint ("^1",s, "^1 was pushed into an accident by ^1", a, "\n");
522                                         else if(deathtype == DEATH_CHEAT)
523                                                 bprint ("^1",s, "^1 was unfairly eliminated by ^1", a, "\n");
524                                         else if (deathtype == DEATH_FIRE)
525                                         bprint ("^1",s, "^1 was burnt to death by ^1", a, "\n");
526                                         else if (deathtype == DEATH_CUSTOM)
527                                                 bprint ("^1",s, "^1 ", deathmessage, " by ^1", a, "\n");
528                                         else
529                                                 bprint ("^1",s, "^1 was fragged by ", a, "\n");
530                                         */
531
532                                         //w = DEATH_WEAPONOF(deathtype);
533                                         bprint("deathtype: ", ftos(deathtype), "\n");
534                                         if(WEP_VALID(w))
535                                         {
536                                                 deathtype = DEATH_WEAPON;
537                                         }
538
539                                         msg = a;
540                                         if (deathtype == DEATH_CUSTOM)
541                                                 msg = strcat(deathmessage, " by ^1", msg);
542                                         Send_KillNotification(s, msg, ftos(w), deathtype, MSG_KILL);
543                                 //}
544
545                                 if(g_ctf && targ.flagcarried)
546                                 {
547                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
548                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
549                                         GiveFrags(attacker, targ, 0); // for logging
550                                 }
551                                 else
552                                         GiveFrags(attacker, targ, 1);
553
554                                 if (targ.killcount > 2) {
555                                         /*
556                                         if(sv_gentle)
557                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " scoring spree was ended by ", a, "\n");
558                                         else
559                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
560                                         */
561                                         Send_KillNotification(s, ftos(targ.killcount), a, KILL_END_SPREE, MSG_SPREE);
562                                 }
563
564                                 attacker.killcount = attacker.killcount + 1;
565
566                                 if (attacker.killcount > 2) {
567                                         /*
568                                         if(sv_gentle)
569                                                 bprint ("^1",a,"^1 made ",ftos(attacker.killcount)," scores in a row\n");
570                                         else
571                                                 bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
572                                         */
573                                         Send_KillNotification(a, ftos(attacker.killcount), "", KILL_SPREE, MSG_SPREE);
574                                 }
575
576                                 LogDeath("frag", deathtype, attacker, targ);
577
578                                 if (attacker.killcount == 3)
579                                 {
580                                         //if(sv_gentle) {
581                                         //      bprint (a,"^7 made a ^1TRIPLE SCORE\n");
582                                         //} else {
583                                         //      bprint (a,"^7 made a ^1TRIPLE FRAG\n");
584                                         //
585                                         Send_KillNotification(a, "", "", KILL_SPREE_3, MSG_SPREE);
586                                         AnnounceTo(attacker, "03kills");
587
588                                         //}
589                                 }
590                                 else if (attacker.killcount == 5)
591                                 {
592                                         //if(sv_gentle) {
593                                         //      bprint (a,"^7 unleashes ^1SCORING RAGE\n");
594                                         //} else {
595                                         //      bprint (a,"^7 unleashes ^1RAGE\n");
596                                         Send_KillNotification(a, "", "", KILL_SPREE_5, MSG_SPREE);
597                                         AnnounceTo(attacker, "05kills");
598                                         //}
599                                 }
600                                 else if (attacker.killcount == 10)
601                                 {
602                                         //if(sv_gentle) {
603                                         //      bprint (a,"^7 made ^1TEN SCORES IN A ROW!\n");
604                                         //} else {
605                                         //      bprint (a,"^7 starts the ^1MASSACRE!\n");
606                                         Send_KillNotification(a, "", "", KILL_SPREE_10, MSG_SPREE);
607                                         AnnounceTo(attacker, "10kills");
608                                         //}
609                                 }
610                                 else if (attacker.killcount == 15)
611                                 {
612                                         /*
613                                         if(sv_gentle) {
614                                                 bprint (a,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
615                                         } else {
616                                                 bprint (a,"^7 executes ^1MAYHEM!\n");
617                                                 AnnounceTo(attacker, "15kills");
618                                         }
619                                         */
620                                         Send_KillNotification(a, "", "", KILL_SPREE_15, MSG_SPREE);
621                                         AnnounceTo(attacker, "15kills");
622                                 }
623                                 else if (attacker.killcount == 20)
624                                 {
625                                         /*
626                                         if(sv_gentle) {
627                                                 bprint (a,"^7 made ^1TWENTY SCORES IN A ROW!\n");
628                                         } else {
629                                                 bprint (a,"^7 is a ^1BERSERKER!\n");
630                                                 AnnounceTo(attacker, "20kills");
631                                         }
632                                         */
633                                         Send_KillNotification(a, "", "", KILL_SPREE_20, MSG_SPREE);
634                                         AnnounceTo(attacker, "20kills");
635                                 }
636                                 else if (attacker.killcount == 25)
637                                 {
638                                         /*
639                                         if(sv_gentle) {
640                                                 bprint (a,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
641                                         } else {
642                                                 bprint (a,"^7 inflicts ^1CARNAGE!\n");
643                                                 AnnounceTo(attacker, "25kills");
644                                         }*/
645                                         Send_KillNotification(a, "", "", KILL_SPREE_25, MSG_SPREE);
646                                         AnnounceTo(attacker, "25kills");
647                                 }
648                                 else if (attacker.killcount == 30)
649                                 {
650                                         /*
651                                         if(sv_gentle) {
652                                                 bprint (a,"^7 made ^1THIRTY SCORES IN A ROW!\n");
653                                         } else {
654                                                 bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
655                                                 AnnounceTo(attacker, "30kills");
656                                         }
657                                         */
658                                         Send_KillNotification(a, "", "", KILL_SPREE_30, MSG_SPREE);
659                                         AnnounceTo(attacker, "30kills");
660                                 }
661                         }
662                 }
663                 else
664                 {
665                         //centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
666                         Send_CSQC_Centerprint(targ, "", MSG_KILL_ACTION);
667                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
668                                 msg = inflictor.message;
669                         else if (deathtype == DEATH_CUSTOM)
670                                 msg = deathmessage;
671                                 //bprint ("^1",s, "^1 ", inflictor.message, "\n");
672                         /*else if (deathtype == DEATH_DROWN)
673                                 if(sv_gentle)
674                                         bprint ("^1",s, "^1 was in the water for too long\n");
675                                 else
676                                         bprint ("^1",s, "^1 drowned\n");
677                         else if (deathtype == DEATH_SLIME)
678                                 bprint ("^1",s, "^1 was slimed\n");
679                         else if (deathtype == DEATH_LAVA)
680                                 if(sv_gentle)
681                                         bprint ("^1",s, "^1 found a hot place\n");
682                                 else
683                                         bprint ("^1",s, "^1 turned into hot slag\n");
684                         else if (deathtype == DEATH_FALL)
685                                 if(sv_gentle)
686                                         bprint ("^1",s, "^1 tested gravity (and it worked)\n");
687                                 else
688                                         bprint ("^1",s, "^1 hit the ground with a crunch\n");
689                         else if (deathtype == DEATH_SHOOTING_STAR)
690                                 bprint ("^1",s, "^1 became a shooting star\n");
691                         else if (deathtype == DEATH_SWAMP)
692                                 if(sv_gentle)
693                                         bprint ("^1",s, "^1 discovered a swamp\n");
694                                 else
695                                         bprint ("^1",s, "^1 is now conserved for centuries to come\n");
696                         else if(deathtype == DEATH_TURRET)
697                                 bprint ("^1",s, "^1 was mowed down by a turret \n");
698                         else if (deathtype == DEATH_CUSTOM)
699                                 bprint ("^1",s, "^1 ", deathmessage, "\n");
700                         else if(deathtype == DEATH_TOUCHEXPLODE)
701                                 bprint ("^1",s, "^1 died in an accident\n");
702                         else if(deathtype == DEATH_CHEAT)
703                                 bprint ("^1",s, "^1 was unfairly eliminated\n");
704                         else if(deathtype == DEATH_FIRE)
705                                 if(sv_gentle)
706                                         bprint ("^1",s, "^1 felt a little hot\n");
707                                 else
708                                         bprint ("^1",s, "^1 burnt to death\n");
709                         else
710                                 if(sv_gentle)
711                                         bprint ("^1",s, "^1 needs a restart\n");
712                                 else
713                                         bprint ("^1",s, "^1 died\n");
714                         */
715                         GiveFrags(targ, targ, -1);
716                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
717                                 AnnounceTo(targ, "botlike");
718                         }
719                         Send_KillNotification(s, msg, "", deathtype, MSG_KILL_ACTION);
720
721                         if (targ.killcount > 2)
722                                 Send_KillNotification(s, ftos(targ.killcount), "", 0, MSG_KILL_ACTION_SPREE);
723                                 //if(sv_gentle)
724                                 //      bprint ("^1",s,"^1 needs a restart after a ",ftos(targ.killcount)," scoring spree\n");
725                                 //else
726                                 //      bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
727
728                         LogDeath("accident", deathtype, targ, targ);
729                 }
730
731                 targ.death_origin = targ.origin;
732                 if(targ != attacker)
733                         targ.killer_origin = attacker.origin;
734
735                 // FIXME: this should go in PutClientInServer
736                 if (targ.killcount)
737                         targ.killcount = 0;
738         }
739 }
740
741 // these are updated by each Damage call for use in button triggering and such
742 entity damage_targ;
743 entity damage_inflictor;
744 entity damage_attacker;
745
746 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
747 {
748         float mirrordamage;
749         float mirrorforce;
750         float teamdamage0;
751         entity attacker_save;
752         mirrordamage = 0;
753         mirrorforce = 0;
754
755         if (gameover || targ.killcount == -666)
756                 return;
757
758         local entity oldself;
759         oldself = self;
760         self = targ;
761         damage_targ = targ;
762         damage_inflictor = inflictor;
763         damage_attacker = attacker;
764                 attacker_save = attacker;
765
766         if(targ.classname == "player")
767                 if(targ.hook)
768                         if(targ.hook.aiment)
769                                 if(targ.hook.aiment == attacker)
770                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
771
772         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
773         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
774         {
775                 if(targ.classname == "player")
776                         if not(IsDifferentTeam(targ, attacker))
777                         {
778                                 self = oldself;
779                                 return;
780                         }
781         }
782
783         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE || deathtype == DEATH_QUIET)
784         {
785                 // These are ALWAYS lethal
786                 // No damage modification here
787                 // Instead, prepare the victim for his death...
788                 targ.armorvalue = 0;
789                 targ.spawnshieldtime = 0;
790                 targ.health = 0.9; // this is < 1
791                 targ.flags -= targ.flags & FL_GODMODE;
792                 damage = 100000;
793         }
794         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
795         {
796                 // no processing
797         }
798         else
799         {
800                 if (targ.classname == "player")
801                 if (attacker.classname == "player")
802                 if (!targ.isbot)
803                 if (attacker.isbot)
804                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
805
806                 // nullify damage if teamplay is on
807                 if(deathtype != DEATH_TELEFRAG)
808                 if(attacker.classname == "player")
809                 {
810                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
811                         {
812                                 damage = 0;
813                                 force = '0 0 0';
814                         }
815                         else if(attacker.team == targ.team)
816                         {
817                                 if(teamplay == 1)
818                                         damage = 0;
819                                 else if(attacker != targ)
820                                 {
821                                         if(teamplay == 3)
822                                                 damage = 0;
823                                         else if(teamplay == 4)
824                                         {
825                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
826                                                 {
827                                                         teamdamage0 = max(attacker.dmg_team, cvar("g_teamdamage_threshold"));
828                                                         attacker.dmg_team = attacker.dmg_team + damage;
829                                                         if(attacker.dmg_team > teamdamage0 && !g_ca)
830                                                                 mirrordamage = cvar("g_mirrordamage") * (attacker.dmg_team - teamdamage0);
831                                                         mirrorforce = cvar("g_mirrordamage") * vlen(force);
832                                                         if(g_minstagib)
833                                                         {
834                                                                 if(cvar("g_friendlyfire") == 0)
835                                                                         damage = 0;
836                                                         }
837                                                         else if(g_ca)
838                                                                 damage = 0;
839                                                         else
840                                                                 damage = cvar("g_friendlyfire") * damage;
841                                                         // mirrordamage will be used LATER
842                                                 }
843                                                 else
844                                                         damage = 0;
845                                         }
846                                 }
847                         }
848                 }
849
850                 if(targ.classname == "player")
851                 if(attacker.classname == "player")
852                 if(attacker != targ)
853                 {
854                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
855                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
856                 }
857
858                 if(targ.classname == "player")
859                 if (g_minstagib)
860                 {
861                         if ((deathtype == DEATH_FALL)  ||
862                                 (deathtype == DEATH_DROWN) ||
863                                 (deathtype == DEATH_SLIME) ||
864                                 (deathtype == DEATH_LAVA)  ||
865                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
866                         {
867                                 self = oldself;
868                                 return;
869                         }
870                         if(damage > 0)
871                             damage = 10000;
872                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
873                         {
874                                 targ.armorvalue -= 1;
875                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(targ.armorvalue)));
876                                 damage = 0;
877                                 targ.hitsound += 1;
878                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
879                         }
880                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
881                         {
882                                 damage = 0;
883                                 if (targ != attacker)
884                                 {
885                                         if ((targ.health >= 1) && (targ.classname == "player"))
886                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "Secondary fire inflicts no damage!"));
887                                         damage = 0;
888                                         mirrordamage = 0;
889                                         force = '0 0 0';
890                                         // keep mirrorforce
891                                         attacker = targ;
892                                 }
893                         }
894                 }
895
896                 if not(DEATH_ISSPECIAL(deathtype))
897                 {
898                         damage *= g_weapondamagefactor;
899                         mirrordamage *= g_weapondamagefactor;
900                         force = force * g_weaponforcefactor;
901                         mirrorforce *= g_weaponforcefactor;
902                 }
903
904                 // apply strength multiplier
905                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
906                 {
907                         if(targ == attacker)
908                         {
909                                 damage = damage * cvar("g_balance_powerup_strength_selfdamage");
910                                 force = force * cvar("g_balance_powerup_strength_selfforce");
911                         }
912                         else
913                         {
914                                 damage = damage * cvar("g_balance_powerup_strength_damage");
915                                 force = force * cvar("g_balance_powerup_strength_force");
916                         }
917                 }
918
919                 // apply invincibility multiplier
920                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
921                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
922
923                 if (targ == attacker)
924                 {
925                         if(g_ca || (g_cts && !cvar("g_cts_selfdamage")))
926                                 damage = 0;
927                         else
928                                 damage = damage * cvar("g_balance_selfdamagepercent");  // Partial damage if the attacker hits himself
929                 }
930
931                 // CTF: reduce damage/force
932                 if(g_ctf)
933                 if(targ == attacker)
934                 if(targ.flagcarried)
935                 {
936                         damage = damage * cvar("g_ctf_flagcarrier_selfdamage");
937                         force = force * cvar("g_ctf_flagcarrier_selfforce");
938                 }
939
940                 if(g_runematch)
941                 {
942                         // apply strength rune
943                         if (attacker.runes & RUNE_STRENGTH)
944                         {
945                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
946                                 {
947                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
948                                         force = force * cvar("g_balance_rune_strength_combo_force");
949                                 }
950                                 else
951                                 {
952                                         damage = damage * cvar("g_balance_rune_strength_damage");
953                                         force = force * cvar("g_balance_rune_strength_force");
954                                 }
955                         }
956                         else if (attacker.runes & CURSE_WEAK)
957                         {
958                                 damage = damage * cvar("g_balance_curse_weak_damage");
959                                 force = force * cvar("g_balance_curse_weak_force");
960                         }
961
962                         // apply defense rune
963                         if (targ.runes & RUNE_DEFENSE)
964                         {
965                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
966                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
967                                 else
968                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
969                         }
970                         else if (targ.runes & CURSE_VULNER)
971                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
972                 }
973
974                 // count the damage
975                 if(attacker)
976                 if(!targ.deadflag)
977                 if(targ.takedamage == DAMAGE_AIM)
978                 if(targ != attacker)
979                 {
980                         if(targ.classname == "player")
981                         {
982                                 // HEAD SHOT:
983                                 // find height of hit on player axis
984                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
985                                 vector headmins, headmaxs, org;
986                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
987                                 headmins = org + GetHeadshotMins(targ);
988                                 headmaxs = org + GetHeadshotMaxs(targ);
989                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
990                                 {
991                                         deathtype |= HITTYPE_HEADSHOT;
992                                 }
993                         }
994                         else if(targ.classname == "turret_head")
995                         {
996                                 deathtype |= HITTYPE_HEADSHOT;
997                         }
998                         if(deathtype & HITTYPE_HEADSHOT)
999                                 damage *= 1 + damage_headshotbonus;
1000
1001                         if(targ.classname == "player")
1002                         {
1003                                 if(IsDifferentTeam(targ, attacker))
1004                                 {
1005                                         if(damage > 0)
1006                                         {
1007                                                 if(targ.BUTTON_CHAT)
1008                                                         attacker.typehitsound += 1;
1009                                                 else
1010                                                         attacker.hitsound += 1;
1011
1012                                                 damage_goodhits += 1;
1013                                                 damage_gooddamage += damage;
1014
1015                                                 if not(DEATH_ISSPECIAL(deathtype))
1016                                                 {
1017                                                         if(!g_minstagib)
1018                                                         if(IsFlying(targ))
1019                                                                 yoda = 1;
1020
1021                                                         if(g_minstagib)
1022                                                         if(targ.items & IT_STRENGTH)
1023                                                                 yoda = 1;
1024
1025                                                         if(deathtype & HITTYPE_HEADSHOT)
1026                                                                 headshot = 1;
1027                                                 }
1028                                         }
1029                                 }
1030                                 else
1031                                 {
1032                                         if(deathtype != DEATH_FIRE)
1033                                                 attacker.typehitsound += 1;
1034                                         if(mirrordamage > 0)
1035                                                 if(time > attacker.teamkill_complain)
1036                                                 {
1037                                                         attacker.teamkill_complain = time + 5;
1038                                                         attacker.teamkill_soundtime = time + 0.4;
1039                                                         attacker.teamkill_soundsource = targ;
1040                                                 }
1041                                 }
1042                         }
1043                 }
1044         }
1045
1046         // apply push
1047         if (self.damageforcescale)
1048         if (vlen(force))
1049         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
1050         {
1051                 self.velocity = self.velocity + self.damageforcescale * force;
1052                 self.flags &~= FL_ONGROUND;
1053                 UpdateCSQCProjectile(self);
1054         }
1055         // apply damage
1056         if (damage != 0 || (self.damageforcescale && vlen(force)))
1057         if (self.event_damage)
1058                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
1059         self = oldself;
1060
1061         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
1062         {
1063                 // Savage: vampire mode
1064                 if (g_vampire)
1065                 if (!g_minstagib)
1066                 if (time >= self.spawnshieldtime)
1067                 {
1068                         attacker.health += damage;
1069                 }
1070                 if(g_runematch)
1071                 {
1072                         if (attacker.runes & RUNE_VAMPIRE)
1073                         {
1074                         // apply vampire rune
1075                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
1076                                 {
1077                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
1078                                         attacker.health = bound(
1079                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
1080                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
1081                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
1082                                 }
1083                                 else
1084                                 {
1085                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
1086                                         attacker.health = bound(
1087                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
1088                                                                                         // empathy won't let you gain health in the same way...
1089                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
1090                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
1091                                         }
1092                         }
1093                         // apply empathy curse
1094                         else if (attacker.runes & CURSE_EMPATHY)
1095                         {
1096                                 attacker.health = bound(
1097                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
1098                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
1099                                         attacker.health);
1100                         }
1101                 }
1102         }
1103
1104         // apply mirror damage if any
1105         if(mirrordamage > 0 || mirrorforce > 0)
1106         {
1107                 attacker = attacker_save;
1108                 if(g_minstagib)
1109                         if(mirrordamage > 0)
1110                         {
1111                                 // just lose extra LIVES, don't kill the player for mirror damage
1112                                 if(attacker.armorvalue > 0)
1113                                 {
1114                                         attacker.armorvalue = attacker.armorvalue - 1;
1115                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
1116                                         attacker.hitsound += 1;
1117                                 }
1118                                 mirrordamage = 0;
1119                         }
1120                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
1121                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
1122         }
1123 }
1124
1125 vector NearestPointOnBox(entity box, vector org)
1126 {
1127         vector m1, m2, nearest;
1128
1129         m1 = box.mins + box.origin;
1130         m2 = box.maxs + box.origin;
1131
1132         nearest_x = bound(m1_x, org_x, m2_x);
1133         nearest_y = bound(m1_y, org_y, m2_y);
1134         nearest_z = bound(m1_z, org_z, m2_z);
1135
1136         return nearest;
1137 }
1138
1139 void Damage_RecordDamage(entity attacker, float deathtype, float damage)
1140 {
1141         float weaponid;
1142         weaponid = DEATH_WEAPONOF(deathtype);
1143
1144         if not(inWarmupStage)
1145         if (weaponid)
1146         if ((clienttype(attacker) == CLIENTTYPE_REAL) | (clienttype(attacker) == CLIENTTYPE_BOT)) {
1147                 attacker.stats_hit[weaponid - 1] += damage;
1148                 attacker.stat_hit = weaponid + 64 * floor(attacker.stats_hit[weaponid - 1]);
1149         }
1150 }
1151
1152 float RadiusDamage_running;
1153 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
1154 // Returns total damage applies to creatures
1155 {
1156         entity  targ;
1157         float   finaldmg;
1158         float   power;
1159         vector  blastorigin;
1160         vector  force;
1161         vector  diff;
1162         vector  center;
1163         vector  nearest;
1164         float   total_damage_to_creatures;
1165         entity  next;
1166         float   tfloordmg;
1167         float   tfloorforce;
1168
1169         float stat_damagedone;
1170         float stat_maxdamage;
1171
1172         if(RadiusDamage_running)
1173         {
1174                 string save;
1175                 print("RadiusDamage called recursively!\n");
1176                 print("Expect stuff to go HORRIBLY wrong.\n");
1177                 print("Causing a stack trace...\n");
1178                 save = cvar_string("prvm_backtraceforwarnings");
1179                 cvar_set("prvm_backtraceforwarnings", "1");
1180                 fclose(-1); // calls VM_Warning
1181                 cvar_set("prvm_backtraceforwarnings", save);
1182                 return 0;
1183         }
1184
1185         RadiusDamage_running = 1;
1186
1187         tfloordmg = cvar("g_throughfloor_damage");
1188         tfloorforce = cvar("g_throughfloor_force");
1189
1190         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1191         total_damage_to_creatures = 0;
1192
1193         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1194         if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1195         {
1196                 force = inflictor.velocity;
1197                 if(vlen(force) == 0)
1198                         force = '0 0 -1';
1199                 else
1200                         force = normalize(force);
1201                 if(forceintensity >= 0)
1202                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, attacker);
1203                 else
1204                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, attacker);
1205         }
1206
1207         stat_damagedone = 0;
1208         stat_maxdamage = 0;
1209
1210         targ = WarpZone_FindRadius (blastorigin, rad, FALSE);
1211         while (targ)
1212         {
1213                 next = targ.chain;
1214                 if (targ != inflictor)
1215                         if (ignore != targ) if(targ.takedamage)
1216                         {
1217                                 // LordHavoc: measure distance to nearest point on target (not origin)
1218                                 // (this guarentees 100% damage on a touch impact)
1219                                 nearest = targ.WarpZone_findradius_nearest;
1220                                 diff = targ.WarpZone_findradius_dist;
1221                                 // round up a little on the damage to ensure full damage on impacts
1222                                 // and turn the distance into a fraction of the radius
1223                                 power = 1 - ((vlen (diff) - 2) / rad);
1224                                 //bprint(" ");
1225                                 //bprint(ftos(power));
1226                                 //if (targ == attacker)
1227                                 //      print(ftos(power), "\n");
1228                                 if (power > 0)
1229                                 {
1230                                         if (power > 1)
1231                                                 power = 1;
1232                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1233                                         if (finaldmg > 0)
1234                                         {
1235                                                 local float a;
1236                                                 local float c;
1237                                                 local float hits;
1238                                                 local float total;
1239                                                 local float hitratio;
1240                                                 local vector hitloc;
1241                                                 local vector myblastorigin;
1242                                                 myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
1243                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1244                                                 // if it's a player, use the view origin as reference
1245                                                 if (targ.classname == "player")
1246                                                         center = targ.origin + targ.view_ofs;
1247                                                 force = normalize(center - myblastorigin);
1248                                                 force = force * (finaldmg / coredamage) * forceintensity;
1249                                                 // test line of sight to multiple positions on box,
1250                                                 // and do damage if any of them hit
1251                                                 hits = 0;
1252                                                 if (targ.classname == "player")
1253                                                         total = ceil(bound(1, finaldmg, 50));
1254                                                 else
1255                                                         total = ceil(bound(1, finaldmg/10, 5));
1256                                                 hitloc = nearest;
1257                                                 c = 0;
1258                                                 while (c < total)
1259                                                 {
1260                                                         //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1261                                                         WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1262                                                         if (trace_fraction == 1 || trace_ent == targ)
1263                                                         {
1264                                                                 hits = hits + 1;
1265                                                                 if (hits > 1)
1266                                                                         hitloc = hitloc + nearest;
1267                                                                 else
1268                                                                         hitloc = nearest;
1269                                                         }
1270                                                         nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1271                                                         nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1272                                                         nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1273                                                         c = c + 1;
1274                                                 }
1275                                                 nearest = hitloc * (1 / max(1, hits));
1276                                                 hitratio = (hits / total);
1277                                                 a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1278                                                 finaldmg = finaldmg * a;
1279                                                 a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1280                                                 force = force * a;
1281                                                 //if (targ == attacker)
1282                                                 //{
1283                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1284                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1285                                                 //      print(" (", ftos(a), ")\n");
1286                                                 //}
1287                                                 if(hits || tfloordmg || tfloorforce)
1288                                                 {
1289                                                         if(targ.iscreature)
1290                                                         {
1291                                                                 total_damage_to_creatures += finaldmg;
1292
1293                                                                 if(targ.flags & FL_CLIENT)
1294                                                                 if(targ.deadflag == DEAD_NO)
1295                                                                 if(targ != attacker)
1296                                                                 if(!teamplay || targ.team != attacker.team)
1297                                                                 {
1298                                                                         stat_damagedone += finaldmg;
1299                                                                         stat_maxdamage += coredamage;
1300                                                                 }
1301                                                         }
1302
1303                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1304                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1305                                                         else
1306                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1307                                                 }
1308                                         }
1309                                 }
1310                         }
1311                 targ = next;
1312         }
1313
1314         RadiusDamage_running = 0;
1315
1316         Damage_RecordDamage(attacker, deathtype, min(stat_maxdamage, stat_damagedone));
1317
1318         return total_damage_to_creatures;
1319 }
1320
1321 .float fire_damagepersec;
1322 .float fire_endtime;
1323 .float fire_deathtype;
1324 .entity fire_owner;
1325 .float fire_hitsound;
1326 .entity fire_burner;
1327
1328 void fireburner_think();
1329
1330 float Fire_IsBurning(entity e)
1331 {
1332         return (time < e.fire_endtime);
1333 }
1334
1335 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1336 {
1337         float dps;
1338         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1339
1340         if(e.classname == "player")
1341         {
1342                 if(e.deadflag)
1343                         return -1;
1344         }
1345         else
1346         {
1347                 if(!e.fire_burner)
1348                 {
1349                         // print("adding a fire burner to ", e.classname, "\n");
1350                         e.fire_burner = spawn();
1351                         e.fire_burner.classname = "fireburner";
1352                         e.fire_burner.think = fireburner_think;
1353                         e.fire_burner.nextthink = time;
1354                         e.fire_burner.owner = e;
1355                 }
1356         }
1357
1358         t = max(t, 0.1);
1359         dps = d / t;
1360         if(Fire_IsBurning(e))
1361         {
1362                 mintime = e.fire_endtime - time;
1363                 maxtime = max(mintime, t);
1364
1365                 mindps = e.fire_damagepersec;
1366                 maxdps = max(mindps, dps);
1367
1368                 if(maxtime > mintime || maxdps > mindps)
1369                 {
1370                         mindamage = mindps * mintime;
1371                         maxdamage = mindamage + d;
1372
1373                         // interval [mintime, maxtime] * [mindps, maxdps]
1374                         // intersected with
1375                         // [mindamage, maxdamage]
1376                         // maximum of this!
1377
1378                         if(maxdamage >= maxtime * maxdps)
1379                         {
1380                                 totaltime = maxtime;
1381                                 totaldamage = maxtime * maxdps;
1382
1383                                 // this branch increases totaldamage if either t > mintime, or dps > mindps
1384                         }
1385                         else
1386                         {
1387                                 // maxdamage is inside the interval!
1388                                 // first, try to use mindps; only if this fails, increase dps as needed
1389                                 totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
1390                                 totaldamage = maxdamage;
1391                                 // can totaldamage / totaltime be >= maxdps?
1392                                 // max(mindps, maxdamage / maxtime) >= maxdps?
1393                                 // we know maxdamage < maxtime * maxdps
1394                                 // so it cannot be
1395
1396                                 // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
1397                         }
1398
1399                         // total conditions for increasing:
1400                         //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
1401                         // however:
1402                         //     if maxtime = mintime, maxdps = mindps
1403                         // then:
1404                         //     maxdamage = mindamage + d
1405                         //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
1406                         // so the last condition is not needed
1407
1408                         e.fire_damagepersec = totaldamage / totaltime;
1409                         e.fire_endtime = time + totaltime;
1410                         if(totaldamage > 1.2 * mindamage)
1411                         {
1412                                 e.fire_deathtype = dt;
1413                                 if(e.fire_owner != o)
1414                                 {
1415                                         e.fire_owner = o;
1416                                         e.fire_hitsound = FALSE;
1417                                 }
1418                         }
1419                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1420                 }
1421                 else
1422                         return 0;
1423         }
1424         else
1425         {
1426                 e.fire_damagepersec = dps;
1427                 e.fire_endtime = time + t;
1428                 e.fire_deathtype = dt;
1429                 e.fire_owner = o;
1430                 e.fire_hitsound = FALSE;
1431                 return d;
1432         }
1433 }
1434
1435 void Fire_ApplyDamage(entity e)
1436 {
1437         float t, d, hi, ty;
1438         entity o;
1439
1440         if not(Fire_IsBurning(e))
1441                 return;
1442
1443         o = e.owner;
1444         while(o.owner)
1445                 o = o.owner;
1446         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1447                 o = e.fire_owner;
1448
1449         // water and slime stop fire
1450         if(e.waterlevel)
1451         if(e.watertype != CONTENT_LAVA)
1452                 e.fire_endtime = 0;
1453
1454         t = min(frametime, e.fire_endtime - time);
1455         d = e.fire_damagepersec * t;
1456
1457         hi = e.fire_owner.hitsound;
1458         ty = e.fire_owner.typehitsound;
1459         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1460         if(e.fire_hitsound && e.fire_owner)
1461         {
1462                 e.fire_owner.hitsound = hi;
1463                 e.fire_owner.typehitsound = ty;
1464         }
1465         e.fire_hitsound = TRUE;
1466
1467         Damage_RecordDamage(e.fire_owner, e.fire_deathtype, d);
1468
1469         if not(IS_INDEPENDENT_PLAYER(e))
1470         FOR_EACH_PLAYER(other) if(e != other)
1471         {
1472                 if(other.classname == "player")
1473                 if(other.deadflag == DEAD_NO)
1474                 if not(IS_INDEPENDENT_PLAYER(other))
1475                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1476                 {
1477                         t = cvar("g_balance_firetransfer_time") * (e.fire_endtime - time);
1478                         d = cvar("g_balance_firetransfer_damage") * e.fire_damagepersec * t;
1479                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1480                 }
1481         }
1482 }
1483
1484 void Fire_ApplyEffect(entity e)
1485 {
1486         if(Fire_IsBurning(e))
1487                 e.effects |= EF_FLAME;
1488         else
1489                 e.effects &~= EF_FLAME;
1490 }
1491
1492 void fireburner_think()
1493 {
1494         // for players, this is done in the regular loop
1495         if(wasfreed(self.owner))
1496         {
1497                 remove(self);
1498                 return;
1499         }
1500         Fire_ApplyEffect(self.owner);
1501         if(!Fire_IsBurning(self.owner))
1502         {
1503                 self.owner.fire_burner = world;
1504                 remove(self);
1505                 return;
1506         }
1507         Fire_ApplyDamage(self.owner);
1508         self.nextthink = time;
1509 }