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