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"
|
2023-10-21 02:12:34 +02:00
|
|
|
#include "CLobbyScreen.h"
|
|
|
|
#include "SelectionTab.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CServerHandler.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-05-18 22:31:05 +02:00
|
|
|
#include "../gui/MouseButton.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "../gui/WindowHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/CComponent.h"
|
2023-08-25 12:07:38 +02:00
|
|
|
#include "../widgets/ComboBox.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
2023-05-30 16:31:45 +02:00
|
|
|
#include "../widgets/Slider.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/mapping/CMapInfo.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../../lib/mapping/CMapHeader.h"
|
2023-05-24 00:14:06 +02:00
|
|
|
#include "../../lib/mapping/MapFormat.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../../lib/rmg/CMapGenOptions.h"
|
2022-12-13 02:38:18 +02:00
|
|
|
#include "../../lib/rmg/CRmgTemplateStorage.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "../../lib/RoadHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-12-12 01:27:59 +02:00
|
|
|
RandomMapTab::RandomMapTab():
|
2022-12-12 09:48:39 +02:00
|
|
|
InterfaceObjectConfigurable()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
recActions = 0;
|
|
|
|
mapGenOptions = std::make_shared<CMapGenOptions>();
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("toggleMapSize", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
auto mapSizeVal = getPossibleMapSizes();
|
|
|
|
mapGenOptions->setWidth(mapSizeVal[btnId]);
|
|
|
|
mapGenOptions->setHeight(mapSizeVal[btnId]);
|
2022-12-14 03:23:21 +02:00
|
|
|
if(mapGenOptions->getMapTemplate())
|
|
|
|
if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
|
|
|
|
setTemplate(nullptr);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
addCallback("toggleTwoLevels", [&](bool on)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
mapGenOptions->setHasTwoLevels(on);
|
2022-12-14 03:23:21 +02:00
|
|
|
if(mapGenOptions->getMapTemplate())
|
|
|
|
if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
|
|
|
|
setTemplate(nullptr);
|
2018-01-05 19:21:07 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
});
|
2022-12-12 01:27:59 +02:00
|
|
|
|
|
|
|
addCallback("setPlayersCount", [&](int btnId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
mapGenOptions->setHumanOrCpuPlayerCount(btnId);
|
2022-12-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2018-01-05 19:21:07 +02:00
|
|
|
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-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2018-01-05 19:21:07 +02:00
|
|
|
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
|
|
|
|
2022-12-12 09:38:27 +02:00
|
|
|
addCallback("setMonsterStrength", [&](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
|
|
|
|
2022-12-13 01:47:29 +02:00
|
|
|
//new callbacks available only from mod
|
2022-12-15 22:46:36 +02:00
|
|
|
addCallback("teamAlignments", [&](int)
|
|
|
|
{
|
2023-05-16 15:20:35 +02:00
|
|
|
GH.windows().createAndPushWindow<TeamAlignmentsWidget>(*this);
|
2022-12-15 22:46:36 +02:00
|
|
|
});
|
|
|
|
|
2022-12-20 18:35:40 +02:00
|
|
|
for(auto road : VLC->roadTypeHandler->objects)
|
2022-12-15 22:46:36 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
std::string cbRoadType = "selectRoad_" + road->getJsonKey();
|
2022-12-17 02:54:37 +02:00
|
|
|
addCallback(cbRoadType, [&, road](bool on)
|
2022-12-15 22:46:36 +02:00
|
|
|
{
|
2023-07-08 08:44:10 +02:00
|
|
|
mapGenOptions->setRoadEnabled(road->getId(), on);
|
2022-12-17 02:54:37 +02:00
|
|
|
updateMapInfoByHost();
|
2022-12-15 22:46:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode config(JsonPath::builtin("config/widgets/randomMapTab.json"));
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-12 01:27:59 +02:00
|
|
|
|
2023-10-21 02:12:34 +02:00
|
|
|
if(auto w = widget<CButton>("buttonShowRandomMaps"))
|
|
|
|
{
|
|
|
|
w->addCallback([&]()
|
|
|
|
{
|
|
|
|
(static_cast<CLobbyScreen *>(parent))->toggleTab((static_cast<CLobbyScreen *>(parent))->tabSel);
|
|
|
|
(static_cast<CLobbyScreen *>(parent))->tabSel->showRandom = true;
|
|
|
|
(static_cast<CLobbyScreen *>(parent))->tabSel->filter(0, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 12:07:38 +02:00
|
|
|
//set combo box callbacks
|
|
|
|
if(auto w = widget<ComboBox>("templateList"))
|
|
|
|
{
|
|
|
|
w->onConstructItems = [](std::vector<const void *> & curItems){
|
|
|
|
auto templates = VLC->tplh->getTemplates();
|
|
|
|
|
|
|
|
boost::range::sort(templates, [](const CRmgTemplate * a, const CRmgTemplate * b){
|
|
|
|
return a->getName() < b->getName();
|
|
|
|
});
|
|
|
|
|
|
|
|
curItems.push_back(nullptr); //default template
|
|
|
|
|
|
|
|
for(auto & t : templates)
|
|
|
|
curItems.push_back(t);
|
|
|
|
};
|
|
|
|
|
|
|
|
w->onSetItem = [&](const void * item){
|
|
|
|
this->setTemplate(reinterpret_cast<const CRmgTemplate *>(item));
|
|
|
|
};
|
|
|
|
|
|
|
|
w->getItemText = [this](int idx, const void * item){
|
|
|
|
if(item)
|
|
|
|
return reinterpret_cast<const CRmgTemplate *>(item)->getName();
|
|
|
|
if(idx == 0)
|
|
|
|
return readText(variables["randomTemplate"]);
|
|
|
|
return std::string("");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
2022-12-07 23:36:20 +02:00
|
|
|
mapInfo->mapHeader = std::make_unique<CMapHeader>();
|
2023-05-20 16:34:39 +02:00
|
|
|
mapInfo->mapHeader->version = EMapFormat::VCMI;
|
2023-09-27 22:53:13 +02:00
|
|
|
mapInfo->mapHeader->name.appendLocalString(EMetaText::GENERAL_TXT, 740);
|
|
|
|
mapInfo->mapHeader->description.appendLocalString(EMetaText::GENERAL_TXT, 741);
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapHeader->difficulty = 1; // Normal
|
|
|
|
mapInfo->mapHeader->height = mapGenOptions->getHeight();
|
|
|
|
mapInfo->mapHeader->width = mapGenOptions->getWidth();
|
|
|
|
mapInfo->mapHeader->twoLevel = mapGenOptions->getHasTwoLevels();
|
|
|
|
|
|
|
|
// Generate player information
|
|
|
|
int playersToGen = PlayerColor::PLAYER_LIMIT_I;
|
2023-10-28 20:30:38 +02:00
|
|
|
if(mapGenOptions->getHumanOrCpuPlayerCount() != CMapGenOptions::RANDOM_SIZE)
|
2020-07-07 13:45:53 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
playersToGen = mapGenOptions->getHumanOrCpuPlayerCount();
|
2020-07-07 13:45:53 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapHeader->howManyTeams = playersToGen;
|
|
|
|
|
2023-10-29 11:05:24 +02:00
|
|
|
//TODO: Assign all human-controlled colors in first place
|
2023-07-24 08:44:37 +02:00
|
|
|
|
2023-08-10 20:21:37 +02:00
|
|
|
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
|
|
|
|
{
|
|
|
|
mapInfo->mapHeader->players[i].canComputerPlay = false;
|
|
|
|
mapInfo->mapHeader->players[i].canHumanPlay = false;
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:30:38 +02:00
|
|
|
std::vector<PlayerColor> availableColors;
|
|
|
|
for (ui8 color = 0; color < PlayerColor::PLAYER_LIMIT_I; color++)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
availableColors.push_back(PlayerColor(color));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-10-28 20:30:38 +02:00
|
|
|
|
|
|
|
//First restore known players
|
|
|
|
for (auto& player : mapGenOptions->getPlayersSettings())
|
|
|
|
{
|
|
|
|
PlayerInfo playerInfo;
|
|
|
|
playerInfo.isFactionRandom = (player.second.getStartingTown() == CMapGenOptions::CPlayerSettings::RANDOM_TOWN);
|
|
|
|
playerInfo.canComputerPlay = (player.second.getPlayerType() != EPlayerType::HUMAN);
|
|
|
|
playerInfo.canHumanPlay = (player.second.getPlayerType() != EPlayerType::COMP_ONLY);
|
|
|
|
|
|
|
|
auto team = player.second.getTeam();
|
|
|
|
playerInfo.team = team;
|
|
|
|
//occupiedTeams.insert(team);
|
|
|
|
playerInfo.hasMainTown = true;
|
|
|
|
playerInfo.generateHeroAtMainTown = true;
|
|
|
|
mapInfo->mapHeader->players[player.first] = playerInfo;
|
|
|
|
vstd::erase(availableColors, player.first);
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfoChanged(mapInfo, mapGenOptions);
|
|
|
|
}
|
|
|
|
|
2023-10-29 11:05:24 +02:00
|
|
|
// This method only sets GUI options, doesn't alter any actual configurations done
|
2018-01-05 19:21:07 +02:00
|
|
|
void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
|
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
mapGenOptions = opts;
|
|
|
|
|
2023-10-28 20:30:38 +02:00
|
|
|
//Prepare allowed options - add all, then erase the ones above the limit
|
2022-12-14 02:37:11 +02:00
|
|
|
for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
|
|
|
|
{
|
|
|
|
playerCountAllowed.insert(i);
|
|
|
|
compCountAllowed.insert(i);
|
2023-07-20 20:34:00 +02:00
|
|
|
if (i >= 2)
|
|
|
|
{
|
|
|
|
playerTeamsAllowed.insert(i);
|
|
|
|
}
|
|
|
|
if (i >= 1)
|
|
|
|
{
|
|
|
|
compTeamsAllowed.insert(i);
|
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2023-10-28 20:30:38 +02:00
|
|
|
|
2022-12-14 02:37:11 +02:00
|
|
|
auto * tmpl = mapGenOptions->getMapTemplate();
|
|
|
|
if(tmpl)
|
|
|
|
{
|
2023-10-29 11:05:24 +02:00
|
|
|
playerCountAllowed = tmpl->getPlayers().getNumbers();
|
|
|
|
compCountAllowed = tmpl->getCpuPlayers().getNumbers();
|
2023-10-28 20:30:38 +02:00
|
|
|
auto compNumbers = tmpl->getCpuPlayers().getNumbers();
|
|
|
|
if (!compNumbers.empty())
|
|
|
|
{
|
|
|
|
compCountAllowed = compNumbers;
|
|
|
|
minComps = *boost::min_element(compCountAllowed);
|
|
|
|
}
|
|
|
|
|
2022-12-14 02:37:11 +02:00
|
|
|
playerCountAllowed = tmpl->getPlayers().getNumbers();
|
2023-10-28 20:30:38 +02:00
|
|
|
|
|
|
|
auto minPlayerCount = *boost::min_element(playerCountAllowed);
|
|
|
|
auto maxCompCount = *boost::max_element(compCountAllowed);
|
|
|
|
for (int i = 1; i >= (minPlayerCount - maxCompCount) && i >= 1; i--)
|
|
|
|
{
|
|
|
|
//We can always add extra CPUs to meet the minimum total player count
|
|
|
|
playerCountAllowed.insert(i);
|
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2023-10-28 20:30:38 +02:00
|
|
|
|
|
|
|
si8 playerLimit = opts->getPlayerLimit();
|
|
|
|
si8 humanOrCpuPlayerCount = opts->getHumanOrCpuPlayerCount();
|
2023-10-29 11:05:24 +02:00
|
|
|
si8 compOnlyPlayersCount = opts->getCompOnlyPlayerCount();
|
2023-10-28 20:30:38 +02:00
|
|
|
|
2023-10-29 11:05:24 +02:00
|
|
|
if(humanOrCpuPlayerCount != CMapGenOptions::RANDOM_SIZE)
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
vstd::erase_if(compCountAllowed, [playerLimit, humanOrCpuPlayerCount](int el)
|
|
|
|
{
|
|
|
|
return (playerLimit - humanOrCpuPlayerCount) < el;
|
2022-12-14 02:37:11 +02:00
|
|
|
});
|
2023-10-28 20:30:38 +02:00
|
|
|
vstd::erase_if(playerTeamsAllowed, [humanOrCpuPlayerCount](int el)
|
|
|
|
{
|
|
|
|
return humanOrCpuPlayerCount <= el;
|
2022-12-14 02:37:11 +02:00
|
|
|
});
|
2022-12-14 03:45:50 +02:00
|
|
|
|
|
|
|
if(!playerTeamsAllowed.count(opts->getTeamCount()))
|
2023-10-28 20:30:38 +02:00
|
|
|
{
|
2022-12-14 03:45:50 +02:00
|
|
|
opts->setTeamCount(CMapGenOptions::RANDOM_SIZE);
|
2023-10-28 20:30:38 +02:00
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2023-10-29 11:05:24 +02:00
|
|
|
if(compOnlyPlayersCount != CMapGenOptions::RANDOM_SIZE)
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
// This setting doesn't impact total number of players
|
|
|
|
vstd::erase_if(compTeamsAllowed, [compOnlyPlayersCount](int el)
|
|
|
|
{
|
|
|
|
return compOnlyPlayersCount<= el;
|
2022-12-14 02:37:11 +02:00
|
|
|
});
|
2022-12-14 03:45:50 +02:00
|
|
|
|
|
|
|
if(!compTeamsAllowed.count(opts->getCompOnlyTeamCount()))
|
2023-10-28 20:30:38 +02:00
|
|
|
{
|
2022-12-14 03:45:50 +02:00
|
|
|
opts->setCompOnlyTeamCount(CMapGenOptions::RANDOM_SIZE);
|
2023-10-28 20:30:38 +02:00
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMapSize"))
|
2023-04-19 22:14:23 +02:00
|
|
|
{
|
|
|
|
for(auto toggle : w->buttons)
|
|
|
|
{
|
|
|
|
if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
|
|
|
|
{
|
|
|
|
const auto & mapSizes = getPossibleMapSizes();
|
|
|
|
int3 size( mapSizes[toggle.first], mapSizes[toggle.first], 1 + mapGenOptions->getHasTwoLevels());
|
|
|
|
|
|
|
|
bool sizeAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
|
|
|
|
button->block(!sizeAllowed);
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
|
2023-04-19 22:14:23 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleButton>("buttonTwoLevels"))
|
2023-04-19 22:14:23 +02:00
|
|
|
{
|
|
|
|
int3 size( opts->getWidth(), opts->getWidth(), 2);
|
|
|
|
|
|
|
|
bool undergoundAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
|
|
|
|
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getHasTwoLevels());
|
2023-04-19 22:14:23 +02:00
|
|
|
w->block(!undergoundAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2023-10-28 20:30:38 +02:00
|
|
|
// FIXME: OH3 allows any setting here, even if currently selected template doesn't fit it
|
|
|
|
// TODO: Set max players to current template limit wherever template is explicitely selected
|
|
|
|
w->setSelected(opts->getHumanOrCpuPlayerCount());
|
2022-12-14 02:37:11 +02:00
|
|
|
deactivateButtonsFrom(*w, playerCountAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMaxTeams"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getTeamCount());
|
2022-12-14 03:23:21 +02:00
|
|
|
deactivateButtonsFrom(*w, playerTeamsAllowed);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getCompOnlyPlayerCount());
|
2022-12-14 03:23:21 +02:00
|
|
|
deactivateButtonsFrom(*w, compCountAllowed);
|
2022-12-14 02:37:11 +02:00
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getCompOnlyTeamCount());
|
2022-12-14 02:37:11 +02:00
|
|
|
deactivateButtonsFrom(*w, compTeamsAllowed);
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupWaterContent"))
|
2022-12-14 02:37:11 +02:00
|
|
|
{
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getWaterContent());
|
2022-12-14 02:37:11 +02:00
|
|
|
if(opts->getMapTemplate())
|
|
|
|
{
|
|
|
|
std::set<int> allowedWater(opts->getMapTemplate()->getWaterContentAllowed().begin(), opts->getMapTemplate()->getWaterContentAllowed().end());
|
|
|
|
deactivateButtonsFrom(*w, allowedWater);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
deactivateButtonsFrom(*w, {-1});
|
|
|
|
}
|
2022-12-12 09:38:27 +02:00
|
|
|
if(auto w = widget<CToggleGroup>("groupMonsterStrength"))
|
2022-12-12 01:58:39 +02:00
|
|
|
w->setSelected(opts->getMonsterStrength());
|
2022-12-17 06:38:33 +02:00
|
|
|
if(auto w = widget<CButton>("templateButton"))
|
|
|
|
{
|
|
|
|
if(tmpl)
|
2023-07-31 18:50:55 +02:00
|
|
|
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
|
2022-12-17 06:38:33 +02:00
|
|
|
else
|
2023-07-31 18:50:55 +02:00
|
|
|
w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
|
2022-12-17 06:38:33 +02:00
|
|
|
}
|
2022-12-20 18:35:40 +02:00
|
|
|
for(auto r : VLC->roadTypeHandler->objects)
|
2022-12-17 06:38:33 +02:00
|
|
|
{
|
2023-01-01 22:42:28 +02:00
|
|
|
if(auto w = widget<CToggleButton>(r->getJsonKey()))
|
2022-12-17 06:38:33 +02:00
|
|
|
{
|
2023-07-08 08:44:10 +02:00
|
|
|
w->setSelected(opts->isRoadEnabled(r->getId()));
|
2022-12-17 06:38:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 02:38:18 +02:00
|
|
|
void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
|
|
|
|
{
|
|
|
|
mapGenOptions->setMapTemplate(tmpl);
|
2022-12-14 02:37:11 +02:00
|
|
|
setMapGenOptions(mapGenOptions);
|
2022-12-13 02:38:18 +02:00
|
|
|
if(auto w = widget<CButton>("templateButton"))
|
|
|
|
{
|
|
|
|
if(tmpl)
|
2023-07-31 18:50:55 +02:00
|
|
|
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
|
2022-12-13 02:38:18 +02:00
|
|
|
else
|
2023-07-31 18:50:55 +02:00
|
|
|
w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
|
2022-12-13 02:38:18 +02:00
|
|
|
}
|
2022-12-14 02:37:11 +02:00
|
|
|
updateMapInfoByHost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomMapTab::deactivateButtonsFrom(CToggleGroup & group, const std::set<int> & allowed)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
logGlobal->debug("Blocking buttons");
|
|
|
|
for(auto toggle : group.buttons)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
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
|
|
|
{
|
2022-12-14 02:37:11 +02:00
|
|
|
if(allowed.count(CMapGenOptions::RANDOM_SIZE)
|
|
|
|
|| allowed.count(toggle.first)
|
|
|
|
|| toggle.first == CMapGenOptions::RANDOM_SIZE)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
button->block(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
button->block(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2022-12-13 01:47:29 +02:00
|
|
|
|
2023-07-20 20:34:00 +02:00
|
|
|
void TeamAlignmentsWidget::checkTeamCount()
|
|
|
|
{
|
|
|
|
//Do not allow to select one team only
|
|
|
|
std::set<TeamID> teams;
|
|
|
|
for (int plId = 0; plId < players.size(); ++plId)
|
|
|
|
{
|
|
|
|
teams.insert(TeamID(players[plId]->getSelected()));
|
|
|
|
}
|
|
|
|
if (teams.size() < 2)
|
|
|
|
{
|
|
|
|
//Do not let player close the window
|
|
|
|
buttonOk->block(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buttonOk->block(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 01:53:26 +02:00
|
|
|
TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
|
2022-12-30 01:35:16 +02:00
|
|
|
InterfaceObjectConfigurable()
|
2022-12-17 01:53:26 +02:00
|
|
|
{
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode config(JsonPath::builtin("config/widgets/randomMapTeamsWidget.json"));
|
2022-12-17 18:38:16 +02:00
|
|
|
variables = config["variables"];
|
2022-12-17 01:53:26 +02:00
|
|
|
|
2023-10-29 11:05:24 +02:00
|
|
|
int totalPlayers = randomMapTab.obtainMapGenOptions().getPlayerLimit();
|
|
|
|
//randomMapTab.obtainMapGenOptions().getTotalPlayersCount();
|
2022-12-17 06:19:16 +02:00
|
|
|
assert(totalPlayers <= PlayerColor::PLAYER_LIMIT_I);
|
|
|
|
auto settings = randomMapTab.obtainMapGenOptions().getPlayersSettings();
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["totalPlayers"].Integer() = totalPlayers;
|
|
|
|
|
|
|
|
pos.w = variables["windowSize"]["x"].Integer() + totalPlayers * variables["cellMargin"]["x"].Integer();
|
|
|
|
pos.h = variables["windowSize"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
variables["backgroundRect"]["x"].Integer() = pos.x;
|
|
|
|
variables["backgroundRect"]["y"].Integer() = pos.y;
|
|
|
|
variables["backgroundRect"]["w"].Integer() = pos.w;
|
|
|
|
variables["backgroundRect"]["h"].Integer() = pos.h;
|
|
|
|
variables["okButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["ok"]["x"].Integer();
|
|
|
|
variables["okButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["ok"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
variables["cancelButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["cancel"]["x"].Integer();
|
|
|
|
variables["cancelButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["cancel"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
|
|
|
|
|
|
|
|
addCallback("ok", [&](int)
|
|
|
|
{
|
|
|
|
for(int plId = 0; plId < players.size(); ++plId)
|
|
|
|
{
|
|
|
|
randomMapTab.obtainMapGenOptions().setPlayerTeam(PlayerColor(plId), TeamID(players[plId]->getSelected()));
|
|
|
|
}
|
|
|
|
randomMapTab.updateMapInfoByHost();
|
2023-05-16 17:34:23 +02:00
|
|
|
assert(GH.windows().isTopWindow(this));
|
|
|
|
GH.windows().popWindows(1);
|
2022-12-17 18:38:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
addCallback("cancel", [&](int)
|
|
|
|
{
|
2023-05-16 17:34:23 +02:00
|
|
|
assert(GH.windows().isTopWindow(this));
|
|
|
|
GH.windows().popWindows(1);
|
2022-12-17 18:38:16 +02:00
|
|
|
});
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-17 06:19:16 +02:00
|
|
|
|
2022-12-17 01:53:26 +02:00
|
|
|
center(pos);
|
|
|
|
|
2022-12-17 18:38:16 +02:00
|
|
|
OBJ_CONSTRUCTION;
|
|
|
|
|
2023-10-28 20:30:38 +02:00
|
|
|
// Window should have X * X columns, where X is players + compOnly players.
|
|
|
|
// For random player count, X is 8
|
|
|
|
|
|
|
|
if (totalPlayers > settings.size())
|
|
|
|
{
|
|
|
|
auto savedPlayers = randomMapTab.obtainMapGenOptions().getSavedPlayersMap();
|
|
|
|
for (const auto & player : savedPlayers)
|
|
|
|
{
|
|
|
|
if (!vstd::contains(settings, player.first))
|
|
|
|
{
|
|
|
|
settings[player.first] = player.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CMapGenOptions::CPlayerSettings> settingsVec;
|
|
|
|
for (const auto & player : settings)
|
|
|
|
{
|
|
|
|
settingsVec.push_back(player.second);
|
|
|
|
}
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int plId = 0; plId < totalPlayers; ++plId)
|
|
|
|
{
|
|
|
|
players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["player_id"].Integer() = plId;
|
2022-12-21 22:54:06 +02:00
|
|
|
OBJ_CONSTRUCTION_TARGETED(players[plId].get());
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int teamId = 0; teamId < totalPlayers; ++teamId)
|
|
|
|
{
|
|
|
|
auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
|
|
|
|
assert(button);
|
|
|
|
if(sel == teamId)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
button->addOverlay(buildWidget(variables["flagsAnimation"]));
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-17 18:50:33 +02:00
|
|
|
button->addOverlay(nullptr);
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
2023-07-20 20:34:00 +02:00
|
|
|
button->addCallback([this](bool)
|
|
|
|
{
|
|
|
|
checkTeamCount();
|
|
|
|
});
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2022-12-21 22:54:06 +02:00
|
|
|
OBJ_CONSTRUCTION_TARGETED(players.back().get());
|
2023-10-28 20:30:38 +02:00
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
for(int teamId = 0; teamId < totalPlayers; ++teamId)
|
|
|
|
{
|
2022-12-17 18:38:16 +02:00
|
|
|
variables["point"]["x"].Integer() = variables["cellOffset"]["x"].Integer() + plId * variables["cellMargin"]["x"].Integer();
|
|
|
|
variables["point"]["y"].Integer() = variables["cellOffset"]["y"].Integer() + teamId * variables["cellMargin"]["y"].Integer();
|
|
|
|
auto button = buildWidget(variables["button"]);
|
|
|
|
players.back()->addToggle(teamId, std::dynamic_pointer_cast<CToggleBase>(button));
|
2022-12-17 06:19:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-28 20:30:38 +02:00
|
|
|
// plId is not neccessarily player color, just an index
|
|
|
|
auto team = settingsVec.at(plId).getTeam();
|
2022-12-17 06:19:16 +02:00
|
|
|
if(team == TeamID::NO_TEAM)
|
2023-10-28 20:30:38 +02:00
|
|
|
{
|
|
|
|
logGlobal->warn("Player %d (id %d) has uninitialized team", settingsVec.at(plId).getColor(), plId);
|
2022-12-17 06:19:16 +02:00
|
|
|
players.back()->setSelected(plId);
|
2023-10-28 20:30:38 +02:00
|
|
|
}
|
2022-12-17 06:19:16 +02:00
|
|
|
else
|
|
|
|
players.back()->setSelected(team.getNum());
|
|
|
|
}
|
2023-07-20 20:34:00 +02:00
|
|
|
|
|
|
|
buttonOk = widget<CButton>("buttonOK");
|
|
|
|
buttonCancel = widget<CButton>("buttonCancel");
|
2022-12-17 01:53:26 +02:00
|
|
|
}
|