Added initial implementation of doubly-linked lists

- Added lists.c
- Added lists.h
- Changed initialisation of rooms to add a third room and to add the rooms to a
  list.
- Added datastructures for area and path nodes for doubly-linked lists.
This commit is contained in:
Barry 2021-12-05 23:33:53 +00:00
parent 241ac7a92b
commit 6c93805d6f
4 changed files with 196 additions and 15 deletions

View File

@ -1,8 +1,8 @@
* Game Design
** The gameplay:
- Each player may travel to areas connected to the current one, depending on flags.
- Players may perform actions, success depending on their stats.
- Players may create characters.
- A GM may create content using GNU Guile.
* Implementation Plans
* TODOs
* SilverMUD: An extensible terminal-top role playing game engine
SilverMUD is a tool for creating engaging and communal stories, all over the
world through the internet. It's designed to give a game master the same power
to improvise that they have at the table, through simple programming and
easy-to-understand structures.
** Player's Guide
** Gamemaster's Guide
** Developer's Guide

View File

@ -12,6 +12,7 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "misc/lists.h"
#include "misc/playerdata.h"
#include "misc/texteffects.h"
#include "misc/inputhandling.h"
@ -27,22 +28,21 @@ int main()
userMessage messageBuffer;
char receiveBuffer[2048];
fd_set connectedClients;
playerArea * areaA, * areaB, * areaC;
playerInfo connectedPlayers[64];
struct sockaddr_in serverAddress, clientAddress;
// Initialize areas:
areaA = createArea("Spawn - North", "A large area, mostly empty, as if the designer hadn't bothered to put anything in it, just yet.");
areaB = createArea("Spawn - South", "A strange, white void. You feel rather uncomfortable.");
areaC = createArea("Temple of Emacs", "A beautifully ornate statue of GNU is above you on a pedestal. Inscribed into the pillar, over and over, is the phrase \"M-x exalt\", in delicate gold letters. You can't help but be awestruck.");
createPath(areaA, areaB, "To South Spawn", "To North Spawn");
createPath(areaC, areaB, "Back to South Spawn", "Path to Enlightenment.");
areaNode * areas = createAreaList(createArea("Spawn - North", "A large area, mostly empty, as if the designer hadn't bothered to put anything in it, just yet."));
addAreaNodeToList(areas, createArea("Spawn - South", "A strange, white void. You feel rather uncomfortable."));
addAreaNodeToList(areas, createArea("Temple of Emacs", "A beautifully ornate statue of GNU is above you on a pedestal. Inscribed into the pillar, over and over, is the phrase \"M-x exalt\", in delicate gold letters. You can't help but be awestruck."));
createPath(getAreaFromList(areas, 0), getAreaFromList(areas, 1), "To South Spawn", "To North Spawn");
createPath(getAreaFromList(areas, 2), getAreaFromList(areas, 1), "Back to South Spawn", "Path to Enlightenment.");
// Initialize playerdata:
for (int index = 0; index < maxClients; index++)
{
strcpy(connectedPlayers[index].playerName, "UNNAMED");
connectedPlayers[index].currentArea = areaA;
connectedPlayers[index].currentArea = getAreaFromList(areas, 0);
}
// Give an intro: Display the Silverkin Industries logo and splash text.

130
src/misc/lists.c Normal file
View File

@ -0,0 +1,130 @@
// Implementation of lists library for SilverMUD.
// Barry Kane, 2021
#include "lists.h"
#include "playerdata.h"
areaNode * createAreaList(playerArea * initialArea)
{
areaNode * newAreaList = malloc(sizeof(areaNode));
newAreaList->data = initialArea;
newAreaList->next = NULL;
newAreaList->prev = NULL;
return newAreaList;
}
pathNode * createPathList(playerPath * initialPath)
{
pathNode * newPathList = malloc(sizeof(pathNode));
newPathList->data = initialPath;
newPathList->next = NULL;
newPathList->prev = NULL;
return newPathList;
}
int addAreaNodeToList(areaNode * toList, playerArea * areaToAdd)
{
areaNode * current;
current = toList;
while(current->next != NULL)
{
current = current->next;
}
current->next = malloc(sizeof(areaNode));
current->next->prev = current;
current->next->data = areaToAdd;
current->next->next = NULL;
}
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 0;
}
current->prev->next = current->next;
if(current->next != NULL)
{
current->next->prev = current->prev;
}
free(current);
}
int addPathNodeToList(pathNode * toList, playerPath * pathToAdd)
{
pathNode * current;
current = toList;
while(current->next != NULL)
{
current = current->next;
}
current->next = malloc(sizeof(pathNode));
current->next->prev = current;
current->next->data = pathToAdd;
current->next->next = NULL;
}
int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete)
{
pathNode * current = fromList;
while(current->data != pathToDelete || current->next != NULL)
{
current = current->next;
}
if(current->next == NULL && current->data != pathToDelete)
{
return 0;
}
current->prev->next = current->next;
if(current->next != NULL)
{
current->next->prev = current->prev;
}
free(current);
}
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;
}
pathNode * getPathNode(pathNode * fromList, int listIndex)
{
pathNode * current = fromList;
for(int index = 0; index < listIndex; index++)
{
if(current->next != NULL)
{
current = current->next;
}
else
{
return NULL;
}
}
return current;
}
playerArea * getAreaFromList(areaNode * fromList, int listIndex)
{
areaNode * current = getAreaNode(fromList, listIndex);
return current->data;
}

51
src/misc/lists.h Normal file
View File

@ -0,0 +1,51 @@
// lists.h: A header file for the lists library for SilverMUD.
// Barry Kane, 2021.
#ifndef LISTS_H
#define LISTS_H
#include "playerdata.h"
typedef struct areaNode areaNode;
typedef struct pathNode pathNode;
struct pathNode
{
playerPath * data;
pathNode * next;
pathNode * prev;
};
struct areaNode
{
playerArea * data;
areaNode * next;
areaNode * prev;
};
areaNode * createAreaList(playerArea * initialArea);
pathNode * createPathList(playerPath * initialPath);
int addAreaNodeToList(areaNode * toList, playerArea * areaToAdd);
int deleteAreaNodeFromList(areaNode * fromList, playerArea * areaToDelete);
int addPathNodeToList(pathNode * toList, playerPath * pathToAdd);
int deletePathNodeFromList(pathNode * fromList, playerPath * pathToDelete);
areaNode * getAreaNode(areaNode * fromList, int listIndex);
pathNode * getPathNode(pathNode * fromList, int listIndex);
playerArea * getAreaFromList(areaNode * fromList, int listIndex);
// TO BE IMPLEMENTED:
/* int saveAreaList(areaNode * listToSave); */
/* int savePathList(pathNode * listToSave); */
/* int loadAreaList(areaNode * listToLoad); */
/* int loadPathList(pathNode * listToLoad); */
#endif