2023-06-18 14:19:26 +02:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2011-03-22 02:34:45 +01:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2011-03-22 02:34:45 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2011-03-22 02:34:45 +01:00
|
|
|
* 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
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2011-03-22 02:34:45 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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-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"
|
2017-11-24 18:19:16 +01:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2023-11-19 20:19:32 +01:00
|
|
|
#include "ps/TemplateLoader.h"
|
2021-03-02 21:01:14 +01:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2023-07-07 22:12:16 +02:00
|
|
|
#include "scriptinterface/JSON.h"
|
|
|
|
#include "scriptinterface/Object.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"
|
2019-07-12 18:38:51 +02:00
|
|
|
#include "simulation2/helpers/MapEdgeTiles.h"
|
2018-05-01 23:15:55 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
|
|
|
#include <set>
|
2018-05-01 23:15:55 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-05-04 23:24:25 +02:00
|
|
|
|
2018-05-08 12:03:46 +02:00
|
|
|
extern bool IsQuitRequested();
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool MapGenerationInterruptCallback(JSContext* UNUSED(cx))
|
2018-02-19 16:46:25 +01:00
|
|
|
{
|
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
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Provides callback's for the JavaScript.
|
|
|
|
*/
|
|
|
|
class CMapGenerationCallbacks
|
2011-04-10 07:31:18 +02:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
public:
|
|
|
|
// Only the constructor and the destructor are called by C++.
|
|
|
|
|
|
|
|
CMapGenerationCallbacks(std::atomic<int>& progress, ScriptInterface& scriptInterface,
|
|
|
|
Script::StructuredClone& mapData, const u16 flags) :
|
|
|
|
m_Progress{progress},
|
|
|
|
m_ScriptInterface{scriptInterface},
|
|
|
|
m_MapData{mapData}
|
|
|
|
{
|
|
|
|
m_ScriptInterface.SetCallbackData(static_cast<void*>(this));
|
2018-02-19 16:46:25 +01:00
|
|
|
|
2021-06-03 16:48:38 +02:00
|
|
|
// Enable the script to be aborted
|
2023-11-19 20:19:32 +01:00
|
|
|
JS_AddInterruptCallback(m_ScriptInterface.GetGeneralJSContext(),
|
|
|
|
&MapGenerationInterruptCallback);
|
2018-02-19 16:46:25 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Set initial seed, callback data.
|
|
|
|
// Expose functions, globals and classes relevant to the map scripts.
|
|
|
|
#define REGISTER_MAPGEN_FUNC(func) \
|
|
|
|
ScriptFunction::Register<&CMapGenerationCallbacks::func, \
|
|
|
|
ScriptInterface::ObjectFromCBData<CMapGenerationCallbacks>>(rq, #func, flags);
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// VFS
|
|
|
|
JSI_VFS::RegisterScriptFunctions_ReadOnlySimulationMaps(m_ScriptInterface, flags);
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Globalscripts may use VFS script functions
|
|
|
|
m_ScriptInterface.LoadGlobalScripts();
|
2019-07-17 02:07:10 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// File loading
|
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
REGISTER_MAPGEN_FUNC(LoadLibrary);
|
|
|
|
REGISTER_MAPGEN_FUNC(LoadHeightmapImage);
|
|
|
|
REGISTER_MAPGEN_FUNC(LoadMapTerrain);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Template functions
|
|
|
|
REGISTER_MAPGEN_FUNC(GetTemplate);
|
|
|
|
REGISTER_MAPGEN_FUNC(TemplateExists);
|
|
|
|
REGISTER_MAPGEN_FUNC(FindTemplates);
|
|
|
|
REGISTER_MAPGEN_FUNC(FindActorTemplates);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Progression and profiling
|
|
|
|
REGISTER_MAPGEN_FUNC(SetProgress);
|
|
|
|
REGISTER_MAPGEN_FUNC(GetMicroseconds);
|
|
|
|
REGISTER_MAPGEN_FUNC(ExportMap);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Engine constants
|
2014-07-27 00:33:16 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// 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));
|
2020-01-29 01:30:07 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Number of impassable tiles at the map border
|
|
|
|
m_ScriptInterface.SetGlobal("MAP_BORDER_WIDTH", static_cast<int>(MAP_EDGE_TILES));
|
2014-07-27 00:33:16 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
#undef REGISTER_MAPGEN_FUNC
|
2014-07-27 00:33:16 +02:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
~CMapGenerationCallbacks()
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
JS_AddInterruptCallback(m_ScriptInterface.GetGeneralJSContext(), nullptr);
|
|
|
|
m_ScriptInterface.SetCallbackData(nullptr);
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
private:
|
2019-07-13 23:41:09 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// These functions are called by JS.
|
2019-07-13 23:41:09 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Load all scripts of the given library
|
|
|
|
*
|
|
|
|
* @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 LoadLibrary(const VfsPath& libraryName)
|
|
|
|
{
|
|
|
|
// Ignore libraries that are already loaded
|
|
|
|
if (m_LoadedLibraries.find(libraryName) != m_LoadedLibraries.end())
|
|
|
|
return true;
|
2020-01-29 01:30:07 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Mark this as loaded, to prevent it recursively loading itself
|
|
|
|
m_LoadedLibraries.insert(libraryName);
|
2021-03-02 21:01:14 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
VfsPath path = VfsPath(L"maps/random/") / libraryName / VfsPath();
|
|
|
|
VfsPaths pathnames;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Load all scripts in mapgen directory
|
|
|
|
Status ret = vfs::GetPathnames(g_VFS, path, L"*.js", pathnames);
|
|
|
|
if (ret == INFO::OK)
|
|
|
|
{
|
|
|
|
for (const VfsPath& p : pathnames)
|
|
|
|
{
|
|
|
|
LOGMESSAGE("Loading map generator script '%s'", p.string8());
|
|
|
|
|
|
|
|
if (!m_ScriptInterface.LoadGlobalScriptFile(p))
|
|
|
|
{
|
|
|
|
LOGERROR("CMapGenerationCallbacks::LoadScripts: Failed to load script '%s'",
|
|
|
|
p.string8());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Some error reading directory
|
|
|
|
wchar_t error[200];
|
|
|
|
LOGERROR(
|
|
|
|
"CMapGenerationCallbacks::LoadScripts: Error reading scripts in directory '%s': %s",
|
|
|
|
path.string8(),
|
|
|
|
utf8_from_wstring(StatusDescription(ret, error, ARRAY_SIZE(error))));
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-15 16:44:53 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Finalize map generation and pass results from the script to the engine.
|
|
|
|
* The `data` has to be according to this format:
|
|
|
|
* https://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
|
|
|
|
*/
|
|
|
|
void ExportMap(JS::HandleValue data)
|
2023-06-18 14:19:26 +02:00
|
|
|
{
|
|
|
|
// Copy results
|
|
|
|
m_MapData = Script::WriteStructuredClone(ScriptRequest(m_ScriptInterface), data);
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Load an image file and return it as a height array.
|
|
|
|
*/
|
|
|
|
JS::Value LoadHeightmapImage(const VfsPath& filename)
|
|
|
|
{
|
|
|
|
std::vector<u16> heightmap;
|
|
|
|
if (LoadHeightmapImageVfs(filename, heightmap) != INFO::OK)
|
|
|
|
{
|
|
|
|
LOGERROR("Could not load heightmap file '%s'", filename.string8());
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
JS::RootedValue returnValue(rq.cx);
|
|
|
|
Script::ToJSVal(rq, &returnValue, heightmap);
|
|
|
|
return returnValue;
|
|
|
|
}
|
2014-06-07 14:12:53 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Load an Atlas terrain file (PMP) returning textures and heightmap.
|
|
|
|
*
|
|
|
|
* See CMapReader::UnpackTerrain, CMapReader::ParseTerrain for the reordering
|
|
|
|
*/
|
|
|
|
JS::Value LoadMapTerrain(const VfsPath& filename)
|
|
|
|
{
|
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2015-06-21 22:22:34 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
if (!VfsFileExists(filename))
|
|
|
|
{
|
|
|
|
ScriptException::Raise(rq, "Terrain file \"%s\" does not exist!",
|
|
|
|
filename.string8().c_str());
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2014-06-07 14:12:53 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
CFileUnpacker unpacker;
|
|
|
|
unpacker.Read(filename, "PSMP");
|
2014-06-07 14:12:53 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
if (unpacker.GetVersion() < CMapIO::FILE_READ_VERSION)
|
|
|
|
{
|
|
|
|
ScriptException::Raise(rq, "Could not load terrain file \"%s\" too old version!",
|
|
|
|
filename.string8().c_str());
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// unpack size
|
|
|
|
ssize_t patchesPerSide = (ssize_t)unpacker.UnpackSize();
|
|
|
|
size_t verticesPerSide = patchesPerSide * PATCH_SIZE + 1;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// unpack heightmap
|
|
|
|
std::vector<u16> heightmap;
|
|
|
|
heightmap.resize(SQR(verticesPerSide));
|
|
|
|
unpacker.UnpackRaw(&heightmap[0], SQR(verticesPerSide) * sizeof(u16));
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// unpack texture names
|
|
|
|
size_t textureCount = unpacker.UnpackSize();
|
|
|
|
std::vector<std::string> textureNames;
|
|
|
|
textureNames.reserve(textureCount);
|
|
|
|
for (size_t i = 0; i < textureCount; ++i)
|
2011-03-22 02:34:45 +01:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
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());
|
2011-05-04 23:24:25 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// 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)
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
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);
|
2011-04-11 00:21:15 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
2023-11-19 20:19:32 +01:00
|
|
|
|
|
|
|
JS::RootedValue returnValue(rq.cx);
|
|
|
|
|
|
|
|
Script::CreateObject(
|
|
|
|
rq,
|
|
|
|
&returnValue,
|
|
|
|
"height", heightmap,
|
|
|
|
"textureNames", textureNames,
|
|
|
|
"textureIDs", textureIDs);
|
|
|
|
|
|
|
|
return returnValue;
|
2011-03-22 02:34:45 +01:00
|
|
|
}
|
2023-11-19 20:19:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the map generation progress, which is one of multiple stages
|
|
|
|
* determining the loading screen progress.
|
|
|
|
*/
|
|
|
|
void SetProgress(int progress)
|
2011-04-11 00:21:15 +02:00
|
|
|
{
|
2023-11-19 20:19:32 +01: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);
|
|
|
|
else
|
|
|
|
LOGWARNING("The random map script tried to reduce the loading progress from %d to %d",
|
|
|
|
currentProgress, progress);
|
2011-04-11 00:21:15 +02:00
|
|
|
}
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Microseconds since the epoch.
|
|
|
|
*/
|
|
|
|
double GetMicroseconds() const
|
2018-02-05 17:02:00 +01:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
return JS_Now();
|
2018-02-05 17:02:00 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Return the template data of the given template name.
|
|
|
|
*/
|
|
|
|
CParamNode GetTemplate(const std::string& templateName)
|
|
|
|
{
|
|
|
|
const CParamNode& templateRoot =
|
|
|
|
m_TemplateLoader.GetTemplateFileData(templateName).GetOnlyChild();
|
|
|
|
if (!templateRoot.IsOk())
|
|
|
|
LOGERROR("Invalid template found for '%s'", templateName.c_str());
|
2018-02-05 17:02:00 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
return templateRoot;
|
|
|
|
}
|
2019-07-21 03:35:39 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Check whether the given template exists.
|
|
|
|
*/
|
|
|
|
bool TemplateExists(const std::string& templateName) const
|
2019-07-21 03:35:39 +02:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
return m_TemplateLoader.TemplateExists(templateName);
|
2019-07-21 03:35:39 +02:00
|
|
|
}
|
2018-02-07 00:05:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Returns all template names of simulation entity templates.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> FindTemplates(const std::string& path, bool includeSubdirectories)
|
2019-07-21 03:35:39 +02:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
return m_TemplateLoader.FindTemplates(path, includeSubdirectories, SIMULATION_TEMPLATES);
|
2019-07-21 03:35:39 +02:00
|
|
|
}
|
2018-02-07 00:05:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Returns all template names of actors.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> FindActorTemplates(const std::string& path, bool includeSubdirectories)
|
2018-02-07 00:05:58 +01:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
return m_TemplateLoader.FindTemplates(path, includeSubdirectories, ACTOR_TEMPLATES);
|
2018-02-07 00:05:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
/**
|
|
|
|
* Current map generation progress.
|
|
|
|
*/
|
|
|
|
std::atomic<int>& m_Progress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides the script context.
|
|
|
|
*/
|
|
|
|
ScriptInterface& m_ScriptInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Result of the mapscript generation including terrain, entities and environment settings.
|
|
|
|
*/
|
|
|
|
Script::StructuredClone& m_MapData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently loaded script librarynames.
|
|
|
|
*/
|
|
|
|
std::set<VfsPath> m_LoadedLibraries;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend to loading template data.
|
|
|
|
*/
|
|
|
|
CTemplateLoader m_TemplateLoader;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
Script::StructuredClone RunMapGenerationScript(std::atomic<int>& progress, ScriptInterface& scriptInterface,
|
|
|
|
const VfsPath& script, const std::string& settings, const u16 flags)
|
|
|
|
{
|
|
|
|
ScriptRequest rq(scriptInterface);
|
2018-02-07 00:05:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Parse settings
|
|
|
|
JS::RootedValue settingsVal(rq.cx);
|
|
|
|
if (!Script::ParseJSON(rq, settings, &settingsVal) && settingsVal.isUndefined())
|
2018-02-07 00:05:58 +01:00
|
|
|
{
|
2023-11-19 20:19:32 +01:00
|
|
|
LOGERROR("RunMapGenerationScript: Failed to parse settings");
|
|
|
|
return nullptr;
|
2018-02-07 00:05:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Prevent unintentional modifications to the settings object by random map scripts
|
|
|
|
if (!Script::FreezeObject(rq, settingsVal, true))
|
|
|
|
{
|
|
|
|
LOGERROR("RunMapGenerationScript: Failed to deepfreeze settings");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-02-07 00:05:58 +01:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Init RNG seed
|
|
|
|
u32 seed = 0;
|
|
|
|
if (!Script::HasProperty(rq, settingsVal, "Seed") ||
|
|
|
|
!Script::GetProperty(rq, settingsVal, "Seed", seed))
|
|
|
|
LOGWARNING("RunMapGenerationScript: No seed value specified - using 0");
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
boost::rand48 mapGenRNG{seed};
|
|
|
|
scriptInterface.ReplaceNondeterministicRNG(mapGenRNG);
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
Script::StructuredClone mapData;
|
|
|
|
CMapGenerationCallbacks callbackData{progress, scriptInterface, mapData, flags};
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Copy settings to global variable
|
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
|
|
|
if (!Script::SetProperty(rq, global, "g_MapSettings", settingsVal, flags & JSPROP_READONLY,
|
|
|
|
flags & JSPROP_ENUMERATE))
|
|
|
|
{
|
|
|
|
LOGERROR("RunMapGenerationScript: Failed to define g_MapSettings");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
// Load RMS
|
|
|
|
LOGMESSAGE("Loading RMS '%s'", script.string8());
|
|
|
|
if (!scriptInterface.LoadGlobalScriptFile(script))
|
|
|
|
{
|
|
|
|
LOGERROR("RunMapGenerationScript: Failed to load RMS '%s'", script.string8());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2023-11-19 20:19:32 +01:00
|
|
|
return mapData;
|
2011-04-10 07:31:18 +02:00
|
|
|
}
|