Issue with Hooking PM_AirMove for changing user air-acceleration on ReAPI

Сообщения
15
Реакции
0
Can anyone assist me out with this issue? I've talked extensively about this on these links:

Custom Air-Accelerate Implementation on ReHLDS. - AlliedModders

This is the plugin that deals with custom-airacceleration:
AlliedModders - View Single Post - [NATIVE] Player AirAccelerate

Thanks and best regards,
MaNaReaver.
 
Сообщения
15
Реакции
0
If you're talking about the code, this is what I tried using. Here, the set_movevar function is hooked to set a custom-airacceleration for users who've typed "/ aa", but the issue is that the custom-airacceleration causes chokes and client misprediction due to clashes with server-airacceleration. I also worked on updating this plugin to include moveVars enum to call SVC_NEWMOVEVARS, and also tried reapi's RegisterHookChain and UnregisterHookChain to replicate what Arkshine's Orpheu + HLDS Plugin did: AlliedModders - View Single Post - [orpheu] Set airaccelerate

I'd suggest you read the above links for more information regarding this problem.

Код:
#include <amxmodx>
#include <reapi>

new Float: g_fPlayerCustomAA [33]

public plugin_init ()
{
    register_plugin ("Custom Player AirAccelerate", "1.0", "Huehue aka AJW1337 //")
    RegisterHookChain (RG_PM_AirMove, "PM_AirMove", false)
    register_clcmd ("say /aa", "ToggleAirAccelerateMenu")
}

public client_connect (id) g_fPlayerCustomAA [id] = 10.0

public PM_AirMove (id) set_movevar (mv_airaccelerate, g_fPlayerCustomAA [id]) // The problem lies here

public ToggleAirAccelerateMenu (id)
{
    new szTitle [128]
    formatex (szTitle, charsmax (szTitle), "\wAiraccelerate Menu ^n \dCurrent Airaccelerate: \r%.f", g_fPlayerCustomAA [id])
    new iMenu = menu_create (szTitle, "AirAccelerate_Menu_Handler")
    menu_additem (iMenu, "Set \y10 \wAiraccelerate")
    menu_additem (iMenu, "Set \y100 \wAiraccelerate")
    menu_display (id, iMenu, 0)
}

public AirAccelerate_Menu_Handler (id, iMenu, Item)
{
    if (Item == MENU_EXIT)
    {
        menu_destroy (iMenu)
        return PLUGIN_HANDLED
    }

    switch (++ Item)
    {
        case 1: g_fPlayerCustomAA [id] = 10.0
        case 2: g_fPlayerCustomAA [id] = 100.0
    }

    client_print (id, print_chat, "* You have changed your airaccelerate to %.0f", g_fPlayerCustomAA [id])
    menu_destroy (iMenu)
    return PLUGIN_HANDLED
}
 
Последнее редактирование:
Сообщения
1,701
Реакции
1,512
Помог
26 раз(а)
Try to send SVC_NEWMOVEVARS.
 
Сообщения
15
Реакции
0
This didn't work, what am I doing wrong here :(
Код:
#include <amxmodx>
#include <reapi>

new Float:g_fPlayerCustomAA[33]

enum Movevars
{
    Float:Gravity,
    Float:StopSpeed,
    Float:MaxSpeed,
    Float:SpectatorMaxSpeed,
    Float:Accelerate,
    Float:AirAccelerate,
    Float:WaterAccelerate,
    Float:Friction,
    Float:EdgeFriction,
    Float:WaterFriction,
    Float:EntGravity,
    Float:Bounce,
    Float:StepSize,
    Float:MaxVelocity,
    Float:ZMax,
    Float:WaveHeight,
    Footsteps,
    Float:RollAngle,
    Float:RollSpeed,
    Float:SkyColorRed,
    Float:SkyColorGreen,
    Float:SkyColorBlue,
    Float:SkyVecX,
    Float:SkyVecY,
    Float:SkyVecZ,
    SkyName[ 16 ],
};
new MoveVarsData[ Movevars ];

public plugin_init()
{
    register_plugin("Custom Player AirAccelerate", "1.0", "Huehue a.k.a AJW1337//")
    handleCache();
    RegisterHookChain(RG_PM_AirMove, "PM_AirMove", false)
    register_clcmd("say /aa", "ToggleAirAccelerateMenu")
}

public client_connect(id)
{
    g_fPlayerCustomAA[id] = 10.0
}

public PM_AirMove(id)
{
    set_movevar(mv_airaccelerate, g_fPlayerCustomAA[id])
}

public ToggleAirAccelerateMenu(id)
{
    new szTitle[128]
    formatex(szTitle, charsmax(szTitle), "\wAiraccelerate Menu^n\dCurrent Airaccelerate: \r%.f", g_fPlayerCustomAA[id])
    new iMenu = menu_create(szTitle, "AirAccelerate_Menu_Handler")
    menu_additem(iMenu, "Set \y10 \wAiraccelerate")
    menu_additem(iMenu, "Set \y100 \wAiraccelerate")
    menu_display(id, iMenu, 0)
}

public AirAccelerate_Menu_Handler(id, iMenu, Item)
{
    if(Item == MENU_EXIT)
    {
        menu_destroy(iMenu)
        return PLUGIN_HANDLED
    }

    switch(++Item)
    {
        case 1:
        {
            g_fPlayerCustomAA[id] = 10.0;
        }
        case 2:
        {
            g_fPlayerCustomAA[id] = 100.0;
        }
    }
    sendNewMoveVars(id);
    client_print(id, print_chat, "* You have changed your airaccelerate to %.0f", g_fPlayerCustomAA[id])

    menu_destroy(iMenu)
    return PLUGIN_HANDLED
}

public handleCache()
{
    MoveVarsData[ Gravity ] = get_cvar_float("sv_gravity");
    MoveVarsData[ StopSpeed ] = get_cvar_float("sv_stopspeed");
    MoveVarsData[ MaxSpeed ] = get_cvar_float("sv_maxspeed");
    MoveVarsData[ SpectatorMaxSpeed ] = get_cvar_float("sv_spectatormaxspeed");
    MoveVarsData[ Accelerate ] = get_cvar_float("sv_accelerate");
    MoveVarsData[ AirAccelerate ] = get_cvar_float("sv_airaccelerate");
    MoveVarsData[ WaterAccelerate ] = get_cvar_float("sv_wateraccelerate");
    MoveVarsData[ Friction ] = get_cvar_float("sv_friction");
    MoveVarsData[ EdgeFriction ] = get_cvar_float("edgefriction");
    MoveVarsData[ WaterFriction ] = get_cvar_float("sv_waterfriction");
    MoveVarsData[ Bounce ] = get_cvar_float("sv_bounce");
    MoveVarsData[ StepSize ] = get_cvar_float("sv_stepsize");
    MoveVarsData[ MaxVelocity ] = get_cvar_float("sv_maxvelocity");
    MoveVarsData[ ZMax ] = get_cvar_float("sv_zmax");
    MoveVarsData[ WaveHeight ] = get_cvar_float("sv_wateramp");
    MoveVarsData[ Footsteps ] = get_cvar_num("mp_footsteps");
    MoveVarsData[ SkyColorRed ] = get_cvar_float("sv_skycolor_r");
    MoveVarsData[ SkyColorGreen ] = get_cvar_float("sv_skycolor_g");
    MoveVarsData[ SkyColorBlue ] = get_cvar_float("sv_skycolor_b");
    MoveVarsData[ SkyVecX ] = get_cvar_float("sv_skyvec_x");
    MoveVarsData[ SkyVecY ] = get_cvar_float("sv_skyvec_y");
    MoveVarsData[ SkyVecZ ] = get_cvar_float("sv_skyvec_z");
    get_cvar_string("sv_skyname", MoveVarsData[ SkyName ], charsmax( MoveVarsData[ SkyName ]));

    MoveVarsData[ EntGravity ] = _:1.0;
    MoveVarsData[ RollAngle  ] = _:0.0;
    MoveVarsData[ RollSpeed  ] = _:0.0;
}

public sendNewMoveVars(id)
{
    message_begin( MSG_ONE, SVC_NEWMOVEVARS, _, id);
    {
        write_long( _:MoveVarsData[ Gravity           ] );
        write_long( _:MoveVarsData[ StopSpeed         ] );
        write_long( _:MoveVarsData[ MaxSpeed          ] );
        write_long( _:MoveVarsData[ SpectatorMaxSpeed ] );
        write_long( _:MoveVarsData[ Accelerate        ] );
        write_long( _:g_fPlayerCustomAA[ id           ] );
        write_long( _:MoveVarsData[ WaterAccelerate   ] );
        write_long( _:MoveVarsData[ Friction          ] );
        write_long( _:MoveVarsData[ EdgeFriction      ] );
        write_long( _:MoveVarsData[ WaterFriction     ] );
        write_long( _:MoveVarsData[ EntGravity        ] );
        write_long( _:MoveVarsData[ Bounce            ] );
        write_long( _:MoveVarsData[ StepSize          ] );
        write_long( _:MoveVarsData[ MaxVelocity       ] );
        write_long( _:MoveVarsData[ ZMax              ] );
        write_long( _:MoveVarsData[ WaveHeight        ] );
        write_byte( !!MoveVarsData[ Footsteps         ] );
        write_long( _:MoveVarsData[ RollAngle         ] );
        write_long( _:MoveVarsData[ RollSpeed         ] );
        write_long( _:MoveVarsData[ SkyColorBlue      ] );
        write_long( _:MoveVarsData[ SkyColorGreen     ] );
        write_long( _:MoveVarsData[ SkyColorRed       ] );
        write_long( _:MoveVarsData[ SkyVecX           ] );
        write_long( _:MoveVarsData[ SkyVecY           ] );
        write_long( _:MoveVarsData[ SkyVecZ           ] );
        write_string( MoveVarsData[ SkyName           ] );
    }
    message_end();
}
 
Сообщения
1,701
Реакции
1,512
Помог
26 раз(а)
Сообщения
2,491
Реакции
2,794
Помог
61 раз(а)
Код:
#include <amxmodx>
#include <reapi>

new MenuId;
new bool:AirAccelerateChanged = false; // Very small optimization. Can be removed
new Float:AirAccelerate;
new Float:PlayerAirAccelerate[MAX_CLIENTS + 1];

public plugin_init() {
    register_clcmd ("say /aa", "CmdMenu");

    // IMPORTANT !!!!!
    // Need to be checked if this message can be hooked.
    // I think no.
    // In this case the better way is to create SV_WriteMovevarsToClient hook in reapi
    // But we also can hook CVAR and send custom message
    register_message(SVC_NEWMOVEVARS, "MsgHookMoveVars");

    RegisterHookChain (RG_PM_AirMove, "PM_AirMove_Pre", false);
    // You need to restore original movevar.
    // If you don't do it, you will pass this condition
    // https://github.com/dreamstalker/rehlds/blob/98db4672cf90f82052888ae5a5c0fb71a7298dce/rehlds/engine/sv_main.cpp#L1042
    // And spam SVC_NEWMOVEVARS to every client
    RegisterHookChain (RG_PM_AirMove, "PM_AirMove_Post", true);
    
    // Binding sv_airaccelerate CVAR give to us possibility to have actual value
    bind_pcvar_float(get_cvar_pointer("sv_airaccelerate"), AirAccelerate);
    
    // You need to register callback only once to prevent memory leak
    new menuCallback = menu_makecallback("MenuCallback");
    
    // Using one global menu is better than create new one every time
    MenuId = menu_create("\wAiraccelerate Menu", "MenuHandler");
    menu_additem(MenuId, "Set \y10 \wAiraccelerate", "10", .callback = menuCallback);
    menu_additem(MenuId, "Set \y100 \wAiraccelerate", "100", .callback = menuCallback);
}

public client_putinserver(id) {
    // 0.0 means that we don't change Airaccelerate
    PlayerAirAccelerate[id] = 0.0;
}

public CmdMenu(id) {
    menu_display(id, MenuId);
    return PLUGIN_HANDLED_MAIN;
}

public MenuHandler(const id, const menu, const item) {
    if (item == MENU_EXIT) {
        return PLUGIN_HANDLED;
    }
    
    new data[5];
    menu_item_getinfo(menu, item, .info = data, .infolen = charsmax(data));
    PlayerAirAccelerate[id] = float(str_to_num(data));
    client_print(id, print_chat, "* You have changed your airaccelerate to %.0f", PlayerAirAccelerate[id]);
    return PLUGIN_HANDLED;
}

public MenuCallback(const id, const menu, const item) {
    if (PlayerAirAccelerate[id] == 0.0) {
        // If we don't change lets enable item
        return ITEM_ENABLED;
    }
    
    new data[5];
    menu_item_getinfo(menu, item, .info = data, .infolen = charsmax(data));
    
    // Enable or disable item if it's actual value of player
    return float(str_to_num(data)) != PlayerAirAccelerate[id] ? ITEM_ENABLED : ITEM_DISABLED;
}

public MsgHookMoveVars(const msgId, const msgDest, const msgEnt) {
    enum { arg_air_accelerate = 6 };
    // If we don't change or change to default one skip
    if (PlayerAirAccelerate[msgEnt] == 0.0 || PlayerAirAccelerate[msgEnt] == AirAccelerate) {
        return PLUGIN_CONTINUE;
    }

    // Replace actual one with our custom value
    set_msg_arg_float(arg_air_accelerate, ARG_ANGLE, PlayerAirAccelerate[msgEnt]);
    return PLUGIN_CONTINUE;
}

public PM_AirMove_Pre(id) {
    if (PlayerAirAccelerate[id] == 0.0 || PlayerAirAccelerate[id] == AirAccelerate) {
        // We don't change a value. Very small optimization
        AirAccelerateChanged = false;
        return HC_CONTINUE;
    }

    set_movevar(mv_airaccelerate, PlayerAirAccelerate[id]);
    AirAccelerateChanged = true;
    return HC_CONTINUE;
}

public PM_AirMove_Post(id) {
    // If we changed  movevar lets restore to default value
    if (AirAccelerateChanged) {
        set_movevar(mv_airaccelerate, AirAccelerate);
    }
}
 
Сообщения
15
Реакции
0
Thank you so much, this works amazingly well! Although there's just a small bit of lag, I consider it a great improvement over previous works on ReAPI. I had a small question regarding further optimization of the plugin. Do you think adding an UnregisterHookChain when no one is using /aa would help reduce the server-load and lag even more, or the if check doesn't matter much in the long run?
 

Пользователи, просматривающие эту тему

Сейчас на форуме нет ни одного пользователя.
Сверху Снизу