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