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