Adding groundwork to allow player to move in the world

This commit is contained in:
Warwick 2023-08-17 11:57:11 +01:00
parent 4c07b5df6a
commit 553e46333b
7 changed files with 78 additions and 26 deletions

View file

@ -3,11 +3,11 @@ This is a project I have created to teach myself C and protobuf. It will
eventually be a MUD that may or may not link into my other software projects. eventually be a MUD that may or may not link into my other software projects.
## TODO ## TODO
### Road to v0.1 ### Road to v0.0.1
- [x] Create initial communication process between clients and server. - [x] Create initial communication process between clients and server.
- [ ] Handle multiple connections. - [ ] Handle multiple connections.
- [ ] Use openssl so passwords arn't going over the internet via plaintext. - [ ] Use openssl so passwords arn't going over the internet via plaintext.
- [ ] Create world data structure. - [x] Create world data structure.
- [ ] Create an initial set of actions to allow some kind of interaction. - [ ] Create an initial set of actions to allow some kind of interaction.
- [ ] Move - [ ] Move
- [ ] Look - [ ] Look

View file

@ -1,15 +1,36 @@
#include "command.h" #include "command.h"
Room *cmd__move(move_direction direction, Room *current_room) { Room *cmd__check_move(Room *current_room, Room *to_room) {
if (current_room == NULL || to_room == NULL)
return current_room;
return to_room;
};
// Update room based on move direction
void cmd__move_direction(move_direction direction, Room *current_room) {
if (current_room == NULL) {
err__warn("Cannot move from current_room as room is NULL");
}
switch (direction) { switch (direction) {
case NORTH: case NORTH:
return current_room->north; current_room = cmd__check_move(current_room, current_room->north);
break;
case EAST: case EAST:
return current_room->east; current_room = cmd__check_move(current_room, current_room->east);
break;
case SOUTH: case SOUTH:
return current_room->south; current_room = cmd__check_move(current_room, current_room->south);
break;
case WEST: case WEST:
return current_room->west; current_room = cmd__check_move(current_room, current_room->west);
}
}
void cmd__recv_proc_cmd(Player *player, Command *command) {
if (command->has_move_direction) {
move_direction dir = {command->move_direction};
cmd__move_direction(dir, player->current_location);
} }
return NULL;
} }

View file

@ -1,9 +1,13 @@
#pragma once #pragma once
#include "command_internal.h"
#include "world.h" #include "world.h"
#include <error.h> #include <error.h>
#include <message.h>
#include <world_structures.h> #include <world_structures.h>
// Returns pointer to new room based on current room and direction If room // Will process a command given a player to execute it as
// doesn't exist returns NULL void cmd__recv_proc_cmd(Player *player, Command *command);
Room *cmd__move(move_direction direction, Room *current_room);
// Update room pointer based on move direction
void cmd__move_direction(move_direction direction, Room *current_room);

View file

@ -0,0 +1,6 @@
#pragma once
#include <world_structures.h>
// Check that room is valid before moving
Room *cmd__check_move(Room *current_room, Room *to_room);

View file

@ -1,5 +1,8 @@
#pragma once #pragma once
#include "../src/protobuf_gen/amessage.pb-c.h"
#include "../src/protobuf_gen/commands.pb-c.h"
#include "../src/protobuf_gen/player.pb-c.h"
#include "error.h" #include "error.h"
#include "message_internal.h" #include "message_internal.h"
#include <netinet/in.h> //sockaddr_in #include <netinet/in.h> //sockaddr_in
@ -26,5 +29,5 @@ void msg__recv_login(int *con_sockfd);
void msg__req_login(int *con_sockfd, char *usernm, char *passwd); void msg__req_login(int *con_sockfd, char *usernm, char *passwd);
// send and recive commands // send and recive commands
void msg__recv_command(int *con_sockfd); Command *msg__recv_command(int *con_sockfd);
void msg__req_command(int *con_sockfd, char *usernm, char *passwd); void msg__req_command(int *con_sockfd, char *usernm, char *passwd);

View file

@ -2,14 +2,21 @@
// Make world structures accessible to both server and client // Make world structures accessible to both server and client
// Define moveable directions // Define moveable directions
// TODO eventually move over to a hash table so players can move through
// exits defined in the room description like hatch, crack, door, etc.
typedef enum { NORTH, EAST, SOUTH, WEST } move_direction; typedef enum { NORTH, EAST, SOUTH, WEST } move_direction;
// Define what a room is. // Define what a room is.
typedef struct room { typedef struct Room Room;
char* name; struct Room {
char* description; char *name;
struct room* north; char *description;
struct room* east; Room *north;
struct room* south; Room *east;
struct room* west; Room *south;
} Room; Room *west;
};
typedef struct player {
Room *current_location;
} Player;

View file

@ -1,7 +1,5 @@
#include "message.h" #include "message.h"
#include "error.h" #include "error.h"
#include "protobuf_gen/amessage.pb-c.h"
#include "protobuf_gen/player.pb-c.h"
#include "world_structures.h" #include "world_structures.h"
#include <stdio.h> #include <stdio.h>
@ -148,8 +146,22 @@ int msg__create_serv_connection(int portno) {
return sockfd; return sockfd;
} }
Command *msg__recv_command(int *con_sockfd) {
uint8_t msgbuf[MAX_MSG_SIZE];
read_from_sock(msgbuf, sizeof msgbuf, con_sockfd);
size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf);
void msg__destroy_connection(int sockfd) { close(sockfd); } Command *cmd = command__unpack(NULL, msg_len, msgbuf);
if (cmd == NULL) {
err__warn("Error unpacking login message");
command__free_unpacked(cmd, NULL);
return NULL;
}
return cmd;
}
void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {}
int msg__accept_cli_connection(int server_sockfd) { int msg__accept_cli_connection(int server_sockfd) {
struct sockaddr_in cli_addr; struct sockaddr_in cli_addr;
@ -164,5 +176,4 @@ int msg__accept_cli_connection(int server_sockfd) {
return cli_sockfd; return cli_sockfd;
} }
void msg__recv_command(int *con_sockfd) {} void msg__destroy_connection(int sockfd) { close(sockfd); }
void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {}