Raised This Month: $ Target: $400
 0% 

Optimizing - Remove dropped weapons


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
atomen
Veteran Member
Join Date: Oct 2006
Location: Stockholm, Sweden
Old 06-26-2009 , 21:41   Optimizing - Remove dropped weapons
Reply With Quote #1

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...
__________________
atomen is offline
Send a message via MSN to atomen
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-26-2009 , 22:01   Re: Optimizing - Remove dropped weapons
Reply With Quote #2

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
atomen
Veteran Member
Join Date: Oct 2006
Location: Stockholm, Sweden
Old 06-27-2009 , 13:14   Re: Optimizing - Remove dropped weapons
Reply With Quote #3

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?
__________________

Last edited by atomen; 06-27-2009 at 13:27.
atomen is offline
Send a message via MSN to atomen
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 06-27-2009 , 13:27   Re: Optimizing - Remove dropped weapons
Reply With Quote #4

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 );

__________________

Last edited by xPaw; 06-27-2009 at 14:32.
xPaw is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-27-2009 , 13:30   Re: Optimizing - Remove dropped weapons
Reply With Quote #5

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)

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
atomen
Veteran Member
Join Date: Oct 2006
Location: Stockholm, Sweden
Old 06-27-2009 , 14:20   Re: Optimizing - Remove dropped weapons
Reply With Quote #6

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?
__________________
atomen is offline
Send a message via MSN to atomen
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-27-2009 , 14:27   Re: Optimizing - Remove dropped weapons
Reply With Quote #7

@ xPaw

static szClassname[ 9 ]; // weaponbox

->

static szClassname[ 10 ]; // weaponbox
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 06-27-2009 , 14:32   Re: Optimizing - Remove dropped weapons
Reply With Quote #8

@ConnorMcLeod: my bad :d
@atomen: use my code
__________________
xPaw is offline
SchlumPF*
Veteran Member
Join Date: Mar 2007
Old 06-27-2009 , 14:39   Re: Optimizing - Remove dropped weapons
Reply With Quote #9

Quote:
Originally Posted by xPaw View Post
@atomen: use my code
but dont use FM_SetModel, use Ham_Spawn
__________________
SchlumPF* is offline
Send a message via ICQ to SchlumPF*
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 06-27-2009 , 14:47   Re: Optimizing - Remove dropped weapons
Reply With Quote #10

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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:22.


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