AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   is_user_bot check problem (https://forums.alliedmods.net/showthread.php?t=337210)

NEUCOUNTRA 04-08-2022 07:39

is_user_bot check problem
 
I made plugin that gives possibility to heal and armor by executing a command. I wanted to import this function to bot, but don't get any respond and health/armor doesn't change. The goal - heal if health lower then maxhealth variable (maxhealth = 100) and armor if it equals 0. How it can be solved?

Code:


#include <amxmodx>
#include <fun>

new maxhealth = 100;

public plugin_init()
{
        register_plugin("health", "CNC", "0.1")
        register_clcmd("say /health", "give_health")
        register_clcmd("health", "give_health")
        register_clcmd("say /armor", "give_armor")
}

public give_health(id)
{
        if(is_user_alive(id))
    {
        client_print(id, print_chat, "Health function called successfully")
        set_user_health(id, get_user_health(id) + 100)
        client_cmd(id, "spk items/medshot4")
    }
    else
    {
        client_print(id, print_chat, "You are dead!")
    }
}

public give_armor(id)
{
    if(is_user_alive(id))
    {
        client_print(id, print_chat, "Armor gain")
        set_user_armor(id, get_user_armor(id) + 100)
        client_cmd(id, "spk items/ammopickup1")
    }
    else
    {
        client_print(id, print_chat, "You are dead!")
    }
}

public bot_check(id)
{
    if(is_user_bot(id))
    {
        console_print(id, "bot")
    }
   
    if(is_user_bot(id) && get_user_health(id) < maxhealth)
    {
        give_health(id);
        client_print(id, print_chat, "Bot healed")
    }
   
    if(is_user_bot(id) && get_user_armor(id) == 0)
    {
        give_armor(id);
        client_print(id, print_chat, "Bot armored")
    }
}


lexzor 04-08-2022 08:15

Re: is_user_bot check problem
 
try: do a check if player is bot or not. if it is use engine and
PHP Code:

 entity_set_intiEntEV_FL_healthvalue 


NEUCOUNTRA 04-08-2022 08:23

Re: is_user_bot check problem
 
Quote:

Originally Posted by lexzor (Post 2776122)
try: do a check if player is bot or not. if it is use engine and
PHP Code:

 entity_set_intiEntEV_FL_healthvalue 


I have already checked or I don't understand something?

Code:


if(is_user_bot(id) && get_user_health(id) < maxhealth)
    {
        give_health(id);
        client_print(id, print_chat, "Bot healed")
    }

Where I should place that?

Code:

entity_set_int( iEnt, EV_FL_health, value )
Sorry, I'm new in pawn :<

Moody92 04-08-2022 09:28

Re: is_user_bot check problem
 
Quote:

Originally Posted by NEUCOUNTRA (Post 2776123)
I have already checked or I don't understand something?

Code:


if(is_user_bot(id) && get_user_health(id) < maxhealth)
    {
        give_health(id);
        client_print(id, print_chat, "Bot healed")
    }

Where I should place that?

Code:

entity_set_int( iEnt, EV_FL_health, value )
Sorry, I'm new in pawn :<

Your bot function is not being called anywhere. You need to trigger it somehow, just like the commands you have for the non-bot users.

PHP Code:

public bot_check(id)
{
    if(
is_user_bot(id))
    {
        
console_print(id"bot")
    }
    
    if(
is_user_bot(id) && get_user_health(id) < maxhealth)
    {
        
give_health(id);
        
client_print(idprint_chat"Bot healed")
    }
    
    if(
is_user_bot(id) && get_user_armor(id) == 0)
    {
        
give_armor(id);
        
client_print(idprint_chat"Bot armored")
    } 

EDIT: You probably also need to modify the function, usually bots are unable to call a function by themselves so you probably need to loop through the bots / or find the id of the bot somehow if a player is healing them.

NEUCOUNTRA 04-09-2022 04:08

Re: is_user_bot check problem
 
Quote:

Originally Posted by Moody92 (Post 2776129)
Your bot function is not being called anywhere. You need to trigger it somehow, just like the commands you have for the non-bot users.

PHP Code:

public bot_check(id)
{
    if(
is_user_bot(id))
    {
        
console_print(id"bot")
    }
    
    if(
is_user_bot(id) && get_user_health(id) < maxhealth)
    {
        
give_health(id);
        
client_print(idprint_chat"Bot healed")
    }
    
    if(
is_user_bot(id) && get_user_armor(id) == 0)
    {
        
give_armor(id);
        
client_print(idprint_chat"Bot armored")
    } 

EDIT: You probably also need to modify the function, usually bots are unable to call a function by themselves so you probably need to loop through the bots / or find the id of the bot somehow if a player is healing them.


How it might look, yesterday I tried to create a check, if player is bot and then I called function, but it didn't work. How I can realise a trigger?

Also I tried to use set_task on client_putinserver(), it didn't work too.

kww 04-09-2022 06:02

Re: is_user_bot check problem
 
The worst way I can suggest is to use preThink.
Make something like random command execution.
Code below will check if player is a bot. If it is a bot then generate random number from 1 to 100 and if number is in range of USAGE_CHANCE then execute your function
PHP Code:

// IMHO USAGE_CHANCE should be pretty small
#define USAGE_CHANCE 1..5

new bool:g_bBot[MAX_PLAYERS+1]

// when client connects to server
public client_putinserver(id)
{
    
// cache bots to probably decrease CPU usage
    
g_bBot[id] = bool:is_user_bot(id)
}

public 
client_PreThink(id)
{
    if(
g_bBot[id])
    {
        switch(
random_num(1100))
        {
            case 
USAGE_CHANCE:
            {
                
// run heal function
                
give_health(id)
            }
        }
    }



Moody92 04-09-2022 07:19

Re: is_user_bot check problem
 
Quote:

Originally Posted by kww (Post 2776198)
The worst way I can suggest is to use preThink.
Make something like random command execution.
Code below will check if player is a bot. If it is a bot then generate random number from 1 to 100 and if number is in range of USAGE_CHANCE then execute your function
PHP Code:

// IMHO USAGE_CHANCE should be pretty small
#define USAGE_CHANCE 1..5

new bool:g_bBot[MAX_PLAYERS+1]

// when client connects to server
public client_putinserver(id)
{
    
// cache bots to probably decrease CPU usage
    
g_bBot[id] = bool:is_user_bot(id)
}

public 
client_PreThink(id)
{
    if(
g_bBot[id])
    {
        switch(
random_num(1100))
        {
            case 
USAGE_CHANCE:
            {
                
// run heal function
                
give_health(id)
            }
        }
    }



I do not think running a prethink is a good idea, you will be spamming the server. I copy pasta'd some of your code, but the way I put it is starting a task every round that loops thru every bot and if they get lucky (chance is 1 in 100), then they get geared up. Not sure if this is the best idea either.

Not tested.

PHP Code:

#include <amxmodx>
#include <fun>

// IMHO USAGE_CHANCE should be pretty small
#define USAGE_CHANCE 5

new maxhealth 100;
new 
maxarmor 100;
new 
Float:frequency 5.0 // 5 seconds frequency, you can change this

public plugin_init()
{
    
register_plugin("health""CNC""0.1")
    
register_clcmd("say /health""give_health")
    
register_clcmd("health""give_health")
    
register_clcmd("say /armor""give_armor")
    
    
register_logevent("logevent_round_start"2"1=Round_Start"
    
register_logevent("logevent_round_end"2"1=Round_End"
}

public 
logevent_round_start(){
    
set_task(frequency"bot_check"440033__"b")
}

public 
logevent_round_end(){
    
remove_task(440033)
    
}
public 
give_health(id)
{
    new 
pHealth
    pHealth 
get_user_health(id//Obtain current user health
    
    //Check if user is alive
    
if(!is_user_alive(id))
    {
        
client_print(idprint_chat"You are dead!")
        return 
PLUGIN_HANDLED
    
}
    
    
//Check if user already has max hp, if yes then ignore the rest
    
if (pHealth == maxhealth)
    {
        
client_print(idprint_chat"You are already at full health.")
        return 
PLUGIN_HANDLED
    
}
    
    
client_print(idprint_chat"Health function called successfully")
    
set_user_health(idmaxhealth// Set to 100 -- get_user_health(id) + 100 this will set user health to above 100 if their health is 40 it will add 100 to that
    
client_cmd(id"spk items/medshot4")
    
    return 
PLUGIN_CONTINUE
}

public 
give_armor(id)
{
    new 
pArmor
    pArmor 
get_user_armor(id//Obtain current user health
    
    //Check if user is alive
    
if(!is_user_alive(id))
    {
        
client_print(idprint_chat"You are dead!")
        return 
PLUGIN_HANDLED
    
}
    
    
//Check if user already has max armor, if yes then ignore the rest
    
if (pArmor == maxarmor)
    {
        
client_print(idprint_chat"You are already at full armor.")
        return 
PLUGIN_HANDLED
    
}
    
    
client_print(idprint_chat"Armor gain")
    
set_user_armor(idmaxarmor)
    
client_cmd(id"spk items/ammopickup1")
    
    return 
PLUGIN_CONTINUE
}

public 
bot_check()
{
    new 
sBots[32], pNumszName[33]
    
get_players(sBotspNum"abdefghi"//only get bot players
    
    
    
for(new i=0pNumi++)
    {
        switch(
random_num(1100)) //You can change this, a random number will be picked from 1 to 100 and if it is the same number as USAGE_CHANCE then it will give armor and bot - this is run for each bot
        
{
            case 
USAGE_CHANCE:
            {
                if(
is_user_connected(sBots[i]))
                {
                    
give_health(sBots[i])
                    
give_armor(sBots[i])
                    
get_user_name(sBots[i], szNamecharsmax(szName)) 
                    
client_print(0print_chat"Bot %s has been fully healed and armored up!"szName// Prints to everyone
                
}
            }
            
        }
    }
    



DJEarthQuake 04-09-2022 12:25

Re: is_user_bot check problem
 
Think can be rough especially when multiple plugins are using it. A task with CVAR to adjust the timing.

Moody92 04-10-2022 04:07

Re: is_user_bot check problem
 
Quote:

Originally Posted by kww (Post 2776256)
As i wrote.


PHP Code:

// then it should be used on this manner
set_task(frequencybot_check440033, .flags[] = "b"


Corrected in first post, thanks.


All times are GMT -4. The time now is 06:38.

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