AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   HP Modification Issue: Need Help! KNIFE PLUGIN (https://forums.alliedmods.net/showthread.php?t=344822)

vres 12-11-2023 17:35

HP Modification Issue: Need Help! KNIFE PLUGIN
 
Hello everyone, I created this plugin to allow playing with different HP values on knife servers without the need to change the map. However, it has a flaw: when an admin types /35hp, 1hp, or acer, only their HP changes, and others remain unaffected.
Note: This is my first plugin.

Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <fun>

new mod35hp, mod1hp;

public plugin_init() {
        RegisterHam(Ham_Spawn, "player", "fwHamPlayerSpawnPost", 1)
       
        register_clcmd("say /35hp", "cmd35hp");
        register_clcmd("say /1hp", "cmd1hp");
        register_clcmd("say /acer", "cmdacer");
        set_task(1.0, "hudgoster", _, _, _, "b");
}


public cmd35hp(id){
        if (!is_user_connected(id) || !is_user_admin(id)) {
                return PLUGIN_HANDLED;
        }
        mod1hp = 0
        mod35hp = 1
        hpver();
       
        return PLUGIN_HANDLED;
}

public cmd1hp(id){
        if (!is_user_connected(id) || !is_user_admin(id)) {
                return PLUGIN_HANDLED;
        }
        mod35hp = 0
        mod1hp = 1
        hpver();

        return PLUGIN_HANDLED;
}

public cmdacer(id){
        if (!is_user_connected(id) || !is_user_admin(id)) {
                return PLUGIN_HANDLED;
        }
        mod1hp = 0
        mod35hp = 0
        hpver();

        return PLUGIN_HANDLED;
}

public fwHamPlayerSpawnPost(iPlayer) {
        if (is_user_alive(iPlayer)) {
                if(mod1hp == 1 && mod35hp == 0){
                        set_user_health(iPlayer, 1)
                        client_print(0, print_chat, "1hp mod active");
                }else if(mod1hp == 0 && mod35hp == 1){
                        set_user_health(iPlayer, 35)
                        client_print(0, print_chat, "35hp mod active");
                       
                }
        }
}

public hudgoster(){
        set_hudmessage(200, 100, 0, -0.98, -0.73, .effects= 1 , .holdtime= 0.8)
       
        if(mod1hp==1){
               
                show_hudmessage(0, "1HP mode active")       
        }else if(mod35hp==1){
               
                show_hudmessage(0, "35HP mode active")
        }else{
                return PLUGIN_HANDLED;
        }
        return PLUGIN_HANDLED;
}

public hpver(){
       
        for (new i = 1; i <= get_maxplayers(); i++)
        {
                if (is_user_connected(i) && is_user_alive(i) && mod1hp == 1){
                        set_user_health(i, 1)
                }else if(is_user_connected(i) && is_user_alive(i) && mod35hp == 1){
                        set_user_health(i, 35)
                }else{
                        set_user_health(i, 100)
                       
                }
                       
        }
        return PLUGIN_HANDLED;
}


georgik57 12-11-2023 20:00

Re: HP Modification Issue: Need Help! KNIFE PLUGIN
 
1. you can define the needed access directly in register_clcmd, no need to check if user is admin in function
Quote:

Originally Posted by amxmodx.inc
/* Registers function which will be called from client console.
* Set FlagManager to 1 to make FlagManager always include this command
* Set FlagManager to 0 to make FlagManager never include this command
* Returns the command ID.
*/
native register_clcmd(const client_cmd[],const function[],flags=-1, const info[]="", FlagManager=-1);

2. you can just register all say commands and filter them inside a single function. see this code as an example:
PHP Code:

public plugin_init()
    
register_clcmd("say""fwClCmdSay")

public 
fwClCmdSay(const iID)
{
    new 
szTemp[1]; // first char will be " second char /
    
read_args(szTempcharsmax(szTemp))
    
    
trim(szTemp)
    
remove_quotes(szTemp)
    
replace(szTempcharsmax(szTemp), "/""")
    
replace(szTempcharsmax(szTemp), ".""")
    
replace(szTempcharsmax(szTemp), "!""")
    
    if (
equali(szTemp"restart") || equali(szTemp"rr"))
    {
        
fwConCmdRestart(iID)
        
        return 
PLUGIN_HANDLED_MAIN;
    }
    else if (
equali(szTemp"warm") || equali(szTemp"wrm"))
    {
        
fwConCmdWarm(iID)
        
        return 
PLUGIN_HANDLED_MAIN;
    }
    
    return 
PLUGIN_CONTINUE;


3. instead of looping through all player ids you can get them all filtered with a single native which is faster
Quote:

Originally Posted by amxmodx.inc
/* Sets indexes of players.
* Flags:
* "a" - don't collect dead players.
* "b" - don't collect alive players.
* "c" - skip bots.
* "d" - skip real players.
* "e" - match with team.
* "f" - match with part of name.
* "g" - ignore case sensitivity.
* "h" - skip HLTV.
* Example: Get all alive CTs: get_players(players,num,"ae","CT") */
native get_players(players[32], &num ,const flags[]="", const team[]="");

Usage example:
PHP Code:

    new iCVarHUD get_pcvar_num(g_iIDPCVarHUD), iiIDsPlayers[32], iCountPlayers;
    
//client_print(0, print_chat, "[fwTaskShowHUD] C4: (%d)%s.", iIDsPlayers[i], szTemp)
    
    
if (iCVarHUD 1)
    {
        
// red = 0, green = 160, blue = 0, Float:x = -1.0, Float:y = 0.65,
        // effects = 2, Float:fxtime = 6.0, Float:holdtime = 3.0,
        // Float:fadeintime = 0.1, Float:fadeouttime = 1.5, bool:reliable = true
        #if defined _dhudmessage_included
        
set_dhudmessage
        
(
            
25500, -1.00.900.0,
            
0.1 BOMBMSG_INTERVAL10.00.0false
        
)
        
#else
        
set_dhudmessage
        
(
            
25500, -1.00.900.0,
            
0.1 BOMBMSG_INTERVAL10.00.0
        
)
        
#endif
    
}
    else
        
set_hudmessage(25500, -1.00.900.00.1 BOMBMSG_INTERVAL10.00.0)

    
get_players(iIDsPlayersiCountPlayers"ec""SPECTATOR")
    
    for (
0iCountPlayersi++)
    {
        if (
iCVarHUD 1)
            
show_dhudmessage(iIDsPlayers[i], "C4: %s%s%.1f m%s"g_szName[0] ? g_szName ""g_szName[0] ? "(" ""fm_entity_range(g_iIDEntBombiIDsPlayers[i]) / 25.4g_szName[0] ? ")" "")
        else
            
show_hudmessage(iIDsPlayers[i], "C4: %s%s%.1f m%s"g_szName[0] ? g_szName ""g_szName[0] ? "(" ""fm_entity_range(g_iIDEntBombiIDsPlayers[i]) / 25.4g_szName[0] ? ")" "")
    } 

4. instead of having 2 variables you can just have 1 and store the hp which should be set on the players
5. you should try using dhud since your message is displayed only once and not spammed. unlike default hud, it has auto resize and looks better. if you're using amxx <= 182 download the necessarry include file from here otherwise this step is not needed: https://forums.alliedmods.net/attach...4&d=1583426083
Very good job overall for your first plugin. Congrats.

bigdaddy424 12-11-2023 21:21

Re: HP Modification Issue: Need Help! KNIFE PLUGIN
 
same principle
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <fun>

enum _:MOD
{
    
ACER,
    
MOD1HP,
    
MOD35HP
}

new 
modhealth

public plugin_init()
{
    
RegisterHam(Ham_Spawn"player""fwHamPlayerSpawnPost"1)
    
    
register_clcmd("say /35hp""cmd35hp");
    
register_clcmd("say /1hp""cmd1hp");
    
register_clcmd("say /acer""cmdacer");
    
set_task(1.0"hudgoster"___"b");
}

public 
cmd35hp(id)
{
    if (
is_user_admin(id))
    {
        
mod MOD35HP
        hpver
()
    }
    return 
PLUGIN_HANDLED
}

public 
cmd1hp(id)
{
    if (
is_user_admin(id))
    {
        
mod MOD1HP
        hpver
()
    }
    return 
PLUGIN_HANDLED
}

public 
cmdacer(id)
{
    if (
is_user_admin(id))
    {
        
mod ACER
        hpver
()
    }
    return 
PLUGIN_HANDLED
}

public 
fwHamPlayerSpawnPost(id)
{
    if (
is_user_alive(id))
    {
        
set_user_health(idhealth)
    }
}

public 
hudgoster()
{
    if (
mod)
    {
        
set_hudmessage(2001000, -0.98, -0.73, .effects, .holdtime0.8)
        
show_hudmessage(0"%dHP mode active"mod == MOD35HP 35 1)    
    }
}

public 
hpver()
{
    switch (
mod)
    {
        case 
ACERhealth 100
        
case MOD35HPhealth 35
        
case MOD1HPhealth 1
    
}

    new 
players[MAX_PLAYERS], numid
    get_players
(playersnum"ah")

    for (new 
0numi++)
    {
        
id players[i]
        
set_user_health(idhealth)
    }



101 12-12-2023 03:35

Re: HP Modification Issue: Need Help! KNIFE PLUGIN
 
1 Attachment(s)
Quote:

Originally Posted by vres (Post 2814347)
it has a flaw: when an admin types /35hp, 1hp, or acer, only their HP changes, and others remain unaffected.

you may applied the function while other players were dead .

this is general , u can edit the cmd as u like :
PHP Code:

#include <amxmodx>
#include <hamsandwich>
#include <fun>

new MaxP,hp;
new 
bool:gBotsReg;

public 
plugin_init() 
{
    
MaxP get_maxplayers()+1;
    
hp 100;
    
RegisterHam(Ham_Spawn"player""fwHamPlayerSpawnPost",1);
    
register_clcmd("say","cmdhp");
}

public 
cmdhp(id)
{
    new 
Args[16];
    
read_args(Args,16);
    if (
contain(Args,"/mod")!=-1)
    {
        if ( 
get_user_flags(id)&ADMIN_RCON )
        {
            new 
index strfind(Args,"d",false,4)+1;    // start from 4 , bring what is after "d" letter (in case admin was drunk)
            
new value str_to_num(Args[index]);
            if (! 
value)
            {        
                
client_print(idprint_chat,"Usage /mod%d",random(99)+1);
            }
            else
            {
                
hp=value;
                
Start_Mod();    // You Can do <Restart Round> instead of the loop below : server_cmd("sv_restart 1");
            
}
        }
        else 
client_print(idprint_chat,"You don't have access to this command");
    }
    return 
PLUGIN_CONTINUE;
}

Start_Mod()
{
    new 
P_Team;
    for (new 
i=1iMaxP i++)
    {
        
P_Team get_user_team(i);
        if (
P_Team==|| P_Team==2)
            
ExecuteHamB(Ham_CS_RoundRespawn,i);
    }
}
    
public 
hudgoster()
{
    
set_hudmessage(2001000, -0.98, -0.73, .effects, .holdtime0.8)
    if (!
show_hudmessage(0"%dHP mode active",hp))
        
remove_task(100);
}

public 
client_authorized(id)
{
    if(!
task_exists(100))
        
set_task(1.0,"hudgoster",100,_,_,"b");
    if(!
gBotsReg    &&    get_cvar_pointer("bot_quota")    &&    is_user_bot(id))
        
set_task(0.1,"register_Ham_for_bots",id);    
}

public 
register_Ham_for_bots(id)
{
    if(!
gBotsReg)
    {
        
gBotsReg true;
        
RegisterHamFromEntity(Ham_Spawn,id,"fwHamPlayerSpawnPost",1); 
    }
}

public 
fwHamPlayerSpawnPost(id)
{
    
set_user_health(id,hp);



vres 12-13-2023 09:27

Re: HP Modification Issue: Need Help! KNIFE PLUGIN
 
My issue has been resolved, and I've learned new things. Thank you :)


All times are GMT -4. The time now is 00:32.

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