Completed Reorganization of Area Data

- Moved the appropriate data structures and functions into areaData.
- Made movePlayerToArea a gameplay primitive.
This commit is contained in:
Barry Kane 2022-10-23 17:07:13 +01:00
parent 52b4b1e2f0
commit f2dd83857f
7 changed files with 169 additions and 134 deletions

View File

@ -1,8 +1,81 @@
// areadata.c: Implements functions for playerAreas and playerPaths in SilverMUD:
// Barra Ó Catháin, 2022.
#include <string.h>
#include "areadata.h"
#include "playerdata.h"
// ====================
// -=[ Area/Paths: ]=-:
// ====================
// Create an area given a name and description:
playerArea * createArea(char * nameString, char * descriptionString)
{
// Allocate and zero memory for the new area:
playerArea * createdArea = calloc(1, sizeof(playerArea));
// Copy the strings into the newly created area:
strncpy(createdArea->areaName, nameString, 32 - 1);
strncpy(createdArea->areaDescription, descriptionString, MAX - 35);
// Properly null-terminate the strings:
createdArea->areaName[31] = '\0';
createdArea->areaDescription[MAX] = '\0';
// Ensure that all the paths are set to NULL:
for(int index = 0; index < 16; index++)
{
createdArea->areaExits[index] = NULL;
}
// Return the pointer:
return createdArea;
}
// Create a path between two areas given two areas and two strings:
int createPath(playerArea * fromArea, playerArea * toArea, char * fromDescription, char * toDescription)
{
int fromAreaSlot, toAreaSlot;
for(fromAreaSlot = 0; fromAreaSlot < 16; fromAreaSlot++)
{
if(fromArea->areaExits[fromAreaSlot] == NULL)
{
break;
}
if((fromArea->areaExits[fromAreaSlot] != NULL) && (fromAreaSlot == 15))
{
return 1;
}
}
for(toAreaSlot = 0; toAreaSlot < 32; toAreaSlot++)
{
if(toArea->areaExits[toAreaSlot] == 0)
{
break;
}
if((toArea->areaExits[toAreaSlot] != 0) && (toAreaSlot == 31))
{
return 2;
}
}
playerPath * fromPath = malloc(sizeof(playerPath));
playerPath * toPath = malloc(sizeof(playerPath));
fromArea->areaExits[fromAreaSlot] = fromPath;
toArea->areaExits[toAreaSlot] = toPath;
strncpy(fromPath->pathName, fromDescription, 32 - 1);
fromPath->pathName[31] = '\0';
strncpy(toPath->pathName, toDescription, 32 - 1);
toPath->pathName[31] = '\0';
fromArea->areaExits[fromAreaSlot]->areaToJoin = toArea;
toArea->areaExits[toAreaSlot]->areaToJoin = fromArea;
return 0;
}
// =========================
// -=[ Area/Path Lists: ]=-:
// =========================
// Create and initialize an areaList:
areaNode * createAreaList(playerArea * initialArea)
{
areaNode * newAreaList = malloc(sizeof(areaNode));
@ -12,7 +85,7 @@ areaNode * createAreaList(playerArea * initialArea)
return newAreaList;
}
// Create and initialize an pathList:
pathNode * createPathList(playerPath * initialPath)
{
pathNode * newPathList = malloc(sizeof(pathNode));
@ -22,13 +95,16 @@ pathNode * createPathList(playerPath * initialPath)
return newPathList;
}
// 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;
@ -37,6 +113,7 @@ int addAreaNodeToList(areaNode * toList, playerArea * areaToAdd)
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;
@ -46,7 +123,7 @@ int deleteAreaNodeFromList(areaNode * fromList, playerArea * areaToDelete)
}
if(current->next == NULL && current->data != areaToDelete)
{
return 0;
return -1;
}
current->prev->next = current->next;
if(current->next != NULL)
@ -57,21 +134,25 @@ int deleteAreaNodeFromList(areaNode * fromList, playerArea * areaToDelete)
return 0;
}
// Adds an pathNode to the end of a list, returning it's position:
int addPathNodeToList(pathNode * toList, playerPath * pathToAdd)
{
pathNode * current;
int index = 0;
current = toList;
while(current->next != NULL)
{
current = current->next;
index++;
}
current->next = malloc(sizeof(pathNode));
current->next->prev = current;
current->next->data = pathToAdd;
current->next->next = NULL;
return 0;
return index;
}
// Removes an pathNode from the list, returning 0 on success and -1 on failure:
int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete)
{
pathNode * current = fromList;
@ -81,7 +162,7 @@ int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete)
}
if(current->next == NULL && current->data != pathToDelete)
{
return 0;
return -1;
}
current->prev->next = current->next;
if(current->next != NULL)
@ -92,6 +173,7 @@ int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete)
return 0;
}
// Return the areaNode at the given index from the list:
areaNode * getAreaNode(areaNode * fromList, int listIndex)
{
areaNode * current = fromList;
@ -109,6 +191,7 @@ areaNode * getAreaNode(areaNode * fromList, int listIndex)
return current;
}
// Return the pathNode at the given index from the list:
pathNode * getPathNode(pathNode * fromList, int listIndex)
{
pathNode * current = fromList;
@ -126,9 +209,9 @@ pathNode * getPathNode(pathNode * fromList, int listIndex)
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

@ -2,7 +2,36 @@
// Barra Ó Catháin, 2022.
#ifndef AREADATA_H
#define AREADATA_H
#include "playerdata.h"
#include "constants.h"
// ====================
// -=[ Area/Paths: ]=-:
// ====================
typedef struct playerPath playerPath;
typedef struct playerArea playerArea;
struct playerPath
{
char pathName[32];
playerArea * areaToJoin;
};
struct playerArea
{
char areaName[32];
char areaDescription[MAX - 35];
playerPath * areaExits[16];
};
// Create an area given a name and description:
playerArea * createArea(char * nameString, char * descriptionString);
// Create a path between two areas given two areas and two strings:
int createPath(playerArea * fromArea, playerArea * toArea, char * fromDescription, char * toDescription);
// =========================
// -=[ Area/Path Lists: ]=-:
// =========================
typedef struct areaNode areaNode;
typedef struct pathNode pathNode;
@ -21,22 +50,31 @@ struct areaNode
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:

View File

@ -708,3 +708,37 @@ outcome statCheck(playerInfo * player, int chance, coreStat statToCheck)
}
}
// Move a player to a different area given a path in the area:
int movePlayerToArea(playerInfo * player, char * requestedPath)
{
// Check if a number was given first:
int selected = atoi(requestedPath);
if(selected != 0)
{
if(player->currentArea->areaExits[selected - 1] != NULL &&
player->currentArea->areaExits[selected - 1]->areaToJoin != NULL)
{
player->currentArea = player->currentArea->areaExits[selected - 1]->areaToJoin;
return 0;
}
else
{
return 1;
}
}
// Otherwise search for the description:
for (int index = 0; index < 16; index++)
{
if(player->currentArea->areaExits[index] != NULL)
{
if(strncmp(player->currentArea->areaExits[index]->pathName, requestedPath, 32) == 0)
{
printf("%s: %s\n", player->playerName, player->currentArea->areaExits[index]->pathName);
player->currentArea = player->currentArea->areaExits[index]->areaToJoin;
return 0;
}
}
}
return 1;
}

View File

@ -74,6 +74,9 @@ int evaluateNextCommand(gameLogicParameters * parameters, commandQueue * queue);
// -=[ Gameplay Primitives ]=-:
// ============================
// Player movement:
int movePlayerToArea(playerInfo * player, char * requestedPath);
typedef enum outcome
{
CRITICAL_FAILURE,

View File

@ -24,6 +24,7 @@ int messageSend(gnutls_session_t receivingSession, userMessage * messageToSend)
returnValue = gnutls_record_send(receivingSession, messageToSend->messageContent,
sizeof(((userMessage*)0)->messageContent));
} while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED);
return returnValue;
}
@ -41,6 +42,7 @@ int messageReceive(gnutls_session_t receiveFromSession, userMessage * receiveToM
returnValue = gnutls_record_recv(receiveFromSession, receiveToMessage->messageContent,
sizeof(((userMessage*)0)->messageContent));
} while (returnValue == GNUTLS_E_AGAIN || returnValue == GNUTLS_E_INTERRUPTED);
return returnValue;
}
@ -61,10 +63,6 @@ int queueOutputMessage(outputMessageQueue * queue, userMessage messageToQueue)
// Allocate the internal userMessage to store the message:
newOutputMessage->content = malloc(sizeof(userMessage));
// Allocate the internal strings to store the message:
//outputMessage->content->senderName = malloc(sizeof(char)*32);
//outputMessage->content->messageContent = malloc(sizeof(char)*MAX);
// Copy the userMessage to the internal userMessage:
strncpy(newOutputMessage->content->senderName, messageToQueue.senderName, 32);
@ -134,8 +132,7 @@ int queueTargetedOutputMessage(outputMessageQueue * queue,
// Copy the userMessage to the internal userMessage:
strncpy(newOutputMessage->content->senderName, messageToQueue->senderName, 32);
strncpy(newOutputMessage->content->messageContent, messageToQueue->messageContent, MAX);
// Wait for the queue to unlock:
while (queue->lock);

View File

@ -8,103 +8,6 @@
#include "constants.h"
#include "playerdata.h"
// Move a player to a different area given a path in the area:
int movePlayerToArea(playerInfo * player, char * requestedPath)
{
// Check if a number was given first:
int selected = atoi(requestedPath);
if(selected != 0)
{
if(player->currentArea->areaExits[selected -1] != NULL && player->currentArea->areaExits[selected -1]->areaToJoin != NULL)
{
player->currentArea = player->currentArea->areaExits[selected - 1]->areaToJoin;
return 0;
}
else
{
return 1;
}
}
// Otherwise search for the description:
for (int index = 0; index < 16; index++)
{
if(player->currentArea->areaExits[index] != NULL)
{
if(strncmp(player->currentArea->areaExits[index]->pathName, requestedPath, 32) == 0)
{
printf("%s: %s\n", player->playerName, player->currentArea->areaExits[index]->pathName);
player->currentArea = player->currentArea->areaExits[index]->areaToJoin;
return 0;
}
}
}
return 1;
}
// Create an area given a name and description:
playerArea * createArea(char * nameString, char * descriptionString)
{
// Allocate and zero memory for the new area:
playerArea * createdArea = calloc(1, sizeof(playerArea));
// Copy the strings into the newly created area:
strncpy(createdArea->areaName, nameString, 32 - 1);
strncpy(createdArea->areaDescription, descriptionString, MAX - 35);
// Properly null-terminate the strings:
createdArea->areaName[31] = '\0';
createdArea->areaDescription[MAX] = '\0';
// Ensure that all the paths are set to NULL:
for(int index = 0; index < 16; index++)
{
createdArea->areaExits[index] = NULL;
}
// Return the pointer:
return createdArea;
}
// Create a path between two areas given two areas and two strings:
int createPath(playerArea * fromArea, playerArea * toArea, char * fromDescription, char * toDescription)
{
int fromAreaSlot, toAreaSlot;
for(fromAreaSlot = 0; fromAreaSlot < 16; fromAreaSlot++)
{
if(fromArea->areaExits[fromAreaSlot] == NULL)
{
break;
}
if((fromArea->areaExits[fromAreaSlot] != NULL) && (fromAreaSlot == 15))
{
return 1;
}
}
for(toAreaSlot = 0; toAreaSlot < 32; toAreaSlot++)
{
if(toArea->areaExits[toAreaSlot] == 0)
{
break;
}
if((toArea->areaExits[toAreaSlot] != 0) && (toAreaSlot == 31))
{
return 2;
}
}
playerPath * fromPath = malloc(sizeof(playerPath));
playerPath * toPath = malloc(sizeof(playerPath));
fromArea->areaExits[fromAreaSlot] = fromPath;
toArea->areaExits[toAreaSlot] = toPath;
strncpy(fromPath->pathName, fromDescription, 32 - 1);
fromPath->pathName[31] = '\0';
strncpy(toPath->pathName, toDescription, 32 - 1);
toPath->pathName[31] = '\0';
fromArea->areaExits[fromAreaSlot]->areaToJoin = toArea;
toArea->areaExits[toAreaSlot]->areaToJoin = fromArea;
return 0;
}
// Create a new skill and add it to the global skill list:
int createSkill(skillList * globalSkillList, char * skillName, int skillNameLength, bool trainedSkill)
{

View File

@ -4,24 +4,9 @@
#define PLAYERDATA_H
#include <stdlib.h>
#include <stdbool.h>
#include "areadata.h"
#include "constants.h"
typedef struct playerPath playerPath;
typedef struct playerArea playerArea;
struct playerPath
{
char pathName[32];
playerArea * areaToJoin;
};
struct playerArea
{
char areaName[32];
char areaDescription[MAX - 35];
playerPath * areaExits[16];
};
typedef struct statBlock
{
// Levelling:
@ -83,15 +68,6 @@ typedef enum coreStat
INVALID
} coreStat;
// Move a player to a different area given a path in the area:
int movePlayerToArea(playerInfo * player, char * requestedPath);
// Create an area given a name and description:
playerArea * createArea(char * nameString, char * descriptionString);
// Create a path between two areas given two areas and two strings:
int createPath(playerArea * fromArea, playerArea * toArea, char * fromDescription, char * toDescription);
// Create a new skill and add it to the global skill list:
int createSkill(skillList * globalSkillList, char * skillName, int skillNameLength, bool trainedSkill);
@ -111,4 +87,5 @@ coreStat getCoreStatFromString(char * string, int stringLength);
// Deallocate a player:
int deallocatePlayer(playerInfo * playerToDeallocate);
#endif