More cleaning up.

- Brought remaining files in line with style guides, and improved comments.
This commit is contained in:
Barry Kane 2023-02-15 22:09:21 +00:00
parent f31f0c79a5
commit f5cb3ad16e
7 changed files with 105 additions and 90 deletions

View File

@ -124,7 +124,7 @@ void * outputThreadHandler(void * parameters)
{
break;
}
if (&connectedPlayers[index] == message->recipients[targetIndex])
if (&connectedPlayers[index] == message->recipients[sentCount])
{
sentCount++;
messageSend(tlssessions[index], message->content);
@ -156,7 +156,7 @@ void userInputSanatize(char * inputString, int length)
inputString[length - 1] = '\0';
}
// Sanatize user names so they display correctly;
// Sanatize user names so they display correctly:
void userNameSanatize(char * inputString, int length)
{
for(int index = 0; index <= length; index++)

View File

@ -11,7 +11,7 @@
static inline void deallocateListNode(listNode * node, listDataType type)
{
// Delete the node:
switch(type)
switch (type)
{
case PLAYER:
{
@ -60,7 +60,7 @@ list * createList(listDataType type)
int destroyList(list ** list)
{
// Check if the list is empty:
if((*list)->itemCount == 0)
if ((*list)->itemCount == 0)
{
free(*list);
list = NULL;
@ -68,7 +68,7 @@ int destroyList(list ** list)
}
else
{
while((*list)->itemCount > 0)
while ((*list)->itemCount > 0)
{
removeFromList((*list), (*list)->type, (*list)->itemCount - 1);
}
@ -82,13 +82,13 @@ int destroyList(list ** list)
listData * getFromList(list * list, size_t listIndex)
{
// Check that we were given a valid index:
if(listIndex > (list->itemCount - 1))
if (listIndex > (list->itemCount - 1))
{
perror("Invalid index specified.\n");
return NULL;
}
// Return the head if index is 0:
else if(listIndex == 0)
else if (listIndex == 0)
{
return &(list->head->data);
}
@ -96,7 +96,7 @@ listData * getFromList(list * list, size_t listIndex)
else
{
listNode * currentNode = list->head;
while(listIndex-- > 0)
while (listIndex-- > 0)
{
currentNode = currentNode->next;
}
@ -108,23 +108,23 @@ listData * getFromList(list * list, size_t listIndex)
listNode * getNodeFromList(list * list, size_t listIndex)
{
// Check that we were given a valid index:
if(listIndex > (list->itemCount - 1))
if (listIndex > (list->itemCount - 1))
{
perror("Invalid index specified.\n");
return NULL;
}
// Return the head if index is 0:
else if(listIndex == 0)
else if (listIndex == 0)
{
return list->head;
}
// Loop through the entries in the list until we get to the right one:
else
{
if((list->itemCount / 2) < listIndex)
if ((list->itemCount / 2) < listIndex)
{
listNode * currentNode = list->tail;
while(listIndex-- > 0)
while (listIndex-- > 0)
{
currentNode = currentNode->previous;
}
@ -133,7 +133,7 @@ listNode * getNodeFromList(list * list, size_t listIndex)
else
{
listNode * currentNode = list->head;
while(listIndex-- > 0)
while (listIndex-- > 0)
{
currentNode = currentNode->next;
}
@ -146,14 +146,14 @@ listNode * getNodeFromList(list * list, size_t listIndex)
listNode * addToList(list * list, void * data, listDataType type)
{
// Check the type:
if(type != list->type)
if (type != list->type)
{
fprintf(stderr, "Not the correct type for this list.\n");
return NULL;
}
// If this is the first item in the list:
if(list->itemCount == 0)
if (list->itemCount == 0)
{
// Allocate the new node for the list:
list->head = calloc(1, sizeof(listNode));
@ -164,7 +164,7 @@ listNode * addToList(list * list, void * data, listDataType type)
list->tail = list->head;
// Add the data to the new node:
switch(type)
switch (type)
{
case PATH:
{
@ -194,7 +194,7 @@ listNode * addToList(list * list, void * data, listDataType type)
list->tail->next = calloc(1, sizeof(listNode));
// Add the data to the new node:
switch(type)
switch (type)
{
case PATH:
{
@ -235,26 +235,26 @@ listNode * addToList(list * list, void * data, listDataType type)
listNode * insertIntoList(list * list, void * data, listDataType type, size_t listIndex)
{
// Check that the types are correct:
if(list->type != type)
if (list->type != type)
{
fprintf(stderr, "Types do not match.\n");
return NULL;
}
// Handle the special case of adding to the end of the list:
if(listIndex == (list->itemCount - 1))
if (listIndex == (list->itemCount - 1))
{
return addToList(list, data, type);
}
// Handle the special case of adding to the beginning of the list:
if(listIndex == 0)
if (listIndex == 0)
{
// Create the new node:
listNode * newNode = calloc(1, sizeof(listNode));
// Add the data to the node:
switch(type)
switch (type)
{
case PATH:
{
@ -290,7 +290,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
}
// Check that the index is valid:
if(listIndex > (list->itemCount - 1))
if (listIndex > (list->itemCount - 1))
{
fprintf(stderr, "Index is invalid for the list.\n");
return NULL;
@ -313,7 +313,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
previousNode->next->previous = previousNode;
// Add the data to the node:
switch(type)
switch (type)
{
case PATH:
{
@ -344,7 +344,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
bool deleteFromList(list * list, void * data, listDataType type)
{
size_t index = 0;
if(getIndexFromList(list, data, type, &index) == false)
if (getIndexFromList(list, data, type, &index) == false)
{
return false;
}
@ -359,19 +359,19 @@ bool deleteFromList(list * list, void * data, listDataType type)
int removeFromList(list * list, listDataType type, size_t listIndex)
{
// Check that we're removing the correct type:
if(list->type != type)
if (list->type != type)
{
return -1;
}
// Check the list index is valid:
if(listIndex > list->itemCount - 1)
if (listIndex > list->itemCount - 1)
{
return -2;
}
// The first node in the list:
if(listIndex == 0)
if (listIndex == 0)
{
// Get the current head and move the list's head on:
listNode * oldHead = list->head;
@ -379,7 +379,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
// If we haven't removed the last item, set the previous pointer
// in the new head to null.
if(list->head != NULL)
if (list->head != NULL)
{
list->head->previous = NULL;
}
@ -392,7 +392,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
return list->itemCount;
}
// The last node in the list:
else if(listIndex == (list->itemCount - 1))
else if (listIndex == (list->itemCount - 1))
{
// Move the tail up by one:
list->tail = list->tail->previous;
@ -430,7 +430,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
bool getIndexFromList(list * list, void * data, listDataType type, size_t * index)
{
// Check the list types are the same:
if(list->type == type)
if (list->type == type)
{
fprintf(stderr, "List types do not match.\n");
return false;
@ -438,11 +438,11 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
for(*index = 0; *index < list->itemCount; *index += 1)
{
switch(type)
switch (type)
{
case AREA:
{
if(getFromList(list, *index)->area == data)
if (getFromList(list, *index)->area == data)
{
return true;
}
@ -450,7 +450,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
}
case PLAYER:
{
if(getFromList(list, *index)->player == data)
if (getFromList(list, *index)->player == data)
{
return true;
}
@ -458,7 +458,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
}
case PATH:
{
if(getFromList(list, *index)->path == data)
if (getFromList(list, *index)->path == data)
{
return true;
}
@ -466,7 +466,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
}
case SKILL:
{
if(getFromList(list, *index)->skill == data)
if (getFromList(list, *index)->skill == data)
{
return true;
}

View File

@ -15,6 +15,7 @@ typedef struct playerSkill playerSkill;
// -=[ Data Structures ]=-:
// ========================
// An enum of the possible data types that can be stored in a list:
typedef enum listDataType
{
PATH,
@ -23,6 +24,7 @@ typedef enum listDataType
SKILL
} listDataType;
// A union containing a pointers to all data types that can be stored in a list:
typedef union listData
{
playerPath * path;
@ -31,6 +33,7 @@ typedef union listData
playerSkill * skill;
} listData;
// A doubly linked node for the linked list type:
typedef struct listNode listNode;
typedef struct listNode
{
@ -39,6 +42,7 @@ typedef struct listNode
listNode * previous;
} listNode;
// A header structure for the list containing the length, head, tail, and type of the list.
typedef struct list
{
listDataType type;

View File

@ -11,7 +11,7 @@
// Create a new skill and add it to the global skill list:
listNode * createSkill(list * globalSkillList, char * skillName, int skillNameLength, bool trainedSkill)
{
if(skillNameLength >= 32)
if (skillNameLength >= 32)
{
fprintf(stderr, "Skill name is too long. Please shorten the name and try again.\n");
return NULL;
@ -35,9 +35,9 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
// Check if the skill exists in the game:
size_t globalIndex = 0;
bool skillExists = false;
while(globalIndex < globalSkillList->itemCount)
while (globalIndex < globalSkillList->itemCount)
{
if(strncmp(skillName, getFromList(globalSkillList, globalIndex)->skill->skillName, skillNameLength) == 0)
if (strncmp(skillName, getFromList(globalSkillList, globalIndex)->skill->skillName, skillNameLength) == 0)
{
skillExists = true;
break;
@ -45,7 +45,7 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
globalIndex++;
}
if(!skillExists)
if (!skillExists)
{
fprintf(stderr, "Skill doesn't exist in skill list.\n");
return -1;
@ -54,16 +54,16 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
// Check if the player has the skill:
size_t playerIndex = 0;
bool playerHasSkill = false;
while(playerIndex < targetPlayer->skills->itemCount)
while (playerIndex < targetPlayer->skills->itemCount)
{
if(strncmp(skillName, getFromList(targetPlayer->skills, playerIndex)->skill->skillName, skillNameLength) == 0)
if (strncmp(skillName, getFromList(targetPlayer->skills, playerIndex)->skill->skillName, skillNameLength) == 0)
{
playerHasSkill = true;
break;
}
playerIndex++;
}
if(playerHasSkill)
if (playerHasSkill)
{
getFromList(targetPlayer->skills, playerIndex)->skill->skillPoints++;
}
@ -85,7 +85,7 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
coreStat getCoreStatFromString(char * inputString, int stringLength)
{
// Check we've got a long enough string to fit a stat:
if(stringLength < 4)
if (stringLength < 4)
{
return INVALID;
}
@ -99,11 +99,11 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// If we have a string that's at most just the stat name plus a null character, or
// a dirtier string, we can check in a better order and ignore impossibilites:
if(stringLength < 9)
if (stringLength < 9)
{
if(stringLength <= 4)
if (stringLength <= 4)
{
if(strncmp(string, "wits", 4) == 0)
if (strncmp(string, "wits", 4) == 0)
{
free(string);
return WITS;
@ -115,19 +115,19 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
}
}
// Hopefully one of the seven letter long ones:
else if(stringLength <= 7)
else if (stringLength <= 7)
{
if(strncmp(string, "strength", 7) == 0)
if (strncmp(string, "strength", 7) == 0)
{
free(string);
return STRENGTH;
}
else if(strncmp(string, "dexerity", 7) == 0)
else if (strncmp(string, "dexerity", 7) == 0)
{
free(string);
return DEXERITY;
}
if(strncmp(string, "wits", 4) == 0)
if (strncmp(string, "wits", 4) == 0)
{
free(string);
return WITS;
@ -141,27 +141,27 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// Hopefully one of the 8 letter long stats:
else
{
if(strncmp(string, "intellect", 8) == 0)
if (strncmp(string, "intellect", 8) == 0)
{
free(string);
return INTELLECT;
}
else if(strncmp(string, "endurance", 8) == 0)
else if (strncmp(string, "endurance", 8) == 0)
{
free(string);
return ENDURANCE;
}
else if(strncmp(string, "strength", 7) == 0)
else if (strncmp(string, "strength", 7) == 0)
{
free(string);
return STRENGTH;
}
else if(strncmp(string, "dexerity", 7) == 0)
else if (strncmp(string, "dexerity", 7) == 0)
{
free(string);
return DEXERITY;
}
if(strncmp(string, "wits", 4) == 0)
if (strncmp(string, "wits", 4) == 0)
{
free(string);
return WITS;
@ -176,27 +176,27 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// Worst case, it's definitely a dirty string, compare them all:
else
{
if(strncmp(string, "wits", 4) == 0)
if (strncmp(string, "wits", 4) == 0)
{
free(string);
return WITS;
}
else if(strncmp(string, "intellect", 8) == 0)
else if (strncmp(string, "intellect", 8) == 0)
{
free(string);
return INTELLECT;
}
else if(strncmp(string, "strength", 7) == 0)
else if (strncmp(string, "strength", 7) == 0)
{
free(string);
return STRENGTH;
}
else if(strncmp(string, "endurance", 8) == 0)
else if (strncmp(string, "endurance", 8) == 0)
{
free(string);
return ENDURANCE;
}
else if(strncmp(string, "dexerity", 7) == 0)
else if (strncmp(string, "dexerity", 7) == 0)
{
free(string);
return DEXERITY;
@ -209,6 +209,7 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
}
}
// Deallocate a player's information including the skill lists and stats:
int deallocatePlayer(playerInfo * playerToDeallocate)
{
// Deallocate the skill list:

View File

@ -14,6 +14,7 @@ typedef struct playerPath playerPath;
typedef struct listNode listNode;
typedef struct list list;
// The basic information that needs to be stored for a player or creature's stats:
typedef struct statBlock
{
// Levelling:
@ -36,6 +37,7 @@ typedef struct statBlock
int skillPoints;
} statBlock;
// Information about a skill, including skill levels and modifiers for the player:
typedef struct playerSkill
{
char skillName[32];
@ -44,6 +46,7 @@ typedef struct playerSkill
bool trainedSkill;
} playerSkill;
// Information about a single player's character:
typedef struct playerInfo
{
char playerName[32];
@ -52,6 +55,7 @@ typedef struct playerInfo
list * skills;
} playerInfo;
// An enum of the main stats of the game:
typedef enum coreStat
{
WITS,
@ -72,7 +76,7 @@ int takeSkillbyID(list * globalSkillList, int skillID, playerInfo * targetPlayer
// Take a string containing a core stat name and return the core stat:
coreStat getCoreStatFromString(char * string, int stringLength);
// Deallocate a player:
// Deallocate a player's information including the skill lists and stats:
int deallocatePlayer(playerInfo * playerToDeallocate);
#endif

View File

@ -5,10 +5,11 @@
#include <unistd.h>
#include <ncurses.h>
void slowPrint(char * stringToPrint, int delay)
// A character by character print, similar to a serial terminal with lower baud rate:
void slowPrint(const char * stringToPrint, int delay)
{
int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0')
while (stringToPrint[characterIndex] != '\0')
{
putchar(stringToPrint[characterIndex]);
// Flush the buffer so there's no line buffering.
@ -18,14 +19,15 @@ void slowPrint(char * stringToPrint, int delay)
}
}
void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded)
// The same, altered to work with ncurses:
void slowPrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded)
{
int characterIndex = 0;
if(bolded)
if (bolded)
{
wattron(window, A_BOLD);
}
while(stringToPrint[characterIndex] != '\0')
while (stringToPrint[characterIndex] != '\0')
{
waddch(window, stringToPrint[characterIndex]);
// Refresh the ncurses screen.
@ -33,17 +35,18 @@ void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bol
usleep(delay);
characterIndex++;
}
if(bolded)
if (bolded)
{
wattroff(window, A_BOLD);
}
wrefresh(window);
}
void bruteforcePrint(char * stringToPrint, int delay)
// A character by character "brute-force" print, similar to Hollywood hacking scenes:
void bruteforcePrint(const char * stringToPrint, int delay)
{
unsigned int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0')
while (stringToPrint[characterIndex] != '\0')
{
for(unsigned char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++)
{
@ -58,14 +61,15 @@ void bruteforcePrint(char * stringToPrint, int delay)
}
}
void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded)
// The same, altered to work with ncurses:
void bruteforcePrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded)
{
int characterIndex = 0;
if(bolded)
if (bolded)
{
wattron(window, A_BOLD);
}
while(stringToPrint[characterIndex] != '\0')
while (stringToPrint[characterIndex] != '\0')
{
for(char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++)
{
@ -78,19 +82,20 @@ void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bo
waddch(window, stringToPrint[characterIndex]);
characterIndex++;
}
if(bolded)
if (bolded)
{
wattroff(window, A_BOLD);
}
wrefresh(window);
}
// Word-wrap a string to a given width:
void wrapString(char * stringToWrap, int stringLength, int screenWidth)
{
int characterCount = 0;
for(int index = 0; index < stringLength; index++)
{
if(stringToWrap[index] == '\n')
if (stringToWrap[index] == '\n')
{
characterCount = 0;
}
@ -98,13 +103,13 @@ void wrapString(char * stringToWrap, int stringLength, int screenWidth)
{
characterCount++;
}
if(characterCount == screenWidth)
if (characterCount == screenWidth)
{
while(!isspace(stringToWrap[index]) && index > 0)
while (!isspace(stringToWrap[index]) && index > 0)
{
index--;
}
if(index == 0)
if (index == 0)
{
return;
}

View File

@ -5,20 +5,23 @@
#include <stdio.h>
#include <ncurses.h>
// A character by character print, similar to a serial terminal with lower baud rate.
void slowPrint(char * stringToPrint, int delay);
// A character by character print, similar to a serial terminal with lower baud rate:
void slowPrint(const char * stringToPrint, int delay);
// The same, altered to work with ncurses.
void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded);
// The same, altered to work with ncurses:
void slowPrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded);
// A character by character "brute-force" print, similar to Hollywood hacking scenes.
void bruteforcePrint(char * stringToPrint, int delay);
// A character by character "brute-force" print, similar to Hollywood hacking scenes:
void bruteforcePrint(const char * stringToPrint, int delay);
// The same, altered to work with ncurses.
void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded);
// The same, altered to work with ncurses:
void bruteforcePrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded);
// Word-wrap a string to a given width:
void wrapString(char * stringToWrap, int stringLength, int screenWidth);
// A string containing an ASCII art version of the Silverkin Industries logo.
char * logostring =
const char * logostring =
" ///////\n"
" //////////////////////////////////////////\n"
" ///////////////////////////////////////////////////////////\n"
@ -32,6 +35,4 @@ char * logostring =
" # # # # # # # # ## # ### # # ## //\n"
" # # ### ##### ##### ### # # # # #### ### /\n";
void wrapString(char * stringToWrap, int stringLength, int screenWidth);
#endif