X2CommunityCore / X2WOTCCommunityHighlander

https://steamcommunity.com/workshop/filedetails/?id=1134256495
MIT License
60 stars 68 forks source link

Weapon glitches when dropped by killed units #1290

Open Iridar opened 10 months ago

Iridar commented 10 months ago

Allegedly this bug is fixed by using the Chimera Squad version of XGUnit::DropWeapon() function.

Iridar commented 10 months ago

The function used in Chimera doesn't account for customizable weapons, so you'll need to use the DecorateWeaponMesh function found in XGWeapon.uc to populate the newly spawned weapon with the attachments, colors and patterns from the old weapon. Otherwise only the base mesh without any color nor attachment will be spawned for xcom weapons.

BlackDog86 commented 9 months ago

For reference: WOTC XGUnit::DropWeapon

simulated function DropWeapon()
{
    local XGWeapon  kWeapon;
    local Vector    vLoc;
    local Rotator   rRot;
    local XComWeapon kXComWeapon;

    // Lose the weapon we're holding here.  Drop it or launch it.
    kWeapon = GetInventory().GetActiveWeapon();

    if (kWeapon != none && GetPawn().m_bDropWeaponOnDeath )
    {       
        kXComWeapon = XComWeapon(kWeapon.m_kEntity);
        m_kPawn.Mesh.GetSocketWorldLocationAndRotation(kXComWeapon.DefaultSocket, vLoc, rRot);
        m_kPawn.Mesh.DetachComponent(kXComWeapon.Mesh);
        kXComWeapon.SetBase(None);
        kWeapon.m_kEntity.AttachComponent(kXComWeapon.Mesh);
        SkeletalMeshComponent(kXComWeapon.Mesh).SetPhysicsAsset(SkeletalMeshComponent(kXComWeapon.Mesh).PhysicsAsset, true);
        //GetInventory().DropItem( kWeapon );
        //GetInventory().UnequipItem();
        kWeapon.m_kEntity.CollisionComponent = kXComWeapon.Mesh;
        SkeletalMeshComponent(kXComWeapon.Mesh).PhysicsWeight=1.0f;
        SkeletalMeshComponent(kXComWeapon.Mesh).ForceSkelUpdate();
        SkeletalMeshComponent(kXComWeapon.Mesh).UpdateRBBonesFromSpaceBases(TRUE, TRUE);
        SkeletalMeshComponent(kXComWeapon.Mesh).bSyncActorLocationToRootRigidBody=true;

        kXComWeapon.Mesh.WakeRigidBody();
        kWeapon.m_kEntity.SetPhysics(PHYS_RigidBody /*PHYS_None*/);
        kWeapon.m_kEntity.SetHidden(false);
        kWeapon.m_kEntity.SetLocation(vLoc);
        kWeapon.m_kEntity.SetRotation(rRot);

        SkeletalMeshComponent(kXComWeapon.Mesh).SetRBPosition(vLoc);
        SkeletalMeshComponent(kXComWeapon.Mesh).SetRBRotation(rRot);
        SkeletalMeshComponent(kXComWeapon.Mesh).SetRBLinearVelocity(vect(0,0,0), false);
        SkeletalMeshComponent(kXComWeapon.Mesh).SetRBAngularVelocity(vect(0,0,0), false);
    }
}

Chimera Squad XGUnit::DropWeapon

simulated function DropWeapon()
{
    local XGWeapon  kWeapon;
    local Vector    vLoc;
    local Rotator   rRot;
    local XComWeapon kXComWeapon;   
    local SkeletalMeshComponent DroppedWeaponMesh;

    // Lose the weapon we're holding here.  Drop it or launch it.
    kWeapon = GetInventory().GetActiveWeapon();

    if (kWeapon != none)
    {   
        kXComWeapon = XComWeapon(kWeapon.m_kEntity);        
        m_kPawn.Mesh.GetSocketWorldLocationAndRotation(kXComWeapon.DefaultSocket, vLoc, rRot);      
        kXComWeapon.DropWeapon();

        //Spawn a separate actor that will be the dropped weapon
        DroppedWeapon = Spawn(class'XComWeapon', , 'DroppedWeapon', vLoc, rRot, Actor(kXComWeapon.ObjectArchetype));                
        DroppedWeaponMesh = SkeletalMeshComponent(DroppedWeapon.Mesh);
        DroppedWeapon.CollisionComponent = DroppedWeaponMesh;       
        DroppedWeapon.SetPhysics(PHYS_RigidBody);       
        DroppedWeapon.SetVisible(true);

        DroppedWeaponMesh.PhysicsWeight = 1.0f;
        DroppedWeaponMesh.ForceSkelUpdate();
        DroppedWeaponMesh.UpdateRBBonesFromSpaceBases(TRUE, TRUE);
        DroppedWeaponMesh.bSyncActorLocationToRootRigidBody = true;
        DroppedWeaponMesh.WakeRigidBody();      
        DroppedWeapon.SetLocation(vLoc);
        DroppedWeapon.SetRotation(rRot);
        DroppedWeaponMesh.SetRBPosition(vLoc);
        DroppedWeaponMesh.SetRBRotation(rRot);
        DroppedWeaponMesh.SetRBLinearVelocity(vect(0, 0, 0), false);
        DroppedWeaponMesh.SetRBAngularVelocity(vect(2, 0, 0), false);

        DroppedWeapon.AddToSlomoGroup();

    }
}