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