]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_damage.qc
make sv_gentle a global variable, read in the same fashion as sv_cheats. It thus...
[divverent/nexuiz.git] / data / qcsrc / server / g_damage.qc
1
2 float checkrules_firstblood;
3
4 .float announcetime;
5 float yoda;
6 void announce(entity player, string msg)
7 {
8         if(time > player.announcetime)
9         if(clienttype(player) == CLIENTTYPE_REAL)
10         {
11                 player.announcetime = time + 0.3;
12                 play2(player, msg);
13         }
14 }
15
16 float IsDifferentTeam(entity a, entity b)
17 {
18         if(teams_matter)
19         {
20                 if(a.team == b.team)
21                         return 0;
22         }
23         else
24         {
25                 if(a == b)
26                         return 0;
27         }
28         return 1;
29 }
30
31 float IsFlying(entity a)
32 {
33         if(a.flags & FL_ONGROUND)
34                 return 0;
35         if(a.waterlevel >= 2)
36                 return 0;
37         traceline(a.origin, a.origin - '0 0 48', MOVE_NORMAL, a);
38         if(trace_fraction < 1)
39                 return 0;
40         return 1;
41 }
42
43 void(entity player, float f) UpdateFrags =
44 {
45         player.frags += f;
46         player.totalfrags += f;
47 }
48
49 void GiveFrags (entity attacker, entity targ, float f)
50 {
51         if(gameover) return;
52
53         if(g_arena)
54                 if(cvar("g_arena_roundbased"))
55                         return;
56
57         if(g_domination)
58         {
59                 if(cvar("g_domination_disable_frags"))
60                         if(f > 0)
61                                 return;
62         }
63         else if(g_runematch)
64         {
65                 if(f > 0)
66                         f = RunematchHandleFrags(attacker, targ, f);
67         }
68         else if(g_keyhunt)
69         {
70                 f = kh_HandleFrags(attacker, targ, f);
71         }
72         else if(g_lms)
73         {
74                 // count remaining lives, not frags in lms
75                 targ.frags -= 1;
76                 // keep track of the worst players lives
77                 if(targ.frags < lms_lowest_lives)
78                         lms_lowest_lives = targ.frags;
79                 // player has no more lives left
80                 if (!targ.frags)
81                 {
82                         if(!lms_next_place)
83                                 lms_next_place = player_count;
84                         targ.frags = -lms_next_place;
85                         lms_next_place -= 1;
86                 }
87                 return;
88         }
89
90         if(f)
91                 UpdateFrags(attacker, f);
92 }
93
94 string AppendItemcodes(string s, entity player)
95 {
96         float w;
97         w = player.weapon;
98         //if(w == 0)
99         //      w = player.switchweapon;
100         if(w == 0)
101                 w = player.cnt; // previous weapon!
102         s = strcat(s, ftos(W_ItemCode(w)));
103         if(time < player.strength_finished)
104                 s = strcat(s, "S");
105         if(time < player.invincible_finished)
106                 s = strcat(s, "I");
107         if(player.flagcarried != world)
108                 s = strcat(s, "F");
109         if(player.buttonchat)
110                 s = strcat(s, "T");
111         if(player.kh_next)
112                 s = strcat(s, "K");
113         if(player.runes)
114                 s = strcat(s, "|", ftos(player.runes));
115         return s;
116 }
117
118 void LogDeath(string mode, float deathtype, entity killer, entity killed)
119 {
120         string s;
121         if(!cvar("sv_eventlog"))
122                 return;
123         s = strcat(":kill:", mode);
124         s = strcat(s, ":", ftos(killer.playerid));
125         s = strcat(s, ":", ftos(killed.playerid));
126         s = strcat(s, ":type=", ftos(deathtype));
127         s = strcat(s, ":items=");
128         s = AppendItemcodes(s, killer);
129         if(killed != killer)
130         {
131                 s = strcat(s, ":victimitems=");
132                 s = AppendItemcodes(s, killed);
133         }
134         GameLogEcho(s, FALSE);
135 }
136
137 void Obituary (entity attacker, entity targ, float deathtype)
138 {
139         string  s, a;
140
141         if (targ.classname == "player" || targ.classname == "corpse")
142         {
143                 if (targ.classname == "corpse")
144                         s = "A corpse";
145                 else
146                         s = targ.netname;
147                 a = attacker.netname;
148
149                 if (targ == attacker)
150                 {
151                         if (deathtype == DEATH_TEAMCHANGE)
152                         {
153                                 centerprint(targ, strcat("You are now on: ", ColoredTeamName(targ.team)));
154                         }
155                         else if (deathtype == DEATH_AUTOTEAMCHANGE)
156                         {
157                                 centerprint(targ, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(targ.team)));
158                                 return;
159                         }
160                         else if (deathtype == DEATH_CAMP)
161                                 centerprint(targ, "^1Die camper!\n\n\n");
162                         else if (deathtype == DEATH_NOAMMO)
163                                 centerprint(targ, "^1You were killed for running out of ammo...\n\n\n");
164                         else if (deathtype == DEATH_ROT)
165                                 centerprint(targ, "^1You grew too old without taking your medicine\n\n\n");
166                         else if (deathtype == DEATH_MIRRORDAMAGE)
167                                 centerprint(targ, "^1Don't shoot your team mates!\n\n\n");
168                         else
169                                 centerprint(targ, "^1You killed your own dumb self!\n\n\n");
170
171                         if (deathtype == IT_GRENADE_LAUNCHER)
172                                 bprint ("^1",s, "^1 detonated\n");
173                         else if (deathtype == IT_ELECTRO)
174                                 bprint ("^1",s, "^1 played with plasma\n");
175                         else if (deathtype == IT_ROCKET_LAUNCHER)
176                                 bprint ("^1",s, "^1 exploded\n");
177                         else if (deathtype == DEATH_KILL)
178                                 bprint ("^1",s, "^1 couldn't take it anymore\n");
179                         else if (deathtype == DEATH_ROT)
180                                 bprint ("^1",s, "^1 died\n");
181                         else if (deathtype == DEATH_NOAMMO)
182                         {
183                                 bprint ("^7",s, " ^7committed suicide. What's the point of living without ammo?\n");
184                                 //sound (self, CHAN_BODY, "minstagib/mockery.wav", 1, ATTN_NONE);
185                         }
186                         else if (deathtype == DEATH_CAMP)
187                                 bprint ("^1",s, "^1 thought he found a nice camping ground\n");
188                         else if (deathtype == DEATH_MIRRORDAMAGE)
189                                 bprint ("^1",s, "^1 didn't become friends with the Lord of Teamplay\n");
190                         else if (deathtype != DEATH_TEAMCHANGE)
191                                 bprint ("^1",s, "^1 couldn't resist the urge to self-destruct\n");
192
193                         if(deathtype != DEATH_TEAMCHANGE)
194                         {
195                                 LogDeath("suicide", deathtype, targ, targ);
196                                 GiveFrags(attacker, targ, -1);
197                         }
198                         if (targ.killcount > 2)
199                                 bprint ("^1",s,"^1 ended it all with a ",ftos(targ.killcount)," kill spree\n");
200                 }
201                 else if (attacker.classname == "player" || attacker.classname == "gib")
202                 {
203                         if(teamplay && attacker.team == targ.team)
204                         {
205                                 centerprint(attacker, "^1Moron! You fragged a teammate!\n\n\n");
206                                 bprint ("^1", a, "^1 mows down a teammate\n");
207                                 GiveFrags(attacker, targ, -1);
208                                 if (targ.killcount > 2)
209                                         bprint ("^1",s,"'s ^1",ftos(targ.killcount)," kill spree was ended by a teammate!\n");
210                                 if (attacker.killcount > 2)
211                                         bprint ("^1",a,"^1 ended a ",ftos(attacker.killcount)," kill spree by killing a teammate\n");
212                                 attacker.killcount = 0;
213
214                                 LogDeath("tk", deathtype, attacker, targ);
215                         }
216                         else
217                         {
218                                 if (!checkrules_firstblood)
219                                 {
220                                         checkrules_firstblood = TRUE;
221                                         //sound(world, CHAN_AUTO, "announcer/firstblood.wav", 1, ATTN_NONE);
222                                         //if (g_minstagib)
223                                                 //sound(world, CHAN_AUTO, "announce/male/mapkill1.wav", 1, ATTN_NONE);
224                                         bprint("^1",a, "^1 drew first blood", "\n");
225                                 }
226
227                                 if(sv_gentle > 0) {
228                                         centerprint(attacker, strcat("^4You scored against ^7", s, "\n\n\n"));
229                                         centerprint(targ, strcat(a,"^1 scored against you ^7\n\n\n"));
230                                 } else {
231                                         centerprint(attacker, strcat("^4You fragged ^7", s, "\n\n\n"));
232                                         centerprint(targ, strcat("^1You were fragged by ^7", a, "\n\n\n"));
233                                 }
234
235                                 if (deathtype == IT_LASER)
236                                         bprint ("^1",s, "^1 was blasted by ", a, "\n");
237                                 else if (deathtype == IT_UZI)
238                                         bprint ("^1",s, "^1 was riddled full of holes by ", a, "\n");
239                                 else if (deathtype == IT_SHOTGUN)
240                                         bprint ("^1",s, "^1 was gunned by ", a, "\n");
241                                 else if (deathtype == IT_GRENADE_LAUNCHER)
242                                         bprint ("^1", s, "^1 was blasted by ", a, "\n");
243                                 else if (deathtype == IT_ELECTRO)
244                                         bprint ("^1",s, "^1 was blasted by ", a, "\n");
245                                 else if (deathtype == IT_CRYLINK)
246                                         bprint ("^1",s, "^1 was blasted by ", a, "\n");
247                                 else if (deathtype == IT_NEX)
248                                         bprint ("^1",s, "^1 has been vaporized by ", a, "\n");
249                                 else if (deathtype == IT_HAGAR)
250                                         bprint ("^1",s, "^1 was pummeled by ", a, "\n");
251                                 else if (deathtype == IT_ROCKET_LAUNCHER)
252                                         bprint ("^1",s, "^1 was blasted by ", a, "\n");
253                                 else if (deathtype == DEATH_TELEFRAG)
254                                         bprint ("^1",s, "^1 was telefragged by ", a, "\n");
255                                 else if (deathtype == DEATH_DROWN)
256                                         bprint ("^1",s, "^1 was drowned by ", a, "\n");
257                                 else if (deathtype == DEATH_SLIME)
258                                         bprint ("^1",s, "^1 was slimed by ", a, "\n");
259                                 else if (deathtype == DEATH_LAVA)
260                                         bprint ("^1",s, "^1 was cooked by ", a, "\n");
261                                 else if (deathtype == DEATH_FALL)
262                                         bprint ("^1",s, "^1 was grounded by ", a, "\n");
263                                 else if (deathtype == DEATH_SHOOTING_STAR)
264                                         bprint ("^1",s, "^1 was shot into space by ", a, "\n");
265                                 else if (deathtype == DEATH_SWAMP)
266                                         bprint ("^1",s, "^1 was conserved by ", a, "\n");
267                                 else if (deathtype == DEATH_HURTTRIGGER)
268                                         bprint ("^1",s, "^1 was thrown into a world of hurt by ", a, "\n");
269                                 else
270                                         bprint ("^1",s, "^1 was fragged by ", a, "\n");
271
272                                 GiveFrags(attacker, targ, 1);
273                                 if (targ.killcount > 2)
274                                         bprint ("^1",s,"'s ^1", ftos(targ.killcount), " kill spree was ended by ", a, "\n");
275                                 attacker.killcount = attacker.killcount + 1;
276                                 if (attacker.killcount > 2)
277                                         bprint ("^1",a,"^1 has ",ftos(attacker.killcount)," frags in a row\n");
278
279                                 LogDeath("frag", deathtype, attacker, targ);
280
281                                 if (attacker.killcount == 3)
282                                 {
283                                         bprint (a,"^7 made a ^1TRIPLE FRAG\n");
284                                         announce(attacker, "announcer/male/03kills.ogg");
285                                 }
286                                 else if (attacker.killcount == 5)
287                                 {
288                                         bprint (a,"^7 unleashes ^1RAGE\n");
289                                         announce(attacker, "announcer/male/05kills.ogg");
290                                 }
291                                 else if (attacker.killcount == 10)
292                                 {
293                                         bprint (a,"^7 starts the ^1MASSACRE!\n");
294                                         announce(attacker, "announcer/male/10kills.ogg");
295                                 }
296                                 else if (attacker.killcount == 15)
297                                 {
298                                         bprint (a,"^7 executes ^1MAYHEM!\n");
299                                         announce(attacker, "announcer/male/15kills.ogg");
300                                 }
301                                 else if (attacker.killcount == 20)
302                                 {
303                                         bprint (a,"^7 is a ^1BERSERKER!\n");
304                                         announce(attacker, "announcer/male/20kills.ogg");
305                                 }
306                                 else if (attacker.killcount == 25)
307                                 {
308                                         bprint (a,"^7 inflicts ^1CARNAGE!\n");
309                                         announce(attacker, "announcer/male/25kills.ogg");
310                                 }
311                                 else if (attacker.killcount == 30)
312                                 {
313                                         bprint (a,"^7 unleashes ^1ARMAGEDDON!\n");
314                                         announce(attacker, "announcer/male/30kills.ogg");
315                                 }
316                         }
317                 }
318                 else
319                 {
320                         centerprint(targ, "^1Watch your step!\n\n\n");
321                         if (deathtype == DEATH_HURTTRIGGER && attacker.message != "")
322                                 bprint ("^1",s, "^1 ", attacker.message, "\n");
323                         else if (deathtype == DEATH_DROWN)
324                                 bprint ("^1",s, "^1 drowned\n");
325                         else if (deathtype == DEATH_SLIME)
326                                 bprint ("^1",s, "^1 was slimed\n");
327                         else if (deathtype == DEATH_LAVA)
328                                 bprint ("^1",s, "^1 turned into hot slag\n");
329                         else if (deathtype == DEATH_FALL)
330                                 bprint ("^1",s, "^1 hit the ground with a crunch\n");
331                         else if (deathtype == DEATH_SHOOTING_STAR)
332                                 bprint ("^1",s, "^1 became a shooting star\n");
333                         else if (deathtype == DEATH_SWAMP)
334                                 bprint ("^1",s, "^1 is now conserved for centuries to come\n");
335                         else
336                                 bprint ("^1",s, "^1 died\n");
337                         GiveFrags(targ, targ, -1);
338                         if(targ.frags == -5) {
339                                 announce(targ, "announcer/male/botlike.ogg");
340                         }
341
342                         if (targ.killcount > 2)
343                                 bprint ("^1",s,"^1 died with a ",ftos(targ.killcount)," kill spree\n");
344
345                         LogDeath("accident", deathtype, targ, targ);
346                 }
347                 targ.death_origin = targ.origin;
348                 if(targ != attacker)
349                         targ.killer_origin = attacker.origin;
350                 // FIXME: this should go in PutClientInServer
351                 if (targ.killcount)
352                         targ.killcount = 0;
353         }
354 }
355
356 // these are updated by each Damage call for use in button triggering and such
357 entity damage_targ;
358 entity damage_inflictor;
359 entity damage_attacker;
360
361 void Damage (entity targ, entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
362 {
363         float mirrordamage;
364         float mirrorforce;
365         entity attacker_save;
366         mirrordamage = 0;
367         mirrorforce = 0;
368
369         if (gameover || targ.killcount == -666)
370                 return;
371
372         local entity oldself;
373         oldself = self;
374         self = targ;
375         damage_targ = targ;
376         damage_inflictor = inflictor;
377         damage_attacker = attacker;
378                 attacker_save = attacker;
379
380         if(deathtype == DEATH_KILL || deathtype == DEATH_TEAMCHANGE || deathtype == DEATH_AUTOTEAMCHANGE)
381         {
382                 // These are ALWAYS lethal
383                 // No damage modification here
384                 // Instead, prepare the victim for his death...
385                 targ.armorvalue = 0;
386                 targ.spawnshieldtime = 0;
387                 targ.health = 1;
388                 targ.flags -= targ.flags & FL_GODMODE;
389                 damage = 100000;
390         }
391         else
392         {
393                 if (targ.classname == "player")
394                 if (attacker.classname == "player")
395                 if (!targ.isbot)
396                 if (attacker.isbot)
397                         damage = damage * bound(0.1, (skill + 5) * 0.1, 1);
398
399                 // nullify damage if teamplay is on
400                 if(deathtype != DEATH_TELEFRAG)
401                 if(attacker.team == targ.team)
402                 if(attacker.classname == "player")
403                 {
404                         if(teamplay == 1)
405                                 damage = 0;
406                         else if(attacker != targ)
407                         {
408                                 if(teamplay == 3)
409                                         damage = 0;
410                                 else if(teamplay == 4)
411                                 {
412                                         if(targ.classname == "player" && targ.deadflag == DEAD_NO)
413                                         {
414                                                 mirrordamage = cvar("g_mirrordamage") * damage;
415                                                 mirrorforce = cvar("g_mirrordamage") * vlen(force);
416                                                 if(g_minstagib)
417                                                 {
418                                                         if(cvar("g_friendlyfire") == 0)
419                                                                 damage = 0;
420                                                 }
421                                                 else
422                                                         damage = cvar("g_friendlyfire") * damage;
423                                                 // mirrordamage will be used LATER
424                                         }
425                                         else
426                                                 damage = 0;
427                                 }
428                         }
429                 }
430
431                 if(g_lms)
432                 if(targ.classname == "player")
433                 if(attacker.classname == "player")
434                 if(attacker != targ)
435                 {
436                         targ.lms_traveled_distance = cvar("g_lms_campcheck_distance");
437                         attacker.lms_traveled_distance = cvar("g_lms_campcheck_distance");
438                 }
439
440                 if(targ != attacker)
441                 if(!targ.deadflag)
442                 if(damage > 0)
443                 if(targ.classname == "player")
444                 if(attacker)
445                         attacker.hitsound += 1;
446
447                 if (g_minstagib)
448                 {
449                         if ((deathtype == DEATH_FALL)  ||
450                                 (deathtype == DEATH_DROWN) ||
451                                 (deathtype == DEATH_SLIME) ||
452                                 (deathtype == DEATH_LAVA))
453                                 return;
454                         if (targ.armorvalue && (deathtype == IT_NEX) && damage)
455                         {
456                                 targ.armorvalue -= 1;
457                                 centerprint(targ, strcat("^3Remaining extra lives: ",ftos(targ.armorvalue),"\n"));
458                                 damage = 0;
459                                 targ.hitsound += 1;
460                         }
461                         else if (deathtype == IT_NEX && targ.items & IT_STRENGTH)
462                         {
463                                 if(clienttype(attacker) == CLIENTTYPE_REAL)
464                                         if(IsDifferentTeam(targ, attacker))
465                                                 yoda = 1;
466                         }
467                         if (deathtype == IT_LASER)
468                         {
469                                 damage = 0;
470                                 if (targ != attacker)
471                                 {
472                                         if (targ.classname == "player")
473                                                 centerprint(attacker, "Secondary fire inflicts no damage!\n");
474                                         damage = 0;
475                                         mirrordamage = 0;
476                                         force = '0 0 0';
477                                         // keep mirrorforce
478                                         attacker = targ;
479                                 }
480                         }
481                 } else {
482                         if (!targ.deadflag)
483                                 if(targ.takedamage == DAMAGE_AIM)
484                                         if(IsFlying(targ))
485                                                 if(IsDifferentTeam(targ, attacker))
486                                                         yoda = 1;
487                 }
488
489                 // apply strength multiplier
490                 if (attacker.items & IT_STRENGTH && !g_minstagib)
491                 {
492                         damage = damage * cvar("g_balance_powerup_strength_damage");
493                         force = force * cvar("g_balance_powerup_strength_force");
494                 }
495                 // apply invincibility multiplier
496                 if (targ.items & IT_INVINCIBLE && !g_minstagib)
497                         damage = damage * cvar("g_balance_powerup_invincible_takedamage");
498
499
500                 if(g_runematch)
501                 {
502                         // apply strength rune
503                         if (attacker.runes & RUNE_STRENGTH)
504                         {
505                                 if(attacker.runes & CURSE_WEAK) // have both curse & rune
506                                 {
507                                         damage = damage * cvar("g_balance_rune_strength_combo_damage");
508                                         force = force * cvar("g_balance_rune_strength_combo_force");
509                                 }
510                                 else
511                                 {
512                                         damage = damage * cvar("g_balance_rune_strength_damage");
513                                         force = force * cvar("g_balance_rune_strength_force");
514                                 }
515                         }
516                         else if (attacker.runes & CURSE_WEAK)
517                         {
518                                 damage = damage * cvar("g_balance_curse_weak_damage");
519                                 force = force * cvar("g_balance_curse_weak_force");
520                         }
521
522                         // apply defense rune
523                         if (targ.runes & RUNE_DEFENSE)
524                         {
525                                 if (targ.runes & CURSE_VULNER) // have both curse & rune
526                                         damage = damage * cvar("g_balance_rune_defense_combo_takedamage");
527                                 else
528                                         damage = damage * cvar("g_balance_rune_defense_takedamage");
529                         }
530                         else if (targ.runes & CURSE_VULNER)
531                                 damage = damage * cvar("g_balance_curse_vulner_takedamage");
532                 }
533         }
534
535         // apply push
536         if (self.damageforcescale)
537         if (vlen(force))
538         {
539                 self.velocity = self.velocity + self.damageforcescale * force;
540                 self.flags = self.flags - (self.flags & FL_ONGROUND);
541         }
542         // apply damage
543         if (self.event_damage)
544                 self.event_damage (inflictor, attacker, damage, deathtype, hitloc, force);
545         self = oldself;
546
547         if(targ.classname == "player" && attacker.classname == "player" && attacker != targ && attacker.health > 2)
548         {
549                 // Savage: vampire mode
550                 if (g_vampire)
551                 if (!g_minstagib)
552                 if (!g_instagib)
553                 if (time > self.spawnshieldtime)
554                 {
555                         attacker.health += damage;
556                 }
557                 if(g_runematch)
558                 {
559                         if (attacker.runes & RUNE_VAMPIRE)
560                         {
561                         // apply vampire rune
562                                 if (attacker.runes & CURSE_EMPATHY) // have the curse too
563                                 {
564                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb");
565                                         attacker.health = bound(
566                                                 cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 40
567                                                 attacker.health + damage * cvar("g_balance_rune_vampire_combo_absorb"),
568                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
569                                 }
570                                 else
571                                 {
572                                         //attacker.health = attacker.health + damage * cvar("g_balance_rune_vampire_absorb");
573                                         attacker.health = bound(
574                                                 attacker.health,        // LA: was 3, but changed so that you can't lose health
575                                                                                         // empathy won't let you gain health in the same way...
576                                                 attacker.health + damage * cvar("g_balance_rune_vampire_absorb"),
577                                                 cvar("g_balance_rune_vampire_maxhealth"));      // LA: was 1000, now 500
578                                         }
579                         }
580                         // apply empathy curse
581                         else if (attacker.runes & CURSE_EMPATHY)
582                         {
583                                 attacker.health = bound(
584                                         cvar("g_balance_curse_empathy_minhealth"), // LA: was 3, now 20
585                                         attacker.health + damage * cvar("g_balance_curse_empathy_takedamage"),
586                                         attacker.health);
587                         }
588                 }
589         }
590
591         // apply mirror damage if any
592         if(mirrordamage > 0 || mirrorforce > 0)
593         {
594                 attacker = attacker_save;
595                 if(g_minstagib)
596                         if(mirrordamage > 0)
597                         {
598                                 // just lose extra LIVES, don't kill the player for mirror damage
599                                 if(attacker.armorvalue > 0)
600                                 {
601                                         attacker.armorvalue = attacker.armorvalue - 1;
602                                         centerprint(attacker, strcat("^3Remaining extra lives: ",ftos(attacker.armorvalue),"\n"));
603                                         attacker.hitsound += 1;
604                                 }
605                                 mirrordamage = 0;
606                         }
607                 force = normalize(attacker.origin + attacker.view_ofs - hitloc) * mirrorforce;
608                 Damage(attacker, inflictor, attacker, mirrordamage, DEATH_MIRRORDAMAGE, attacker.origin, force);
609         }
610 }
611
612 vector NearestPointOnBox(entity box, vector org)
613 {
614         vector m1, m2, nearest;
615
616         m1 = box.mins + box.origin;
617         m2 = box.maxs + box.origin;
618
619         nearest_x = bound(m1_x, org_x, m2_x);
620         nearest_y = bound(m1_y, org_y, m2_y);
621         nearest_z = bound(m1_z, org_z, m2_z);
622
623         return nearest;
624 }
625
626 float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype)
627 // Returns total damage applies to creatures
628 {
629         entity  targ;
630         float   finaldmg;
631         float   power;
632         vector  blastorigin;
633         vector  force;
634         vector  diff;
635         vector  center;
636         vector  nearest;
637         float   total_damage_to_creatures;
638
639         blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
640         total_damage_to_creatures = 0;
641
642         targ = findradius (blastorigin, rad);
643         while (targ)
644         {
645                 if (targ != inflictor)
646                         if (ignore != targ)
647                         {
648                                 // LordHavoc: measure distance to nearest point on target (not origin)
649                                 // (this guarentees 100% damage on a touch impact)
650                                 nearest = NearestPointOnBox(targ, blastorigin);
651                                 diff = nearest - blastorigin;
652                                 // round up a little on the damage to ensure full damage on impacts
653                                 // and turn the distance into a fraction of the radius
654                                 power = 1 - ((vlen (diff) - 2) / rad);
655                                 //bprint(" ");
656                                 //bprint(ftos(power));
657                                 if (power > 0)
658                                 {
659                                         if (power > 1)
660                                                 power = 1;
661                                         finaldmg = coredamage * power + edgedamage * (1 - power);
662                                         if (finaldmg > 0)
663                                         {
664                                                 center = targ.origin + (targ.mins + targ.maxs) * 0.5;
665                                                 // if it's a player, use the view origin as reference
666                                                 if (targ.classname == "player")
667                                                         center = targ.origin + targ.view_ofs;
668                                                 force = normalize(center - blastorigin) * (finaldmg / coredamage) * forceintensity;
669                                                 if (targ == attacker)
670                                                         finaldmg = finaldmg * cvar("g_balance_selfdamagepercent");      // Partial damage if the attacker hits himself
671                                                 // test line of sight to multiple positions on box,
672                                                 // and do damage if any of them hit
673                                                 local float c;
674                                                 c = ceil(finaldmg / 10);
675                                                 if (c > 20)
676                                                         c = 20;
677                                                 while (c > 0)
678                                                 {
679                                                         c = c - 1;
680                                                         traceline(blastorigin, nearest, TRUE, inflictor);
681                                                         if (trace_fraction == 1 || trace_ent == targ
682                                                             || cvar("g_throughfloor"))
683                                                         {
684                                                                 if(targ.iscreature)
685                                                                         total_damage_to_creatures += finaldmg;
686                                                                 Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
687                                                                 break;
688                                                         }
689                                                         nearest_x = targ.mins_x + random() * targ.size_x;
690                                                         nearest_y = targ.mins_y + random() * targ.size_y;
691                                                         nearest_z = targ.mins_z + random() * targ.size_z;
692                                                 }
693                                         }
694                                 }
695                         }
696                 targ = targ.chain;
697         }
698
699         return total_damage_to_creatures;
700 }