2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* RandomMapTab.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "RandomMapTab.h"
|
|
|
|
#include "CSelectionBase.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CServerHandler.h"
|
|
|
|
#include "../gui/CAnimation.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/mapping/CMapInfo.h"
|
|
|
|
#include "../../lib/rmg/CMapGenOptions.h"
|
2022-12-12 01:27:59 +02:00
|
|
|
#include "../../lib/CModHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-12-12 01:27:59 +02:00
|
|
|
RandomMapTab::RandomMapTab():
|
|
|
|
InterfaceBuilder()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
recActions = 0;
|
|
|
|
mapGenOptions = std::make_shared<CMapGenOptions>();
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
const JsonNode config(ResourceID("config/windows.json"));
|
|
|
|
addCallback("toggleMapSize", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
auto mapSizeVal = getPossibleMapSizes();
|
|
|
|
mapGenOptions->setWidth(mapSizeVal[btnId]);
|
|
|
|
mapGenOptions->setHeight(mapSizeVal[btnId]);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
addCallback("toggleTwoLevels", [&](bool on)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setHasTwoLevels(on);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setPlayersCount", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setPlayerCount(btnId);
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams")))
|
|
|
|
deactivateButtonsFrom(w.get(), btnId);
|
2020-07-07 13:45:53 +02:00
|
|
|
|
|
|
|
// deactive some CompOnlyPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers")))
|
|
|
|
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
|
2020-07-07 13:45:53 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
validatePlayersCnt(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setTeamsCount", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setTeamCount(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setCompOnlyPlayers", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setCompOnlyPlayerCount(btnId);
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2022-11-18 02:05:35 +02:00
|
|
|
// deactive some MaxPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers")))
|
|
|
|
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
|
2022-11-18 02:05:35 +02:00
|
|
|
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams")))
|
|
|
|
deactivateButtonsFrom(w.get(), (btnId == 0 ? 1 : btnId));
|
2018-01-05 19:21:07 +02:00
|
|
|
validateCompOnlyPlayersCnt(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setCompOnlyTeams", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setCompOnlyTeamCount(btnId);
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setWaterContent", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setWaterContent(static_cast<EWaterContent::EWaterContent>(btnId));
|
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setMonsterStrenght", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(btnId < 0)
|
|
|
|
mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
|
|
|
|
else
|
2018-08-24 13:37:05 +02:00
|
|
|
mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
init(config["randomMapTab"]);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::updateMapInfoByHost()
|
|
|
|
{
|
|
|
|
if(CSH->isGuest())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate header info
|
|
|
|
mapInfo = std::make_shared<CMapInfo>();
|
|
|
|
mapInfo->isRandomMap = true;
|
|
|
|
mapInfo->mapHeader = make_unique<CMapHeader>();
|
|
|
|
mapInfo->mapHeader->version = EMapFormat::SOD;
|
|
|
|
mapInfo->mapHeader->name = CGI->generaltexth->allTexts[740];
|
|
|
|
mapInfo->mapHeader->description = CGI->generaltexth->allTexts[741];
|
|
|
|
mapInfo->mapHeader->difficulty = 1; // Normal
|
|
|
|
mapInfo->mapHeader->height = mapGenOptions->getHeight();
|
|
|
|
mapInfo->mapHeader->width = mapGenOptions->getWidth();
|
|
|
|
mapInfo->mapHeader->twoLevel = mapGenOptions->getHasTwoLevels();
|
|
|
|
|
|
|
|
// Generate player information
|
|
|
|
mapInfo->mapHeader->players.clear();
|
|
|
|
int playersToGen = PlayerColor::PLAYER_LIMIT_I;
|
|
|
|
if(mapGenOptions->getPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
2020-07-07 13:45:53 +02:00
|
|
|
{
|
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
|
|
|
playersToGen = mapGenOptions->getPlayerCount() + mapGenOptions->getCompOnlyPlayerCount();
|
|
|
|
else
|
|
|
|
playersToGen = mapGenOptions->getPlayerCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapHeader->howManyTeams = playersToGen;
|
|
|
|
|
|
|
|
for(int i = 0; i < playersToGen; ++i)
|
|
|
|
{
|
|
|
|
PlayerInfo player;
|
|
|
|
player.isFactionRandom = true;
|
|
|
|
player.canComputerPlay = true;
|
2020-07-07 13:45:53 +02:00
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE && i >= mapGenOptions->getPlayerCount())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
player.canHumanPlay = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
player.canHumanPlay = true;
|
|
|
|
}
|
|
|
|
player.team = TeamID(i);
|
|
|
|
player.hasMainTown = true;
|
|
|
|
player.generateHeroAtMainTown = true;
|
|
|
|
mapInfo->mapHeader->players.push_back(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
mapInfoChanged(mapInfo, mapGenOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
|
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMapSize")))
|
|
|
|
w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleButton>(widget("buttonTwoLevels")))
|
|
|
|
w->setSelected(opts->getHasTwoLevels());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxPlayers")))
|
|
|
|
w->setSelected(opts->getPlayerCount());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams")))
|
|
|
|
w->setSelected(opts->getTeamCount());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers")))
|
|
|
|
w->setSelected(opts->getCompOnlyPlayerCount());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams")))
|
|
|
|
w->setSelected(opts->getCompOnlyTeamCount());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupWaterContent")))
|
|
|
|
w->setSelected(opts->getWaterContent());
|
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupgroupMonsterStrengthMaxTeams")))
|
|
|
|
w->setSelected(opts->getMonsterStrength());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::deactivateButtonsFrom(CToggleGroup * group, int startId)
|
|
|
|
{
|
|
|
|
logGlobal->debug("Blocking buttons from %d", startId);
|
|
|
|
for(auto toggle : group->buttons)
|
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(startId == CMapGenOptions::RANDOM_SIZE || toggle.first < startId)
|
|
|
|
{
|
|
|
|
button->block(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
button->block(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::validatePlayersCnt(int playersCnt)
|
|
|
|
{
|
|
|
|
if(playersCnt == CMapGenOptions::RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mapGenOptions->getTeamCount() >= playersCnt)
|
|
|
|
{
|
|
|
|
mapGenOptions->setTeamCount(playersCnt - 1);
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupMaxTeams")))
|
|
|
|
w->setSelected(mapGenOptions->getTeamCount());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2020-07-07 13:45:53 +02:00
|
|
|
// total players should not exceed PlayerColor::PLAYER_LIMIT_I (8 in homm3)
|
|
|
|
if(mapGenOptions->getCompOnlyPlayerCount() + playersCnt > PlayerColor::PLAYER_LIMIT_I)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2020-07-07 13:45:53 +02:00
|
|
|
mapGenOptions->setCompOnlyPlayerCount(PlayerColor::PLAYER_LIMIT_I - playersCnt);
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyPlayers")))
|
|
|
|
w->setSelected(mapGenOptions->getCompOnlyPlayerCount());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
validateCompOnlyPlayersCnt(mapGenOptions->getCompOnlyPlayerCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::validateCompOnlyPlayersCnt(int compOnlyPlayersCnt)
|
|
|
|
{
|
|
|
|
if(compOnlyPlayersCnt == CMapGenOptions::RANDOM_SIZE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mapGenOptions->getCompOnlyTeamCount() >= compOnlyPlayersCnt)
|
|
|
|
{
|
|
|
|
int compOnlyTeamCount = compOnlyPlayersCnt == 0 ? 0 : compOnlyPlayersCnt - 1;
|
|
|
|
mapGenOptions->setCompOnlyTeamCount(compOnlyTeamCount);
|
|
|
|
updateMapInfoByHost();
|
2022-12-12 01:58:39 +02:00
|
|
|
if(auto w = dynamic_pointer_cast<CToggleGroup>(widget("groupCompOnlyTeams")))
|
|
|
|
w->setSelected(compOnlyTeamCount);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> RandomMapTab::getPossibleMapSizes()
|
|
|
|
{
|
2022-12-12 02:46:42 +02:00
|
|
|
return {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_GIANT};
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|