]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_damage.qc
Merge branch 'master' into mario/monsters
[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=", ftos(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 void Send_KillNotification (string s1, string s2, string s3, float msg, float type)
301 {
302         WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);
303         WriteByte(MSG_BROADCAST, TE_CSQC_KILLNOTIFY);
304         WriteString(MSG_BROADCAST, s1);
305         WriteString(MSG_BROADCAST, s2);
306         WriteString(MSG_BROADCAST, s3);
307         WriteShort(MSG_BROADCAST, msg);
308         WriteByte(MSG_BROADCAST, type);
309 }
310
311 // Function is used to send a generic centerprint whose content CSQC gets to decide (gentle version or not in the below cases)
312 void Send_CSQC_KillCenterprint(entity e, string s1, string s2, float msg, float type)
313 {
314         if (clienttype(e) == CLIENTTYPE_REAL)
315         {
316                 msg_entity = e;
317                 WRITESPECTATABLE_MSG_ONE({
318                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
319                         WriteByte(MSG_ONE, TE_CSQC_KILLCENTERPRINT);
320                         WriteString(MSG_ONE, s1);
321                         WriteString(MSG_ONE, s2);
322                         WriteShort(MSG_ONE, msg);
323                         WriteByte(MSG_ONE, type);
324                 });
325         }
326 }
327
328 void Obituary (entity attacker, entity inflictor, entity targ, float deathtype)
329 {
330         string  s, a, msg;
331         float type;
332
333         if (targ.classname == "player")
334         {
335                 s = targ.netname;
336                 a = attacker.netname;
337
338                 if (targ == attacker) // suicides
339                 {
340                         if (deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
341                                 msg = ColoredTeamName(targ.team); // TODO: check if needed?
342                         else
343                                 msg = "";
344             if(!g_cts) // no "killed your own dumb self" message in CTS
345                 Send_CSQC_KillCenterprint(targ, msg, "", deathtype, MSG_SUICIDE);
346
347                         if(deathtype != DEATH_TEAMCHANGE && deathtype != DEATH_QUIET)
348                         {
349                                 LogDeath("suicide", deathtype, targ, targ);
350                                 GiveFrags(attacker, targ, -1, deathtype);
351                         }
352
353                         if (targ.killcount > 2)
354                                 msg = ftos(targ.killcount);
355                         else
356                                 msg = "";
357                         if(teamplay && deathtype == DEATH_MIRRORDAMAGE)
358                         {
359                                 if(attacker.team == COLOR_TEAM1)
360                                         deathtype = KILL_TEAM_RED;
361                                 else
362                                         deathtype = KILL_TEAM_BLUE;
363                         }
364
365                         Send_KillNotification(s, msg, "", deathtype, MSG_SUICIDE);
366                 }
367                 else if (attacker.classname == "player")
368                 {
369                         if(!IsDifferentTeam(attacker, targ))
370                         {
371                                 if(attacker.team == COLOR_TEAM1)
372                                         type = KILL_TEAM_RED;
373                                 else
374                                         type = KILL_TEAM_BLUE;
375
376                                 GiveFrags(attacker, targ, -1, deathtype);
377
378                                 Send_CSQC_KillCenterprint(attacker, s, "", type, MSG_KILL);
379
380                                 if (targ.killcount > 2)
381                                         msg = ftos(targ.killcount);
382                                 else
383                                         msg = "";
384
385                                 if (attacker.killcount > 2) {
386                                         msg = ftos(attacker.killcount);
387                                         type = KILL_TEAM_SPREE;
388                                 }
389                                 Send_KillNotification(a, s, msg, type, MSG_KILL);
390
391                                 attacker.killcount = 0;
392
393                                 LogDeath("tk", deathtype, attacker, targ);
394                         }
395                         else
396                         {
397                                 if (!checkrules_firstblood)
398                                 {
399                                         checkrules_firstblood = TRUE;
400                                         Send_KillNotification(a, "", "", KILL_FIRST_BLOOD, MSG_KILL);
401                                         // TODO: make these print a newline if they dont
402                                         Send_CSQC_KillCenterprint(attacker, "", "", KILL_FIRST_BLOOD, MSG_KILL);
403                                         Send_CSQC_KillCenterprint(targ, "", "", KILL_FIRST_VICTIM, MSG_KILL);
404                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD, 1);
405                                         PlayerStats_Event(targ, PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM, 1);
406                                 }
407
408                                 if(targ.istypefrag) {
409                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_TYPEFRAG, MSG_KILL);
410                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_TYPEFRAGGED, MSG_KILL);
411                                 } else {
412                                         Send_CSQC_KillCenterprint(attacker, s, Obituary_ExtraFragInfo(targ), KILL_FRAG, MSG_KILL);
413                                         Send_CSQC_KillCenterprint(targ, a, Obituary_ExtraFragInfo(attacker), KILL_FRAGGED, MSG_KILL);
414                                 }
415
416                                 attacker.taunt_soundtime = time + 1;
417
418                                 if (deathtype == DEATH_HURTTRIGGER && inflictor.message2 != "")
419                                         msg = inflictor.message2;
420                                 else if (deathtype == DEATH_CUSTOM)
421                                         msg = deathmessage;
422                                 else
423                                         msg = "";
424
425                                 if(strstrofs(msg, "%", 0) < 0)
426                                         msg = strcat("%s ", msg, " by %s");
427
428                                 Send_KillNotification(a, s, msg, deathtype, MSG_KILL);
429
430                                 GiveFrags(attacker, targ, 1, deathtype);
431
432                                 if (targ.killcount > 2) {
433                                         Send_KillNotification(s, ftos(targ.killcount), a, KILL_END_SPREE, MSG_SPREE);
434                                 }
435
436                                 attacker.killcount = attacker.killcount + 1;
437
438                                 if (attacker.killcount == 3)
439                                 {
440                                         Send_KillNotification(a, "", "", KILL_SPREE_3, MSG_SPREE);
441                                         AnnounceTo(attacker, "03kills");
442                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3, 1);
443                                 }
444                                 else if (attacker.killcount == 5)
445                                 {
446                                         Send_KillNotification(a, "", "", KILL_SPREE_5, MSG_SPREE);
447                                         AnnounceTo(attacker, "05kills");
448                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5, 1);
449                                 }
450                                 else if (attacker.killcount == 10)
451                                 {
452                                         Send_KillNotification(a, "", "", KILL_SPREE_10, MSG_SPREE);
453                                         AnnounceTo(attacker, "10kills");
454                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10, 1);
455                                 }
456                                 else if (attacker.killcount == 15)
457                                 {
458                                         Send_KillNotification(a, "", "", KILL_SPREE_15, MSG_SPREE);
459                                         AnnounceTo(attacker, "15kills");
460                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15, 1);
461                                 }
462                                 else if (attacker.killcount == 20)
463                                 {
464                                         Send_KillNotification(a, "", "", KILL_SPREE_20, MSG_SPREE);
465                                         AnnounceTo(attacker, "20kills");
466                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20, 1);
467                                 }
468                                 else if (attacker.killcount == 25)
469                                 {
470                                         Send_KillNotification(a, "", "", KILL_SPREE_25, MSG_SPREE);
471                                         AnnounceTo(attacker, "25kills");
472                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25, 1);
473                                 }
474                                 else if (attacker.killcount == 30)
475                                 {
476                                         Send_KillNotification(a, "", "", KILL_SPREE_30, MSG_SPREE);
477                                         AnnounceTo(attacker, "30kills");
478                                         PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30, 1);
479                                 }
480                                 else if (attacker.killcount > 2) {
481                                         Send_KillNotification(a, ftos(attacker.killcount), "", KILL_SPREE, MSG_SPREE);
482                                 }
483                                 LogDeath("frag", deathtype, attacker, targ);
484                         }
485                 }
486                 else
487                 {
488                         Send_CSQC_KillCenterprint(targ, "", "", deathtype, MSG_KILL_ACTION);
489                         if (deathtype == DEATH_HURTTRIGGER && inflictor.message != "")
490                                 msg = inflictor.message;
491                         else if (deathtype == DEATH_CUSTOM)
492                                 msg = deathmessage;
493                         else
494                                 msg = "";
495                         if(strstrofs(msg, "%", 0) < 0)
496                                 msg = strcat("%s ", msg);
497
498                         GiveFrags(targ, targ, -1, deathtype);
499                         if(PlayerScore_Add(targ, SP_SCORE, 0) == -5) {
500                                 AnnounceTo(targ, "botlike");
501                                 PlayerStats_Event(attacker, PLAYERSTATS_ACHIEVEMENT_BOTLIKE, 1);
502                         }
503                         Send_KillNotification(s, msg, "", deathtype, MSG_KILL_ACTION);
504
505                         if (targ.killcount > 2)
506                                 Send_KillNotification(s, ftos(targ.killcount), "", 0, MSG_KILL_ACTION_SPREE);
507
508                         LogDeath("accident", deathtype, targ, targ);
509                 }
510
511                 targ.death_origin = targ.origin;
512                 if(targ != attacker)
513                         targ.killer_origin = attacker.origin;
514
515                 // FIXME: this should go in PutClientInServer
516                 if (targ.killcount)
517                         targ.killcount = 0;
518         }
519 }
520
521 void Ice_Think()
522 {
523         if(self.owner.health < 1)
524         {
525                 remove(self);
526                 return;
527         }
528         setorigin(self, self.owner.origin - '0 0 16');
529         self.nextthink = time;
530 }
531
532 void Freeze (entity targ, float freeze_time)
533 {
534         float monster = (targ.flags & FL_MONSTER);
535         float player = (targ.flags & FL_CLIENT);
536         
537         if(!player && !monster) // only specified entities can be freezed
538                 return;
539                 
540         if(targ.frozen || targ.freezetag_frozen)
541                 return;
542                 
543         targ.frozen = 1;
544         targ.revive_progress = 0;
545         targ.health = 1;
546         targ.revive_speed = freeze_time;
547
548         entity ice;
549         ice = spawn();
550         ice.owner = targ;
551         ice.classname = "ice";
552         ice.scale = targ.scale;
553         ice.think = Ice_Think;
554         ice.nextthink = time;
555         ice.frame = floor(random() * 21); // ice model has 20 different looking frames
556         setmodel(ice, "models/ice/ice.md3");
557
558         entity oldself;
559         oldself = self;
560         self = ice;
561         Ice_Think();
562         self = oldself;
563
564         RemoveGrapplingHook(targ);
565 }
566
567 void Unfreeze (entity targ)
568 {
569         targ.frozen = 0;
570         targ.revive_progress = 0;
571         targ.health = ((targ.classname == STR_PLAYER) ? autocvar_g_balance_health_start : targ.max_health);
572
573         // remove the ice block
574         entity ice;
575         for(ice = world; (ice = find(ice, classname, "ice")); ) if(ice.owner == targ)
576         {
577                 remove(ice);
578                 break;
579         }
580 }
581
582 // these are updated by each Damage call for use in button triggering and such
583 entity damage_targ;
584 entity damage_inflictor;
585 entity damage_attacker;
586
587 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
588 {
589         float mirrordamage;
590         float mirrorforce;
591         float complainteamdamage = 0; 
592         entity attacker_save;
593         mirrordamage = 0;
594         mirrorforce = 0;
595
596         if (gameover || targ.killcount == -666)
597                 return;
598
599         entity oldself;
600         oldself = self;
601         self = targ;
602         damage_targ = targ;
603         damage_inflictor = inflictor;
604         damage_attacker = attacker;
605                 attacker_save = attacker;
606
607         if(targ.classname == "player")
608                 if(targ.hook)
609                         if(targ.hook.aiment)
610                                 if(targ.hook.aiment == attacker)
611                                         RemoveGrapplingHook(targ); // STOP THAT, you parasite!
612
613         // special rule: gravity bomb does not hit team mates (other than for disconnecting the hook)
614         if(DEATH_ISWEAPON(deathtype, WEP_HOOK) || DEATH_ISWEAPON(deathtype, WEP_TUBA))
615         {
616                 if(targ.classname == "player")
617                         if not(IsDifferentTeam(targ, attacker))
618                         {
619                                 self = oldself;
620                                 return;
621                         }
622         }
623
624         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE || deathtype == DEATH_QUIET)
625         {
626                 // These are ALWAYS lethal
627                 // No damage modification here
628                 // Instead, prepare the victim for his death...
629                 targ.armorvalue = 0;
630                 targ.spawnshieldtime = 0;
631                 targ.health = 0.9; // this is < 1
632                 targ.flags -= targ.flags & FL_GODMODE;
633                 damage = 100000;
634         }
635         else if(deathtype == DEATH_MIRRORDAMAGE || deathtype == DEATH_NOAMMO)
636         {
637                 // no processing
638         }
639         else
640         {
641                 /*
642                 skill based bot damage? gtfo. (tZork)
643                 if (targ.classname == "player")
644                 if (attacker.classname == "player")
645                 if (!targ.isbot)
646                 if (attacker.isbot)
647                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
648         */
649         
650                 // nullify damage if teamplay is on
651                 if(deathtype != DEATH_TELEFRAG)
652                 if(attacker.classname == "player")
653                 {
654                         if(targ.classname == "player" && targ != attacker && (IS_INDEPENDENT_PLAYER(attacker) || IS_INDEPENDENT_PLAYER(targ)))
655                         {
656                                 damage = 0;
657                                 force = '0 0 0';
658                         }
659                         else if(!IsDifferentTeam(attacker, targ))
660                         {
661                                 if(autocvar_teamplay_mode == 1)
662                                         damage = 0;
663                                 else if(attacker != targ)
664                                 {
665                                         if(autocvar_teamplay_mode == 3)
666                                                 damage = 0;
667                                         else if(autocvar_teamplay_mode == 4)
668                                         {
669                                                 if(targ.classname == "player" && targ.deadflag == DEAD_NO)
670                                                 {
671                                                         attacker.dmg_team = attacker.dmg_team + damage;
672                                                         complainteamdamage = attacker.dmg_team - autocvar_g_teamdamage_threshold;
673                                                         if(complainteamdamage > 0 && !g_ca) // FIXME why is g_ca ruled out here? Why not just g_mirrordamage 0 on CA servers?
674                                                                 mirrordamage = autocvar_g_mirrordamage * complainteamdamage;
675                                                         mirrorforce = autocvar_g_mirrordamage * vlen(force);
676                                                         if(g_minstagib)
677                                                         {
678                                                                 if(autocvar_g_friendlyfire == 0)
679                                                                         damage = 0;
680                                                         }
681                                                         else if(g_ca)
682                                                                 damage = 0;
683                                                         else
684                                                                 damage = autocvar_g_friendlyfire * damage;
685                                                         // mirrordamage will be used LATER
686
687                                                         if(autocvar_g_mirrordamage_virtual)
688                                                         {
689                                                                 vector v  = healtharmor_applydamage(attacker.armorvalue, autocvar_g_balance_armor_blockpercent, mirrordamage);
690                                                                 attacker.dmg_take += v_x;
691                                                                 attacker.dmg_save += v_y;
692                                                                 attacker.dmg_inflictor = inflictor;
693                                                                 mirrordamage = v_z; // = 0, to make fteqcc stfu
694                                                                 mirrorforce = 0;
695                                                         }
696
697                                                         if(autocvar_g_friendlyfire_virtual)
698                                                         {
699                                                                 vector v = healtharmor_applydamage(targ.armorvalue, autocvar_g_balance_armor_blockpercent, damage);
700                                                                 targ.dmg_take += v_x;
701                                                                 targ.dmg_save += v_y;
702                                                                 targ.dmg_inflictor = inflictor;
703                                                                 damage = 0;
704                                                                 if(!autocvar_g_friendlyfire_virtual_force)
705                                                                         force = '0 0 0';
706                                                         }
707                                                 }
708                                                 else
709                                                         damage = 0;
710                                         }
711                                 }
712                         }
713                 }
714
715                 if(targ.classname == "player")
716                 if(attacker.classname == "player")
717                 if(attacker != targ)
718                 {
719                         targ.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
720                         attacker.lms_traveled_distance = autocvar_g_lms_campcheck_distance;
721                 }
722
723                 if(targ.classname == "player")
724                 if (g_minstagib)
725                 {
726                         if ((deathtype == DEATH_FALL)  ||
727                                 (deathtype == DEATH_DROWN) ||
728                                 (deathtype == DEATH_SLIME) ||
729                                 (deathtype == DEATH_LAVA)  ||
730                                 (!DEATH_ISWEAPON(deathtype, WEP_LASER) && damage > 0 && damage < 100))
731                         {
732                                 self = oldself;
733                                 return;
734                         }
735                         if(damage > 0)
736                             damage = 10000;
737                         if (targ.armorvalue && (deathtype == WEP_MINSTANEX) && damage)
738                         {
739                                 targ.armorvalue -= 1;
740                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue)));
741                                 damage = 0;
742                                 targ.hitsound += 1;
743                                 attacker.hitsound += 1; // TODO change this to a future specific hitsound for armor hit
744                         }
745                         if (DEATH_ISWEAPON(deathtype, WEP_LASER))
746                         {
747                                 damage = 0;
748                                 mirrordamage = 0;
749                                 complainteamdamage = 0;
750                                 if (targ != attacker)
751                                 {
752                                         if ((targ.health >= 1) && (targ.classname == "player"))
753                                                 centerprint(attacker, "Secondary fire inflicts no damage!");
754                                         force = '0 0 0';
755                                         // keep mirrorforce
756                                         attacker = targ;
757                                 }
758                         }
759                 }
760
761                 if not(DEATH_ISSPECIAL(deathtype))
762                 {
763                         damage *= g_weapondamagefactor;
764                         mirrordamage *= g_weapondamagefactor;
765                         complainteamdamage *= g_weapondamagefactor;
766                         force = force * g_weaponforcefactor;
767                         mirrorforce *= g_weaponforcefactor;
768                 }
769                 
770                 if(targ.frozen && attacker.classname != "monster_spider")
771                 {
772                         damage = 0;
773                         force *= 0.2;
774                 }
775                 
776                 // should this be changed at all? If so, in what way?
777                 frag_attacker = attacker;
778                 frag_target = targ;
779                 frag_damage = damage;
780                 frag_force = force;
781         frag_deathtype = deathtype;
782                 MUTATOR_CALLHOOK(PlayerDamage_Calculate);
783                 damage = frag_damage;
784                 force = frag_force;
785                 
786                 // apply strength multiplier
787                 if ((attacker.items & IT_STRENGTH) && !g_minstagib)
788                 {
789                         if(targ == attacker)
790                         {
791                                 damage = damage * autocvar_g_balance_powerup_strength_selfdamage;
792                                 force = force * autocvar_g_balance_powerup_strength_selfforce;
793                         }
794                         else
795                         {
796                                 damage = damage * autocvar_g_balance_powerup_strength_damage;
797                                 force = force * autocvar_g_balance_powerup_strength_force;
798                         }
799                 }
800
801                 // apply invincibility multiplier
802                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
803                         damage = damage * autocvar_g_balance_powerup_invincible_takedamage;
804
805                 if (targ == attacker)
806                 {
807                         if(g_ca || (g_cts && !autocvar_g_cts_selfdamage))
808                                 damage = 0;
809                         else
810                                 damage = damage * autocvar_g_balance_selfdamagepercent; // Partial damage if the attacker hits himself
811                 }
812
813                 if(g_runematch)
814                 {
815                         // apply strength rune
816                         if (attacker.runes & RUNE_STRENGTH)
817                         {
818                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
819                                 {
820                                         damage = damage * autocvar_g_balance_rune_strength_combo_damage;
821                                         force = force * autocvar_g_balance_rune_strength_combo_force;
822                                 }
823                                 else
824                                 {
825                                         damage = damage * autocvar_g_balance_rune_strength_damage;
826                                         force = force * autocvar_g_balance_rune_strength_force;
827                                 }
828                         }
829                         else if (attacker.runes & CURSE_WEAK)
830                         {
831                                 damage = damage * autocvar_g_balance_curse_weak_damage;
832                                 force = force * autocvar_g_balance_curse_weak_force;
833                         }
834
835                         // apply defense rune
836                         if (targ.runes & RUNE_DEFENSE)
837                         {
838                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
839                                         damage = damage * autocvar_g_balance_rune_defense_combo_takedamage;
840                                 else
841                                         damage = damage * autocvar_g_balance_rune_defense_takedamage;
842                         }
843                         else if (targ.runes & CURSE_VULNER)
844                                 damage = damage * autocvar_g_balance_curse_vulner_takedamage;
845                 }
846
847                 // count the damage
848                 if(attacker)
849                 if(!targ.deadflag)
850                 if(targ.takedamage == DAMAGE_AIM)
851                 if(targ != attacker)
852                 {
853                         entity victim;
854                         if((targ.vehicle_flags & VHF_ISVEHICLE) && targ.owner)
855                                 victim = targ.owner;
856                         else
857                                 victim = targ;
858
859                         if(victim.classname == "player" || victim.turrcaps_flags & TFL_TURRCAPS_ISTURRET || victim.flags & FL_MONSTER)
860                         {
861                                 if(IsDifferentTeam(victim, attacker))
862                                 {
863                                         if(damage > 0)
864                                         {
865                                                 if(deathtype != DEATH_FIRE)
866                                                 {
867                                                         if(victim.BUTTON_CHAT)
868                                                                 attacker.typehitsound += 1;
869                                                         else
870                                                                 attacker.hitsound += 1;
871                                                 }
872
873                                                 damage_goodhits += 1;
874                                                 damage_gooddamage += damage;
875
876                                                 if not(DEATH_ISSPECIAL(deathtype))
877                                                 {
878                                                         if(targ.classname == "player") // don't do this for vehicles
879                                                         if(!g_minstagib)
880                                                         if(IsFlying(victim))
881                                                                 yoda = 1;
882
883                                                         if(g_minstagib)
884                                                         if(victim.items & IT_STRENGTH)
885                                                                 yoda = 1;
886                                                 }
887                                         }
888                                 }
889                                 else
890                                 {
891                                         if(deathtype != DEATH_FIRE)
892                                         {
893                                                 attacker.typehitsound += 1;
894                                         }
895                                         if(complainteamdamage > 0)
896                                                 if(time > attacker.teamkill_complain)
897                                                 {
898                                                         attacker.teamkill_complain = time + 5;
899                                                         attacker.teamkill_soundtime = time + 0.4;
900                                                         attacker.teamkill_soundsource = targ;
901                                                 }
902                                 }
903                         }
904                 }
905         }
906
907         // apply push
908         if (self.damageforcescale)
909         if (vlen(force))
910         if (self.classname != "player" || time >= self.spawnshieldtime || g_midair)
911         {
912                 vector farce = damage_explosion_calcpush(self.damageforcescale * force, self.velocity, autocvar_g_balance_damagepush_speedfactor);
913                 if(self.movetype == MOVETYPE_PHYSICS)
914                 {
915                         entity farcent;
916                         farcent = spawn();
917                         farcent.classname = "farce";
918                         farcent.enemy = self;
919                         farcent.movedir = farce * 10;
920                         if(self.mass)
921                                 farcent.movedir = farcent.movedir * self.mass;
922                         farcent.origin = hitloc;
923                         farcent.forcetype = FORCETYPE_FORCEATPOS;
924                         farcent.nextthink = time + 0.1;
925                         farcent.think = SUB_Remove;
926                 }
927                 else
928                         self.velocity = self.velocity + farce;
929                 self.flags &~= FL_ONGROUND;
930                 UpdateCSQCProjectile(self);
931         }
932         // apply damage
933         if (damage != 0 || (self.damageforcescale && vlen(force)))
934         if (self.event_damage)
935                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
936         self = oldself;
937
938         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
939         {
940                 if(g_runematch)
941                 {
942                         if (attacker.runes & RUNE_VAMPIRE)
943                         {
944                         // apply vampire rune
945                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
946                                 {
947                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb;
948                                         attacker.health = bound(
949                                                 autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 40
950                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_combo_absorb,
951                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
952                                 }
953                                 else
954                                 {
955                                         //attacker.health = attacker.health + damage * autocvar_g_balance_rune_vampire_absorb;
956                                         attacker.health = bound(
957                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
958                                                                                         // empathy won't let you gain health in the same way...
959                                                 attacker.health + damage * autocvar_g_balance_rune_vampire_absorb,
960                                                 autocvar_g_balance_rune_vampire_maxhealth);     // LA: was 1000, now 500
961                                         }
962                         }
963                         // apply empathy curse
964                         else if (attacker.runes & CURSE_EMPATHY)
965                         {
966                                 attacker.health = bound(
967                                         autocvar_g_balance_curse_empathy_minhealth, // LA: was 3, now 20
968                                         attacker.health + damage * autocvar_g_balance_curse_empathy_takedamage,
969                                         attacker.health);
970                         }
971                 }
972         }
973
974         // apply mirror damage if any
975         if(mirrordamage > 0 || mirrorforce > 0)
976         {
977                 attacker = attacker_save;
978                 if(g_minstagib)
979                 if(mirrordamage > 0)
980                 {
981                         // just lose extra LIVES, don't kill the player for mirror damage
982                         if(attacker.armorvalue > 0)
983                         {
984                                 attacker.armorvalue = attacker.armorvalue - 1;
985                                 centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue)));
986                                 attacker.hitsound += 1;
987                         }
988                         mirrordamage = 0;
989                 }
990
991                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
992                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
993         }
994 }
995
996 float RadiusDamage_running;
997 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
998         // Returns total damage applies to creatures
999 {
1000         entity  targ;
1001         vector  blastorigin;
1002         vector  force;
1003         float   total_damage_to_creatures;
1004         entity  next;
1005         float   tfloordmg;
1006         float   tfloorforce;
1007
1008         float stat_damagedone;
1009
1010         if(RadiusDamage_running)
1011         {
1012                 backtrace("RadiusDamage called recursively! Expect stuff to go HORRIBLY wrong.");
1013                 return 0;
1014         }
1015
1016         RadiusDamage_running = 1;
1017
1018         tfloordmg = autocvar_g_throughfloor_damage;
1019         tfloorforce = autocvar_g_throughfloor_force;
1020
1021         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
1022         total_damage_to_creatures = 0;
1023
1024         if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
1025                 if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
1026                 {
1027                         force = inflictor.velocity;
1028                         if(vlen(force) == 0)
1029                                 force = '0 0 -1';
1030                         else
1031                                 force = normalize(force);
1032                         if(forceintensity >= 0)
1033                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
1034                         else
1035                                 Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
1036                 }
1037
1038         stat_damagedone = 0;
1039
1040         targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
1041         while (targ)
1042         {
1043                 next = targ.chain;
1044                 if (targ != inflictor)
1045                         if (ignore != targ) if(targ.takedamage)
1046                         {
1047                                 vector nearest;
1048                                 vector diff;
1049                                 float power;
1050
1051                                 // LordHavoc: measure distance to nearest point on target (not origin)
1052                                 // (this guarentees 100% damage on a touch impact)
1053                                 nearest = targ.WarpZone_findradius_nearest;
1054                                 diff = targ.WarpZone_findradius_dist;
1055                                 // round up a little on the damage to ensure full damage on impacts
1056                                 // and turn the distance into a fraction of the radius
1057                                 power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
1058                                 //bprint(" ");
1059                                 //bprint(ftos(power));
1060                                 //if (targ == attacker)
1061                                 //      print(ftos(power), "\n");
1062                                 if (power > 0)
1063                                 {
1064                                         float finaldmg;
1065                                         if (power > 1)
1066                                                 power = 1;
1067                                         finaldmg = coredamage * power + edgedamage * (1 - power);
1068                                         if (finaldmg > 0)
1069                                         {
1070                                                 float a;
1071                                                 float c;
1072                                                 vector hitloc;
1073                                                 vector myblastorigin;
1074                                                 vector center;
1075
1076                                                 myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
1077
1078                                                 // if it's a player, use the view origin as reference
1079                                                 center = CENTER_OR_VIEWOFS(targ);
1080
1081                                                 force = normalize(center - myblastorigin);
1082                                                 force = force * (finaldmg / coredamage) * forceintensity;
1083                                                 hitloc = nearest;
1084
1085                                                 if(targ != directhitentity)
1086                                                 {
1087                                                         float hits;
1088                                                         float total;
1089                                                         float hitratio;
1090                                                         float mininv_f, mininv_d;
1091
1092                                                         // test line of sight to multiple positions on box,
1093                                                         // and do damage if any of them hit
1094                                                         hits = 0;
1095
1096                                                         // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
1097                                                         // so for a given max stddev:
1098                                                         // n = (1 / (2 * max stddev of hitratio))^2
1099
1100                                                         mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
1101                                                         mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
1102
1103                                                         if(autocvar_g_throughfloor_debug)
1104                                                                 print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
1105
1106                                                         total = 0.25 * pow(max(mininv_f, mininv_d), 2);
1107
1108                                                         if(autocvar_g_throughfloor_debug)
1109                                                                 print(sprintf(" steps=%f", total));
1110
1111                                                         if (targ.classname == "player")
1112                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
1113                                                         else
1114                                                                 total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
1115
1116                                                         if(autocvar_g_throughfloor_debug)
1117                                                                 print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
1118
1119                                                         for(c = 0; c < total; ++c)
1120                                                         {
1121                                                                 //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
1122                                                                 WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
1123                                                                 if (trace_fraction == 1 || trace_ent == targ)
1124                                                                 {
1125                                                                         ++hits;
1126                                                                         if (hits > 1)
1127                                                                                 hitloc = hitloc + nearest;
1128                                                                         else
1129                                                                                 hitloc = nearest;
1130                                                                 }
1131                                                                 nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
1132                                                                 nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
1133                                                                 nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
1134                                                         }
1135
1136                                                         nearest = hitloc * (1 / max(1, hits));
1137                                                         hitratio = (hits / total);
1138                                                         a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
1139                                                         finaldmg = finaldmg * a;
1140                                                         a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
1141                                                         force = force * a;
1142
1143                                                         if(autocvar_g_throughfloor_debug)
1144                                                                 print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
1145                                                 }
1146
1147                                                 // laser force adjustments :P
1148                                                 if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
1149                                                 {
1150                                                         if (targ == attacker)
1151                                                         {
1152                                                                 vector vel;
1153
1154                                                                 float force_zscale;
1155                                                                 float force_velocitybiasramp;
1156                                                                 float force_velocitybias;
1157
1158                                                                 force_velocitybiasramp = autocvar_sv_maxspeed;
1159                                                                 if(deathtype & HITTYPE_SECONDARY)
1160                                                                 {
1161                                                                         force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
1162                                                                         force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
1163                                                                 }
1164                                                                 else
1165                                                                 {
1166                                                                         force_zscale = autocvar_g_balance_laser_primary_force_zscale;
1167                                                                         force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
1168                                                                 }
1169
1170                                                                 vel = targ.velocity;
1171                                                                 vel_z = 0;
1172                                                                 vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
1173                                                                 force =
1174                                                                         vlen(force)
1175                                                                         *
1176                                                                         normalize(normalize(force) + vel);
1177
1178                                                                 force_z *= force_zscale;
1179                                                         }
1180                                                         else
1181                                                         {
1182                                                                 if(deathtype & HITTYPE_SECONDARY)
1183                                                                 {
1184                                                                         force *= autocvar_g_balance_laser_secondary_force_other_scale;
1185                                                                 }
1186                                                                 else
1187                                                                 {
1188                                                                         force *= autocvar_g_balance_laser_primary_force_other_scale;
1189                                                                 }
1190                                                         }
1191                                                 }
1192
1193                                                 //if (targ == attacker)
1194                                                 //{
1195                                                 //      print("hits ", ftos(hits), " / ", ftos(total));
1196                                                 //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
1197                                                 //      print(" (", ftos(a), ")\n");
1198                                                 //}
1199                                                 if(finaldmg || vlen(force))
1200                                                 {
1201                                                         if(targ.iscreature)
1202                                                         {
1203                                                                 total_damage_to_creatures += finaldmg;
1204
1205                                                                 if(accuracy_isgooddamage(attacker, targ))
1206                                                                         stat_damagedone += finaldmg;
1207                                                         }
1208
1209                                                         if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
1210                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
1211                                                         else
1212                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
1213                                                 }
1214                                         }
1215                                 }
1216                         }
1217                 targ = next;
1218         }
1219
1220         RadiusDamage_running = 0;
1221
1222         if(!DEATH_ISSPECIAL(deathtype))
1223                 accuracy_add(attacker, DEATH_WEAPONOFWEAPONDEATH(deathtype), 0, min(coredamage, stat_damagedone));
1224
1225         return total_damage_to_creatures;
1226 }
1227
1228 .float fire_damagepersec;
1229 .float fire_endtime;
1230 .float fire_deathtype;
1231 .entity fire_owner;
1232 .float fire_hitsound;
1233 .entity fire_burner;
1234
1235 void fireburner_think();
1236
1237 float Fire_IsBurning(entity e)
1238 {
1239         return (time < e.fire_endtime);
1240 }
1241
1242 float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
1243 {
1244         float dps;
1245         float maxtime, mintime, maxdamage, mindamage, maxdps, mindps, totaldamage, totaltime;
1246
1247         if(e.classname == "player")
1248         {
1249                 if(e.deadflag)
1250                         return -1;
1251         }
1252         else
1253         {
1254                 if(!e.fire_burner)
1255                 {
1256                         // print("adding a fire burner to ", e.classname, "\n");
1257                         e.fire_burner = spawn();
1258                         e.fire_burner.classname = "fireburner";
1259                         e.fire_burner.think = fireburner_think;
1260                         e.fire_burner.nextthink = time;
1261                         e.fire_burner.owner = e;
1262                 }
1263         }
1264
1265         t = max(t, 0.1);
1266         dps = d / t;
1267         if(Fire_IsBurning(e))
1268         {
1269                 mintime = e.fire_endtime - time;
1270                 maxtime = max(mintime, t);
1271
1272                 mindps = e.fire_damagepersec;
1273                 maxdps = max(mindps, dps);
1274
1275                 if(maxtime > mintime || maxdps > mindps)
1276                 {
1277                         // Constraints:
1278                         
1279                         // damage we have right now
1280                         mindamage = mindps * mintime;
1281
1282                         // damage we want to get
1283                         maxdamage = mindamage + d;
1284
1285                         // but we can't exceed maxtime * maxdps!
1286                         totaldamage = min(maxdamage, maxtime * maxdps);
1287
1288                         // LEMMA:
1289                         // Look at:
1290                         // totaldamage = min(mindamage + d, maxtime * maxdps)
1291                         // We see:
1292                         // totaldamage <= maxtime * maxdps
1293                         // ==> totaldamage / maxdps <= maxtime.
1294                         // We also see:
1295                         // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
1296                         //                     >= min(mintime, maxtime)
1297                         // ==> totaldamage / maxdps >= mintime.
1298
1299                         /*
1300                         // how long do we damage then?
1301                         // at least as long as before
1302                         // but, never exceed maxdps
1303                         totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
1304                         */
1305
1306                         // alternate:
1307                         // at most as long as maximum allowed
1308                         // but, never below mindps
1309                         totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
1310
1311                         // assuming t > mintime, dps > mindps:
1312                         // we get d = t * dps = maxtime * maxdps
1313                         // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
1314                         // totaldamage / maxdps = maxtime
1315                         // totaldamage / mindps > totaldamage / maxdps = maxtime
1316                         // FROM THIS:
1317                         // a) totaltime = max(mintime, maxtime) = maxtime
1318                         // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
1319
1320                         // assuming t <= mintime:
1321                         // we get maxtime = mintime
1322                         // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
1323                         // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
1324
1325                         // assuming dps <= mindps:
1326                         // we get mindps = maxdps.
1327                         // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
1328                         // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
1329                         // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
1330
1331                         e.fire_damagepersec = totaldamage / totaltime;
1332                         e.fire_endtime = time + totaltime;
1333                         if(totaldamage > 1.2 * mindamage)
1334                         {
1335                                 e.fire_deathtype = dt;
1336                                 if(e.fire_owner != o)
1337                                 {
1338                                         e.fire_owner = o;
1339                                         e.fire_hitsound = FALSE;
1340                                 }
1341                         }
1342                         if(accuracy_isgooddamage(o, e))
1343                                 accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, max(0, totaldamage - mindamage));
1344                         return max(0, totaldamage - mindamage); // can never be negative, but to make sure
1345                 }
1346                 else
1347                         return 0;
1348         }
1349         else
1350         {
1351                 e.fire_damagepersec = dps;
1352                 e.fire_endtime = time + t;
1353                 e.fire_deathtype = dt;
1354                 e.fire_owner = o;
1355                 e.fire_hitsound = FALSE;
1356                 if(accuracy_isgooddamage(o, e))
1357                         accuracy_add(o, DEATH_WEAPONOFWEAPONDEATH(dt), 0, d);
1358                 return d;
1359         }
1360 }
1361
1362 void Fire_ApplyDamage(entity e)
1363 {
1364         float t, d, hi, ty;
1365         entity o;
1366
1367         if not(Fire_IsBurning(e))
1368                 return;
1369
1370         for(t = 0, o = e.owner; o.owner && t < 16; o = o.owner, ++t);
1371         if(clienttype(o) == CLIENTTYPE_NOTACLIENT)
1372                 o = e.fire_owner;
1373
1374         // water and slime stop fire
1375         if(e.waterlevel)
1376         if(e.watertype != CONTENT_LAVA)
1377                 e.fire_endtime = 0;
1378
1379         // ice stops fire
1380         if(e.freezetag_frozen || e.frozen)
1381                 e.fire_endtime = 0;
1382
1383         t = min(frametime, e.fire_endtime - time);
1384         d = e.fire_damagepersec * t;
1385
1386         hi = e.fire_owner.hitsound;
1387         ty = e.fire_owner.typehitsound;
1388         Damage(e, e, e.fire_owner, d, e.fire_deathtype, e.origin, '0 0 0');
1389         if(e.fire_hitsound && e.fire_owner)
1390         {
1391                 e.fire_owner.hitsound = hi;
1392                 e.fire_owner.typehitsound = ty;
1393         }
1394         e.fire_hitsound = TRUE;
1395
1396         if not(IS_INDEPENDENT_PLAYER(e))
1397         FOR_EACH_PLAYER(other) if(e != other)
1398         {
1399                 if(other.classname == "player")
1400                 if(other.deadflag == DEAD_NO)
1401                 if not(IS_INDEPENDENT_PLAYER(other))
1402                 if(boxesoverlap(e.absmin, e.absmax, other.absmin, other.absmax))
1403                 {
1404                         t = autocvar_g_balance_firetransfer_time * (e.fire_endtime - time);
1405                         d = autocvar_g_balance_firetransfer_damage * e.fire_damagepersec * t;
1406                         Fire_AddDamage(other, o, d, t, DEATH_FIRE);
1407                 }
1408         }
1409 }
1410
1411 void Fire_ApplyEffect(entity e)
1412 {
1413         if(Fire_IsBurning(e))
1414                 e.effects |= EF_FLAME;
1415         else
1416                 e.effects &~= EF_FLAME;
1417 }
1418
1419 void fireburner_think()
1420 {
1421         // for players, this is done in the regular loop
1422         if(wasfreed(self.owner))
1423         {
1424                 remove(self);
1425                 return;
1426         }
1427         Fire_ApplyEffect(self.owner);
1428         if(!Fire_IsBurning(self.owner))
1429         {
1430                 self.owner.fire_burner = world;
1431                 remove(self);
1432                 return;
1433         }
1434         Fire_ApplyDamage(self.owner);
1435         self.nextthink = time;
1436 }