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.
## TODO
### Road to v0.1
### Road to v0.0.1
- [x] Create initial communication process between clients and server.
- [ ] Handle multiple connections.
- [ ] 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.
- [ ] Move
- [ ] Look

View file

@ -1,15 +1,36 @@
#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) {
case NORTH:
return current_room->north;
current_room = cmd__check_move(current_room, current_room->north);
break;
case EAST:
return current_room->east;
current_room = cmd__check_move(current_room, current_room->east);
break;
case SOUTH:
return current_room->south;
current_room = cmd__check_move(current_room, current_room->south);
break;
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
#include "command_internal.h"
#include "world.h"
#include <error.h>
#include <message.h>
#include <world_structures.h>
// Returns pointer to new room based on current room and direction If room
// doesn't exist returns NULL
Room *cmd__move(move_direction direction, Room *current_room);
// Will process a command given a player to execute it as
void cmd__recv_proc_cmd(Player *player, Command *command);
// 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
#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 "message_internal.h"
#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);
// 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);

View file

@ -2,14 +2,21 @@
// Make world structures accessible to both server and client
// 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;
// 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;
typedef struct Room Room;
struct Room {
char *name;
char *description;
Room *north;
Room *east;
Room *south;
Room *west;
};
typedef struct player {
Room *current_location;
} Player;

View file

@ -1,7 +1,5 @@
#include "message.h"
#include "error.h"
#include "protobuf_gen/amessage.pb-c.h"
#include "protobuf_gen/player.pb-c.h"
#include "world_structures.h"
#include <stdio.h>
@ -60,7 +58,7 @@ size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out) {
}
void read_from_sock(uint8_t *recvbuf, unsigned bufsize, int *sockfd) {
bzero(recvbuf, bufsize);
bzero(recvbuf, bufsize);
if (read(*sockfd, recvbuf, bufsize) < 0) {
err__warn("Connection did not send data");
@ -148,8 +146,22 @@ int msg__create_serv_connection(int portno) {
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) {
struct sockaddr_in cli_addr;
@ -164,5 +176,4 @@ int msg__accept_cli_connection(int server_sockfd) {
return cli_sockfd;
}
void msg__recv_command(int *con_sockfd) {}
void msg__req_command(int *con_sockfd, char *usernm, char *passwd) {}
void msg__destroy_connection(int sockfd) { close(sockfd); }