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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_MAPGENERATOR
|
|
|
|
#define INCLUDED_MAPGENERATOR
|
|
|
|
|
|
|
|
#include "ps/FileIo.h"
|
2011-04-10 07:31:18 +02:00
|
|
|
#include "ps/ThreadUtil.h"
|
2011-03-22 02:34:45 +01:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
2011-03-23 09:12:04 +01:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-05-15 21:51:33 +02:00
|
|
|
#include <set>
|
2011-04-10 07:31:18 +02:00
|
|
|
|
|
|
|
class CMapGeneratorWorker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Random map generator interface. Initialized by CMapReader and then checked
|
|
|
|
* periodically during loading, until it's finished (progress value is 0).
|
|
|
|
*
|
|
|
|
* The actual work is performed by CMapGeneratorWorker in a separate thread.
|
|
|
|
*/
|
2011-03-22 02:34:45 +01:00
|
|
|
class CMapGenerator
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
CMapGenerator();
|
2011-04-10 07:31:18 +02:00
|
|
|
~CMapGenerator();
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
/**
|
|
|
|
* Start the map generator thread
|
|
|
|
*
|
|
|
|
* @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
|
|
|
|
* @param settings JSON string containing settings for the map generator
|
|
|
|
*/
|
|
|
|
void GenerateMap(const VfsPath& scriptFile, const std::string& settings);
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
/**
|
|
|
|
* Get status of the map generator thread
|
|
|
|
*
|
2011-04-15 07:23:51 +02:00
|
|
|
* @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
|
2011-04-10 07:31:18 +02:00
|
|
|
*/
|
|
|
|
int GetProgress();
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
/**
|
|
|
|
* Get random map data, according to this format:
|
|
|
|
* http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
|
|
|
|
*
|
|
|
|
* @return StructuredClone containing map data
|
|
|
|
*/
|
|
|
|
shared_ptr<ScriptInterface::StructuredClone> GetResults();
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
private:
|
|
|
|
CMapGeneratorWorker* m_Worker;
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Random map generator worker thread.
|
|
|
|
* (This is run in a thread so that the GUI remains responsive while loading)
|
|
|
|
*
|
|
|
|
* Thread-safety:
|
|
|
|
* - Initialize and constructor/destructor must be called from the main thread.
|
|
|
|
* - ScriptInterface created and destroyed by thread
|
|
|
|
* - StructuredClone used to return JS map data - jsvals can't be used across threads/runtimes
|
|
|
|
*/
|
|
|
|
class CMapGeneratorWorker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CMapGeneratorWorker();
|
|
|
|
~CMapGeneratorWorker();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the map generator thread
|
|
|
|
*
|
|
|
|
* @param scriptFile The VFS path for the script, e.g. "maps/random/latium.js"
|
|
|
|
* @param settings JSON string containing settings for the map generator
|
|
|
|
*/
|
|
|
|
void Initialize(const VfsPath& scriptFile, const std::string& settings);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get status of the map generator thread
|
|
|
|
*
|
2011-05-04 23:24:25 +02:00
|
|
|
* @return Progress percentage 1-100 if active, 0 when finished, or -1 on error
|
2011-04-10 07:31:18 +02:00
|
|
|
*/
|
|
|
|
int GetProgress();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get random map data, according to this format:
|
|
|
|
* http://trac.wildfiregames.com/wiki/Random_Map_Generator_Internals#Dataformat
|
|
|
|
*
|
|
|
|
* @return StructuredClone containing map data
|
|
|
|
*/
|
|
|
|
shared_ptr<ScriptInterface::StructuredClone> GetResults();
|
2011-03-22 02:34:45 +01:00
|
|
|
|
|
|
|
private:
|
2011-04-10 07:31:18 +02:00
|
|
|
// Mapgen
|
2011-03-22 02:34:45 +01:00
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
/**
|
|
|
|
* Load all scripts of the given library
|
|
|
|
*
|
|
|
|
* @param libraryName String specifying name of the library (subfolder of ../maps/random/)
|
2011-04-15 07:23:51 +02:00
|
|
|
* @return true if all scripts ran successfully, false if there's an error
|
2011-04-10 07:31:18 +02:00
|
|
|
*/
|
2011-03-22 02:34:45 +01:00
|
|
|
bool LoadScripts(const std::wstring& libraryName);
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
// callbacks for script functions
|
|
|
|
static bool LoadLibrary(void* cbdata, std::wstring name);
|
|
|
|
static void ExportMap(void* cbdata, CScriptValRooted data);
|
|
|
|
static void SetProgress(void* cbdata, int progress);
|
|
|
|
static void MaybeGC(void* cbdata);
|
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
std::set<std::wstring> m_LoadedLibraries;
|
2011-04-10 07:31:18 +02:00
|
|
|
shared_ptr<ScriptInterface::StructuredClone> m_MapData;
|
2011-03-23 09:12:04 +01:00
|
|
|
boost::rand48 m_MapGenRNG;
|
2011-04-10 07:31:18 +02:00
|
|
|
int m_Progress;
|
2011-04-15 07:23:51 +02:00
|
|
|
ScriptInterface* m_ScriptInterface;
|
2011-05-04 23:24:25 +02:00
|
|
|
VfsPath m_ScriptPath;
|
2011-04-10 07:31:18 +02:00
|
|
|
std::string m_Settings;
|
|
|
|
|
|
|
|
// Thread
|
|
|
|
static void* RunThread(void* data);
|
|
|
|
bool Run();
|
|
|
|
|
|
|
|
pthread_t m_WorkerThread;
|
|
|
|
CMutex m_WorkerMutex;
|
2011-03-22 02:34:45 +01:00
|
|
|
};
|
|
|
|
|
2011-04-10 07:31:18 +02:00
|
|
|
|
2011-03-22 02:34:45 +01:00
|
|
|
#endif //INCLUDED_MAPGENERATOR
|