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