Created a simple world storage method

This commit is contained in:
Warwick 2023-08-09 14:47:05 +01:00
parent 56c1b1e02a
commit 32fd8e5bc2
5 changed files with 50 additions and 2 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
#Build files #Build files
.cmake
.cache .cache
CMakeCache.txt CMakeCache.txt
CMakeFiles CMakeFiles

View file

@ -5,7 +5,7 @@ project(urchin
HOMEPAGE_URL "https://git.warwick-new.co.uk/" HOMEPAGE_URL "https://git.warwick-new.co.uk/"
LANGUAGES C) LANGUAGES C)
set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD 99)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_CLANG_TIDY) set(CMAKE_C_CLANG_TIDY)

View file

@ -10,6 +10,8 @@
#include <strings.h> #include <strings.h>
#include <sys/socket.h> // sockaddr #include <sys/socket.h> // sockaddr
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include "world.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Set up containers for file descriptor id's // Set up containers for file descriptor id's
@ -27,10 +29,12 @@ int main(int argc, char *argv[]) {
socklen_t clilen = sizeof cli_addr; // We can do this here as cli_addr isn't socklen_t clilen = sizeof cli_addr; // We can do this here as cli_addr isn't
// touched until it's used // touched until it's used
// Create world
Room* world = wrld__create();
// Create socket // Create socket
serv_sockfd = msg__create_serv_connection(portno); serv_sockfd = msg__create_serv_connection(portno);
// Accept connection // Accept connection
cli_sockfd = msg__accept_cli_connection(serv_sockfd); cli_sockfd = msg__accept_cli_connection(serv_sockfd);
@ -63,6 +67,7 @@ int main(int argc, char *argv[]) {
msg__destroy_connection(cli_sockfd); msg__destroy_connection(cli_sockfd);
msg__destroy_connection(serv_sockfd); msg__destroy_connection(serv_sockfd);
wrld__destroy(world);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

23
src/server/world.c Normal file
View file

@ -0,0 +1,23 @@
#include "world.h"
Room* wrld__create() {
Room *rooms = malloc(WORLD_SIZE * sizeof(*rooms));
for (unsigned int i = 0; i < WORLD_SIZE; i++) {
char strbuf[(int)log10(WORLD_SIZE) + 1 + 80];
snprintf(strbuf, sizeof(strbuf), "Room name: %d",i);
rooms[i].name = strbuf;
rooms[i].description = strbuf;
rooms[i].north = NULL;
rooms[i].east = NULL;
rooms[i].south = NULL;
rooms[i].west = NULL;
}
return rooms;
}
void wrld__destroy(Room* rooms){
free(rooms);
}

19
src/server/world.h Normal file
View file

@ -0,0 +1,19 @@
#define WORLD_SIZE 100
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// Define what a room is.
typedef struct room {
char* name;
char* description;
struct room* north;
struct room* east;
struct room* south;
struct room* west;
} Room;
// Creator and destructor for the world
Room* wrld__create();
void wrld__destroy(Room* rooms);