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