Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ZoneArea.cxx
Go to the documentation of this file.
1 #include "ZoneArea.hxx"
2 #include "../engine/basics/PointFunctions.hxx"
3 #include "../services/Randomizer.hxx"
4 #include "../engine/TileManager.hxx"
5 #include <MapFunctions.hxx>
6 
7 void mergeZoneAreas(ZoneArea &mainZone, ZoneArea &toBeMerged)
8 {
9  mainZone.m_gridNodes.insert(mainZone.end(), toBeMerged.begin(), toBeMerged.end());
10  mainZone.m_hasPower |= toBeMerged.m_hasPower;
11  mainZone.m_hasWater |= toBeMerged.m_hasWater;
12  mainZone.xmin = std::min(mainZone.xmin, toBeMerged.xmin);
13  mainZone.xmax = std::max(mainZone.xmax, toBeMerged.xmax);
14  mainZone.ymin = std::min(mainZone.ymin, toBeMerged.ymin);
15  mainZone.ymax = std::max(mainZone.ymax, toBeMerged.ymax);
16 };
17 
19  : MapGrid{zoneNode}, m_zoneType(zoneNode.zoneType), m_zoneDensity(zoneNode.zoneDensity),
20  xmin(std::max(0, zoneNode.coordinate.x - 1)), xmax(std::min(Settings::instance().mapSize, zoneNode.coordinate.x + 1)),
21  ymin(std::max(0, zoneNode.coordinate.y - 1)), ymax(std::min(Settings::instance().mapSize, zoneNode.coordinate.y + 1))
22 {
23  //update vacancy in case constructor is called with a zonenode
24  m_isVacant = checkVacancy();
25 }
26 
28 {
29  constexpr int amountOfBuildingsToSpawn = 5;
30  auto &randomizer = Randomizer::instance();
31  // shuffle mapNodes so placement of building looks random
32  randomizer.shuffle(begin(), end());
33 
34  int buildingsSpawned = 0;
35 
36  // pick every single zone node we have
37  for (auto &node : m_gridNodes)
38  {
39  if (buildingsSpawned >= amountOfBuildingsToSpawn)
40  {
41  break;
42  }
43  if (node.occupied)
44  {
45  continue;
46  }
47 
48  // get the maximum size we can spawn at this node
49  TileSize maxTileSize = getMaximumTileSize(node.coordinate);
50  std::string buildingTileID =
52 
53  // place the building
54  MapFunctions::instance().setTileID(buildingTileID, node.coordinate);
55  buildingsSpawned++;
56  }
57 }
58 
60 {
61  return m_gridNodes.end() !=
62  std::find_if(m_gridNodes.begin(), m_gridNodes.end(), [](const ZoneNode &node) { return node.occupied == false; });
63 }
64 
66 {
67  TileSize possibleSize = {1, 1};
68 
69  for (int distance = 1; distance <= possibleSize.width || distance <= possibleSize.height; distance++)
70  {
71  std::vector<Point> xDirection = PointFunctions::getArea({originPoint.x - distance, originPoint.y}, originPoint);
72  std::vector<Point> yDirection = PointFunctions::getArea(originPoint, {originPoint.x, originPoint.y + distance});
73  // check if there's a tile in x direction (top of the origin point)
74  bool increaseX = true;
75  bool increaseY = true;
76 
77  for (auto coord : xDirection)
78  {
79  if (!isMemberOf(coord))
80  {
81  increaseX = false;
82  break;
83  }
84  }
85  for (auto coord : yDirection)
86  {
87  if (!isMemberOf(coord))
88  {
89  increaseY = false;
90  break;
91  }
92  }
93 
94  if (increaseX)
95  {
96  possibleSize.width++;
97  }
98  if (increaseY)
99  {
100  possibleSize.height++;
101  }
102  }
103  return possibleSize;
104 }
105 
107 {
108  m_gridNodes.push_back(zoneNode);
109 
110  //update vacancy
112 
113  if (zoneNode.coordinate.x == xmin)
114  {
115  xmin = std::max(0, xmin - 1);
116  }
117  else if (zoneNode.coordinate.x == xmax)
118  {
119  xmax = std::min(Settings::instance().mapSize, xmax + 1);
120  }
121  else if (zoneNode.coordinate.y == ymin)
122  {
123  ymin = std::max(0, ymin - 1);
124  }
125  else if (zoneNode.coordinate.y == ymax)
126  {
127  ymax = std::min(Settings::instance().mapSize, ymax + 1);
128  }
129 }
130 
132 {
133  m_gridNodes.erase(std::remove_if(begin(), end(), [coordinate](const ZoneNode &node) { return node.coordinate == coordinate; }),
134  end());
135  //update vacancy
137 }
138 
139 void ZoneArea::setVacancy(Point coordinate, bool vacancy)
140 {
141  auto node = std::find_if(begin(), end(), [coordinate](const ZoneNode &zNode) { return zNode.coordinate == coordinate; });
142  if (node != end())
143  {
144  if (vacancy)
145  {
146  node->occupied = false;
147  }
148  else
149  {
150  node->occupied = true;
151  }
153  }
154 }
ZoneArea::removeZoneNode
void removeZoneNode(Point coordinate)
Remove a zoneNode on a given coordinate from this zoneArea.
Definition: ZoneArea.cxx:131
TileSize::height
unsigned int height
Definition: tileData.hxx:89
MapGrid::begin
auto begin()
Definition: MapGrid.hxx:46
mergeZoneAreas
void mergeZoneAreas(ZoneArea &mainZone, ZoneArea &toBeMerged)
Definition: ZoneArea.cxx:7
ZoneArea
Definition: ZoneArea.hxx:19
ZoneArea::checkVacancy
bool checkVacancy() const
internal function to check for vacancy
Definition: ZoneArea.cxx:59
MapFunctions.hxx
TileSize
How many tiles are occupied by a building.
Definition: tileData.hxx:86
PointFunctions::getArea
static std::vector< Point > getArea(const Point &isoCoordinatesStart, const Point &isoCoordinatesEnd)
Gets all nodes in a rectangular area between start and end point.
Definition: PointFunctions.cxx:156
MapGrid::end
auto end()
Definition: MapGrid.hxx:47
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
ZoneArea::ymin
int ymin
Definition: ZoneArea.hxx:118
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
ZoneArea::ymax
int ymax
Definition: ZoneArea.hxx:118
ZoneArea::xmax
int xmax
Definition: ZoneArea.hxx:118
ZoneArea::xmin
int xmin
Definition: ZoneArea.hxx:118
MapGrid< ZoneNode >::isMemberOf
bool isMemberOf(Point coordinate) const
If this coordinate is part of the grid.
Definition: MapGrid.inl.hxx:22
TileManager::getRandomTileIDForZoneWithRandomSize
std::optional< std::string > getRandomTileIDForZoneWithRandomSize(ZoneType zone, ZoneDensity zoneDensity, TileSize maxTileSize={1, 1})
Pick a single random tileID for a zone with a random tilesize within the supplied max Size.
Definition: TileManager.cxx:72
ZoneArea::spawnBuildings
void spawnBuildings()
Spawn buildings on nodes in this area if all demands are fulfilled.
Definition: ZoneArea.cxx:27
ZoneArea.hxx
ZoneNode::coordinate
Point coordinate
Definition: ZoneArea.hxx:10
ZoneArea::m_zoneType
ZoneType m_zoneType
Definition: ZoneArea.hxx:112
ZoneNode
Definition: ZoneArea.hxx:8
MapGrid::m_gridNodes
std::vector< T > m_gridNodes
Definition: MapGrid.hxx:50
ZoneArea::setVacancy
void setVacancy(Point coordinate, bool vacancy)
Set vacancy for this tile.
Definition: ZoneArea.cxx:139
ZoneArea::m_zoneDensity
ZoneDensity m_zoneDensity
Definition: ZoneArea.hxx:113
Point
Definition: Point.hxx:7
ZoneArea::m_isVacant
bool m_isVacant
Definition: ZoneArea.hxx:117
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
ZoneArea::m_hasPower
bool m_hasPower
Definition: ZoneArea.hxx:115
MapFunctions::setTileID
bool setTileID(const std::string &tileID, Point coordinate)
Set the Tile ID Of Node object.
Definition: MapFunctions.cxx:378
ZoneArea::m_hasWater
bool m_hasWater
Definition: ZoneArea.hxx:116
ZoneArea::ZoneArea
ZoneArea(ZoneNode zoneNode)
Definition: ZoneArea.cxx:18
string
std::string string
Definition: AudioConfig.hxx:14
ZoneArea::addNode
void addNode(ZoneNode zoneNode) override
Add a zoneNode to this zoneArea.
Definition: ZoneArea.cxx:106
MapGrid
Definition: MapGrid.hxx:7
TileSize::width
unsigned int width
Definition: tileData.hxx:88
ZoneArea::getMaximumTileSize
TileSize getMaximumTileSize(Point originPoint)
Returns the possible size of buildings that can be placed on this coordinate in a zone.
Definition: ZoneArea.cxx:65