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