AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   [Request] Block Usp Damage (https://forums.alliedmods.net/showthread.php?t=334586)

Snake. 10-06-2021 02:34

[Request] Block Usp Damage
 
Hello, can a codder give me a basic code which blocks usp to give damage players?

DJEarthQuake 10-08-2021 10:39

Re: [Request] Block Usp Damage
 
The raw code is too out control to post. It breaks the game. There needs to be balance. Here is some basic code to make the USP only fire blanks and push magnum elites as a counter-measure. It pauses on Gungame and assassinations maps. Warns if using a scope plugin I made is running that is brings back Scope for Colt and works on USP too. The player name code could be added later for those who use Amxx 1.8.2. If using 182 the names will shows as letter n.
CVARS
mp_leets 0|1 - replace default pistols with the Dual-Elites with armor piercing damage.
mp_usp 0|1 - 0 is off USP is normal. 1 is USP fires blanks.
PHP Code:

#include amxmodx
#include engine
#include engine_stocks
#include hamsandwich
#define MAX_NAME_LENGTH 32
#define charsmin        -1
#define fNULL 0.0
new g_leets,g_usp

public plugin_init()
{
    
RegisterHam(Ham_TakeDamage,"player","Damage"0)
    
register_plugin("Elite:no USP""1.0"".sρiηX҉.Snake");
    
g_usp register_cvar("mp_usp""0")
    new 
mapname[MAX_NAME_LENGTH];get_mapname(mapnamecharsmax(mapname))
    if(
containi(mapname,"as_") != charsmin)
    {
        
log_amx "Assassination map detected"
        
pause "a";
    }

    
/*Conflicts with Gungame*/
    
if(get_cvar_pointer("gg_enabled") || get_cvar_pointer("scope_colt_cost"))
        
set_task(10.0,"@eval_incompat",2448)

}

public 
plugin_precache()
{
    new 
ent;g_leets register_cvar("mp_leets""1")
    if(
get_pcvar_num(g_leets))
    {
        
ent find_ent(charsmin,"game_player_equip") ? find_ent(charsmin,"game_player_equip") : create_entity("game_player_equip")
        !
is_valid_ent(ent) ? log_amx("ERROR! Unable to init player equipment functions.") & set_fail_state("unable to equip player"):
        
DispatchKeyValueent"item_assaultsuit""1"),
        
DispatchKeyValueent"weapon_knife""1"),
        
DispatchKeyValueent"weapon_elite""1" ),
        
DispatchKeyValueent"ammo_9mm""4"),
        
DispatchSpawn(ent);
    }


}

@
eval_incompat()
{
    
server_print "Checking if we should control USP damage"

    
if(get_cvar_num("gg_enabled") == 1)
    {
        
log_amx "Gungame is on"
        
pause "a";
    }
    else if(
get_cvar_pointer("scope_colt_cost"))
    
client_print 0print_chat"Scoped colt USP is firing blanks because of .Snake!"
    
server_print "No plugin conflicts found."
}

public 
Damage VictimInflictorAttackerFloat:fDamage )
{
    if(
get_pcvar_num(g_usp) || get_pcvar_num(g_leets))
    {

        if(
is_user_alive(Attacker))
        {
            new 
damwpnname[MAX_NAME_LENGTH]
            new 
weapon get_user_weapon(Attacker)
            
get_weaponname(weapon,damwpnname,charsmax(damwpnname))

            if(
get_pcvar_num(g_usp) && containi(damwpnname,"usp") != charsmin)
            {
                
SetHamParamFloat(4,fNULL)
                
server_print "%n is shooting USP blanks at %n"Attacker,Victim
                client_print 0
print_chat"%n is shooting USP blanks at %n"Attacker,Victim
                client_cmd Attacker
,"spk idiot"
                
return HAM_SUPERCEDE
            
}
            else if(
get_pcvar_num(g_leets) && containi(damwpnname,"elite") != charsmin)
            {

                
SetHamParamFloat(4,fDamage*3.5)
                
client_print 0print_chat"%n is shooting Armor piercers at %n"Attacker,Victim

            
}
            else  return 
HAM_IGNORED




        
}


    }
    return 
HAM_HANDLED



HamletEagle 10-08-2021 11:02

Re: [Request] Block Usp Damage
 
@DJ, this isn't going to work like you expect it to.

PHP Code:

#define ent create_entity("game_player_equip")
DispatchKeyValueent"item_assaultsuit""1" ) && DispatchKeyValueent"weapon_knife""1" ) && DispatchKeyValueent"weapon_elite""5" ) && DispatchSpawn(ent); 

Translates into

PHP Code:

DispatchKeyValuecreate_entity("game_player_equip"), "item_assaultsuit""1" ) && DispatchKeyValuecreate_entity("game_player_equip"), "weapon_knife""1" ) && DispatchKeyValuecreate_entity("game_player_equip"), "weapon_elite""5" ) && DispatchSpawn(create_entity("game_player_equip")); 

Every single time you reference "ent" you are creating a new entity so obviously, that can't work. Plus, there is no reason to write one-liners like that and you aren't writing functional code where everything needs to be an expression. Just write a proper block of code:

PHP Code:


public plugin_precache()
{
    new 
ent create_entity("game_player_equip")
    
DispatchKeyValueent"item_assaultsuit""1" )
    
DispatchKeyValueent"weapon_knife""1" )
    
DispatchKeyValueent"weapon_elite""5" )
    
DispatchSpawn(ent);



DJEarthQuake 10-08-2021 12:44

Re: [Request] Block Usp Damage
 
It is functional. Do not see the difference except what you did is more vertical and what I did was horizontal. Yours translates like mine. The only potential problem how it was done is if admin resets rounds a bunch of times there could be some debris from extra elites since I thought it easier to give several instead of make a separate entry for ammo.

HamletEagle 10-08-2021 12:53

Re: [Request] Block Usp Damage
 
Not sure how you do not see the issue. Your version creates 4 DIFFERENT game_player_equip entities. One where you dispatch "item_assaultsuit" keyvalue, another one where you dispatch "weapon_knife" key value, another one where you dispatch "weapon_elite" keyvalue and another one that you actually call spawn on.

Ignore the fact that you wrote everything on one line(which is a bad thing to do btw) and I split it into multiple lines. Look at what is passed inside Dispatch* calls(this is what your code translates to after preprocessing):
Code:

DispatchKeyValue( create_entity("game_player_equip"), "item_assaultsuit", "1" ) && DispatchKeyValue( create_entity("game_player_equip"), "weapon_knife", "1" ) && DispatchKeyValue( create_entity("game_player_equip"), "weapon_elite", "5" ) && DispatchSpawn(create_entity("game_player_equip"));

#define is a textual replacement. Every single appearance of "ent" inside the sma file is replaced by create_entity(). Define does not create a variable. The code I posted creates only one entity because create_entity() is called only once and saved inside a variable.
I suggest you read about #define and the preprocessor. Here, it may appear that it works because the effect is you create 4 entities, but in other causes it could seriously mess up your code and you'll have no clue why.

DJEarthQuake 10-08-2021 13:30

Re: [Request] Block Usp Damage
 
Code:
    new ent = create_entity("game_player_equip")     DispatchKeyValue( ent, "item_assaultsuit", "1" )     DispatchKeyValue( ent, "weapon_knife", "1" )     DispatchKeyValue( ent, "weapon_elite", "5" )     DispatchSpawn(ent);
It's all the same per ent = create_entity("game_player_equip") albeit variable or macro. Use the same perspective and confront yourself with. Here's how it looks.

Code:
#include amxmodx #include engine public plugin_precache() DispatchKeyValue( create_entity("game_player_equip"), "item_assaultsuit", "1" ) && DispatchKeyValue( create_entity("game_player_equip"), "weapon_knife", "1" ) && DispatchKeyValue( create_entity("game_player_equip"), "weapon_elite", "5" ) && DispatchSpawn(create_entity("game_player_equip"));

Macro or not ent is still create_entity("game_player_equip")

Static instead of variable is what I would have chosen.
new const instead of variable is what I would have chosen.

Code:
#include amxmodx #include engine public plugin_precache() {     new const ent = create_entity("game_player_equip")     DispatchKeyValue( ent, "item_assaultsuit", "1" )     DispatchKeyValue( ent, "weapon_knife", "1" )     DispatchKeyValue( ent, "weapon_elite", "5" )     DispatchSpawn(ent); }

HamletEagle 10-08-2021 13:37

Re: [Request] Block Usp Damage
 
I feel like I'm talking to a wall, but I'll say it again, ignore the fact that you wrote everything in one line and used && to glue everything together. This isn't about that. If you do not see the difference between calling a native 4 times for literally no benefit vs only doing it once then I don't know what to tell you.

Doing #define ent create_entity("") and new ent = create_entity("") are 2 completely different things.
The first one does a textual replacement, changing "ent" in every place inside the code with create_entity(""). If you wanted to create an entity, give it a model, make it solid, it wouldn't work. At every line where you wanted to set those properties, you would be creating a DIFFERENT entity, instead of only one entity with all the needed properties. Your code would end up with multiple ents where each ent has only one of the properties you were trying to set.

The second code allocates memory on the stack for 4 bytes of memory. create_entity() return value is placed at that memory location. From now on, inside that function, when you use "ent" you refer to that one entity that was created, which will be the same entity.

And static is wrong, it should be new. precache is called only once, you don't need to remember the value between function calls, therefore you do not need a global like variable with local scope.

DJEarthQuake 10-08-2021 13:50

Re: [Request] Block Usp Damage
 
I would have made it new const instead of a variable.
https://cdn.discordapp.com/attachmen...wn_defines.jpg

From the Pawn manual. I do know Amxx has it's own nuances since it relates to the GoldSrc HLDS but you were bringing up defines.

DJEarthQuake 10-08-2021 15:13

Re: [Request] Block Usp Damage
 
Static won't even compile! lol I was in the wrong directory from force of habit. I meant new const instead of static. Macros do chump a bit but few maps use this ent and only custom ones at that. So like an array repeats everything in it when applied to a native like giving a weapon in this case, you are illustrating by using this as a macro it is a memory chewer? I would have not used a macro beyond small requests and only this one because do not feel comfortable sharing script flat out disabling the USP silently. Thanks for chiming in. Please help me understand further if this is so. Have any supporting documentation or evidence beyond I said so? I need to see this. I pasted what the manual shows on GitHub today.

Quote:

you don't need to remember the value between function calls, therefore you do not need a global like variable with local scope.
I get it. Did not think it was that big of a deal. Since it is the same value time and time again why look it up again or define it over and over as a new variable that destroys itself?

HamletEagle 10-08-2021 15:23

Re: [Request] Block Usp Damage
 
Quote:

Originally Posted by DJEarthQuake (Post 2759976)
I would have made it new const instead of a variable.
https://cdn.discordapp.com/attachmen...wn_defines.jpg

From the Pawn manual. I do know Amxx has it's own nuisances since it relates to the GoldSrc HLDS but you were bringing up defines.

A constant is by definition, well, constant. It doesn't matter that it gets replaced with its value because that value is the same value all the time.

For example:
PHP Code:

#define something 5

native_1(something)
native_2(something

translates to

PHP Code:

native_1(5)
native_2(5

BUT a native call is not a constant, it will return a different value on different calls.

PHP Code:

#define something create_entity("")

native_1(something)
native_2(something

Will translate into

PHP Code:

native_1(create_entity(""))
native_2(create_entity("")) 

and here each call to create_entity will return a different entity index, therefore resulting in native_1 and native_2 being applied to different entities. So, for example, at a run you may end up with:

PHP Code:

native_1(134)
native_2(135)

//2 different ent indexes. 


Quote:

Originally Posted by DJEarthQuake (Post 2759989)
Static won't even compile! lol I was in the wrong directory from force of habit. I meant new const instead of static. Macros do chump a bit but few maps use this ent and only custom ones at that. So like an array repeats everything in it when applied to a native like giving a weapon in this case, you are illustrating by using this as a macro it is a memory chewer? I would have not used a macro beyond small requests and only this one because do not feel comfortable sharing script flat out disabling the USP silently. Thanks for chiming in. Please help me understand further if this is so. Have any supporting documentation or evidence beyond I said so? I need to see this. I pasted what the manual shows on GitHub today.

static WILL compile. But it's wrong to use static, it should be new. "static" should be used when you need a global-like variable but with local scope(limited to the function that defines the static variable). This is useful when you want to remember the value of the variable between multiple function call(like a global) but you don't want other functions to be able to change that variable.
precache is called only once, you don't need a static variable. You can use it, but you don't need it. Use "new".

It isn't a memory chewer. A macro isn't a variable. Macros do text based replacement(find and replace from word or notepad if it helps you visualize). Because of that, they have some quirks and if you aren't aware of how they work, you can end up with horribly hard to debug bugs and wrong code. In which way would it be wrong? It depends: wrong output, wasting memory, crashing, who knows. One thins is for sure: the plugin will not work as expected.

Here is an example of such a bug:

PHP Code:

#define square(%1) %1 * %1 

You would expect this macro to output x * x for input x. So if you did square(5) to get back 25. But look what happens when you use it in the following way:

PHP Code:

new 5
square
(3) = 3a 4a 

So square(a + 3) = square(5 + 3) = square( 8 ) = 4 * 8 + 3 = 35. But the square of 8 is 64. This is because it did a textual replacement of x with a + 3. It did not firstly evaluate a + 3 to 8 and then do 8 * 8, like a function would do.
Macros are parsed before the code is actually compiled. They don't make it into the final binary. Macros are not functions. Macros are not variables.


All times are GMT -4. The time now is 23:56.

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