View Single Post
ElooKoN
        ^_^
Join Date: Apr 2011
Old 08-22-2020 , 09:27   Re: Weird bug in Counter-Strike: Source – Night vision gives ammo
Reply With Quote #8

Thanks so much!

I was also writing a SourceMod patch yesterday, which looks almost the same (but using the old syntax). Your solution hooking item_pickup may be more reliable though, but you would want to use EntIndexToEntRef() for the entity IDs I think just to be safe:

PHP Code:
public Action:CS_OnBuyCommand(clientID, const String:weapon[]) {
    
    if (
IsValidClient(clientID) && IsPlayerAlive(clientID) && !IsFakeClient(clientID)) {
        
        if (
StrEqual(weapon"vest") || StrEqual(weapon"vesthelm") || StrEqual(weapon"nvgs")) {
            
            
/**
            *    Find out current ammo and clip size of player
            */
            
new PrimaryAmmo = -1;
            new 
PrimaryClipSize = -1;
            new 
SecondaryAmmo = -1;
            new 
SecondaryClipSize = -1;
            
            new 
PrimaryWeaponEntity GetPlayerWeaponSlot(clientID0);
            new 
PrimaryWeaponEntityRef = -1;
            
            new 
SecondaryWeaponEntity GetPlayerWeaponSlot(clientID1);
            new 
SecondaryWeaponEntityRef = -1;
            
            if (
PrimaryWeaponEntity > -1) {
                
PrimaryWeaponEntityRef EntIndexToEntRef(PrimaryWeaponEntity);
                
PrimaryAmmo GetAmmo(clientID0);
                
PrimaryClipSize GetClipSize(PrimaryWeaponEntity);
            }
            
            if (
SecondaryWeaponEntity > -1) {
                
SecondaryWeaponEntityRef EntIndexToEntRef(SecondaryWeaponEntity);
                
SecondaryAmmo GetAmmo(clientID1);
                
SecondaryClipSize GetClipSize(SecondaryWeaponEntity);
            }
            
            
/**
            *    Cache information
            */
            
new Handle:handle CreateDataPack();
            
            
WritePackCell(handleGetClientUserId(clientID));
            
WritePackCell(handlePrimaryWeaponEntityRef);
            
WritePackCell(handlePrimaryAmmo);
            
WritePackCell(handlePrimaryClipSize);
            
WritePackCell(handleSecondaryWeaponEntityRef);
            
WritePackCell(handleSecondaryAmmo);
            
WritePackCell(handleSecondaryClipSize);

            
/**
            *    Restore in the next frame, otherwise this will have no effect
            */
            
RequestFrame(GameBugFix_RestoreClientAmmoAndClipSizehandle);
            
        }
        
    }
    
}

public 
GameBugFix_RestoreClientAmmoAndClipSize(Handle:handle) {
    
    
ResetPack(handle);
    
    new 
clientID GetClientOfUserId(ReadPackCell(handle));    
    new 
PrimaryWeaponEntity ReadPackCell(handle);
    new 
PrimaryAmmo ReadPackCell(handle);
    new 
PrimaryClipSize ReadPackCell(handle);
    new 
SecondaryWeaponEntity ReadPackCell(handle);
    new 
SecondaryAmmo ReadPackCell(handle);
    new 
SecondaryClipSize ReadPackCell(handle);

    if (
IsValidClient(clientID) && IsPlayerAlive(clientID)) {
        
        
/**
        *    Primary
        */
        
if (PrimaryWeaponEntity != -1) {
            
PrimaryWeaponEntity EntRefToEntIndex(PrimaryWeaponEntity);
            
            if (
IsValidEntity(PrimaryWeaponEntity)) {
                
SetPlayerAmmoForWeaponEntity(clientIDPrimaryWeaponEntityPrimaryAmmo);
                
SetClipSize(PrimaryWeaponEntityPrimaryClipSize);
            }
        }
    
        
/**
        *    Secondary
        */    
        
if (SecondaryWeaponEntity != -1) {
            
SecondaryWeaponEntity EntRefToEntIndex(SecondaryWeaponEntity);
    
            if (
IsValidEntity(SecondaryWeaponEntity)) {
                
SetPlayerAmmoForWeaponEntity(clientIDSecondaryWeaponEntitySecondaryAmmo);
                
SetClipSize(SecondaryWeaponEntitySecondaryClipSize);
            }
        }
        
    }
    
    
CloseHandle(handle);
    
}

stock bool:IsValidClient(const clientID) {

    if (!
IsValidClientID(clientID)) {
        return 
false;
    }
    
    if (!
IsClientInGame(clientID)) {
        return 
false;
    }

    return 
true;
    
}

stock GetAmmo(const clientID, const slot) {
    new 
weapon GetPlayerWeaponSlot(clientIDslot);
    
    if (
IsValidEntity(weapon)) {
        new 
offset GetEntProp(weaponProp_Send"m_iPrimaryAmmoType"1) * 4;
        return 
GetEntData(clientIDOffset_m_iAmmo offset);
    }
    
    return -
1;
}

stock SetPlayerAmmoForWeaponEntity(const clientID, const entity, const amount) {
    new 
offset GetEntProp(entityProp_Send"m_iPrimaryAmmoType"1) * 4;
    
    
SetEntData(clientIDOffset_m_iAmmo offsetamount4true);
}

stock GetClipSize(const entity) {
    return 
GetEntProp(entityProp_Send"m_iClip1");
}

stock SetClipSize(const entity, const amount) {
    
SetEntProp(entityProp_Send"m_iClip1"amount);

ElooKoN is offline