2023-06-18 14:19:26 +02:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2011-03-22 02:34:45 +01:00
|
|
|
* 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"
|
|
|
|
|
2018-02-05 17:02:00 +01:00
|
|
|
#include "graphics/MapIO.h"
|
2018-02-07 00:05:58 +01:00
|
|
|
#include "graphics/Patch.h"
|
2017-11-23 21:54:48 +01:00
|
|
|
#include "graphics/Terrain.h"
|
2018-02-19 16:46:25 +01:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2018-02-05 17:02:00 +01:00
|
|
|
#include "lib/status.h"
|
2011-03-22 02:34:45 +01:00
|
|
|
#include "lib/timer.h"
|
2018-05-01 23:15:55 +02:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
2018-02-07 00:05:58 +01:00
|
|
|
#include "maths/MathUtil.h"
|
2011-03-22 02:34:45 +01:00
|
|
|
#include "ps/CLogger.h"
|
2018-02-07 00:05:58 +01:00
|
|
|
#include "ps/FileIo.h"
|
2016-06-21 12:33:11 +02:00
|
|
|
#include "ps/Profile.h"
|
2021-06-03 16:48:38 +02:00
|
|
|
#include "ps/TaskManager.h"
|
2017-11-24 18:19:16 +01:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2021-03-02 21:01:14 +01:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2020-11-14 11:57:50 +01:00
|
|
|
#include "scriptinterface/ScriptContext.h"
|
2018-02-05 17:02:00 +01:00
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
2018-05-01 23:15:55 +02:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2021-05-14 12:18:03 +02:00
|
|
|
#include "scriptinterface/JSON.h"
|
2019-07-12 18:38:51 +02:00
|
|
|
#include "simulation2/helpers/MapEdgeTiles.h"
|
2018-05-01 23:15:55 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-05-04 23:24:25 +02:00
|
|
|
|
2020-11-12 10:34:40 +01:00
|
|
|
// TODO: Maybe this should be optimized depending on the map size.
|
2020-11-14 11:57:50 +01:00
|
|
|
constexpr int RMS_CONTEXT_SIZE = 96 * 1024 * 1024;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2018-05-08 12:03:46 +02:00
|
|
|
extern bool IsQuitRequested();
|
|
|
|
|
2018-02-19 16:46:25 +01:00
|
|
|
static bool
|
|
|
|
MapGeneratorInterruptCallback(JSContext* UNUSED(cx))
|
|
|
|
{
|
2018-05-08 12:03:46 +02:00
|
|
|
// This may not use SDL_IsQuitRequested(), because it runs in a thread separate to SDL, see SDL_PumpEvents
|
|
|
|
if (IsQuitRequested())
|
2018-02-19 16:46:25 +01:00
|
|
|
{
|
|
|
|
LOGWARNING("Quit requested!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
CMapGeneratorWorker::CMapGeneratorWorker(ScriptInterface* scriptInterface) :
|
|
|
|
m_ScriptInterface(scriptInterface)
|
2023-06-18 14:19:26 +02:00
|
|
|
{}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
CMapGeneratorWorker::~CMapGeneratorWorker()
|
|
|
|
{
|
2021-06-03 16:48:38 +02:00
|
|
|
// Cancel or wait for the task to end.
|
|
|
|
m_WorkerThread.CancelOrWait();
|
2011-04-10 07:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings)
|
|
|
|
{
|
2019-06-06 21:30:48 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_WorkerMutex);
|
2011-04-10 07:31:18 +02:00
|
|
|
|
|
|
|
// Set progress to positive value
|
2023-06-18 14:19:26 +02:00
|
|
|
m_Progress.store(1);
|
2011-04-10 07:31:18 +02:00
|
|
|
m_ScriptPath = scriptFile;
|
|
|
|
m_Settings = settings;
|
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
// Start generating the map asynchronously.
|
|
|
|
m_WorkerThread = Threading::TaskManager::Instance().PushTask([this]() {
|
|
|
|
PROFILE2("Map Generation");
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
std::shared_ptr<ScriptContext> mapgenContext = ScriptContext::CreateContext(RMS_CONTEXT_SIZE);
|
2018-02-19 16:46:25 +01:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
// Enable the script to be aborted
|
|
|
|
JS_AddInterruptCallback(mapgenContext->GetGeneralJSContext(), MapGeneratorInterruptCallback);
|
2018-02-19 16:46:25 +01:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext);
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
// Run map generation scripts
|
2023-06-18 14:19:26 +02:00
|
|
|
if (!Run() || m_Progress.load() > 0)
|
2021-06-03 16:48:38 +02:00
|
|
|
{
|
|
|
|
// Don't leave progress in an unknown state, if generator failed, set it to -1
|
2023-06-18 14:19:26 +02:00
|
|
|
m_Progress.store(-1);
|
2021-06-03 16:48:38 +02:00
|
|
|
}
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
SAFE_DELETE(m_ScriptInterface);
|
2019-07-17 02:07:10 +02:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
// 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.
|
|
|
|
});
|
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
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-27 00:33:16 +02:00
|
|
|
// Parse settings
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue settingsVal(rq.cx);
|
2021-05-14 12:18:03 +02:00
|
|
|
if (!Script::ParseJSON(rq, m_Settings, &settingsVal) && settingsVal.isUndefined())
|
2014-07-27 00:33:16 +02:00
|
|
|
{
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGERROR("CMapGeneratorWorker::Run: Failed to parse settings");
|
2014-07-27 00:33:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2017-10-06 20:53:24 +02:00
|
|
|
// Prevent unintentional modifications to the settings object by random map scripts
|
2021-05-13 19:23:52 +02:00
|
|
|
if (!Script::FreezeObject(rq, settingsVal, true))
|
2017-10-06 20:53:24 +02:00
|
|
|
{
|
|
|
|
LOGERROR("CMapGeneratorWorker::Run: Failed to deepfreeze settings");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:33:16 +02:00
|
|
|
// Init RNG seed
|
2017-06-02 17:07:58 +02:00
|
|
|
u32 seed = 0;
|
2021-05-13 19:23:52 +02:00
|
|
|
if (!Script::HasProperty(rq, settingsVal, "Seed") ||
|
|
|
|
!Script::GetProperty(rq, settingsVal, "Seed", seed))
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGWARNING("CMapGeneratorWorker::Run: No seed value specified - using 0");
|
2014-07-27 00:33:16 +02:00
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
InitScriptInterface(seed);
|
|
|
|
|
|
|
|
RegisterScriptFunctions_MapGenerator();
|
2014-07-27 00:33:16 +02:00
|
|
|
|
|
|
|
// Copy settings to global variable
|
2020-11-14 09:46:32 +01:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2021-05-13 19:23:52 +02:00
|
|
|
if (!Script::SetProperty(rq, global, "g_MapSettings", settingsVal, true, true))
|
2014-07-27 00:33:16 +02:00
|
|
|
{
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGERROR("CMapGeneratorWorker::Run: Failed to define g_MapSettings");
|
2014-07-27 00:33:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load RMS
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGMESSAGE("Loading RMS '%s'", m_ScriptPath.string8());
|
2014-07-27 00:33:16 +02:00
|
|
|
if (!m_ScriptInterface->LoadGlobalScriptFile(m_ScriptPath))
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("CMapGeneratorWorker::Run: Failed to load RMS '%s'", m_ScriptPath.string8());
|
2014-07-27 00:33:16 +02:00
|
|
|
return false;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2014-07-27 00:33:16 +02:00
|
|
|
return true;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
#define REGISTER_MAPGEN_FUNC(func) \
|
2021-05-15 15:54:58 +02:00
|
|
|
ScriptFunction::Register<&CMapGeneratorWorker::func, ScriptInterface::ObjectFromCBData<CMapGeneratorWorker>>(rq, #func);
|
2021-03-02 21:01:14 +01:00
|
|
|
#define REGISTER_MAPGEN_FUNC_NAME(func, name) \
|
2021-05-15 15:54:58 +02:00
|
|
|
ScriptFunction::Register<&CMapGeneratorWorker::func, ScriptInterface::ObjectFromCBData<CMapGeneratorWorker>>(rq, name);
|
2021-03-02 21:01:14 +01:00
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
void CMapGeneratorWorker::InitScriptInterface(const u32 seed)
|
2019-07-13 23:41:09 +02:00
|
|
|
{
|
2020-01-29 01:30:07 +01:00
|
|
|
m_ScriptInterface->SetCallbackData(static_cast<void*>(this));
|
|
|
|
|
|
|
|
m_ScriptInterface->ReplaceNondeterministicRNG(m_MapGenRNG);
|
|
|
|
m_MapGenRNG.seed(seed);
|
|
|
|
|
2019-07-13 23:41:09 +02:00
|
|
|
// VFS
|
2022-05-15 08:34:17 +02:00
|
|
|
JSI_VFS::RegisterScriptFunctions_ReadOnlySimulationMaps(*m_ScriptInterface);
|
2019-07-13 23:41:09 +02:00
|
|
|
|
|
|
|
// Globalscripts may use VFS script functions
|
|
|
|
m_ScriptInterface->LoadGlobalScripts();
|
|
|
|
|
|
|
|
// File loading
|
2021-03-02 21:01:14 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
REGISTER_MAPGEN_FUNC_NAME(LoadScripts, "LoadLibrary");
|
|
|
|
REGISTER_MAPGEN_FUNC_NAME(LoadHeightmap, "LoadHeightmapImage");
|
|
|
|
REGISTER_MAPGEN_FUNC(LoadMapTerrain);
|
2019-07-13 23:41:09 +02:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
void CMapGeneratorWorker::RegisterScriptFunctions_MapGenerator()
|
|
|
|
{
|
2021-03-02 21:01:14 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
// Template functions
|
2021-03-02 21:01:14 +01:00
|
|
|
REGISTER_MAPGEN_FUNC(GetTemplate);
|
|
|
|
REGISTER_MAPGEN_FUNC(TemplateExists);
|
|
|
|
REGISTER_MAPGEN_FUNC(FindTemplates);
|
|
|
|
REGISTER_MAPGEN_FUNC(FindActorTemplates);
|
2020-01-29 01:30:07 +01:00
|
|
|
|
|
|
|
// Progression and profiling
|
2021-03-02 21:01:14 +01:00
|
|
|
REGISTER_MAPGEN_FUNC(SetProgress);
|
|
|
|
REGISTER_MAPGEN_FUNC(GetMicroseconds);
|
|
|
|
REGISTER_MAPGEN_FUNC(ExportMap);
|
2020-01-29 01:30:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
#undef REGISTER_MAPGEN_FUNC
|
|
|
|
#undef REGISTER_MAPGEN_FUNC_NAME
|
|
|
|
|
2023-06-18 14:19:26 +02:00
|
|
|
int CMapGeneratorWorker::GetProgress() const
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2023-06-18 14:19:26 +02:00
|
|
|
return m_Progress.load();
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
double CMapGeneratorWorker::GetMicroseconds()
|
2019-07-15 16:44:53 +02:00
|
|
|
{
|
|
|
|
return JS_Now();
|
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::StructuredClone CMapGeneratorWorker::GetResults()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2019-06-06 21:30:48 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_WorkerMutex);
|
2011-03-22 02:34:45 +01:00
|
|
|
return m_MapData;
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
void CMapGeneratorWorker::ExportMap(JS::HandleValue data)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2023-06-18 14:19:26 +02:00
|
|
|
{
|
|
|
|
// Copy results
|
|
|
|
std::lock_guard<std::mutex> lock(m_WorkerMutex);
|
|
|
|
m_MapData = Script::WriteStructuredClone(ScriptRequest(m_ScriptInterface), data);
|
|
|
|
}
|
|
|
|
m_Progress.store(0);
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
void CMapGeneratorWorker::SetProgress(int progress)
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
2023-06-18 14:19:26 +02:00
|
|
|
// When the task is started, `m_Progress` is only mutated by this thread.
|
|
|
|
const int currentProgress = m_Progress.load();
|
|
|
|
if (progress >= currentProgress)
|
|
|
|
m_Progress.store(progress);
|
2018-03-01 13:06:55 +01:00
|
|
|
else
|
2023-06-18 14:19:26 +02:00
|
|
|
LOGWARNING("The random map script tried to reduce the loading progress from %d to %d",
|
|
|
|
currentProgress, progress);
|
2011-04-10 07:31:18 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
CParamNode CMapGeneratorWorker::GetTemplate(const std::string& templateName)
|
2014-06-07 14:12:53 +02:00
|
|
|
{
|
2022-12-30 08:34:23 +01:00
|
|
|
const CParamNode& templateRoot = m_TemplateLoader.GetTemplateFileData(templateName).GetOnlyChild();
|
2014-06-07 14:12:53 +02:00
|
|
|
if (!templateRoot.IsOk())
|
2015-01-22 21:36:24 +01:00
|
|
|
LOGERROR("Invalid template found for '%s'", templateName.c_str());
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-06-07 14:12:53 +02:00
|
|
|
return templateRoot;
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
bool CMapGeneratorWorker::TemplateExists(const std::string& templateName)
|
2015-06-21 22:22:34 +02:00
|
|
|
{
|
2021-03-02 21:01:14 +01:00
|
|
|
return m_TemplateLoader.TemplateExists(templateName);
|
2015-06-21 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
std::vector<std::string> CMapGeneratorWorker::FindTemplates(const std::string& path, bool includeSubdirectories)
|
2014-06-07 14:12:53 +02:00
|
|
|
{
|
2021-03-02 21:01:14 +01:00
|
|
|
return m_TemplateLoader.FindTemplates(path, includeSubdirectories, SIMULATION_TEMPLATES);
|
2014-06-07 14:12:53 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
std::vector<std::string> CMapGeneratorWorker::FindActorTemplates(const std::string& path, bool includeSubdirectories)
|
2014-06-07 14:12:53 +02:00
|
|
|
{
|
2021-03-02 21:01:14 +01:00
|
|
|
return m_TemplateLoader.FindTemplates(path, includeSubdirectories, ACTOR_TEMPLATES);
|
2014-06-07 14:12:53 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 23:41:09 +02:00
|
|
|
bool CMapGeneratorWorker::LoadScripts(const VfsPath& libraryName)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
|
|
|
// Ignore libraries that are already loaded
|
|
|
|
if (m_LoadedLibraries.find(libraryName) != m_LoadedLibraries.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Mark this as loaded, to prevent it recursively loading itself
|
|
|
|
m_LoadedLibraries.insert(libraryName);
|
|
|
|
|
2019-07-13 23:41:09 +02:00
|
|
|
VfsPath path = VfsPath(L"maps/random/") / libraryName / VfsPath();
|
2011-05-04 23:24:25 +02:00
|
|
|
VfsPaths pathnames;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-05-04 23:24:25 +02:00
|
|
|
// Load all scripts in mapgen directory
|
2011-05-25 12:39:13 +02:00
|
|
|
Status ret = vfs::GetPathnames(g_VFS, path, L"*.js", pathnames);
|
2011-05-04 23:24:25 +02:00
|
|
|
if (ret == INFO::OK)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2015-06-21 20:46:33 +02:00
|
|
|
for (const VfsPath& p : pathnames)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2015-06-21 20:46:33 +02:00
|
|
|
LOGMESSAGE("Loading map generator script '%s'", p.string8());
|
2011-05-04 23:24:25 +02:00
|
|
|
|
2015-06-21 20:46:33 +02:00
|
|
|
if (!m_ScriptInterface->LoadGlobalScriptFile(p))
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2015-06-21 20:46:33 +02:00
|
|
|
LOGERROR("CMapGeneratorWorker::LoadScripts: Failed to load script '%s'", p.string8());
|
2011-04-11 00:21:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-04 23:24:25 +02:00
|
|
|
else
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2011-05-04 23:24:25 +02:00
|
|
|
// Some error reading directory
|
|
|
|
wchar_t error[200];
|
2015-01-22 21:37:38 +01:00
|
|
|
LOGERROR("CMapGeneratorWorker::LoadScripts: Error reading scripts in directory '%s': %s", path.string8(), utf8_from_wstring(StatusDescription(ret, error, ARRAY_SIZE(error))));
|
2011-05-04 23:24:25 +02:00
|
|
|
return false;
|
2011-04-11 00:21:15 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-05-04 23:24:25 +02:00
|
|
|
return true;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
JS::Value CMapGeneratorWorker::LoadHeightmap(const VfsPath& filename)
|
2018-02-05 17:02:00 +01:00
|
|
|
{
|
|
|
|
std::vector<u16> heightmap;
|
2019-07-13 23:41:09 +02:00
|
|
|
if (LoadHeightmapImageVfs(filename, heightmap) != INFO::OK)
|
2018-02-05 17:02:00 +01:00
|
|
|
{
|
2019-07-13 23:41:09 +02:00
|
|
|
LOGERROR("Could not load heightmap file '%s'", filename.string8());
|
2018-02-05 17:02:00 +01:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:01:14 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue returnValue(rq.cx);
|
2021-05-13 11:43:33 +02:00
|
|
|
Script::ToJSVal(rq, &returnValue, heightmap);
|
2018-02-05 17:02:00 +01:00
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2018-02-07 00:05:58 +01:00
|
|
|
// See CMapReader::UnpackTerrain, CMapReader::ParseTerrain for the reordering
|
2021-03-02 21:01:14 +01:00
|
|
|
JS::Value CMapGeneratorWorker::LoadMapTerrain(const VfsPath& filename)
|
2018-02-07 00:05:58 +01:00
|
|
|
{
|
2021-03-02 21:01:14 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2019-07-21 03:35:39 +02:00
|
|
|
|
2018-02-07 00:05:58 +01:00
|
|
|
if (!VfsFileExists(filename))
|
2019-07-21 03:35:39 +02:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptException::Raise(rq, "Terrain file \"%s\" does not exist!", filename.string8().c_str());
|
2019-07-21 03:35:39 +02:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2018-02-07 00:05:58 +01:00
|
|
|
|
|
|
|
CFileUnpacker unpacker;
|
|
|
|
unpacker.Read(filename, "PSMP");
|
|
|
|
|
|
|
|
if (unpacker.GetVersion() < CMapIO::FILE_READ_VERSION)
|
2019-07-21 03:35:39 +02:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
ScriptException::Raise(rq, "Could not load terrain file \"%s\" too old version!", filename.string8().c_str());
|
2019-07-21 03:35:39 +02:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2018-02-07 00:05:58 +01:00
|
|
|
|
|
|
|
// unpack size
|
|
|
|
ssize_t patchesPerSide = (ssize_t)unpacker.UnpackSize();
|
|
|
|
size_t verticesPerSide = patchesPerSide * PATCH_SIZE + 1;
|
|
|
|
|
|
|
|
// unpack heightmap
|
|
|
|
std::vector<u16> heightmap;
|
|
|
|
heightmap.resize(SQR(verticesPerSide));
|
|
|
|
unpacker.UnpackRaw(&heightmap[0], SQR(verticesPerSide) * sizeof(u16));
|
|
|
|
|
|
|
|
// unpack texture names
|
|
|
|
size_t textureCount = unpacker.UnpackSize();
|
|
|
|
std::vector<std::string> textureNames;
|
|
|
|
textureNames.reserve(textureCount);
|
|
|
|
for (size_t i = 0; i < textureCount; ++i)
|
|
|
|
{
|
|
|
|
CStr texturename;
|
|
|
|
unpacker.UnpackString(texturename);
|
|
|
|
textureNames.push_back(texturename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unpack texture IDs per tile
|
|
|
|
ssize_t tilesPerSide = patchesPerSide * PATCH_SIZE;
|
|
|
|
std::vector<CMapIO::STileDesc> tiles;
|
|
|
|
tiles.resize(size_t(SQR(tilesPerSide)));
|
|
|
|
unpacker.UnpackRaw(&tiles[0], sizeof(CMapIO::STileDesc) * tiles.size());
|
|
|
|
|
|
|
|
// reorder by patches and store and save texture IDs per tile
|
|
|
|
std::vector<u16> textureIDs;
|
|
|
|
for (ssize_t x = 0; x < tilesPerSide; ++x)
|
|
|
|
{
|
|
|
|
size_t patchX = x / PATCH_SIZE;
|
|
|
|
size_t offX = x % PATCH_SIZE;
|
|
|
|
for (ssize_t y = 0; y < tilesPerSide; ++y)
|
|
|
|
{
|
|
|
|
size_t patchY = y / PATCH_SIZE;
|
|
|
|
size_t offY = y % PATCH_SIZE;
|
|
|
|
// m_Priority and m_Tex2Index unused
|
|
|
|
textureIDs.push_back(tiles[(patchY * patchesPerSide + patchX) * SQR(PATCH_SIZE) + (offY * PATCH_SIZE + offX)].m_Tex1Index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:18:22 +01:00
|
|
|
JS::RootedValue returnValue(rq.cx);
|
2019-07-22 21:35:14 +02:00
|
|
|
|
2021-05-13 19:23:52 +02:00
|
|
|
Script::CreateObject(
|
2020-11-13 14:18:22 +01:00
|
|
|
rq,
|
2019-07-22 21:35:14 +02:00
|
|
|
&returnValue,
|
|
|
|
"height", heightmap,
|
|
|
|
"textureNames", textureNames,
|
|
|
|
"textureIDs", textureIDs);
|
2018-02-07 00:05:58 +01:00
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2011-04-11 00:21:15 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2020-01-29 01:30:07 +01:00
|
|
|
CMapGenerator::CMapGenerator() : m_Worker(new CMapGeneratorWorker(nullptr))
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CMapGenerator::~CMapGenerator()
|
|
|
|
{
|
|
|
|
delete m_Worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapGenerator::GenerateMap(const VfsPath& scriptFile, const std::string& settings)
|
|
|
|
{
|
|
|
|
m_Worker->Initialize(scriptFile, settings);
|
|
|
|
}
|
|
|
|
|
2023-06-18 14:19:26 +02:00
|
|
|
int CMapGenerator::GetProgress() const
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
|
|
|
return m_Worker->GetProgress();
|
|
|
|
}
|
|
|
|
|
2021-05-10 13:51:32 +02:00
|
|
|
Script::StructuredClone CMapGenerator::GetResults()
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
|
|
|
return m_Worker->GetResults();
|
|
|
|
}
|