diff --git a/src/Entity.c b/src/Entity.c index 5b6633c85..b8fa58990 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -686,33 +686,21 @@ void Entities_DrawShadows(void) { *#########################################################################################################################*/ struct _TabListData TabList; -bool TabList_Valid(EntityID id) { - return TabList.PlayerNames[id] || TabList.ListNames[id] || TabList.GroupNames[id]; -} +static void TabList_Delete(EntityID id) { + int i, index; + index = TabList.NameOffsets[id]; + if (!index) return; -static void TabList_RemoveAt(int index) { - int i; - StringsBuffer_Remove(&TabList.Buffer, index); + StringsBuffer_Remove(&TabList._buffer, index - 1); + StringsBuffer_Remove(&TabList._buffer, index - 2); + StringsBuffer_Remove(&TabList._buffer, index - 3); for (i = 0; i < TABLIST_MAX_NAMES; i++) { - if (TabList.PlayerNames[i] == index) { TabList.PlayerNames[i] = 0; } - if (TabList.PlayerNames[i] > index) { TabList.PlayerNames[i]--; } - - if (TabList.ListNames[i] == index) { TabList.ListNames[i] = 0; } - if (TabList.ListNames[i] > index) { TabList.ListNames[i]--; } - - if (TabList.GroupNames[i] == index) { TabList.GroupNames[i] = 0; } - if (TabList.GroupNames[i] > index) { TabList.GroupNames[i]--; } + if (TabList.NameOffsets[i] > index) TabList.NameOffsets[i] -= 3; } -} -static void TabList_Delete(EntityID id) { - if (!TabList_Valid(id)) return; - - TabList_RemoveAt(TabList.PlayerNames[id]); - TabList_RemoveAt(TabList.ListNames[id]); - TabList_RemoveAt(TabList.GroupNames[id]); - TabList.GroupRanks[id] = 0; + TabList.NameOffsets[id] = 0; + TabList.GroupRanks[id] = 0; } void TabList_Remove(EntityID id) { @@ -725,7 +713,7 @@ void TabList_Set(EntityID id, const String* player, const String* list, const St uint8_t oldRank; struct Event_Int* events; - if (TabList_Valid(id)) { + if (TabList.NameOffsets[id]) { oldPlayer = TabList_UNSAFE_GetPlayer(id); oldList = TabList_UNSAFE_GetList(id); oldGroup = TabList_UNSAFE_GetGroup(id); @@ -741,21 +729,20 @@ void TabList_Set(EntityID id, const String* player, const String* list, const St } TabList_Delete(id); - TabList.PlayerNames[id] = TabList.Buffer.count; StringsBuffer_Add(&TabList.Buffer, player); - TabList.ListNames[id] = TabList.Buffer.count; StringsBuffer_Add(&TabList.Buffer, list); - TabList.GroupNames[id] = TabList.Buffer.count; StringsBuffer_Add(&TabList.Buffer, group); - TabList.GroupRanks[id] = rank; + StringsBuffer_Add(&TabList._buffer, player); + StringsBuffer_Add(&TabList._buffer, list); + StringsBuffer_Add(&TabList._buffer, group); + TabList.NameOffsets[id] = TabList._buffer.count; + TabList.GroupRanks[id] = rank; Event_RaiseInt(events, id); } -static void TabList_Free(void) { StringsBuffer_Clear(&TabList.Buffer); } +static void TabList_Free(void) { StringsBuffer_Clear(&TabList._buffer); } static void TabList_Reset(void) { - Mem_Set(TabList.PlayerNames, 0, sizeof(TabList.PlayerNames)); - Mem_Set(TabList.ListNames, 0, sizeof(TabList.ListNames)); - Mem_Set(TabList.GroupNames, 0, sizeof(TabList.GroupNames)); + Mem_Set(TabList.NameOffsets, 0, sizeof(TabList.NameOffsets)); Mem_Set(TabList.GroupRanks, 0, sizeof(TabList.GroupRanks)); - StringsBuffer_Clear(&TabList.Buffer); + StringsBuffer_Clear(&TabList._buffer); } struct IGameComponent TabList_Component = { diff --git a/src/Entity.h b/src/Entity.h index 9cf5e83fd..5583144d4 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -151,28 +151,27 @@ void Entities_DrawShadows(void); #define TABLIST_MAX_NAMES 256 /* Data for all entries in tab list */ CC_VAR extern struct _TabListData { - /* Raw unformatted name (for Tab name auto complete) */ - uint16_t PlayerNames[TABLIST_MAX_NAMES]; - /* Formatted name for display in tab list. */ - uint16_t ListNames[TABLIST_MAX_NAMES]; - /* Name of the group this entry is in (e.g. rank name, map name) */ - uint16_t GroupNames[TABLIST_MAX_NAMES]; + /* Buffer indices for player/list/group names. */ + /* Use TabList_UNSAFE_GetPlayer/List/Group to get these names. */ + /* NOTE: An Offset of 0 means the entry is unused. */ + uint16_t NameOffsets[TABLIST_MAX_NAMES]; /* Position/Order of this entry within the group. */ uint8_t GroupRanks[TABLIST_MAX_NAMES]; - StringsBuffer Buffer; + StringsBuffer _buffer; } TabList; -/* Returns whether the tab list entry with the given ID is used at all. */ -CC_API bool TabList_Valid(EntityID id); /* Removes the tab list entry with the given ID, raising TabListEvents.Removed event. */ CC_API void TabList_Remove(EntityID id); /* Sets the data for the tab list entry with the given id. */ /* Raises TabListEvents.Changed if replacing, TabListEvents.Added if a new entry. */ CC_API void TabList_Set(EntityID id, const String* player, const String* list, const String* group, uint8_t rank); -#define TabList_UNSAFE_GetPlayer(id) StringsBuffer_UNSAFE_Get(&TabList.Buffer, TabList.PlayerNames[id]); -#define TabList_UNSAFE_GetList(id) StringsBuffer_UNSAFE_Get(&TabList.Buffer, TabList.ListNames[id]); -#define TabList_UNSAFE_GetGroup(id) StringsBuffer_UNSAFE_Get(&TabList.Buffer, TabList.GroupNames[id]); +/* Raw unformatted name (for Tab name auto complete) */ +#define TabList_UNSAFE_GetPlayer(id) StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets[id] - 3); +/* Formatted name for display in tab list. */ +#define TabList_UNSAFE_GetList(id) StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets[id] - 2); +/* Name of the group this entry is in (e.g. rank name, map name) */ +#define TabList_UNSAFE_GetGroup(id) StringsBuffer_UNSAFE_Get(&TabList._buffer, TabList.NameOffsets[id] - 1); /* Represents another entity in multiplayer */ struct NetPlayer { diff --git a/src/Widgets.c b/src/Widgets.c index 99a031a45..6d3f8cc55 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -1688,12 +1688,11 @@ static void ChatInputWidget_TabKey(struct InputWidget* w) { numMatches = 0; for (i = 0; i < TABLIST_MAX_NAMES; i++) { - EntityID id = (EntityID)i; - if (!TabList_Valid(id)) continue; + if (!TabList.NameOffsets[i]) continue; name = TabList_UNSAFE_GetPlayer(i); if (!String_CaselessContains(&name, &part)) continue; - matches[numMatches++] = id; + matches[numMatches++] = (EntityID)i; } if (numMatches == 1) { @@ -2079,7 +2078,7 @@ static void PlayerListWidget_Init(void* widget) { int id; for (id = 0; id < TABLIST_MAX_NAMES; id++) { - if (!TabList_Valid((EntityID)id)) continue; + if (!TabList.NameOffsets[id]) continue; PlayerListWidget_AddName(w, (EntityID)id, -1); }