From 5ef75e6b7429e3c5961df1c1cff5579376791b89 Mon Sep 17 00:00:00 2001 From: Jesabel Pugliese Date: Mon, 23 Jun 2025 23:30:03 -0300 Subject: [PATCH 1/3] feat(game): handle default guns dropped in map --- common/map/map.cpp | 6 ++++++ common/map/map.h | 6 ++++++ server/game/game.cpp | 3 ++- server/states/game_state.cpp | 6 +++++- server/states/game_state.h | 3 ++- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/common/map/map.cpp b/common/map/map.cpp index 9ed9f259..30c7ac9e 100644 --- a/common/map/map.cpp +++ b/common/map/map.cpp @@ -33,6 +33,8 @@ const std::vector>& Map::get_spawns_cts() const { r const std::vector>& Map::get_bomb_sites() const { return bomb_sites; } +const std::vector>& Map::get_guns() const { return guns; } + void Map::validate() const { if (spawns_tts.empty()) throw std::runtime_error("Map '" + name + "' has no Terrorist spawns"); @@ -54,3 +56,7 @@ void Map::add_tile(Tile&& tile) { if (added_tile.is_bomb_site) bomb_sites.emplace_back(added_tile); } + +void Map::add_gun(GunType gun_type, const Vector2D& pos) { + guns.push_back(std::make_pair(gun_type, pos)); +} diff --git a/common/map/map.h b/common/map/map.h index f3dd2bcb..06dbf1c2 100644 --- a/common/map/map.h +++ b/common/map/map.h @@ -4,8 +4,11 @@ #include #include #include +#include #include +#include "common/models.h" + #include "tile.h" class Map { @@ -19,6 +22,7 @@ class Map { std::vector> spawns_tts; std::vector> spawns_cts; std::vector> bomb_sites; + std::vector> guns; public: Map(const std::string& name, int max_players, int height, int width); @@ -32,8 +36,10 @@ class Map { const std::vector>& get_spawns_tts() const; const std::vector>& get_spawns_cts() const; const std::vector>& get_bomb_sites() const; + const std::vector>& get_guns() const; void validate() const; void add_tile(Tile&& tile); + void add_gun(GunType gun_type, const Vector2D& pos); }; diff --git a/server/game/game.cpp b/server/game/game.cpp index c504e7ad..af0a6eb8 100644 --- a/server/game/game.cpp +++ b/server/game/game.cpp @@ -13,7 +13,8 @@ #include "game_config.h" Game::Game(const std::string& name, std::shared_ptr&& game_clock, Map&& map): - Logic(GameState(std::move(game_clock), map.get_max_players())), + Logic( + GameState(std::move(game_clock), map.get_max_players(), map.get_guns())), name(name), physics_system(std::move(map), state.get_players(), state.get_dropped_guns(), state.get_bomb()) {} diff --git a/server/states/game_state.cpp b/server/states/game_state.cpp index ce3359c3..80439124 100644 --- a/server/states/game_state.cpp +++ b/server/states/game_state.cpp @@ -3,11 +3,15 @@ #include #include "server/game/game_config.h" +#include "server/items/gun.h" #include "server/physics/circular_hitbox.h" #include "server/physics/rect_hitbox.h" -GameState::GameState(std::shared_ptr&& game_clock, int max_players): +GameState::GameState(std::shared_ptr&& game_clock, int max_players, + const std::vector>& guns): phase(std::move(game_clock)), max_players(max_players) { + for (const auto& [gun_type, pos]: guns) + add_dropped_gun(std::move(Gun::make_gun(gun_type)), pos); updates = get_full_update(); } diff --git a/server/states/game_state.h b/server/states/game_state.h index 26a50324..d571506f 100644 --- a/server/states/game_state.h +++ b/server/states/game_state.h @@ -27,7 +27,8 @@ class GameState: public State { bool is_tts_win_condition() const; public: - GameState(std::shared_ptr&& game_clock, int max_players); + GameState(std::shared_ptr&& game_clock, int max_players, + const std::vector>& guns); bool player_is_in_game(const std::string& player_name) const; bool all_players_ready() const; From 71cc375313f4e7c2cbd74721bd48b6f4e0244194 Mon Sep 17 00:00:00 2001 From: Jesabel Pugliese Date: Mon, 23 Jun 2025 23:35:47 -0300 Subject: [PATCH 2/3] feat(map): implement loading of guns from map YAML --- server/map/map_builder.cpp | 18 ++++++++++++++++++ server/map/map_builder.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/server/map/map_builder.cpp b/server/map/map_builder.cpp index 159d8693..42738ad0 100644 --- a/server/map/map_builder.cpp +++ b/server/map/map_builder.cpp @@ -1,6 +1,7 @@ #include "map_builder.h" #include "common/map/tile.h" +#include "common/models.h" #include "common/physics/physics_config.h" #include "common/utils/vector_2d.h" @@ -18,6 +19,9 @@ Map MapBuilder::build() { YAML::Node tiles = map_data["tiles"]; load_tiles(tiles, map); + YAML::Node guns = map_data["guns"]; + load_guns(guns, map); + map.validate(); return map; } @@ -39,3 +43,17 @@ void MapBuilder::load_tiles(const YAML::Node& tiles, Map& map) { map.add_tile(Tile{pos, id, is_collidable, is_spawn_tt, is_spawn_ct, is_bomb_site}); } } + +void MapBuilder::load_guns(const YAML::Node& guns, Map& map) { + if (!guns || !guns.IsSequence()) + return; + + for (const auto& gun_data: guns) { + int x = gun_data["x"].as(); + int y = gun_data["y"].as(); + GunType gun_type = static_cast(gun_data["type"].as()); + + Vector2D pos = Vector2D(x, y) * PhysicsConfig::meter_size; + map.add_gun(gun_type, pos); + } +} diff --git a/server/map/map_builder.h b/server/map/map_builder.h index 7fffaf41..9d4d1c23 100644 --- a/server/map/map_builder.h +++ b/server/map/map_builder.h @@ -12,6 +12,8 @@ class MapBuilder { void load_tiles(const YAML::Node& tiles, Map& map); + void load_guns(const YAML::Node& guns, Map& map); + public: explicit MapBuilder(const std::string& filename); From e0a378331ee8a42bda0b9cb90e83be421a0760b7 Mon Sep 17 00:00:00 2001 From: Jesabel Pugliese Date: Mon, 23 Jun 2025 23:42:49 -0300 Subject: [PATCH 3/3] test(map): add tests for loading map with default dropped guns --- tests/server/game/test_game.cpp | 16 +++ tests/server/map/map3.yaml | 187 ++++++++++++++++++++++++++++++++ tests/server/map/test_map.cpp | 3 +- 3 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 tests/server/map/map3.yaml diff --git a/tests/server/game/test_game.cpp b/tests/server/game/test_game.cpp index 93fe4f90..c8e023dc 100644 --- a/tests/server/game/test_game.cpp +++ b/tests/server/game/test_game.cpp @@ -709,3 +709,19 @@ TEST_F(TestGame, PlayerCanPickUpDroppedItem) { EXPECT_EQ(updates.get_dropped_guns()[0].hitbox.get_pos(), updates.get_players().at("ct").get_pos()); } + +TEST_F(TestGame, GameStartWithDroppedGunsWhenMapHasDefaultDroppedGuns) { + Map map_with_dropped_guns(MapBuilder("./tests/server/map/map3.yaml").build()); + Game game_with_dropped_guns("test_game_with_dropped_guns", clock, + std::move(map_with_dropped_guns)); + game_with_dropped_guns.join_player("tt"); + + GameUpdate updates = game_with_dropped_guns.get_full_update(); + EXPECT_EQ(updates.get_dropped_guns().size(), 2); + EXPECT_EQ(updates.get_dropped_guns()[0].item, GunType::AK47); + EXPECT_EQ(updates.get_dropped_guns()[0].hitbox.get_pos(), + Vector2D(PhysicsConfig::meter_size, PhysicsConfig::meter_size)); + EXPECT_EQ(updates.get_dropped_guns()[1].item, GunType::M3); + EXPECT_EQ(updates.get_dropped_guns()[1].hitbox.get_pos(), + Vector2D(3 * PhysicsConfig::meter_size, 3 * PhysicsConfig::meter_size)); +} diff --git a/tests/server/map/map3.yaml b/tests/server/map/map3.yaml new file mode 100644 index 00000000..c9234809 --- /dev/null +++ b/tests/server/map/map3.yaml @@ -0,0 +1,187 @@ +name: test_map +max_players: 10 +height: 5 +width: 5 +tiles: + - x: 1 + y: 1 + id: 1 + is_collidable: false + is_spawn_tt: true + is_spawn_ct: false + is_bomb_site: false + - x: 3 + y: 3 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: true + is_bomb_site: false + - x: 1 + y: 2 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 2 + y: 1 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 2 + y: 2 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: true + - x: 3 + y: 2 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 1 + y: 3 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 2 + y: 3 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 0 + y: 0 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 1 + y: 0 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 2 + y: 0 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 3 + y: 0 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 4 + y: 0 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 0 + y: 1 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 4 + y: 1 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 0 + y: 2 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 4 + y: 2 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 0 + y: 3 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 4 + y: 3 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 0 + y: 4 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 1 + y: 4 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 2 + y: 4 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 3 + y: 4 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 4 + y: 4 + id: 1 + is_collidable: true + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false + - x: 3 + y: 1 + id: 1 + is_collidable: false + is_spawn_tt: false + is_spawn_ct: false + is_bomb_site: false +guns: + - x: 1 + y: 1 + type: 0 + - x: 3 + y: 3 + type: 1 diff --git a/tests/server/map/test_map.cpp b/tests/server/map/test_map.cpp index c7159d86..928968bc 100644 --- a/tests/server/map/test_map.cpp +++ b/tests/server/map/test_map.cpp @@ -14,7 +14,7 @@ TEST(YamlTest, LoadMap) { } TEST(YamlTest, BuildMap) { - MapBuilder builder("./tests/server/map/map.yaml"); + MapBuilder builder("./tests/server/map/map3.yaml"); Map map = builder.build(); EXPECT_EQ(map.get_name(), "test_map"); @@ -27,4 +27,5 @@ TEST(YamlTest, BuildMap) { EXPECT_EQ(map.get_spawns_tts().size(), 1); EXPECT_EQ(map.get_spawns_cts().size(), 1); EXPECT_EQ(map.get_bomb_sites().size(), 1); + EXPECT_EQ(map.get_guns().size(), 2); }