diff --git a/GenMap/src/Cell.cpp b/GenMap/src/Cell.cpp index 6a8eea0cf9066ee39b884298a01d25638ba96031..b1bafb466bef5e4b9012a7735ca7395f4f960a81 100644 --- a/GenMap/src/Cell.cpp +++ b/GenMap/src/Cell.cpp @@ -1,26 +1,5 @@ #include "Cell.h" -std::array<std::pair<int, int>, 6> Cell::NeighborsCoords(unsigned int r, unsigned int c) -{ - std::array<std::pair<int, int>, 6> neighbors_coords; - if (x % 2 == 0) - { - neighbors_coords = { { {x - 1, y - 1}, {x - 1, y}, {x, y - 1}, {x, y + 1}, {x + 1, y - 1}, {x + 1, y} } }; - } - else - { - neighbors_coords = { { {x - 1, y}, {x - 1, y + 1}, {x, y - 1}, {x, y + 1}, {x + 1, y}, {x + 1, y + 1} } }; - } - for (auto& i : neighbors_coords) - { - if (i.first < 0 || i.first >= r || i.second < 0 || i.second >= c) - { - i = {-1, -1}; - } - } - return neighbors_coords; -} - Cell* Cell::FindAdjRegion() const { if (area == nullptr) @@ -28,6 +7,7 @@ Cell* Cell::FindAdjRegion() const auto c = std::find_if(begin(neighbors), end(neighbors), [](Cell* c) {if (c!= nullptr) return (c->GetRegion() != nullptr); }); return (c == end(neighbors) ? nullptr : *c); } + else return nullptr; } diff --git a/GenMap/src/Cell.h b/GenMap/src/Cell.h index 75740ed0dc23fb8ad9aa1bfcd108a2d937900fc5..c1d6e701987733daa199b4364ee20be4cecfaee8 100644 --- a/GenMap/src/Cell.h +++ b/GenMap/src/Cell.h @@ -1,9 +1,7 @@ #pragma once #include "Region.h" -#include <vector> #include <array> #include <algorithm> -#include <set> class Region; @@ -18,14 +16,14 @@ protected: public: Cell(unsigned int X, unsigned int Y, Region* R): x(X), y(Y), area(R), neighbors{}, n_neighbors(0) {}; - Cell(const Cell& c) = default; + Cell(const Cell& c) = delete; Cell(Cell&& c) = default; virtual ~Cell() = default; Cell& operator=(const Cell& c) = delete; Cell& operator=(Cell&& c) = delete; + inline void AddNeighbor(Cell* c) { neighbors[n_neighbors++] = c; }; - std::array<std::pair<int, int>, 6> NeighborsCoords(unsigned int r, unsigned int c); - inline const std::pair<unsigned int, unsigned int> GetCoords() const { return { x,y }; }; + inline const std::pair<unsigned int, unsigned int> GetCoords() const { return { x, y }; }; inline Region* GetRegion() const { return area; }; inline void SetRegion(Region* r) { area = r; }; Cell* FindAdjRegion() const ; diff --git a/GenMap/src/Map.cpp b/GenMap/src/Map.cpp index 9b0523ed49af2813260c1c66d2bc9d39f7b84966..ccc53a1f255209aa1c08750ce3ccef876f460696 100644 --- a/GenMap/src/Map.cpp +++ b/GenMap/src/Map.cpp @@ -8,13 +8,13 @@ Map::Map(unsigned int r, unsigned int c) : //Creation de toutes les cellules de la map for (unsigned int i = 0; i < R; i++) { - map_cells.push_back({}); + map_cells.push_back({}); // ouch for (unsigned int j = 0; j < C; j++) { map_cells[i].push_back(Cell(i, j, nullptr)); } } - //donner � chaque cellule sa liste de voisins + //donner a chaque cellule sa liste de voisins auto in_bound = [r, c](const std::pair<unsigned int, unsigned int>& coords) { @@ -26,15 +26,6 @@ Map::Map(unsigned int r, unsigned int c) : std::pair<unsigned int, unsigned int> coords; std::array<std::array<std::pair<int, int>, 6>, 2> topology({ { { { {-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 0} } }, { { {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, 0}, {1, 1} } } } }); - /* - auto neighbors_coords = [&topology](std::pair<unsigned int, unsigned int> c) - { - std::array<std::pair<int, int>, 6> n{}; - std::fill(begin(n), end(n), c); - return std::accumulate(begin(n), end(n), begin(topology[c.first%2]), [](std::pair<int, int> a, std::pair<int, int> b) -> std::pair<int, int> {return { a.first + b.first,a.second + b.second }; }); - }; - */ - for (auto& r_it : map_cells) { for (auto& c_it : r_it) @@ -45,67 +36,69 @@ Map::Map(unsigned int r, unsigned int c) : p = pair_sum(p, coords); if (in_bound(p)) c_it.AddNeighbor(&map_cells[p.first][p.second]); } - } } +} +Map::~Map() +{ + std::for_each(begin(map_regions), end(map_regions), [](Region* i) { delete i; }); } -void Map::GenerateRegions() { - std::random_device rd; - std::mt19937 g(rd()); +void Map::GenerateRegions() { + std::random_device rdevice; + std::mt19937 generator(rdevice()); std::vector<Cell*> cells_list; - unsigned int n_regions = R * C / 20; + unsigned int n_regions = R * C / 20; // a reformuler + if (n_regions < 2 && R * C > 1) n_regions = 2; - for (unsigned int i = 0; i < R * C; i++) { - cells_list.push_back(&map_cells[i / C][i % C]); + for (unsigned int i = 0; i < R * C; i++) + { + cells_list.push_back(&map_cells[i / C][i % C]); // a reecrire } - std::shuffle(begin(cells_list), end(cells_list), g); + std::shuffle(begin(cells_list), end(cells_list), generator); - for (auto i = begin(cells_list); i != begin(cells_list)+n_regions; i++) { + for (auto i = begin(cells_list); i != begin(cells_list) + n_regions; i++) + { map_regions.push_back(new Region()); map_regions.back()->AddCell(*i); - (*i)->SetRegion(map_regions.back()); } - Cell* p = nullptr; - bool b = true; + bool CellRemainings = true; std::set<Region*> regions; - while (b) + while (CellRemainings) { - b = false; + CellRemainings = false; for (auto c : cells_list) { - auto AdjCell = c->FindAdjRegion(); + Cell* AdjCell = c->FindAdjRegion(); if (AdjCell) { - auto AdjRegion = AdjCell->GetRegion(); + Region* AdjRegion = AdjCell->GetRegion(); if (regions.find(AdjRegion) == end(regions)) { regions.insert(AdjRegion); - b = true; + CellRemainings = true; AdjRegion->AddCell(c); - c->SetRegion(AdjRegion); } - } + regions.clear(); } - regions.clear(); } - - std::for_each(begin(map_regions), end(map_regions), [](Region* r) {r->AddNeighbors(); }); + std::for_each(begin(map_regions), end(map_regions), [](Region* r) {r->AddNeighbors(); }); // créer fonction } -void Map::DeleteRandRegions() { +void Map::DeleteRandRegions() +{ unsigned int dead_regions = 0; for (unsigned int i = 0; i < map_regions.size(); i++) { - if (ContiguityTest(map_regions[i]) && dead_regions < map_regions.size()/10) + if (ContiguityTest(map_regions[i]) && dead_regions < map_regions.size()/10 && map_regions.size() > 2) { delete(map_regions[i]); dead_regions++; @@ -116,9 +109,10 @@ void Map::DeleteRandRegions() { } -bool Map::ContiguityTest(Region* r) { // très optimisable +bool Map::ContiguityTest(Region* r) +{ auto r1 = std::find_if(begin(map_regions), end(map_regions), [r](Region* i) {return (r != i); }); //attention si pas trouvé - + //if (r1 == end(map_regions)) return false; std::set<Region*> reachable_regions, last_step, new_step; last_step.insert(*r1); diff --git a/GenMap/src/Map.h b/GenMap/src/Map.h index 22d2b1c844b3d7ed4fbc17ee817950b3ebbbd31e..fe991ea9fa70a97dd308a444d43a36b0ad9f4aec 100644 --- a/GenMap/src/Map.h +++ b/GenMap/src/Map.h @@ -17,9 +17,10 @@ public: Map(unsigned int r, unsigned int c); Map(const Map& m) = delete; Map(Map&& m) = default; - virtual ~Map() { std::for_each(begin(map_regions), end(map_regions), [](Region* i) {delete i; }); }; + virtual ~Map(); Map& operator=(const Map& m) = delete; Map& operator=(Map&& m) = delete; + void GenerateRegions(); void DeleteRandRegions(); bool ContiguityTest(Region* r); diff --git a/GenMap/src/Region.cpp b/GenMap/src/Region.cpp index 58febe27032e8b9572b266bbfde07979e3d072b7..777f083e54a8ffb6c6803127544a9578bc346be7 100644 --- a/GenMap/src/Region.cpp +++ b/GenMap/src/Region.cpp @@ -1,9 +1,14 @@ #include "Region.h" +void Region::AddCell(Cell* c) +{ + region_cells.push_back(c); c->SetRegion(this); +} + std::vector<std::pair<unsigned int, unsigned int>> Region::GetCells() const { std::vector<std::pair<unsigned int, unsigned int>> cells_pair; - for (auto const &i : region_cells) + for (auto const& i : region_cells) { cells_pair.push_back(i->GetCoords()); } diff --git a/GenMap/src/Region.h b/GenMap/src/Region.h index 94a1c3eca271bb499da3492d92c50e6c29a0f922..0b74435323cd5905db1d8fac49b9692845c4edd2 100644 --- a/GenMap/src/Region.h +++ b/GenMap/src/Region.h @@ -1,7 +1,6 @@ #pragma once #include "Cell.h" #include <vector> -#include <iostream> #include <set> #include <algorithm> @@ -14,13 +13,14 @@ private: std::set<Region*> region_neighbors; public: - Region() {}; + Region() = default; Region(const Region& r) = delete; Region(Region&& r) = default; - virtual ~Region() ; + virtual ~Region(); Region& operator=(const Region& r) = delete; Region& operator=(Region&& r) = delete; - inline void AddCell(Cell* c) { region_cells.push_back(c); }; + + void AddCell(Cell* c); std::vector<std::pair<unsigned int, unsigned int>> GetCells() const; void inline AddNeighbor(Region* r) { region_neighbors.insert(r); } void AddNeighbors();