source: Revenant/geoworld/include/synthsession.h@ 8125274

port/mars-tycoon
Last change on this file since 8125274 was 4962c49, checked in by Jonathan Neufeld <support@…>, 3 years ago

Introduce CLI Terrain Generator utility

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#pragma once
2
3#include <list>
4#include <set>
5#include <stack>
6
7#include <math.h>
8
9#include <linalgadapt.h>
10#include <fluidyn.h>
11#include <coroutines.h>
12#include <mars_grids.h>
13#include <mars_ptr.h>
14#include <mars_calc.h>
15#include <mars_splines.h>
16#include <mars_events.h>
17
18#include "geomorph.h"
19#include "gmregistry.h"
20#include "gwtypes.h"
21#include "geoworld.h"
22#include "gwconfig.h"
23#include "oregen.h"
24
25namespace geoworld
26{
27 class GeoMorph;
28
29 class AgentMapGradientFieldAdapter : public GMGradientField
30 {
31 private:
32 const AgentMap & _amap;
33
34 public:
35 AgentMapGradientFieldAdapter (const AgentMap & amap)
36 : _amap(amap) {}
37
38 inline ResultType operator () (const IndexType x, const IndexType y, const IndexType z) const
39 { return _amap.gradient(x, y, z); }
40 };
41
42 class AgentMapScalarFieldAdapter : public GMScalarField
43 {
44 private:
45 const AgentMap & _amap;
46
47 public:
48 AgentMapScalarFieldAdapter (const AgentMap & amap)
49 : _amap (amap) {}
50
51 inline ResultType operator () (const IndexType x, const IndexType y, const IndexType z) const
52 { return _amap.sample(x, y, z); }
53
54 inline const GMGradientField * queryGradient() const
55 { return new AgentMapGradientFieldAdapter (_amap); }
56
57 inline void releaseGradient(const GMGradientField * pGF) const
58 { delete pGF; }
59 };
60
61 class GeoMorphRegistry;
62
63 class ISynthesisSessionListener
64 {
65 public:
66 virtual void onGenerate (GeoWorld * const pWorld) {}
67 virtual void onGenerateGeoHostLayers (GeoWorld * const pWorld, DepositSynthesizer * const pDepSynth) {}
68 virtual void onCreateMiddleTier(GeoWorld * const pWorld, const std::string & sFileName) {}
69 virtual void onPrepareMorphs (GeoWorld * const pWorld, const MorphList & morphs) {}
70 virtual void onInitMorph (GeoWorld * const pWorld, const GeoMorph * pMorph) {}
71 virtual void onRaster (GeoWorld * const pWorld) {}
72 virtual void onDoMorph (GeoWorld * const pWorld, mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal) {}
73 virtual void onRejects (GeoWorld * const pWorld, const MorphList & rejects) {}
74 virtual void onSpawnMinerals (GeoWorld * const pWorld, DepositSynthesizer * const pDepSynth) {}
75
76 virtual void onGenerate2 (PagedGeoWorld * const pPgWorld) {}
77 virtual void onPass2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass) {}
78 virtual void onPrepareMorphs2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, const MorphList & morphs) {}
79 virtual void onInitMorph2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, const GeoMorph * pMorph) {}
80 virtual void onRaster2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass) {}
81 virtual void onDoMorph2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal) {}
82 virtual void onRejects2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, const MorphList & rejects) {}
83 virtual void onSpawnMinerals2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, DepositSynthesizer * const pDepSynth) {}
84 };
85
86 class SynthesisSession : public mars::Observable< ISynthesisSessionListener >
87 {
88 public:
89 SynthesisSession();
90 ~SynthesisSession();
91
92 bool init(IMineralsDBReader & minreader, IDepositSynthConfigReader & depsynthreader, ISynthesisSessionConfigReader & cfgreader);
93 bool init(IMetaConfigReader & cfgreader, GeoMorphRegistry & registry);
94 inline void setTileDim (const unsigned short nDim) { _config.tiledim = nDim; }
95 void generate ( const std::string & sFileName, const GeoMorphRegistry & registry, const unsigned long nWorldWidth, const unsigned long nWorldHeight, const unsigned short nWorldDepth );
96
97 inline const PagedGeoWorld & getWorld () const { return *_pPgWorld; }
98
99 protected:
100 class MyMacroListener : public IGeoWorldListener
101 {
102 private:
103 SynthesisSession * _pObservable;
104 GeoWorld * const _pWorld;
105
106 public:
107 MyMacroListener (SynthesisSession * pObservable, GeoWorld * const pWorld)
108 : _pObservable (pObservable), _pWorld(pWorld) {}
109
110 void runningMorph (const mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal)
111 { _pObservable->fire(&ISynthesisSessionListener::onDoMorph, _pWorld, pGM, i, nTotal); };
112 };
113
114 class MyMicroListener : public IGeoWorldListener
115 {
116 private:
117 SynthesisSession * _pObservable;
118 PagedGeoWorld * const _pPgWorld;
119 unsigned short _nPass;
120 MorphList _rejects;
121
122 public:
123 MyMicroListener (SynthesisSession * pObservable, PagedGeoWorld * const pPgWorld, const unsigned short nPass);
124
125 void runningMorph (const mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal)
126 { _pObservable->fire(&ISynthesisSessionListener::onDoMorph2, _pPgWorld, _nPass, pGM, i, nTotal); }
127 virtual void runningMorphs (MorphList::const_iterator i0, MorphList::const_iterator iN, const size_t i, const size_t nCount, const size_t nTotal);
128 virtual void rejectedMorph (const mars::ptr< GeoMorph > & pGM)
129 { _rejects.push_back(pGM); }
130
131 ~MyMicroListener();
132 };
133
134 private:
135 class BaseAccessor : public IGeoWorldAccessor
136 {
137 private:
138 const AllAgentMaps & _amaps;
139
140 public:
141 inline BaseAccessor (const AllAgentMaps & amaps)
142 : _amaps(amaps) {}
143
144 inline const GMScalarField * queryField (const FieldType enft) const
145 { return new AgentMapScalarFieldAdapter(_amaps[enft]); }
146 };
147
148 class ReadOnlyPagedGeoWorldAccesser : public BaseAccessor
149 {
150 private:
151 class BBoxComparator : public std::binary_function< mars::BBox< long >, mars::BBox< long >, bool >
152 {
153 private:
154 unsigned long _nMaxScanLine;
155
156 public:
157 BBoxComparator ()
158 : _nMaxScanLine(FileSource::MAX_AXIS_TILES * FileSource::MAX_TILE_DIM) {}
159
160 bool operator() (const mars::BBox< long > & lval, const mars::BBox< long > & rval) const
161 {
162 if (lval.top == rval.top)
163 if (lval.left == rval.left)
164 if (lval.bottom == rval.bottom)
165 if (lval.right == rval.right)
166 return false;
167 else
168 return lval.right < rval.right;
169 else
170 return lval.bottom < rval.bottom;
171 else
172 return lval.left < rval.left;
173 else
174 return lval.top < rval.top;
175 }
176 };
177
178 typedef std::map< mars::BBox< long >, const LockedGWSection *, BBoxComparator > LockedSectionsMap;
179
180 mutable LockedSectionsMap _mapLockedSections;
181 const PagedGeoWorld * _pPgWld;
182
183 const LockedGWSection & acquireSection (const long x, const long y) const;
184
185 static MorphList compilePasses (const PagedGeoWorld * pPgGW);
186
187 public:
188 ReadOnlyPagedGeoWorldAccesser (const PagedGeoWorld * pPagedGeoWorld);
189 ~ReadOnlyPagedGeoWorldAccesser();
190
191 void releaseCache ();
192
193 inline const GeoHeightMap::Precision operator () (const long x, const long y) const
194 { return acquireSection(x, y).heightmap(x, y); }
195 inline const GeoHeightMap::Precision mean (const long x, const long y) const
196 {
197 return
198 static_cast <GeoHeightMap::Precision> (
199 (
200 static_cast <int> (operator () (x - 1, y - 1)) +
201 static_cast <int> (operator () (x + 1, y - 1)) +
202 static_cast <int> (operator () (x - 1, y + 1)) +
203 static_cast <int> (operator () (x + 1, y + 1))
204 )
205 / 4
206 );
207 // TODO: Automatically select precision type for "mean" calculations
208 }
209 inline VectorTag< long >::V3 createCoords (const long x, const long y) const
210 {
211 return
212 VectorTag< long >::V3
213 (
214 x, y,
215 mean(x, y)
216 );
217 }
218 };
219
220 class GeoWorldAccesser : public BaseAccessor
221 {
222 private:
223 GeoWorld * const _pGeoWorld;
224 const GeoHeightMap & _hm;
225
226 public:
227 GeoWorldAccesser (GeoWorld * const pGeoWorld);
228
229 inline const GeoHeightMap::Precision operator () (const long x, const long y) const
230 { return _hm(x, y); }
231 inline const GeoHeightMap::Precision mean (const long x, const long y) const
232 { return _hm.meanVN (x, y); }
233 inline VectorTag< long >::V3 createCoords (const long x, const long y) const
234 { return VectorTag< long >::V3(x, y, _hm.meanVN (x, y)); }
235 };
236
237 FileSource * _pFile;
238 GeoWorld * _pWorld;
239 PagedGeoWorld * _pPgWorld;
240 DepositSynthesizer * _pDepSynth;
241 DepositSynthesizerSettings _cfgdepsynth;
242 MineralsDB _mindb;
243 SynthesisSessionConfig _config;
244
245 void spawnRandomly (MorphList & outlist, const GeoMorphRegistry & registry, const size_t nCount, const unsigned long nWWidth, const unsigned long nWHeight, const unsigned short nLOD);
246 void generate ( PagedGeoWorld * pPgWorld, const GeoMorphRegistry & registry );
247 };
248}
Note: See TracBrowser for help on using the repository browser.