Skip to content

Commit

Permalink
Merge branch 'main' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
afwbkbc committed Nov 8, 2023
2 parents 1da65e7 + 6f74578 commit 02b54fc
Show file tree
Hide file tree
Showing 39 changed files with 834 additions and 157 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ Gentoo: `emerge cmake libsdl2 sdl2-image freetype glu glew ossp-uuid yaml-cpp`

Ubuntu: `apt install cmake build-essential libfreetype-dev libsdl2-dev libsdl2-image-dev libglu-dev libglew-dev libossp-uuid-dev libyaml-cpp-dev`

ArchLinux: `pacman -Syu cmake base-devel freetype2 sdl2 sdl2_image glew yaml-cpp` (you'll need to install `ossp-uuid` manually because it's not in repos)
ArchLinux:
`pacman -Syu cmake base-devel freetype2 sdl2 sdl2_image glew yaml-cpp` (you'll need to install `ossp-uuid` manually because it's not in repos)
AUR-package: https://aur.archlinux.org/packages/uuid

FreeBSD: `pkg install pkgconf cmake sdl2 sdl2_image glew ossp-uuid yaml-cpp`

Expand Down
1 change: 1 addition & 0 deletions src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ SET( SRC ${SRC}
${PWD}/Player.cpp
${PWD}/Slot.cpp
${PWD}/Slots.cpp
${PWD}/Event.cpp

PARENT_SCOPE )
51 changes: 51 additions & 0 deletions src/game/Event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Event.h"

#include <cstring>

#include "base/Base.h"

namespace game {

Event::Event( const Event::event_type_t type )
: type( type ) {
memset( &data, 0, sizeof( data ) );
}

Event::Event( const Event& other )
: type( other.type ) {
switch ( type ) {
case ET_QUIT: {
NEW( data.quit.reason, std::string, *other.data.quit.reason );
break;
}
case ET_GLOBAL_MESSAGE: {
NEW( data.global_message.message, std::string, *other.data.global_message.message );
break;
}
default: {
//
}
}
}

Event::~Event() {
switch ( type ) {
case ET_QUIT: {
if ( data.quit.reason ) {
DELETE( data.quit.reason );
}
break;
}
case ET_GLOBAL_MESSAGE: {
if ( data.global_message.message ) {
DELETE( data.global_message.message );
}
break;
}
default: {
//
}
}
}

}
34 changes: 34 additions & 0 deletions src/game/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <vector>
#include <string>

namespace game {

class Event {
public:

enum event_type_t {
ET_NONE,
ET_QUIT,
ET_GLOBAL_MESSAGE,
};
Event( const event_type_t type );
Event( const Event& other );
virtual ~Event();

const event_type_t type = ET_NONE;

union {
struct {
std::string* reason;
} quit;
struct {
std::string* message;
} global_message;
} data;
};

typedef std::vector< Event > game_events_t;

}
129 changes: 121 additions & 8 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ mt_id_t Game::MT_Init( State* state ) {
return MT_CreateRequest( request );
}

mt_id_t Game::MT_Chat( const std::string& message ) {
MT_Request request = {};
request.op = OP_CHAT;
NEW( request.data.save_map.path, std::string, message );
return MT_CreateRequest( request );
}

mt_id_t Game::MT_GetMapData() {
MT_Request request = {};
request.op = OP_GET_MAP_DATA;
Expand Down Expand Up @@ -66,6 +73,12 @@ mt_id_t Game::MT_EditMap( const types::Vec2< size_t >& tile_coords, map_editor::
return MT_CreateRequest( request );
}

mt_id_t Game::MT_GetEvents() {
MT_Request request = {};
request.op = OP_GET_EVENTS;
return MT_CreateRequest( request );
}

#ifdef DEBUG
#define x( _method, _op ) \
mt_id_t Game::_method( const std::string& path ) { \
Expand All @@ -92,6 +105,9 @@ void Game::Start() {
m_game_state = GS_NONE;
m_init_cancel = false;

ASSERT( !m_pending_events, "game events already set" );
NEW( m_pending_events, game_events_t );

NEW( m_random, util::Random );

#ifdef DEBUG
Expand All @@ -112,6 +128,16 @@ void Game::Stop() {
DELETE( m_map_editor );
m_map_editor = nullptr;

ASSERT( m_pending_events, "game events not set" );
DELETE( m_pending_events );
m_pending_events = nullptr;

if ( m_state ) {
DELETE( m_state );
m_state = nullptr;
m_connection = nullptr;
}

MTModule::Stop();
}

Expand Down Expand Up @@ -155,7 +181,7 @@ void Game::Iterate() {
#endif
}
else {
// notify server of successful download and complete initialization
// notify server of successful download and continue initialization
m_slot->SetPlayerFlag( Slot::PF_MAP_DOWNLOADED );
m_connection->UpdateSlot( m_slot_num, m_slot, true );
}
Expand Down Expand Up @@ -208,6 +234,13 @@ void Game::Iterate() {
m_old_map = nullptr;
}

// notify server of successful initialization
m_slot->SetPlayerFlag( Slot::PF_GAME_INITIALIZED );
if ( m_connection ) {
m_connection->UpdateSlot( m_slot_num, m_slot, true );
}

// run main loop
m_game_state = GS_RUNNING;
}
else {
Expand Down Expand Up @@ -664,6 +697,34 @@ const MT_Response Game::ProcessRequest( const MT_Request& request, MT_CANCELABLE
response.result = R_SUCCESS;
break;
}
case OP_CHAT: {
Log( "got chat message request: " + *request.data.chat.message );

if ( m_connection ) {
m_connection->Message( *request.data.chat.message );
}
else {
auto e = Event( Event::ET_GLOBAL_MESSAGE );
NEW( e.data.global_message.message, std::string, "<" + m_player->GetPlayerName() + "> " + *request.data.chat.message );
AddEvent( e );
}

response.result = R_SUCCESS;
break;
}
case OP_GET_EVENTS: {
//Log( "got events request" );
if ( !m_pending_events->empty() ) {
Log( "sending " + std::to_string( m_pending_events->size() ) + " events to frontend" );
response.data.get_events.events = m_pending_events; // will be destroyed in DestroyResponse
NEW( m_pending_events, game_events_t ); // reset
}
else {
response.data.get_events.events = nullptr;
}
response.result = R_SUCCESS;
break;
}
default: {
ASSERT( false, "unknown request op " + std::to_string( request.op ) );
}
Expand All @@ -680,6 +741,12 @@ void Game::DestroyRequest( const MT_Request& request ) {
}
break;
}
case OP_CHAT: {
if ( request.data.chat.message ) {
DELETE( request.data.chat.message );
}
break;
}
default: {
// nothing to delete
}
Expand Down Expand Up @@ -728,20 +795,34 @@ void Game::DestroyResponse( const MT_Response& response ) {
}
break;
}
case OP_GET_EVENTS: {
if ( response.data.get_events.events ) {
DELETE( response.data.get_events.events );
}
break;
}
default: {
// nothing to delete
}
}
}
}

void Game::AddEvent( const Event& event ) {
Log( "Sending event (type=" + std::to_string( event.type ) + ")" );
m_pending_events->push_back( event );
}

void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {

ASSERT( !m_connection || m_game_state == GS_NONE, "multiplayer game already initializing or running" );
ASSERT( m_game_state == GS_NONE || m_game_state == GS_RUNNING, "game still initializing" );

Log( "Initializing game" );

ASSERT( m_pending_events, "pending events not set" );
m_pending_events->clear();

m_game_state = GS_PREPARING_MAP;
m_initialization_error = "";

Expand All @@ -754,8 +835,24 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {
m_connection->ResetHandlers();
m_connection->IfServer(
[ this ]( Server* connection ) -> void {
connection->m_on_player_leave = [ this ]( const size_t slot_num, Slot* slot, const Player* player ) -> void {
Log( "Player " + player->GetPlayerName() + " left the game." );

connection->m_on_player_join = [ this, connection ]( const size_t slot_num, Slot* slot, const Player* player ) -> void {
Log( "Player " + player->GetFullName() + " is connecting..." );
connection->GlobalMessage( "Connection from " + player->GetFullName() + "..." );
// actual join will happen after he downloads and initializes map
};

connection->m_on_flags_update = [ this, connection ]( const size_t slot_num, Slot* slot, const Slot::player_flag_t old_flags, const Slot::player_flag_t new_flags ) -> void {
if ( !( old_flags & Slot::PF_GAME_INITIALIZED ) && ( new_flags & Slot::PF_GAME_INITIALIZED ) ) {
const auto* player = slot->GetPlayer();
Log( player->GetFullName() + " joined the game." );
connection->GlobalMessage( player->GetFullName() + " joined the game." );
}
};

connection->m_on_player_leave = [ this, connection ]( const size_t slot_num, Slot* slot, const Player* player ) -> void {
Log( "Player " + player->GetFullName() + " left the game." );
connection->GlobalMessage( player->GetFullName() + " left the game." );
};

connection->m_on_map_request = [ this ]() -> const std::string {
Expand All @@ -773,21 +870,32 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {

m_connection->IfClient(
[ this ]( Client* connection ) -> void {
connection->m_on_disconnect = [ this ]() -> void {
Log( "Server disconnected" );
connection->m_on_disconnect = [ this, connection ]() -> void {
Log( "Connection lost" );
DELETE( connection );
m_state->DetachConnection();
m_connection = nullptr;
if ( m_game_state != GS_RUNNING ) {
m_initialization_error = "Lost connection to server";
}
else {
// TODO: return to main menu
auto e = Event( Event::ET_QUIT );
NEW( e.data.quit.reason, std::string, "Lost connection to server" );
AddEvent( e );
}
};
connection->m_on_error = [ this ]( const std::string& reason ) -> void {
connection->m_on_error = [ this, connection ]( const std::string& reason ) -> void {
m_initialization_error = reason;
};
}
);

m_connection->m_on_message = [ this ]( const std::string& message ) -> void {
auto e = Event( Event::ET_GLOBAL_MESSAGE );
NEW( e.data.global_message.message, std::string, message );
AddEvent( e );
};

}
else {
m_slot_num = 0;
Expand Down Expand Up @@ -878,7 +986,6 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {
// wait for server to initialize
ui->SetLoaderText( "Waiting for server" );

//connection->m_on_game_state_change = [ this, connection, ui ]( const Connection::game_state_t state ) -> void {
const auto f_download_map = [ this, ui, connection ] {

ui->SetLoaderText( "Downloading map" );
Expand Down Expand Up @@ -939,9 +1046,15 @@ void Game::ResetGame() {
m_map = nullptr;
}

ASSERT( m_pending_events, "pending events not set" );
m_pending_events->clear();

if ( m_state ) {
// ui thread will reset state as needed
m_state = nullptr;
if ( m_connection ) {
m_connection->ResetHandlers();
}
m_connection = nullptr;
}
}
Expand Down
Loading

0 comments on commit 02b54fc

Please sign in to comment.