Cleanup MapGenerator.cpp, use VfsPath, create RegisterScriptFunctions, add header comments, register TERRAIN_TILE_SIZE directly.
Refs 7a17a3152c
, #4034, #4964.
Differential Revision: https://code.wildfiregames.com/D2068
VfsPath change accepted by historic_bruno.
This was SVN commit r22466.
This commit is contained in:
parent
e06285279a
commit
7300dd186e
@ -29,12 +29,6 @@ const g_DamageTypes = new DamageTypes();
|
||||
const MAX_HEIGHT_RANGE = 0xFFFF / HEIGHT_UNITS_PER_METRE; // Engine limit, Roughly 700 meters
|
||||
const MIN_HEIGHT = - SEA_LEVEL;
|
||||
|
||||
/**
|
||||
* Length of one tile of the terrain grid in metres.
|
||||
* Useful to transform footprint sizes of templates to the coordinate system used by getMapSize.
|
||||
*/
|
||||
const TERRAIN_TILE_SIZE = Engine.GetTerrainTileSize();
|
||||
|
||||
const MAX_HEIGHT = MAX_HEIGHT_RANGE - SEA_LEVEL;
|
||||
|
||||
/**
|
||||
|
@ -131,22 +131,7 @@ bool CMapGeneratorWorker::Run()
|
||||
// Replace RNG with a seeded deterministic function
|
||||
m_ScriptInterface->ReplaceNondeterministicRNG(m_MapGenRNG);
|
||||
|
||||
// Functions for RMS
|
||||
JSI_VFS::RegisterScriptFunctions_Maps(*m_ScriptInterface);
|
||||
m_ScriptInterface->RegisterFunction<bool, std::wstring, CMapGeneratorWorker::LoadLibrary>("LoadLibrary");
|
||||
m_ScriptInterface->RegisterFunction<JS::Value, std::wstring, CMapGeneratorWorker::LoadHeightmap>("LoadHeightmapImage");
|
||||
m_ScriptInterface->RegisterFunction<JS::Value, std::string, CMapGeneratorWorker::LoadMapTerrain>("LoadMapTerrain");
|
||||
m_ScriptInterface->RegisterFunction<void, JS::HandleValue, CMapGeneratorWorker::ExportMap>("ExportMap");
|
||||
m_ScriptInterface->RegisterFunction<void, int, CMapGeneratorWorker::SetProgress>("SetProgress");
|
||||
m_ScriptInterface->RegisterFunction<CParamNode, std::string, CMapGeneratorWorker::GetTemplate>("GetTemplate");
|
||||
m_ScriptInterface->RegisterFunction<bool, std::string, CMapGeneratorWorker::TemplateExists>("TemplateExists");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, std::string, bool, CMapGeneratorWorker::FindTemplates>("FindTemplates");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, std::string, bool, CMapGeneratorWorker::FindActorTemplates>("FindActorTemplates");
|
||||
m_ScriptInterface->RegisterFunction<int, CMapGeneratorWorker::GetTerrainTileSize>("GetTerrainTileSize");
|
||||
m_ScriptInterface->SetGlobal("MAP_BORDER_WIDTH", static_cast<int>(MAP_EDGE_TILES));
|
||||
|
||||
// Globalscripts may use VFS script functions
|
||||
m_ScriptInterface->LoadGlobalScripts();
|
||||
RegisterScriptFunctions();
|
||||
|
||||
// Parse settings
|
||||
JS::RootedValue settingsVal(cx);
|
||||
@ -190,6 +175,39 @@ bool CMapGeneratorWorker::Run()
|
||||
return true;
|
||||
}
|
||||
|
||||
void CMapGeneratorWorker::RegisterScriptFunctions()
|
||||
{
|
||||
// VFS
|
||||
JSI_VFS::RegisterScriptFunctions_Maps(*m_ScriptInterface);
|
||||
|
||||
// Globalscripts may use VFS script functions
|
||||
m_ScriptInterface->LoadGlobalScripts();
|
||||
|
||||
// File loading
|
||||
m_ScriptInterface->RegisterFunction<bool, VfsPath, CMapGeneratorWorker::LoadLibrary>("LoadLibrary");
|
||||
m_ScriptInterface->RegisterFunction<JS::Value, VfsPath, CMapGeneratorWorker::LoadHeightmap>("LoadHeightmapImage");
|
||||
m_ScriptInterface->RegisterFunction<JS::Value, VfsPath, CMapGeneratorWorker::LoadMapTerrain>("LoadMapTerrain");
|
||||
|
||||
// Progression
|
||||
m_ScriptInterface->RegisterFunction<void, int, CMapGeneratorWorker::SetProgress>("SetProgress");
|
||||
m_ScriptInterface->RegisterFunction<void, JS::HandleValue, CMapGeneratorWorker::ExportMap>("ExportMap");
|
||||
|
||||
// Template functions
|
||||
m_ScriptInterface->RegisterFunction<CParamNode, std::string, CMapGeneratorWorker::GetTemplate>("GetTemplate");
|
||||
m_ScriptInterface->RegisterFunction<bool, std::string, CMapGeneratorWorker::TemplateExists>("TemplateExists");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, std::string, bool, CMapGeneratorWorker::FindTemplates>("FindTemplates");
|
||||
m_ScriptInterface->RegisterFunction<std::vector<std::string>, std::string, bool, CMapGeneratorWorker::FindActorTemplates>("FindActorTemplates");
|
||||
|
||||
// Engine constants
|
||||
|
||||
// Length of one tile of the terrain grid in metres.
|
||||
// Useful to transform footprint sizes to the tilegrid coordinate system.
|
||||
m_ScriptInterface->SetGlobal("TERRAIN_TILE_SIZE", static_cast<int>(TERRAIN_TILE_SIZE));
|
||||
|
||||
// Number of impassable tiles at the map border
|
||||
m_ScriptInterface->SetGlobal("MAP_BORDER_WIDTH", static_cast<int>(MAP_EDGE_TILES));
|
||||
}
|
||||
|
||||
int CMapGeneratorWorker::GetProgress()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_WorkerMutex);
|
||||
@ -202,7 +220,7 @@ shared_ptr<ScriptInterface::StructuredClone> CMapGeneratorWorker::GetResults()
|
||||
return m_MapData;
|
||||
}
|
||||
|
||||
bool CMapGeneratorWorker::LoadLibrary(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name)
|
||||
bool CMapGeneratorWorker::LoadLibrary(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& name)
|
||||
{
|
||||
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(pCxPrivate->pCBData);
|
||||
return self->LoadScripts(name);
|
||||
@ -259,12 +277,7 @@ std::vector<std::string> CMapGeneratorWorker::FindActorTemplates(ScriptInterface
|
||||
return self->m_TemplateLoader.FindTemplates(path, includeSubdirectories, ACTOR_TEMPLATES);
|
||||
}
|
||||
|
||||
int CMapGeneratorWorker::GetTerrainTileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
||||
{
|
||||
return TERRAIN_TILE_SIZE;
|
||||
}
|
||||
|
||||
bool CMapGeneratorWorker::LoadScripts(const std::wstring& libraryName)
|
||||
bool CMapGeneratorWorker::LoadScripts(const VfsPath& libraryName)
|
||||
{
|
||||
// Ignore libraries that are already loaded
|
||||
if (m_LoadedLibraries.find(libraryName) != m_LoadedLibraries.end())
|
||||
@ -273,7 +286,7 @@ bool CMapGeneratorWorker::LoadScripts(const std::wstring& libraryName)
|
||||
// Mark this as loaded, to prevent it recursively loading itself
|
||||
m_LoadedLibraries.insert(libraryName);
|
||||
|
||||
VfsPath path = L"maps/random/" + libraryName + L"/";
|
||||
VfsPath path = VfsPath(L"maps/random/") / libraryName / VfsPath();
|
||||
VfsPaths pathnames;
|
||||
|
||||
// Load all scripts in mapgen directory
|
||||
@ -302,12 +315,12 @@ bool CMapGeneratorWorker::LoadScripts(const std::wstring& libraryName)
|
||||
return true;
|
||||
}
|
||||
|
||||
JS::Value CMapGeneratorWorker::LoadHeightmap(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& vfsPath)
|
||||
JS::Value CMapGeneratorWorker::LoadHeightmap(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& filename)
|
||||
{
|
||||
std::vector<u16> heightmap;
|
||||
if (LoadHeightmapImageVfs(vfsPath, heightmap) != INFO::OK)
|
||||
if (LoadHeightmapImageVfs(filename, heightmap) != INFO::OK)
|
||||
{
|
||||
LOGERROR("Could not load heightmap file '%s'", utf8_from_wstring(vfsPath).c_str());
|
||||
LOGERROR("Could not load heightmap file '%s'", filename.string8());
|
||||
return JS::UndefinedValue();
|
||||
}
|
||||
|
||||
@ -320,7 +333,7 @@ JS::Value CMapGeneratorWorker::LoadHeightmap(ScriptInterface::CxPrivate* pCxPriv
|
||||
}
|
||||
|
||||
// See CMapReader::UnpackTerrain, CMapReader::ParseTerrain for the reordering
|
||||
JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPrivate, const std::string& filename)
|
||||
JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& filename)
|
||||
{
|
||||
if (!VfsFileExists(filename))
|
||||
throw PSERROR_File_OpenFailed();
|
||||
@ -375,8 +388,7 @@ JS::Value CMapGeneratorWorker::LoadMapTerrain(ScriptInterface::CxPrivate* pCxPri
|
||||
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(pCxPrivate->pCBData);
|
||||
JSContext* cx = self->m_ScriptInterface->GetContext();
|
||||
JSAutoRequest rq(cx);
|
||||
JS::RootedValue returnValue(cx);
|
||||
self->m_ScriptInterface->Eval("({})", &returnValue);
|
||||
JS::RootedValue returnValue(cx, JS::ObjectValue(*JS_NewPlainObject(cx)));
|
||||
self->m_ScriptInterface->SetProperty(returnValue, "height", heightmap);
|
||||
self->m_ScriptInterface->SetProperty(returnValue, "textureNames", textureNames);
|
||||
self->m_ScriptInterface->SetProperty(returnValue, "textureIDs", textureIDs);
|
||||
|
@ -112,42 +112,123 @@ public:
|
||||
shared_ptr<ScriptInterface::StructuredClone> GetResults();
|
||||
|
||||
private:
|
||||
// Mapgen
|
||||
|
||||
/**
|
||||
* Expose functions defined in this class to the script.
|
||||
*/
|
||||
void RegisterScriptFunctions();
|
||||
|
||||
/**
|
||||
* Load all scripts of the given library
|
||||
*
|
||||
* @param libraryName String 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
|
||||
*/
|
||||
bool LoadScripts(const std::wstring& libraryName);
|
||||
bool LoadScripts(const VfsPath& libraryName);
|
||||
|
||||
// callbacks for script functions
|
||||
static bool LoadLibrary(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& name);
|
||||
/**
|
||||
* Recursively load all script files in the given folder.
|
||||
*/
|
||||
static bool LoadLibrary(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& name);
|
||||
|
||||
/**
|
||||
* Finalize map generation and pass results from the script to the engine.
|
||||
*/
|
||||
static void ExportMap(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue data);
|
||||
static JS::Value LoadHeightmap(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& src);
|
||||
static JS::Value LoadMapTerrain(ScriptInterface::CxPrivate* pCxPrivate, const std::string& filename);
|
||||
|
||||
/**
|
||||
* Load an image file and return it as a height array.
|
||||
*/
|
||||
static JS::Value LoadHeightmap(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& src);
|
||||
|
||||
/**
|
||||
* Load an Atlas terrain file (PMP) returning textures and heightmap.
|
||||
*/
|
||||
static JS::Value LoadMapTerrain(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& filename);
|
||||
|
||||
/**
|
||||
* Sets the progress bar, but only within the boundaries of the time that the loading screen consumes.
|
||||
*/
|
||||
static void SetProgress(ScriptInterface::CxPrivate* pCxPrivate, int progress);
|
||||
|
||||
/**
|
||||
* Return the template data of the given template name.
|
||||
*/
|
||||
static CParamNode GetTemplate(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
|
||||
|
||||
/**
|
||||
* Check whether the given template exists.
|
||||
*/
|
||||
static bool TemplateExists(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName);
|
||||
|
||||
/**
|
||||
* Returns all template names of simulation entity templates.
|
||||
*/
|
||||
static std::vector<std::string> FindTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories);
|
||||
|
||||
/**
|
||||
* Returns all template names of actors.
|
||||
*/
|
||||
static std::vector<std::string> FindActorTemplates(ScriptInterface::CxPrivate* pCxPrivate, const std::string& path, bool includeSubdirectories);
|
||||
static int GetTerrainTileSize(ScriptInterface::CxPrivate* pCxPrivate);
|
||||
|
||||
std::set<std::wstring> m_LoadedLibraries;
|
||||
shared_ptr<ScriptInterface::StructuredClone> m_MapData;
|
||||
boost::rand48 m_MapGenRNG;
|
||||
int m_Progress;
|
||||
ScriptInterface* m_ScriptInterface;
|
||||
VfsPath m_ScriptPath;
|
||||
std::string m_Settings;
|
||||
CTemplateLoader m_TemplateLoader;
|
||||
|
||||
// Thread
|
||||
/**
|
||||
* Perform map generation in an independent thread.
|
||||
*/
|
||||
static void* RunThread(void* data);
|
||||
|
||||
/**
|
||||
* Perform the map generation.
|
||||
*/
|
||||
bool Run();
|
||||
|
||||
/**
|
||||
* Currently loaded script librarynames.
|
||||
*/
|
||||
std::set<VfsPath> m_LoadedLibraries;
|
||||
|
||||
/**
|
||||
* Result of the mapscript generation including terrain, entities and environment settings.
|
||||
*/
|
||||
shared_ptr<ScriptInterface::StructuredClone> m_MapData;
|
||||
|
||||
/**
|
||||
* Deterministic random number generator.
|
||||
*/
|
||||
boost::rand48 m_MapGenRNG;
|
||||
|
||||
/**
|
||||
* Current map generation progress.
|
||||
*/
|
||||
int m_Progress;
|
||||
|
||||
/**
|
||||
* Provides the script context.
|
||||
*/
|
||||
ScriptInterface* m_ScriptInterface;
|
||||
|
||||
/**
|
||||
* Map generation script to run.
|
||||
*/
|
||||
VfsPath m_ScriptPath;
|
||||
|
||||
/**
|
||||
* Map and simulation settings chosen in the gamesetup stage.
|
||||
*/
|
||||
std::string m_Settings;
|
||||
|
||||
/**
|
||||
* Backend to loading template data.
|
||||
*/
|
||||
CTemplateLoader m_TemplateLoader;
|
||||
|
||||
/**
|
||||
* Holds the mapgeneration thread identifier.
|
||||
*/
|
||||
pthread_t m_WorkerThread;
|
||||
|
||||
/**
|
||||
* Avoids thread synchronization issues.
|
||||
*/
|
||||
std::mutex m_WorkerMutex;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user