Advertisement
CUgopEntity

WeaponShotgun.cpp

Jan 23rd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdafx.h"
  2. #include "weaponshotgun.h"
  3. #include "WeaponHUD.h"
  4. #include "xr_weapon_list.h"
  5. #include "entity.h"
  6. #include "effectorshot.h"
  7.  
  8. CWeaponShotgun::CWeaponShotgun(void) : CWeaponCustomPistol("TOZ34")
  9. {
  10.     m_eSoundShotBoth    = ESoundTypes(SOUND_TYPE_WEAPON_SHOOTING /*| eSoundType*/);
  11. }
  12.  
  13. CWeaponShotgun::~CWeaponShotgun(void)
  14. {
  15. }
  16. //
  17. //void CWeaponShotgun::OnZoomIn() {
  18. //}
  19.  
  20. void CWeaponShotgun::net_Destroy()
  21. {
  22.     inherited::net_Destroy();
  23.  
  24.     // sounds
  25.     SoundDestroy        (sndShot        );
  26. }
  27.  
  28. void CWeaponShotgun::Load   (LPCSTR section)
  29. {
  30.     inherited::Load     (section);
  31.  
  32.     // «вук и анимаци¤ дл¤ дуплета
  33.     SoundCreate         (sndShotBoth,       "shoot_both"   ,m_eSoundShotBoth);
  34.     animGet             (mhud_shot_boths,   "shoot_both");
  35.  
  36.     //
  37.     if(pSettings->LineExists(section,"fire_point2")) vFirePoint2 = pSettings->ReadVECTOR(section,"fire_point2");
  38.     else vFirePoint2 = vFirePoint;
  39. }
  40.  
  41. void CWeaponShotgun::OnShot () {
  42.     swap(m_pHUD->vFirePoint, m_pHUD->vFirePoint2);
  43.     swap(vFirePoint, vFirePoint2);
  44.     UpdateFP();
  45.     inherited::OnShot();
  46. }
  47.  
  48. void CWeaponShotgun::Fire2Start () {
  49.     inherited::Fire2Start();
  50.     if (IsValid())
  51.     {
  52.         if (!IsWorking())
  53.         {
  54.             if (STATE==eReload)         return;
  55.             if (STATE==eShowing)        return;
  56.             if (STATE==eHiding)         return;
  57.  
  58.             if (!iAmmoElapsed && iAmmoCurrent) 
  59.             {
  60.                 CWeapon::FireStart  ();
  61.                 SwitchState         (eMagEmpty);
  62.             }
  63.             else                           
  64.             {
  65.                 CWeapon::FireStart  ();
  66.                 SwitchState         ((iAmmoElapsed < iMagazineSize)?eFire:eFire2);
  67.             }
  68.         }
  69.     }else{
  70.         if (!iAmmoElapsed && !iAmmoCurrent)
  71.             SwitchState         (eMagEmpty);
  72.     }
  73. }
  74.  
  75. void CWeaponShotgun::Fire2End () {
  76.     inherited::Fire2End();
  77.     if (IsWorking())
  78.     {
  79.         CWeapon::FireEnd    ();
  80.         if (iAmmoCurrent && !iAmmoElapsed)  TryReload   ();
  81.         else                                SwitchState (eIdle);
  82.     }
  83. }
  84.  
  85.  
  86. void CWeaponShotgun::OnShotBoth()
  87. {
  88.     if(iAmmoElapsed < iMagazineSize) { OnShot(); return; }
  89.  
  90.     // Sound
  91.     Sound->PlayAtPos            (sndShotBoth,H_Root(),vLastFP);
  92.  
  93.     // Camera
  94.     if (hud_mode)  
  95.     {
  96.         CEffectorShot* S        = dynamic_cast<CEffectorShot*>  (Level().Cameras.GetEffector(cefShot));
  97.         if (!S) S               = (CEffectorShot*)Level().Cameras.AddEffector(xr_new<CEffectorShot> (camMaxAngle,camRelaxSpeed));
  98.         R_ASSERT                (S);
  99.         S->Shot                 (camDispersion);
  100.     }
  101.    
  102.     // Animation
  103.     m_pHUD->animPlay            (mhud_shot_boths[Random.randI(mhud_shots.size())],FALSE);
  104.    
  105.     // Flames
  106.     fFlameTime                  = .1f;
  107.    
  108.     // Shell Drop
  109.     OnShellDrop                 ();
  110. }
  111.  
  112. void CWeaponShotgun::switch2_Fire   ()
  113. {
  114.     if (fTime<=0)
  115.     {
  116.         UpdateFP                    ();
  117.  
  118.         // Fire
  119.         Fvector                     p1, d;
  120.         CEntity*                    E = dynamic_cast<CEntity*>(H_Parent());
  121.         if (E) E->g_fireParams      (p1,d);
  122.         bFlame                      =   TRUE;
  123.         OnShot                      ();
  124.         FireTrace                   (p1,vLastFP,d);
  125.         fTime                       += fTimeToFire;
  126.  
  127.         // Patch for "previous frame position" :)))
  128.         dwFP_Frame                  = 0xffffffff;
  129.         dwXF_Frame                  = 0xffffffff;
  130.     }
  131. }
  132.  
  133. void CWeaponShotgun::switch2_Fire2  ()
  134. {
  135.     if (fTime<=0)
  136.     {
  137.         UpdateFP                    ();
  138.  
  139.         // Fire
  140.         Fvector                     p1, d;
  141.         CEntity*                    E = dynamic_cast<CEntity*>(H_Parent());
  142.         if (E) E->g_fireParams      (p1,d);
  143.         bFlame                      =   TRUE;
  144.         OnShotBoth                      ();
  145.         //iAmmoElapsed  --;
  146.         FireTrace                   (p1,vLastFP,d);
  147.         FireTrace                   (p1,vLastFP,d);
  148.         fTime                       += fTimeToFire*2.f;
  149.  
  150.         // Patch for "previous frame position" :)))
  151.         dwFP_Frame                  = 0xffffffff;
  152.         dwXF_Frame                  = 0xffffffff;
  153.     }
  154. }
  155.  
  156. #define FLAME_TIME 0.05f
  157. void CWeaponShotgun::OnDrawFlame    ()
  158. {
  159.     if(STATE != eFire2) inherited::OnDrawFlame();
  160.     else if (fFlameTime>0) 
  161.     {
  162.         // fire flash
  163.         Fvector P = vLastFP;
  164.         float k=fFlameTime/FLAME_TIME;
  165.         Fvector D; D.mul(vLastFD,::Random.randF(fFlameLength*k)/float(iFlameDiv));
  166.         float f = fFlameSize;
  167.         for (int i=0; i<iFlameDiv; i++)
  168.         {
  169.             f       *= 0.9f;
  170.             float   S = f+f*::Random.randF  ();
  171.             float   A = ::Random.randF      (PI_MUL_2);
  172.             ::Render->add_Patch             (hFlames[Random.randI(hFlames.size())],P,S,A,hud_mode);
  173.             P.add(D);
  174.         }
  175.  
  176.         swap(m_pHUD->vFirePoint, m_pHUD->vFirePoint2);
  177.         swap(vFirePoint, vFirePoint2);
  178.         UpdateFP();
  179.  
  180.         // fire flash 2
  181.         P = vLastFP;
  182.         k=fFlameTime/FLAME_TIME;
  183.         D; D.mul(vLastFD,::Random.randF(fFlameLength*k)/float(iFlameDiv));
  184.         f = fFlameSize;
  185.         for (int i=0; i<iFlameDiv; i++)
  186.         {
  187.             f       *= 0.9f;
  188.             float   S = f+f*::Random.randF  ();
  189.             float   A = ::Random.randF      (PI_MUL_2);
  190.             ::Render->add_Patch             (hFlames[Random.randI(hFlames.size())],P,S,A,hud_mode);
  191.             P.add(D);
  192.         }
  193.         fFlameTime -= Device.fTimeDelta;
  194.     }
  195. }
  196.  
  197.  
  198. void CWeaponShotgun::OnVisible  ()
  199. {
  200.     inherited::OnVisible    ();
  201.     if(STATE == eFire2) OnDrawFlame();
  202. }
  203.  
  204.  
  205. BOOL CWeaponShotgun::FireTrace      (const Fvector& P, const Fvector& Peff, Fvector& D)
  206. {
  207.     BOOL bResult = 0;
  208.     int grape_shot = 5;
  209.     while(grape_shot--) {
  210.         if(!Parent) break;
  211.         Collide::ray_query      RQ;
  212.  
  213.         // direct it by dispersion factor
  214.         Fvector                 dir;
  215.         dir.random_dir          (D,(fireDispersionBase+fireDispersion*fireDispersion_Current)*GetPrecision(),Random);
  216.  
  217.         // increase dispersion
  218.         fireDispersion_Current  += fireDispersion_Inc;
  219.         clamp                   (fireDispersion_Current,0.f,1.f);
  220.  
  221.         // ...and trace line
  222.         H_Parent()->setEnabled  (false);
  223.         bResult         = Level().ObjectSpace.RayPick( P, dir, fireDistance, RQ );
  224.         H_Parent()->setEnabled  (true);
  225.         D                       = dir;
  226.  
  227.         // ...analyze
  228.         Fvector end_point;
  229.         end_point.mad       (P,D,RQ.range);
  230.         if (bResult)
  231.         {
  232.             if (Local() && RQ.O)
  233.             {
  234.                 float power     =   float(iHitPower);
  235.                 float scale     =   1-(RQ.range/fireDistance);  clamp(scale,0.f,1.f);
  236.                 power           *=  _sqrt(scale);
  237.                 float impulse   =   fHitImpulseScale*power;
  238.                 CEntity* E      =   dynamic_cast<CEntity*>(RQ.O);
  239.                 //CGameObject* GO   =   dynamic_cast<CGameObject*>(RQ.O);
  240.                 if (E) power    *=  E->HitScale(RQ.element);
  241.  
  242.                 // object-space
  243.                 Fvector p_in_object_space,position_in_bone_space;
  244.                 Fmatrix m_inv;
  245.                 m_inv.invert            (RQ.O->clXFORM());
  246.                 m_inv.transform_tiny    (p_in_object_space, end_point);
  247.  
  248.                 // bone-space
  249.                 CKinematics*=   PKinematics(RQ.O->Visual());
  250.                 Fmatrix& m_bone =   (V->LL_GetInstance(RQ.element)).mTransform;
  251.                 Fmatrix  m_inv_bone;
  252.                 m_inv_bone.invert           (m_bone);
  253.                 m_inv_bone.transform_tiny   (position_in_bone_space, p_in_object_space);
  254.  
  255.  
  256.                 //  
  257.                 NET_Packet      P;
  258.                 u_EventGen      (P,GE_HIT,RQ.O->ID());
  259.                 P.w_u16         (u16(H_Parent()->ID()));
  260.                 P.w_dir         (D);
  261.                 P.w_float       (power);
  262.                 P.w_s16         ((s16)RQ.element);
  263.                 P.w_vec3        (position_in_bone_space);
  264.                 P.w_float       (impulse);
  265.                 u_EventSend     (P);
  266.             }
  267.             FireShotmark        (D,end_point,RQ);
  268.         }
  269.     }
  270.  
  271.     //// tracer
  272.     //if (tracerFrame != Device.dwFrame)
  273.     //{
  274.     //  tracerFrame = Device.dwFrame;
  275.     //  Level().Tracers.Add (Peff,end_point,tracerHeadSpeed,tracerTrailCoeff,tracerStartLength,tracerWidth);
  276.     //}
  277.  
  278.     // light
  279.     Light_Start         ();
  280.    
  281.     // Ammo
  282.     if (Local())
  283.     {
  284.         iAmmoElapsed    --;
  285.         if (iAmmoElapsed==0)
  286.         {
  287.             OnMagazineEmpty ();
  288.         }
  289.     }
  290.    
  291.     return              bResult;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement