]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/g_damage.qc
Merge remote-tracking branch 'origin/master' into samual/spawn_weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_damage.qc
index a49aad27bdc401027a66ca15a1e181140cf765c4..710c7d4e2a88e7e7fa5ca4fdeb019c5e84ab5d96 100644 (file)
@@ -986,11 +986,10 @@ void Damage (entity targ, entity inflictor, entity attacker, float damage, float
 }
 
 float RadiusDamage_running;
-float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity ignore, float forceintensity, float deathtype, entity directhitentity)
+float RadiusDamageForSource (entity inflictor, vector inflictororigin, vector inflictorvelocity, entity attacker, float coredamage, float edgedamage, float rad, entity cantbe, entity mustbe, float inflictorselfdamage, float forceintensity, float deathtype, entity directhitentity)
        // Returns total damage applies to creatures
 {
        entity  targ;
-       vector  blastorigin;
        vector  force;
        float   total_damage_to_creatures;
        entity  next;
@@ -1010,205 +1009,205 @@ float RadiusDamage (entity inflictor, entity attacker, float coredamage, float e
        tfloordmg = autocvar_g_throughfloor_damage;
        tfloorforce = autocvar_g_throughfloor_force;
 
-       blastorigin = (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5);
        total_damage_to_creatures = 0;
 
        if(deathtype != (WEP_HOOK | HITTYPE_SECONDARY | HITTYPE_BOUNCE)) // only send gravity bomb damage once
                if(DEATH_WEAPONOF(deathtype) != WEP_TUBA) // do not send tuba damage (bandwidth hog)
                {
-                       force = inflictor.velocity;
+                       force = inflictorvelocity;
                        if(vlen(force) == 0)
                                force = '0 0 -1';
                        else
                                force = normalize(force);
                        if(forceintensity >= 0)
-                               Damage_DamageInfo(blastorigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
+                               Damage_DamageInfo(inflictororigin, coredamage, edgedamage, rad, forceintensity * force, deathtype, 0, attacker);
                        else
-                               Damage_DamageInfo(blastorigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
+                               Damage_DamageInfo(inflictororigin, coredamage, edgedamage, -rad, (-forceintensity) * force, deathtype, 0, attacker);
                }
 
        stat_damagedone = 0;
 
-       targ = WarpZone_FindRadius (blastorigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
+       targ = WarpZone_FindRadius (inflictororigin, rad + MAX_DAMAGEEXTRARADIUS, FALSE);
        while (targ)
        {
                next = targ.chain;
-               if (targ != inflictor)
-                       if (ignore != targ) if(targ.takedamage)
+               if ((targ != inflictor) || inflictorselfdamage)
+               if (((cantbe != targ) && !mustbe) || (mustbe == targ))
+               if (targ.takedamage)
+               {
+                       vector nearest;
+                       vector diff;
+                       float power;
+
+                       // LordHavoc: measure distance to nearest point on target (not origin)
+                       // (this guarentees 100% damage on a touch impact)
+                       nearest = targ.WarpZone_findradius_nearest;
+                       diff = targ.WarpZone_findradius_dist;
+                       // round up a little on the damage to ensure full damage on impacts
+                       // and turn the distance into a fraction of the radius
+                       power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
+                       //bprint(" ");
+                       //bprint(ftos(power));
+                       //if (targ == attacker)
+                       //      print(ftos(power), "\n");
+                       if (power > 0)
                        {
-                               vector nearest;
-                               vector diff;
-                               float power;
-
-                               // LordHavoc: measure distance to nearest point on target (not origin)
-                               // (this guarentees 100% damage on a touch impact)
-                               nearest = targ.WarpZone_findradius_nearest;
-                               diff = targ.WarpZone_findradius_dist;
-                               // round up a little on the damage to ensure full damage on impacts
-                               // and turn the distance into a fraction of the radius
-                               power = 1 - ((vlen (diff) - bound(MIN_DAMAGEEXTRARADIUS, targ.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
-                               //bprint(" ");
-                               //bprint(ftos(power));
-                               //if (targ == attacker)
-                               //      print(ftos(power), "\n");
-                               if (power > 0)
+                               float finaldmg;
+                               if (power > 1)
+                                       power = 1;
+                               finaldmg = coredamage * power + edgedamage * (1 - power);
+                               if (finaldmg > 0)
                                {
-                                       float finaldmg;
-                                       if (power > 1)
-                                               power = 1;
-                                       finaldmg = coredamage * power + edgedamage * (1 - power);
-                                       if (finaldmg > 0)
-                                       {
-                                               float a;
-                                               float c;
-                                               vector hitloc;
-                                               vector myblastorigin;
-                                               vector center;
+                                       float a;
+                                       float c;
+                                       vector hitloc;
+                                       vector myblastorigin;
+                                       vector center;
 
-                                               myblastorigin = WarpZone_TransformOrigin(targ, blastorigin);
+                                       myblastorigin = WarpZone_TransformOrigin(targ, inflictororigin);
 
-                                               // if it's a player, use the view origin as reference
-                                               if (targ.classname == "player")
-                                                       center = targ.origin + targ.view_ofs;
-                                               else
-                                                       center = targ.origin + (targ.mins + targ.maxs) * 0.5;
+                                       // if it's a player, use the view origin as reference
+                                       if (targ.classname == "player")
+                                               center = targ.origin + targ.view_ofs;
+                                       else
+                                               center = targ.origin + (targ.mins + targ.maxs) * 0.5;
 
-                                               force = normalize(center - myblastorigin);
-                                               force = force * (finaldmg / coredamage) * forceintensity;
-                                               hitloc = nearest;
+                                       force = normalize(center - myblastorigin);
+                                       force = force * (finaldmg / coredamage) * forceintensity;
+                                       hitloc = nearest;
 
-                                               if(targ != directhitentity)
-                                               {
-                                                       float hits;
-                                                       float total;
-                                                       float hitratio;
-                                                       float mininv_f, mininv_d;
+                                       if(targ != directhitentity)
+                                       {
+                                               float hits;
+                                               float total;
+                                               float hitratio;
+                                               float mininv_f, mininv_d;
 
-                                                       // test line of sight to multiple positions on box,
-                                                       // and do damage if any of them hit
-                                                       hits = 0;
+                                               // test line of sight to multiple positions on box,
+                                               // and do damage if any of them hit
+                                               hits = 0;
 
-                                                       // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
-                                                       // so for a given max stddev:
-                                                       // n = (1 / (2 * max stddev of hitratio))^2
+                                               // we know: max stddev of hitratio = 1 / (2 * sqrt(n))
+                                               // so for a given max stddev:
+                                               // n = (1 / (2 * max stddev of hitratio))^2
 
-                                                       mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
-                                                       mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
+                                               mininv_d = (finaldmg * (1-tfloordmg)) / autocvar_g_throughfloor_damage_max_stddev;
+                                               mininv_f = (vlen(force) * (1-tfloorforce)) / autocvar_g_throughfloor_force_max_stddev;
 
-                                                       if(autocvar_g_throughfloor_debug)
-                                                               print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
+                                               if(autocvar_g_throughfloor_debug)
+                                                       print(sprintf("THROUGHFLOOR: D=%f F=%f max(dD)=1/%f max(dF)=1/%f", finaldmg, vlen(force), mininv_d, mininv_f));
 
-                                                       total = 0.25 * pow(max(mininv_f, mininv_d), 2);
+                                               total = 0.25 * pow(max(mininv_f, mininv_d), 2);
 
-                                                       if(autocvar_g_throughfloor_debug)
-                                                               print(sprintf(" steps=%f", total));
+                                               if(autocvar_g_throughfloor_debug)
+                                                       print(sprintf(" steps=%f", total));
 
-                                                       if (targ.classname == "player")
-                                                               total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
-                                                       else
-                                                               total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
+                                               if (targ.classname == "player")
+                                                       total = ceil(bound(autocvar_g_throughfloor_min_steps_player, total, autocvar_g_throughfloor_max_steps_player));
+                                               else
+                                                       total = ceil(bound(autocvar_g_throughfloor_min_steps_other, total, autocvar_g_throughfloor_max_steps_other));
 
-                                                       if(autocvar_g_throughfloor_debug)
-                                                               print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
+                                               if(autocvar_g_throughfloor_debug)
+                                                       print(sprintf(" steps=%f dD=%f dF=%f", total, finaldmg * (1-tfloordmg) / (2 * sqrt(total)), vlen(force) * (1-tfloorforce) / (2 * sqrt(total))));
 
-                                                       for(c = 0; c < total; ++c)
+                                               for(c = 0; c < total; ++c)
+                                               {
+                                                       //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
+                                                       WarpZone_TraceLine(inflictororigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
+                                                       if (trace_fraction == 1 || trace_ent == targ)
                                                        {
-                                                               //traceline(targ.WarpZone_findradius_findorigin, nearest, MOVE_NOMONSTERS, inflictor);
-                                                               WarpZone_TraceLine(blastorigin, WarpZone_UnTransformOrigin(targ, nearest), MOVE_NOMONSTERS, inflictor);
-                                                               if (trace_fraction == 1 || trace_ent == targ)
-                                                               {
-                                                                       ++hits;
-                                                                       if (hits > 1)
-                                                                               hitloc = hitloc + nearest;
-                                                                       else
-                                                                               hitloc = nearest;
-                                                               }
-                                                               nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
-                                                               nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
-                                                               nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
+                                                               ++hits;
+                                                               if (hits > 1)
+                                                                       hitloc = hitloc + nearest;
+                                                               else
+                                                                       hitloc = nearest;
                                                        }
+                                                       nearest_x = targ.origin_x + targ.mins_x + random() * targ.size_x;
+                                                       nearest_y = targ.origin_y + targ.mins_y + random() * targ.size_y;
+                                                       nearest_z = targ.origin_z + targ.mins_z + random() * targ.size_z;
+                                               }
 
-                                                       nearest = hitloc * (1 / max(1, hits));
-                                                       hitratio = (hits / total);
-                                                       a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
-                                                       finaldmg = finaldmg * a;
-                                                       a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
-                                                       force = force * a;
+                                               nearest = hitloc * (1 / max(1, hits));
+                                               hitratio = (hits / total);
+                                               a = bound(0, tfloordmg + (1-tfloordmg) * hitratio, 1);
+                                               finaldmg = finaldmg * a;
+                                               a = bound(0, tfloorforce + (1-tfloorforce) * hitratio, 1);
+                                               force = force * a;
 
-                                                       if(autocvar_g_throughfloor_debug)
-                                                               print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
-                                               }
+                                               if(autocvar_g_throughfloor_debug)
+                                                       print(sprintf(" D=%f F=%f\n", finaldmg, vlen(force)));
+                                       }
 
-                                               // laser force adjustments :P
-                                               if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
+                                       // laser force adjustments :P
+                                       if(DEATH_WEAPONOF(deathtype) == WEP_LASER)
+                                       {
+                                               if (targ == attacker)
                                                {
-                                                       if (targ == attacker)
+                                                       vector vel;
+
+                                                       float force_zscale;
+                                                       float force_velocitybiasramp;
+                                                       float force_velocitybias;
+
+                                                       force_velocitybiasramp = autocvar_sv_maxspeed;
+                                                       if(deathtype & HITTYPE_SECONDARY)
                                                        {
-                                                               vector vel;
-
-                                                               float force_zscale;
-                                                               float force_velocitybiasramp;
-                                                               float force_velocitybias;
-
-                                                               force_velocitybiasramp = autocvar_sv_maxspeed;
-                                                               if(deathtype & HITTYPE_SECONDARY)
-                                                               {
-                                                                       force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
-                                                                       force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
-                                                               }
-                                                               else
-                                                               {
-                                                                       force_zscale = autocvar_g_balance_laser_primary_force_zscale;
-                                                                       force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
-                                                               }
-
-                                                               vel = targ.velocity;
-                                                               vel_z = 0;
-                                                               vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
-                                                               force =
-                                                                       vlen(force)
-                                                                       *
-                                                                       normalize(normalize(force) + vel);
-
-                                                               force_z *= force_zscale;
+                                                               force_zscale = autocvar_g_balance_laser_secondary_force_zscale;
+                                                               force_velocitybias = autocvar_g_balance_laser_secondary_force_velocitybias;
                                                        }
                                                        else
                                                        {
-                                                               if(deathtype & HITTYPE_SECONDARY)
-                                                               {
-                                                                       force *= autocvar_g_balance_laser_secondary_force_other_scale;
-                                                               }
-                                                               else
-                                                               {
-                                                                       force *= autocvar_g_balance_laser_primary_force_other_scale;
-                                                               }
+                                                               force_zscale = autocvar_g_balance_laser_primary_force_zscale;
+                                                               force_velocitybias = autocvar_g_balance_laser_primary_force_velocitybias;
                                                        }
-                                               }
 
-                                               //if (targ == attacker)
-                                               //{
-                                               //      print("hits ", ftos(hits), " / ", ftos(total));
-                                               //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
-                                               //      print(" (", ftos(a), ")\n");
-                                               //}
-                                               if(finaldmg || vlen(force))
+                                                       vel = targ.velocity;
+                                                       vel_z = 0;
+                                                       vel = normalize(vel) * bound(0, vlen(vel) / force_velocitybiasramp, 1) * force_velocitybias;
+                                                       force =
+                                                               vlen(force)
+                                                               *
+                                                               normalize(normalize(force) + vel);
+
+                                                       force_z *= force_zscale;
+                                               }
+                                               else
                                                {
-                                                       if(targ.iscreature)
+                                                       if(deathtype & HITTYPE_SECONDARY)
                                                        {
-                                                               total_damage_to_creatures += finaldmg;
-
-                                                               if(accuracy_isgooddamage(attacker, targ))
-                                                                       stat_damagedone += finaldmg;
+                                                               force *= autocvar_g_balance_laser_secondary_force_other_scale;
                                                        }
-
-                                                       if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
-                                                               Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
                                                        else
-                                                               Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
+                                                       {
+                                                               force *= autocvar_g_balance_laser_primary_force_other_scale;
+                                                       }
+                                               }
+                                       }
+
+                                       //if (targ == attacker)
+                                       //{
+                                       //      print("hits ", ftos(hits), " / ", ftos(total));
+                                       //      print(" finaldmg ", ftos(finaldmg), " force ", vtos(force));
+                                       //      print(" (", ftos(a), ")\n");
+                                       //}
+                                       if(finaldmg || vlen(force))
+                                       {
+                                               if(targ.iscreature)
+                                               {
+                                                       total_damage_to_creatures += finaldmg;
+
+                                                       if(accuracy_isgooddamage(attacker, targ))
+                                                               stat_damagedone += finaldmg;
                                                }
+
+                                               if(targ == directhitentity || DEATH_ISSPECIAL(deathtype))
+                                                       Damage (targ, inflictor, attacker, finaldmg, deathtype, nearest, force);
+                                               else
+                                                       Damage (targ, inflictor, attacker, finaldmg, deathtype | HITTYPE_SPLASH, nearest, force);
                                        }
                                }
                        }
+               }
                targ = next;
        }
 
@@ -1220,6 +1219,11 @@ float RadiusDamage (entity inflictor, entity attacker, float coredamage, float e
        return total_damage_to_creatures;
 }
 
+float RadiusDamage (entity inflictor, entity attacker, float coredamage, float edgedamage, float rad, entity cantbe, entity mustbe, float forceintensity, float deathtype, entity directhitentity)
+{
+       return RadiusDamageForSource (inflictor, (inflictor.origin + (inflictor.mins + inflictor.maxs) * 0.5), inflictor.velocity, attacker, coredamage, edgedamage, rad, cantbe, mustbe, FALSE, forceintensity, deathtype, directhitentity);
+}
+
 .float fire_damagepersec;
 .float fire_endtime;
 .float fire_deathtype;
@@ -1269,43 +1273,59 @@ float Fire_AddDamage(entity e, entity o, float d, float t, float dt)
 
                if(maxtime > mintime || maxdps > mindps)
                {
+                       // Constraints:
+                       
+                       // damage we have right now
                        mindamage = mindps * mintime;
-                       maxdamage = mindamage + d;
-
-                       // interval [mintime, maxtime] * [mindps, maxdps]
-                       // intersected with
-                       // [mindamage, maxdamage]
-                       // maximum of this!
-
-                       if(maxdamage >= maxtime * maxdps)
-                       {
-                               totaltime = maxtime;
-                               totaldamage = maxtime * maxdps;
 
-                               // this branch increases totaldamage if either t > mintime, or dps > mindps
-                       }
-                       else
-                       {
-                               // maxdamage is inside the interval!
-                               // first, try to use mindps; only if this fails, increase dps as needed
-                               totaltime = min(maxdamage / mindps, maxtime); // maxdamage / mindps >= mindamage / mindps = mintime
-                               totaldamage = maxdamage;
-                               // can totaldamage / totaltime be >= maxdps?
-                               // max(mindps, maxdamage / maxtime) >= maxdps?
-                               // we know maxdamage < maxtime * maxdps
-                               // so it cannot be
-
-                               // this branch ALWAYS increases totaldamage, but requires maxdamage < maxtime * maxdps
-                       }
+                       // damage we want to get
+                       maxdamage = mindamage + d;
 
-                       // total conditions for increasing:
-                       //     maxtime > mintime OR maxdps > mindps OR maxtime * maxdps > maxdamage
-                       // however:
-                       //     if maxtime = mintime, maxdps = mindps
-                       // then:
-                       //     maxdamage = mindamage + d
-                       //     mindamage = mindps * mintime = maxdps * maxtime < maxdamage!
-                       // so the last condition is not needed
+                       // but we can't exceed maxtime * maxdps!
+                       totaldamage = min(maxdamage, maxtime * maxdps);
+
+                       // LEMMA:
+                       // Look at:
+                       // totaldamage = min(mindamage + d, maxtime * maxdps)
+                       // We see:
+                       // totaldamage <= maxtime * maxdps
+                       // ==> totaldamage / maxdps <= maxtime.
+                       // We also see:
+                       // totaldamage / mindps = min(mindamage / mindps + d, maxtime * maxdps / mindps)
+                       //                     >= min(mintime, maxtime)
+                       // ==> totaldamage / maxdps >= mintime.
+
+                       /*
+                       // how long do we damage then?
+                       // at least as long as before
+                       // but, never exceed maxdps
+                       totaltime = max(mintime, totaldamage / maxdps); // always <= maxtime due to lemma
+                       */
+
+                       // alternate:
+                       // at most as long as maximum allowed
+                       // but, never below mindps
+                       totaltime = min(maxtime, totaldamage / mindps); // always >= mintime due to lemma
+
+                       // assuming t > mintime, dps > mindps:
+                       // we get d = t * dps = maxtime * maxdps
+                       // totaldamage = min(maxdamage, maxtime * maxdps) = min(... + d, maxtime * maxdps) = maxtime * maxdps
+                       // totaldamage / maxdps = maxtime
+                       // totaldamage / mindps > totaldamage / maxdps = maxtime
+                       // FROM THIS:
+                       // a) totaltime = max(mintime, maxtime) = maxtime
+                       // b) totaltime = min(maxtime, totaldamage / maxdps) = maxtime
+
+                       // assuming t <= mintime:
+                       // we get maxtime = mintime
+                       // a) totaltime = max(mintime, ...) >= mintime, also totaltime <= maxtime by the lemma, therefore totaltime = mintime = maxtime
+                       // b) totaltime = min(maxtime, ...) <= maxtime, also totaltime >= mintime by the lemma, therefore totaltime = mintime = maxtime
+
+                       // assuming dps <= mindps:
+                       // we get mindps = maxdps.
+                       // With this, the lemma says that mintime <= totaldamage / mindps = totaldamage / maxdps <= maxtime.
+                       // a) totaltime = max(mintime, totaldamage / maxdps) = totaldamage / maxdps
+                       // b) totaltime = min(maxtime, totaldamage / mindps) = totaldamage / maxdps
 
                        e.fire_damagepersec = totaldamage / totaltime;
                        e.fire_endtime = time + totaltime;