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