Raised This Month: $ Target: $400
 0% 

Boolean delay (wait 2 seconds before false in case it gets true again)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fatal_nl
Member
Join Date: Apr 2017
Old 08-07-2018 , 00:21   Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #1

The code below is to block voice from dead players. Admins can use the bind +adminvoice which is handled by another plugin (amx_super-serious.axx). Dead players are getting a HUD message and when admins using +adminvoice to talk to other admins they shouldn't get that message. Everything works fine theoretically, but when a admin repeatedly press the key bind to +adminvoice, they eventually get the HUD message.

Is there a way to code something that set a delay for g_bAdminVoice? Like: if it get true with +adminvoice, wait 2 seconds before it get false with -adminvoice only in case it get's true again.

Edit: I can probably fixing this issue with set_task. Some help or other suggestions are still appreciated.

The code:

PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <reapi>

#define PLUGIN "No Dead Mic ReAPI"
#define AUTHOR "fatal"
#define VERSION "0.2"

#define MAX_PLAYERS    32

new bool:g_bAdmin[MAX_PLAYERS+1]
new 
bool:g_bEnforcer[MAX_PLAYERS+1]
new 
bool:g_bAdminVoice[MAX_PLAYERS+1]

public 
plugin_init()
{
    
register_plugin(PLUGIN,VERSION,AUTHOR)
    
    
register_forward(FM_Voice_SetClientListening,"VoiceSetListening")
    
register_clcmd("+adminvoice","AdminVoiceOn")
    
register_clcmd("-adminvoice","AdminVoiceOff")
    
    
set_cvar_num("sv_alltalk",1)
}

public 
client_authorized(id)
{
    
g_bAdmin[id] = bool:(get_user_flags(id) & ADMIN_LEVEL_G)
    
g_bEnforcer[id] = bool:(get_user_flags(id) & ADMIN_IMMUNITY)
}

public 
client_putinserver(id)
{
    
g_bAdminVoice[id] = false
}

public 
AdminVoiceOn(id)
{
    if( !
g_bAdmin[id] )
    {
        return 
PLUGIN_CONTINUE
    
}

    
g_bAdminVoice[id] = true
    client_print
(0print_chat"ON"// debug
    
    
return PLUGIN_CONTINUE
}

public 
AdminVoiceOff(id)
{
    if(!
g_bAdminVoice[id])
        return 
PLUGIN_CONTINUE

    g_bAdminVoice
[id] = false
    client_print
(0print_chat"OFF"// debug
    
    
return PLUGIN_CONTINUE
}

public 
VoiceSetListening(iReceiver,iSender)
{
    if(
is_user_alive(iSender) || g_bEnforcer[iSender]) return 1

    engfunc
(EngFunc_SetClientListening,iReceiver,iSender0)

    return 
4;
}

public 
VTC_OnClientStartSpeak(const index)
{
    if(!
is_user_alive(index) && !g_bAdminVoice[index] && !g_bEnforcer[index])
    {
        
set_hudmessage(0,255,0,-1.0,0.35,1,6.0,4.0,0.5,0.15,-1)
        
show_hudmessage(index,"Dead people can't talk on mic.")
        
client_cmd(index,"spk fvox/blip.wav"
    }


Last edited by fatal_nl; 08-07-2018 at 00:42.
fatal_nl is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 08-07-2018 , 13:52   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #2

Look for examples of get_systime usage. You can also block that command if admin is spamming, sorta cooldown.
__________________
Relaxing is offline
fatal_nl
Member
Join Date: Apr 2017
Old 08-07-2018 , 19:14   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #3

Thanks for the suggestion Relaxing.

I tried this below. Problem is if someone spams the key bound to +adminvoice longer than 2 seconds, the issue is still there. As soon g_bAdminVoice get false and true again in VTC_OnClientStartSpeak it gives the HUD message. For practical reasons (end users) I can't make it longer than 2 seconds. There must be another way to solve this. I am a coding rookie. I'm more of an electrician that compare this issue with a light bulb with a momentary switch connected. To solve the light flickering when someone repeatedly pressing the switch I would use a capacitor big enough for steady light.

PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <reapi>

#define PLUGIN "No Dead Mic ReAPI"
#define AUTHOR "fatal"
#define VERSION "0.2.1

#define MAX_PLAYERS 32

new bool:g_bAdmin[MAX_PLAYERS+1]
new 
bool:g_bEnforcer[MAX_PLAYERS+1]
new 
bool:g_bAdminVoice[MAX_PLAYERS+1]
new 
g_AdminVoiceDelay[MAX_PLAYERS+1]

public 
plugin_init()
{
    
register_plugin(PLUGIN,VERSION,AUTHOR)
    
    
register_forward(FM_Voice_SetClientListening,"VoiceSetListening")
    
register_clcmd("+adminvoice","AdminVoiceOn")
    
register_clcmd("-adminvoice","AdminVoiceOff")
    
    
set_cvar_num("sv_alltalk",1)
}

public 
client_authorized(id)
{
    
g_bAdmin[id] = bool:(get_user_flags(id) & ADMIN_LEVEL_G)
    
g_bEnforcer[id] = bool:(get_user_flags(id) & ADMIN_IMMUNITY)
}

public 
client_putinserver(id)
{
    
g_bAdminVoice[id] = false
}

public 
AdminVoiceOn(id)
{
    if( !
g_bAdmin[id] || is_user_alive(id) )
    {
        return 
PLUGIN_CONTINUE
    
}
    
    if(
get_systime() - g_AdminVoiceDelay[id] < 2)
    {
        
client_print(idprint_chat"[ADMIN VOCOM]: Please stop spamming the mic key.")
         
        return 
PLUGIN_HANDLED
    
}
    
    
g_AdminVoiceDelay[id] = get_systime()
    
g_bAdminVoice[id] = true
    
    
return PLUGIN_CONTINUE
}

public 
AdminVoiceOff(id)
{
    if(!
g_bAdminVoice[id])
        return 
PLUGIN_CONTINUE

    g_bAdminVoice
[id] = false
    
    
return PLUGIN_CONTINUE
}

public 
VoiceSetListening(iReceiver,iSender)
{
    if(
is_user_alive(iSender) || g_bEnforcer[iSender]) return 1

    engfunc
(EngFunc_SetClientListening,iReceiver,iSender0)

    return 
4;
}

public 
VTC_OnClientStartSpeak(const index)
{
    if(!
is_user_alive(index) && !g_bAdminVoice[index] && !g_bEnforcer[index])
    {
        
set_hudmessage(0,255,0,-1.0,0.35,1,6.0,4.0,0.5,0.15,-1)
        
show_hudmessage(index,"Dead people can't talk on mic.")
        
client_cmd(index,"spk fvox/blip.wav"
    }


Last edited by fatal_nl; 08-07-2018 at 19:18.
fatal_nl is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 08-07-2018 , 20:02   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #4

So you want admins to use a custom command for 2 seconds only, and regain that ability to do it again after 2 more seconds, right? .-.
__________________
Relaxing is offline
fatal_nl
Member
Join Date: Apr 2017
Old 08-07-2018 , 21:08   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #5

I think this plugin needs some explanation first:

The plugin sets all talk on. FM_Voice_SetClientListening handles +voicerecord and -voicerecord. Only alive players can talk. Dead players can only listen (to prevent ghosting). Dead players are getting a HUD message when they try to talk with the VTC_OnClientStartSpeak function (that doesn't need a forward).

Admins can talk to each other with +adminvoice bind to a key. This is part of amx_super-serious.amxx. Luckily the FM_Voice_SetClientListening function doesn't interfere with this. Admins are still able to talk to each other while dead. However the VTC_OnClientStartSpeak function also get triggered when +adminvoice is used (they can still talk as it is supposed).

To hide the HUD message I added to +adminvoice clcmd to a function to set it true and clcmd -adminvoice to another command to make it false so I was able to determine when or when not printing the HUD message. Only problem is: as soon it get false (accidentally release mic key) and instantly true again (or after the 2 seconds in the code above) it prints the HUD message. Almost all admins have the habit to do that..

I'm using PLUGIN_CONTINUE instead of PLUGIN_HANDLED for the parts + or -adminvoice needs to re-used in amx_super-serious.amxx.

Everything would be more easy if I was able to catch the +voicerecord event (set true and false for +adminvoice) but as far as I know that's not possible without using the Orpheu module. I am using reAPI/ReHLDS* for other plugins as well so I'm unable to install that module.

* This is maybe a red flag for moderators but not everyone is using Reunion.

Edit:

Is this possible: keep g_bAdminVoice in function AdminVoiceOff true for 5 seconds before setting to false. If it becomes true again withing these 5 seconds in AdminVoiceOn reset this timer and don't let it become false until the full 5 seconds expire.

Last edited by fatal_nl; 08-07-2018 at 21:58.
fatal_nl is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 08-08-2018 , 09:42   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #6

If you're capable you can merge both plugins into a single and give it a try.
__________________
Relaxing is offline
fatal_nl
Member
Join Date: Apr 2017
Old 08-08-2018 , 17:24   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #7

I was able to solve it with set_task:

PHP Code:
public AdminVoiceOn(id)
{
    if( !
g_bAdmin[id] || is_user_alive(id) )
    {
        return 
PLUGIN_CONTINUE
    
}
    
    
g_bAdminVoice[id] = true
    
    
return PLUGIN_CONTINUE
}

public 
AdminVoiceOff(id)
{
    if(!
g_bAdminVoice[id])
        return 
PLUGIN_CONTINUE

    
    
if(!task_exists(id))
    {
        
set_task(2.0"AdminVoiceOffDelay"id)
    }
    else
    {    
        
remove_task(id)
        
set_task(2.0"AdminVoiceOffDelay"id)
    }
    
    return 
PLUGIN_CONTINUE
}

public 
AdminVoiceOffDelay(id)
{
    
g_bAdminVoice[id] = false
    remove_task
(id)
}

public 
client_disconnect(id)

    if(
task_exists(id))
    { 
        
remove_task(id)
    } 


Last edited by fatal_nl; 08-08-2018 at 18:21.
fatal_nl is offline
fatal_nl
Member
Join Date: Apr 2017
Old 08-08-2018 , 19:25   Re: Boolean delay (wait 2 seconds before false in case it gets true again)
Reply With Quote #8

Where in the code is g_bAdmin getting filled with a 6 digit number (262144) instead of 0 or 1 just as in g_bEnforcer or g_bAdminVoice?

Edit:

I removed the code. Solved: it was the admin level constant..

Last edited by fatal_nl; 08-08-2018 at 19:28.
fatal_nl is offline
Reply



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 12:44.


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