source: Revenant/geoworld/include/synthsession.h@ 7ef8ec4

port/mars-tycoon
Last change on this file since 7ef8ec4 was 80a6a52, checked in by Jonathan Neufeld <support@…>, 3 years ago

Get to a compile state for terrain procedural generation

  • Property mode set to 100644
File size: 8.2 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 inline void setTileDim (const unsigned short nDim) { _config.tiledim = nDim; }
94 void generate ( const std::string & sFileName, const GeoMorphRegistry & registry, const unsigned long nWorldWidth, const unsigned long nWorldHeight, const unsigned short nWorldDepth );
95
96 inline const PagedGeoWorld & getWorld () const { return *_pPgWorld; }
97
98 protected:
99 class MyMacroListener : public IGeoWorldListener
100 {
101 private:
102 SynthesisSession * _pObservable;
103 GeoWorld * const _pWorld;
104
105 public:
106 MyMacroListener (SynthesisSession * pObservable, GeoWorld * const pWorld)
107 : _pObservable (pObservable), _pWorld(pWorld) {}
108
109 void runningMorph (const mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal)
110 { _pObservable->fire(&ISynthesisSessionListener::onDoMorph, _pWorld, pGM, i, nTotal); };
111 };
112
113 class MyMicroListener : public IGeoWorldListener
114 {
115 private:
116 SynthesisSession * _pObservable;
117 PagedGeoWorld * const _pPgWorld;
118 unsigned short _nPass;
119 MorphList _rejects;
120
121 public:
122 MyMicroListener (SynthesisSession * pObservable, PagedGeoWorld * const pPgWorld, const unsigned short nPass);
123
124 void runningMorph (const mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal)
125 { _pObservable->fire(&ISynthesisSessionListener::onDoMorph2, _pPgWorld, _nPass, pGM, i, nTotal); }
126 virtual void runningMorphs (MorphList::const_iterator i0, MorphList::const_iterator iN, const size_t i, const size_t nCount, const size_t nTotal);
127 virtual void rejectedMorph (const mars::ptr< GeoMorph > & pGM)
128 { _rejects.push_back(pGM); }
129
130 ~MyMicroListener();
131 };
132
133 private:
134 class BaseAccessor : public IGeoWorldAccessor
135 {
136 private:
137 const AllAgentMaps & _amaps;
138
139 public:
140 inline BaseAccessor (const AllAgentMaps & amaps)
141 : _amaps(amaps) {}
142
143 inline const GMScalarField * queryField (const FieldType enft) const
144 { return new AgentMapScalarFieldAdapter(_amaps[enft]); }
145 };
146
147 class ReadOnlyPagedGeoWorldAccesser : public BaseAccessor
148 {
149 private:
150 class BBoxComparator : public std::binary_function< mars::BBox< long >, mars::BBox< long >, bool >
151 {
152 private:
153 unsigned long _nMaxScanLine;
154
155 public:
156 BBoxComparator ()
157 : _nMaxScanLine(FileSource::MAX_AXIS_TILES * FileSource::MAX_TILE_DIM) {}
158
159 bool operator() (const mars::BBox< long > & lval, const mars::BBox< long > & rval) const
160 {
161 if (lval.top == rval.top)
162 if (lval.left == rval.left)
163 if (lval.bottom == rval.bottom)
164 if (lval.right == rval.right)
165 return false;
166 else
167 return lval.right < rval.right;
168 else
169 return lval.bottom < rval.bottom;
170 else
171 return lval.left < rval.left;
172 else
173 return lval.top < rval.top;
174 }
175 };
176
177 typedef std::map< mars::BBox< long >, const LockedGWSection *, BBoxComparator > LockedSectionsMap;
178
179 mutable LockedSectionsMap _mapLockedSections;
180 const PagedGeoWorld * _pPgWld;
181
182 const LockedGWSection & acquireSection (const long x, const long y) const;
183
184 static MorphList compilePasses (const PagedGeoWorld * pPgGW);
185
186 public:
187 ReadOnlyPagedGeoWorldAccesser (const PagedGeoWorld * pPagedGeoWorld);
188 ~ReadOnlyPagedGeoWorldAccesser();
189
190 void releaseCache ();
191
192 inline const GeoHeightMap::Precision operator () (const long x, const long y) const
193 { return acquireSection(x, y).heightmap(x, y); }
194 inline const GeoHeightMap::Precision mean (const long x, const long y) const
195 {
196 return
197 static_cast <GeoHeightMap::Precision> (
198 (
199 static_cast <int> (operator () (x - 1, y - 1)) +
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 )
204 / 4
205 );
206 // TODO: Automatically select precision type for "mean" calculations
207 }
208 inline VectorTag< long >::V3 createCoords (const long x, const long y) const
209 {
210 return
211 VectorTag< long >::V3
212 (
213 x, y,
214 mean(x, y)
215 );
216 }
217 };
218
219 class GeoWorldAccesser : public BaseAccessor
220 {
221 private:
222 GeoWorld * const _pGeoWorld;
223 const GeoHeightMap & _hm;
224
225 public:
226 GeoWorldAccesser (GeoWorld * const pGeoWorld);
227
228 inline const GeoHeightMap::Precision operator () (const long x, const long y) const
229 { return _hm(x, y); }
230 inline const GeoHeightMap::Precision mean (const long x, const long y) const
231 { return _hm.meanVN (x, y); }
232 inline VectorTag< long >::V3 createCoords (const long x, const long y) const
233 { return VectorTag< long >::V3(x, y, _hm.meanVN (x, y)); }
234 };
235
236 FileSource * _pFile;
237 GeoWorld * _pWorld;
238 PagedGeoWorld * _pPgWorld;
239 DepositSynthesizer * _pDepSynth;
240 DepositSynthesizerSettings _cfgdepsynth;
241 MineralsDB _mindb;
242 SynthesisSessionConfig _config;
243
244 void spawnRandomly (MorphList & outlist, const GeoMorphRegistry & registry, const size_t nCount, const unsigned long nWWidth, const unsigned long nWHeight, const unsigned short nLOD);
245 void generate ( PagedGeoWorld * pPgWorld, const GeoMorphRegistry & registry );
246 };
247}
Note: See TracBrowser for help on using the repository browser.