0ad/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp

369 lines
8.7 KiB
C++
Raw Normal View History

/* Copyright (C) 2019 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 "MessageHandler.h"
#include "../GameLoop.h"
#include "../CommandProc.h"
#include "graphics/GameView.h"
#include "graphics/LOSTexture.h"
#include "graphics/MapIO.h"
#include "graphics/MapWriter.h"
#include "graphics/Patch.h"
#include "graphics/Terrain.h"
#include "graphics/TerrainTextureEntry.h"
#include "graphics/TerrainTextureManager.h"
#include "lib/bits.h"
#include "lib/file/vfs/vfs_path.h"
#include "lib/status.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/Game.h"
#include "ps/Loader.h"
#include "ps/World.h"
#include "renderer/Renderer.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
#include "simulation2/components/ICmpPlayer.h"
#include "simulation2/components/ICmpPlayerManager.h"
#include "simulation2/components/ICmpPosition.h"
#include "simulation2/components/ICmpRangeManager.h"
#include "simulation2/components/ICmpTerrain.h"
namespace
{
void InitGame()
{
if (g_Game)
{
delete g_Game;
g_Game = NULL;
}
Remove default CGame constructor values to make the code less error-prone, use CRenderer::IsInitialised() to test if the CGame should be rendered to remove indirection/proxies, making the code easier to read. 1c0536bf08 introduced a disableGraphics bool with a default value and relied on the default being reasonable except for the few needed cases. be93b31411 introduced the replayLog argument with a default value and relied on the default being reasonable except for the few needed cases. 5747619c39 fixed a bug in that commit because the default value hadn't actually been considered to be correct for all CGame constructor calls and was wrong for two. By requiring callers to specify the value, authors are forced to establish thought which value is the correct one, as opposed to hoping that a default value will be good by default. As you can see in the diff, it also makes it easier to compare what values changed if they are always defined in the caller. Use CRenderer::IsInitialised() to determine if this is a non-visual CGame, for the purpose of removing less transparent proxy functions that are unneeded as long as there are about 30 other calls testing for CRenderer::IsInitialised() to determine if the Game should be rendered. Supersedes: * CGame constructor argument bool disableGraphics from 1c0536bf08. * CGame::IsGraphicsDisabled() proxy from a533fff883 to the proxy from 1c0536bf08 and two local nonVisual = args.Has("autostart-nonvisual") variables in GameSetup.cpp from a533fff883. Call the Renderer destructor instead of calling delete on the non-pointer (SAFE_DELETE would not be supported for instance). Started as a preparation for D2197, but actually independent. Differential Revision: https://code.wildfiregames.com/D2211 This was SVN commit r22785.
2019-08-25 04:02:55 -07:00
g_Game = new CGame(false);
// Default to player 1 for playtesting
g_Game->SetPlayerID(1);
}
void StartGame(JS::MutableHandleValue attrs)
{
g_Game->StartGame(attrs, "");
// TODO: Non progressive load can fail - need a decent way to handle this
LDR_NonprogressiveLoad();
// Disable fog-of-war - this must be done before starting the game,
// as visual actors cache their visibility state on first render.
CmpPtr<ICmpRangeManager> cmpRangeManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
if (cmpRangeManager)
cmpRangeManager->SetLosRevealAll(-1, true);
PSRETURN ret = g_Game->ReallyStartGame();
ENSURE(ret == PSRETURN_OK);
}
}
namespace AtlasMessage {
QUERYHANDLER(GenerateMap)
{
try
{
InitGame();
// Random map
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface();
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
JS::RootedValue settings(cx);
scriptInterface.ParseJSON(*msg->settings, &settings);
scriptInterface.SetProperty(settings, "mapType", "random");
JS::RootedValue attrs(cx);
ScriptInterface::CreateObject(
cx,
&attrs,
"mapType", "random",
"script", *msg->filename,
"settings", settings);
StartGame(&attrs);
msg->status = 0;
}
catch (PSERROR_Game_World_MapLoadFailed&)
{
// Cancel loading
LDR_Cancel();
// Since map generation failed and we don't know why, use the blank map as a fallback
InitGame();
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface();
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
// Set up 8-element array of empty objects to satisfy init
JS::RootedValue playerData(cx);
ScriptInterface::CreateArray(cx, &playerData);
for (int i = 0; i < 8; ++i)
{
JS::RootedValue player(cx);
ScriptInterface::CreateObject(cx, &player);
scriptInterface.SetPropertyInt(playerData, i, player);
}
JS::RootedValue settings(cx);
ScriptInterface::CreateObject(
cx,
&settings,
"mapType", "scenario",
"PlayerData", playerData);
JS::RootedValue attrs(cx);
ScriptInterface::CreateObject(
cx,
&attrs,
"mapType", "scenario",
"map", "maps/scenarios/_default",
"settings", settings);
StartGame(&attrs);
msg->status = -1;
}
}
MESSAGEHANDLER(LoadMap)
{
InitGame();
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface();
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
// Scenario
CStrW map = *msg->filename;
CStrW mapBase = map.BeforeLast(L".pmp"); // strip the file extension, if any
JS::RootedValue attrs(cx);
ScriptInterface::CreateObject(
cx,
&attrs,
"mapType", "scenario",
"map", mapBase);
StartGame(&attrs);
}
MESSAGEHANDLER(ImportHeightmap)
{
std::vector<u16> heightmap_source;
if (LoadHeightmapImageOs(*msg->filename, heightmap_source) != INFO::OK)
{
LOGERROR("Failed to decode heightmap.");
return;
}
// resize terrain to heightmap size
// Notice that the number of tiles/pixels per side of the heightmap image is
// one less than the number of vertices per side of the heightmap.
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
terrain->Resize((sqrt(heightmap_source.size()) - 1) / PATCH_SIZE);
// copy heightmap data into map
u16* heightmap = g_Game->GetWorld()->GetTerrain()->GetHeightMap();
ENSURE(heightmap_source.size() == (std::size_t) SQR(g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide()));
std::copy(heightmap_source.begin(), heightmap_source.end(), heightmap);
// update simulation
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
if (cmpTerrain)
cmpTerrain->ReloadTerrain();
g_Game->GetView()->GetLOSTexture().MakeDirty();
}
MESSAGEHANDLER(SaveMap)
{
CMapWriter writer;
VfsPath pathname = VfsPath(*msg->filename).ChangeExtension(L".pmp");
writer.SaveMap(pathname,
g_Game->GetWorld()->GetTerrain(),
g_Renderer.GetWaterManager(), g_Renderer.GetSkyManager(),
&g_LightEnv, g_Game->GetView()->GetCamera(), g_Game->GetView()->GetCinema(),
&g_Renderer.GetPostprocManager(),
g_Game->GetSimulation2());
}
QUERYHANDLER(GetMapSettings)
{
msg->settings = g_Game->GetSimulation2()->GetMapSettingsString();
}
2011-06-09 16:47:20 -07:00
BEGIN_COMMAND(SetMapSettings)
{
std::string m_OldSettings, m_NewSettings;
void SetSettings(const std::string& settings)
{
g_Game->GetSimulation2()->SetMapSettings(settings);
}
2011-06-09 16:47:20 -07:00
void Do()
{
m_OldSettings = g_Game->GetSimulation2()->GetMapSettingsString();
m_NewSettings = *msg->settings;
SetSettings(m_NewSettings);
2011-06-09 16:47:20 -07:00
}
// TODO: we need some way to notify the Atlas UI when the settings are changed
// externally, otherwise this will have no visible effect
2011-06-09 16:47:20 -07:00
void Undo()
{
// SetSettings(m_OldSettings);
2011-06-09 16:47:20 -07:00
}
void Redo()
{
// SetSettings(m_NewSettings);
}
void MergeIntoPrevious(cSetMapSettings* prev)
{
prev->m_NewSettings = m_NewSettings;
2011-06-09 16:47:20 -07:00
}
};
END_COMMAND(SetMapSettings)
MESSAGEHANDLER(LoadPlayerSettings)
{
g_Game->GetSimulation2()->LoadPlayerSettings(msg->newplayers);
}
QUERYHANDLER(GetMapSizes)
{
msg->sizes = g_Game->GetSimulation2()->GetMapSizes();
}
QUERYHANDLER(GetRMSData)
{
msg->data = g_Game->GetSimulation2()->GetRMSData();
}
BEGIN_COMMAND(ResizeMap)
{
int m_OldTiles, m_NewTiles;
cResizeMap()
{
}
void MakeDirty()
{
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
if (cmpTerrain)
cmpTerrain->ReloadTerrain();
// The LOS texture won't normally get updated when running Atlas
// (since there's no simulation updates), so explicitly dirty it
g_Game->GetView()->GetLOSTexture().MakeDirty();
}
void ResizeTerrain(int tiles)
{
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
terrain->Resize(tiles / PATCH_SIZE);
MakeDirty();
}
void Do()
{
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
if (!cmpTerrain)
{
m_OldTiles = m_NewTiles = 0;
}
else
{
m_OldTiles = (int)cmpTerrain->GetTilesPerSide();
m_NewTiles = msg->tiles;
}
ResizeTerrain(m_NewTiles);
}
void Undo()
{
ResizeTerrain(m_OldTiles);
}
void Redo()
{
ResizeTerrain(m_NewTiles);
}
};
END_COMMAND(ResizeMap)
QUERYHANDLER(VFSFileExists)
{
msg->exists = VfsFileExists(*msg->path);
}
QUERYHANDLER(VFSFileRealPath)
{
VfsPath pathname(*msg->path);
if (pathname.empty())
return;
OsPath realPathname;
if (g_VFS->GetRealPath(pathname, realPathname) == INFO::OK)
msg->realPath = realPathname.string();
}
static Status AddToFilenames(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData)
{
std::vector<std::wstring>& filenames = *(std::vector<std::wstring>*)cbData;
filenames.push_back(pathname.string().c_str());
return INFO::OK;
}
QUERYHANDLER(GetMapList)
{
#define GET_FILE_LIST(path, list) \
std::vector<std::wstring> list; \
vfs::ForEachFile(g_VFS, path, AddToFilenames, (uintptr_t)&list, L"*.xml", vfs::DIR_RECURSIVE); \
msg->list = list;
GET_FILE_LIST(L"maps/scenarios/", scenarioFilenames);
GET_FILE_LIST(L"maps/skirmishes/", skirmishFilenames);
GET_FILE_LIST(L"maps/tutorials/", tutorialFilenames);
#undef GET_FILE_LIST
}
}