2011-03-22 02:34:45 +01:00
|
|
|
/* Copyright (C) 2011 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "MapGenerator.h"
|
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2011-03-22 02:34:45 +01:00
|
|
|
#include "lib/timer.h"
|
2011-04-15 07:23:51 +02:00
|
|
|
#include "lib/utf8.h"
|
2011-03-22 02:34:45 +01:00
|
|
|
#include "ps/CLogger.h"
|
2011-03-23 09:12:04 +01:00
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
// TODO: what's a good default? perhaps based on map size
|
|
|
|
#define RMS_RUNTIME_SIZE 96 * 1024 * 1024
|
|
|
|
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGeneratorWorker::CMapGeneratorWorker()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
// If something happens before we initialize, that's a failure
|
|
|
|
m_Progress = -1;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGeneratorWorker::~CMapGeneratorWorker()
|
|
|
|
{
|
|
|
|
// Wait for thread to end
|
|
|
|
pthread_join(m_WorkerThread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings)
|
|
|
|
{
|
|
|
|
CScopeLock lock(m_WorkerMutex);
|
|
|
|
|
|
|
|
// Set progress to positive value
|
|
|
|
m_Progress = 1;
|
|
|
|
m_ScriptPath = scriptFile;
|
|
|
|
m_Settings = settings;
|
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
// Preload random map and library scripts
|
|
|
|
fs_util::ForEachFile(g_VFS, L"maps/random/", PreloadScript, (uintptr_t)this, L"*.js", fs_util::DIR_RECURSIVE);
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
// Launch the worker thread
|
|
|
|
int ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this);
|
|
|
|
debug_assert(ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* CMapGeneratorWorker::RunThread(void *data)
|
|
|
|
{
|
|
|
|
debug_SetThreadName("MapGenerator");
|
|
|
|
|
|
|
|
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(data);
|
|
|
|
|
|
|
|
self->m_ScriptInterface = new ScriptInterface("RMS", "MapGenerator", ScriptInterface::CreateRuntime(RMS_RUNTIME_SIZE));
|
|
|
|
|
|
|
|
// Run map generation scripts
|
2011-04-11 00:21:15 +02:00
|
|
|
if ((!self->Run()) || (self->m_Progress > 0))
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
2011-04-11 00:21:15 +02:00
|
|
|
// Don't leave progress in an unknown state, if generator failed, set it to -1
|
2011-04-10 07:31:18 +02:00
|
|
|
CScopeLock lock(self->m_WorkerMutex);
|
|
|
|
self->m_Progress = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point the random map scripts are done running, so the thread has no further purpose
|
|
|
|
// and can die. The data will be stored in m_MapData already if successful, or m_Progress
|
|
|
|
// will contain an error value on failure.
|
|
|
|
|
|
|
|
// Cleanup ScriptInterface
|
|
|
|
SAFE_DELETE(self->m_ScriptInterface);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
return NULL;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
bool CMapGeneratorWorker::Run()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
m_ScriptInterface->SetCallbackData(static_cast<void*> (this));
|
|
|
|
|
|
|
|
// Replace RNG with a seeded deterministic function
|
|
|
|
m_ScriptInterface->ReplaceNondeterministicFunctions(m_MapGenRNG);
|
|
|
|
|
|
|
|
// Functions for RMS
|
|
|
|
m_ScriptInterface->RegisterFunction<bool, std::wstring, CMapGeneratorWorker::LoadLibrary>("LoadLibrary");
|
|
|
|
m_ScriptInterface->RegisterFunction<void, CScriptValRooted, CMapGeneratorWorker::ExportMap>("ExportMap");
|
|
|
|
m_ScriptInterface->RegisterFunction<void, int, CMapGeneratorWorker::SetProgress>("SetProgress");
|
|
|
|
m_ScriptInterface->RegisterFunction<void, CMapGeneratorWorker::MaybeGC>("MaybeGC");
|
|
|
|
|
|
|
|
// Parse settings
|
|
|
|
CScriptValRooted settingsVal = m_ScriptInterface->ParseJSON(m_Settings);
|
|
|
|
if (settingsVal.undefined())
|
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGERROR(L"CMapGeneratorWorker::Run: Failed to parse settings");
|
2011-04-10 07:31:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
// Init RNG seed
|
|
|
|
uint32 seed;
|
2011-04-10 07:31:18 +02:00
|
|
|
if (!m_ScriptInterface->GetProperty(settingsVal.get(), "Seed", seed))
|
2011-03-23 09:12:04 +01:00
|
|
|
{ // No seed specified
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGWARNING(L"CMapGeneratorWorker::Run: No seed value specified - using 0");
|
2011-03-22 02:34:45 +01:00
|
|
|
seed = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-23 09:12:04 +01:00
|
|
|
m_MapGenRNG.seed(seed);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
// Copy settings to global variable
|
|
|
|
if (!m_ScriptInterface->SetProperty(m_ScriptInterface->GetGlobalObject(), "g_MapSettings", settingsVal))
|
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGERROR(L"CMapGeneratorWorker::Run: Failed to define g_MapSettings");
|
2011-03-22 02:34:45 +01:00
|
|
|
return false;
|
2011-04-10 07:31:18 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
// Find RMS file and run
|
|
|
|
ScriptFilesMap::iterator it = m_ScriptFiles.find(m_ScriptPath);
|
|
|
|
if (it != m_ScriptFiles.end())
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGMESSAGE(L"CMapGeneratorWorker::Run: Loading RMS '%ls'", m_ScriptPath.string().c_str());
|
|
|
|
// Note: we're not really accessing the file here
|
|
|
|
if (m_ScriptInterface->LoadGlobalScript(m_ScriptPath, it->second))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGERROR(L"CMapGeneratorWorker::Run: Failed to load RMS '%ls'", m_ScriptPath.string().c_str());
|
|
|
|
return false;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
int CMapGeneratorWorker::GetProgress()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
CScopeLock lock(m_WorkerMutex);
|
|
|
|
return m_Progress;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
shared_ptr<ScriptInterface::StructuredClone> CMapGeneratorWorker::GetResults()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
CScopeLock lock(m_WorkerMutex);
|
2011-03-22 02:34:45 +01:00
|
|
|
return m_MapData;
|
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
bool CMapGeneratorWorker::LoadLibrary(void* cbdata, std::wstring name)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(cbdata);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
return self->LoadScripts(name);
|
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
void CMapGeneratorWorker::ExportMap(void* cbdata, CScriptValRooted data)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(cbdata);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
// Copy results
|
2011-04-10 07:31:18 +02:00
|
|
|
CScopeLock lock(self->m_WorkerMutex);
|
|
|
|
self->m_MapData = self->m_ScriptInterface->WriteStructuredClone(data.get());
|
|
|
|
self->m_Progress = 0;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
void CMapGeneratorWorker::SetProgress(void* cbdata, int progress)
|
|
|
|
{
|
|
|
|
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(cbdata);
|
|
|
|
|
|
|
|
// Copy data
|
|
|
|
CScopeLock lock(self->m_WorkerMutex);
|
|
|
|
self->m_Progress = progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGeneratorWorker::MaybeGC(void* cbdata)
|
|
|
|
{
|
|
|
|
CMapGeneratorWorker* self = static_cast<CMapGeneratorWorker*>(cbdata);
|
|
|
|
self->m_ScriptInterface->MaybeGC();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMapGeneratorWorker::LoadScripts(const std::wstring& libraryName)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
// Ignore libraries that are already loaded
|
|
|
|
if (m_LoadedLibraries.find(libraryName) != m_LoadedLibraries.end())
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2011-03-22 02:34:45 +01:00
|
|
|
return true;
|
2011-04-11 00:21:15 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
// Mark this as loaded, to prevent it recursively loading itself
|
|
|
|
m_LoadedLibraries.insert(libraryName);
|
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGMESSAGE(L"Loading '%ls' library", libraryName.c_str());
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
std::wstring libraryPath = L"maps/random/" + libraryName + L"/";
|
|
|
|
|
|
|
|
// Iterate preloaded script map, running library scripts
|
|
|
|
for (ScriptFilesMap::iterator it = m_ScriptFiles.begin(); it != m_ScriptFiles.end(); ++it)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
std::wstring path = it->first.string();
|
|
|
|
if (path.find(libraryPath) == 0)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
// This script is part of the library, so load it
|
|
|
|
// Note: we're not really accessing the file here
|
|
|
|
if (m_ScriptInterface->LoadGlobalScript(path, it->second))
|
|
|
|
{
|
|
|
|
LOGMESSAGE(L"Successfully loaded library script '%ls'", path.c_str());
|
|
|
|
}
|
|
|
|
else
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
// Script failed to run
|
|
|
|
LOGERROR(L"Failed loading library script '%ls'", path.c_str());
|
2011-04-11 00:21:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
2011-04-15 07:23:51 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibError CMapGeneratorWorker::PreloadScript(const VfsPath& pathname, const FileInfo& UNUSED(fileInfo), const uintptr_t cbData)
|
|
|
|
{
|
|
|
|
CMapGeneratorWorker* self = (CMapGeneratorWorker*)cbData;
|
|
|
|
|
|
|
|
if (!VfsFileExists(g_VFS, pathname))
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
LOGERROR(L"CMapGeneratorWorker::PreloadScript: File '%ls' does not exist", pathname.string().c_str());
|
|
|
|
return ERR::VFS_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVFSFile file;
|
|
|
|
|
|
|
|
PSRETURN ret = file.Load(g_VFS, pathname);
|
|
|
|
if (ret != PSRETURN_OK)
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2011-04-15 07:23:51 +02:00
|
|
|
LOGERROR(L"CMapGeneratorWorker::PreloadScript: Failed to load file '%ls', error=%hs", pathname.string().c_str(), GetErrorString(ret));
|
|
|
|
return ERR::FAIL;
|
2011-04-11 00:21:15 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
std::string content(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize());
|
|
|
|
|
|
|
|
self->m_ScriptFiles.insert(std::make_pair(pathname, wstring_from_utf8(content)));
|
|
|
|
return INFO::OK;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2011-04-11 00:21:15 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2011-04-15 07:23:51 +02:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGenerator::CMapGenerator() : m_Worker(new CMapGeneratorWorker())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CMapGenerator::~CMapGenerator()
|
|
|
|
{
|
|
|
|
delete m_Worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::GenerateMap(const VfsPath& scriptFile, const std::string& settings)
|
|
|
|
{
|
|
|
|
m_Worker->Initialize(scriptFile, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CMapGenerator::GetProgress()
|
|
|
|
{
|
|
|
|
return m_Worker->GetProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<ScriptInterface::StructuredClone> CMapGenerator::GetResults()
|
|
|
|
{
|
|
|
|
return m_Worker->GetResults();
|
|
|
|
}
|