Hello. I am creating a speed api (I've started a thread in Suggestions/Requests as well because I couldn't find a way to do this, but I found one) and I am having some problems with it. Actually breaking ones.
Code:
#include < amxmodx >
#include < HamSandwich >
#include < fun >
#define MAX_PLUGINS 256
#define MAX_PLAYERS 33
new Float: g_Speed[ MAX_PLAYERS ][ MAX_PLUGINS ]
new Array: g_PluginOrderOnClient[ MAX_PLAYERS ]
new g_SpeedLocked[ MAX_PLAYERS ]
// dunno, need to copypasta from somewhere
new const Float: g_WeaponSpeed[ ] =
{
0.0, //CSW ID starts with 1 for CSW_P228, so we want that to work
250.0, //CSW_P228
0.0, //no idea
250.0, //CSW_SCOUT
250.0, //CSW_HEGRENADE
250.0, //CSW_XM1014
250.0, //CSW_C4
250.0, //CSW_MAC10
250.0, //CSW_AUG
250.0, //CSW_SMOKEGRENADE
250.0, //CSW_ELITE
250.0, //CSW_FIVESEVEN
250.0, //CSW_UMP45
250.0, //CSW_SG550
250.0, //CSW_GALIL
250.0, //CSW_FAMAS
250.0, //CSW_USP
250.0, //CSW_GLOCK18
250.0, //CSW_AWP
250.0, //CSW_MP5NAVY
250.0, //CSW_M249
250.0, //CSW_M3
250.0, //CSW_M4A1
250.0, //CSW_TMP
250.0, //CSW_G3SG1
250.0, //CSW_FLASHBANG
250.0, //CSW_DEAGLE
250.0, //CSW_SG552
250.0, //CSW_AK47
250.0, //CSW_KNIFE
250.0 //CSW_P90
}
enum Speed
{
Speed_Overwrite,
Speed_Multiply,
Speed_Reset
}
public plugin_init( )
{
for( new i; i < MAX_PLAYERS; i++ )
g_PluginOrderOnClient[ i ] = ArrayCreate( )
RegisterHam( Ham_Item_PreFrame, "player", "_Ham_Item_PreFrame" )
}
public _Ham_Item_PreFrame( id )
if( is_user_alive( id ) )
Recount( id )
public plugin_natives( )
{
register_library( "speedset" )
register_native( "Speed_Set", "_Speed_Set" )
register_native( "Speed_ForceChangeAbility", "_Speed_ForceChangeAbility" )
}
public _Speed_ForceChangeAbility( Plugin, Params )
{
if( Params != 2 )
return 0
new id = get_param( 1 )
g_SpeedLocked[ id ] = get_param( 2 ) ? Plugin : 0
return 1
}
public _Speed_Set( Plugin, Params )
{
if( Params < 1 || Params > 3 )
return 0
new id = get_param( 1 ), Speed: type = Speed: get_param( 2 ), Float: arg
if( g_SpeedLocked[ id ] && g_SpeedLocked[ id ] != Plugin )
{
new PluginName[ 32 ]
get_plugin( g_SpeedLocked[ id ], PluginName, charsmax( PluginName ) )
log_amx( "Changing this players' speed is currently locked by plugin: %s. Only plugin available to change the speed is the one that locked it.", PluginName )
return 0
}
if( !is_user_alive( id ) )
return 0
if( Params == 3 )
arg = Float: get_param( 3 )
else
type = Speed_Reset
if( arg < 0.0 )
return 0
switch( type )
{
case Speed_Overwrite:
{
g_Speed[ id ][ Plugin ] = arg
new aId = ArrayInArray( g_PluginOrderOnClient[ id ], Plugin )
if( aId )
ArrayDeleteItem( g_PluginOrderOnClient[ id ], aId )
ArrayPushCell( g_PluginOrderOnClient[ id ], Plugin )
}
case Speed_Multiply: g_Speed[ id ][ Plugin ] = arg * ( -1.0 ) // to determine whether the plugin has multiplied the speed or overwritten it
case Speed_Reset:
{
g_Speed[ id ][ Plugin ] = 0.0
new aId = ArrayInArray( g_PluginOrderOnClient[ id ], Plugin )
if( aId )
ArrayDeleteItem( g_PluginOrderOnClient[ id ], aId )
ExecuteHamB( Ham_Item_PreFrame, id )
return 1
}
}
Recount( id )
return 1
}
public Recount( id )
{
new size = ArraySize( g_PluginOrderOnClient[ id ] )
new LastArrCell = size ? ArrayGetCell( g_PluginOrderOnClient[ id ], size - 1 ) : 0
new Float: speed = LastArrCell ? g_Speed[ id ][ LastArrCell ] : g_WeaponSpeed[ get_user_weapon( id ) ]
for( new i; i < MAX_PLUGINS; i++ )
{
if( !g_Speed[ id ][ i ] )
continue
if( g_Speed[ id ][ i ] < 0 )
speed *= -1.0 * g_Speed[ id ][ i ]
}
set_user_maxspeed( id, speed )
}
ArrayInArray( Array: Which, item )
{
new iitem
for( new i; i < ArraySize( Which ); i++ )
{
iitem = ArrayGetCell( Which, i )
if( item == iitem )
return i
}
return 0
}
The highlighted lines don't work correctly. First, I'll explain what I'm trying to achieve with this:
- Create a speed API that makes plugins not interfere with themselves
- Once a plugin has set a speed, it has the ability to lock the speed so other plugins don't overwrite it
- If a plugin sets a speed with Speed_Overwrite and another plugin does the same, once the other plugin uses Speed_Reset, the speed gets reverted back to the value before
- Speed_Multiply multiplies the current value of speed, gets counted together by all plugins and is cancellable only with Speed_Reset, while preventable with locking the speed by any other plugin
- Every plugin can use only one speed type at the same time, if you use Speed_Overwrite and then Speed_Multiply without resetting the speed first with Speed_Reset, the speed value will get overwritten
What doesn't work: The speed doesn't get reverted back, it resets back to the weapon speed as if the array was empty
There's quite a few more errors that break the API's functionality, but I will explain them once I get this stuff sorted.
Thank you.