Pyrogenesis  trunk
MapGenerator.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_MAPGENERATOR
19 #define INCLUDED_MAPGENERATOR
20 
21 #include "ps/FileIo.h"
22 #include "ps/Future.h"
23 #include "ps/TemplateLoader.h"
25 
26 #include <boost/random/linear_congruential.hpp>
27 #include <mutex>
28 #include <set>
29 #include <string>
30 
32 
33 /**
34  * Random map generator interface. Initialized by CMapReader and then checked
35  * periodically during loading, until it's finished (progress value is 0).
36  *
37  * The actual work is performed by CMapGeneratorWorker in a separate thread.
38  */
40 {
42 
43 public:
44  CMapGenerator();
46 
47  /**
48  * Start the map generator thread
49  *
50  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
51  * @param settings JSON string containing settings for the map generator
52  */
53  void GenerateMap(const VfsPath& scriptFile, const std::string& settings);
54 
55  /**
56  * Get status of the map generator thread
57  *
58  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
59  */
60  int GetProgress();
61 
62  /**
63  * Get random map data, according to this format:
64  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
65  *
66  * @return StructuredClone containing map data
67  */
69 
70 private:
72 
73 };
74 
75 /**
76  * Random map generator worker thread.
77  * (This is run in a thread so that the GUI remains responsive while loading)
78  *
79  * Thread-safety:
80  * - Initialize and constructor/destructor must be called from the main thread.
81  * - ScriptInterface created and destroyed by thread
82  * - StructuredClone used to return JS map data - JS:Values can't be used across threads/contexts.
83  */
85 {
86 public:
87  CMapGeneratorWorker(ScriptInterface* scriptInterface);
89 
90  /**
91  * Start the map generator thread
92  *
93  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
94  * @param settings JSON string containing settings for the map generator
95  */
96  void Initialize(const VfsPath& scriptFile, const std::string& settings);
97 
98  /**
99  * Get status of the map generator thread
100  *
101  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
102  */
103  int GetProgress();
104 
105  /**
106  * Get random map data, according to this format:
107  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
108  *
109  * @return StructuredClone containing map data
110  */
112 
113  /**
114  * Set initial seed, callback data.
115  * Expose functions, globals and classes defined in this class relevant to the map and test scripts.
116  */
117  void InitScriptInterface(const u32 seed);
118 
119 private:
120 
121  /**
122  * Expose functions defined in this class that are relevant to mapscripts but not the tests.
123  */
124  void RegisterScriptFunctions_MapGenerator();
125 
126  /**
127  * Load all scripts of the given library
128  *
129  * @param libraryName VfsPath specifying name of the library (subfolder of ../maps/random/)
130  * @return true if all scripts ran successfully, false if there's an error
131  */
132  bool LoadScripts(const VfsPath& libraryName);
133 
134  /**
135  * Finalize map generation and pass results from the script to the engine.
136  */
137  void ExportMap(JS::HandleValue data);
138 
139  /**
140  * Load an image file and return it as a height array.
141  */
142  JS::Value LoadHeightmap(const VfsPath& src);
143 
144  /**
145  * Load an Atlas terrain file (PMP) returning textures and heightmap.
146  */
147  JS::Value LoadMapTerrain(const VfsPath& filename);
148 
149  /**
150  * Sets the map generation progress, which is one of multiple stages determining the loading screen progress.
151  */
152  void SetProgress(int progress);
153 
154  /**
155  * Microseconds since the epoch.
156  */
157  double GetMicroseconds();
158 
159  /**
160  * Return the template data of the given template name.
161  */
162  CParamNode GetTemplate(const std::string& templateName);
163 
164  /**
165  * Check whether the given template exists.
166  */
167  bool TemplateExists(const std::string& templateName);
168 
169  /**
170  * Returns all template names of simulation entity templates.
171  */
172  std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories);
173 
174  /**
175  * Returns all template names of actors.
176  */
177  std::vector<std::string> FindActorTemplates(const std::string& path, bool includeSubdirectories);
178 
179  /**
180  * Perform the map generation.
181  */
182  bool Run();
183 
184  /**
185  * Currently loaded script librarynames.
186  */
187  std::set<VfsPath> m_LoadedLibraries;
188 
189  /**
190  * Result of the mapscript generation including terrain, entities and environment settings.
191  */
193 
194  /**
195  * Deterministic random number generator.
196  */
197  boost::rand48 m_MapGenRNG;
198 
199  /**
200  * Current map generation progress.
201  */
203 
204  /**
205  * Provides the script context.
206  */
208 
209  /**
210  * Map generation script to run.
211  */
213 
214  /**
215  * Map and simulation settings chosen in the gamesetup stage.
216  */
217  std::string m_Settings;
218 
219  /**
220  * Backend to loading template data.
221  */
223 
224  /**
225  * Holds the completion result of the asynchronous map generation.
226  * TODO: this whole class could really be a future on its own.
227  */
229 
230  /**
231  * Avoids thread synchronization issues.
232  */
233  std::mutex m_WorkerMutex;
234 };
235 
236 
237 #endif //INCLUDED_MAPGENERATOR
std::set< VfsPath > m_LoadedLibraries
Currently loaded script librarynames.
Definition: MapGenerator.h:187
An entity initialisation parameter node.
Definition: ParamNode.h:150
CTemplateLoader m_TemplateLoader
Backend to loading template data.
Definition: MapGenerator.h:222
int GetProgress()
Get status of the map generator thread.
Definition: MapGenerator.cpp:416
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:47
int m_Progress
Current map generation progress.
Definition: MapGenerator.h:202
CMapGeneratorWorker * m_Worker
Definition: MapGenerator.h:71
std::shared_ptr< JSStructuredCloneData > StructuredClone
Structured clones are a way to serialize &#39;simple&#39; JS::Values into a buffer that can safely be passed ...
Definition: StructuredClone.h:36
Script::StructuredClone m_MapData
Result of the mapscript generation including terrain, entities and environment settings.
Definition: MapGenerator.h:192
boost::rand48 m_MapGenRNG
Deterministic random number generator.
Definition: MapGenerator.h:197
std::mutex m_WorkerMutex
Avoids thread synchronization issues.
Definition: MapGenerator.h:233
Random map generator interface.
Definition: MapGenerator.h:39
Future< void > m_WorkerThread
Holds the completion result of the asynchronous map generation.
Definition: MapGenerator.h:228
CMapGenerator()
Definition: MapGenerator.cpp:402
Script::StructuredClone GetResults()
Get random map data, according to this format: http://trac.wildfiregames.com/wiki/Random_Map_Generato...
Definition: MapGenerator.cpp:421
uint32_t u32
Definition: types.h:39
Config::Value_type Value
Definition: json_spirit_value.h:182
Definition: path.h:79
ScriptInterface * m_ScriptInterface
Provides the script context.
Definition: MapGenerator.h:207
std::string m_Settings
Map and simulation settings chosen in the gamesetup stage.
Definition: MapGenerator.h:217
VfsPath m_ScriptPath
Map generation script to run.
Definition: MapGenerator.h:212
CParamNode GetTemplate(const std::string &templateName)
Definition: GameSetup.cpp:785
Random map generator worker thread.
Definition: MapGenerator.h:84
bool TemplateExists(const std::string &templateName)
Definition: JSInterface_GUIManager.cpp:66
void GenerateMap(const VfsPath &scriptFile, const std::string &settings)
Start the map generator thread.
Definition: MapGenerator.cpp:411
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
~CMapGenerator()
Definition: MapGenerator.cpp:406
double GetMicroseconds()
Microseconds since the epoch.
Definition: JSInterface_Debug.cpp:36
NONCOPYABLE(CMapGenerator)
static Status Run(const Operation &op, const Parameters &p=Parameters(), const CompletedHook &completedHook=CompletedHook(), const IssueHook &issueHook=IssueHook())
Definition: io.h:243