AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   AMX Mod X Stocks (Custom useful stocks) (https://forums.alliedmods.net/showthread.php?t=287751)

happy_2012 09-13-2016 16:43

AMX Mod X Stocks (Custom useful stocks)
 
Hello,
So I have been coding some private plugins, and I came up with a various set of custom stocks, so I moved them to an include file, and decided to share them, perhaps they can be useful for you and your own plugins!

By including this file, you will not need to include amxmodx module in your plugin as we check it, and if it is not included, it will be automatically included.

Code is available on GitHub @ https://github.com/Chilimax/AMX-Mod-...amx_stocks.inc

I am seriously looking forward some ideas and suggestions to improve this code and make it much more useful :)

Natsheh 09-13-2016 16:47

Re: AMX Mod X Stocks (Custom useful stocks)
 
This is lame...

addons_zz 09-13-2016 17:47

Re: AMX Mod X Stocks (Custom useful stocks)
 
Quote:

Originally Posted by happy_2012 (Post 2453729)
I am seriously looking forward some ideas and suggestions to improve this code and make it much more useful :)

You may follow the AMXX doc style. It is helpful to see it/have it.
Code:
/* Core functions * * (c) Copyright 1998-2003, ITB CompuPhase * * This file is provided as is (no warranties). */ /**  * Returns the free memory space available to the plugin.  *  * @note This is a debugging function that is not intended for general plugin  *       use.  *  * @return  Free memory space in bytes  */ native heapspace(); /**  * Returns the function index of a public function declared in the plugin.  *  * @param name  Function name  *  * @return      Function index > 0 on success, -1 if function was not found  * @error       If the function name is too long (longer than 63 characters)  *              an error will be thrown.  */ native funcidx(const name[]); /**  * Returns the number of arguments passed into the currently executed function.  *  * @return  Number of arguments passed  */ native numargs(); /**  * Retrieves an argument value passed into the currently executed function.  *  * @param arg       Argument index  * @param index     Index to retrieve from the argument (for arrays and strings)  *  * @return          Argument value at given index  */ native getarg(arg, index = 0); /**  * Sets the value of an argument passed into the currently executed function.  *  * @note This is not equal to assigning a new value to a by-reference argument.  *  * @param arg       Argument index  * @param index     Index to set in the argument (for arrays and strings)  */ native setarg(arg, index = 0, value);

happy_2012 09-13-2016 18:13

Re: AMX Mod X Stocks (Custom useful stocks)
 
How can this be helpful mate? What I meant by ideas is new features :P

addons_zz 09-13-2016 18:41

Re: AMX Mod X Stocks (Custom useful stocks)
 
Quote:

Originally Posted by happy_2012 (Post 2453744)
How can this be helpful mate? What I meant by ideas is new features :P

Looks like there is not much code on it for now, so as it is simpler, there is no problem.
But, just gathering bunches of code together without a proper documentation is not helpful.
At least it is my preference, instead of get on that nightmare, I prefer start a clean notion one from zero.

happy_2012 09-13-2016 19:13

Re: AMX Mod X Stocks (Custom useful stocks)
 
I will add notes on each stock :)

fysiks 09-13-2016 21:51

Re: AMX Mod X Stocks (Custom useful stocks)
 
get_connected_target() makes no sense. What is it supposed to be doing?

happy_2012 09-14-2016 06:21

Re: AMX Mod X Stocks (Custom useful stocks)
 
you can use it to choose a random connected player :)
get_connected_target( random_num( 1, 32 ) ) I see it pretty useful in plugins which choose a random player regardless if they were dead or alive e.g deathrun terrorist choosing :P

PRoSToTeM@ 09-14-2016 11:54

Re: AMX Mod X Stocks (Custom useful stocks)
 
What about this?:
PHP Code:

#if !defined MAX_PLAYERS
const MAX_PLAYERS 32;
#endif

stock GetPlayers(players[MAX_PLAYERS], const predicateFuncName[]) {
#if !defined MaxClients
    
static MaxClients = -1;
    if (
MaxClients == -1) {
        
MaxClients get_maxplayers();
    }
#endif
    
    
new count 0;
    new 
predFuncId get_func_id(predicateFuncName);
    for (new 
1<= MaxClientsi++) {
        
callfunc_begin_i(predFuncId);
        
callfunc_push_int(i);
        if (
bool:callfunc_end()) {
            
players[count++] = i;
        }
    }
    
    return 
count;
}

stock GetRandomPlayers(randomPlayers[], randomPlayerCount, const predicateFuncName[]) {
    static 
players[MAX_PLAYERS];
    new 
playerCount GetPlayers(playerspredicateFuncName);
    
    new 
realCount min(randomPlayerCountplayerCount);
    for (new 
0realCounti++) {
        new 
rand random_num(0playerCount 1);
        
        
randomPlayers[i] = players[rand];
        
players[rand] = players[playerCount 1];
        
        
playerCount--;
    }
    
    return 
realCount;
}

stock GetRandomPlayer(const predicateFuncName[]) {
    new 
player[1];
    return 
GetRandomPlayers(player1predicateFuncName) ? player[0] : 0;


Example:
PHP Code:

    register_clcmd("say /smth""OnClientCommand_Say_Smth");
}

public 
bool:IsPlayerAliveAdmin(clientEntIndex) {
    return 
is_user_alive(clientEntIndex) && is_user_admin(clientEntIndex);
}

public 
OnClientCommand_Say_Smth(clientEntIndex) {
    
client_print(clientEntIndexprint_chat"RandPl: %d"GetRandomPlayer("IsPlayerAliveAdmin"));
    
    static 
players[MAX_PLAYERS];
    new 
count GetRandomPlayers(playerssizeof(players), "IsPlayerAliveAdmin");
    
client_print(clientEntIndexprint_chat"RandPlCount: %d"count);
    for (new 
0counti++) {
        
client_print(clientEntIndexprint_chat"RandPl#%d: %d"iplayers[i]);
    }
    
    
count GetPlayers(players"IsPlayerAliveAdmin");
    
client_print(clientEntIndexprint_chat"PlCount: %d"count);
    for (new 
0counti++) {
        
client_print(clientEntIndexprint_chat"Pl#%d: %d"iplayers[i]);
    }



fysiks 09-14-2016 21:16

Re: AMX Mod X Stocks (Custom useful stocks)
 
Quote:

Originally Posted by happy_2012 (Post 2453824)
you can use it to choose a random connected player :)
get_connected_target( random_num( 1, 32 ) ) I see it pretty useful in plugins which choose a random player regardless if they were dead or alive e.g deathrun terrorist choosing :P

Umm . . . no. First, the function name doesn't describe what the function is actually doing (it doesn't do anything logical). Second, the function will fail relatively often (i.e. return -1). Third, if you want a truly random player, you should do this:

PHP Code:

stock get_random_player()
{
    new 
iPlayers[32], iPlayersNum
    get_players
(iPlayersiPlayersNum)
    return 
iPlayers[random(iPlayersNum)]


This will always return a valid player unless there are zero players on the server.


All times are GMT -4. The time now is 01:34.

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