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