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 Sep 3, 2023
2 parents 1727980 + d21304c commit 545a011
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 226 deletions.
9 changes: 7 additions & 2 deletions src/game/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@ void MapSettings::Unserialize( Buffer buf ) {
clouds = buf.ReadInt();
}

void GlobalSettings::Initialize() {
game_rules.Initialize();
global_difficulty = game_rules.GetDefaultDifficultyLevel();
}

const Buffer GlobalSettings::Serialize() const {
Buffer buf;

buf.WriteString( map.Serialize().ToString() );
buf.WriteInt( difficulty );
buf.WriteString( game_rules.Serialize().ToString() );
buf.WriteString( global_difficulty.Serialize().ToString() );
buf.WriteString( game_name );

return buf;
}

void GlobalSettings::Unserialize( Buffer buf ) {
map.Unserialize( buf.ReadString() );
difficulty = buf.ReadInt();
game_rules.Unserialize( buf.ReadString() );
global_difficulty.Unserialize( buf.ReadString() );
game_name = buf.ReadString();
}

Expand Down
17 changes: 5 additions & 12 deletions src/game/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,15 @@ CLASS( MapSettings, Serializable )

// settings that are synced between players (host has authority)
CLASS( GlobalSettings, Serializable )
typedef uint8_t parameter_t;

MapSettings map = {};
void Initialize();

// TODO: use difficulty levels from rules
static constexpr parameter_t DIFFICULTY_CITIZEN = 1;
static constexpr parameter_t DIFFICULTY_SPECIALIST = 2;
static constexpr parameter_t DIFFICULTY_TALENT= 3;
static constexpr parameter_t DIFFICULTY_LIBRARIAN = 4;
static constexpr parameter_t DIFFICULTY_THINKER = 5;
static constexpr parameter_t DIFFICULTY_TRANSCEND = 6;
parameter_t difficulty = DIFFICULTY_CITIZEN;

rules::Default game_rules; // TODO: custom rules
typedef uint8_t parameter_t;

MapSettings map = {};

rules::Default game_rules = {}; // TODO: custom rules
rules::DifficultyLevel global_difficulty = {};

std::string game_name = "";

Expand Down
4 changes: 4 additions & 0 deletions src/game/connection/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ void Client::UpdateSlot( const size_t slot_num, const Slot* slot ) {
m_network->MT_SendPacket( p );
}

void Client::UpdateGameSettings() {
// client can't change them
}

void Client::Error( const std::string& reason ) {
Log( "Network protocol error: " + reason );
Disconnect( "Network protocol error" );
Expand Down
2 changes: 1 addition & 1 deletion src/game/connection/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CLASS( Client, Connection)
Client( LocalSettings* const settings );

void UpdateSlot( const size_t slot_num, const Slot* slot ) override;

void UpdateGameSettings() override;
protected:
void ProcessEvent( const network::Event& event ) override;

Expand Down
2 changes: 1 addition & 1 deletion src/game/connection/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CLASS( Connection, base::Module )
const Player* GetPlayer() const;

virtual void UpdateSlot( const size_t slot_num, const Slot* slot ) = 0;

virtual void UpdateGameSettings() = 0;
protected:
network::Network * const m_network = g_engine->GetNetwork();

Expand Down
33 changes: 22 additions & 11 deletions src/game/connection/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ void Server::ProcessEvent( const network::Event& event ) {
case Event::ET_LISTEN: {
ASSERT( !m_player, "player already set" );
Log( "Listening" );
m_state->m_settings.global.Initialize();
m_state->m_slots.Resize( 7 ); // TODO: make dynamic?
const auto& rules = m_state->m_settings.global.game_rules;
NEW( m_player, ::game::Player, {
m_state->m_settings.local.player_name,
::game::Player::PR_HOST,
m_state->m_settings.global.game_rules.m_factions[ 0 ],
m_state->m_settings.global.game_rules.m_difficulty_levels[ 6 ] // transcend by default
rules.GetDefaultFaction(),
rules.GetDefaultDifficultyLevel(),
});
m_state->AddPlayer( m_player );
m_slot = 0; // host always has slot 0
Expand Down Expand Up @@ -109,25 +111,19 @@ void Server::ProcessEvent( const network::Event& event ) {
break;
}

const auto& rules = m_state->m_settings.global.game_rules;
NEWV( player, ::game::Player, {
packet.data.str,
::game::Player::PR_PLAYER,
m_state->m_settings.global.game_rules.m_factions[ 0 ],
m_state->m_settings.global.game_rules.m_difficulty_levels[ 6 ] // transcend by default
rules.GetDefaultFaction(),
rules.GetDefaultDifficultyLevel(),
});
m_state->AddPlayer( player );

m_state->AddCIDSlot( event.cid, slot_num );
auto& slot = m_state->m_slots.GetSlot( slot_num );
slot.SetPlayer( player, event.cid, event.data.remote_address );

{
Log( "Sending global settings to " + std::to_string( event.cid ) );
Packet p;
p.type = Packet::PT_GLOBAL_SETTINGS;
p.data.str = m_state->m_settings.global.Serialize().ToString();
m_network->MT_SendPacket( p, event.cid );
}
{
Log( "Sending players list to " + std::to_string( event.cid ) );
Packet p;
Expand All @@ -136,6 +132,7 @@ void Server::ProcessEvent( const network::Event& event ) {
p.data.str = m_state->m_slots.Serialize().ToString();
g_engine->GetNetwork()->MT_SendPacket( p, event.cid );
}
SendGlobalSettings( event.cid );

if ( m_on_player_join ) {
m_on_player_join( slot_num, &slot, player );
Expand Down Expand Up @@ -216,6 +213,12 @@ void Server::UpdateSlot( const size_t slot_num, const Slot* slot ) {
});
}

void Server::UpdateGameSettings() {
Broadcast( [ this ]( const size_t cid ) -> void {
SendGlobalSettings( cid );
});
}

void Server::Kick( const size_t cid, const std::string& reason = "" ) {
Log( "Kicking " + std::to_string( cid ) + ( !reason.empty() ? " (reason: " + reason + ")" : "" ) );
Packet p;
Expand All @@ -235,5 +238,13 @@ void Server::Error( const size_t cid, const std::string& reason ) {
Kick( cid, "Network protocol error" );
}

void Server::SendGlobalSettings( size_t cid ) {
Log( "Sending global settings to " + std::to_string( cid ) );
Packet p;
p.type = Packet::PT_GLOBAL_SETTINGS;
p.data.str = m_state->m_settings.global.Serialize().ToString();
m_network->MT_SendPacket( p, cid );
}

}
}
3 changes: 2 additions & 1 deletion src/game/connection/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CLASS( Server, Connection )
Server( LocalSettings* const settings );

void UpdateSlot( const size_t slot_num, const Slot* slot ) override;
void UpdateGameSettings() override;

void KickFromSlot( const size_t slot_num, const std::string& reason = "Kicked by host" );
void BanFromSlot( const size_t slot_num, const std::string& reason = "Banned by host" );
Expand All @@ -22,7 +23,7 @@ CLASS( Server, Connection )
void Kick( const size_t cid, const std::string& reason );
void KickFromSlot( Slot& slot, const std::string& reason );
void Error( const size_t cid, const std::string& reason );

void SendGlobalSettings( size_t cid );
};

}
Expand Down
6 changes: 3 additions & 3 deletions src/game/rules/Rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const types::Buffer Rules::Serialize() const {
buf.WriteInt( difficulty_level.first );
buf.WriteString( difficulty_level.second.Serialize().ToString() );
}

return buf;
}

Expand All @@ -36,14 +36,14 @@ void Rules::Unserialize( types::Buffer buf ) {
const size_t faction_id = buf.ReadInt();
m_factions[ faction_id ].Unserialize( buf.ReadString() );
}

m_difficulty_levels.clear();
const size_t difficulty_levels_count = buf.ReadInt();
for ( size_t i = 0 ; i < difficulty_levels_count ; i++ ) {
const size_t difficulty_level_id = buf.ReadInt();
m_difficulty_levels[ difficulty_level_id ].Unserialize( buf.ReadString() );
}

m_is_initialized = true;

}
Expand Down
3 changes: 3 additions & 0 deletions src/game/rules/Rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ CLASS( Rules, types::Serializable )
std::map< size_t, Faction > m_factions;
std::map< size_t, DifficultyLevel > m_difficulty_levels;

virtual const Faction& GetDefaultFaction() const = 0;
virtual const DifficultyLevel& GetDefaultDifficultyLevel() const = 0;

void Initialize();

const types::Buffer Serialize() const;
Expand Down
56 changes: 31 additions & 25 deletions src/game/rules/default/Default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,101 @@ void Default::InitRules() {

// SMAC

{ 0, {
{ FT_RANDOM, {
"Random",
Color::FromRGB( 255, 255, 255 )
} },

{ 1, {
{ FT_GAIANS, {
"Gaians",
Color::FromRGB( 16, 228, 0 )
} },

{ 2, {
{ FT_HIVE, {
"Hive",
Color::FromRGB( 0, 97, 235 )
} },

{ 3, {
{ FT_UNIVERSITY, {
"University",
Color::FromRGB( 216, 224, 235 )
} },

{ 4, {
{ FT_MORGANITES, {
"Morganites",
Color::FromRGB( 255, 255, 0 )
} },

{ 5, {
{ FT_SPARTANS, {
"Spartans",
Color::FromRGB( 137, 166, 166 )
} },

{ 6, {
{ FT_BELIEVERS, {
"Believers",
Color::FromRGB( 224, 156, 28 )
} },

{ 7, {
{ FT_PEACEKEEPERS, {
"Peacekeepers",
Color::FromRGB( 164, 176, 232 )
} },

// SMACX

{ 8, {
{ FT_CONSCIOUSNESS, {
"Consciousness",
Color::FromRGB( 44, 128, 104 )
} },

{ 9, {
{ FT_PIRATES, {
"Pirates",
Color::FromRGB( 0, 255, 255 )
} },

{ 10, {
{ FT_DRONES, {
"Drones",
Color::FromRGB( 173, 196, 192 )
} },

{ 11, {
{ FT_ANGELS, {
"Angels",
Color::FromRGB( 103, 91, 181 )
} },

{ 12, {
{ FT_PLANETCULT, {
"Planet Cult",
Color::FromRGB( 232, 84, 84 )
} },

{ 13, {
{ FT_CARETAKERS, {
"Caretakers",
Color::FromRGB( 116, 156, 56 )
} },

{ 14, {
{ FT_USURPERS, {
"Usurpers",
Color::FromRGB( 212, 208, 116 )
} },
};

m_difficulty_levels = {

{ 1, { "Citizen", -3 } },
{ 2, { "Specialist", -2 } },
{ 3, { "Talent", -1 } },
{ 4, { "Librarian", 0 } },
{ 5, { "Thinker", 1 } },
{ 6, { "Transcend", 2 } },

{ DT_CITIZEN, { "Citizen", -3 } },
{ DT_SPECIALIST, { "Specialist", -2 } },
{ DT_TALENT, { "Talent", -1 } },
{ DT_LIBRARIAN, { "Librarian", 0 } },
{ DT_THINKER, { "Thinker", 1 } },
{ DT_TRANSCEND, { "Transcend", 2 } },
};


}

const Faction& Default::GetDefaultFaction() const {
return m_factions.at( FT_RANDOM );
}

const DifficultyLevel& Default::GetDefaultDifficultyLevel() const {
return m_difficulty_levels.at( DT_TRANSCEND );
}

}
Expand Down
31 changes: 31 additions & 0 deletions src/game/rules/default/Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,40 @@ namespace rules {

CLASS( Default, Rules )

const Faction& GetDefaultFaction() const;
const DifficultyLevel& GetDefaultDifficultyLevel() const;

protected:
void InitRules();

private:

enum faction_type_t {
FT_RANDOM,
FT_GAIANS,
FT_HIVE,
FT_UNIVERSITY,
FT_MORGANITES,
FT_SPARTANS,
FT_BELIEVERS,
FT_PEACEKEEPERS,
FT_CONSCIOUSNESS,
FT_PIRATES,
FT_DRONES,
FT_ANGELS,
FT_PLANETCULT,
FT_CARETAKERS,
FT_USURPERS,
};

enum difficulty_type_t {
DT_CITIZEN,
DT_SPECIALIST,
DT_TALENT,
DT_LIBRARIAN,
DT_THINKER,
DT_TRANSCEND,
};
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/task/mainmenu/SlidingMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CLASS( SlidingMenu, MenuObject )

protected:
bool IsReadyToClose() const;

private:
const MenuBlock::choices_t m_choices = {};
std::string m_choice = "";
Expand Down
Loading

0 comments on commit 545a011

Please sign in to comment.