Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServerGameState + SharedGameState Redesign Initial Implementation #45

Merged
merged 15 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include "client/util.hpp"
#include "client/lobbyfinder.hpp"

#include "shared/game/gamestate.hpp"
//#include "shared/game/gamestate.hpp"
#include "shared/game/sharedgamestate.hpp"
#include "shared/network/packet.hpp"
#include "shared/network/session.hpp"
#include "shared/utilities/config.hpp"
Expand All @@ -36,8 +37,8 @@ class Client {
void processClientInput();
void processServerInput(boost::asio::io_context& context);


GameState gameState;
SharedGameState gameState;
//GameState gameState;

float cubeMovementDelta = 0.05f;

Expand Down
141 changes: 76 additions & 65 deletions include/debugger/debugger.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "shared/game/gamestate.hpp"
#include "shared/game/servergamestate.hpp"
#include "shared/game/gamelogic/constants.hpp"

const std::string NO_SHORTHAND = "NO_SHORTHAND";
Expand Down Expand Up @@ -28,10 +28,10 @@ class Command {
* @brief The Command's run() method is overloaded by each particular
* command to implement its particular behavior.
* @param arguments Vector of string arguments from user
* @param state Reference to the GameState instance maintained by the
* @param state Reference to the ServerGameState instance maintained by the
* debugger.
*/
virtual void run(std::vector<std::string> arguments, GameState& state) = 0;
virtual void run(std::vector<std::string> arguments, ServerGameState& state) = 0;
};

// Vector of all commands known by the debugger
Expand Down Expand Up @@ -62,7 +62,7 @@ class QuitCommand : public Command {
this->shorthand = "q";
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// This command ignores arguments
std::cout << "Quitting gsdb..." << std::endl;

Expand All @@ -79,7 +79,7 @@ class StepCommand : public Command {
//this->num_expected_args = 0;
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// Possible variations:
// step - call GameState::update() once on the given state instance.
// step n - call update() n times.
Expand Down Expand Up @@ -124,7 +124,7 @@ class StateCommand : public Command {
this->shorthand = NO_SHORTHAND;
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// This command ignores arguments

// Print GameState instance' state
Expand All @@ -139,16 +139,16 @@ class PrintCommand : public Command {
this->shorthand = "p";
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// Variations:
// print [id number] - print state of object with given id
// print [id number] [property] - print state of given property
// print [global id number] - print state of object with given id
// print [global id number] [property] - print state of given property

if (arguments.size() == 1) {
std::cout << "Error: Incorrect number of arguments for 'print' command\n";
}
else if (arguments.size() >= 2 && arguments.size() < 4) {
// print [id number]
// print [global id number]
// Verify that second argument is an integer
unsigned int id;
try {
Expand All @@ -171,24 +171,38 @@ class PrintCommand : public Command {
std::cout << object->to_string() << std::endl;
}
else if (arguments.size() == 3) {
// print [id number] [property]
// print [global id number] [property]

// Attempt to print object's given property (must be a better
// way to do this)
std::string property = arguments.at(2);

if (property.compare("id") == 0) {
std::cout << object->id << std::endl;
if (property.compare("globalID") == 0) {
std::cout << object->globalID << std::endl;
}
else if (property.compare("position") == 0) {
std::cout << glm::to_string(object->position) << std::endl;
else if (property.compare("typeID") == 0) {
std::cout << object->typeID << std::endl;
}
else if (property.compare("velocity") == 0) {
std::cout << glm::to_string(object->velocity) << std::endl;
else if (property.compare("type") == 0) {
std::cout << objectTypeString(object->type) << std::endl;
}
else if (property.compare("acceleration") == 0) {
std::cout << glm::to_string(object->acceleration) << std::endl;

else if (property.compare("physics") == 0) {
std::cout << object->physics.to_string() << std::endl;
}
else if (property.compare("physics.movable") == 0) {
std::cout << (object->physics.movable ? "true" : "false") << std::endl;
}
else if (property.compare("physics.position") == 0) {
std::cout << glm::to_string(object->physics.position) << std::endl;
}
else if (property.compare("physics.velocity") == 0) {
std::cout << glm::to_string(object->physics.velocity) << std::endl;
}
else if (property.compare("physics.acceleration") == 0) {
std::cout << glm::to_string(object->physics.acceleration) << std::endl;
}
else if (property.compare("physics.facing") == 0) {
std::cout << glm::to_string(object->physics.facing) << std::endl;
}
else {
std::cout << "Error: Didn't recognize object property '" << property << "'.\n";
Expand All @@ -208,7 +222,7 @@ class HelpCommand : public Command {
this->shorthand = NO_SHORTHAND;
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// This command ignores arguments (though it may make sense to have
// command descriptors, e.g. help step -> "the step command does ..."

Expand All @@ -232,13 +246,14 @@ class CreateCommand : public Command {
this->shorthand = "c";
}

void run(std::vector<std::string> arguments, GameState& state) override {
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// This command ignores arguments

// Create a new object in the game state
Object* obj = state.createObject();
// Create a new base object in the game state
unsigned int typeID = state.createObject(ObjectType::Object);
Object* obj = state.getBaseObject(typeID);

std::cout << "Created new object (id " << obj->id << ")" << std::endl;
std::cout << "Created new object (global id " << obj->globalID << ")" << std::endl;
}
};

Expand All @@ -249,14 +264,14 @@ class DeleteCommand : public Command {
this->shorthand = "d";
}

void run(std::vector<std::string> arguments, GameState& state) override {
// delete [object id]
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// delete [object global id]
if (arguments.size() != 2) {
std::cout << "Error: Incorrect number of arguments for 'delete' command.\n";
return;
}

// Get id argument
// Get global id argument
unsigned int id;
try {
id = std::stoi(arguments.at(1));
Expand All @@ -270,10 +285,10 @@ class DeleteCommand : public Command {
bool success = state.removeObject(id);

if (success) {
std::cout << "Deleted object (id " << id << ")" << std::endl;
std::cout << "Deleted object (global id " << id << ")" << std::endl;
}
else {
std::cout << "Failed to delete object (id " << id << ") - object does not exist.\n";
std::cout << "Failed to delete object (global id " << id << ") - object does not exist.\n";
}
}
};
Expand All @@ -285,14 +300,14 @@ class SetCommand : public Command {
this->shorthand = NO_SHORTHAND;
}

void run(std::vector<std::string> arguments, GameState& state) override {
// set [object id] [property] [new value]
void run(std::vector<std::string> arguments, ServerGameState& state) override {
// set [object global id] [property] [new value]
if (arguments.size() != 4) {
std::cout << "Error: Incorrect number of arguments for 'set' command.\n";
return;
}

// Get id argument
// Get global id argument
unsigned int id;
try {
id = std::stoi(arguments.at(1));
Expand All @@ -319,50 +334,46 @@ class SetCommand : public Command {
Object* obj = state.getObject(id);

if (obj == nullptr) {
std::cout << "No object with id " << id << " exists.\n";
std::cout << "No object with global id " << id << " exists.\n";
return;
}

// Set property
if (property.compare("id") == 0) {
obj->id = value;
std::cout << "Set object (original id " << id << ") id to " << value << ".\n";
}
else if (property.compare("position.x") == 0) {
obj->position.x = value;
std::cout << "Set object (id " << id << ") position.x to " << value << ".\n";
if (property.compare("physics.position.x") == 0) {
obj->physics.position.x = value;
std::cout << "Set object (global id " << id << ") position.x to " << value << ".\n";
}
else if (property.compare("position.y") == 0) {
obj->position.y = value;
std::cout << "Set object (id " << id << ") position.y to " << value << ".\n";
else if (property.compare("physics.position.y") == 0) {
obj->physics.position.y = value;
std::cout << "Set object (global id " << id << ") position.y to " << value << ".\n";
}
else if (property.compare("position.z") == 0) {
obj->position.z = value;
std::cout << "Set object (id " << id << ") position.z to " << value << ".\n";
else if (property.compare("physics.position.z") == 0) {
obj->physics.position.z = value;
std::cout << "Set object (global id " << id << ") position.z to " << value << ".\n";
}
else if (property.compare("velocity.x") == 0) {
obj->velocity.x = value;
std::cout << "Set object (id " << id << ") velocity.x to " << value << ".\n";
else if (property.compare("physics.velocity.x") == 0) {
obj->physics.velocity.x = value;
std::cout << "Set object (global id " << id << ") velocity.x to " << value << ".\n";
}
else if (property.compare("velocity.y") == 0) {
obj->velocity.y = value;
std::cout << "Set object (id " << id << ") velocity.y to " << value << ".\n";
else if (property.compare("physics.velocity.y") == 0) {
obj->physics.velocity.y = value;
std::cout << "Set object (global id " << id << ") velocity.y to " << value << ".\n";
}
else if (property.compare("velocity.z") == 0) {
obj->velocity.z = value;
std::cout << "Set object (id " << id << ") velocity.z to " << value << ".\n";
else if (property.compare("physics.velocity.z") == 0) {
obj->physics.velocity.z = value;
std::cout << "Set object (global id " << id << ") velocity.z to " << value << ".\n";
}
else if (property.compare("acceleration.x") == 0) {
obj->acceleration.x = value;
std::cout << "Set object (id " << id << ") acceleration.x to " << value << ".\n";
else if (property.compare("physics.acceleration.x") == 0) {
obj->physics.acceleration.x = value;
std::cout << "Set object (global id " << id << ") acceleration.x to " << value << ".\n";
}
else if (property.compare("acceleration.y") == 0) {
obj->acceleration.y = value;
std::cout << "Set object (id " << id << ") acceleration.y to " << value << ".\n";
else if (property.compare("physics.acceleration.y") == 0) {
obj->physics.acceleration.y = value;
std::cout << "Set object (global id " << id << ") acceleration.y to " << value << ".\n";
}
else if (property.compare("acceleration.z") == 0) {
obj->acceleration.z = value;
std::cout << "Set object (id " << id << ") acceleration.z to " << value << ".\n";
else if (property.compare("physics.acceleration.z") == 0) {
obj->physics.acceleration.z = value;
std::cout << "Set object (global id " << id << ") acceleration.z to " << value << ".\n";
}
else {
std::cout << "Error: Didn't recognize object property '" << property << "'.\n";
Expand Down
5 changes: 3 additions & 2 deletions include/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "shared/network/session.hpp"
#include "shared/utilities/config.hpp"
#include "shared/utilities/typedefs.hpp"
#include "shared/game/servergamestate.hpp"

using boost::asio::ip::tcp;

Expand Down Expand Up @@ -49,6 +50,6 @@ class Server {
/// @brief Mapping from player id to session for that player.
std::unordered_map<EntityID, std::shared_ptr<Session>> sessions;

/// @brief Master copy of the GameState, living on the server
GameState state;
/// @brief Master copy of the ServerGameState, living on the server
ServerGameState state;
};
13 changes: 7 additions & 6 deletions include/shared/game/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "shared/utilities/typedefs.hpp"
#include "shared/utilities/serialize.hpp"
#include "shared/utilities/serialize_macro.hpp"
#include "shared/game/gamestate.hpp"
//#include "shared/game/gamestate.hpp"
#include "shared/game/sharedgamestate.hpp"


/****************************************************
Expand Down Expand Up @@ -59,15 +60,15 @@ struct LobbyActionEvent {
};

/**
* Event sent by the server to a client, telling the client to update their GameState
* to this new GameState
* Event sent by the server to a client, telling the client to update their SharedGameState
* to this new SharedGameState
*/
struct LoadGameStateEvent {
// Dummy value doesn't matter because will be overridden with whatever you deserialize
LoadGameStateEvent() : state(GameState(GamePhase::TITLE_SCREEN, GameConfig{})){}
LoadGameStateEvent(GameState state) : state(state) {}
LoadGameStateEvent() : state(SharedGameState(GamePhase::TITLE_SCREEN, GameConfig{})){}
LoadGameStateEvent(SharedGameState state) : state(state) {}

GameState state;
SharedGameState state;

DEF_SERIALIZE(Archive& ar, const unsigned int version) {
ar & state;
Expand Down
Loading