]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/w_laser.qc
Finishing up the spread work by adding new multiplier system for accuracy
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_laser.qc
index cccc219686641d4b9e5d0767429310592c3de77d..78a824d2d9bd4c46087a661b2c533604c5dc30d7 100644 (file)
@@ -43,34 +43,44 @@ void W_Laser_Think()
        CSQCProjectile(self, TRUE, PROJECTILE_LASER, TRUE);
 }
 
-// TODO: change this into a macro to run faster (less function calls is better)
-float W_Laser_Shockwave_CheckSpreadAngle(vector targetorg, vector sw_shotorg, vector sw_shotdir)
-{
-       vector angle_to_head = normalize(targetorg - sw_shotorg);
-       vector angle_to_attack = sw_shotdir;
 
-       if(vlen(angle_to_head - angle_to_attack) <= autocvar_g_balance_laser_primary_spread)
-               return TRUE;
+float W_Laser_Shockwave_CheckSpread(vector targetorg, vector nearest_on_line, vector sw_shotorg, vector attack_hitpos)
+{
+       float spreadlimit;
+       float distance_of_attack = vlen(sw_shotorg - attack_hitpos);
+       float distance_from_line = vlen(targetorg - nearest_on_line);
+       
+       spreadlimit = (distance_of_attack ? min(1, (vlen(sw_shotorg - nearest_on_line) / distance_of_attack)) : 1);
+       spreadlimit = (autocvar_g_balance_laser_primary_spread_min * (1 - spreadlimit) + autocvar_g_balance_laser_primary_spread_max * spreadlimit);
+       
+       if(spreadlimit && (distance_from_line <= spreadlimit))
+       {
+               //te_lightning2(world, targetorg, nearest_on_line);
+               //te_lightning2(world, targetorg, sw_shotorg);
+               //print("just in case: ", ftos(distance_from_line), ", ", ftos(spreadlimit), ".\n");
+               
+               return bound(0, (distance_from_line / spreadlimit), 1);
+       }
        else
                return FALSE;
 }
 
-float W_Laser_Shockwave_IsVisible(entity head, vector sw_shotorg, vector sw_shotdir)
+float W_Laser_Shockwave_IsVisible(entity head, vector nearest_on_line, vector sw_shotorg, vector attack_hitpos)
 {
-       vector nearest = head.WarpZone_findradius_nearest;
+       vector nearest_to_attacker = head.WarpZone_findradius_nearest;
        vector center = (head.origin + (head.mins + head.maxs) * 0.5);
        vector corner;
        float i;
 
        // STEP ONE: Check if the nearest point is clear
-       if(W_Laser_Shockwave_CheckSpreadAngle(nearest, sw_shotorg, sw_shotdir))
+       if(W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, sw_shotorg, attack_hitpos))
        {
-               WarpZone_TraceLine(sw_shotorg, nearest, MOVE_WORLDONLY, self);
+               WarpZone_TraceLine(sw_shotorg, nearest_to_attacker, MOVE_WORLDONLY, self);
                if(trace_fraction == 1) { return TRUE; } // yes, the nearest point is clear and we can allow the damage
        }
 
        // STEP TWO: Check if shotorg to center point is clear
-       if(W_Laser_Shockwave_CheckSpreadAngle(center, sw_shotorg, sw_shotdir))
+       if(W_Laser_Shockwave_CheckSpread(center, nearest_on_line, sw_shotorg, attack_hitpos))
        {
                WarpZone_TraceLine(sw_shotorg, center, MOVE_WORLDONLY, self);
                if(trace_fraction == 1) { return TRUE; } // yes, the center point is clear and we can allow the damage
@@ -80,7 +90,7 @@ float W_Laser_Shockwave_IsVisible(entity head, vector sw_shotorg, vector sw_shot
        for(i=1; i<=8; ++i)
        {
                corner = get_corner_position(head, i);
-               if(W_Laser_Shockwave_CheckSpreadAngle(corner, sw_shotorg, sw_shotdir))
+               if(W_Laser_Shockwave_CheckSpread(corner, nearest_on_line, sw_shotorg, attack_hitpos))
                {
                        WarpZone_TraceLine(sw_shotorg, corner, MOVE_WORLDONLY, self);
                        if(trace_fraction == 1) { return TRUE; } // yes, this corner is clear and we can allow the damage
@@ -95,14 +105,14 @@ void W_Laser_Shockwave (void)
        // declarations
        float final_damage, final_spread;
        entity head, next, aim_ent;
-       vector nearest, attack_hitpos, angle_to_head, angle_to_attack, final_force, center;
+       vector attack_hitpos, final_force, center;
        
        // set up the shot direction
        vector wanted_shot_direction = (v_forward * cos(autocvar_g_balance_laser_primary_shotangle * DEG2RAD) + v_up * sin(autocvar_g_balance_laser_primary_shotangle * DEG2RAD));
        W_SetupShot_Dir(self, wanted_shot_direction, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_primary_damage);
        vector attack_endpos = (w_shotorg + (w_shotdir * autocvar_g_balance_laser_primary_radius));
 
-       // find out what we're pointing at
+       // find out what we're pointing at and acquire the warpzone transform
        WarpZone_TraceLine(w_shotorg, attack_endpos, FALSE, self);
        aim_ent = trace_ent;
        attack_hitpos = trace_endpos;
@@ -116,7 +126,7 @@ void W_Laser_Shockwave (void)
        // did we hit a player directly?
        if(aim_ent.takedamage)
        {
-               // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
+               // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc) // todo
                if (aim_ent.classname == "player")
                        center = aim_ent.origin + aim_ent.view_ofs;
                else
@@ -124,7 +134,6 @@ void W_Laser_Shockwave (void)
 
                final_force = (normalize(center - attack_hitpos) * autocvar_g_balance_laser_primary_force);
                Damage(aim_ent, self, self, autocvar_g_balance_laser_primary_damage, WEP_LASER, aim_ent.origin, final_force);
-               print("Player hit directly via aim!\n");
        }
 
        // now figure out if I hit anything else than what my aim directly pointed at...
@@ -135,7 +144,7 @@ void W_Laser_Shockwave (void)
                
                if((head != self && head != aim_ent) && (head.takedamage))
                {
-                       // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc)
+                       // if it's a player, use the view origin as reference (stolen from RadiusDamage functions in g_damage.qc) // todo
                        if (head.classname == "player")
                                center = head.origin + head.view_ofs;
                        else
@@ -150,34 +159,29 @@ void W_Laser_Shockwave (void)
                        // h = hypotenuse, which is the distance between attacker to head
                        // a = adjacent side, which is the distance between attacker and the point on w_shotdir that is closest to head.origin
 
-                       nearest = WarpZoneLib_NearestPointOnBox(center + head.mins, center + head.maxs, w_shotorg + a * w_shotdir);
+                       vector nearest_on_line = (w_shotorg + a * w_shotdir);
+                       vector nearest_to_attacker = WarpZoneLib_NearestPointOnBox(center + head.mins, center + head.maxs, nearest_on_line);
+                       float distance_to_target = vlen(w_shotorg - nearest_to_attacker);
+                       float distance_of_attack = vlen(w_shotorg - attack_hitpos);
 
-                       if(vlen(w_shotorg - nearest) <= autocvar_g_balance_laser_primary_radius)
+                       if(distance_to_target <= autocvar_g_balance_laser_primary_radius)
                        {
-                               if(W_Laser_Shockwave_IsVisible(head, w_shotorg, w_shotdir))
+                               if(W_Laser_Shockwave_IsVisible(head, nearest_on_line, w_shotorg, attack_hitpos))
                                {
-                                       //WarpZone_TraceLine(w_shotorg, nearest, MOVE_WORLDONLY, self);
-                                       //if(trace_fraction == 1)
-                                       //{
-                                       // finally lets do some damage bitches!
-                                       if(autocvar_g_balance_laser_primary_spread)
-                                               final_damage = (final_spread / autocvar_g_balance_laser_primary_spread);
-                                       else
-                                               final_damage = 1;
-
-                                       //final_force = (normalize(nearest - w_shotorg) * autocvar_g_balance_laser_primary_force); // we dont want to use nearest here, because that would result in some rather weird force dirs for the attacker...
-                                       print(strcat("head.origin: ", vtos(head.origin), ", (w_shotorg + a * w_shotdir): ", vtos(w_shotorg + a * w_shotdir), ".\n"));
-                                       print("a = ", ftos(a), " h = ", ftos(h), " ang = ", ftos(ang), "\n");
-                                       final_force = (normalize(center - (w_shotorg + a * w_shotdir)) * autocvar_g_balance_laser_primary_force);
-                                       final_damage = (autocvar_g_balance_laser_primary_damage * final_damage + autocvar_g_balance_laser_primary_edgedamage * (1 - final_damage));
+                                       float multiplier_from_accuracy = (1 - W_Laser_Shockwave_CheckSpread(nearest_to_attacker, nearest_on_line, w_shotorg, attack_hitpos));
+                                       float multiplier_from_distance = (1 - (distance_of_attack ? min(1, (distance_to_target / autocvar_g_balance_laser_primary_radius)) : 1));
+
+                                       float multiplier = max(autocvar_g_balance_laser_primary_multiplier_min, ((multiplier_from_accuracy * autocvar_g_balance_laser_primary_multiplier_accuracy) + (multiplier_from_distance * autocvar_g_balance_laser_primary_multiplier_distance)));
+                                       print("multiplier = ", ftos(multiplier), ", multiplier_from_accuracy = ", ftos(multiplier_from_accuracy), ", multiplier_from_distance = ", ftos(multiplier_from_distance), "\n");
+
+                                       //print(strcat("head.origin: ", vtos(head.origin), ", nearest_on_line: ", vtos(nearest_on_line), ".\n"));
+                                       final_force = ((normalize(center - nearest_on_line) * autocvar_g_balance_laser_primary_force) * multiplier);
+                                       final_damage = (autocvar_g_balance_laser_primary_damage * multiplier + autocvar_g_balance_laser_primary_edgedamage * (1 - multiplier));
                                        
                                        print(strcat("damage: ", ftos(final_damage), ", force: ", vtos(final_force), ".\n"));
                                        
                                        Damage(head, self, self, final_damage, WEP_LASER, head.origin, final_force);
                                        
-                                       print(strcat(vtos(angle_to_head), " - ", vtos(angle_to_attack), ": ", ftos(vlen(angle_to_head - angle_to_attack)), ".\n"));
-                                       //te_lightning2(world, nearest, w_shotorg);
-                                       
                                        //pointparticles(particleeffectnum("rocket_guide"), w_shotorg, w_shotdir * 1000, 1);
                                        //SendCSQCShockwaveParticle(autocvar_g_balance_laser_primary_spread, trace_endpos);
                                }