]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
Merge remote-tracking branch 'origin/master' into samual/notification_rewrite
[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         WriteByte(MSG_ENTITY, self.species);
18         return TRUE;
19 }
20
21 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, float deathtype, float bloodtype, entity dmgowner)
22 {
23         // TODO maybe call this from non-edgedamage too?
24         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
25
26         entity e;
27
28         if(!sound_allowed(MSG_BROADCAST, dmgowner))
29                 deathtype |= 0x8000;
30
31         e = spawn();
32         setorigin(e, org);
33         e.projectiledeathtype = deathtype;
34         e.dmg = coredamage;
35         e.dmg_edge = edgedamage;
36         e.dmg_radius = rad;
37         e.dmg_force = vlen(force);
38         e.velocity = force;
39         e.oldorigin_x = compressShortVector(e.velocity);
40         e.species = bloodtype;
41
42         Net_LinkEntity(e, FALSE, 0.2, Damage_DamageInfo_SendEntity);
43 }
44
45 float checkrules_firstblood;
46
47 float yoda;
48 float damage_goodhits;
49 float damage_gooddamage;
50
51 .float dmg_team;
52 .float teamkill_complain;
53 .float teamkill_soundtime;
54 .entity teamkill_soundsource;
55 .entity pusher;
56 .float istypefrag;
57 .float taunt_soundtime;
58
59
60 float IsDifferentTeam(entity a, entity b)
61 {
62         if(teamplay)
63         {
64                 if(a.team == b.team)
65                         return 0;
66         }
67         else
68         {
69                 if(a == b)
70                         return 0;
71         }
72         return 1;
73 }
74
75 float IsFlying(entity a)
76 {
77         if(a.flags & FL_ONGROUND)
78                 return 0;
79         if(a.waterlevel >= WATERLEVEL_SWIMMING)
80                 return 0;
81         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
82         if(trace_fraction < 1)
83                 return 0;
84         return 1;
85 }
86
87 void UpdateFrags(entity player, float f)
88 {
89         PlayerTeamScore_AddScore(player, f);
90 }
91
92 // NOTE: f=0 means still count as a (positive) kill, but count no frags for it
93 void W_SwitchWeapon_Force(entity e, float w);
94 entity GiveFrags_randomweapons;
95 void GiveFrags (entity attacker, entity targ, float f, float deathtype)
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                 if(targ.playerid)
118                         PlayerStats_Event(attacker, sprintf("kills-%d", targ.playerid), 1);
119         }
120
121         PlayerScore_Add(targ, SP_DEATHS, 1);
122
123         if(g_arena || g_ca)
124                 if(autocvar_g_arena_roundbased)
125                         return;
126
127         if(targ != attacker) // not for suicides
128         if(g_weaponarena_random)
129         {
130                 // after a frag, exchange the current weapon (or the culprit, if detectable) by a new random weapon
131                 float culprit;
132                 culprit = DEATH_WEAPONOF(deathtype);
133                 if(!culprit)
134                         culprit = attacker.weapon;
135                 else if(!WEPSET_CONTAINS_EW(attacker, culprit))
136                         culprit = attacker.weapon;
137
138                 if(g_weaponarena_random_with_laser && culprit == WEP_LASER)
139                 {
140                         // no exchange
141                 }
142                 else
143                 {
144                         if(!GiveFrags_randomweapons)
145                         {
146                                 GiveFrags_randomweapons = spawn();
147                                 GiveFrags_randomweapons.classname = "GiveFrags_randomweapons";
148                         }
149
150                         if(inWarmupStage)
151                                 WEPSET_COPY_EA(GiveFrags_randomweapons, warmup_start_weapons);
152                         else
153                                 WEPSET_COPY_EA(GiveFrags_randomweapons, start_weapons);
154
155                         // all others (including the culprit): remove
156                         WEPSET_ANDNOT_EE(GiveFrags_randomweapons, attacker);
157                         WEPSET_ANDNOT_EW(GiveFrags_randomweapons, culprit);
158
159                         // among the remaining ones, choose one by random
160                         W_RandomWeapons(GiveFrags_randomweapons, 1);
161
162                         if(!WEPSET_EMPTY_E(GiveFrags_randomweapons))
163                         {
164                                 WEPSET_OR_EE(attacker, GiveFrags_randomweapons);
165                                 WEPSET_ANDNOT_EW(attacker, culprit);
166                         }
167                 }
168
169                 // after a frag, choose another random weapon set
170                 if not(WEPSET_CONTAINS_EW(attacker, attacker.weapon))
171                         W_SwitchWeapon_Force(attacker, w_getbestweapon(attacker));
172         }
173
174         // FIXME fix the mess this is (we have REAL points now!)
175         entity oldself;
176         oldself = self;
177         self = attacker;
178         frag_attacker = attacker;
179         frag_target = targ;
180         frag_score = f;
181         if(MUTATOR_CALLHOOK(GiveFragsForKill))
182         {
183                 f = frag_score;
184                 self = oldself;
185         }
186         else
187         {
188                 self = oldself;
189                 if(g_runematch)
190                 {
191                         f = RunematchHandleFrags(attacker, targ, f);
192                 }
193                 else if(g_lms)
194                 {
195                         // remove a life
196                         float tl;
197                         tl = PlayerScore_Add(targ, SP_LMS_LIVES, -1);
198                         if(tl < lms_lowest_lives)
199                                 lms_lowest_lives = tl;
200                         if(tl <= 0)
201                         {
202                                 if(!lms_next_place)
203                                         lms_next_place = player_count;
204                                 else
205                                         lms_next_place = min(lms_next_place, player_count);
206                                 PlayerScore_Add(targ, SP_LMS_RANK, lms_next_place); // won't ever spawn again
207                                 --lms_next_place;
208                         }
209                         f = 0;
210                 }
211         }
212
213         attacker.totalfrags += f;
214
215         if(f)
216                 UpdateFrags(attacker, f);
217 }
218
219 /*string Obituary_ExtraFragInfo(entity player) // Extra fragmessage information
220 {
221         string health_output = string_null;
222         string ping_output = string_null;
223         string handicap_output = string_null;
224         string output = string_null;
225
226         if(autocvar_sv_fraginfo && ((autocvar_sv_fraginfo == 2) || inWarmupStage))
227         {
228                 // health/armor of attacker (person who killed you)
229                 if(autocvar_sv_fraginfo_stats && (player.health >= 1))
230                         health_output = strcat("^7(Health ^1", ftos(rint(player.health)), "^7 / Armor ^2", ftos(rint(player.armorvalue)), "^7)");
231                 
232                 // ping display
233                 if(autocvar_sv_fraginfo_ping)
234                         ping_output = ((clienttype(player) == CLIENTTYPE_BOT) ? "^2Bot" : strcat("Ping ", ((player.ping >= 150) ? "^1" : "^2"), ftos(rint(player.ping)), "ms"));
235                         
236                 // handicap display 
237                 if(autocvar_sv_fraginfo_handicap) 
238                 {
239                         if(autocvar_sv_fraginfo_handicap == 2)  
240                                 handicap_output = strcat(output, strcat("Handicap ^2", ((player.cvar_cl_handicap <= 1) ? "Off" : ftos(rint(player.cvar_cl_handicap)))));
241                         else if(player.cvar_cl_handicap) // with _handicap 1, only show this if there actually is a handicap enabled.   
242                                 handicap_output = strcat("Handicap ^2", ftos(rint(player.cvar_cl_handicap)));
243                 }
244                 
245                 // format the string
246                 output = strcat(health_output, (health_output ? ((ping_output || handicap_output) ? " ^7(" : "") : ((ping_output || handicap_output) ? "^7(" : "")), 
247                         ping_output, (handicap_output ? "^7 / " : ""), 
248                         handicap_output, ((ping_output || handicap_output) ? "^7)" : ""));
249                 
250                 // add new line to the beginning if there is a message
251                 if(output) { output = strcat("\n", output); }
252         }
253         
254         return output;
255 }*/
256
257 string AppendItemcodes(string s, entity player)
258 {
259         float w;
260         w = player.weapon;
261         //if(w == 0)
262         //      w = player.switchweapon;
263         if(w == 0)
264                 w = player.cnt; // previous weapon!
265         s = strcat(s, ftos(w));
266         if(time < player.strength_finished)
267                 s = strcat(s, "S");
268         if(time < player.invincible_finished)
269                 s = strcat(s, "I");
270         if(player.flagcarried != world)
271                 s = strcat(s, "F");
272         if(player.BUTTON_CHAT)
273                 s = strcat(s, "T");
274         if(player.kh_next)
275                 s = strcat(s, "K");
276         if(player.runes)
277                 s = strcat(s, "|", ftos(player.runes));
278         return s;
279 }
280
281 void LogDeath(string mode, float deathtype, entity killer, entity killed)
282 {
283         string s;
284         if(!autocvar_sv_eventlog)
285                 return;
286         s = strcat(":kill:", mode);
287         s = strcat(s, ":", ftos(killer.playerid));
288         s = strcat(s, ":", ftos(killed.playerid));
289         s = strcat(s, ":type=", Deathtype_Name(deathtype));
290         s = strcat(s, ":items=");
291         s = AppendItemcodes(s, killer);
292         if(killed != killer)
293         {
294                 s = strcat(s, ":victimitems=");
295                 s = AppendItemcodes(s, killed);
296         }
297         GameLogEcho(s);
298 }
299
300 #define INFO_NO_MSG 0
301
302 void Obituary_SpecialDeath(entity notif_target, float murder, float deathtype, string s1, string s2, float f1, float f2, float f3)
303 {
304         float handled = 0, hits = 0;
305         if(DEATH_ISSPECIAL(deathtype))
306         {
307                 #define DEATHTYPE(name,msg_death,msg_death_by,position) \
308                         { if(deathtype == max(0, name)) \
309                         { \
310                                 #if msg_death != NO_MSG \
311                                         if not(murder) \
312                                         { \
313                                                 Send_Notification_Legacy_Wrapper(NOTIF_ONE, notif_target, MSG_DEATH, msg_death, s1, s2, f1, f2, f3); \
314                                                 Send_Notification_Legacy_Wrapper(NOTIF_ANY_EXCEPT, notif_target, MSG_INFO, INFO_##msg_death, s1, s2, f1, f2, f3); \
315                                                 ++handled; \
316                                         } \
317                                 #endif \
318                                 #if msg_death_by != NO_MSG \
319                                         if(murder) \
320                                         { \
321                                                 Send_Notification_Legacy_Wrapper(NOTIF_ONE, notif_target, MSG_DEATH, msg_death_by, s1, s2, f1, f2, f3); \
322                                                 Send_Notification_Legacy_Wrapper(NOTIF_ANY_EXCEPT, notif_target, MSG_INFO, INFO_##msg_death_by, s1, s2, f1, f2, f3); \
323                                                 ++handled; \
324                                         } \
325                                 #endif \
326                                 ++hits; \
327                         } }
328
329                 DEATHTYPES
330                 #undef DEATHTYPE
331                 
332                 if not(hits)
333                 {
334                         backtrace("Obituary_SpecialDeath(): Unhandled deathtype- Please notify Samual!\n");
335                 }
336                 if not(handled)
337                 {
338                         dprint(sprintf("Obituary_SpecialDeath(): ^1Deathtype ^7(%s-%d)^1 has no notification!\n", Deathtype_Name(deathtype), deathtype));
339                         return;
340                 }
341         }
342 }
343
344 float w_deathtype;
345 float Obituary_WeaponDeath(entity notif_target, float murder, float deathtype, string s1, string s2, float f1)
346 {
347         float death_weapon = DEATH_WEAPONOF(deathtype);
348
349         if(death_weapon)
350         {
351                 w_deathtype = deathtype;
352                 float death_message = weapon_action(death_weapon, ((murder) ? WR_KILLMESSAGE : WR_SUICIDEMESSAGE));
353                 w_deathtype = FALSE;
354
355                 if(death_message)
356                 {
357                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, notif_target, MSG_WEAPON, death_message, s1, s2, f1, NO_FL_ARG, NO_FL_ARG);
358                         Send_Notification_Legacy_Wrapper(NOTIF_ANY_EXCEPT, notif_target, MSG_INFO, Get_Notif_Infval(MSG_WEAPON, death_message), s1, s2, f1, NO_FL_ARG, NO_FL_ARG);
359                         //print(Get_Field_Value(F_INFVAL, MSG_WEAPON, death_message), "\n");
360                 }
361                 else { dprint(sprintf("Obituary_WeaponDeath(): ^1Deathtype ^7(%s-%d)^1 has no notification for weapon %d!\n", Deathtype_Name(deathtype), deathtype, death_weapon)); }
362
363                 return TRUE;
364         }
365         return FALSE;
366 }
367
368 .float FRAG_VERBOSE;
369 void Obituary(entity attacker, entity inflictor, entity targ, float deathtype)
370 {
371         // Sanity check
372         if not(targ.classname == STR_PLAYER) { backtrace("Obituary called on non-player?!\n"); return; }
373
374         // Declarations
375         string s1 = NO_STR_ARG, s2 = NO_STR_ARG;
376         float f1 = NO_FL_ARG, f2 = NO_FL_ARG, f3 = NO_FL_ARG;
377         float notif_firstblood = FALSE;
378
379         //dprint(sprintf("Obituary(): Deathtype = %s (%d), Attacker = %s, Inflictor = %s, Target = %s...\n", Deathtype_Name(deathtype), deathtype, attacker.netname, inflictor.netname, targ.netname));
380
381         // =======
382         // SUICIDE
383         // =======
384         if(targ == attacker)
385         {
386                 if(DEATH_ISSPECIAL(deathtype))
387                 {
388                         if(deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
389                         {
390                                 s1 = targ.netname;
391                                 f1 = targ.team;
392                         }
393                         else
394                         {
395                                 switch(deathtype)
396                                 {
397                                         case DEATH_MIRRORDAMAGE:
398                                         {
399                                                 s1 = targ.netname;
400                                                 f1 = targ.killcount;
401                                                 break;
402                                         }
403                                         
404                                         default:
405                                         {
406                                                 s1 = targ.netname;
407                                                 f1 = targ.killcount;
408                                                 s2 = NO_STR_ARG;
409                                                 f2 = f3 = NO_FL_ARG;
410                                                 break;
411                                         }
412                                 }
413                                 LogDeath("suicide", deathtype, targ, targ);
414                                 GiveFrags(attacker, targ, -1, deathtype);
415                         }
416                         
417                         Obituary_SpecialDeath(targ, FALSE, deathtype, s1, s2, f1, f2, NO_FL_ARG);
418                 }
419                 else if not(Obituary_WeaponDeath(targ, FALSE, deathtype, targ.netname, NO_STR_ARG, targ.killcount))
420                 {
421                         backtrace("SUICIDE: what the hell happened here?\n");
422                 }
423         }
424
425         // ======
426         // MURDER
427         // ======
428         else if(attacker.classname == "player")
429         {
430                 s1 = attacker.netname;
431                 s2 = targ.netname;
432
433                 // TODO: ADD REAL CHECK HERE!
434                 attacker.FRAG_VERBOSE = TRUE;
435                 targ.FRAG_VERBOSE = TRUE;
436                 
437                 if(!IsDifferentTeam(attacker, targ))
438                 {
439                         LogDeath("tk", deathtype, attacker, targ);
440                         GiveFrags(attacker, targ, -1, deathtype);
441
442                         attacker.killcount = 0;
443                         
444                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, attacker, MSG_DEATH, DEATH_TEAMKILL_FRAG, s2, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
445                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, targ, MSG_DEATH, DEATH_TEAMKILL_FRAGGED, s1, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
446                         Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_INFO, APP_TEAM_NUM_4(targ.team, INFO_DEATH_TEAMKILL_), s2, s1, targ.killcount, NO_FL_ARG, NO_FL_ARG);
447
448                         // In this case, the death message will ALWAYS be "foo was betrayed by bar"
449                         // No need for specific death/weapon messages...
450                 }
451                 else
452                 {
453                         LogDeath("frag", deathtype, attacker, targ);
454                         GiveFrags(attacker, targ, 1, deathtype);
455
456                         attacker.taunt_soundtime = time + 1;
457                         attacker.killcount = attacker.killcount + 1;
458                         
459                         #define ADD_ACHIEVEMENT_CASE(numa,numb) \
460                                 case numa: \
461                                 { \
462                                         AnnounceTo(attacker, strcat(#numb, "kills")); \
463                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_##numa, 1); \
464                                         break; \
465                                 }
466                         switch(attacker.killcount)
467                         {
468                                 ADD_ACHIEVEMENT_CASE(3, 03)
469                                 ADD_ACHIEVEMENT_CASE(5, 05)
470                                 ADD_ACHIEVEMENT_CASE(10, 10)
471                                 ADD_ACHIEVEMENT_CASE(15, 15)
472                                 ADD_ACHIEVEMENT_CASE(20, 20)
473                                 ADD_ACHIEVEMENT_CASE(25, 25)
474                                 ADD_ACHIEVEMENT_CASE(30, 30)
475                                 default: break;
476                         }
477                         #undef ADD_ACHIEVEMENT_CASE
478
479                         if(!checkrules_firstblood)
480                         {
481                                 checkrules_firstblood = TRUE;
482                                 notif_firstblood = TRUE; // modify the current messages so that they too show firstblood information
483                                 PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD, 1);
484                                 PlayerStats_Event(targ, PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM, 1);
485                         }
486
487                         if(notif_firstblood) // first blood, no kill sprees yet
488                         {
489                                 if(targ.istypefrag)
490                                 {
491                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, attacker, MSG_DEATH, (attacker.FRAG_VERBOSE ? DEATH_MURDER_TYPEFRAG_FIRST_VERBOSE : DEATH_MURDER_TYPEFRAG_FIRST),
492                                                 s2, s1, (attacker.FRAG_VERBOSE ? ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping) : NO_FL_ARG), NO_FL_ARG, NO_FL_ARG);
493                                                 
494                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, targ, MSG_DEATH, (targ.FRAG_VERBOSE ? DEATH_MURDER_TYPEFRAGGED_FIRST_VERBOSE : DEATH_MURDER_TYPEFRAGGED_FIRST),
495                                                 s1, NO_STR_ARG, (targ.FRAG_VERBOSE ? attacker.health : NO_FL_ARG), (targ.FRAG_VERBOSE ? attacker.armorvalue : NO_FL_ARG), (targ.FRAG_VERBOSE ? ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping) : NO_FL_ARG));
496                                 }
497                                 else
498                                 {
499                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, attacker, MSG_DEATH, (attacker.FRAG_VERBOSE ? DEATH_MURDER_FRAG_FIRST_VERBOSE : DEATH_MURDER_FRAG_FIRST),
500                                                 s2, s1, (attacker.FRAG_VERBOSE ? ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping) : NO_FL_ARG), NO_FL_ARG, NO_FL_ARG);
501                                                 
502                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, targ, MSG_DEATH, (targ.FRAG_VERBOSE ? DEATH_MURDER_FRAGGED_FIRST_VERBOSE : DEATH_MURDER_FRAGGED_FIRST),
503                                                 s1, NO_STR_ARG, (targ.FRAG_VERBOSE ? attacker.health : NO_FL_ARG), (targ.FRAG_VERBOSE ? attacker.armorvalue : NO_FL_ARG), (targ.FRAG_VERBOSE ? ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping) : NO_FL_ARG));
504                                 }
505                         }
506                         else // normal frags, kill sprees listed
507                         {
508                                 if(targ.istypefrag)
509                                 {
510                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, attacker, MSG_DEATH, (attacker.FRAG_VERBOSE ? DEATH_MURDER_TYPEFRAG_VERBOSE : DEATH_MURDER_TYPEFRAG),
511                                                 s2, NO_STR_ARG, attacker.killcount, (attacker.FRAG_VERBOSE ? ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping) : NO_FL_ARG), NO_FL_ARG);
512                                                 
513                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, targ, MSG_DEATH, (targ.FRAG_VERBOSE ? DEATH_MURDER_TYPEFRAGGED_VERBOSE : DEATH_MURDER_TYPEFRAGGED),
514                                                 s1, NO_STR_ARG, (targ.FRAG_VERBOSE ? attacker.health : NO_FL_ARG), (targ.FRAG_VERBOSE ? attacker.armorvalue : NO_FL_ARG), (targ.FRAG_VERBOSE ? ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping) : NO_FL_ARG));
515                                 }
516                                 else
517                                 {
518                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, attacker, MSG_DEATH, (attacker.FRAG_VERBOSE ? DEATH_MURDER_FRAG_VERBOSE : DEATH_MURDER_FRAG),
519                                                 s2, NO_STR_ARG, attacker.killcount, (attacker.FRAG_VERBOSE ? ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping) : NO_FL_ARG), NO_FL_ARG);
520                                                 
521                                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, targ, MSG_DEATH, (targ.FRAG_VERBOSE ? DEATH_MURDER_FRAGGED_VERBOSE : DEATH_MURDER_FRAGGED),
522                                                 s1, NO_STR_ARG, (targ.FRAG_VERBOSE ? attacker.health : NO_FL_ARG), (targ.FRAG_VERBOSE ? attacker.armorvalue : NO_FL_ARG), (targ.FRAG_VERBOSE ? ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping) : NO_FL_ARG));
523                                 }
524                         }
525
526                         //print("targ_killcount = ", ftos(targ.killcount), ", attacker_killcount = ", ftos(attacker.killcount), ".\n");
527                         if not(Obituary_WeaponDeath(targ, TRUE, deathtype, targ.netname, attacker.netname, targ.killcount))
528                                 Obituary_SpecialDeath(targ, TRUE, deathtype, s2, s1, targ.killcount, NO_FL_ARG, NO_FL_ARG);
529                 }
530         }
531
532         // =============
533         // ACCIDENT/TRAP
534         // =============
535         else
536         {
537                 switch(deathtype)
538                 {
539                         // For now, we're just forcing HURTTRIGGER to behave as "DEATH_VOID" and giving it no special options...
540                         // Later on you will only be able to make custom messages using DEATH_CUSTOM,
541                         // and there will be a REAL DEATH_VOID implementation which mappers will use.
542                         /*case DEATH_HURTTRIGGER:
543                         {
544                                 s1 = targ.netname;
545                                 s2 = inflictor.message;
546                                 if(strstrofs(s2, "%", 0) < 0) { s2 = strcat("%s ", s2); }
547                                 break;
548                         }*/
549
550                         case DEATH_CUSTOM:
551                         {
552                                 s1 = targ.netname;
553                                 s2 = deathmessage;
554                                 f1 = targ.killcount;
555                                 if(strstrofs(s2, "%", 0) < 0) { s2 = strcat("%s ", s2); }
556                                 f2 = f3 = NO_FL_ARG;
557                                 break;
558                         }
559                         
560                         default:
561                         {
562                                 s1 = targ.netname;
563                                 f1 = targ.killcount;
564                                 s2 = NO_STR_ARG;
565                                 f2 = f3 = NO_FL_ARG;
566                                 break;
567                         }
568                 }
569
570                 LogDeath("accident", deathtype, targ, targ);
571                 GiveFrags(targ, targ, -1, deathtype);
572
573                 if(PlayerScore_Add(targ, SP_SCORE, 0) == -5)
574                 {
575                         AnnounceTo(targ, "botlike");
576                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
577                 }
578
579                 Obituary_SpecialDeath(targ, FALSE, deathtype, s1, s2, f1, f2, f3);
580         }
581
582         // Set final information for the death
583         targ.death_origin = targ.origin;
584         if(targ != attacker) { targ.killer_origin = attacker.origin; }
585         if(targ.killcount) { targ.killcount = 0; }
586 }
587
588 // these are updated by each Damage call for use in button triggering and such
589 entity damage_targ;
590 entity damage_inflictor;
591 entity damage_attacker;
592
593 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
594 {
595         float mirrordamage;
596         float mirrorforce;
597         float teamdamage0;
598         entity attacker_save;
599         mirrordamage = 0;
600         mirrorforce = 0;
601
602         if (gameover || targ.killcount == -666)
603                 return;
604
605         entity oldself;
606         oldself = self;
607         self = targ;
608         damage_targ = targ;
609         damage_inflictor = inflictor;
610         damage_attacker = attacker;
611                 attacker_save = attacker;
612
613         if(targ.classname == "player")
614                 if(targ.hook)
615                         if(targ.hook.aiment)
616                                 if(targ.hook.aiment == attacker)
617                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
618
619         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
620         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
621         {
622                 if(targ.classname == "player")
623                         if not(IsDifferentTeam(targ, attacker))
624                         {
625                                 self = oldself;
626                                 return;
627                         }
628         }
629
630         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
631         {
632                 // These are ALWAYS lethal
633                 // No damage modification here
634                 // Instead, prepare the victim for his death...
635                 targ.armorvalue = 0;
636                 targ.spawnshieldtime = 0;
637                 targ.health = 0.9; // this is < 1
638                 targ.flags -= targ.flags & FL_GODMODE;
639                 damage = 100000;
640         }
641         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
642         {
643                 // no processing
644         }
645         else
646         {
647                 /*
648                 skill based bot damage? gtfo. (tZork)
649                 if (targ.classname == "player")
650                 if (attacker.classname == "player")
651                 if (!targ.isbot)
652                 if (attacker.isbot)
653                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
654         */
655         
656                 // nullify damage if teamplay is on
657                 if(deathtype != DEATH_TELEFRAG)
658                 if(attacker.classname == "player")
659                 {
660                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
661                         {
662                                 damage = 0;
663                                 force = '0 0 0';
664                         }
665                         else if(!IsDifferentTeam(attacker, targ))
666                         {
667                                 if(autocvar_teamplay_mode == 1)
668                                         damage = 0;
669                                 else if(attacker != targ)
670                                 {
671                                         if(autocvar_teamplay_mode == 3)
672                                                 damage = 0;
673                                         else if(autocvar_teamplay_mode == 4)
674                                         {
675                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
676                                                 {
677                                                         teamdamage0 = max(attacker.dmg_team, autocvar_g_teamdamage_threshold);
678                                                         attacker.dmg_team = attacker.dmg_team + damage;
679                                                         if(attacker.dmg_team > teamdamage0 && !g_ca)
680                                                                 mirrordamage = autocvar_g_mirrordamage * (attacker.dmg_team - teamdamage0);
681                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
682                                                         if(g_minstagib)
683                                                         {
684                                                                 if(autocvar_g_friendlyfire == 0)
685                                                                         damage = 0;
686                                                         }
687                                                         else if(g_ca)
688                                                                 damage = 0;
689                                                         else
690                                                                 damage = autocvar_g_friendlyfire * damage;
691                                                         // mirrordamage will be used LATER
692
693                                                         if(autocvar_g_mirrordamage_virtual)
694                                                         {
695                                                                 vector v  = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
696                                                                 attacker.dmg_take += v_x;
697                                                                 attacker.dmg_save += v_y;
698                                                                 attacker.dmg_inflictor = inflictor;
699                                                                 mirrordamage = v_z; // = 0, to make fteqcc stfu
700                                                                 mirrorforce = 0;
701                                                         }
702
703                                                         if(autocvar_g_friendlyfire_virtual)
704                                                         {
705                                                                 vector v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
706                                                                 targ.dmg_take += v_x;
707                                                                 targ.dmg_save += v_y;
708                                                                 targ.dmg_inflictor = inflictor;
709                                                                 damage = 0;
710                                                                 if(!autocvar_g_friendlyfire_virtual_force)
711                                                                         force = '0 0 0';
712                                                         }
713                                                 }
714                                                 else
715                                                         damage = 0;
716                                         }
717                                 }
718                         }
719                 }
720
721                 if(targ.classname == "player")
722                 if(attacker.classname == "player")
723                 if(attacker != targ)
724                 {
725                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
726                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
727                 }
728
729                 if(targ.classname == "player")
730                 if (g_minstagib)
731                 {
732                         if ((deathtype == DEATH_FALL)  ||
733                                 (deathtype == DEATH_DROWN) ||
734                                 (deathtype == DEATH_SLIME) ||
735                                 (deathtype == DEATH_LAVA)  ||
736                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
737                         {
738                                 self = oldself;
739                                 return;
740                         }
741                         if(damage > 0)
742                             damage = 10000;
743                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
744                         {
745                                 targ.armorvalue -= 1;
746                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
747                                 damage = 0;
748                                 targ.hitsound += 1;
749                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
750                         }
751                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
752                         {
753                                 damage = 0;
754                                 mirrordamage = 0;
755                                 if (targ != attacker)
756                                 {
757                                         if ((targ.health >= 1) && (targ.classname == "player"))
758                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
759                                         force = '0 0 0';
760                                         // keep mirrorforce
761                                         attacker = targ;
762                                 }
763                         }
764                 }
765
766                 if not(DEATH_ISSPECIAL(deathtype))
767                 {
768                         damage *= g_weapondamagefactor;
769                         mirrordamage *= g_weapondamagefactor;
770                         force = force * g_weaponforcefactor;
771                         mirrorforce *= g_weaponforcefactor;
772                 }
773                 
774                 // should this be changed at all? If so, in what way?
775                 frag_attacker = attacker;
776                 frag_target = targ;
777                 frag_damage = damage;
778                 frag_force = force;
779         frag_deathtype = deathtype;
780                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
781                 damage = frag_damage;
782                 force = frag_force;
783                 
784                 // apply strength multiplier
785                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
786                 {
787                         if(targ == attacker)
788                         {
789                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
790                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
791                         }
792                         else
793                         {
794                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
795                                 force = force * autocvar_g_balance_powerup_strength_force;
796                         }
797                 }
798
799                 // apply invincibility multiplier
800                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
801                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
802
803                 if (targ == attacker)
804                 {
805                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
806                                 damage = 0;
807                         else
808                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
809                 }
810
811                 if(g_runematch)
812                 {
813                         // apply strength rune
814                         if (attacker.runes & RUNE_STRENGTH)
815                         {
816                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
817                                 {
818                                         damage = damage * autocvar_g_balance_rune_strength_combo_damage;
819                                         force = force * autocvar_g_balance_rune_strength_combo_force;
820                                 }
821                                 else
822                                 {
823                                         damage = damage * autocvar_g_balance_rune_strength_damage;
824                                         force = force * autocvar_g_balance_rune_strength_force;
825                                 }
826                         }
827                         else if (attacker.runes & CURSE_WEAK)
828                         {
829                                 damage = damage * autocvar_g_balance_curse_weak_damage;
830                                 force = force * autocvar_g_balance_curse_weak_force;
831                         }
832
833                         // apply defense rune
834                         if (targ.runes & RUNE_DEFENSE)
835                         {
836                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
837                                         damage = damage * autocvar_g_balance_rune_defense_combo_takedamage;
838                                 else
839                                         damage = damage * autocvar_g_balance_rune_defense_takedamage;
840                         }
841                         else if (targ.runes & CURSE_VULNER)
842                                 damage = damage * autocvar_g_balance_curse_vulner_takedamage;
843                 }
844
845                 // count the damage
846                 if(attacker)
847                 if(!targ.deadflag)
848                 if(targ.takedamage == DAMAGE_AIM)
849                 if(targ != attacker)
850                 {
851                         entity victim;
852                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
853                                 victim = targ.owner;
854                         else
855                                 victim = targ;
856
857                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
858                         {
859                                 if(IsDifferentTeam(victim, attacker))
860                                 {
861                                         if(damage > 0)
862                                         {
863                                                 if(deathtype != DEATH_FIRE)
864                                                 {
865                                                         if(victim.BUTTON_CHAT)
866                                                                 attacker.typehitsound += 1;
867                                                         else
868                                                                 attacker.hitsound += 1;
869                                                 }
870
871                                                 damage_goodhits += 1;
872                                                 damage_gooddamage += damage;
873
874                                                 if not(DEATH_ISSPECIAL(deathtype))
875                                                 {
876                                                         if(targ.classname == "player") // don't do this for vehicles
877                                                         if(!g_minstagib)
878                                                         if(IsFlying(victim))
879                                                                 yoda = 1;
880
881                                                         if(g_minstagib)
882                                                         if(victim.items & IT_STRENGTH)
883                                                                 yoda = 1;
884                                                 }
885                                         }
886                                 }
887                                 else
888                                 {
889                                         if(deathtype != DEATH_FIRE)
890                                         {
891                                                 attacker.typehitsound += 1;
892                                         }
893                                         if(mirrordamage > 0)
894                                                 if(time > attacker.teamkill_complain)
895                                                 {
896                                                         attacker.teamkill_complain = time + 5;
897                                                         attacker.teamkill_soundtime = time + 0.4;
898                                                         attacker.teamkill_soundsource = targ;
899                                                 }
900                                 }
901                         }
902                 }
903         }
904
905         // apply push
906         if (self.damageforcescale)
907         if (vlen(force))
908         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
909         {
910                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
911                 if(self.movetype == MOVETYPE_PHYSICS)
912                 {
913                         entity farcent;
914                         farcent = spawn();
915                         farcent.classname = "farce";
916                         farcent.enemy = self;
917                         farcent.movedir = farce * 10;
918                         if(self.mass)
919                                 farcent.movedir = farcent.movedir * self.mass;
920                         farcent.origin = hitloc;
921                         farcent.forcetype = FORCETYPE_FORCEATPOS;
922                         farcent.nextthink = time + 0.1;
923                         farcent.think = SUB_Remove;
924                 }
925                 else
926                         self.velocity = self.velocity + farce;
927                 self.flags &~= FL_ONGROUND;
928                 UpdateCSQCProjectile(self);
929         }
930         // apply damage
931         if (damage != 0 || (self.damageforcescale && vlen(force)))
932         if (self.event_damage)
933                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
934         self = oldself;
935
936         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
937         {
938                 if(g_runematch)
939                 {
940                         if (attacker.runes & RUNE_VAMPIRE)
941                         {
942                         // apply vampire rune
943                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
944                                 {
945                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb;
946                                         attacker.health = bound(
947                                                 autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 40
948                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb,
949                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
950                                 }
951                                 else
952                                 {
953                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_absorb;
954                                         attacker.health = bound(
955                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
956                                                                                         // empathy won't let you gain health in the same way...
957                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_absorb,
958                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
959                                         }
960                         }
961                         // apply empathy curse
962                         else if (attacker.runes & CURSE_EMPATHY)
963                         {
964                                 attacker.health = bound(
965                                         autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 20
966                                         attacker.health + damage * autocvar_g_balance_curse_empathy_takedamage,
967                                         attacker.health);
968                         }
969                 }
970         }
971
972         // apply mirror damage if any
973         if(mirrordamage > 0 || mirrorforce > 0)
974         {
975                 attacker = attacker_save;
976                 if(g_minstagib)
977                 if(mirrordamage > 0)
978                 {
979                         // just lose extra LIVES, don't kill the player for mirror damage
980                         if(attacker.armorvalue > 0)
981                         {
982                                 attacker.armorvalue = attacker.armorvalue - 1;
983                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
984                                 attacker.hitsound += 1;
985                         }
986                         mirrordamage = 0;
987                 }
988
989                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
990                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
991         }
992 }
993
994 float RadiusDamage_running;
995 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
996         // Returns total damage applies to creatures
997 {
998         entity  targ;
999         vector  blastorigin;
1000         vector  force;
1001         float   total_damage_to_creatures;
1002         entity  next;
1003         float   tfloordmg;
1004         float   tfloorforce;
1005
1006         float stat_damagedone;
1007
1008         if(RadiusDamage_running)
1009         {
1010                 backtrace("RadiusDamage called recursively! Expect stuff to go HORRIBLY wrong.");
1011                 return 0;
1012         }
1013
1014         RadiusDamage_running = 1;
1015
1016         tfloordmg = autocvar_g_throughfloor_damage;
1017         tfloorforce = autocvar_g_throughfloor_force;
1018
1019         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1020         total_damage_to_creatures = 0;
1021
1022         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1023                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1024                 {
1025                         force = inflictor.velocity;
1026                         if(vlen(force) == 0)
1027                                 force = '0 0 -1';
1028                         else
1029                                 force = normalize(force);
1030                         if(forceintensity >= 0)
1031                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
1032                         else
1033                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
1034                 }
1035
1036         stat_damagedone = 0;
1037
1038         targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
1039         while (targ)
1040         {
1041                 next = targ.chain;
1042                 if (targ != inflictor)
1043                         if (ignore != targ) if(targ.takedamage)
1044                         {
1045                                 vector nearest;
1046                                 vector diff;
1047                                 float power;
1048
1049                                 // LordHavoc: measure distance to nearest point on target (not origin)
1050                                 // (this guarentees 100% damage on a touch impact)
1051                                 nearest = targ.WarpZone_findradius_nearest;
1052                                 diff = targ.WarpZone_findradius_dist;
1053                                 // round up a little on the damage to ensure full damage on impacts
1054                                 // and turn the distance into a fraction of the radius
1055                                 power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
1056                                 //bprint(" ");
1057                                 //bprint(ftos(power));
1058                                 //if (targ == attacker)
1059                                 //      print(ftos(power), "\n");
1060                                 if (power > 0)
1061                                 {
1062                                         float finaldmg;
1063                                         if (power > 1)
1064                                                 power = 1;
1065                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1066                                         if (finaldmg > 0)
1067                                         {
1068                                                 float a;
1069                                                 float c;
1070                                                 vector hitloc;
1071                                                 vector myblastorigin;
1072                                                 vector center;
1073
1074                                                 myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
1075
1076                                                 // if it's a player, use the view origin as reference
1077                                                 center = CENTER_OR_VIEWOFS(targ);
1078
1079                                                 force = normalize(center - myblastorigin);
1080                                                 force = force * (finaldmg / coredamage) * forceintensity;
1081                                                 hitloc = nearest;
1082
1083                                                 if(targ != directhitentity)
1084                                                 {
1085                                                         float hits;
1086                                                         float total;
1087                                                         float hitratio;
1088                                                         float mininv_f, mininv_d;
1089
1090                                                         // test line of sight to multiple positions on box,
1091                                                         // and do damage if any of them hit
1092                                                         hits = 0;
1093
1094                                                         // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1095                                                         // so for a given max stddev:
1096                                                         // n = (1 / (2 * max stddev of hitratio))^2
1097
1098                                                         mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1099                                                         mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1100
1101                                                         if(autocvar_g_throughfloor_debug)
1102                                                                 print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1103
1104                                                         total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1105
1106                                                         if(autocvar_g_throughfloor_debug)
1107                                                                 print(sprintf(" steps=%f", total));
1108
1109                                                         if (targ.classname == "player")
1110                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1111                                                         else
1112                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1113
1114                                                         if(autocvar_g_throughfloor_debug)
1115                                                                 print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1116
1117                                                         for(c = 0; c < total; ++c)
1118                                                         {
1119                                                                 //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1120                                                                 WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1121                                                                 if (trace_fraction == 1 || trace_ent == targ)
1122                                                                 {
1123                                                                         ++hits;
1124                                                                         if (hits > 1)
1125                                                                                 hitloc = hitloc + nearest;
1126                                                                         else
1127                                                                                 hitloc = nearest;
1128                                                                 }
1129                                                                 nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1130                                                                 nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1131                                                                 nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1132                                                         }
1133
1134                                                         nearest = hitloc * (1 / max(1, hits));
1135                                                         hitratio = (hits / total);
1136                                                         a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1137                                                         finaldmg = finaldmg * a;
1138                                                         a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1139                                                         force = force * a;
1140
1141                                                         if(autocvar_g_throughfloor_debug)
1142                                                                 print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1143                                                 }
1144
1145                                                 // laser force adjustments :P
1146                                                 if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1147                                                 {
1148                                                         if (targ == attacker)
1149                                                         {
1150                                                                 vector vel;
1151
1152                                                                 float force_zscale;
1153                                                                 float force_velocitybiasramp;
1154                                                                 float force_velocitybias;
1155
1156                                                                 force_velocitybiasramp = autocvar_sv_maxspeed;
1157                                                                 if(deathtype & HITTYPE_SECONDARY)
1158                                                                 {
1159                                                                         force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1160                                                                         force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1161                                                                 }
1162                                                                 else
1163                                                                 {
1164                                                                         force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1165                                                                         force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1166                                                                 }
1167
1168                                                                 vel = targ.velocity;
1169                                                                 vel_z = 0;
1170                                                                 vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1171                                                                 force =
1172                                                                         vlen(force)
1173                                                                         *
1174                                                                         normalize(normalize(force) + vel);
1175
1176                                                                 force_z *= force_zscale;
1177                                                         }
1178                                                         else
1179                                                         {
1180                                                                 if(deathtype & HITTYPE_SECONDARY)
1181                                                                 {
1182                                                                         force *= autocvar_g_balance_laser_secondary_force_other_scale;
1183                                                                 }
1184                                                                 else
1185                                                                 {
1186                                                                         force *= autocvar_g_balance_laser_primary_force_other_scale;
1187                                                                 }
1188                                                         }
1189                                                 }
1190
1191                                                 //if (targ == attacker)
1192                                                 //{
1193                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1194                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1195                                                 //      print(" (", ftos(a), ")\n");
1196                                                 //}
1197                                                 if(finaldmg || vlen(force))
1198                                                 {
1199                                                         if(targ.iscreature)
1200                                                         {
1201                                                                 total_damage_to_creatures += finaldmg;
1202
1203                                                                 if(accuracy_isgooddamage(attacker, targ))
1204                                                                         stat_damagedone += finaldmg;
1205                                                         }
1206
1207                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1208                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1209                                                         else
1210                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1211                                                 }
1212                                         }
1213                                 }
1214                         }
1215                 targ = next;
1216         }
1217
1218         RadiusDamage_running = 0;
1219
1220         if(!DEATH_ISSPECIAL(deathtype))
1221                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1222
1223         return total_damage_to_creatures;
1224 }
1225
1226 .float fire_damagepersec;
1227 .float fire_endtime;
1228 .float fire_deathtype;
1229 .entity fire_owner;
1230 .float fire_hitsound;
1231 .entity fire_burner;
1232
1233 void fireburner_think();
1234
1235 float Fire_IsBurning(entity e)
1236 {
1237         return (time < e.fire_endtime);
1238 }
1239
1240 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1241 {
1242         float dps;
1243         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1244
1245         if(e.classname == "player")
1246         {
1247                 if(e.deadflag)
1248                         return -1;
1249         }
1250         else
1251         {
1252                 if(!e.fire_burner)
1253                 {
1254                         // print("adding a fire burner to ", e.classname, "\n");
1255                         e.fire_burner = spawn();
1256                         e.fire_burner.classname = "fireburner";
1257                         e.fire_burner.think = fireburner_think;
1258                         e.fire_burner.nextthink = time;
1259                         e.fire_burner.owner = e;
1260                 }
1261         }
1262
1263         t = max(t, 0.1);
1264         dps = d / t;
1265         if(Fire_IsBurning(e))
1266         {
1267                 mintime = e.fire_endtime - time;
1268                 maxtime = max(mintime, t);
1269
1270                 mindps = e.fire_damagepersec;
1271                 maxdps = max(mindps, dps);
1272
1273                 if(maxtime > mintime || maxdps > mindps)
1274                 {
1275                         // Constraints:
1276                         
1277                         // damage we have right now
1278                         mindamage = mindps * mintime;
1279
1280                         // damage we want to get
1281                         maxdamage = mindamage + d;
1282
1283                         // but we can't exceed maxtime * maxdps!
1284                         totaldamage = min(maxdamage, maxtime * maxdps);
1285
1286                         // LEMMA:
1287                         // Look at:
1288                         // totaldamage = min(mindamage + d, maxtime * maxdps)
1289                         // We see:
1290                         // totaldamage <= maxtime * maxdps
1291                         // ==> totaldamage / maxdps <= maxtime.
1292                         // We also see:
1293                         // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
1294                         //                     >= min(mintime, maxtime)
1295                         // ==> totaldamage / maxdps >= mintime.
1296
1297                         /*
1298                         // how long do we damage then?
1299                         // at least as long as before
1300                         // but, never exceed maxdps
1301                         totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
1302                         */
1303
1304                         // alternate:
1305                         // at most as long as maximum allowed
1306                         // but, never below mindps
1307                         totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
1308
1309                         // assuming t > mintime, dps > mindps:
1310                         // we get d = t * dps = maxtime * maxdps
1311                         // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
1312                         // totaldamage / maxdps = maxtime
1313                         // totaldamage / mindps > totaldamage / maxdps = maxtime
1314                         // FROM THIS:
1315                         // a) totaltime = max(mintime, maxtime) = maxtime
1316                         // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
1317
1318                         // assuming t <= mintime:
1319                         // we get maxtime = mintime
1320                         // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
1321                         // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
1322
1323                         // assuming dps <= mindps:
1324                         // we get mindps = maxdps.
1325                         // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
1326                         // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
1327                         // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
1328
1329                         e.fire_damagepersec = totaldamage / totaltime;
1330                         e.fire_endtime = time + totaltime;
1331                         if(totaldamage > 1.2 * mindamage)
1332                         {
1333                                 e.fire_deathtype = dt;
1334                                 if(e.fire_owner != o)
1335                                 {
1336                                         e.fire_owner = o;
1337                                         e.fire_hitsound = FALSE;
1338                                 }
1339                         }
1340                         if(accuracy_isgooddamage(o, e))
1341                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1342                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1343                 }
1344                 else
1345                         return 0;
1346         }
1347         else
1348         {
1349                 e.fire_damagepersec = dps;
1350                 e.fire_endtime = time + t;
1351                 e.fire_deathtype = dt;
1352                 e.fire_owner = o;
1353                 e.fire_hitsound = FALSE;
1354                 if(accuracy_isgooddamage(o, e))
1355                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1356                 return d;
1357         }
1358 }
1359
1360 void Fire_ApplyDamage(entity e)
1361 {
1362         float t, d, hi, ty;
1363         entity o;
1364
1365         if not(Fire_IsBurning(e))
1366                 return;
1367
1368         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1369         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1370                 o = e.fire_owner;
1371
1372         // water and slime stop fire
1373         if(e.waterlevel)
1374         if(e.watertype != CONTENT_LAVA)
1375                 e.fire_endtime = 0;
1376
1377         // ice stops fire
1378         if(e.freezetag_frozen)
1379                 e.fire_endtime = 0;
1380
1381         t = min(frametime, e.fire_endtime - time);
1382         d = e.fire_damagepersec * t;
1383
1384         hi = e.fire_owner.hitsound;
1385         ty = e.fire_owner.typehitsound;
1386         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1387         if(e.fire_hitsound && e.fire_owner)
1388         {
1389                 e.fire_owner.hitsound = hi;
1390                 e.fire_owner.typehitsound = ty;
1391         }
1392         e.fire_hitsound = TRUE;
1393
1394         if not(IS_INDEPENDENT_PLAYER(e))
1395         FOR_EACH_PLAYER(other) if(e != other)
1396         {
1397                 if(other.classname == "player")
1398                 if(other.deadflag == DEAD_NO)
1399                 if not(IS_INDEPENDENT_PLAYER(other))
1400                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1401                 {
1402                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1403                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1404                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1405                 }
1406         }
1407 }
1408
1409 void Fire_ApplyEffect(entity e)
1410 {
1411         if(Fire_IsBurning(e))
1412                 e.effects |= EF_FLAME;
1413         else
1414                 e.effects &~= EF_FLAME;
1415 }
1416
1417 void fireburner_think()
1418 {
1419         // for players, this is done in the regular loop
1420         if(wasfreed(self.owner))
1421         {
1422                 remove(self);
1423                 return;
1424         }
1425         Fire_ApplyEffect(self.owner);
1426         if(!Fire_IsBurning(self.owner))
1427         {
1428                 self.owner.fire_burner = world;
1429                 remove(self);
1430                 return;
1431         }
1432         Fire_ApplyDamage(self.owner);
1433         self.nextthink = time;
1434 }