2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMapInfo.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-11-21 12:13:33 +03:00
|
|
|
#include "StdInc.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "CMapInfo.h"
|
|
|
|
|
2017-06-04 21:42:48 +02:00
|
|
|
#include "../filesystem/ResourceID.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "../StartInfo.h"
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "CMapService.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "CMapHeader.h"
|
2023-05-24 00:14:06 +02:00
|
|
|
#include "MapFormat.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../campaign/CampaignHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../filesystem/Filesystem.h"
|
|
|
|
#include "../serializer/CMemorySerializer.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#include "../rmg/CMapGenOptions.h"
|
|
|
|
#include "../CCreatureHandler.h"
|
2023-05-20 16:34:39 +02:00
|
|
|
#include "../GameSettings.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../CHeroHandler.h"
|
|
|
|
#include "../CModHandler.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CMapInfo::CMapInfo()
|
|
|
|
: scenarioOptionsOfSave(nullptr), amountOfPlayersOnMap(0), amountOfHumanControllablePlayers(0), amountOfHumanPlayersInSave(0), isRandomMap(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CMapInfo::~CMapInfo()
|
|
|
|
{
|
|
|
|
vstd::clear_pointer(scenarioOptionsOfSave);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapInfo::mapInit(const std::string & fname)
|
|
|
|
{
|
|
|
|
fileURI = fname;
|
|
|
|
CMapService mapService;
|
|
|
|
mapHeader = mapService.loadMapHeader(ResourceID(fname, EResType::MAP));
|
|
|
|
countPlayers();
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
void CMapInfo::saveInit(const ResourceID & file)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
CLoadFile lf(*CResourceHandler::get()->getResourceName(file), MINIMAL_SERIALIZATION_VERSION);
|
|
|
|
lf.checkMagicBytes(SAVEGAME_MAGIC);
|
|
|
|
|
2022-12-07 23:36:20 +02:00
|
|
|
mapHeader = std::make_unique<CMapHeader>();
|
2023-02-11 18:30:06 +02:00
|
|
|
lf >> *(mapHeader) >> scenarioOptionsOfSave;
|
2018-01-05 19:21:07 +02:00
|
|
|
fileURI = file.getName();
|
|
|
|
countPlayers();
|
|
|
|
std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(file));
|
|
|
|
date = std::asctime(std::localtime(&time));
|
|
|
|
// We absolutely not need this data for lobby and server will read it from save
|
|
|
|
// FIXME: actually we don't want them in CMapHeader!
|
|
|
|
mapHeader->triggeredEvents.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMapInfo::campaignInit()
|
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
campaign = CampaignHandler::getHeader(fileURI);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
void CMapInfo::countPlayers()
|
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
for(int i=0; i<PlayerColor::PLAYER_LIMIT_I; i++)
|
2012-11-11 15:23:31 +03:00
|
|
|
{
|
|
|
|
if(mapHeader->players[i].canHumanPlay)
|
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
amountOfPlayersOnMap++;
|
|
|
|
amountOfHumanControllablePlayers++;
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
|
|
|
else if(mapHeader->players[i].canComputerPlay)
|
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
amountOfPlayersOnMap++;
|
2012-11-11 15:23:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
if(scenarioOptionsOfSave)
|
2023-02-11 18:30:06 +02:00
|
|
|
for(const auto & playerInfo : scenarioOptionsOfSave->playerInfos)
|
|
|
|
if(playerInfo.second.isControlledByHuman())
|
2018-01-05 19:21:07 +02:00
|
|
|
amountOfHumanPlayersInSave++;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string CMapInfo::getName() const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
if(campaign && !campaign->getName().empty())
|
|
|
|
return campaign->getName();
|
2018-01-05 19:21:07 +02:00
|
|
|
else if(mapHeader && mapHeader->name.length())
|
|
|
|
return mapHeader->name;
|
|
|
|
else
|
|
|
|
return VLC->generaltexth->allTexts[508];
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string CMapInfo::getNameForList() const
|
2013-07-02 18:23:32 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
if(scenarioOptionsOfSave)
|
|
|
|
{
|
|
|
|
// TODO: this could be handled differently
|
|
|
|
std::vector<std::string> path;
|
|
|
|
boost::split(path, fileURI, boost::is_any_of("\\/"));
|
|
|
|
return path[path.size()-1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return getName();
|
|
|
|
}
|
2013-07-02 18:23:32 +03:00
|
|
|
}
|
2013-07-02 15:08:30 +03:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string CMapInfo::getDescription() const
|
2016-08-31 05:18:01 +02:00
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
if(campaign)
|
|
|
|
return campaign->getDescription();
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
|
|
|
return mapHeader->description;
|
2016-08-31 05:18:01 +02:00
|
|
|
}
|
2013-07-02 15:08:30 +03:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
int CMapInfo::getMapSizeIconId() const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
if(!mapHeader)
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
switch(mapHeader->width)
|
|
|
|
{
|
|
|
|
case CMapHeader::MAP_SIZE_SMALL:
|
|
|
|
return 0;
|
|
|
|
case CMapHeader::MAP_SIZE_MIDDLE:
|
|
|
|
return 1;
|
|
|
|
case CMapHeader::MAP_SIZE_LARGE:
|
|
|
|
return 2;
|
|
|
|
case CMapHeader::MAP_SIZE_XLARGE:
|
|
|
|
return 3;
|
2022-12-12 02:46:42 +02:00
|
|
|
case CMapHeader::MAP_SIZE_HUGE:
|
|
|
|
return 4;
|
|
|
|
case CMapHeader::MAP_SIZE_XHUGE:
|
|
|
|
return 5;
|
|
|
|
case CMapHeader::MAP_SIZE_GIANT:
|
|
|
|
return 6;
|
2018-01-05 19:21:07 +02:00
|
|
|
default:
|
|
|
|
return 4;
|
|
|
|
}
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-20 16:34:39 +02:00
|
|
|
int CMapInfo::getMapSizeFormatIconId() const
|
2012-11-03 16:30:47 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
switch(mapHeader->version)
|
|
|
|
{
|
2023-05-20 16:34:39 +02:00
|
|
|
case EMapFormat::ROE:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["iconIndex"].Integer();
|
|
|
|
case EMapFormat::AB:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["iconIndex"].Integer();
|
|
|
|
case EMapFormat::SOD:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["iconIndex"].Integer();
|
|
|
|
case EMapFormat::WOG:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["iconIndex"].Integer();
|
|
|
|
case EMapFormat::HOTA:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["iconIndex"].Integer();
|
|
|
|
case EMapFormat::VCMI:
|
|
|
|
return VLC->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["iconIndex"].Integer();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-05-20 16:34:39 +02:00
|
|
|
return 0;
|
2012-11-03 16:30:47 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string CMapInfo::getMapSizeName() const
|
2013-07-02 18:23:32 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
switch(mapHeader->width)
|
|
|
|
{
|
|
|
|
case CMapHeader::MAP_SIZE_SMALL:
|
|
|
|
return "S";
|
|
|
|
case CMapHeader::MAP_SIZE_MIDDLE:
|
|
|
|
return "M";
|
|
|
|
case CMapHeader::MAP_SIZE_LARGE:
|
|
|
|
return "L";
|
|
|
|
case CMapHeader::MAP_SIZE_XLARGE:
|
|
|
|
return "XL";
|
2022-12-12 02:46:42 +02:00
|
|
|
case CMapHeader::MAP_SIZE_HUGE:
|
|
|
|
return "H";
|
|
|
|
case CMapHeader::MAP_SIZE_XHUGE:
|
|
|
|
return "XH";
|
|
|
|
case CMapHeader::MAP_SIZE_GIANT:
|
|
|
|
return "G";
|
2018-01-05 19:21:07 +02:00
|
|
|
default:
|
|
|
|
return "C";
|
|
|
|
}
|
2013-07-02 18:23:32 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|