Refactored areas to use linked-lists.

- Refactored the server to rely on the linked-list version of area lists.
- Removed all old code pertaining to Area/Path lists.
- Removed a no-longer useful test for corestat-from string performance.y
This commit is contained in:
Barry Kane 2022-11-18 14:44:25 +00:00
parent 6b3d9febf6
commit 51f1a953e7
7 changed files with 35 additions and 166 deletions

View File

@ -70,81 +70,3 @@ int createOneWayPath(playerArea * fromArea, playerArea * toArea, char * descript
return 0;
}
// =========================
// -=[ Area/Path Lists: ]=-:
// =========================
// Create and initialize an areaList:
areaNode * createAreaList(playerArea * initialArea)
{
areaNode * newAreaList = malloc(sizeof(areaNode));
newAreaList->data = initialArea;
newAreaList->next = NULL;
newAreaList->prev = NULL;
return newAreaList;
}
// Adds an areaNode to the end of a list, returning it's position:
int addAreaNodeToList(areaNode * toList, playerArea * areaToAdd)
{
areaNode * current;
int index = 0;
current = toList;
while(current->next != NULL)
{
current = current->next;
index++;
}
current->next = malloc(sizeof(areaNode));
current->next->prev = current;
current->next->data = areaToAdd;
current->next->next = NULL;
return 0;
}
// Removes an areaNode from the list, returning 0 on success and -1 on failure:
int deleteAreaNodeFromList(areaNode * fromList, playerArea * areaToDelete)
{
areaNode * current = fromList;
while(current->data != areaToDelete && current->next != NULL)
{
current = current->next;
}
if(current->next == NULL && current->data != areaToDelete)
{
return -1;
}
current->prev->next = current->next;
if(current->next != NULL)
{
current->next->prev = current->prev;
}
free(current);
return 0;
}
// Return the areaNode at the given index from the list:
areaNode * getAreaNode(areaNode * fromList, int listIndex)
{
areaNode * current = fromList;
for(int index = 0; index < listIndex; index++)
{
if(current->next != NULL)
{
current = current->next;
}
else
{
return NULL;
}
}
return current;
}
// Return the playerArea of the areaNode at the given index from the list:
playerArea * getAreaFromList(areaNode * fromList, int listIndex)
{
areaNode * current = getAreaNode(fromList, listIndex);
return current->data;
}

View File

@ -35,54 +35,6 @@ int createPath(playerArea * fromArea, playerArea * toArea, char * fromDescriptio
// Create a one-way path between two areas given two areas and a string:
int createOneWayPath(playerArea * fromArea, playerArea * toArea, char * description);
// =========================
// -=[ Area/Path Lists: ]=-:
// =========================
typedef struct areaNode areaNode;
typedef struct pathNode pathNode;
struct pathNode
{
playerPath * data;
pathNode * next;
pathNode * prev;
};
struct areaNode
{
playerArea * data;
areaNode * next;
areaNode * prev;
};
// Create and initialize an areaList:
areaNode * createAreaList(playerArea * initialArea);
// Create and initialize an pathList:
pathNode * createPathList(playerPath * initialPath);
// Adds an areaNode to the end of a list, returning it's position:
int addAreaNodeToList(areaNode * toList, playerArea * areaToAdd);
// Removes an areaNode from the list, returning 0 on success and -1 on failure:
int deleteAreaNodeFromList(areaNode * fromList, playerArea * areaToDelete);
// Adds an pathNode to the end of a list, returning it's position:
int addPathNodeToList(pathNode * toList, playerPath * pathToAdd);
// Removes an pathNode from the list, returning 0 on success and -1 on failure:
int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete);
// Return the areaNode at the given index from the list:
areaNode * getAreaNode(areaNode * fromList, int listIndex);
// Return the pathNode at the given index from the list:
pathNode * getPathNode(pathNode * fromList, int listIndex);
// Return the playerArea of the areaNode at the given index from the list:
playerArea * getAreaFromList(areaNode * fromList, int listIndex);
// TO BE IMPLEMENTED:
/* int saveAreaList(areaNode * listToSave); */

View File

@ -40,7 +40,7 @@ void * gameLogicLoop(void * parameters)
{
queueMessagedCommand(commandQueue, currentInput);
}
else if(currentInput->sender->currentArea == getAreaFromList(threadParameters->areaList, 0))
else if(currentInput->sender->currentArea == getFromList(threadParameters->areaList, 0)->area)
{
currentInput = NULL;
threadParameters->inputQueue->lock = false;
@ -338,7 +338,7 @@ int evaluateNextCommand(gameLogicParameters * parameters, commandQueue * queue)
if(strncmp(currentCommand->command, "move", 4) == 0)
{
char requestedPath[32];
if(strlen(currentCommand->arguments) > 0 && currentCommand->caller->currentArea != getAreaFromList(parameters->areaList, 0))
if(strlen(currentCommand->arguments) > 0 && currentCommand->caller->currentArea != getFromList(parameters->areaList, 0)->area)
{
memcpy(requestedPath, currentCommand->arguments, 32);
userNameSanatize(requestedPath, 32);
@ -393,7 +393,7 @@ int evaluateNextCommand(gameLogicParameters * parameters, commandQueue * queue)
// TODO: Implement login/character creation. Will be a while:
if(strncmp(currentCommand->command, "join", 4) == 0)
{
if(currentCommand->caller->currentArea == getAreaFromList(parameters->areaList, 0))
if(currentCommand->caller->currentArea == getFromList(parameters->areaList, 0)->area)
{
bool validName = true;
for(int index = 0; index < *parameters->playerCount; index++)
@ -410,7 +410,7 @@ int evaluateNextCommand(gameLogicParameters * parameters, commandQueue * queue)
if(validName)
{
strncpy(currentCommand->caller->playerName, currentCommand->arguments, 16);
currentCommand->caller->currentArea = getAreaFromList(parameters->areaList, 1);
currentCommand->caller->currentArea = getFromList(parameters->areaList, 1)->area;
// Call the look command after joining. It's fine to unlock, because the loop won't
// continue until the command is queued:
queue->lock = false;

View File

@ -16,7 +16,7 @@
typedef struct gameLogicParameters
{
int * playerCount;
areaNode * areaList;
list * areaList;
playerInfo * connectedPlayers;
inputMessageQueue * inputQueue;
outputMessageQueue * outputQueue;

View File

@ -67,26 +67,32 @@ int main(int argc, char ** argv)
// -==[ TEST GAME-STATE INITIALIZATION ]==-
// Initialize test areas:
areaNode * areas = createAreaList(createArea("Login Area", "Please login with the /join command."));
addAreaNodeToList(areas, createArea("Octal One - Docking Bay Alpha",
list * areas = createList(AREA);
addToList(areas, createArea("Login Area", "Please login with the /join command."), AREA);
// Create the areas:
addToList(areas, createArea("Octal One - Docking Bay Alpha",
"You are standing in the main docking bay of the largest station in the Octal System. "
"The sheer size of the bay is awe-inpiring. The number of ships is endless. "
"The bay is curved along with the body of the station. A catwalk runs along the back wall of the bay. "
"Two large arches lie at each end, leading to the other bays, and in the center, a set of doors leading to the interior of the station."));
"Two large arches lie at each end, leading to the other bays, and in the center, a set of doors leading to the interior of the station."), AREA);
addAreaNodeToList(areas, createArea("Octal One - Station Access Control",
addToList(areas, createArea("Octal One - Station Access Control",
"You enter into the hallway leading to the main interior of the station."
"The attendant informs you that due to a computer error, exits cannot be proccessed at the moment, so you will be unable to leave, until it is resolved. "
"He apologizes profusely for the inconvenience, and clears you for entry if you wish to continue."));
"The attendant informs you that due to a computer error, exits cannot be proccessed at the moment,"
" so you will be unable to leave, until it is resolved. "
"He apologizes profusely for the inconvenience, and clears you for entry if you wish to continue."), AREA);
addAreaNodeToList(areas, createArea("Octal One - Floor Zero",
addToList(areas, createArea("Octal One - Floor Zero",
"You've never quite seen so many people in one place. A large ring of shopfronts surrounds an area filled with benches and tables. "
"There's so many buisnesses in sight that you feel you could find everything you need, and this is only one of 25 main floors, "
"not to mention the 6 outer pylons which surround the main hull of the station. Staircases lead to an upper platform allowing access to the pylons. "));
"not to mention the 6 outer pylons which surround the main hull of the station. Staircases lead to an upper platform allowing access to the pylons. "), AREA);
// Initialize test paths:
createPath(getAreaFromList(areas, 1), getAreaFromList(areas, 2), "Enter the station interior.", "Return to Docking Bay Alpha.");
createOneWayPath(getAreaFromList(areas, 2), getAreaFromList(areas, 3), "Continue to station interior. ");
createPath(getFromList(areas, 1)->area, getFromList(areas, 2)->area,
"Enter the station interior.", "Return to Docking Bay Alpha.");
createOneWayPath(getFromList(areas, 2)->area, getFromList(areas, 3)->area,
"Continue to station interior. ");
skillList * globalSkillList = malloc(sizeof(skillList));
globalSkillList->head = NULL;
@ -106,7 +112,7 @@ int main(int argc, char ** argv)
// OH NO IT'S NOT MEMORY SAFE BETTER REWRITE IT IN RUST
// But wait, we know the string won't be too big, so it's fine.
strcpy(connectedPlayers[index].playerName, testString);
connectedPlayers[index].currentArea = getAreaFromList(areas, 0);
connectedPlayers[index].currentArea = getFromList(areas, 0)->area;
connectedPlayers[index].stats = calloc(1, sizeof(statBlock));
connectedPlayers[index].stats->specPoints = 30;
connectedPlayers[index].stats->skillPoints = 30;
@ -300,7 +306,7 @@ int main(int argc, char ** argv)
// Clear out the old player state so that a new one may join:
sprintf(testString, "UNNAMED %d", index);
strcpy(connectedPlayers[index].playerName, testString);
connectedPlayers[index].currentArea = getAreaFromList(areas, 0);
connectedPlayers[index].currentArea = getFromList(areas, 0)->area;
// Prepare a fresh SSL session for the next new player:
gnutls_init(&tlssessions[index], GNUTLS_SERVER);

View File

@ -1,11 +0,0 @@
// Hopefully optimized corestat to string:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../src/playerdata.h"
void main(int argc, char ** argv)
{
getCoreStatFromString(argv[1], strlen(argv[1]));
}