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