Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Added rg_set/get_global_iteminfo natives #279

Merged
merged 10 commits into from
Sep 5, 2023
22 changes: 22 additions & 0 deletions reapi/extra/amxmodx/scripting/include/reapi_gamedll.inc
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,28 @@ native rg_set_iteminfo(const entity, ItemInfo:type, any:...);
*/
native rg_get_iteminfo(const ent, ItemInfo:type, any:...);

/**
* Sets a parameter of the global CBasePlayerItem::m_ItemInfoArray array
* @note To have effect on client side (i.g. ammo size on HUD) you should
* alter this value BEFORE WeaponList message is sent to client, or
* force it's alteration by sending again to the specific client.
* Hooking WeaponList message with AMXX's register_message is a choice.
*
* @param weapon_id Weapon id, see WEAPON_* constants
* @param type Item info type. See ItemInfo constants.
*
*/
native rg_set_global_iteminfo(const {WeaponIdType,_}:weapon_id, ItemInfo:type, any:...);

/**
* Gets a parameter of the global CBasePlayerItem::m_ItemInfoArray array
*
* @param weapon_id Weapon id, see WEAPON_* constants
* @param type Item info type. See ItemInfo constants.
*
*/
native rg_get_global_iteminfo(const {WeaponIdType,_}:weapon_id, ItemInfo:type, any:...);

/*
* Adds hint message to the queue.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ enum WpnInfo
};

/**
* Item's info types for use with rg_set_iteminfo/rg_get_iteminfo()
* Item's info types for use with rg_set_[global_]iteminfo/rg_get_[global_]iteminfo()
*/
enum ItemInfo
{
Expand Down
132 changes: 132 additions & 0 deletions reapi/src/natives/natives_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,136 @@ cell AMX_NATIVE_CALL rg_get_iteminfo(AMX *amx, cell *params)
return TRUE;
}

/**
* Sets a parameter of the global CBasePlayerItem::m_ItemInfoArray array
* @note To have effect on client side (i.g. ammo size on HUD) you should
* alter this value BEFORE WeaponList message is sent to client, or
* force it's alteration by sending again to the specific client.
* Hooking WeaponList message with AMXX's register_message is a choice.
*
* @param weapon_id Weapon id, see WEAPON_* constants
* @param type Item info type. See ItemInfo constants.
*
* native rg_set_global_iteminfo(const {WeaponIdType,_}:weapon_id, ItemInfo:type, any:...);
*/
cell AMX_NATIVE_CALL rg_set_global_iteminfo(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_weapon_id, arg_type, arg_value };

WeaponIdType weaponId = static_cast<WeaponIdType>(params[arg_weapon_id]);
if (!GetWeaponInfoRange(weaponId, false))
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid weapon id %i", __FUNCTION__, weaponId);
return FALSE;
}

ItemInfo* II = g_ReGameApi->GetItemInfo(weaponId);

char itembuf[256];
cell *ptr = getAmxAddr(amx, params[arg_value]);

ItemInfo_e type = static_cast<ItemInfo_e>(params[arg_type]);
switch (type)
{
case ItemInfo_iSlot: II->iSlot = *ptr; break;
case ItemInfo_iPosition: II->iPosition = *ptr; break;
case ItemInfo_iMaxAmmo1: II->iMaxAmmo1 = *ptr; break;
case ItemInfo_iMaxAmmo2: II->iMaxAmmo2 = *ptr; break;
case ItemInfo_iMaxClip: II->iMaxClip = *ptr; break;
case ItemInfo_iId: II->iId = *ptr; break;
case ItemInfo_iFlags: II->iFlags = *ptr; break;
case ItemInfo_iWeight: II->iWeight = *ptr; break;
case ItemInfo_pszAmmo1: II->pszAmmo1 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszAmmo2: II->pszAmmo2 = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;
case ItemInfo_pszName: II->pszName = STRING(getAmxStringAlloc(amx, params[arg_value], itembuf)); break;

default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "Unknown ItemInfo type %d", type);
return FALSE;
}

return TRUE;
}

/**
* Gets a parameter of the global CBasePlayerItem::m_ItemInfoArray array
*
* @param weapon_id Weapon id, see WEAPON_* constants
* @param type Item info type. See ItemInfo constants.
*
* native rg_get_global_iteminfo(const {WeaponIdType,_}:weapon_id, ItemInfo:type, any:...);
*/
cell AMX_NATIVE_CALL rg_get_global_iteminfo(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_weapon_id, arg_type, arg_output, arg_length };

WeaponIdType weaponId = static_cast<WeaponIdType>(params[arg_weapon_id]);
if (!GetWeaponInfoRange(weaponId, false))
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid weapon id %i", __FUNCTION__, weaponId);
return FALSE;
}

ItemInfo_e type = static_cast<ItemInfo_e>(params[arg_type]);
if ((type == ItemInfo_pszAmmo1 || type == ItemInfo_pszAmmo2 || type == ItemInfo_pszName) && PARAMS_COUNT != 4)
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "Bad arg count. Expected %d, got %d.", 4, PARAMS_COUNT);
return FALSE;
}

ItemInfo* II = g_ReGameApi->GetItemInfo(weaponId);

cell *dest = getAmxAddr(amx, params[arg_output]);
size_t length = (PARAMS_COUNT == 4) ? *getAmxAddr(amx, params[arg_length]) : 0;

switch (type)
{
case ItemInfo_iSlot: return II->iSlot;
case ItemInfo_iPosition: return II->iPosition;
case ItemInfo_iMaxAmmo1: return II->iMaxAmmo1;
case ItemInfo_iMaxAmmo2: return II->iMaxAmmo2;
case ItemInfo_iMaxClip: return II->iMaxClip;
case ItemInfo_iId: return II->iId;
case ItemInfo_iFlags: return II->iFlags;
case ItemInfo_iWeight: return II->iWeight;
case ItemInfo_pszAmmo1:
{
if (II->pszAmmo1 == nullptr) {
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszAmmo1, length);
break;
}
case ItemInfo_pszAmmo2:
{
if (II->pszAmmo2 == nullptr) {
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszAmmo2, length);
break;
}
case ItemInfo_pszName:
{
if (II->pszName == nullptr) {
setAmxString(dest, "", 1);
break;
}

setAmxString(dest, II->pszName, length);
break;
}
default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "Unknown ItemInfo type %d", type);
return FALSE;
}

return TRUE;
}

/*
* Adds hint message to the queue.
*
Expand Down Expand Up @@ -2554,6 +2684,8 @@ AMX_NATIVE_INFO Misc_Natives_RG[] =

{ "rg_set_iteminfo", rg_set_iteminfo },
{ "rg_get_iteminfo", rg_get_iteminfo },
{ "rg_set_global_iteminfo", rg_set_global_iteminfo },
{ "rg_get_global_iteminfo", rg_get_global_iteminfo },

{ "rg_hint_message", rg_hint_message },

Expand Down
Loading