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