source: Revenant/gdmodule/src/core.cpp@ c61312b

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

(WIP) Introduction of GD Native module and Godot-CPP API sub-module

  • Property mode set to 100644
File size: 7.7 KB
Line 
1#include "core.h"
2
3#include <geoworld.h>
4#include <iostream>
5#include <fstream>
6#include <gmregistry.h>
7#include <gwmorphs/gmdefault.h>
8#include <yamlgmcfgrd.h>
9#include <yamlminreader.h>
10#include <yamlsessreader.h>
11#include <yamldepositsynthreader.h>
12#include <yamlmetaconfig.h>
13#include <synthsession.h>
14#include <PNGDEM.h>
15#include <mars_log.h>
16
17using namespace geoworld;
18using namespace genesis;
19using namespace mars;
20using namespace std;
21
22namespace godot {
23 TerrainGeneratorCore::TerrainGeneratorCore(const boost::filesystem::path path, geoworld::GeoMorphRegistry * geoMorphRegistry) : sess(new SynthesisSession()), geoMorphRegistry(geoMorphRegistry) {
24 LOGN << "Attempting to load from config " << path;
25
26 MetaConfig cfg;
27 std::ifstream incfg;
28
29 incfg.exceptions(std::ifstream::failbit | std::ifstream::badbit);
30 incfg.open(path.c_str(), std::ios::in);
31
32 YAMLMetaConfigReader cfgreader(incfg);
33
34 cfgreader.load(cfg);
35
36 incfg.close();
37
38 LOGN << "Now loading Genesis = " << cfg.resources.genesis << ", Factories = " << cfg.resources.gmfactories << ", and Deposit Synthesis = " << cfg.resources.depsynth;
39 std::ifstream ingenesis, ingmfactories, indepsynth;
40 ingenesis.exceptions(std::ifstream::failbit | std::ifstream::badbit);
41 ingmfactories.exceptions(std::ifstream::failbit | std::ifstream::badbit);
42 indepsynth.exceptions(std::ifstream::failbit | std::ifstream::badbit);
43
44 const boost::filesystem::path
45 container = path.parent_path(),
46 genesisConfig = container / cfg.resources.genesis,
47 factoriesConfig = container / cfg.resources.gmfactories,
48 depSynthConfig = container / cfg.resources.depsynth;
49
50 ingenesis.open(genesisConfig.c_str(), std::ios::in);
51 ingmfactories.open(factoriesConfig.c_str(), std::ios::in);
52 indepsynth.open(depSynthConfig.c_str(), std::ios::in);
53
54 auto gmfactoriesreader = YAMLGeoMorphConfigReader(ingmfactories);
55
56 LOGN << "Configuring GM Factories";
57 geoMorphRegistry->configure(gmfactoriesreader);
58
59 LOGN << "Loading Minerals = " << cfg.resources.minerals << ", Classes = " << cfg.resources.classes;
60 const boost::filesystem::path
61 mineralsConfig = container / cfg.resources.minerals,
62 classesConfig = container / cfg.resources.classes;
63
64 auto mineralsReader = MineralsYAMLReader(
65 mineralsConfig.c_str(),
66 classesConfig.c_str(),
67 NULL
68 );
69 auto synthReader = YAMLDepositSynthCfgReader(indepsynth);
70 auto synthSessReader = YAMLSynthSessCfgReader(ingenesis);
71
72 LOGN << "Initializing synthesis session";
73 this->sess->addListener(this);
74 bool bResult = this->sess->init(
75 mineralsReader,
76 synthReader,
77 synthSessReader
78 );
79 if (!bResult)
80 LOG(mars::Log::Fatal) << "Failed to generate the world";
81
82 ingenesis.close();
83 ingmfactories.close();
84 indepsynth.close();
85 }
86
87 void TerrainGeneratorCore::generate(const boost::filesystem::path outputFile, const unsigned short nTileDim, const unsigned long nWorldWidth, const unsigned long nWorldHeight, const unsigned short nWorldDepth) {
88 LOGN << "Generating to " << outputFile << ", with dimensions " << nWorldWidth << 'x' << nWorldHeight << 'x' << nWorldDepth << '@' << nTileDim;
89 this->sess->setTileDim(nTileDim);
90 this->sess->generate(outputFile.c_str(), *geoMorphRegistry, nWorldWidth, nWorldHeight, nWorldDepth);
91 }
92
93 TerrainGeneratorCore::~TerrainGeneratorCore() {
94 delete sess;
95 delete geoMorphRegistry;
96 }
97
98 void TerrainGeneratorCore::onRaster( GeoWorld * const pWorld )
99 {
100 LOGN << "Rastering...";
101 }
102
103 void TerrainGeneratorCore::onRejects( GeoWorld * const pWorld, const MorphList & rejects )
104 {
105 LOG(mars::Log::Warn) << "Rejects: ";
106
107 for (MorphList::const_iterator i = rejects.begin(); i != rejects.end(); ++i)
108 LOG(mars::Log::Warn) << "\t- " << (*i)->getTypeName();
109 }
110
111 void TerrainGeneratorCore::onInitMorph( GeoWorld * const pWorld, const GeoMorph * pMorph )
112 {
113 LOGN << "Initialize Morph: " << pMorph->getTypeName();
114
115 const BoundedGeoMorph *pBGM = dynamic_cast< const BoundedGeoMorph * >(pMorph);
116 if (NULL != pBGM)
117 {
118 LOGN << ", bbox=" << pBGM->getBBox();
119 }
120 }
121
122 void TerrainGeneratorCore::onGenerate( GeoWorld * const pWorld )
123 {
124 LOGN << "Generating world at LOD " << pWorld->getLOD();
125 }
126
127 void TerrainGeneratorCore::onDoMorph (GeoWorld * const pWorld, mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal)
128 {
129 LOGN << "Morph " << typeid(*pGM).name() << ", " << (i + 1) << " of " << nTotal;
130 }
131
132 void TerrainGeneratorCore::onCreateMiddleTier (GeoWorld * const pWorld, const std::string & sFileName)
133 {
134 LOGN << "Generating Pageable-World File \"" << sFileName << "\"";
135 }
136
137 void TerrainGeneratorCore::onGenerateMiddleTier (GeoWorld * const pWorld, const PagedGeoWorld * const pPgWorld)
138 {
139 LOGN << "Dumping a sample";
140
141 const LockedGWSection * pSection = pPgWorld->lockro(-500, -500, 2000, 1000);
142
143 *pSection >> GLOBALDEM;
144 DUMP_GLOBALDEM();
145
146 pPgWorld->unlock(pSection);
147 }
148
149 void TerrainGeneratorCore::onPrepareMorphs( GeoWorld * const pWorld, const MorphList & morphs )
150 {
151 LOGN << "Chosen morphs: ";
152
153 for (MorphList::const_iterator i = morphs.begin(); i != morphs.end(); ++i)
154 LOGN << "\t- " << (*i)->getTypeName();
155 }
156
157 void TerrainGeneratorCore::onPrepareMorphs2( PagedGeoWorld * const pPgWorld, const unsigned short nPass, const MorphList & morphs )
158 {
159 LOGN << "P" << nPass << ": " << "Chosen morphs: ";
160
161 for (MorphList::const_iterator i = morphs.begin(); i != morphs.end(); ++i)
162 LOGN << "\t- " << (*i)->getTypeName();
163 }
164
165 void TerrainGeneratorCore::onInitMorph2( PagedGeoWorld * const pPgWorld, const unsigned short nPass, const GeoMorph * pMorph )
166 {
167 LOGN << "P" << nPass << ": " << "Initialize Morph: " << pMorph->getTypeName();
168
169 const BoundedGeoMorph *pBGM = dynamic_cast< const BoundedGeoMorph * >(pMorph);
170 if (NULL != pBGM)
171 {
172 LOGN << ", bbox=" << pBGM->getBBox();
173 }
174 }
175
176 void TerrainGeneratorCore::onDoMorph2( PagedGeoWorld * const pPgWorld, const unsigned short nPass, mars::ptr< GeoMorph > & pGM, const size_t i, const size_t nTotal )
177 {
178 LOGN << "P" << nPass << ": " << "Morph " << typeid(*pGM).name() << ", " << (i + 1) << " of " << nTotal;
179 }
180
181 void TerrainGeneratorCore::onRaster2( PagedGeoWorld * const pPgWorld, const unsigned short nPass )
182 {
183 LOGN << "P" << nPass << ": " << "Rastering...";
184 }
185
186 void TerrainGeneratorCore::onPass2( PagedGeoWorld * const pPgWorld, const unsigned short nPass )
187 {
188 LOGN << "Begin pass #" << nPass;
189 }
190
191 void TerrainGeneratorCore::onGenerate2( PagedGeoWorld * const pPgWorld )
192 {
193 LOGN << "Generating world at LOD " << pPgWorld->getLOD();
194 }
195
196 void TerrainGeneratorCore::onRejects2( PagedGeoWorld * const pPgWorld, const unsigned short nPass, const MorphList & rejects )
197 {
198 LOG(mars::Log::Warn) << "P" << nPass << ": " << "Rejects: ";
199
200 for (MorphList::const_iterator i = rejects.begin(); i != rejects.end(); ++i)
201 LOG(mars::Log::Warn) << "\t- " << (*i)->getTypeName();
202 }
203
204 void TerrainGeneratorCore::onGenerateGeoHostLayers (GeoWorld * const pWorld, DepositSynthesizer * const pDepSynth)
205 {
206 LOGN << "Generating Geology Host layers...";
207 }
208
209 void TerrainGeneratorCore::onSpawnMinerals (GeoWorld * const pWorld, DepositSynthesizer * const pDepSynth)
210 {
211 LOGN << "Generating some minerals...";
212 }
213 void TerrainGeneratorCore::onSpawnMinerals2 (PagedGeoWorld * const pPgWorld, const unsigned short nPass, DepositSynthesizer * const pDepSynth)
214 {
215 LOGN << "Generating some minerals...";
216 }
217}
Note: See TracBrowser for help on using the repository browser.