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