Pioneer
CityOnPlanet.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _CITYONPLANET_H
5 #define _CITYONPLANET_H
6 
7 #include "CollMesh.h"
8 #include "FrameId.h"
9 #include "Random.h"
10 #include "JsonFwd.h"
11 
12 #include <set>
13 
14 class Geom;
15 class Planet;
16 class SpaceStation;
17 class Frame;
18 class SystemPath;
19 class SystemBody;
20 
21 namespace Graphics {
22  class Renderer;
23  class Frustum;
24  class Material;
25 } // namespace Graphics
26 
27 namespace FileSystem {
28  struct FileInfo;
29 } // namespace FileSystem
30 
31 namespace SceneGraph {
32  class Model;
33  class Animation;
34 } // namespace SceneGraph
35 
36 class CityOnPlanet {
37 public:
38  CityOnPlanet() = delete;
39  CityOnPlanet(Planet *planet, SpaceStation *station, const Uint32 seed);
40  virtual ~CityOnPlanet();
41 
42  void Render(Graphics::Renderer *r, const Graphics::Frustum &camera, const SpaceStation *station, const vector3d &viewCoords, const matrix4x4d &viewTransform);
43  inline Planet *GetPlanet() const { return m_planet; }
44  float GetClipRadius() const { return m_clipRadius; }
45 
46  static void Init();
47  static void Uninit();
48 
49  static void SetCityModelPatterns(const SystemPath &path);
50 
51  static constexpr double RADIUS = 5000.0;
52  // size of a city grid cell
53  static constexpr uint32_t CELLSIZE = 50;
54  // maximum number of cells a single building may take up
55  static constexpr uint32_t CELLMAX = 32;
56  static constexpr uint32_t CELLMASK = CELLMAX - 1;
57 
58 private:
59 
60  // Defines which "city sector" a building should optimally be placed in
61  enum class SectorKind : uint32_t {
62  None, // no specific sector for a building
63  Storage, // warehouses and personal storage buildings
64  Industry, // industrial buildings with high pollution or power draw
65  Monument, // civic / recreational buildings
66  Habitat, // living spaces and office buildings
67  Frontier, // general buildings scattered at the edge of the city
68  };
69 
70  // Building type definition
71  struct BuildingType {
72  uint8_t cellSize[2] = {1, 1};
73 
74  float rarityAirless = 0;
75  float rarityAtmo = 0;
76 
77  SectorKind buildingKind = SectorKind::None;
78 
79  SceneGraph::Model *model;
80  SceneGraph::Animation *idleAnimation;
81  };
82 
83  struct CityRadiusDef {
84  double population = 0.0; // billions of people
85  float baseSize = 0.0; // base size of the city (meters)
86  float atmoSize = 0.0; // additional size from atmosphere (meters)
87  float randomSize = 0.0; // random additional size (meters)
88  float density = 0.0; // density of buildings within the city
89  };
90 
91  struct CityFlavourType {
92  std::string flavourName;
93  std::vector<CityRadiusDef> sizeDefs;
94  std::vector<BuildingType> buildingTypes;
95  };
96 
97 private:
98 
99  void Generate(SpaceStation *station);
100  void CalcCityRadius(const SystemBody *body);
101 
102  void SetGridOccupancy(uint32_t x, uint32_t y, const uint8_t size[2]);
103  bool TestGridOccupancy(uint32_t x, uint32_t y, const uint8_t size[2]);
104 
105  // Quickly check if the given single grid cell is set.
106  // It is expected as a precondition that the position is valid and within
107  // the extents of the grid.
108  inline bool TestGridQuick(uint32_t x, uint32_t y) const
109  {
110  // bitset is stored in lsb order with 8 cells per byte
111  return m_gridBitset[y * m_gridPitch + x / 8] & (1 << (x & 7));
112  }
113 
114  void AddStaticGeomsToCollisionSpace();
115  void RemoveStaticGeomsFromCollisionSpace();
116 
117  struct BuildingInstance {
118  Uint32 instIndex;
119  float clipRadius;
120  int rotation; // 0-3
121  vector3d pos;
122  Geom *geom;
123  };
124 
125  const SystemBody *m_body;
126  Planet *m_planet;
127 
128  double m_cityRadius;
129  double m_cityDensity;
130  uint32_t m_citySize;
131 
132  FrameId m_frame;
133  Random m_rand;
134 
135  std::vector<BuildingInstance> m_buildings;
136  std::vector<BuildingInstance> m_enabledBuildings;
137  std::vector<Uint32> m_buildingCounts;
138 
139  // bitmask occupancy grid for quick population of the city
140  std::unique_ptr<uint8_t[]> m_gridBitset;
141  // width of a single grid row in bytes
142  uint32_t m_gridPitch;
143  uint32_t m_gridLen;
144 
145  int m_detailLevel;
146  float m_clipRadius;
147  vector3d m_realCentre;
148  vector3d m_gridOrigin;
149 
150  CityFlavourType *m_cityType;
151 
152  // --------------------------------------------------------
153  // statics
154 private:
155 
156  static std::vector<CityFlavourType> s_cityFlavours;
157 
158  static std::unique_ptr<Graphics::Material> s_debugMat;
159 
160  static void LoadCityFlavour(const FileSystem::FileInfo &file);
161  static void LoadBuildingType(std::string_view key, const Json &buildingDef, BuildingType &out);
162  static void GetModelSize(const Aabb &aabb, uint8_t size[2]);
163 };
164 
165 #endif /* _CITYONPLANET_H */
nlohmann::json Json
Definition: Json.h:8
Definition: CityOnPlanet.h:36
static void Init()
Definition: CityOnPlanet.cpp:229
Planet * GetPlanet() const
Definition: CityOnPlanet.h:43
static constexpr double RADIUS
Definition: CityOnPlanet.h:51
static constexpr uint32_t CELLMAX
Definition: CityOnPlanet.h:55
static void Uninit()
Definition: CityOnPlanet.cpp:262
static constexpr uint32_t CELLSIZE
Definition: CityOnPlanet.h:53
float GetClipRadius() const
Definition: CityOnPlanet.h:44
static constexpr uint32_t CELLMASK
Definition: CityOnPlanet.h:56
virtual ~CityOnPlanet()
Definition: CityOnPlanet.cpp:295
static void SetCityModelPatterns(const SystemPath &path)
Definition: CityOnPlanet.cpp:272
void Render(Graphics::Renderer *r, const Graphics::Frustum &camera, const SpaceStation *station, const vector3d &viewCoords, const matrix4x4d &viewTransform)
Definition: CityOnPlanet.cpp:641
CityOnPlanet()=delete
Definition: FileSystem.h:77
Definition: Frame.h:28
Definition: Geom.h:16
Definition: Frustum.h:17
Definition: Renderer.h:44
Definition: Planet.h:18
Definition: Random.h:27
Definition: Animation.h:19
Definition: Model.h:88
Definition: SpaceStation.h:30
Definition: SystemBody.h:19
Definition: SystemPath.h:13
Definition: CityOnPlanet.h:27
Definition: Background.h:14
Definition: CityOnPlanet.h:31
Definition: Aabb.h:9
Definition: FrameId.h:9