Permalink
Browse files

minor code cleanup

  • Loading branch information...
UnknownShadow200 committed Jan 5, 2019
1 parent 015b72b commit 9cfb8fadf5e63e7292449542ae36a022459cd90f
Showing with 76 additions and 51 deletions.
  1. +23 −27 src/Entity.c
  2. +36 −11 src/Entity.h
  3. +9 −6 src/Inventory.h
  4. +1 −1 src/PacketHandlers.c
  5. +5 −4 src/Platform.h
  6. +2 −2 src/Widgets.c
@@ -383,39 +383,35 @@ void Entities_DrawShadows(void) {
/*########################################################################################################################*
*--------------------------------------------------------TabList----------------------------------------------------------*
*#########################################################################################################################*/
StringsBuffer TabList_Buffer;
uint16_t TabList_PlayerNames[TABLIST_MAX_NAMES];
uint16_t TabList_ListNames[TABLIST_MAX_NAMES];
uint16_t TabList_GroupNames[TABLIST_MAX_NAMES];
uint8_t TabList_GroupRanks[TABLIST_MAX_NAMES];
struct _TabListData TabList;

bool TabList_Valid(EntityID id) {
return TabList_PlayerNames[id] || TabList_ListNames[id] || TabList_GroupNames[id];
return TabList.PlayerNames[id] || TabList.ListNames[id] || TabList.GroupNames[id];
}

static void TabList_RemoveAt(int index) {
int i;
StringsBuffer_Remove(&TabList_Buffer, index);
StringsBuffer_Remove(&TabList.Buffer, index);

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.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.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.GroupNames[i] == index) { TabList.GroupNames[i] = 0; }
if (TabList.GroupNames[i] > index) { TabList.GroupNames[i]--; }
}
}

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_RemoveAt(TabList.PlayerNames[id]);
TabList_RemoveAt(TabList.ListNames[id]);
TabList_RemoveAt(TabList.GroupNames[id]);
TabList.GroupRanks[id] = 0;
}

void TabList_Remove(EntityID id) {
@@ -430,19 +426,19 @@ void TabList_Set(EntityID id, const String* player, const String* list, const St
String_AppendColorless(&colorlessName, player);
TabList_Delete(id);

TabList_PlayerNames[id] = TabList_Buffer.Count; StringsBuffer_Add(&TabList_Buffer, &colorlessName);
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;
TabList.PlayerNames[id] = TabList.Buffer.Count; StringsBuffer_Add(&TabList.Buffer, &colorlessName);
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;
}

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_GroupRanks, 0, sizeof(TabList_GroupRanks));
StringsBuffer_Clear(&TabList_Buffer);
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.GroupRanks, 0, sizeof(TabList.GroupRanks));
StringsBuffer_Clear(&TabList.Buffer);
}

struct IGameComponent TabList_Component = {
@@ -72,7 +72,7 @@ struct Entity {
Vector3 Velocity;

struct Model* Model;
BlockID ModelBlock; /* BlockID, if model name was originally a vaid block id. */
BlockID ModelBlock; /* BlockID, if model name was originally a valid block. */
struct AABB ModelAABB;
Vector3 ModelScale, Size;
float StepSize;
@@ -88,14 +88,26 @@ struct Entity {
};
typedef bool (*Entity_TouchesCondition)(BlockID block);

/* Initialises non-zero fields of the given entity. */
void Entity_Init(struct Entity* e);
/* Gets the position of the eye of the given entity's model. */
Vector3 Entity_GetEyePosition(struct Entity* e);
/* Returns the height of the eye of the given entity's model. */
/* (i.e. distance to eye from feet/base of the model) */
float Entity_GetEyeHeight(struct Entity* e);
/* Calculates the transformation matrix applied when rendering the given entity. */
void Entity_GetTransform(struct Entity* e, Vector3 pos, Vector3 scale, struct Matrix* m);
void Entity_GetPickingBounds(struct Entity* e, struct AABB* bb);
/* Gets the current collision bounds of the given entity. */
void Entity_GetBounds(struct Entity* e, struct AABB* bb);
/* Sets the model of the entity. (i.e its appearance) */
void Entity_SetModel(struct Entity* e, const String* model);
/* Updates cached Size and ModelAABB of the given entity. */
/* NOTE: Only needed when manually changing Model or ModelScale. */
/* Entity_SetModel implicitly call this method. */
void Entity_UpdateModelBounds(struct Entity* e);

/* Whether the given entity is touching any blocks meeting the given condition .*/
bool Entity_TouchesAny(struct AABB* bb, Entity_TouchesCondition cond);
bool Entity_TouchesAnyRope(struct Entity* e);
bool Entity_TouchesAnyLava(struct Entity* e);
@@ -107,31 +119,38 @@ void Entities_RenderModels(double delta, float t);
void Entities_RenderNames(double delta);
void Entities_RenderHoveredNames(double delta);
void Entities_Remove(EntityID id);
/* Gets the ID of the closest entity to the given entity. */
EntityID Entities_GetCloset(struct Entity* src);
void Entities_DrawShadows(void);

#define TABLIST_MAX_NAMES 256
extern StringsBuffer TabList_Buffer;
extern uint16_t TabList_PlayerNames[TABLIST_MAX_NAMES];
extern uint16_t TabList_ListNames[TABLIST_MAX_NAMES];
extern uint16_t TabList_GroupNames[TABLIST_MAX_NAMES];
extern uint8_t TabList_GroupRanks[TABLIST_MAX_NAMES];

/* Returns whether the given tab list entry is used at all. */
/* Data for all entries in tab list */
extern struct _TabListData {
uint16_t PlayerNames[TABLIST_MAX_NAMES];
uint16_t ListNames[TABLIST_MAX_NAMES];
uint16_t GroupNames[TABLIST_MAX_NAMES];
uint8_t GroupRanks[TABLIST_MAX_NAMES];
StringsBuffer Buffer;
} TabList;

/* Returns whether the tab list entry with the given ID is used at all. */
bool TabList_Valid(EntityID id);
/* Removes the tab list entry with the given ID, raising TabListEvents.Removed event. */
void TabList_Remove(EntityID id);
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]);
#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]);
#define Player_Layout struct Entity Base; char DisplayNameRaw[STRING_SIZE]; bool FetchedSkin; struct Texture NameTex;

/* Represents a player entity. */
struct Player { Player_Layout };
/* Sets the display name (name tag above entity) and skin name of the given player. */
void Player_SetName(struct Player* player, const String* name, const String* skin);
/* Remakes the texture for the name tag of the entity. */
void Player_UpdateNameTex(struct Player* player);
/* Resets the skin of the entity to default. */
void Player_ResetSkin(struct Player* player);

/* Represents another entity in multiplayer */
@@ -157,8 +176,14 @@ struct LocalPlayer {
};

extern struct LocalPlayer LocalPlayer_Instance;
/* Returns how high (in blocks) the player can jump. */
float LocalPlayer_JumpHeight(void);
/* Checks consistency of user's enabled hacks. */
/* (e.g. if Hacks.CanNoclip is false, Hacks.Noclip is set to false) */
void LocalPlayer_CheckHacksConsistency(void);
/* Interpolates current position and orientation between Interp.Prev and Interp.Next */
void LocalPlayer_SetInterpPosition(float t);
/* Returns whether local player handles a key being pressed. */
/* e.g. for respawn, toggle fly, etc. */
bool LocalPlayer_HandlesKey(Key key);
#endif
@@ -9,28 +9,31 @@
struct IGameComponent;
extern struct IGameComponent Inventory_Component;

/* Number of blocks in each hotbar */
#define INVENTORY_BLOCKS_PER_HOTBAR 9
/* Number of hotbars that can be selected between */
#define INVENTORY_HOTBARS 9

/* Stores the currently bound blocks for all hotbars. */
extern BlockID Inventory_Table[INVENTORY_HOTBARS * INVENTORY_BLOCKS_PER_HOTBAR];
/* Mapping of indices in inventory menu to block IDs. */
extern BlockID Inventory_Map[BLOCK_COUNT];

/* Currently selected index within a hotbar. */
extern int Inventory_SelectedIndex;
/* Currently selected hotbar. */
extern int Inventory_Offset;
/* Whether the user is allowed to change selected/held block. */
extern bool Inventory_CanChangeSelected;
/* Whether the user can use the inventory at all. */
/* NOTE: false prevents the user from deleting/picking/placing blocks. */
extern bool Inventory_CanUse;

/* Gets the block at the nth index in the current hotbar. */
#define Inventory_Get(idx) (Inventory_Table[Inventory_Offset + (idx)])
/* Sets the block at the nth index in the current hotbar. */
#define Inventory_Set(idx, block) Inventory_Table[Inventory_Offset + (idx)] = block
/* Gets the currently selected block. */
#define Inventory_SelectedBlock Inventory_Get(Inventory_SelectedIndex)
/* Whether the user is allowed to change selected/held block. */
extern bool Inventory_CanChangeSelected;
/* Whether the user can use the inventory at all. */
/* NOTE: false prevents the user from deleting/picking/placing blocks. */
extern bool Inventory_CanUse;

/* Checks if the user can change their selected/held block. */
/* NOTE: Shows a message in chat if they are unable to. */
@@ -119,7 +119,7 @@ static void Handlers_AddTablistEntry(EntityID id, const String* playerName, cons
oldPlayerName = TabList_UNSAFE_GetPlayer(id);
oldListName = TabList_UNSAFE_GetList(id);
oldGroupName = TabList_UNSAFE_GetGroup(id);
oldGroupRank = TabList_GroupRanks[id];
oldGroupRank = TabList.GroupRanks[id];

changed = !String_Equals(playerName, &oldPlayerName) || !String_Equals(listName, &oldListName)
|| !String_Equals(groupName, &oldGroupName) || groupRank != oldGroupRank;
@@ -48,15 +48,15 @@ void GraphicsMode_Make(struct GraphicsMode* m, int bpp, int depth, int stencil,
void GraphicsMode_MakeDefault(struct GraphicsMode* m);

/* Encodes a string in platform specific format. (e.g. unicode on windows, UTF8 on linux) */
/* NOTE: Only useful for platform specific function calls - do NOT try to interpret the data.
Returns the number of bytes written, excluding trailing NULL terminator. */
/* NOTE: Only useful for platform specific function calls - do NOT try to interpret the data. */
/* Returns the number of bytes written, excluding trailing NULL terminator. */
CC_API int Platform_ConvertString(void* data, const String* src);
/* Initalises the platform specific state. */
void Platform_Init(void);
/* Frees the platform specific state. */
void Platform_Free(void);
/* Sets current/working directory to the given directory. */
/* This is the 'base directory' relative paths are based on. */
/* This is the 'base directory' relative paths are relative to. */
ReturnCode Platform_SetCurrentDirectory(const String* path);
/* Exits the process with the given return code .*/
void Platform_Exit(ReturnCode code);
@@ -95,7 +95,8 @@ CC_API void* Mem_Realloc(void* mem, uint32_t numElems, uint32_t elemsSize, const
CC_API void Mem_Free(void* mem);
/* Sets the contents of a block of memory to the given value. */
void Mem_Set(void* dst, uint8_t value, uint32_t numBytes);
/* Copies a block of memory to another block. NOTE: These blocks MUST NOT overlap. */
/* Copies a block of memory to another block of memory. */
/* NOTE: These blocks MUST NOT overlap. */
void Mem_Copy(void* dst, const void* src, uint32_t numBytes);

/* Logs a debug message to console. */
@@ -2016,8 +2016,8 @@ static int PlayerListWidget_PlayerCompare(int x, int y) {
uint8_t xRank, yRank;
String xNameRaw, yNameRaw;

xRank = TabList_GroupRanks[x];
yRank = TabList_GroupRanks[y];
xRank = TabList.GroupRanks[x];
yRank = TabList.GroupRanks[y];
if (xRank != yRank) return (xRank < yRank ? -1 : 1);

String_InitArray(xName, xNameBuffer);

0 comments on commit 9cfb8fa

Please sign in to comment.