Raised This Month: $318 Target: $400
 79%   Almost there!

[CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 08-30-2010 , 12:51   [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #1

I made a grenade launcher with a revolver-type drum that holds 6 grenades. I'm having trouble with the reloading though. I successfully made it to check how many grenades are left in the drum when the reload starts and play the anim for putting a grenade in the drum as many times as needed, and block the attack until the reloading finishes. However, I'm having trouble when I tried to make it stop the reloading prematurely with the attack button and only keep as many grenades in the drum as have already been put in when I stop it.

Here are some parts of the code that are connected to the reloading process:

PHP Code:
//Time needed for inserting one grenade
#define RELOAD_TIME 1.9

new Float:gtime[33];
new 
Float:refilltime[33] = 0.0;
new 
g_refilledAmmo[33] = 0;

//Called when the primary attack button is pressed
public ev_attack1(id) {
    
//Check if the reloading has finished
    
if (gtime[id] < (get_gametime() - refilltime[id]))
    {
        
wpn_playanim(id,anim_throw );
        
emit_sound(idCHAN_WEAPONs_FIRE_11.0ATTN_NORM0PITCH_NORM);
        if(
g_UserRocket[id][rocket_entity] != -1) return PLUGIN_CONTINUE
        shoot_rocket
(id);
        
        
client_cmd(id"-attack");
    
        return 
PLUGIN_CONTINUE;
    }

    
//The reloading hasn't finished, stop it and calculate grenades in drum
    
else
    {
        
refilltime[id] = 0;

        new 
usrwpn wpn_has_weapon(id,g_wpnid);
        new 
newammo wpn_get_userinfo(id,usr_wpn_ammo1,usrwpn) + g_refilledAmmo[id] - 4;
        
wpn_set_userinfo(id,usr_wpn_ammo1,usrwpn,newammo);
        
        
client_cmd(id"-attack");
        
        return 
PLUGIN_CONTINUE;
    }
}

//Start reload process
public ev_reload(id) {
    
//Save the time the reload starts
    
gtime[id] = get_gametime();

    
g_refilledAmmo[id] = 0;

    
//Start inserting of one grenade
    
insert_gren(id);
    new 
usrwpn wpn_has_weapon(id,g_wpnid); 

    
//Check how many grenades left in drum and decide how many more to add
    
switch(wpn_get_userinfo(id,usr_wpn_ammo1,usrwpn))
    {
        
//Drum empty, insert 5 grenades in addition to the one before the switch()
        
case 0
        {
            
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",5);
            
            
//Calculate how long is the reload going to take
            
refilltime[id] = RELOAD_TIME*6
            return 
PLUGIN_CONTINUE;
        }
        
        
//1 grenade left in drum, insert 4 grenades in addition to the one before the switch()
        
case 1
        {
            
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",4);
            
refilltime[id] = RELOAD_TIME*5
            return 
PLUGIN_CONTINUE;
        }

        
//2 grenades left in drum
        
case 2
        {
            
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",3);
            
refilltime[id] = RELOAD_TIME*4
            return 
PLUGIN_CONTINUE;
        }
 
        
//3 grenades left in drum
        
case 3
        {
            
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",2);
            
refilltime[id] = RELOAD_TIME*3
            return 
PLUGIN_CONTINUE;
        }

        
//4 grenades left in drum
        
case 4
        {
            
set_task(RELOAD_TIME,"insert_gren",id,"",0,"a",1);
            
refilltime[id] = RELOAD_TIME*2
            return 
PLUGIN_CONTINUE;
        }
 
        
//only one grenade was shot, no grenades needed in addition to the first reloaded one, just calculate the reloading time
        
case 5:
        {
            
refilltime[id] = RELOAD_TIME;
            return 
PLUGIN_CONTINUE;
        }
    }
    
    return 
PLUGIN_CONTINUE;
}

//Insert one grenade in the drum
public insert_gren(id)
{
    
wpn_playanim(idanim_pullpin);
    
g_refilledAmmo[id]++; //Calculate how many grenades were inserted during the reload

So in case 0, when the gun has to reload 6 grenades, there is no problem, but in the other cases, if I stop the reloading, I get over 20 grenades in the clip.
I added one line client_print(id, print_center,"reloaded ammo: %d", g_refilledAmmo[id]); at the end of the insert_gren function and found out that when I'm reloading all 6 grenades, g_refilledAmmo prints 1, 2, 3, 4, 5 just as it should, when I reload 5 or less, it prints something like 1, 11, 23, 35 and so on, I don't remember the exact numbers but you get the idea.
I tried a lot of different things but was unable to fix this. So, I really need your help!
__________________

Last edited by SeriousSam; 08-30-2010 at 12:59.
SeriousSam is offline
Send a message via Skype™ to SeriousSam
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 09-02-2010 , 14:09   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #2

*bump : It's been a few days already, does really no one even have a suggestion?
__________________
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-02-2010 , 15:55   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #3

See the HLSDK to see the shotgun weapon and how the reload is handled.

I'm making also a weapon which is the HL1 shotgun. I can share some code if you need.
__________________

Last edited by Arkshine; 09-02-2010 at 17:31.
Arkshine is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 09-05-2010 , 14:28   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #4

I took a look at both m3 and m4 shotguns in the cs sdk, but found nothing that can help here, as they use a special reload event, and I'm trying to workaround the standart reload event. Anyways, have you checked out the algorythm? A few friends, c++ prigrammers, took a look at it and told me it should work just fine, yet it doesn't
__________________
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-05-2010 , 14:53   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #5

sorry I don't feel like to check because your code is really ugly :p, and should be redone from scratch. The code you can see in the HLSDK or CSSDK tell you a way to do properly a shotgun-reload style. I've done like that on my own plugin and it works nicely.
__________________
Arkshine is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 09-05-2010 , 16:25   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #6

Anyways, it is really strange why, for example 2 grenades left in drum, execute case 2, I checked, the code in case 2 is executed only once as it should, and that code contains a set_task that should activate the insert_gren function 3 times in 1.9 sec intervals, yet when I check inside the insert_gren function it actually runs about 10 times each time it's called - as if the set_task function is malfunctioning.
BTW can you please share some code from your working shotgun?

EDIT: why the hell all the [ and ] brackets in the code I have pasted in the first post are replaced with ***91; and ***93; !?
__________________

Last edited by SeriousSam; 09-05-2010 at 16:27.
SeriousSam is offline
Send a message via Skype™ to SeriousSam
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 09-14-2010 , 16:26   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #7

I think it's been long enough to bump, I've tried everything I can and I can't get it working, hey Arkshine, please share your code you were talking about?
__________________
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-14-2010 , 16:44   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #8

I can't show the whole code, but you will get the idea. Like I've said, it's exactly like the shotgun in the HLSDK :

PHP Code:

    register_forward
FM_CmdStart"Player_HookButtons" );
    

    public 
Player_HookButtons ( const player, const uc_handle, const randomSeed )
    {
        if ( 
is_user_aliveplayer ) )
        {
            new 
weapon GetWeaponIndexplayer );

            if ( !
GetEntityBitisScientistShotgunweapon ) )
            {
                return 
FMRES_IGNORED;
            }

            new 
Float:currentTime get_gametime();
            new 
Float:nextAttack = -1.0;

            new 
buttons get_ucuc_handleUC_Buttons );
            new 
attack  EVENT_NONE;

            if      ( 
buttons IN_ATTACK  )  { attack EVENT_ATTACK1nextAttack nextPrimaryAttack  player ]; }
            else if ( 
buttons IN_ATTACK2 )  { attack EVENT_ATTACK2nextAttack nextSecondaryAttackplayer ]; }
            else if ( 
buttons IN_RELOAD  )  { attack EVENT_RELOAD; }

            
set_ucuc_handleUC_Buttonsbuttons & ~( IN_ATTACK IN_ATTACK2 IN_RELOAD ) );

            if ( 
attack != EVENT_NONE )
            {
                if ( 
nextAttack currentTime || IsFreezePeriod() )
                {
                    return 
FMRES_HANDLED;
                }

                switch ( 
attack )
                {
                    case 
EVENT_ATTACK1 Shotgun_PrimaryAttackweaponplayer );
                    case 
EVENT_ATTACK2 Shotgun_SecondaryAttackweaponplayer );
                    case 
EVENT_RELOAD  Shotgun_Reloadweaponplayer );
                }
            }
            else
            {
                
Shotgun_WeaponIdleweaponplayer );
            }

            return 
FMRES_HANDLED;
        }

        return 
FMRES_IGNORED;
    }

    
Shotgun_PrimaryAttack( const weapon, const player )
    {
        if( !
Shotgun_CanFireUnderwaterweaponplayer ) )
        {
            return;
        }

        if( 
Shotgun_ShouldReloadattackweaponplayer ) )
        {
            return;
        }
        
        
UpdateTool
        
(
            .
weaponIndex   weapon,
            .
playerIndex   player,
            .
updateAmmoHud true,
            .
clip          = -bulletPerShot,
            .
clipCache     weaponClipplayer ]
        );

        
SendWeaponAnimweaponshootAnimation );
        
SetAnimationplayerPLAYER_ATTACK1 );

        
set_pevplayerpev_effectspevplayerpev_effects ) | EF_MUZZLEFLASH );
        
        
// [...]
        
        
new Float:currentTime get_gametime();

        
nextPrimaryAttack  player ] = currentTime 0.75;
        
nextSecondaryAttackplayer ] = currentTime 0.75;

        if ( 
weaponClipplayer ] )
        {
            
pumpTime      player ] = currentTime 0.85;
            
timeWeaponIdleplayer ] = currentTime 5.0;
        }
        else
        {
            
timeWeaponIdleplayer ] = currentTime 0.75;
        }

        
inSpecialReloadplayer } = START_RELOAD;
    }

    
Shotgun_Reload( const weapon, const player )
    {
        new 
clip   weaponClip  player ];
        new 
bpAmmo weaponBpAmmoplayer ];

        new 
Float:currentTime get_gametime();

        
/*
            | Don't go further if no more bpammo or if clip are full or wa can't attack.

            | Start reload : Prepare to reload.
            | Reload       : We are reloading. Ammo by ammo. Loop until clip are full.
            | End Reload   : We can update the clip/BpAmmo and the Hud too.
        */

        
if ( bpAmmo <= || clip == get_pcvar_numcvarPointerClip ) || nextPrimaryAttackplayer ] > currentTime )
        {
            return 
clip;
        }

        new 
specialReload inSpecialReloadplayer };

        switch( 
specialReload )
        {
            case 
START_RELOAD :
            {
                
SendWeaponAnimweaponSHOTGUN_START_RELOAD );

                
specialReload RELOAD;

                
timeWeaponIdle     player ] = currentTime 0.6;
                
nextPrimaryAttack  player ] = currentTime 1.0;
                
nextSecondaryAttackplayer ] = currentTime 1.0;
            }
            case 
RELOAD :
            {
                if ( 
timeWeaponIdleplayer ] > currentTime )
                {
                    return 
clip;
                }

                
specialReload END_RELOAD;

                
emit_soundplayerCHAN_ITEMrandom_num0) ? shotgunReload1Sound shotgunReload2SoundVOL_NORMATTN_NORM, .pitch 85 random_num00x1f ) );
                
SendWeaponAnimweaponSHOTGUN_RELOAD );

                
timeWeaponIdleplayer ] = currentTime 0.5;
            }
            case 
END_RELOAD :
            {
                
UpdateTool
                
(
                    .
playerIndex   player,
                    .
weaponIndex   weapon,
                    .
updateAmmoHud true,
                    .
clip          =  1,
                    .
bpAmmo        = -1,
                    .
clipCache     weaponClipplayer ],
                    .
bpAmmoCache   weaponBpAmmoplayer ]
                );

                
specialReload RELOAD;
            }
        }

        
inSpecialReloadplayer } = specialReload;

        return 
clip;
    }
    
    
bool:Shotgun_ShouldReload( const attack, const weapon, const player )
    {
        switch( 
attack )
        {
            case 
PRIMARY_ATTACK :
            {
                if( 
weaponClipplayer ] <= )
                {
                    if( !
Shotgun_Reloadweaponplayer ) )
                    {
                        
PlayEmptySoundweapon );
                    }

                    return 
false;
                }
            }
            case 
SECONDARY_ATTACK :
            {
                if ( 
weaponClipplayer ] <= )
                {
                    
Shotgun_Reloadweaponplayer );
                    
PlayEmptySoundweapon );

                    return 
false;
                }
            }
        }

        return 
true;
    }

    
bool:Shotgun_CanFireUnderwater( const weapon, const player )
    {
        const 
HEAD_UNDER_WATER 3;

        if ( 
pevplayerpev_waterlevel ) == HEAD_UNDER_WATER )
        {
            
PlayEmptySoundweapon );
            
nextPrimaryAttackplayer ] = get_gametime() + 0.15;

            return 
false;
        }

        return 
true;
    }

    public 
Shotgun_WeaponIdle( const weapon, const player )
    {
        new 
Float:currentTime     get_gametime();
        new 
Float:currentPumpTime pumpTimeplayer ];

        
// UTIL_ResetEmptySound( player );

        
if ( currentPumpTime && currentPumpTime currentTime )
        {
            
emit_soundweaponCHAN_ITEMshotgunPumpSoundVOL_NORMATTN_NORM, .pitch 95 random_num00x1f ) );
            
pumpTimeplayer ] = 0.0;
        }

        if ( 
timeWeaponIdleplayer ] < currentTime )
        {
            if ( !
weaponClipplayer ] && !inSpecialReloadplayer } && weaponBpAmmoplayer] )
            {
                
Shotgun_Reloadweaponplayer );
            }
            else if ( 
inSpecialReloadplayer } != START_RELOAD )
            {
                if ( 
weaponClipplayer ] != get_pcvar_numcvarPointerClip ) && weaponBpAmmoplayer] )
                {
                    
Shotgun_Reloadweaponplayer );
                }
                else
                {
                    
SendWeaponAnimweaponSHOTGUN_PUMP );
                    
emit_soundweaponCHAN_ITEMshotgunPumpSoundVOL_NORMATTN_NORM, .pitch 95 random_num00x1f ) );

                    
inSpecialReloadplayer } = START_RELOAD;
                    
timeWeaponIdleplayer ] = currentTime 1.5;
                }
            }
            else
            {
                new 
animation;
                new 
Float:rand random_float0.01.0 );

                if ( 
rand <= 0.8 )
                {
                    
animation SHOTGUN_IDLE_DEEP;
                    
timeWeaponIdleplayer ] = currentTime + ( 60.0 12.0 );
                }
                else if ( 
rand <= 0.95 )
                {
                    
animation SHOTGUN_IDLE;
                    
timeWeaponIdleplayer ] = currentTime + ( 20.0 9.0 );
                }
                else
                {
                    
animation SHOTGUN_IDLE4;
                    
timeWeaponIdleplayer ] = currentTime + ( 20.0 9.0 );
                }

                
SendWeaponAnimweaponanimation );
            }
        }
    } 
__________________
Arkshine is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 09-15-2010 , 12:03   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #9

Kind of what I tried, I still don't get why my code doesn't work. Yet, I can't use this in the weapon reload function in the weapon plugin. I guess I'll have to edit the reload native itself in weaponmod. I'll try now
__________________
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-15-2010 , 12:50   Re: [CS 1.6] Grenade Launcher, shotgun-style reloading - HELP !!!
Reply With Quote #10

Start to rewrite it without these ugly tasks.
__________________
Arkshine is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:31.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode