[NOT FOR 2.5.2] fireball weapon: code changes, cfgs
[divverent/nexuiz.git] / data / 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 void UpdateFrags(entity player, float f)
90 {
91         PlayerTeamScore_AddScore(player, f);
92 }
93
94 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
95 void GiveFrags (entity attacker, entity targ, float f)
96 {
97         // TODO route through PlayerScores instead
98         if(gameover) return;
99
100         if(f < 0)
101         {
102                 if(targ == attacker)
103                 {
104                         // suicide
105                         PlayerScore_Add(attacker, SP_SUICIDES, 1);
106                 }
107                 else
108                 {
109                         // teamkill
110                         PlayerScore_Add(attacker, SP_KILLS, -1); // or maybe add a teamkills field?
111                 }
112         }
113         else
114         {
115                 // regular frag
116                 PlayerScore_Add(attacker, SP_KILLS, 1);
117         }
118
119         PlayerScore_Add(targ, SP_DEATHS, 1);
120
121         if(g_arena)
122                 if(cvar("g_arena_roundbased"))
123                         return;
124
125         // FIXME fix the mess this is (we have REAL points now!)
126         if(g_runematch)
127         {
128                 f = RunematchHandleFrags(attacker, targ, f);
129         }
130         else if(g_keyhunt)
131         {
132                 f = kh_HandleFrags(attacker, targ, f);
133         }
134         else if(g_lms)
135         {
136                 // remove a life
137                 float tl;
138                 tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
139                 if(tl < lms_lowest_lives)
140                         lms_lowest_lives = tl;
141                 if(tl <= 0)
142                 {
143                         if(!lms_next_place)
144                                 lms_next_place = player_count;
145                         PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
146                         --lms_next_place;
147                 }
148                 f = 0;
149         }
150         else if(g_ctf)
151         {
152                 if(g_ctf_ignore_frags)
153                         f = 0;
154         }
155
156         attacker.totalfrags += f;
157
158         if(f)
159                 UpdateFrags(attacker, f);
160 }
161
162 string AppendItemcodes(string s, entity player)
163 {
164         float w;
165         w = player.weapon;
166         //if(w == 0)
167         //      w = player.switchweapon;
168         if(w == 0)
169                 w = player.cnt; // previous weapon!
170         s = strcat(s, ftos(w));
171         if(time < player.strength_finished)
172                 s = strcat(s, "S");
173         if(time < player.invincible_finished)
174                 s = strcat(s, "I");
175         if(player.flagcarried != world)
176                 s = strcat(s, "F");
177         if(player.BUTTON_CHAT)
178                 s = strcat(s, "T");
179         if(player.kh_next)
180                 s = strcat(s, "K");
181         if(player.runes)
182                 s = strcat(s, "|", ftos(player.runes));
183         return s;
184 }
185
186 void LogDeath(string mode, float deathtype, entity killer, entity killed)
187 {
188         string s;
189         if(!cvar("sv_eventlog"))
190                 return;
191         s = strcat(":kill:", mode);
192         s = strcat(s, ":", ftos(killer.playerid));
193         s = strcat(s, ":", ftos(killed.playerid));
194         s = strcat(s, ":type=", ftos(deathtype));
195         s = strcat(s, ":items=");
196         s = AppendItemcodes(s, killer);
197         if(killed != killer)
198         {
199                 s = strcat(s, ":victimitems=");
200                 s = AppendItemcodes(s, killed);
201         }
202         GameLogEcho(s);
203 }
204
205 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
206 {
207         string  s, a;
208         float p, w;
209
210         if (targ.classname == "player" || targ.classname == "corpse")
211         {
212                 if (targ.classname == "corpse")
213                         s = "A corpse";
214                 else
215                         s = targ.netname;
216                 a = attacker.netname;
217
218                 if (targ == attacker)
219                 {
220                         if (deathtype == DEATH_TEAMCHANGE) {
221                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You are now on: ", ColoredTeamName(targ.team)));
222                         } else if (deathtype == DEATH_AUTOTEAMCHANGE) {
223                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(targ.team)));
224                                 return;
225                         } else if (deathtype == DEATH_CAMP) {
226                                 if(sv_gentle)
227                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Reconsider your tactics, camper!"));
228                                 else
229                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Die camper!"));
230                         } else if (deathtype == DEATH_NOAMMO) {
231                                 if(sv_gentle)
232                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You are reinserted into the game for running out of ammo..."));
233                                 else
234                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You were killed for running out of ammo..."));
235                         } else if (deathtype == DEATH_ROT) {
236                                 if(sv_gentle)
237                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to preserve your health"));
238                                 else
239                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You grew too old without taking your medicine"));
240                         } else if (deathtype == DEATH_MIRRORDAMAGE) {
241                                 if(sv_gentle)
242                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't go against team mates!"));
243                                 else
244                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Don't shoot your team mates!"));
245                         } else {
246                                 if(sv_gentle)
247                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You need to be more careful!"));
248                                 else
249                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1You killed your own dumb self!"));
250                         }
251
252                         if(sv_gentle) {
253                                 if (deathtype == DEATH_CAMP)
254                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
255                                 else if (deathtype == DEATH_MIRRORDAMAGE)
256                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
257                                 else
258                                         bprint ("^1",s, "^1 will be reinserted into the game due to his own actions\n");
259
260                                 if(deathtype != DEATH_TEAMCHANGE)
261                                 {
262                                         LogDeath("suicide", deathtype, targ, targ);
263                                         GiveFrags(attacker, targ, -1);
264                                 }
265                                 if (targ.killcount > 2)
266                                         bprint ("^1",s,"^1 faded after a ",ftos(targ.killcount)," point spree\n");
267                         } else {
268                                 w = DEATH_WEAPONOF(deathtype);
269                                 if(WEP_VALID(w))
270                                 {
271                                         w_deathtypestring = "couldn't resist the urge to self-destruct";
272                                         w_deathtype = deathtype;
273                                         weapon_action(w, WR_SUICIDEMESSAGE);
274                                         bprint("^1", s, "^1 ", w_deathtypestring, "\n");
275                                 }
276                                 else if (deathtype == DEATH_KILL)
277                                         bprint ("^1",s, "^1 couldn't take it anymore\n");
278                                 else if (deathtype == DEATH_ROT)
279                                         bprint ("^1",s, "^1 died\n");
280                                 else if (deathtype == DEATH_NOAMMO)
281                                         bprint ("^7",s, "^7 committed suicide. What's the point of living without ammo?\n");
282                                 else if (deathtype == DEATH_CAMP)
283                                         bprint ("^1",s, "^1 thought he found a nice camping ground\n");
284                                 else if (deathtype == DEATH_MIRRORDAMAGE)
285                                         bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
286                                 else if (deathtype == DEATH_CHEAT)
287                                         bprint ("^1",s, "^1 unfairly eliminated himself\n");
288                                 else if (deathtype == DEATH_FIRE)
289                                         bprint ("^1",s, "^1 burned to death\n");
290                                 else if (deathtype != DEATH_TEAMCHANGE)
291                                         bprint ("^1",s, "^1 couldn't resist the urge to self-destruct\n");
292
293                                 if(deathtype != DEATH_TEAMCHANGE)
294                                 {
295                                         LogDeath("suicide", deathtype, targ, targ);
296                                         GiveFrags(attacker, targ, -1);
297                                 }
298                                 if (targ.killcount > 2)
299                                         bprint ("^1",s,"^1 ended it all after a ",ftos(targ.killcount)," kill spree\n");
300                         }
301                 }
302                 else if (attacker.classname == "player" || attacker.classname == "gib")
303                 {
304                         if(teams_matter && attacker.team == targ.team)
305                         {
306                                 if(sv_gentle) {
307                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You went against a team mate!"));
308                                         bprint ("^1", a, "^1 took action against a team mate\n");
309                                 } else {
310                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Moron! You fragged ", s, ", a team mate!"));
311                                         bprint ("^1", a, "^1 mows down a team mate\n");
312                                 }
313                                 GiveFrags(attacker, targ, -1);
314                                 if (targ.killcount > 2) {
315                                         if(sv_gentle)
316                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," scoring spree was ended by a team mate!\n");
317                                         else
318                                                 bprint ("^1",s,"'s ^1",ftos(targ.killcount)," kill spree was ended by a team mate!\n");
319                                 }
320                                 if (attacker.killcount > 2) {
321                                         if(sv_gentle)
322                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," scoring spree by going against a team mate\n");
323                                         else
324                                                 bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," kill spree by killing a team mate\n");
325                                 }
326                                 attacker.killcount = 0;
327
328                                 LogDeath("tk", deathtype, attacker, targ);
329                         }
330                         else
331                         {
332                                 string blood_message, victim_message;
333                                 if (!checkrules_firstblood)
334                                 {
335                                         checkrules_firstblood = TRUE;
336                                         if(sv_gentle)
337                                         {
338                                                 bprint("^1",a, "^1 was the first to score", "\n");
339                                                 blood_message = "^1First point\n";
340                                                 //victim_message = "^1First victim\n";  // or First casualty
341                                         }
342                                         else
343                                         {
344                                                 bprint("^1",a, "^1 drew first blood", "\n");
345                                                 blood_message = "^1First blood\n";
346                                                 victim_message = "^1First victim\n";  // or First casualty
347                                         }
348                                 }
349
350                                 if(sv_gentle > 0) {
351                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^4You scored against ^7", s));
352                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, a,"^1 scored against you ^7"));
353                                 } else {
354                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, blood_message, "^4You fragged ^7", s));
355                                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, victim_message, "^1You were fragged by ^7", a));
356                                         attacker.taunt_soundtime = time + 1;
357                                 }
358
359                                 if(sv_gentle) {
360                                         bprint ("^1",s, "^1 needs a restart thanks to ", a, "\n");
361                                 } else {
362                                         w = DEATH_WEAPONOF(deathtype);
363                                         if(WEP_VALID(w))
364                                         {
365                                                 w_deathtypestring = "was blasted by";
366                                                 w_deathtype = deathtype;
367                                                 weapon_action(w, WR_KILLMESSAGE);
368                                                 p = strstrofs(w_deathtypestring, "#", 0);
369                                                 if(p < 0)
370                                                         bprint("^1", s, "^1 ", w_deathtypestring, " ", a, "\n");
371                                                 else
372                                                         bprint("^1", s, "^1 ", substring(w_deathtypestring, 0, p), a, "^1", substring(w_deathtypestring, p+1, strlen(w_deathtypestring) - (p+1)), "\n");
373                                         }
374                                         else if (deathtype == DEATH_TELEFRAG)
375                                                 bprint ("^1",s, "^1 was telefragged by ", a, "\n");
376                                         else if (deathtype == DEATH_DROWN)
377                                                 bprint ("^1",s, "^1 was drowned by ", a, "\n");
378                                         else if (deathtype == DEATH_SLIME)
379                                                 bprint ("^1",s, "^1 was slimed by ", a, "\n");
380                                         else if (deathtype == DEATH_LAVA)
381                                                 bprint ("^1",s, "^1 was cooked by ", a, "\n");
382                                         else if (deathtype == DEATH_FALL)
383                                                 bprint ("^1",s, "^1 was grounded by ", a, "\n");
384                                         else if (deathtype == DEATH_SHOOTING_STAR)
385                                                 bprint ("^1",s, "^1 was shot into space by ", a, "\n");
386                                         else if (deathtype == DEATH_SWAMP)
387                                                 bprint ("^1",s, "^1 was conserved by ", a, "\n");
388                                         else if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
389                                         {
390                                                 p = strstrofs(inflictor.message2, "#", 0);
391                                                 if(p < 0)
392                                                         bprint("^1", s, "^1 ", inflictor.message2, " ", a, "\n");
393                                                 else
394                                                         bprint("^1", s, "^1 ", substring(inflictor.message2, 0, p), a, "^1", substring(inflictor.message2, p+1, strlen(inflictor.message2) - (p+1)), "\n");
395                                         }
396                                         else if(deathtype == DEATH_TURRET)
397                                                 bprint ("^1",s, "^1 was pushed into the line of fire by ^1", a, "\n");
398                                         else if(deathtype == DEATH_TOUCHEXPLODE)
399                                                 bprint ("^1",s, "^1 was pushed into an accident by ^1", a, "\n");
400                                         else if(deathtype == DEATH_CHEAT)
401                                                 bprint ("^1",s, "^1 was unfairly eliminated by ^1", a, "\n");
402                                         else if (deathtype == DEATH_FIRE)
403                                                 bprint ("^1",s, "^1 was burnt to death by ^1", a, "\n");
404                                         else
405                                                 bprint ("^1",s, "^1 was fragged by ", a, "\n");
406                                 }
407
408                                 if(g_ctf && targ.flagcarried)
409                                 {
410                                         UpdateFrags(attacker, ctf_score_value("score_kill"));
411                                         PlayerScore_Add(attacker, SP_CTF_FCKILLS, 1);
412                                         GiveFrags(attacker, targ, 0); // for logging
413                                 }
414                                 else
415                                         GiveFrags(attacker, targ, 1);
416
417                                 if (targ.killcount > 2) {
418                                         if(sv_gentle)
419                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " scoring spree was ended by ", a, "\n");
420                                         else
421                                                 bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
422                                 }
423
424                                 attacker.killcount = attacker.killcount + 1;
425
426                                 if (attacker.killcount > 2) {
427                                         if(sv_gentle)
428                                                 bprint ("^1",a,"^1 made ",ftos(attacker.killcount)," scores in a row\n");
429                                         else
430                                                 bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
431                                 }
432
433                                 LogDeath("frag", deathtype, attacker, targ);
434
435                                 if (attacker.killcount == 3)
436                                 {
437                                         if(sv_gentle) {
438                                                 bprint (a,"^7 made a ^1TRIPLE SCORE\n");
439                                         } else {
440                                                 bprint (a,"^7 made a ^1TRIPLE FRAG\n");
441                                                 announce(attacker, "announcer/male/03kills.wav");
442                                         }
443                                 }
444                                 else if (attacker.killcount == 5)
445                                 {
446                                         if(sv_gentle) {
447                                                 bprint (a,"^7 unleashes ^1SCORING RAGE\n");
448                                         } else {
449                                                 bprint (a,"^7 unleashes ^1RAGE\n");
450                                                 announce(attacker, "announcer/male/05kills.wav");
451                                         }
452                                 }
453                                 else if (attacker.killcount == 10)
454                                 {
455                                         if(sv_gentle) {
456                                                 bprint (a,"^7 made ^1TEN SCORES IN A ROW!\n");
457                                         } else {
458                                                 bprint (a,"^7 starts the ^1MASSACRE!\n");
459                                                 announce(attacker, "announcer/male/10kills.wav");
460                                         }
461                                 }
462                                 else if (attacker.killcount == 15)
463                                 {
464                                         if(sv_gentle) {
465                                                 bprint (a,"^7 made ^1FIFTEEN SCORES IN A ROW!\n");
466                                         } else {
467                                                 bprint (a,"^7 executes ^1MAYHEM!\n");
468                                                 announce(attacker, "announcer/male/15kills.wav");
469                                         }
470                                 }
471                                 else if (attacker.killcount == 20)
472                                 {
473                                         if(sv_gentle) {
474                                                 bprint (a,"^7 made ^1TWENTY SCORES IN A ROW!\n");
475                                         } else {
476                                                 bprint (a,"^7 is a ^1BERSERKER!\n");
477                                                 announce(attacker, "announcer/male/20kills.wav");
478                                         }
479                                 }
480                                 else if (attacker.killcount == 25)
481                                 {
482                                         if(sv_gentle) {
483                                                 bprint (a,"^7 made ^1TWENTY FIFE SCORES IN A ROW!\n");
484                                         } else {
485                                                 bprint (a,"^7 inflicts ^1CARNAGE!\n");
486                                                 announce(attacker, "announcer/male/25kills.wav");
487                                         }
488                                 }
489                                 else if (attacker.killcount == 30)
490                                 {
491                                         if(sv_gentle) {
492                                                 bprint (a,"^7 made ^1THIRTY SCORES IN A ROW!\n");
493                                         } else {
494                                                 bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
495                                                 announce(attacker, "announcer/male/30kills.wav");
496                                         }
497                                 }
498                         }
499                 }
500                 else
501                 {
502                         centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^1Watch your step!"));
503                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
504                                 bprint ("^1",s, "^1 ", inflictor.message, "\n");
505                         else if (deathtype == DEATH_DROWN)
506                                 if(sv_gentle)
507                                         bprint ("^1",s, "^1 was in the water for too long\n");
508                                 else
509                                         bprint ("^1",s, "^1 drowned\n");
510                         else if (deathtype == DEATH_SLIME)
511                                 bprint ("^1",s, "^1 was slimed\n");
512                         else if (deathtype == DEATH_LAVA)
513                                 if(sv_gentle)
514                                         bprint ("^1",s, "^1 found a hot place\n");
515                                 else
516                                         bprint ("^1",s, "^1 turned into hot slag\n");
517                         else if (deathtype == DEATH_FALL)
518                                 if(sv_gentle)
519                                         bprint ("^1",s, "^1 tested gravity (and it worked)\n");
520                                 else
521                                         bprint ("^1",s, "^1 hit the ground with a crunch\n");
522                         else if (deathtype == DEATH_SHOOTING_STAR)
523                                 bprint ("^1",s, "^1 became a shooting star\n");
524                         else if (deathtype == DEATH_SWAMP)
525                                 if(sv_gentle)
526                                         bprint ("^1",s, "^1 discovered a swamp\n");
527                                 else
528                                         bprint ("^1",s, "^1 is now conserved for centuries to come\n");
529                         else if(deathtype == DEATH_TURRET)
530                                 bprint ("^1",s, "^1 was mowed down by a turret \n");
531                         else if(deathtype == DEATH_TOUCHEXPLODE)
532                                 bprint ("^1",s, "^1 died in an accident\n");
533                         else if(deathtype == DEATH_CHEAT)
534                                 bprint ("^1",s, "^1 was unfairly eliminated\n");
535                         else if(deathtype == DEATH_FIRE)
536                                 if(sv_gentle)
537                                         bprint ("^1",s, "^1 felt a little hot\n");
538                                 else
539                                         bprint ("^1",s, "^1 burnt to death\n");
540                         else
541                                 if(sv_gentle)
542                                         bprint ("^1",s, "^1 needs a restart\n");
543                                 else
544                                         bprint ("^1",s, "^1 died\n");
545                         GiveFrags(targ, targ, -1);
546                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
547                                 announce(targ, "announcer/male/botlike.wav");
548                         }
549
550                         if (targ.killcount > 2)
551                                 if(sv_gentle)
552                                         bprint ("^1",s,"^1 needs a restart after a ",ftos(targ.killcount)," scoring spree\n");
553                                 else
554                                         bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
555
556                         LogDeath("accident", deathtype, targ, targ);
557                 }
558                 targ.death_origin = targ.origin;
559                 if(targ != attacker)
560                         targ.killer_origin = attacker.origin;
561                 // FIXME: this should go in PutClientInServer
562                 if (targ.killcount)
563                         targ.killcount = 0;
564         }
565 }
566
567 // these are updated by each Damage call for use in button triggering and such
568 entity damage_targ;
569 entity damage_inflictor;
570 entity damage_attacker;
571
572 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
573 {
574         float mirrordamage;
575         float mirrorforce;
576         float teamdamage0;
577         entity attacker_save;
578         mirrordamage = 0;
579         mirrorforce = 0;
580
581         if (gameover || targ.killcount == -666)
582                 return;
583
584         local entity oldself;
585         oldself = self;
586         self = targ;
587         damage_targ = targ;
588         damage_inflictor = inflictor;
589         damage_attacker = attacker;
590                 attacker_save = attacker;
591
592         if(targ.classname == "player")
593                 if(targ.hook)
594                         if(targ.hook.aiment)
595                                 if(targ.hook.aiment == attacker)
596                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
597
598         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
599         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
600         {
601                 if(targ.classname == "player")
602                         if not(IsDifferentTeam(targ, attacker))
603                         {
604                                 self = oldself;
605                                 return;
606                         }
607         }
608
609         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
610         {
611                 // These are ALWAYS lethal
612                 // No damage modification here
613                 // Instead, prepare the victim for his death...
614                 targ.armorvalue = 0;
615                 targ.spawnshieldtime = 0;
616                 targ.health = 0.9; // this is < 1
617                 targ.flags -= targ.flags & FL_GODMODE;
618                 damage = 100000;
619         }
620         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
621         {
622                 // no processing
623         }
624         else
625         {
626                 if (targ.classname == "player")
627                 if (attacker.classname == "player")
628                 if (!targ.isbot)
629                 if (attacker.isbot)
630                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
631
632                 // nullify damage if teamplay is on
633                 if(deathtype != DEATH_TELEFRAG)
634                 if(attacker.classname == "player")
635                 {
636                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
637                         {
638                                 damage = 0;
639                                 force = '0 0 0';
640                         }
641                         else if(attacker.team == targ.team)
642                         {
643                                 if(teamplay == 1)
644                                         damage = 0;
645                                 else if(attacker != targ)
646                                 {
647                                         if(teamplay == 3)
648                                                 damage = 0;
649                                         else if(teamplay == 4)
650                                         {
651                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
652                                                 {
653                                                         teamdamage0 = max(attacker.dmg_team, cvar("g_teamdamage_threshold"));
654                                                         attacker.dmg_team = attacker.dmg_team + damage;
655                                                         if(attacker.dmg_team > teamdamage0)
656                                                                 mirrordamage = cvar("g_mirrordamage") * (attacker.dmg_team - teamdamage0);
657                                                         mirrorforce = cvar("g_mirrordamage") * vlen(force);
658                                                         if(g_minstagib)
659                                                         {
660                                                                 if(cvar("g_friendlyfire") == 0)
661                                                                         damage = 0;
662                                                         }
663                                                         else
664                                                                 damage = cvar("g_friendlyfire") * damage;
665                                                         // mirrordamage will be used LATER
666                                                 }
667                                                 else
668                                                         damage = 0;
669                                         }
670                                 }
671                         }
672                 }
673
674                 if(targ.classname == "player")
675                 if(attacker.classname == "player")
676                 if(attacker != targ)
677                 {
678                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
679                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
680                 }
681
682                 if(targ.classname == "player")
683                 if (g_minstagib)
684                 {
685                         if ((deathtype == DEATH_FALL)  ||
686                                 (deathtype == DEATH_DROWN) ||
687                                 (deathtype == DEATH_SLIME) ||
688                                 (deathtype == DEATH_LAVA))
689                         {
690                                 self = oldself;
691                                 return;
692                         }
693                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
694                         {
695                                 targ.armorvalue -= 1;
696                                 centerprint(targ, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(targ.armorvalue)));
697                                 damage = 0;
698                                 targ.hitsound += 1;
699                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
700                         }
701                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
702                         {
703                                 damage = 0;
704                                 if (targ != attacker)
705                                 {
706                                         if (targ.classname == "player")
707                                                 centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "Secondary fire inflicts no damage!"));
708                                         damage = 0;
709                                         mirrordamage = 0;
710                                         force = '0 0 0';
711                                         // keep mirrorforce
712                                         attacker = targ;
713                                 }
714                         }
715                 }
716
717                 if not(DEATH_ISSPECIAL(deathtype))
718                 {
719                         damage *= g_weapondamagefactor;
720                         mirrordamage *= g_weapondamagefactor;
721                         force = force * g_weaponforcefactor;
722                         mirrorforce *= g_weaponforcefactor;
723                 }
724
725                 // apply strength multiplier
726                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
727                 {
728                         if(targ == attacker)
729                         {
730                                 damage = damage * cvar("g_balance_powerup_strength_selfdamage");
731                                 force = force * cvar("g_balance_powerup_strength_selfforce");
732                         }
733                         else
734                         {
735                                 damage = damage * cvar("g_balance_powerup_strength_damage");
736                                 force = force * cvar("g_balance_powerup_strength_force");
737                         }
738                 }
739
740                 // apply invincibility multiplier
741                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
742                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
743
744                 if (targ == attacker)
745                         damage = damage * cvar("g_balance_selfdamagepercent");  // Partial damage if the attacker hits himself
746
747                 // CTF: reduce damage/force
748                 if(g_ctf)
749                 if(targ == attacker)
750                 if(targ.flagcarried)
751                 {
752                         damage = damage * cvar("g_ctf_flagcarrier_selfdamage");
753                         force = force * cvar("g_ctf_flagcarrier_selfforce");
754                 }
755
756                 if(g_runematch)
757                 {
758                         // apply strength rune
759                         if (attacker.runes & RUNE_STRENGTH)
760                         {
761                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
762                                 {
763                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
764                                         force = force * cvar("g_balance_rune_strength_combo_force");
765                                 }
766                                 else
767                                 {
768                                         damage = damage * cvar("g_balance_rune_strength_damage");
769                                         force = force * cvar("g_balance_rune_strength_force");
770                                 }
771                         }
772                         else if (attacker.runes & CURSE_WEAK)
773                         {
774                                 damage = damage * cvar("g_balance_curse_weak_damage");
775                                 force = force * cvar("g_balance_curse_weak_force");
776                         }
777
778                         // apply defense rune
779                         if (targ.runes & RUNE_DEFENSE)
780                         {
781                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
782                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
783                                 else
784                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
785                         }
786                         else if (targ.runes & CURSE_VULNER)
787                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
788                 }
789
790                 // count the damage
791                 if(attacker)
792                 if(!targ.deadflag)
793                 if(targ.takedamage == DAMAGE_AIM)
794                 if(targ != attacker)
795                 if(targ.classname == "player")
796                 {
797                         if(IsDifferentTeam(targ, attacker))
798                         {
799                                 if(damage > 0)
800                                 {
801                                         if(targ.BUTTON_CHAT)
802                                                 attacker.typehitsound += 1;
803                                         else
804                                                 attacker.hitsound += 1;
805
806                                         damage_goodhits += 1;
807                                         damage_gooddamage += damage;
808
809                                         if not(DEATH_ISSPECIAL(deathtype))
810                                         {
811                                                 if(!g_minstagib)
812                                                 if(IsFlying(targ))
813                                                         yoda = 1;
814
815                                                 if(g_minstagib)
816                                                 if(targ.items & IT_STRENGTH)
817                                                         yoda = 1;
818
819                                                 // HEAD SHOT:
820                                                 // find height of hit on player axis
821                                                 // if above view_ofs and below maxs, and also in the middle half of the bbox, it is head shot
822                                                 vector headmins, headmaxs, org;
823                                                 org = antilag_takebackorigin(targ, time - ANTILAG_LATENCY(attacker));
824                                                 headmins = org + '0.7 0 0' * targ.mins_x + '0 0.7 0' * targ.mins_y + '0 0 1' * (1.5 * targ.view_ofs_z - 0.5 * targ.maxs_z);
825                                                 headmaxs = org + '0.7 0 0' * targ.maxs_x + '0 0.7 0' * targ.maxs_y + '0 0 1' * targ.maxs_z;
826                                                 if(trace_hits_box(railgun_start, railgun_end, headmins, headmaxs))
827                                                 {
828                                                         damage *= 1 + damage_headshotbonus;
829                                                         headshot = 1;
830                                                         deathtype |= HITTYPE_HEADSHOT;
831                                                 }
832                                         }
833                                 }
834                         }
835                         else
836                         {
837                                 if(deathtype != DEATH_FIRE)
838                                         attacker.typehitsound += 1;
839                                 if(mirrordamage > 0)
840                                         if(time > attacker.teamkill_complain)
841                                         {
842                                                 attacker.teamkill_complain = time + 5;
843                                                 attacker.teamkill_soundtime = time + 0.4;
844                                                 attacker.teamkill_soundsource = targ;
845                                         }
846                         }
847                 }
848         }
849
850         // apply push
851         if (self.damageforcescale)
852         if (vlen(force))
853         {
854                 self.velocity = self.velocity + self.damageforcescale * force;
855                 self.flags &~= FL_ONGROUND;
856                 UpdateCSQCProjectile(self);
857         }
858         // apply damage
859         if (damage != 0 || (self.damageforcescale && vlen(force)))
860         if (self.event_damage)
861                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
862         self = oldself;
863
864         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
865         {
866                 // Savage: vampire mode
867                 if (g_vampire)
868                 if (!g_minstagib)
869                 if (time > self.spawnshieldtime)
870                 {
871                         attacker.health += damage;
872                 }
873                 if(g_runematch)
874                 {
875                         if (attacker.runes & RUNE_VAMPIRE)
876                         {
877                         // apply vampire rune
878                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
879                                 {
880                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
881                                         attacker.health = bound(
882                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
883                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
884                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
885                                 }
886                                 else
887                                 {
888                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
889                                         attacker.health = bound(
890                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
891                                                                                         // empathy won't let you gain health in the same way...
892                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
893                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
894                                         }
895                         }
896                         // apply empathy curse
897                         else if (attacker.runes & CURSE_EMPATHY)
898                         {
899                                 attacker.health = bound(
900                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
901                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
902                                         attacker.health);
903                         }
904                 }
905         }
906
907         // apply mirror damage if any
908         if(mirrordamage > 0 || mirrorforce > 0)
909         {
910                 attacker = attacker_save;
911                 if(g_minstagib)
912                         if(mirrordamage > 0)
913                         {
914                                 // just lose extra LIVES, don't kill the player for mirror damage
915                                 if(attacker.armorvalue > 0)
916                                 {
917                                         attacker.armorvalue = attacker.armorvalue - 1;
918                                         centerprint(attacker, strcat(DAMAGE_CENTERPRINT_SPACER, "^3Remaining extra lives: ",ftos(attacker.armorvalue)));
919                                         attacker.hitsound += 1;
920                                 }
921                                 mirrordamage = 0;
922                         }
923                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
924                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
925         }
926 }
927
928 vector NearestPointOnBox(entity box, vector org)
929 {
930         vector m1, m2, nearest;
931
932         m1 = box.mins + box.origin;
933         m2 = box.maxs + box.origin;
934
935         nearest_x = bound(m1_x, org_x, m2_x);
936         nearest_y = bound(m1_y, org_y, m2_y);
937         nearest_z = bound(m1_z, org_z, m2_z);
938
939         return nearest;
940 }
941
942 .float actual_damage[WEP_COUNT]; //amount of damage done
943 .float max_damage[WEP_COUNT]; //the maximum damage of the weapon
944
945 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(actual_damage);
946 FTEQCC_YOU_SUCK_THIS_IS_NOT_UNREFERENCED(max_damage);
947
948
949 float RadiusDamage_running;
950 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
951 // Returns total damage applies to creatures
952 {
953         entity  targ;
954         float   finaldmg;
955         float   power;
956         vector  blastorigin;
957         vector  force;
958         vector  diff;
959         vector  center;
960         vector  nearest;
961         float   total_damage_to_creatures;
962         entity  next;
963         float weaponid;
964
965         float stat_damagedone;
966         float stat_maxdamage;
967
968         if(RadiusDamage_running)
969         {
970                 string save;
971                 print("RadiusDamage called recursively!\n");
972                 print("Expect stuff to go HORRIBLY wrong.\n");
973                 print("Causing a stack trace...\n");
974                 save = cvar_string("prvm_backtraceforwarnings");
975                 cvar_set("prvm_backtraceforwarnings", "1");
976                 fclose(-1); // calls VM_Warning
977                 cvar_set("prvm_backtraceforwarnings", save);
978                 return 0;
979         }
980
981
982         RadiusDamage_running = 1;
983
984         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
985         total_damage_to_creatures = 0;
986
987         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
988         {
989                 force = inflictor.velocity;
990                 if(vlen(force) == 0)
991                         force = '0 0 -1';
992                 else
993                         force = normalize(force);
994                 if(forceintensity >= 0)
995                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, attacker);
996                 else
997                         Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, attacker);
998         }
999
1000         stat_damagedone = 0;
1001         stat_maxdamage = 0;
1002
1003         targ = findradius (blastorigin, rad);
1004         while (targ)
1005         {
1006                 next = targ.chain;
1007                 if (targ != inflictor)
1008                         if (ignore != targ)
1009                         {
1010                                 // LordHavoc: measure distance to nearest point on target (not origin)
1011                                 // (this guarentees 100% damage on a touch impact)
1012                                 nearest = NearestPointOnBox(targ, blastorigin);
1013                                 diff = nearest - blastorigin;
1014                                 // round up a little on the damage to ensure full damage on impacts
1015                                 // and turn the distance into a fraction of the radius
1016                                 power = 1 - ((vlen (diff) - 2) / rad);
1017                                 //bprint(" ");
1018                                 //bprint(ftos(power));
1019                                 if (power > 0)
1020                                 {
1021                                         if (power > 1)
1022                                                 power = 1;
1023                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1024                                         if (finaldmg > 0)
1025                                         {
1026                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
1027                                                 // if it's a player, use the view origin as reference
1028                                                 if (targ.classname == "player")
1029                                                         center = targ.origin + targ.view_ofs;
1030                                                 force = normalize(center - blastorigin) * (finaldmg / coredamage) * forceintensity;
1031                                                 // test line of sight to multiple positions on box,
1032                                                 // and do damage if any of them hit
1033                                                 local float c;
1034                                                 c = ceil(finaldmg / 10);
1035                                                 if (c > 20)
1036                                                         c = 20;
1037                                                 while (c > 0)
1038                                                 {
1039                                                         c = c - 1;
1040                                                         traceline(blastorigin, nearest, TRUE, inflictor);
1041                                                         if (trace_fraction == 1 || trace_ent == targ
1042                                                             || cvar("g_throughfloor"))
1043                                                         {
1044                                                                 if(targ.iscreature)
1045                                                                 {
1046                                                                         total_damage_to_creatures += finaldmg;
1047
1048                                                                         if(targ.flags & FL_CLIENT)
1049                                                                         if(targ.deadflag == DEAD_NO)
1050                                                                         if(targ != attacker)
1051                                                                         if(!teamplay || targ.team != attacker.team)
1052                                                                         {
1053                                                                                 stat_damagedone += finaldmg;
1054                                                                                 stat_maxdamage += coredamage;
1055                                                                         }
1056                                                                 }
1057
1058                                                                 if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1059                                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1060                                                                 else
1061                                                                         Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1062                                                                 break;
1063                                                         }
1064                                                         nearest_x = targ.mins_x + random() * targ.size_x;
1065                                                         nearest_y = targ.mins_y + random() * targ.size_y;
1066                                                         nearest_z = targ.mins_z + random() * targ.size_z;
1067                                                 }
1068                                         }
1069                                 }
1070                         }
1071                 targ = next;
1072         }
1073
1074         RadiusDamage_running = 0;
1075
1076         weaponid = DEATH_WEAPONOF(deathtype);
1077
1078         // Calculates stats for all RadiusDamage weapons
1079         if not(inWarmupStage)
1080         if(weaponid)
1081         if(clienttype(attacker) == CLIENTTYPE_REAL)
1082         {
1083                 // Track damage done and update the stat to be sent later in g_world.qc
1084                 float f = min(stat_maxdamage, stat_damagedone);
1085                 attacker.actual_damage[weaponid] += f; // value temporarily stored in f due to a ftqecc bug
1086                 attacker.damage_hits = weaponid + 64 * rint(attacker.actual_damage[weaponid]);
1087         }
1088
1089         return total_damage_to_creatures;
1090 }
1091
1092 .float fire_damagepersec;
1093 .float fire_endtime;
1094 .float fire_deathtype;
1095 .entity fire_owner;
1096 .float fire_hitsound;
1097
1098 float Fire_IsBurning(entity e)
1099 {
1100         return (time < e.fire_endtime);
1101 }
1102
1103 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1104 {
1105         float dps;
1106         t = max(t, 0.1);
1107         dps = d / t;
1108         if(Fire_IsBurning(e))
1109         {
1110                 float totaldamage, totaltime, maxdps, olddamage, oldtime;
1111                 oldtime = e.fire_endtime - time;
1112                 olddamage = e.fire_damagepersec * oldtime;
1113                 if(t > oldtime + 0.1 || d > olddamage + 1 || dps > e.fire_damagepersec + 1) // some thresholds to prevent toggling due to roundoff errors
1114                 {
1115                         maxdps = max(dps, e.fire_damagepersec);
1116                         totaltime = max(oldtime, t);
1117                         totaldamage = min(maxdps * totaltime, olddamage + d);
1118                         e.fire_damagepersec = totaldamage / totaltime;
1119                         e.fire_endtime = time + totaltime;
1120                         e.fire_deathtype = dt;
1121                         e.fire_owner = o;
1122                         e.fire_hitsound = FALSE;
1123                         return totaldamage - olddamage; // can never be negative
1124                 }
1125                 else
1126                         return 0;
1127         }
1128         else
1129         {
1130                 e.fire_damagepersec = dps;
1131                 e.fire_endtime = time + t;
1132                 e.fire_deathtype = dt;
1133                 e.fire_owner = o;
1134                 e.fire_hitsound = FALSE;
1135                 return d;
1136         }
1137 }
1138
1139 void Fire_ApplyDamage(entity e)
1140 {
1141         float t, d, hi, ty;
1142
1143         if not(Fire_IsBurning(e))
1144                 return;
1145
1146         // water and slime stop fire
1147         if(e.waterlevel)
1148         if(e.watertype != CONTENT_LAVA)
1149                 e.fire_endtime = 0;
1150
1151         t = min(frametime, e.fire_endtime - time);
1152         d = e.fire_damagepersec * t;
1153
1154         hi = e.fire_owner.hitsound;
1155         ty = e.fire_owner.typehitsound;
1156         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1157         if(e.fire_hitsound && e.fire_owner)
1158         {
1159                 e.fire_owner.hitsound = hi;
1160                 e.fire_owner.typehitsound = ty;
1161         }
1162         e.fire_hitsound = TRUE;
1163
1164         if not(IS_INDEPENDENT_PLAYER(e))
1165         FOR_EACH_PLAYER(other) if(e != other)
1166         {
1167                 if(other.classname == "player")
1168                 if(other.deadflag == DEAD_NO)
1169                 if not(IS_INDEPENDENT_PLAYER(other))
1170                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1171                 {
1172                         t = cvar("g_balance_firetransfer_time") * (e.fire_endtime - time);
1173                         d = cvar("g_balance_firetransfer_damage") * e.fire_damagepersec * t;
1174                         Fire_AddDamage(other, e, d, t, DEATH_FIRE);
1175                 }
1176         }
1177 }
1178
1179 void Fire_ApplyEffect(entity e)
1180 {
1181         if(Fire_IsBurning(e))
1182                 e.effects |= EF_FLAME;
1183         else
1184                 e.effects &~= EF_FLAME;
1185 }