AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Why does this plugin makes the server crash? (https://forums.alliedmods.net/showthread.php?t=335773)

GlobalPlague 01-02-2022 14:26

Why does this plugin makes the server crash?
 
Hello. The following plugin makes it possible for bots to be forced to buy items from the humans' Extra Items menu:

PHP Code:

#include <amxmodx>
#include <zombie_plague_advance>


/*================================================================================
 [Plugin Customization]
=================================================================================*/

// Items name (note: add exact item name)

new const EXTRA_ITEMS[][] = {
    
"Plasma Gun",
    
"Golden Ak 47",
    
"Item3"
}

/*================================================================================
 Customization ends here! Yes, that's it. Editing anything beyond
 here is not officially supported. Proceed at your own risk...
=================================================================================*/

enum
{
    
TASK_STARTGIVE 298,
    
TASK_GIVEITEM
}

#define ID_BOT (taskid - TASK_GIVEITEM)

new cvar_max_botsg_botname[33][32], g_has_item[33], g_maxplayers;

public 
plugin_init()
{
    
/* Plugin register */
    
register_plugin("[ZP] Bot Addon: Force buy items""v0.2""Crazy");

    
/* Cvars */
    
cvar_max_bots register_cvar("zp_force_buy_maxbots""5");

    
/* Max players */
    
g_maxplayers get_maxplayers()
}

public 
client_putinserver(id)
{
    if (
is_user_bot(id))
        
get_user_name(idg_botname[id], charsmax(g_botname[]));
}

public 
zp_round_started(gamemodeid)
{
    for (new 
id 1id <= g_maxplayersid++)
        
g_has_item[id] = false;

    if (
gamemode != MODE_PLAGUE || MODE_SURVIVOR)
        
set_task(2.0"give_item_task"TASK_STARTGIVE);
}

public 
give_item_task(taskid)
{
    static 
idiBotsiMaxBots;
    
iBots 0;
    
iMaxBots get_pcvar_num(cvar_max_bots);

    while (
iBots <= iMaxBots)
    {
        
id get_random_bot(random_num(1get_alive_bots()));

        if (!
is_user_alive(id))
            continue;

        if (
zp_get_user_zombie(id))
            continue;

        if (
g_has_item[id])
            continue;

        
set_task(1.0"give_item"id+TASK_GIVEITEM);

        
iBots++;
    }

    
remove_task(TASK_STARTGIVE);
    
remove_task(TASK_GIVEITEM);
}

public 
give_item(taskid)
{
    static 
idrandomitemid;
    
id ID_BOT;
    
random random_num(0sizeof EXTRA_ITEMS 1);
    
itemid zp_get_extra_item_id(EXTRA_ITEMS[random]);

    if (
itemid == -1)
    {
        
set_task(0.5"give_item"id+TASK_GIVEITEM);
        return;
    }

    
zp_force_buy_extra_item(iditemid1);
    
g_has_item[id] = true;
    
remove_task(id+TASK_GIVEITEM);
}

get_alive_bots()
{
    static 
iBotid;
    
iBot 0;

    for (
id 1id <= g_maxplayersid++)
    {
        if (
is_user_alive(id) && is_user_bot(id))
            
iBot++;
    }

    return 
iBot;
}

get_random_bot(n)
{
    static 
iBotid;
    
iBot 0;

    for (
id 1id <= g_maxplayersid++)
    {
        if (
is_user_alive(id) && is_user_bot(id))
            
iBot++;

        if (
iBot == n)
            return 
id;
    }

    return -
1;


In the following code, you need to type the names of the extra items you want the bots to have access to:

Code:

// Items name (note: add exact item name)

new const EXTRA_ITEMS[][] = {
        "Plasma Gun",
        "Golden Ak 47",
        "Item3"
}

The problem is that the plugin works only during normal infection round, Nemesis mode, Assassin mode, Swarm mode, Multiple infection mode, Plague mode, Armageddon mode, but makes the server to crash during Survivor mode and Sniper mode. During Armageddon round and Plague round, there are survivors, too, but then the server doesn't crash. The server crash only when the game mode is mode where there is only one Sniper or only one Survivor, and all other players are zombies. Can someone fix this bug? I'm using ZPA 1.6.1.

If you are going to help me, don't forget to explain to me what code(s) exactly you edited, so i can learn for myself.

Thanks.

Natsheh 01-02-2022 16:09

Re: Why does this plugin makes the server crash?
 
Because there are some potential loopholes in your loops causing it to be stuck in an infinite loop.


And whats with your get_random_bot logerthem ? Same for get_alive_bots both of them are invalid!

Shadows Adi 01-02-2022 16:14

Re: Why does this plugin makes the server crash?
 
Skip the other game modes too + what natsheh said above.

Code:
public zp_round_started(gamemode, id) {     for (new id = 1; id <= g_maxplayers; id++)         g_has_item[id] = false;     if (gamemode != MODE_PLAGUE && gamemode != MODE_SURVIVOR && gamemode != MODE_SNIPER ) // etc...         set_task(2.0, "give_item_task", TASK_STARTGIVE); }

Code:
#include <amxmodx> #include <zombie_plague_advance> /*================================================================================  [Plugin Customization] =================================================================================*/ // Items name (note: add exact item name) new const EXTRA_ITEMS[][] = {     "Plasma Gun",     "Golden Ak 47",     "Item3" } /*================================================================================  Customization ends here! Yes, that's it. Editing anything beyond  here is not officially supported. Proceed at your own risk... =================================================================================*/ enum {     TASK_STARTGIVE = 298,     TASK_GIVEITEM } #define ID_BOT (taskid - TASK_GIVEITEM) new cvar_max_bots, g_botname[33][32], g_has_item[33], g_maxplayers; public plugin_init() {     /* Plugin register */     register_plugin("[ZP] Bot Addon: Force buy items", "v0.2", "Crazy");     /* Cvars */     cvar_max_bots = register_cvar("zp_force_buy_maxbots", "5");     /* Max players */     g_maxplayers = get_maxplayers() } public client_putinserver(id) {     if (is_user_bot(id))         get_user_name(id, g_botname[id], charsmax(g_botname[])); } public zp_round_started(gamemode, id) {     for (new id = 1; id <= g_maxplayers; id++)         g_has_item[id] = false;     if (gamemode != MODE_PLAGUE && gamemode != MODE_SURVIVOR && gamemode != MODE_SNIPER)         set_task(2.0, "give_item_task", TASK_STARTGIVE); } public give_item_task(taskid) {     static id, iBots, iMaxBots;     iBots = 0;     iMaxBots = get_pcvar_num(cvar_max_bots);     /* Why do you loop here? You can create an infinite loop because not everytime botid will be different. */     for (new i; i <= iMaxBots; i++)     {         id = get_random_bot(i);         // We dont have any more bots, break the loop         if(id == -2)             break;         if (!is_user_alive(id))             continue;         if (zp_get_user_zombie(id))             continue;         if (g_has_item[id])             continue;         set_task(1.0, "give_item", id+TASK_GIVEITEM);         iBots++;     }     remove_task(TASK_STARTGIVE);     remove_task(TASK_GIVEITEM); } public give_item(taskid) {     static id, random, itemid;     id = ID_BOT;     random = random_num(0, sizeof EXTRA_ITEMS - 1);     itemid = zp_get_extra_item_id(EXTRA_ITEMS[random]);     if (itemid == -1)     {         set_task(0.5, "give_item", id+TASK_GIVEITEM);         return;     }     zp_force_buy_extra_item(id, itemid, 1);     g_has_item[id] = true;     remove_task(id+TASK_GIVEITEM); } get_random_bot(index) {     new iPlayers[MAX_PLAYERS], iNum;     get_players(iPlayers, iNum, "ad");     if(iNum < index)         return -2;     return iNum > 0 ? iPlayers[index] : -1; }

Not tested.

Natsheh 01-02-2022 16:16

Re: Why does this plugin makes the server crash?
 
Quote:

Originally Posted by Shadows Adi (Post 2767471)
Skip the game modes if

Code:
public zp_round_started(gamemode, id) {     for (new id = 1; id <= g_maxplayers; id++)         g_has_item[id] = false;     if (gamemode != MODE_PLAGUE && gamemode != MODE_SURVIVOR && gamemode != MODE_SNIPER ) // etc...         set_task(2.0, "give_item_task", TASK_STARTGIVE); }


What?

Shadows Adi 01-02-2022 16:33

Re: Why does this plugin makes the server crash?
 
Quote:

Originally Posted by Natsheh (Post 2767473)
What?

My bad, forgot to text the message lmao

CrazY. 01-02-2022 18:02

Re: Why does this plugin makes the server crash?
 
Don't use that plugin, it's complete garbage and I know, I'm the author. Use this one, will work on any game mode.

Code:

#include <amxmodx>
#include <zombieplague>

new const EXTRA_ITEMS[][] = {
        "Balrog Ethereal"
}

new cvar_max_bots

new g_extraItems[sizeof EXTRA_ITEMS], g_extraItemCount

public plugin_init()
{
        register_plugin("Bots Buy Extra Items", "1.1", "Ainsley Harriott")
        cvar_max_bots = register_cvar("zp_force_buy_maxbots", "3")
}

public plugin_cfg()
{
        for (new i, itemid; i < sizeof EXTRA_ITEMS; i++)
        {
                itemid = zp_get_extra_item_id(EXTRA_ITEMS[i])

                if (itemid != -1)
                {
                        g_extraItems[g_extraItemCount++] = itemid
                }
        }
}

public zp_round_started(gamemode, id)
{
        if (g_extraItemCount > 0)
        {
                GiveBotExtraItems()
        }
}

GiveBotExtraItems()
{
        new bots[32], botCount
        new humanBots[32], humanBotCount

        get_players(bots, botCount, "adh")

        for (new i, bot; i < botCount; i++)
        {
                bot = bots[i]

                if (!zp_get_user_zombie(bot))
                {
                        humanBots[humanBotCount++] = bot
                }
        }

        SortIntegers(humanBots, humanBotCount, Sort_Random)

        for (new i, bot, itemid, max = min(humanBotCount, get_pcvar_num(cvar_max_bots)); i < max; i++)
        {
                bot = humanBots[i]
                itemid = g_extraItems[random(g_extraItemCount)]
                zp_force_buy_extra_item(bot, itemid, 1)
        }
}


Natsheh 01-02-2022 18:16

Re: Why does this plugin makes the server crash?
 
Here a better + fixed function to get a random bot with no replication!!

I saw alot of coders struggle to create such function there you go, try understanding...

PHP Code:

get_random_bot(arrNoReplicate[MAX_PLAYERS]="", &ReplicateSize=0)
{
    static 
iPlayers[MAX_PLAYERS], iNum;

    
get_players(iPlayersiNum"adh");

    if( 
ReplicateSize )
    {
        for(new 
ijiNumj++)
       {
           for(
0ReplicateSizei++)
           {
                if(
iPlayers] == arrNoReplicate])
                     
iPlayersj-- ] = iPlayers [ --iNum ];
           }
       }
    }
    
    if(!
iNum// uncomment and remove return 0; in the following if you want the function to never fails until legitimately there are no alive bots exists.
    
{
        
//ReplicateSize = 0;
        //get_players(iPlayers, iNum, "adh");
        
return 0;
    }

    new 
iChosen 0;

    if( 
iNum )
    {
        
iChosen iPlayersrandom(iNum) ] ;
        if(
MAX_PLAYERS ReplicateSizearrNoReplicate ReplicateSize++ ] = iChosen;
    }
        
    return 
iChosen;



This how to use inside the following function...

PHP Code:

public give_item_task(taskid)
{
    static 
iMaxBots;
    
iMaxBots get_pcvar_num(cvar_max_bots);

    for (new 
iidoldbots[MAX_PLAYERS], oldbots_countiMaxBotsi++)
    {
        if(! (
id get_random_bot(oldbotsoldbots_count)) )
            break;

        if (
zp_get_user_zombie(id))
            continue;

        if (
g_has_item[id])
            continue;

        
set_task(1.0"give_item"id+TASK_GIVEITEM);
    }



CrazY. 01-02-2022 19:12

Re: Why does this plugin makes the server crash?
 
Your code is not right either, it is not even working.

Code:

        0. random bot id -> 9
        1. random bot id -> 2
        2. random bot id -> 0
        3. random bot id -> 0
        4. random bot id -> 0
        5. random bot id -> 0
        6. random bot id -> 0
        7. random bot id -> 0
        8. random bot id -> 0
        9. random bot id -> 0
        10. random bot id -> 0
        11. random bot id -> 0
        12. random bot id -> 0
        13. random bot id -> 0
        14. random bot id -> 0
        15. random bot id -> 0
        16. random bot id -> 0
        17. random bot id -> 0
        18. random bot id -> 0
        19. random bot id -> 0
        20. random bot id -> 0
        21. random bot id -> 0
        22. random bot id -> 0
        23. random bot id -> 0
        24. random bot id -> 0
        25. random bot id -> 0
        26. random bot id -> 0
        27. random bot id -> 0
        28. random bot id -> 0
        29. random bot id -> 0

And you're calling get_players inside a loop. If you need 30 random bots your code will call get_players 30 times, like what is this

Natsheh 01-02-2022 19:17

Re: Why does this plugin makes the server crash?
 
Show your test code..

Also i hope you noticed i am using break in the loop if no bots were found!

Also i don't see it as a problem calling get_players 30 times if you wish to call it less add two new parameters to the function iPlayers and iNum or create both of them globally but you know i just made it easy to use.

And TBH its not recommended to be used in a loop.

You can use a repeated task to give each bot a new extra item.

Like the following...

PHP Code:

public give_item_task(taskid)
{
    static 
iMaxBots 0;
    if(
taskid == TASK_STARTGIVEiMaxbots 0;
    if(!
iMaxBotsiMaxBots get_pcvar_num(cvar_max_bots);

    static 
idoldbots[MAX_PLAYERS], oldbots_count;

        if(! (
id get_random_bot(oldbotsoldbots_count)) )
        {
            
oldbots_count 0;
            return;
        }

        if (!
zp_get_user_zombie(id) && !g_has_item[id])
            
set_task(1.0"give_item"id+TASK_GIVEITEM);

    
iMaxbots--;

    if(
iMaxbots)
    {
          
set_task(0.5"give_item_task"TASK_STARTGIVE 1);
    }



For the OP use the code from crazy's in post #6

GlobalPlague 01-03-2022 17:51

Re: Why does this plugin makes the server crash?
 
Thank you for all your answers intended to help me.

Okay, CrazY., I will use the second plugin you gave me. I will test it, and I will reply if it's working properly.

By the way, are you aware that the same bot can't buy extra items more than once during one round? For example, if a bot buys the Plasma Gun, then the bot gets infected and then humanized by an admin or buys an antidote, then the same bot will not be able to buy Plasma Gun, again, or any other item mentioned in this code:

Code:

// Items name (note: add exact item name)

new const EXTRA_ITEMS[][] = {
        "Plasma Gun",
        "Golden Ak 47",
        "Item3"
}

In other words, the plugin is unable to affect bots if they are humans, buy extra items, then become infected (zombies) and then become humans, again, by using an antidote or get humanized by an admin. If a bot, who have bought an extra item, becomes a zombie, and then becomes human, again, the bot won't be made, by the plugin, to buy any extra item mentioned in the "// Items name" section of the plugin.

The bots I use have the "buy an antidote" function built-in, so I haven't added the Antidote in "// Items name" section of your plugin. Is the problem coming from the fact the antidote usage is built-in in the .dll file that controls the bots?

Also, will the new plugin give extra items to humans and zombies, or only to humans? Will the plugin give extra items during any game mode, or only during normal infection mode? I ask this question, because I don't see any checks intended to return the plugin's function if X game mode is detected.

For example, the second plugin doesn't contain code like this:

Code:

public zp_round_started(gamemode, id)
{
    for (new id = 1; id <= g_maxplayers; id++)
        g_has_item[id] = false;

    if (gamemode != MODE_PLAGUE || MODE_SURVIVOR)
        set_task(2.0, "give_item_task", TASK_STARTGIVE);
}

Does that mean that the second plugin will give extra items during any game mode, just because there are no checks to detect the game modes listed as modes during which extra items shouldn't be given to bots, and then return the plugin's function intended to give bots extra items?


All times are GMT -4. The time now is 11:30.

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