]> 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 47e79bcaabee8ba211071a692ec0c07ab03ba76e..78a824d2d9bd4c46087a661b2c533604c5dc30d7 100644 (file)
@@ -3,6 +3,21 @@ REGISTER_WEAPON(LASER, w_laser, 0, 1, WEP_FLAG_NORMAL | WEP_FLAG_RELOADABLE | WE
 #else
 #ifdef SVQC
 void(float imp) W_SwitchWeapon;
+void() W_LastWeapon;
+
+void SendCSQCShockwaveParticle(float spread, vector endpos) 
+{
+       //WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos);
+       WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);
+       WriteByte(MSG_BROADCAST, TE_CSQC_SHOCKWAVEPARTICLE);
+       WriteCoord(MSG_BROADCAST, w_shotorg_x);
+       WriteCoord(MSG_BROADCAST, w_shotorg_y);
+       WriteCoord(MSG_BROADCAST, w_shotorg_z);
+       WriteCoord(MSG_BROADCAST, endpos_x);
+       WriteCoord(MSG_BROADCAST, endpos_y);
+       WriteCoord(MSG_BROADCAST, endpos_z);
+       WriteByte(MSG_BROADCAST, bound(0, 255 * spread, 255));
+}
 
 void W_Laser_Touch (void)
 {
@@ -10,9 +25,9 @@ void W_Laser_Touch (void)
 
        self.event_damage = SUB_Null;
        if (self.dmg)
-               RadiusDamage (self, self.realowner, autocvar_g_balance_laser_secondary_damage, autocvar_g_balance_laser_secondary_edgedamage, autocvar_g_balance_laser_secondary_radius, world, autocvar_g_balance_laser_secondary_force, self.projectiledeathtype, other);
+               RadiusDamage (self, self.realowner, autocvar_g_balance_laser_secondary_damage, autocvar_g_balance_laser_secondary_edgedamage, autocvar_g_balance_laser_secondary_radius, world, world, autocvar_g_balance_laser_secondary_force, self.projectiledeathtype, other);
        else
-               RadiusDamage (self, self.realowner, autocvar_g_balance_laser_primary_damage, autocvar_g_balance_laser_primary_edgedamage, autocvar_g_balance_laser_primary_radius, world, autocvar_g_balance_laser_primary_force, self.projectiledeathtype, other);
+               RadiusDamage (self, self.realowner, autocvar_g_balance_laser_primary_damage, autocvar_g_balance_laser_primary_edgedamage, autocvar_g_balance_laser_primary_radius, world, world, autocvar_g_balance_laser_primary_force, self.projectiledeathtype, other);
 
        remove (self);
 }
@@ -28,9 +43,157 @@ void W_Laser_Think()
        CSQCProjectile(self, TRUE, PROJECTILE_LASER, 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 nearest_on_line, vector sw_shotorg, vector attack_hitpos)
+{
+       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_CheckSpread(nearest_to_attacker, nearest_on_line, sw_shotorg, attack_hitpos))
+       {
+               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_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
+       }
+
+       // STEP THREE: Check each corner to see if they are clear
+       for(i=1; i<=8; ++i)
+       {
+               corner = get_corner_position(head, i);
+               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
+               }
+       }
+
+       return FALSE;
+}
+
+void W_Laser_Shockwave (void)
+{
+       // declarations
+       float final_damage, final_spread;
+       entity head, next, aim_ent;
+       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 and acquire the warpzone transform
+       WarpZone_TraceLine(w_shotorg, attack_endpos, FALSE, self);
+       aim_ent = trace_ent;
+       attack_hitpos = trace_endpos;
+       
+       // do the jump explosion now (also handles the impact effect)
+       RadiusDamageForSource(self, trace_endpos, '0 0 0', self, autocvar_g_balance_laser_primary_damage, autocvar_g_balance_laser_primary_edgedamage, autocvar_g_balance_laser_primary_jumpradius, world, self, TRUE, autocvar_g_balance_laser_primary_force, WEP_LASER, world);
+       
+       // also do the firing effect now
+       SendCSQCShockwaveParticle(autocvar_g_balance_laser_primary_spread, attack_hitpos);
+       
+       // 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) // todo
+               if (aim_ent.classname == "player")
+                       center = aim_ent.origin + aim_ent.view_ofs;
+               else
+                       center = aim_ent.origin + (aim_ent.mins + aim_ent.maxs) * 0.5;
+
+               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);
+       }
+
+       // now figure out if I hit anything else than what my aim directly pointed at...
+       head = WarpZone_FindRadius(w_shotorg, autocvar_g_balance_laser_primary_radius, FALSE);
+       while(head)
+       {
+               next = head.chain;
+               
+               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) // todo
+                       if (head.classname == "player")
+                               center = head.origin + head.view_ofs;
+                       else
+                               center = head.origin + (head.mins + head.maxs) * 0.5;
+
+                       // find the closest point on the enemy to the center of the attack
+                       float h = vlen(center - self.origin);
+                       float ang = acos(dotproduct(normalize(center - self.origin), w_shotdir));
+                       float a = h * cos(ang);
+                       
+                       // ang = angle between shotdir and h
+                       // 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
+
+                       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(distance_to_target <= autocvar_g_balance_laser_primary_radius)
+                       {
+                               if(W_Laser_Shockwave_IsVisible(head, nearest_on_line, w_shotorg, attack_hitpos))
+                               {
+                                       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);
+                                       
+                                       //pointparticles(particleeffectnum("rocket_guide"), w_shotorg, w_shotdir * 1000, 1);
+                                       //SendCSQCShockwaveParticle(autocvar_g_balance_laser_primary_spread, trace_endpos);
+                               }
+                       }
+               }
+               head = next;
+       }
+}
+
 void W_Laser_Attack (float issecondary)
 {
-       local entity missile;
+       entity missile;
        vector s_forward;
        float a;
        float nodamage;
@@ -44,11 +207,11 @@ void W_Laser_Attack (float issecondary)
        s_forward = v_forward * cos(a * DEG2RAD) + v_up * sin(a * DEG2RAD);
 
        if(nodamage)
-               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, 0);
+               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, 0);
        else if(issecondary == 1)
-               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, autocvar_g_balance_laser_secondary_damage);
+               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_secondary_damage);
        else
-               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CHAN_WEAPON2, autocvar_g_balance_laser_primary_damage);
+               W_SetupShot_Dir (self, s_forward, FALSE, 3, "weapons/lasergun_fire.wav", CH_WEAPON_B, autocvar_g_balance_laser_primary_damage);
        pointparticles(particleeffectnum("laser_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
 
        missile = spawn ();
@@ -74,6 +237,7 @@ void W_Laser_Attack (float issecondary)
        missile.touch = W_Laser_Touch;
 
        missile.flags = FL_PROJECTILE;
+       missile.missile_flags = MIF_SPLASH; 
 
        missile.think = W_Laser_Think;
        missile.nextthink = time + autocvar_g_balance_laser_primary_delay;
@@ -177,7 +341,7 @@ void W_Laser_Attack2 ()
        // only play fire sound if 0.5 sec has passed since player let go the fire button
        if(time - self.prevgauntletfire > 0.5)
        {
-               sound (self, CHAN_WEAPON, "weapons/gauntlet_fire.wav", VOL_BASE, ATTN_NORM);
+               sound (self, CH_WEAPON_A, "weapons/gauntlet_fire.wav", VOL_BASE, ATTN_NORM);
        }
 
        entity beam, oldself;
@@ -214,8 +378,8 @@ void spawnfunc_weapon_laser (void)
 
 float w_laser(float req)
 {
-       local float r1;
-       local float r2;
+       float r1;
+       float r2;
        if (req == WR_AIM)
        {
                if(autocvar_g_balance_laser_secondary)
@@ -240,26 +404,51 @@ float w_laser(float req)
                        {
                                W_DecreaseAmmo(ammo_none, 1, TRUE);
 
-                               W_Laser_Attack(0);
+
+                               if not(autocvar_g_balance_laser_oldprimary)
+                                       W_Laser_Shockwave();
+                               else
+                                       W_Laser_Attack(FALSE);
+
                                weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_laser_primary_animtime, w_ready);
                        }
                }
                else if (self.BUTTON_ATCK2)
                {
-                       if(autocvar_g_balance_laser_secondary)
+                       switch(autocvar_g_balance_laser_secondary)
                        {
-                               W_DecreaseAmmo(ammo_none, 1, TRUE);
+                               case 0: // switch to last used weapon
+                               {
+                                       if(self.switchweapon == WEP_LASER) // don't do this if already switching
+                                               W_LastWeapon();
 
-                               if (weapon_prepareattack(0, 0))
+                                       break;
+                               }
+
+                               case 1: // normal projectile secondary
                                {
-                                       W_Laser_Attack2();
-                                       weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
+                                       if(weapon_prepareattack(0, autocvar_g_balance_laser_secondary_refire))
+                                       {
+                                               W_DecreaseAmmo(ammo_none, 1, TRUE);
+                                               W_Laser_Attack(TRUE);
+                                               weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
+                                       }
+
+                                       break;
+                               }
+
+                               case 2: // gauntlet secondary
+                               {
+                                       W_DecreaseAmmo(ammo_none, 1, TRUE);
+
+                                       if (weapon_prepareattack(0, 0))
+                                       {
+                                               W_Laser_Attack2();
+                                               weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_laser_secondary_animtime, w_ready);
+                                       }
+
+                                       break;
                                }
-                       }
-                       else
-                       {
-                               if(self.switchweapon == WEP_LASER) // don't do this if already switching
-                                       W_SwitchWeapon (self.cnt);
                        }
                }
        }
@@ -290,7 +479,7 @@ float w_laser(float req)
                W_Reload(0, autocvar_g_balance_laser_reload_ammo, autocvar_g_balance_laser_reload_time, "weapons/reload.wav");
        }
        return TRUE;
-};
+}
 #endif
 #ifdef CSQC
 float w_laser(float req)
@@ -299,9 +488,9 @@ float w_laser(float req)
        {
                vector org2;
                org2 = w_org + w_backoff * 6;
-               pointparticles(particleeffectnum("laser_impact"), org2, w_backoff * 1000, 1);
+               pointparticles(particleeffectnum("new_laser_impact"), org2, w_backoff * 1000, 1);
                if(!w_issilent)
-                       sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
+                       sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
        }
        else if(req == WR_PRECACHE)
        {