Pyrogenesis  trunk
MapGenerator.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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/ThreadUtil.h"
23 #include "ps/TemplateLoader.h"
25 
26 #include <boost/random/linear_congruential.hpp>
27 
28 #include <set>
29 
31 
32 /**
33  * Random map generator interface. Initialized by CMapReader and then checked
34  * periodically during loading, until it's finished (progress value is 0).
35  *
36  * The actual work is performed by CMapGeneratorWorker in a separate thread.
37  */
39 {
41 
42 public:
43  CMapGenerator();
45 
46  /**
47  * Start the map generator thread
48  *
49  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
50  * @param settings JSON string containing settings for the map generator
51  */
52  void GenerateMap(const VfsPath& scriptFile, const std::string& settings);
53 
54  /**
55  * Get status of the map generator thread
56  *
57  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
58  */
59  int GetProgress();
60 
61  /**
62  * Get random map data, according to this format:
63  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
64  *
65  * @return StructuredClone containing map data
66  */
67  shared_ptr<ScriptInterface::StructuredClone> GetResults();
68 
69 private:
71 
72 };
73 
74 /**
75  * Random map generator worker thread.
76  * (This is run in a thread so that the GUI remains responsive while loading)
77  *
78  * Thread-safety:
79  * - Initialize and constructor/destructor must be called from the main thread.
80  * - ScriptInterface created and destroyed by thread
81  * - StructuredClone used to return JS map data - jsvals can't be used across threads/runtimes
82  */
84 {
85 public:
88 
89  /**
90  * Start the map generator thread
91  *
92  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
93  * @param settings JSON string containing settings for the map generator
94  */
95  void Initialize(const VfsPath& scriptFile, const std::string& settings);
96 
97  /**
98  * Get status of the map generator thread
99  *
100  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
101  */
102  int GetProgress();
103 
104  /**
105  * Get random map data, according to this format:
106  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
107  *
108  * @return StructuredClone containing map data
109  */
110  shared_ptr<ScriptInterface::StructuredClone> GetResults();
111 
112 private:
113 // Mapgen
114 
115  /**
116  * Load all scripts of the given library
117  *
118  * @param libraryName String specifying name of the library (subfolder of ../maps/random/)
119  * @return true if all scripts ran successfully, false if there's an error
120  */
121  bool LoadScripts(const std::wstring& libraryName);
122 
123  // callbacks for script functions
124  static bool LoadLibrary(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name);
125  static void ExportMap(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue data);
126  static void SetProgress(ScriptInterface::CxPrivate* pCxPrivate, int progress);
127  static void MaybeGC(ScriptInterface::CxPrivate* pCxPrivate);
128  static std::vector<std::string> GetCivData(ScriptInterface::CxPrivate* pCxPrivate);
129  static CParamNode GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
130  static bool TemplateExists(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
131  static std::vector<std::string> FindTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories);
132  static std::vector<std::string> FindActorTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories);
133 
134  std::set<std::wstring> m_LoadedLibraries;
135  shared_ptr<ScriptInterface::StructuredClone> m_MapData;
136  boost::rand48 m_MapGenRNG;
140  std::string m_Settings;
142 
143 // Thread
144  static void* RunThread(void* data);
145  bool Run();
146 
149 };
150 
151 
152 #endif //INCLUDED_MAPGENERATOR
bool TemplateExists(ScriptInterface::CxPrivate *pCxPrivate, const std::string &templateName)
Definition: ScriptFunctions.cpp:889
An entity initialisation parameter node.
Definition: ParamNode.h:148
CTemplateLoader m_TemplateLoader
Definition: MapGenerator.h:141
int GetProgress()
Get status of the map generator thread.
Definition: MapGenerator.cpp:308
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:45
int m_Progress
Definition: MapGenerator.h:137
CMapGeneratorWorker * m_Worker
Definition: MapGenerator.h:70
boost::rand48 m_MapGenRNG
Definition: MapGenerator.h:136
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
Random map generator interface.
Definition: MapGenerator.h:38
CMapGenerator()
Definition: MapGenerator.cpp:294
CMutex m_WorkerMutex
Definition: MapGenerator.h:148
shared_ptr< ScriptInterface::StructuredClone > m_MapData
Definition: MapGenerator.h:135
Definition: path.h:77
ScriptInterface * m_ScriptInterface
Definition: MapGenerator.h:138
std::set< std::wstring > m_LoadedLibraries
Definition: MapGenerator.h:134
std::string m_Settings
Definition: MapGenerator.h:140
VfsPath m_ScriptPath
Definition: MapGenerator.h:139
pthread_t m_WorkerThread
Definition: MapGenerator.h:147
CParamNode GetTemplate(ScriptInterface::CxPrivate *pCxPrivate, const std::string &templateName)
Definition: ScriptFunctions.cpp:894
Random map generator worker thread.
Definition: MapGenerator.h:83
uintptr_t pthread_t
Definition: wpthread.h:63
void GenerateMap(const VfsPath &scriptFile, const std::string &settings)
Start the map generator thread.
Definition: MapGenerator.cpp:303
Definition: ScriptInterface.h:99
shared_ptr< ScriptInterface::StructuredClone > GetResults()
Get random map data, according to this format: http://trac.wildfiregames.com/wiki/Random_Map_Generato...
Definition: MapGenerator.cpp:313
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
~CMapGenerator()
Definition: MapGenerator.cpp:298
NONCOPYABLE(CMapGenerator)
static Status Run(const Operation &op, const Parameters &p=Parameters(), const CompletedHook &completedHook=CompletedHook(), const IssueHook &issueHook=IssueHook())
Definition: io.h:233