Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/MapGenerator.h
Show First 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Random map generator worker thread. | * Random map generator worker thread. | ||||
* (This is run in a thread so that the GUI remains responsive while loading) | * (This is run in a thread so that the GUI remains responsive while loading) | ||||
* | * | ||||
* Thread-safety: | * Thread-safety: | ||||
* - Initialize and constructor/destructor must be called from the main thread. | * - Initialize and constructor/destructor must be called from the main thread. | ||||
* - ScriptInterface created and destroyed by thread | * - ScriptInterface created and destroyed by thread | ||||
* - StructuredClone used to return JS map data - JS:Values can't be used across threads/runtimes. | * - StructuredClone used to return JS map data - JS:Values can't be used across threads/runtimes. | ||||
* | |||||
* In test-mode, threading is disabled. | |||||
*/ | */ | ||||
class CMapGeneratorWorker | class CMapGeneratorWorker | ||||
{ | { | ||||
public: | public: | ||||
CMapGeneratorWorker(); | CMapGeneratorWorker(const bool testing, ScriptInterface* scriptInterface); | ||||
~CMapGeneratorWorker(); | ~CMapGeneratorWorker(); | ||||
/** | /** | ||||
* Start the map generator thread | * Start the map generator thread | ||||
* | * | ||||
* @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js" | * @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js" | ||||
* @param settings JSON string containing settings for the map generator | * @param settings JSON string containing settings for the map generator | ||||
*/ | */ | ||||
Show All 9 Lines | public: | ||||
/** | /** | ||||
* Get random map data, according to this format: | * Get random map data, according to this format: | ||||
* http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat | * http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat | ||||
* | * | ||||
* @return StructuredClone containing map data | * @return StructuredClone containing map data | ||||
*/ | */ | ||||
shared_ptr<ScriptInterface::StructuredClone> GetResults(); | shared_ptr<ScriptInterface::StructuredClone> GetResults(); | ||||
/** | |||||
* Perform the map generation or test. | |||||
*/ | |||||
bool Run(); | |||||
private: | private: | ||||
/** | /** | ||||
* Expose functions defined in this class to the script. | * Perform the map generation - not called for tests. | ||||
*/ | |||||
bool RunMapGenerator(); | |||||
/** | |||||
* Expose functions, globals and classes defined in this class to the map and test scripts. | |||||
*/ | |||||
void RegisterScriptFunctions_Common(); | |||||
/** | |||||
* Expose functions to the mapscripts but not the tests. | |||||
*/ | */ | ||||
void RegisterScriptFunctions(); | void RegisterScriptFunctions_MapGenerator(); | ||||
/** | /** | ||||
* Load all scripts of the given library | * Load all scripts of the given library | ||||
* | * | ||||
* @param libraryName VfsPath specifying name of the library (subfolder of ../maps/random/) | * @param libraryName VfsPath specifying name of the library (subfolder of ../maps/random/) | ||||
* @return true if all scripts ran successfully, false if there's an error | * @return true if all scripts ran successfully, false if there's an error | ||||
*/ | */ | ||||
bool LoadScripts(const VfsPath& libraryName); | bool LoadScripts(const VfsPath& libraryName); | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | private: | ||||
static std::vector<std::string> FindActorTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories); | static std::vector<std::string> FindActorTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories); | ||||
/** | /** | ||||
* Perform map generation in an independent thread. | * Perform map generation in an independent thread. | ||||
*/ | */ | ||||
static void* RunThread(void* data); | static void* RunThread(void* data); | ||||
/** | /** | ||||
* Perform the map generation. | |||||
*/ | |||||
bool Run(); | |||||
/** | |||||
* Currently loaded script librarynames. | * Currently loaded script librarynames. | ||||
*/ | */ | ||||
std::set<VfsPath> m_LoadedLibraries; | std::set<VfsPath> m_LoadedLibraries; | ||||
/** | /** | ||||
* Result of the mapscript generation including terrain, entities and environment settings. | * Result of the mapscript generation including terrain, entities and environment settings. | ||||
*/ | */ | ||||
shared_ptr<ScriptInterface::StructuredClone> m_MapData; | shared_ptr<ScriptInterface::StructuredClone> m_MapData; | ||||
Show All 24 Lines | private: | ||||
std::string m_Settings; | std::string m_Settings; | ||||
/** | /** | ||||
* Backend to loading template data. | * Backend to loading template data. | ||||
*/ | */ | ||||
CTemplateLoader m_TemplateLoader; | CTemplateLoader m_TemplateLoader; | ||||
/** | /** | ||||
* Set to true to spawn a new thread for mapgeneration. | |||||
* Set to false to run the testscripts in the current thread. | |||||
*/ | |||||
bool m_Testing; | |||||
/** | |||||
* Holds the mapgeneration thread identifier. | * Holds the mapgeneration thread identifier. | ||||
*/ | */ | ||||
pthread_t m_WorkerThread; | pthread_t m_WorkerThread; | ||||
/** | /** | ||||
* Avoids thread synchronization issues. | * Avoids thread synchronization issues. | ||||
*/ | */ | ||||
std::mutex m_WorkerMutex; | std::mutex m_WorkerMutex; | ||||
}; | }; | ||||
#endif //INCLUDED_MAPGENERATOR | #endif //INCLUDED_MAPGENERATOR |
Wildfire Games · Phabricator