Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MapGenerator.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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"
24 
25 #include <boost/random/linear_congruential.hpp>
26 
27 #include <set>
28 
30 
31 /**
32  * Random map generator interface. Initialized by CMapReader and then checked
33  * periodically during loading, until it's finished (progress value is 0).
34  *
35  * The actual work is performed by CMapGeneratorWorker in a separate thread.
36  */
38 {
40 
41 public:
42  CMapGenerator();
44 
45  /**
46  * Start the map generator thread
47  *
48  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
49  * @param settings JSON string containing settings for the map generator
50  */
51  void GenerateMap(const VfsPath& scriptFile, const std::string& settings);
52 
53  /**
54  * Get status of the map generator thread
55  *
56  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
57  */
58  int GetProgress();
59 
60  /**
61  * Get random map data, according to this format:
62  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
63  *
64  * @return StructuredClone containing map data
65  */
66  shared_ptr<ScriptInterface::StructuredClone> GetResults();
67 
68 private:
70 
71 };
72 
73 /**
74  * Random map generator worker thread.
75  * (This is run in a thread so that the GUI remains responsive while loading)
76  *
77  * Thread-safety:
78  * - Initialize and constructor/destructor must be called from the main thread.
79  * - ScriptInterface created and destroyed by thread
80  * - StructuredClone used to return JS map data - jsvals can't be used across threads/runtimes
81  */
83 {
84 public:
87 
88  /**
89  * Start the map generator thread
90  *
91  * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
92  * @param settings JSON string containing settings for the map generator
93  */
94  void Initialize(const VfsPath& scriptFile, const std::string& settings);
95 
96  /**
97  * Get status of the map generator thread
98  *
99  * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
100  */
101  int GetProgress();
102 
103  /**
104  * Get random map data, according to this format:
105  * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
106  *
107  * @return StructuredClone containing map data
108  */
109  shared_ptr<ScriptInterface::StructuredClone> GetResults();
110 
111 private:
112 // Mapgen
113 
114  /**
115  * Load all scripts of the given library
116  *
117  * @param libraryName String specifying name of the library (subfolder of ../maps/random/)
118  * @return true if all scripts ran successfully, false if there's an error
119  */
120  bool LoadScripts(const std::wstring& libraryName);
121 
122  // callbacks for script functions
123  static bool LoadLibrary(void* cbdata, std::wstring name);
124  static void ExportMap(void* cbdata, CScriptValRooted data);
125  static void SetProgress(void* cbdata, int progress);
126  static void MaybeGC(void* cbdata);
127  static std::vector<std::string> GetCivData(void* cbdata);
128 
129  std::set<std::wstring> m_LoadedLibraries;
130  shared_ptr<ScriptInterface::StructuredClone> m_MapData;
131  boost::rand48 m_MapGenRNG;
135  std::string m_Settings;
136 
137 // Thread
138  static void* RunThread(void* data);
139  bool Run();
140 
143 };
144 
145 
146 #endif //INCLUDED_MAPGENERATOR
int GetProgress()
Get status of the map generator thread.
static void SetProgress(void *cbdata, int progress)
CMapGeneratorWorker * m_Worker
Definition: MapGenerator.h:69
shared_ptr< ScriptInterface::StructuredClone > GetResults()
Get random map data, according to this format: http://trac.wildfiregames.com/wiki/Random_Map_Generato...
static void MaybeGC(void *cbdata)
static std::vector< std::string > GetCivData(void *cbdata)
boost::rand48 m_MapGenRNG
Definition: MapGenerator.h:131
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
Random map generator interface.
Definition: MapGenerator.h:37
static void * RunThread(void *data)
shared_ptr< ScriptInterface::StructuredClone > m_MapData
Definition: MapGenerator.h:130
static void ExportMap(void *cbdata, CScriptValRooted data)
Definition: path.h:75
ScriptInterface * m_ScriptInterface
Definition: MapGenerator.h:133
std::set< std::wstring > m_LoadedLibraries
Definition: MapGenerator.h:129
std::string m_Settings
Definition: MapGenerator.h:135
int GetProgress()
Get status of the map generator thread.
pthread_t m_WorkerThread
Definition: MapGenerator.h:141
static bool LoadLibrary(void *cbdata, std::wstring name)
Random map generator worker thread.
Definition: MapGenerator.h:82
uintptr_t pthread_t
Definition: wpthread.h:63
void GenerateMap(const VfsPath &scriptFile, const std::string &settings)
Start the map generator thread.
shared_ptr< ScriptInterface::StructuredClone > GetResults()
Get random map data, according to this format: http://trac.wildfiregames.com/wiki/Random_Map_Generato...
Abstraction around a SpiderMonkey JSContext.
bool LoadScripts(const std::wstring &libraryName)
Load all scripts of the given library.
NONCOPYABLE(CMapGenerator)
void Initialize(const VfsPath &scriptFile, const std::string &settings)
Start the map generator thread.