AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimizing - Remove dropped weapons (https://forums.alliedmods.net/showthread.php?t=95755)

atomen 06-26-2009 21:41

Optimizing - Remove dropped weapons
 
Hello!

Well it's pretty simple, the code I've made for deleting dropped weapons
after a certain period works somewhat good but not enough. If you take
a look at the the script you quickly notice how inefficient it is. Therefore
I'm asking you for help!
PHP Code:

#include <amxmodx>
#include <fakemeta>

#define gPLUGIN        "Weapon Controller"
#define gVERSION    "X"
#define gAUTHOR        "."

new const g_weapon_contain[] = "models/w_";

new 
g_pcvar_remove;
new 
g_drop_id;

public 
plugin_init()
{
    
register_plugin(gPLUGINgVERSIONgAUTHOR);
    
register_clcmd("drop""dropped_weapon");

    
register_forward(FM_SetModel"forward_set_model");

    
g_pcvar_remove register_cvar("amx_weapon_removetime""10");
}

public 
forward_set_model(entmodel[])
{
    if (!
pev_valid(ent) || containi(modelg_weapon_contain) == -|| equali(model"models/w_weaponbox.mdl"))
    {
        return 
FMRES_IGNORED;
    }

    static 
Float:info[4], Float:origin[3];

    if(
is_user_connected(g_drop_id))
    {
        
info[3] = ent;
        
g_drop_id 0;
    }

    else
    {
        return 
FMRES_IGNORED;
    }

    if(
engfunc(EngFunc_EntIsOnFloorent))
    {
        
pev(entpev_originorigin);

        for(new 
03i++)
        {
            
info[i] = origin[i];
        }

        
set_task(get_pcvar_float(g_pcvar_remove), "check_weapon"__:info4);
    }

    else
    {
        
set_task(0.1"check_weapon_floor"__:info4);
    }

    return 
FMRES_IGNORED;
}

public 
check_weapon_floor(info[])
{
    static 
origin[3], model[sizeof(g_weapon_contain) + 1];
    new 
ent info[3];

    
pev(entpev_modelmodelsizeof(g_weapon_contain));

    if(!
pev_valid(ent) || containi(modelg_weapon_contain) == -1)
        return;

    if(
engfunc(EngFunc_EntIsOnFloorent))
    {
        
pev(entpev_originorigin);

        for(new 
03i++)
        {
            
info[i] = origin[i];
        }

        
set_task(get_pcvar_float(g_pcvar_remove) - info[4], "check_weapon"__:info4);
    }

    else
    {
        
set_task(0.1"check_weapon_floor"__:info4);
    }
}

public 
check_weapon(info[])
{
    new 
ent info[3];

    if(!
pev_valid(ent))
        return;

    new 
Float:_origin[3];
    
pev(entpev_origin_origin);

    for(new 
03i++)
    {
        new 
origin    _:_origin[i];
        new 
_info    _:info[i];

        
log_amx("%d"1);

        if(
origin != _info)
        {
            return;
        }
    }

    
set_pev(entpev_effectspev(entpev_effects) & EF_NODRAW);
    
engfunc(EngFunc_RemoveEntityent);
}

public 
dropped_weapon(id)
{
    if(
is_user_connected(id) & is_user_alive(id))
    {
        
g_drop_id id;
    }


Some description: The plugin is supposed to delete a dropped weapon after
a certain time has passed (defined by cvar). To check that the weapon hasn't
been picked up under the time passed (time defined by cvar) the code checks
if the origin of the entity has changed. For some reason the origin is only correct
50% of the times...

Exolent[jNr] 06-26-2009 22:01

Re: Optimizing - Remove dropped weapons
 
Welcome back, atomen. :)

All you need to do is modify this plugin:
http://forums.alliedmods.net/showthread.php?t=49323

And add a set_task() instead of instantly removing the weapon.
If the weaponbox entity is still valid (which means no one picked it up) then you remove it.

atomen 06-27-2009 13:14

Re: Optimizing - Remove dropped weapons
 
Thank you for your greeting!

Ok, I decided to optimize my code as good as possible and it has turned out pretty good though there's one small little problem...

You can see I've marked 2 spots in the code and the code which I've marked, logs the entity number. For some reason the entity numbers on the SAME weapon differs:
Code:
#include <amxmodx> #include <hamsandwich> #include <fakemeta> #define gPLUGIN     "." #define gVERSION    "." #define gAUTHOR     "." #define gRANDOM     6497 new const g_weapon_contain[] = "models/w_"; new g_pcvar_remove; new g_drop_id; public plugin_init() {     register_plugin(gPLUGIN, gVERSION, gAUTHOR);     RegisterHam(Ham_AddPlayerItem, "player", "fwd_AddPlayerItem");     register_clcmd("drop", "dropped_weapon");     register_forward(FM_SetModel, "forward_set_model");     g_pcvar_remove = register_cvar("amx_weapon_removetime", "10"); } public fwd_AddPlayerItem(id, ent) {     // When I pickup the weapon I've dropped, it doesn't have the same ent number as it had in the 'FM_SetModel' function!!!     log_amx("ADDPLAYERITEM %d", ent);     if(pev(ent, pev_iuser4))     {         set_pev(ent, pev_iuser4, 0);         remove_task(ent + gRANDOM);     }     return HAM_IGNORED; } public forward_set_model(ent, model[]) {     if (!pev_valid(ent) || containi(model, g_weapon_contain) == -1 || equali(model, "models/w_weaponbox.mdl"))     {         return FMRES_IGNORED;     }     if(is_user_connected(g_drop_id))     {         set_pev(ent, pev_iuser4, 1);         g_drop_id = 0;     }     else     {         return FMRES_IGNORED;     }     new _ent[1]; _ent[0] = ent;     // It always differs with 1-2 numbers     log_amx("TASK EXECUTED: %d", ent);     set_task(get_pcvar_float(g_pcvar_remove), "check_weapon", ent + gRANDOM, _ent, 1);     return FMRES_IGNORED; } public check_weapon(_ent[]) {     new ent = _ent[0];     if(!pev_valid(ent))         return;     static model[26];     pev(ent, pev_model, model, 25);     if(containi(model, g_weapon_contain) == -1 || equali(model, "models/w_weaponbox.mdl") || pev(ent, pev_euser4) == gRANDOM)         return;     set_pev(ent, pev_effects, pev(ent, pev_effects) & EF_NODRAW);     set_pev(ent, pev_iuser4, 0);     engfunc(EngFunc_RemoveEntity, ent); } public dropped_weapon(id) {     if(is_user_connected(id) & is_user_alive(id))     {         g_drop_id = id;     } }
Now to the question, why does the entity number on the same weapon always differ with 1-2 numbers?

xPaw 06-27-2009 13:27

Re: Optimizing - Remove dropped weapons
 
Lol .. Making weapon think, engine removes it self :)
PHP Code:

#include <amxmodx>
#include <fakemeta>

#define REMOVE_TIME 20.0

public plugin_init( ) {
    
register_forwardFM_SetModel"FwdSetModel");
}

public 
FwdSetModeliEntity ) {
    if( !
pev_validiEntity ) )
        return;
    
    static const 
szWeaponbox[] = "weaponbox";
    static 
szClassname10 ]; // weaponbox
    
peviEntitypev_classnameszClassnamecharsmaxszClassname ) );
    
    if( 
equalszClassnameszWeaponbox ) )
        
set_peviEntitypev_nextthinkget_gametime( ) + REMOVE_TIME );



ConnorMcLeod 06-27-2009 13:30

Re: Optimizing - Remove dropped weapons
 
If you want instant remove :

PHP Code:

#include <amxmodx>
#include <engine>
#include <hamsandwich>

public plugin_init()
{
    
RegisterHam(Ham_Spawn"weaponbox""WeaponBox_Spawn"1)
}

public 
WeaponBox_Spawn(iEnt)
{
    
entity_set_int(iEntEV_INT_flagsFL_KILLME)
    
call_think(iEnt)



atomen 06-27-2009 14:20

Re: Optimizing - Remove dropped weapons
 
No, I wan't the weapon to be removed after a certain time has passed(defined by cvar). And after the time has elapsed the plugin must check if the weapon has been picked up and then dropped during the time passed.

But how come the entities number differs?

ConnorMcLeod 06-27-2009 14:27

Re: Optimizing - Remove dropped weapons
 
@ xPaw

static szClassname[ 9 ]; // weaponbox

->

static szClassname[ 10 ]; // weaponbox

xPaw 06-27-2009 14:32

Re: Optimizing - Remove dropped weapons
 
@ConnorMcLeod: my bad :d
@atomen: use my code

SchlumPF* 06-27-2009 14:39

Re: Optimizing - Remove dropped weapons
 
Quote:

Originally Posted by xPaw (Post 858680)
@atomen: use my code

but dont use FM_SetModel, use Ham_Spawn

ConnorMcLeod 06-27-2009 14:47

Re: Optimizing - Remove dropped weapons
 
If i remember, Ham_Spawn works only if you instantly remove the weaponbox, because right after it, the Think is not set so that the weaponbox will be removed.


All times are GMT -4. The time now is 15:27.

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