]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
Actually, make that argument smaller... plus, lets get rid of infval check
[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, 0, 0);
358                         Send_Notification_Legacy_Wrapper(NOTIF_ANY_EXCEPT, notif_target, MSG_INFO, msg_weapon_notifs[death_message - 1].nent_infoname, s1, s2, f1, 0, 0);
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 = "", s2 = "";
376         float f1 = 0, f2 = 0, f3 = 0;
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 = "";
409                                                 f2 = f3 = 0;
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, 0);
418                 }
419                 else if not(Obituary_WeaponDeath(targ, FALSE, deathtype, targ.netname, "", 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(NOTIF_ONE, attacker, MSG_DEATH, DEATH_TEAMKILL_FRAG, s2);
445                         Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_TEAMKILL_FRAGGED, s1);
446                         Send_Notification(NOTIF_ANY, world, MSG_INFO, APP_TEAM_NUM_4(targ.team, INFO_DEATH_TEAMKILL_), s2, s1, targ.killcount);
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                                         if(attacker.FRAG_VERBOSE)
492                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_TYPEFRAG_FIRST_VERBOSE, s2, s1, ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping));
493                                         else
494                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_TYPEFRAG_FIRST, s2, s1);
495
496                                         if(targ.FRAG_VERBOSE)
497                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_TYPEFRAGGED_FIRST_VERBOSE, s1, attacker.health, attacker.armorvalue, ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping));
498                                         else
499                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_TYPEFRAGGED_FIRST, s1);
500                                 }
501                                 else
502                                 {
503                                         if(attacker.FRAG_VERBOSE)
504                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_FRAG_FIRST_VERBOSE, s2, s1, ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping));
505                                         else
506                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_FRAG_FIRST, s2, s1);
507
508                                         if(targ.FRAG_VERBOSE)
509                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_FRAGGED_FIRST_VERBOSE, s1, attacker.health, attacker.armorvalue, ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping));
510                                         else
511                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_FRAGGED_FIRST, s1);
512                                 }
513                         }
514                         else // normal frags, kill sprees listed
515                         {
516                                 if(targ.istypefrag)
517                                 {
518                                         if(attacker.FRAG_VERBOSE)
519                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_TYPEFRAG_VERBOSE, s2, attacker.killcount, ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping));
520                                         else
521                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_TYPEFRAG, s2, attacker.killcount);
522
523                                         if(targ.FRAG_VERBOSE)
524                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_TYPEFRAGGED_VERBOSE, s1, attacker.health, attacker.armorvalue, ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping));
525                                         else
526                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_TYPEFRAGGED, s1);
527                                 }
528                                 else
529                                 {
530                                         if(attacker.FRAG_VERBOSE)
531                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_FRAG_VERBOSE, s2, attacker.killcount, ((clienttype(targ) == CLIENTTYPE_BOT) ? BOT_PING : targ.ping));
532                                         else
533                                                 Send_Notification(NOTIF_ONE, attacker, MSG_DEATH, DEATH_MURDER_FRAG, s2, attacker.killcount);
534
535                                         if(targ.FRAG_VERBOSE)
536                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_FRAGGED_VERBOSE, s1, attacker.health, attacker.armorvalue, ((clienttype(attacker) == CLIENTTYPE_BOT) ? BOT_PING : attacker.ping));
537                                         else
538                                                 Send_Notification(NOTIF_ONE, targ, MSG_DEATH, DEATH_MURDER_FRAGGED, s1);
539                                 }
540                         }
541
542                         //print("targ_killcount = ", ftos(targ.killcount), ", attacker_killcount = ", ftos(attacker.killcount), ".\n");
543                         if not(Obituary_WeaponDeath(targ, TRUE, deathtype, targ.netname, attacker.netname, targ.killcount))
544                                 Obituary_SpecialDeath(targ, TRUE, deathtype, s2, s1, targ.killcount, 0, 0);
545                 }
546         }
547
548         // =============
549         // ACCIDENT/TRAP
550         // =============
551         else
552         {
553                 switch(deathtype)
554                 {
555                         // For now, we're just forcing HURTTRIGGER to behave as "DEATH_VOID" and giving it no special options...
556                         // Later on you will only be able to make custom messages using DEATH_CUSTOM,
557                         // and there will be a REAL DEATH_VOID implementation which mappers will use.
558                         /*case DEATH_HURTTRIGGER:
559                         {
560                                 s1 = targ.netname;
561                                 s2 = inflictor.message;
562                                 if(strstrofs(s2, "%", 0) < 0) { s2 = strcat("%s ", s2); }
563                                 break;
564                         }*/
565
566                         case DEATH_CUSTOM:
567                         {
568                                 s1 = targ.netname;
569                                 s2 = deathmessage;
570                                 f1 = targ.killcount;
571                                 if(strstrofs(s2, "%", 0) < 0) { s2 = strcat("%s ", s2); }
572                                 f2 = f3 = 0;
573                                 break;
574                         }
575                         
576                         default:
577                         {
578                                 s1 = targ.netname;
579                                 f1 = targ.killcount;
580                                 s2 = "";
581                                 f2 = f3 = 0;
582                                 break;
583                         }
584                 }
585
586                 LogDeath("accident", deathtype, targ, targ);
587                 GiveFrags(targ, targ, -1, deathtype);
588
589                 if(PlayerScore_Add(targ, SP_SCORE, 0) == -5)
590                 {
591                         AnnounceTo(targ, "botlike");
592                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
593                 }
594
595                 Obituary_SpecialDeath(targ, FALSE, deathtype, s1, s2, f1, f2, f3);
596         }
597
598         // Set final information for the death
599         targ.death_origin = targ.origin;
600         if(targ != attacker) { targ.killer_origin = attacker.origin; }
601         if(targ.killcount) { targ.killcount = 0; }
602 }
603
604 // these are updated by each Damage call for use in button triggering and such
605 entity damage_targ;
606 entity damage_inflictor;
607 entity damage_attacker;
608
609 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
610 {
611         float mirrordamage;
612         float mirrorforce;
613         float teamdamage0;
614         entity attacker_save;
615         mirrordamage = 0;
616         mirrorforce = 0;
617
618         if (gameover || targ.killcount == -666)
619                 return;
620
621         entity oldself;
622         oldself = self;
623         self = targ;
624         damage_targ = targ;
625         damage_inflictor = inflictor;
626         damage_attacker = attacker;
627                 attacker_save = attacker;
628
629         if(targ.classname == "player")
630                 if(targ.hook)
631                         if(targ.hook.aiment)
632                                 if(targ.hook.aiment == attacker)
633                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
634
635         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
636         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
637         {
638                 if(targ.classname == "player")
639                         if not(IsDifferentTeam(targ, attacker))
640                         {
641                                 self = oldself;
642                                 return;
643                         }
644         }
645
646         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
647         {
648                 // These are ALWAYS lethal
649                 // No damage modification here
650                 // Instead, prepare the victim for his death...
651                 targ.armorvalue = 0;
652                 targ.spawnshieldtime = 0;
653                 targ.health = 0.9; // this is < 1
654                 targ.flags -= targ.flags & FL_GODMODE;
655                 damage = 100000;
656         }
657         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
658         {
659                 // no processing
660         }
661         else
662         {
663                 /*
664                 skill based bot damage? gtfo. (tZork)
665                 if (targ.classname == "player")
666                 if (attacker.classname == "player")
667                 if (!targ.isbot)
668                 if (attacker.isbot)
669                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
670         */
671         
672                 // nullify damage if teamplay is on
673                 if(deathtype != DEATH_TELEFRAG)
674                 if(attacker.classname == "player")
675                 {
676                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
677                         {
678                                 damage = 0;
679                                 force = '0 0 0';
680                         }
681                         else if(!IsDifferentTeam(attacker, targ))
682                         {
683                                 if(autocvar_teamplay_mode == 1)
684                                         damage = 0;
685                                 else if(attacker != targ)
686                                 {
687                                         if(autocvar_teamplay_mode == 3)
688                                                 damage = 0;
689                                         else if(autocvar_teamplay_mode == 4)
690                                         {
691                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
692                                                 {
693                                                         teamdamage0 = max(attacker.dmg_team, autocvar_g_teamdamage_threshold);
694                                                         attacker.dmg_team = attacker.dmg_team + damage;
695                                                         if(attacker.dmg_team > teamdamage0 && !g_ca)
696                                                                 mirrordamage = autocvar_g_mirrordamage * (attacker.dmg_team - teamdamage0);
697                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
698                                                         if(g_minstagib)
699                                                         {
700                                                                 if(autocvar_g_friendlyfire == 0)
701                                                                         damage = 0;
702                                                         }
703                                                         else if(g_ca)
704                                                                 damage = 0;
705                                                         else
706                                                                 damage = autocvar_g_friendlyfire * damage;
707                                                         // mirrordamage will be used LATER
708
709                                                         if(autocvar_g_mirrordamage_virtual)
710                                                         {
711                                                                 vector v  = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
712                                                                 attacker.dmg_take += v_x;
713                                                                 attacker.dmg_save += v_y;
714                                                                 attacker.dmg_inflictor = inflictor;
715                                                                 mirrordamage = v_z; // = 0, to make fteqcc stfu
716                                                                 mirrorforce = 0;
717                                                         }
718
719                                                         if(autocvar_g_friendlyfire_virtual)
720                                                         {
721                                                                 vector v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
722                                                                 targ.dmg_take += v_x;
723                                                                 targ.dmg_save += v_y;
724                                                                 targ.dmg_inflictor = inflictor;
725                                                                 damage = 0;
726                                                                 if(!autocvar_g_friendlyfire_virtual_force)
727                                                                         force = '0 0 0';
728                                                         }
729                                                 }
730                                                 else
731                                                         damage = 0;
732                                         }
733                                 }
734                         }
735                 }
736
737                 if(targ.classname == "player")
738                 if(attacker.classname == "player")
739                 if(attacker != targ)
740                 {
741                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
742                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
743                 }
744
745                 if(targ.classname == "player")
746                 if (g_minstagib)
747                 {
748                         if ((deathtype == DEATH_FALL)  ||
749                                 (deathtype == DEATH_DROWN) ||
750                                 (deathtype == DEATH_SLIME) ||
751                                 (deathtype == DEATH_LAVA)  ||
752                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
753                         {
754                                 self = oldself;
755                                 return;
756                         }
757                         if(damage > 0)
758                             damage = 10000;
759                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
760                         {
761                                 targ.armorvalue -= 1;
762                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
763                                 damage = 0;
764                                 targ.hitsound += 1;
765                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
766                         }
767                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
768                         {
769                                 damage = 0;
770                                 mirrordamage = 0;
771                                 if (targ != attacker)
772                                 {
773                                         if ((targ.health >= 1) && (targ.classname == "player"))
774                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
775                                         force = '0 0 0';
776                                         // keep mirrorforce
777                                         attacker = targ;
778                                 }
779                         }
780                 }
781
782                 if not(DEATH_ISSPECIAL(deathtype))
783                 {
784                         damage *= g_weapondamagefactor;
785                         mirrordamage *= g_weapondamagefactor;
786                         force = force * g_weaponforcefactor;
787                         mirrorforce *= g_weaponforcefactor;
788                 }
789                 
790                 // should this be changed at all? If so, in what way?
791                 frag_attacker = attacker;
792                 frag_target = targ;
793                 frag_damage = damage;
794                 frag_force = force;
795         frag_deathtype = deathtype;
796                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
797                 damage = frag_damage;
798                 force = frag_force;
799                 
800                 // apply strength multiplier
801                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
802                 {
803                         if(targ == attacker)
804                         {
805                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
806                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
807                         }
808                         else
809                         {
810                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
811                                 force = force * autocvar_g_balance_powerup_strength_force;
812                         }
813                 }
814
815                 // apply invincibility multiplier
816                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
817                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
818
819                 if (targ == attacker)
820                 {
821                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
822                                 damage = 0;
823                         else
824                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
825                 }
826
827                 if(g_runematch)
828                 {
829                         // apply strength rune
830                         if (attacker.runes & RUNE_STRENGTH)
831                         {
832                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
833                                 {
834                                         damage = damage * autocvar_g_balance_rune_strength_combo_damage;
835                                         force = force * autocvar_g_balance_rune_strength_combo_force;
836                                 }
837                                 else
838                                 {
839                                         damage = damage * autocvar_g_balance_rune_strength_damage;
840                                         force = force * autocvar_g_balance_rune_strength_force;
841                                 }
842                         }
843                         else if (attacker.runes & CURSE_WEAK)
844                         {
845                                 damage = damage * autocvar_g_balance_curse_weak_damage;
846                                 force = force * autocvar_g_balance_curse_weak_force;
847                         }
848
849                         // apply defense rune
850                         if (targ.runes & RUNE_DEFENSE)
851                         {
852                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
853                                         damage = damage * autocvar_g_balance_rune_defense_combo_takedamage;
854                                 else
855                                         damage = damage * autocvar_g_balance_rune_defense_takedamage;
856                         }
857                         else if (targ.runes & CURSE_VULNER)
858                                 damage = damage * autocvar_g_balance_curse_vulner_takedamage;
859                 }
860
861                 // count the damage
862                 if(attacker)
863                 if(!targ.deadflag)
864                 if(targ.takedamage == DAMAGE_AIM)
865                 if(targ != attacker)
866                 {
867                         entity victim;
868                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
869                                 victim = targ.owner;
870                         else
871                                 victim = targ;
872
873                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET)
874                         {
875                                 if(IsDifferentTeam(victim, attacker))
876                                 {
877                                         if(damage > 0)
878                                         {
879                                                 if(deathtype != DEATH_FIRE)
880                                                 {
881                                                         if(victim.BUTTON_CHAT)
882                                                                 attacker.typehitsound += 1;
883                                                         else
884                                                                 attacker.hitsound += 1;
885                                                 }
886
887                                                 damage_goodhits += 1;
888                                                 damage_gooddamage += damage;
889
890                                                 if not(DEATH_ISSPECIAL(deathtype))
891                                                 {
892                                                         if(targ.classname == "player") // don't do this for vehicles
893                                                         if(!g_minstagib)
894                                                         if(IsFlying(victim))
895                                                                 yoda = 1;
896
897                                                         if(g_minstagib)
898                                                         if(victim.items & IT_STRENGTH)
899                                                                 yoda = 1;
900                                                 }
901                                         }
902                                 }
903                                 else
904                                 {
905                                         if(deathtype != DEATH_FIRE)
906                                         {
907                                                 attacker.typehitsound += 1;
908                                         }
909                                         if(mirrordamage > 0)
910                                                 if(time > attacker.teamkill_complain)
911                                                 {
912                                                         attacker.teamkill_complain = time + 5;
913                                                         attacker.teamkill_soundtime = time + 0.4;
914                                                         attacker.teamkill_soundsource = targ;
915                                                 }
916                                 }
917                         }
918                 }
919         }
920
921         // apply push
922         if (self.damageforcescale)
923         if (vlen(force))
924         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
925         {
926                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
927                 if(self.movetype == MOVETYPE_PHYSICS)
928                 {
929                         entity farcent;
930                         farcent = spawn();
931                         farcent.classname = "farce";
932                         farcent.enemy = self;
933                         farcent.movedir = farce * 10;
934                         if(self.mass)
935                                 farcent.movedir = farcent.movedir * self.mass;
936                         farcent.origin = hitloc;
937                         farcent.forcetype = FORCETYPE_FORCEATPOS;
938                         farcent.nextthink = time + 0.1;
939                         farcent.think = SUB_Remove;
940                 }
941                 else
942                         self.velocity = self.velocity + farce;
943                 self.flags &~= FL_ONGROUND;
944                 UpdateCSQCProjectile(self);
945         }
946         // apply damage
947         if (damage != 0 || (self.damageforcescale && vlen(force)))
948         if (self.event_damage)
949                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
950         self = oldself;
951
952         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
953         {
954                 if(g_runematch)
955                 {
956                         if (attacker.runes & RUNE_VAMPIRE)
957                         {
958                         // apply vampire rune
959                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
960                                 {
961                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb;
962                                         attacker.health = bound(
963                                                 autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 40
964                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb,
965                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
966                                 }
967                                 else
968                                 {
969                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_absorb;
970                                         attacker.health = bound(
971                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
972                                                                                         // empathy won't let you gain health in the same way...
973                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_absorb,
974                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
975                                         }
976                         }
977                         // apply empathy curse
978                         else if (attacker.runes & CURSE_EMPATHY)
979                         {
980                                 attacker.health = bound(
981                                         autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 20
982                                         attacker.health + damage * autocvar_g_balance_curse_empathy_takedamage,
983                                         attacker.health);
984                         }
985                 }
986         }
987
988         // apply mirror damage if any
989         if(mirrordamage > 0 || mirrorforce > 0)
990         {
991                 attacker = attacker_save;
992                 if(g_minstagib)
993                 if(mirrordamage > 0)
994                 {
995                         // just lose extra LIVES, don't kill the player for mirror damage
996                         if(attacker.armorvalue > 0)
997                         {
998                                 attacker.armorvalue = attacker.armorvalue - 1;
999                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
1000                                 attacker.hitsound += 1;
1001                         }
1002                         mirrordamage = 0;
1003                 }
1004
1005                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
1006                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
1007         }
1008 }
1009
1010 float RadiusDamage_running;
1011 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
1012         // Returns total damage applies to creatures
1013 {
1014         entity  targ;
1015         vector  blastorigin;
1016         vector  force;
1017         float   total_damage_to_creatures;
1018         entity  next;
1019         float   tfloordmg;
1020         float   tfloorforce;
1021
1022         float stat_damagedone;
1023
1024         if(RadiusDamage_running)
1025         {
1026                 backtrace("RadiusDamage called recursively! Expect stuff to go HORRIBLY wrong.");
1027                 return 0;
1028         }
1029
1030         RadiusDamage_running = 1;
1031
1032         tfloordmg = autocvar_g_throughfloor_damage;
1033         tfloorforce = autocvar_g_throughfloor_force;
1034
1035         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1036         total_damage_to_creatures = 0;
1037
1038         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1039                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1040                 {
1041                         force = inflictor.velocity;
1042                         if(vlen(force) == 0)
1043                                 force = '0 0 -1';
1044                         else
1045                                 force = normalize(force);
1046                         if(forceintensity >= 0)
1047                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
1048                         else
1049                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
1050                 }
1051
1052         stat_damagedone = 0;
1053
1054         targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
1055         while (targ)
1056         {
1057                 next = targ.chain;
1058                 if (targ != inflictor)
1059                         if (ignore != targ) if(targ.takedamage)
1060                         {
1061                                 vector nearest;
1062                                 vector diff;
1063                                 float power;
1064
1065                                 // LordHavoc: measure distance to nearest point on target (not origin)
1066                                 // (this guarentees 100% damage on a touch impact)
1067                                 nearest = targ.WarpZone_findradius_nearest;
1068                                 diff = targ.WarpZone_findradius_dist;
1069                                 // round up a little on the damage to ensure full damage on impacts
1070                                 // and turn the distance into a fraction of the radius
1071                                 power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
1072                                 //bprint(" ");
1073                                 //bprint(ftos(power));
1074                                 //if (targ == attacker)
1075                                 //      print(ftos(power), "\n");
1076                                 if (power > 0)
1077                                 {
1078                                         float finaldmg;
1079                                         if (power > 1)
1080                                                 power = 1;
1081                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1082                                         if (finaldmg > 0)
1083                                         {
1084                                                 float a;
1085                                                 float c;
1086                                                 vector hitloc;
1087                                                 vector myblastorigin;
1088                                                 vector center;
1089
1090                                                 myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
1091
1092                                                 // if it's a player, use the view origin as reference
1093                                                 center = CENTER_OR_VIEWOFS(targ);
1094
1095                                                 force = normalize(center - myblastorigin);
1096                                                 force = force * (finaldmg / coredamage) * forceintensity;
1097                                                 hitloc = nearest;
1098
1099                                                 if(targ != directhitentity)
1100                                                 {
1101                                                         float hits;
1102                                                         float total;
1103                                                         float hitratio;
1104                                                         float mininv_f, mininv_d;
1105
1106                                                         // test line of sight to multiple positions on box,
1107                                                         // and do damage if any of them hit
1108                                                         hits = 0;
1109
1110                                                         // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1111                                                         // so for a given max stddev:
1112                                                         // n = (1 / (2 * max stddev of hitratio))^2
1113
1114                                                         mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1115                                                         mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1116
1117                                                         if(autocvar_g_throughfloor_debug)
1118                                                                 print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1119
1120                                                         total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1121
1122                                                         if(autocvar_g_throughfloor_debug)
1123                                                                 print(sprintf(" steps=%f", total));
1124
1125                                                         if (targ.classname == "player")
1126                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1127                                                         else
1128                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1129
1130                                                         if(autocvar_g_throughfloor_debug)
1131                                                                 print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1132
1133                                                         for(c = 0; c < total; ++c)
1134                                                         {
1135                                                                 //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1136                                                                 WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1137                                                                 if (trace_fraction == 1 || trace_ent == targ)
1138                                                                 {
1139                                                                         ++hits;
1140                                                                         if (hits > 1)
1141                                                                                 hitloc = hitloc + nearest;
1142                                                                         else
1143                                                                                 hitloc = nearest;
1144                                                                 }
1145                                                                 nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1146                                                                 nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1147                                                                 nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1148                                                         }
1149
1150                                                         nearest = hitloc * (1 / max(1, hits));
1151                                                         hitratio = (hits / total);
1152                                                         a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1153                                                         finaldmg = finaldmg * a;
1154                                                         a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1155                                                         force = force * a;
1156
1157                                                         if(autocvar_g_throughfloor_debug)
1158                                                                 print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1159                                                 }
1160
1161                                                 // laser force adjustments :P
1162                                                 if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1163                                                 {
1164                                                         if (targ == attacker)
1165                                                         {
1166                                                                 vector vel;
1167
1168                                                                 float force_zscale;
1169                                                                 float force_velocitybiasramp;
1170                                                                 float force_velocitybias;
1171
1172                                                                 force_velocitybiasramp = autocvar_sv_maxspeed;
1173                                                                 if(deathtype & HITTYPE_SECONDARY)
1174                                                                 {
1175                                                                         force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1176                                                                         force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1177                                                                 }
1178                                                                 else
1179                                                                 {
1180                                                                         force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1181                                                                         force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1182                                                                 }
1183
1184                                                                 vel = targ.velocity;
1185                                                                 vel_z = 0;
1186                                                                 vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1187                                                                 force =
1188                                                                         vlen(force)
1189                                                                         *
1190                                                                         normalize(normalize(force) + vel);
1191
1192                                                                 force_z *= force_zscale;
1193                                                         }
1194                                                         else
1195                                                         {
1196                                                                 if(deathtype & HITTYPE_SECONDARY)
1197                                                                 {
1198                                                                         force *= autocvar_g_balance_laser_secondary_force_other_scale;
1199                                                                 }
1200                                                                 else
1201                                                                 {
1202                                                                         force *= autocvar_g_balance_laser_primary_force_other_scale;
1203                                                                 }
1204                                                         }
1205                                                 }
1206
1207                                                 //if (targ == attacker)
1208                                                 //{
1209                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1210                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1211                                                 //      print(" (", ftos(a), ")\n");
1212                                                 //}
1213                                                 if(finaldmg || vlen(force))
1214                                                 {
1215                                                         if(targ.iscreature)
1216                                                         {
1217                                                                 total_damage_to_creatures += finaldmg;
1218
1219                                                                 if(accuracy_isgooddamage(attacker, targ))
1220                                                                         stat_damagedone += finaldmg;
1221                                                         }
1222
1223                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1224                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1225                                                         else
1226                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1227                                                 }
1228                                         }
1229                                 }
1230                         }
1231                 targ = next;
1232         }
1233
1234         RadiusDamage_running = 0;
1235
1236         if(!DEATH_ISSPECIAL(deathtype))
1237                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1238
1239         return total_damage_to_creatures;
1240 }
1241
1242 .float fire_damagepersec;
1243 .float fire_endtime;
1244 .float fire_deathtype;
1245 .entity fire_owner;
1246 .float fire_hitsound;
1247 .entity fire_burner;
1248
1249 void fireburner_think();
1250
1251 float Fire_IsBurning(entity e)
1252 {
1253         return (time < e.fire_endtime);
1254 }
1255
1256 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1257 {
1258         float dps;
1259         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1260
1261         if(e.classname == "player")
1262         {
1263                 if(e.deadflag)
1264                         return -1;
1265         }
1266         else
1267         {
1268                 if(!e.fire_burner)
1269                 {
1270                         // print("adding a fire burner to ", e.classname, "\n");
1271                         e.fire_burner = spawn();
1272                         e.fire_burner.classname = "fireburner";
1273                         e.fire_burner.think = fireburner_think;
1274                         e.fire_burner.nextthink = time;
1275                         e.fire_burner.owner = e;
1276                 }
1277         }
1278
1279         t = max(t, 0.1);
1280         dps = d / t;
1281         if(Fire_IsBurning(e))
1282         {
1283                 mintime = e.fire_endtime - time;
1284                 maxtime = max(mintime, t);
1285
1286                 mindps = e.fire_damagepersec;
1287                 maxdps = max(mindps, dps);
1288
1289                 if(maxtime > mintime || maxdps > mindps)
1290                 {
1291                         // Constraints:
1292                         
1293                         // damage we have right now
1294                         mindamage = mindps * mintime;
1295
1296                         // damage we want to get
1297                         maxdamage = mindamage + d;
1298
1299                         // but we can't exceed maxtime * maxdps!
1300                         totaldamage = min(maxdamage, maxtime * maxdps);
1301
1302                         // LEMMA:
1303                         // Look at:
1304                         // totaldamage = min(mindamage + d, maxtime * maxdps)
1305                         // We see:
1306                         // totaldamage <= maxtime * maxdps
1307                         // ==> totaldamage / maxdps <= maxtime.
1308                         // We also see:
1309                         // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
1310                         //                     >= min(mintime, maxtime)
1311                         // ==> totaldamage / maxdps >= mintime.
1312
1313                         /*
1314                         // how long do we damage then?
1315                         // at least as long as before
1316                         // but, never exceed maxdps
1317                         totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
1318                         */
1319
1320                         // alternate:
1321                         // at most as long as maximum allowed
1322                         // but, never below mindps
1323                         totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
1324
1325                         // assuming t > mintime, dps > mindps:
1326                         // we get d = t * dps = maxtime * maxdps
1327                         // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
1328                         // totaldamage / maxdps = maxtime
1329                         // totaldamage / mindps > totaldamage / maxdps = maxtime
1330                         // FROM THIS:
1331                         // a) totaltime = max(mintime, maxtime) = maxtime
1332                         // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
1333
1334                         // assuming t <= mintime:
1335                         // we get maxtime = mintime
1336                         // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
1337                         // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
1338
1339                         // assuming dps <= mindps:
1340                         // we get mindps = maxdps.
1341                         // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
1342                         // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
1343                         // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
1344
1345                         e.fire_damagepersec = totaldamage / totaltime;
1346                         e.fire_endtime = time + totaltime;
1347                         if(totaldamage > 1.2 * mindamage)
1348                         {
1349                                 e.fire_deathtype = dt;
1350                                 if(e.fire_owner != o)
1351                                 {
1352                                         e.fire_owner = o;
1353                                         e.fire_hitsound = FALSE;
1354                                 }
1355                         }
1356                         if(accuracy_isgooddamage(o, e))
1357                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1358                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1359                 }
1360                 else
1361                         return 0;
1362         }
1363         else
1364         {
1365                 e.fire_damagepersec = dps;
1366                 e.fire_endtime = time + t;
1367                 e.fire_deathtype = dt;
1368                 e.fire_owner = o;
1369                 e.fire_hitsound = FALSE;
1370                 if(accuracy_isgooddamage(o, e))
1371                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1372                 return d;
1373         }
1374 }
1375
1376 void Fire_ApplyDamage(entity e)
1377 {
1378         float t, d, hi, ty;
1379         entity o;
1380
1381         if not(Fire_IsBurning(e))
1382                 return;
1383
1384         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1385         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1386                 o = e.fire_owner;
1387
1388         // water and slime stop fire
1389         if(e.waterlevel)
1390         if(e.watertype != CONTENT_LAVA)
1391                 e.fire_endtime = 0;
1392
1393         // ice stops fire
1394         if(e.freezetag_frozen)
1395                 e.fire_endtime = 0;
1396
1397         t = min(frametime, e.fire_endtime - time);
1398         d = e.fire_damagepersec * t;
1399
1400         hi = e.fire_owner.hitsound;
1401         ty = e.fire_owner.typehitsound;
1402         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1403         if(e.fire_hitsound && e.fire_owner)
1404         {
1405                 e.fire_owner.hitsound = hi;
1406                 e.fire_owner.typehitsound = ty;
1407         }
1408         e.fire_hitsound = TRUE;
1409
1410         if not(IS_INDEPENDENT_PLAYER(e))
1411         FOR_EACH_PLAYER(other) if(e != other)
1412         {
1413                 if(other.classname == "player")
1414                 if(other.deadflag == DEAD_NO)
1415                 if not(IS_INDEPENDENT_PLAYER(other))
1416                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1417                 {
1418                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1419                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1420                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1421                 }
1422         }
1423 }
1424
1425 void Fire_ApplyEffect(entity e)
1426 {
1427         if(Fire_IsBurning(e))
1428                 e.effects |= EF_FLAME;
1429         else
1430                 e.effects &~= EF_FLAME;
1431 }
1432
1433 void fireburner_think()
1434 {
1435         // for players, this is done in the regular loop
1436         if(wasfreed(self.owner))
1437         {
1438                 remove(self);
1439                 return;
1440         }
1441         Fire_ApplyEffect(self.owner);
1442         if(!Fire_IsBurning(self.owner))
1443         {
1444                 self.owner.fire_burner = world;
1445                 remove(self);
1446                 return;
1447         }
1448         Fire_ApplyDamage(self.owner);
1449         self.nextthink = time;
1450 }