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 Feb 15, 2024
2 parents e08f3e5 + f74b2b6 commit 4b04eca
Show file tree
Hide file tree
Showing 68 changed files with 985 additions and 389 deletions.
39 changes: 34 additions & 5 deletions gse/default/main.gls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ while (i < #size(units)) {
i++;
}

#game.on_start(() => {
#game.on.start(() => {
let y = 0;
while (y < #game.map.height) {
let x = 0;
while (x < #game.map.width) {
if (x % 2 == y % 2) {
if (#game.random.get_int(0, 1) == 1) {
let tile = #game.map.get_tile(x, y);
let unit = null;
if (tile.is_land) {
if (#game.random.get_int(0, 2) != 1) {
#game.units.spawn('MindWorms', tile);
unit = #game.units.spawn('MindWorms', tile);
} else {
#game.units.spawn('SporeLauncher', tile);
unit = #game.units.spawn('SporeLauncher', tile);
}
} else {
if (#game.random.get_int(0, 1) == 1) {
#game.units.spawn('SeaLurk', tile);
unit = #game.units.spawn('SeaLurk', tile);
}
}
}
Expand All @@ -32,5 +33,33 @@ while (i < #size(units)) {
}
y++;
}
//#game.units.spawn('MINDWORMS', #game.map.get_tile(0, 0));
});

#game.on.spawn_unit((unit) => {
let def = unit.get_def();
if (def.name != 'MindWorms') {
let tile = unit.get_tile();
let neighbours = [tile.get_W(), tile.get_NW(), tile.get_N(), tile.get_NE(), tile.get_E(), tile.get_SE(), tile.get_S(), tile.get_SW()];
let i = 0;
let sz = #size(neighbours);
let nearby_units_count = 0;
while (i < sz) {
let neighbour = neighbours[i];
if (!#is_empty(neighbour.get_units())) {
nearby_units_count++;
}
i++;
}
if (nearby_units_count > 2) {
#game.units.despawn(unit);
}
}
});

#game.on.despawn_unit((unit) => {
let def = unit.get_def();
#print(#to_string(def));
if (def.name == 'SporeLauncher') {
#game.units.spawn('MindWorms', unit.get_tile());
}
});
4 changes: 0 additions & 4 deletions src/base/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ Base::Base()
//
}

Base::~Base() {
//
};

const std::string Base::GetNamespace() const {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace base {
class Base {
public:
Base();
virtual ~Base();
virtual ~Base() = default;

virtual const std::string GetNamespace() const;
const std::string GetName() const;
Expand Down
2 changes: 2 additions & 0 deletions src/base/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef size_t mt_id_t;

class Module : public Base {
public:
virtual ~Module() = default;

virtual void Start() {}
virtual void Stop() {}
virtual void Iterate() {}
Expand Down
4 changes: 2 additions & 2 deletions src/debug/MemoryWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,11 @@ void MemoryWatcher::GLDeleteTextures( GLsizei n, GLuint* textures, const std::st
m_opengl.textures_framebuffers.erase( it2 );
}

m_opengl.textures.erase( it );

DEBUG_STAT_CHANGE_BY( opengl_textures_size, -it->second.size );
DEBUG_STAT_DEC( opengl_textures_count );

m_opengl.textures.erase( it );

glDeleteTextures_real( n, textures );
}

Expand Down
14 changes: 9 additions & 5 deletions src/game/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ Event::Event( const Event& other )
NEW( data.global_message.message, std::string, *other.data.global_message.message );
break;
}
case ET_SPAWN_UNIT: {
NEW( data.spawn_unit.serialized_unit, std::string, *other.data.spawn_unit.serialized_unit );
data.spawn_unit.coords = other.data.spawn_unit.coords;
case ET_UNIT_SPAWN: {
NEW( data.unit_spawn.serialized_unit, std::string, *other.data.unit_spawn.serialized_unit );
data.unit_spawn.coords = other.data.unit_spawn.coords;
break;
}
case ET_UNIT_DESPAWN: {
data.unit_despawn.unit_id = other.data.unit_despawn.unit_id;
break;
}
default: {
Expand All @@ -53,8 +57,8 @@ Event::~Event() {
DELETE( data.global_message.message );
break;
}
case ET_SPAWN_UNIT: {
DELETE( data.spawn_unit.serialized_unit );
case ET_UNIT_SPAWN: {
DELETE( data.unit_spawn.serialized_unit );
break;
}
default: {
Expand Down
8 changes: 6 additions & 2 deletions src/game/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Event {
ET_QUIT,
ET_ERROR,
ET_GLOBAL_MESSAGE,
ET_SPAWN_UNIT,
ET_UNIT_SPAWN,
ET_UNIT_DESPAWN,
};
Event( const event_type_t type );
Event( const Event& other );
Expand All @@ -41,7 +42,10 @@ class Event {
float y;
float z;
} coords;
} spawn_unit;
} unit_spawn;
struct {
size_t unit_id;
} unit_despawn;
} data;
};

Expand Down
105 changes: 87 additions & 18 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,23 +272,33 @@ void Game::Iterate() {
ASSERT( !m_bindings, "bindings already set" );
NEW( m_bindings, Bindings, this );

// run main gse entrypoint
ASSERT( !m_current_turn, "turn is already initialized" );

try {
// run main gse entrypoint
m_bindings->RunMain();

// start initial turn
NextTurn();

// start main loop
m_game_state = GS_RUNNING;

}
catch ( gse::Exception& e ) {
Log( (std::string)"Initialization failed: " + e.ToStringAndCleanup() );
f_init_failed( e.what() );
}

if ( m_game_state == GS_RUNNING ) {
ASSERT( !m_current_turn, "turn is already initialized" );
NextTurn();
m_bindings->Call( Bindings::CS_ONSTART );

for ( auto& it : m_unprocessed_events ) {
ProcessGameEvent( it );
}
m_unprocessed_events.clear();

if ( m_state->IsMaster() ) {
m_bindings->Call( Bindings::CS_ON_START );
}
}
}
else {
Expand Down Expand Up @@ -734,7 +744,7 @@ const MT_Response Game::ProcessRequest( const MT_Request& request, MT_CANCELABLE
Log( "got chat message request: " + *request.data.chat.message );

if ( m_connection ) {
m_connection->Message( *request.data.chat.message );
m_connection->SendMessage( *request.data.chat.message );
}
else {
Message( "<" + m_player->GetPlayerName() + "> " + *request.data.chat.message );
Expand Down Expand Up @@ -877,21 +887,30 @@ const unit::Def* Game::GetUnitDef( const std::string& name ) const {
}
}

void Game::AddGameEvent( const event::Event* event, gse::Context* ctx, const gse::si_t& si ) {
ASSERT( m_current_turn, "turn not initialized" );
event->Apply( this );
m_current_turn->AddEvent( event );
const gse::Value Game::AddGameEvent( const event::Event* event, gse::Context* ctx, const gse::si_t& si ) {
if ( m_connection ) {
m_connection->SendGameEvent( event );
}
const auto result = ProcessGameEvent( event );
return result;
}

void Game::SpawnUnit( unit::Unit* unit ) {
ASSERT( m_units.find( unit->m_id ) == m_units.end(), "duplicate unit id" );
Log( "Spawning unit ('" + unit->m_def->m_name + "') at [ " + std::to_string( unit->m_pos_x ) + " " + std::to_string( unit->m_pos_y ) + " ]" );
m_units.insert_or_assign( unit->m_id, unit );
const auto* tile = m_map->GetTile( unit->m_pos_x, unit->m_pos_y );

auto* tile = m_map->GetTile( unit->m_pos_x, unit->m_pos_y );
const auto* ts = m_map->GetTileState( tile );

ASSERT( m_units.find( unit->m_id ) == m_units.end(), "duplicate unit id" );
m_units.insert_or_assign( unit->m_id, unit );
unit->m_tile = tile;
ASSERT( tile->units.find( unit->m_id ) == tile->units.end(), "duplicate unit id in tile" );
tile->units.insert_or_assign( unit->m_id, unit );

// notify frontend
auto e = Event( Event::ET_SPAWN_UNIT );
NEW( e.data.spawn_unit.serialized_unit, std::string, unit::Unit::Serialize( unit ).ToString() );
auto e = Event( Event::ET_UNIT_SPAWN );
NEW( e.data.unit_spawn.serialized_unit, std::string, unit::Unit::Serialize( unit ).ToString() );
const auto l = tile->is_water_tile
? map::TileState::LAYER_WATER
: map::TileState::LAYER_LAND;
Expand All @@ -900,12 +919,46 @@ void Game::SpawnUnit( unit::Unit* unit ) {
ts->coord.y,
ts->layers[ l ].coords
);
e.data.spawn_unit.coords = {
e.data.unit_spawn.coords = {
c.x,
-c.y,
c.z
};
AddEvent( e );

if ( m_state->IsMaster() ) {
m_bindings->Call( Bindings::CS_ON_SPAWN_UNIT, { unit->Wrap() } );
}
}

void Game::DespawnUnit( const size_t unit_id ) {
const auto& it = m_units.find( unit_id );
ASSERT( it != m_units.end(), "unit id not found" );
auto* unit = it->second;

auto e = Event( Event::ET_UNIT_DESPAWN );
e.data.unit_despawn.unit_id = unit_id;
AddEvent( e );

ASSERT( unit->m_tile, "unit tile not assigned" );
auto* tile = unit->m_tile;
const auto& tile_it = tile->units.find( unit->m_id );
ASSERT( tile_it != tile->units.end(), "unit id not found in tile" );
tile->units.erase( tile_it );

m_units.erase( it );

if ( m_state->IsMaster() ) {
m_bindings->Call( Bindings::CS_ON_DESPAWN_UNIT, { unit->Wrap() } );
}

delete unit;
}

const gse::Value Game::ProcessGameEvent( const event::Event* event ) {
ASSERT( m_current_turn, "turn not initialized" );
m_current_turn->AddEvent( event );
return event->Apply( this );
}

void Game::AddEvent( const Event& event ) {
Expand Down Expand Up @@ -970,9 +1023,8 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {

m_connection->IfClient(
[ this ]( Client* connection ) -> void {
connection->m_on_disconnect = [ this, connection ]() -> void {
connection->m_on_disconnect = [ this ]() -> bool {
Log( "Connection lost" );
DELETE( connection );
m_state->DetachConnection();
m_connection = nullptr;
if ( m_game_state != GS_RUNNING ) {
Expand All @@ -981,9 +1033,11 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {
else {
Quit( "Lost connection to server" );
}
return true;
};
connection->m_on_error = [ this, connection ]( const std::string& reason ) -> void {
connection->m_on_error = [ this ]( const std::string& reason ) -> bool {
m_initialization_error = reason;
return true;
};
}
);
Expand All @@ -994,6 +1048,15 @@ void Game::InitGame( MT_Response& response, MT_CANCELABLE ) {
AddEvent( e );
};

m_connection->m_on_game_event = [ this ]( const event::Event* event ) -> void {
if ( m_game_state == GS_RUNNING ) {
ProcessGameEvent( event );
}
else {
m_unprocessed_events.push_back( event );
}
};

}
else {
m_slot_num = 0;
Expand Down Expand Up @@ -1139,6 +1202,11 @@ void Game::ResetGame() {
m_slot_num = 0;
m_slot = nullptr;

for ( auto& it : m_unprocessed_events ) {
delete it;
}
m_unprocessed_events.clear();

for ( auto& it : m_units ) {
delete it.second;
}
Expand Down Expand Up @@ -1171,6 +1239,7 @@ void Game::ResetGame() {
// ui thread will reset state as needed
m_state = nullptr;
if ( m_connection ) {
m_connection->Disconnect();
m_connection->ResetHandlers();
}
m_connection = nullptr;
Expand Down
6 changes: 5 additions & 1 deletion src/game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ CLASS( Game, MTModule )
void OnGSEError( gse::Exception& e );
void AddUnitDef( const std::string& name, const unit::Def* def, gse::Context* ctx, const gse::si_t& si );
const unit::Def* GetUnitDef( const std::string& name ) const;
void AddGameEvent( const event::Event* event, gse::Context* ctx, const gse::si_t& si );
const gse::Value AddGameEvent( const event::Event* event, gse::Context* ctx, const gse::si_t& si );
void SpawnUnit( unit::Unit* unit );
void DespawnUnit( const size_t unit_id );

private:

const gse::Value ProcessGameEvent( const event::Event* event );

std::unordered_map< std::string, const unit::Def* > m_unit_defs = {};
std::map< size_t, unit::Unit* > m_units = {};

Expand Down Expand Up @@ -306,6 +309,7 @@ CLASS( Game, MTModule )

bindings::Bindings* m_bindings = nullptr;

std::vector< const game::event::Event* > m_unprocessed_events = {};
Turn* m_current_turn = nullptr;

};
Expand Down
4 changes: 2 additions & 2 deletions src/game/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void State::Reset() {
if ( m_connection ) {
if ( m_connection->IsConnected() ) {
auto* connection = m_connection;
m_connection->m_on_disconnect = [ connection ]() -> void {
m_connection->m_on_disconnect = [ connection ]() -> bool {
// TODO: do this synchronously?
DELETE( connection );
return true;
};
m_connection->Disconnect();
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/bindings/Binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ BINDING_DEF( exit )

BINDING_DEF( random )

BINDING_DEF( on_start )
BINDING_DEF( on )

BINDING_DEF( units )

Expand Down
Loading

0 comments on commit 4b04eca

Please sign in to comment.