Add MenuSetClientMapping native.

This commit is contained in:
BotoX 2019-09-05 08:43:33 +02:00
parent d67d6321ad
commit 5ac1379b67
5 changed files with 77 additions and 12 deletions

View File

@ -607,7 +607,7 @@ bool BaseMenuStyle::RedoClientMenu(int client, ItemOrder order)
CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) : CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) :
m_pStyle(pStyle), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false), m_pStyle(pStyle), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false),
m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false), m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false),
m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT), m_RandomMapLen(0) m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT)
{ {
} }
@ -684,7 +684,7 @@ const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =
if (position >= m_items.length()) if (position >= m_items.length())
return NULL; return NULL;
if (client > 0 && position < m_RandomMapLen) if (client > 0 && position < m_RandomMaps[client].length())
{ {
position = m_RandomMaps[client][position]; position = m_RandomMaps[client][position];
} }
@ -701,19 +701,19 @@ const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =
void CBaseMenu::ShufflePerClient(int start, int stop) void CBaseMenu::ShufflePerClient(int start, int stop)
{ {
// limit map len to 255 items since it's using uint8 // limit map len to 255 items since it's using uint8
m_RandomMapLen = MIN(GetItemCount(), 255); int length = MIN(GetItemCount(), 255);
if(stop >= 0) if (stop >= 0)
m_RandomMapLen = MIN(m_RandomMapLen, (unsigned int)stop); length = MIN(length, (unsigned int)stop);
for(int i = 1; i < SM_MAXPLAYERS + 1; i++) for (int i = 1; i < SM_MAXPLAYERS + 1; i++)
{ {
// populate per-client map ... // populate per-client map ...
m_RandomMaps[i].resize(m_RandomMapLen); m_RandomMaps[i].resize(length);
for(unsigned int j = 0; j < m_RandomMapLen; j++) for (unsigned int j = 0; j < length; j++)
m_RandomMaps[i][j] = j; m_RandomMaps[i][j] = j;
// ... and random shuffle it // ... and random shuffle it
for(int j = m_RandomMapLen - 1; j > start; j--) for (int j = length - 1; j > start; j--)
{ {
int x = rand() % (j - start + 1) + start; int x = rand() % (j - start + 1) + start;
uint8_t tmp = m_RandomMaps[i][x]; uint8_t tmp = m_RandomMaps[i][x];
@ -723,14 +723,24 @@ void CBaseMenu::ShufflePerClient(int start, int stop)
} }
} }
void CBaseMenu::SetClientMapping(int client, int *array, int length)
{
length = MIN(length, 255);
m_RandomMaps[client].resize(length);
for (int i = 0; i < length; i++)
{
m_RandomMaps[client][i] = array[i];
}
}
bool CBaseMenu::IsPerClientShuffled() bool CBaseMenu::IsPerClientShuffled()
{ {
return m_RandomMapLen > 0; return m_RandomMaps[1].length() > 0;
} }
unsigned int CBaseMenu::GetRealItemIndex(int client, unsigned int position) unsigned int CBaseMenu::GetRealItemIndex(int client, unsigned int position)
{ {
if (client > 0 && position < m_RandomMapLen) if (client > 0 && position < m_RandomMaps[client].length())
{ {
position = m_RandomMaps[client][position]; position = m_RandomMaps[client][position];
return m_items[position].index; return m_items[position].index;

View File

@ -157,6 +157,7 @@ public:
virtual void SetMenuOptionFlags(unsigned int flags); virtual void SetMenuOptionFlags(unsigned int flags);
virtual IMenuHandler *GetHandler(); virtual IMenuHandler *GetHandler();
virtual void ShufflePerClient(int start, int stop); virtual void ShufflePerClient(int start, int stop);
virtual void SetClientMapping(int client, int *array, int length);
virtual bool IsPerClientShuffled(); virtual bool IsPerClientShuffled();
virtual unsigned int GetRealItemIndex(int client, unsigned int position); virtual unsigned int GetRealItemIndex(int client, unsigned int position);
unsigned int GetBaseMemUsage(); unsigned int GetBaseMemUsage();
@ -175,7 +176,6 @@ protected:
Handle_t m_hHandle; Handle_t m_hHandle;
IMenuHandler *m_pHandler; IMenuHandler *m_pHandler;
unsigned int m_nFlags; unsigned int m_nFlags;
unsigned int m_RandomMapLen;
ke::Vector<uint8_t> m_RandomMaps[SM_MAXPLAYERS+1]; ke::Vector<uint8_t> m_RandomMaps[SM_MAXPLAYERS+1];
}; };

View File

@ -862,6 +862,33 @@ static cell_t MenuShufflePerClient(IPluginContext *pContext, const cell_t *param
return 1; return 1;
} }
static cell_t MenuSetClientMapping(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IBaseMenu *menu;
if ((err = ReadMenuHandle(params[1], &menu)) != HandleError_None)
{
return pContext->ThrowNativeError("Menu handle %x is invalid (error %d)", hndl, err);
}
int client = params[2];
if (client < 1 || client > SM_MAXPLAYERS)
{
return pContext->ThrowNativeError("Invalid client index!");
}
cell_t *array;
pContext->LocalToPhysAddr(params[3], &array);
int length = params[4];
menu->SetClientMapping(client, array, length);
return 1;
}
static cell_t SetMenuPagination(IPluginContext *pContext, const cell_t *params) static cell_t SetMenuPagination(IPluginContext *pContext, const cell_t *params)
{ {
Handle_t hndl = (Handle_t)params[1]; Handle_t hndl = (Handle_t)params[1];
@ -1676,6 +1703,7 @@ REGISTER_NATIVES(menuNatives)
{"SetVoteResultCallback", SetVoteResultCallback}, {"SetVoteResultCallback", SetVoteResultCallback},
{"VoteMenu", VoteMenu}, {"VoteMenu", VoteMenu},
{"MenuShufflePerClient", MenuShufflePerClient}, {"MenuShufflePerClient", MenuShufflePerClient},
{"MenuSetClientMapping", MenuSetClientMapping},
{"SetMenuNoVoteButton", SetMenuNoVoteButton}, {"SetMenuNoVoteButton", SetMenuNoVoteButton},
// Transitional syntax support. // Transitional syntax support.
@ -1705,6 +1733,7 @@ REGISTER_NATIVES(menuNatives)
{"Menu.Cancel", CancelMenu}, {"Menu.Cancel", CancelMenu},
{"Menu.DisplayVote", VoteMenu}, {"Menu.DisplayVote", VoteMenu},
{"Menu.ShufflePerClient", MenuShufflePerClient}, {"Menu.ShufflePerClient", MenuShufflePerClient},
{"Menu.SetClientMapping", MenuSetClientMapping},
{"Menu.Pagination.get", GetMenuPagination}, {"Menu.Pagination.get", GetMenuPagination},
{"Menu.Pagination.set", SetMenuPagination}, {"Menu.Pagination.set", SetMenuPagination},
{"Menu.OptionFlags.get", GetMenuOptionFlags}, {"Menu.OptionFlags.get", GetMenuOptionFlags},

View File

@ -318,6 +318,13 @@ methodmap Menu < Handle
// @param stop Menu item index to stop randomizing at. -1 = infinite // @param stop Menu item index to stop randomizing at. -1 = infinite
public native void ShufflePerClient(int start=0, int stop=-1); public native void ShufflePerClient(int start=0, int stop=-1);
// Fills the client vote option mapping with user supplied values.
//
// @param client Client index.
// @param array Integer array with mapping.
// @param length Length of array.
public native void SetClientMapping(int client, int[] array, int length);
// Sets the menu's default title/instruction message. // Sets the menu's default title/instruction message.
// //
// @param fmt Message string format // @param fmt Message string format
@ -560,11 +567,22 @@ native bool GetMenuItem(Handle menu,
/** /**
* Generates a per-client random mapping for the current vote options. * Generates a per-client random mapping for the current vote options.
* *
* @param menu Menu Handle.
* @param start Menu item index to start randomizing from. * @param start Menu item index to start randomizing from.
* @param stop Menu item index to stop randomizing at. -1 = infinite * @param stop Menu item index to stop randomizing at. -1 = infinite
*/ */
native void MenuShufflePerClient(Handle menu, int start=0, int stop=-1); native void MenuShufflePerClient(Handle menu, int start=0, int stop=-1);
/*
* Fills the client vote option mapping with user supplied values.
*
* @param menu Menu Handle.
* @param client Client index.
* @param array Integer array with mapping.
* @param length Length of array.
*/
native void MenuSetClientMapping(Handle menu, int client, int[] array, int length);
/** /**
* Returns the first item on the page of a currently selected menu. * Returns the first item on the page of a currently selected menu.
* *

View File

@ -645,6 +645,14 @@ namespace SourceMod
*/ */
virtual void ShufflePerClient(int start=0, int stop=-1) =0; virtual void ShufflePerClient(int start=0, int stop=-1) =0;
/**
* @brief Fills the client vote option mapping with user supplied values.
* @param client Client index.
* @param array Integer array with mapping.
* @param length Length of array.
*/
virtual void SetClientMapping(int client, int *array, int length) =0;
/** /**
* @brief Returns true if the menu has a per-client random mapping. * @brief Returns true if the menu has a per-client random mapping.
* @return True on success, false otherwise. * @return True on success, false otherwise.