1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-26 08:41:13 +02:00
vcmi/client/lobby/RandomMapTab.cpp

392 lines
10 KiB
C++
Raw Normal View History

/*
* 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"
2022-12-13 02:38:18 +02:00
#include "../../lib/rmg/CRmgTemplateStorage.h"
2022-12-12 01:27:59 +02:00
RandomMapTab::RandomMapTab():
2022-12-12 09:48:39 +02:00
InterfaceObjectConfigurable()
{
recActions = 0;
mapGenOptions = std::make_shared<CMapGenOptions>();
2022-12-12 01:27:59 +02:00
const JsonNode config(ResourceID("config/windows/randomMapTab.json"));
2022-12-12 01:27:59 +02:00
addCallback("toggleMapSize", [&](int btnId)
{
auto mapSizeVal = getPossibleMapSizes();
mapGenOptions->setWidth(mapSizeVal[btnId]);
mapGenOptions->setHeight(mapSizeVal[btnId]);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("toggleTwoLevels", [&](bool on)
{
mapGenOptions->setHasTwoLevels(on);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("setPlayersCount", [&](int btnId)
{
mapGenOptions->setPlayerCount(btnId);
2022-12-12 01:27:59 +02:00
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMaxTeams"))
2022-12-12 01:58:39 +02:00
deactivateButtonsFrom(w.get(), btnId);
// deactive some CompOnlyPlayers buttons to prevent total number of players exceeds PlayerColor::PLAYER_LIMIT_I
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
2022-12-12 01:58:39 +02:00
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
validatePlayersCnt(btnId);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("setTeamsCount", [&](int btnId)
{
mapGenOptions->setTeamCount(btnId);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("setCompOnlyPlayers", [&](int btnId)
{
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 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
2022-12-12 01:58:39 +02:00
deactivateButtonsFrom(w.get(), PlayerColor::PLAYER_LIMIT_I - btnId + 1);
2022-11-18 02:05:35 +02:00
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
2022-12-12 01:58:39 +02:00
deactivateButtonsFrom(w.get(), (btnId == 0 ? 1 : btnId));
validateCompOnlyPlayersCnt(btnId);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("setCompOnlyTeams", [&](int btnId)
{
mapGenOptions->setCompOnlyTeamCount(btnId);
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
addCallback("setWaterContent", [&](int btnId)
{
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)
{
if(btnId < 0)
mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
else
mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
updateMapInfoByHost();
});
2022-12-12 01:27:59 +02:00
2022-12-13 01:47:29 +02:00
//new callbacks available only from mod
addCallback("templateSelection", [&](int)
{
2022-12-13 02:38:18 +02:00
GH.pushInt(std::make_shared<TemplatesDropBox>(this));
2022-12-13 01:47:29 +02:00
});
init(config);
2022-12-12 01:27:59 +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)
{
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
playersToGen = mapGenOptions->getPlayerCount() + mapGenOptions->getCompOnlyPlayerCount();
else
playersToGen = mapGenOptions->getPlayerCount();
}
mapInfo->mapHeader->howManyTeams = playersToGen;
for(int i = 0; i < playersToGen; ++i)
{
PlayerInfo player;
player.isFactionRandom = true;
player.canComputerPlay = true;
if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE && i >= mapGenOptions->getPlayerCount())
{
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 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMapSize"))
2022-12-12 01:58:39 +02:00
w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleButton>("buttonTwoLevels"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getHasTwoLevels());
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getPlayerCount());
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMaxTeams"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getTeamCount());
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getCompOnlyPlayerCount());
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getCompOnlyTeamCount());
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupWaterContent"))
2022-12-12 01:58:39 +02:00
w->setSelected(opts->getWaterContent());
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-13 02:38:18 +02:00
void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
{
mapGenOptions->setMapTemplate(tmpl);
if(auto w = widget<CButton>("templateButton"))
{
if(tmpl)
w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL);
else
w->addTextOverlay("default", EFonts::FONT_SMALL);
}
}
void RandomMapTab::deactivateButtonsFrom(CToggleGroup * group, int startId)
{
logGlobal->debug("Blocking buttons from %d", startId);
for(auto toggle : group->buttons)
{
if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
{
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 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupMaxTeams"))
2022-12-12 01:58:39 +02:00
w->setSelected(mapGenOptions->getTeamCount());
}
// total players should not exceed PlayerColor::PLAYER_LIMIT_I (8 in homm3)
if(mapGenOptions->getCompOnlyPlayerCount() + playersCnt > PlayerColor::PLAYER_LIMIT_I)
{
mapGenOptions->setCompOnlyPlayerCount(PlayerColor::PLAYER_LIMIT_I - playersCnt);
2022-12-12 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
2022-12-12 01:58:39 +02:00
w->setSelected(mapGenOptions->getCompOnlyPlayerCount());
}
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 09:38:27 +02:00
if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
2022-12-12 01:58:39 +02:00
w->setSelected(compOnlyTeamCount);
}
}
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};
}
2022-12-13 01:47:29 +02:00
2022-12-13 02:38:18 +02:00
TemplatesDropBox::ListItem::ListItem(TemplatesDropBox * _dropBox, Point position)
: CIntObject(LCLICK | HOVER, position),
dropBox(_dropBox)
2022-12-13 01:47:29 +02:00
{
OBJ_CONSTRUCTION;
2022-12-13 02:38:18 +02:00
labelName = std::make_shared<CLabel>(0, 0, FONT_SMALL, EAlignment::TOPLEFT, Colors::WHITE);
2022-12-13 01:47:29 +02:00
labelName->setAutoRedraw(false);
hoverImage = std::make_shared<CPicture>("List10Sl", 0, 0);
hoverImage->visible = false;
pos.w = hoverImage->pos.w;
pos.h = hoverImage->pos.h;
type |= REDRAW_PARENT;
}
2022-12-13 02:38:18 +02:00
void TemplatesDropBox::ListItem::updateItem(int idx, const CRmgTemplate * _item)
{
item = _item;
if(item)
{
labelName->setText(item->getName());
}
else
{
if(idx)
labelName->setText("");
else
labelName->setText("default");
}
}
2022-12-13 01:47:29 +02:00
void TemplatesDropBox::ListItem::hover(bool on)
{
2022-12-13 02:38:18 +02:00
if(labelName->getText().empty())
{
hovered = false;
hoverImage->visible = false;
}
else
{
hoverImage->visible = on;
}
2022-12-13 01:47:29 +02:00
redraw();
}
2022-12-13 02:38:18 +02:00
void TemplatesDropBox::ListItem::clickLeft(tribool down, bool previousState)
{
if(down && hovered)
{
dropBox->setTemplate(item);
}
}
TemplatesDropBox::TemplatesDropBox(RandomMapTab * randomMapTab):
2022-12-13 01:47:29 +02:00
CIntObject(LCLICK | HOVER),
2022-12-13 02:38:18 +02:00
randomMapTab(randomMapTab)
2022-12-13 01:47:29 +02:00
{
2022-12-13 02:38:18 +02:00
curItems = VLC->tplh->getTemplates();
curItems.insert(curItems.begin(), nullptr); //default template
2022-12-13 01:47:29 +02:00
OBJ_CONSTRUCTION;
background = std::make_shared<CPicture>("List10Bk", 158, 76);
int positionsToShow = 10;
for(int i = 0; i < positionsToShow; i++)
2022-12-13 02:38:18 +02:00
listItems.push_back(std::make_shared<ListItem>(this, Point(158, 76 + i * 25)));
2022-12-13 01:47:29 +02:00
2022-12-13 02:38:18 +02:00
slider = std::make_shared<CSlider>(Point(212 + 158, 76), 252, std::bind(&TemplatesDropBox::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, false, CSlider::BLUE);
2022-12-13 01:47:29 +02:00
2022-12-13 02:38:18 +02:00
updateListItems();
2022-12-13 01:47:29 +02:00
pos = background->pos;
}
void TemplatesDropBox::sliderMove(int slidPos)
{
if(!slider)
return; // ignore spurious call when slider is being created
2022-12-13 02:38:18 +02:00
updateListItems();
2022-12-13 01:47:29 +02:00
redraw();
}
void TemplatesDropBox::hover(bool on)
{
hovered = on;
}
void TemplatesDropBox::clickLeft(tribool down, bool previousState)
{
2022-12-13 02:38:18 +02:00
if(down && !hovered)
2022-12-13 01:47:29 +02:00
{
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
}
}
2022-12-13 02:38:18 +02:00
void TemplatesDropBox::updateListItems()
{
int elemIdx = slider->getValue();
for(auto item : listItems)
{
if(elemIdx < curItems.size())
{
item->updateItem(elemIdx, curItems[elemIdx]);
elemIdx++;
}
else
{
item->updateItem(elemIdx);
}
}
}
void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
{
randomMapTab->setTemplate(tmpl);
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
}