BattleOnlyModeTab.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * BattleOnlyModeTab.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleOnlyModeTab.h"
  12. #include "CLobbyScreen.h"
  13. #include "../CServerHandler.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../render/IRenderHandler.h"
  17. #include "../render/CAnimation.h"
  18. #include "../render/Canvas.h"
  19. #include "../render/CanvasImage.h"
  20. #include "../render/IFont.h"
  21. #include "../gui/Shortcut.h"
  22. #include "../gui/WindowHandler.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../widgets/GraphicalPrimitiveCanvas.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../widgets/CTextInput.h"
  27. #include "../widgets/Images.h"
  28. #include "../widgets/CComponent.h"
  29. #include "../windows/GUIClasses.h"
  30. #include "../windows/CHeroOverview.h"
  31. #include "../windows/CCreatureWindow.h"
  32. #include "../windows/InfoWindows.h"
  33. #include "../../lib/CConfigHandler.h"
  34. #include "../../lib/GameLibrary.h"
  35. #include "../../lib/gameState/CGameState.h"
  36. #include "../../lib/networkPacks/PacksForLobby.h"
  37. #include "../../lib/StartInfo.h"
  38. #include "../../lib/VCMIDirs.h"
  39. #include "../../lib/CRandomGenerator.h"
  40. #include "../../lib/CSkillHandler.h"
  41. #include "../../lib/callback/EditorCallback.h"
  42. #include "../../lib/entities/hero/CHero.h"
  43. #include "../../lib/entities/hero/CHeroClass.h"
  44. #include "../../lib/entities/hero/CHeroHandler.h"
  45. #include "../../lib/entities/faction/CTown.h"
  46. #include "../../lib/entities/faction/CTownHandler.h"
  47. #include "../../lib/entities/artifact/CArtifact.h"
  48. #include "../../lib/entities/artifact/CArtHandler.h"
  49. #include "../../lib/mapObjects/CGHeroInstance.h"
  50. #include "../../lib/mapObjects/CGTownInstance.h"
  51. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  52. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  53. #include "../../lib/mapping/CMap.h"
  54. #include "../../lib/mapping/CMapInfo.h"
  55. #include "../../lib/mapping/CMapEditManager.h"
  56. #include "../../lib/mapping/CMapService.h"
  57. #include "../../lib/mapping/MapFormat.h"
  58. #include "../../lib/spells/CSpellHandler.h"
  59. #include "../../lib/spells/SpellSchoolHandler.h"
  60. #include "../../lib/texts/CGeneralTextHandler.h"
  61. #include "../../lib/texts/MetaString.h"
  62. #include "../../lib/texts/TextOperations.h"
  63. #include "../../lib/filesystem/Filesystem.h"
  64. #include "../../lib/serializer/JsonSerializer.h"
  65. #include "../../lib/serializer/JsonDeserializer.h"
  66. BattleOnlyModeTab::BattleOnlyModeTab()
  67. : startInfo(std::make_shared<BattleOnlyModeStartInfo>())
  68. , disabledColor(GAME->server().isHost() ? Colors::WHITE : Colors::ORANGE)
  69. , boxColor(ColorRGBA(128, 128, 128))
  70. , disabledBoxColor(GAME->server().isHost() ? boxColor : ColorRGBA(116, 92, 16))
  71. {
  72. OBJECT_CONSTRUCTION;
  73. //JsonNode node = persistentStorage["lobby"]["battleModeSettings"];
  74. //JsonDeserializer handler(nullptr, node);
  75. //startInfo->serializeJson(handler);
  76. init();
  77. backgroundImage = std::make_shared<CPicture>(ImagePath::builtin("AdventureOptionsBackgroundClear"), 0, 6);
  78. title = std::make_shared<CLabel>(220, 35, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyMode"));
  79. subTitle = std::make_shared<CMultiLineLabel>(Rect(55, 40, 333, 40), FONT_SMALL, ETextAlignment::BOTTOMCENTER, Colors::WHITE, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSubTitle"));
  80. battlefieldSelector = std::make_shared<CButton>(Point(57, 552), AnimationPath::builtin("GSPButtonClear"), CButton::tooltip(), [this](){
  81. std::vector<std::string> texts;
  82. std::vector<std::shared_ptr<IImage>> images;
  83. std::vector<std::shared_ptr<TerrainType>> terrains;
  84. std::copy_if(LIBRARY->terrainTypeHandler->objects.begin(), LIBRARY->terrainTypeHandler->objects.end(), std::back_inserter(terrains), [](auto terrain) { return terrain->isPassable(); });
  85. for (const auto & terrain : terrains)
  86. {
  87. texts.push_back(terrain->getNameTranslated());
  88. const auto & patterns = LIBRARY->terviewh->getTerrainViewPatterns(terrain->getId());
  89. TerrainViewPattern pattern;
  90. for(auto & p : patterns)
  91. if(p[0].id == "n1")
  92. pattern = p[0];
  93. auto image = ENGINE->renderHandler().loadImage(terrain->tilesFilename, pattern.mapping[0].first, 0, EImageBlitMode::OPAQUE);
  94. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  95. images.push_back(image);
  96. }
  97. auto factions = LIBRARY->townh->getDefaultAllowed();
  98. for (const auto & faction : factions)
  99. {
  100. texts.push_back(faction.toFaction()->getNameTranslated());
  101. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("ITPA"), faction.toFaction()->town->clientInfo.icons[true][false] + 2, 0, EImageBlitMode::OPAQUE);
  102. image->scaleTo(Point(35, 23), EScalingAlgorithm::NEAREST);
  103. images.push_back(image);
  104. }
  105. ENGINE->windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefield"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefieldSelect"), [this, terrains, factions](int index){
  106. if(terrains.size() > index)
  107. {
  108. startInfo->selectedTerrain = terrains[index]->getId();
  109. startInfo->selectedTown = FactionID::ANY;
  110. }
  111. else
  112. {
  113. startInfo->selectedTerrain = TerrainId::NONE;
  114. auto it = std::next(factions.begin(), index - terrains.size());
  115. if (it != factions.end())
  116. startInfo->selectedTown = *it;
  117. }
  118. onChange();
  119. }, (startInfo->selectedTerrain != TerrainId::NONE ? static_cast<int>(startInfo->selectedTerrain) : static_cast<int>(startInfo->selectedTown + terrains.size())), images, true, true);
  120. });
  121. battlefieldSelector->block(GAME->server().isGuest());
  122. buttonReset = std::make_shared<CButton>(Point(259, 552), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](){
  123. if(GAME->server().isHost())
  124. {
  125. startInfo->selectedTerrain = TerrainId::DIRT;
  126. startInfo->selectedTown = FactionID::ANY;
  127. startInfo->selectedHero[0] = HeroTypeID::NONE;
  128. startInfo->selectedArmy[0].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
  129. startInfo->secSkillLevel[0].fill(std::make_pair(SecondarySkill::NONE, MasteryLevel::NONE));
  130. startInfo->artifacts[0].clear();
  131. startInfo->spellBook[0] = true;
  132. startInfo->warMachines[0] = false;
  133. startInfo->spells[0].clear();
  134. for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
  135. heroSelector1->selectedArmyInput.at(i)->disable();
  136. for(size_t i=0; i<8; i++)
  137. heroSelector1->selectedSecSkillInput.at(i)->disable();
  138. }
  139. startInfo->selectedHero[1] = HeroTypeID::NONE;
  140. startInfo->selectedArmy[1].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
  141. startInfo->secSkillLevel[1].fill(std::make_pair(SecondarySkill::NONE, MasteryLevel::NONE));
  142. startInfo->artifacts[1].clear();
  143. startInfo->spellBook[1] = true;
  144. startInfo->warMachines[1] = false;
  145. startInfo->spells[1].clear();
  146. for(size_t i=0; i<8; i++)
  147. heroSelector2->selectedSecSkillInput.at(i)->disable();
  148. onChange();
  149. });
  150. buttonReset->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeReset"), EFonts::FONT_SMALL, Colors::WHITE);
  151. heroSelector1 = std::make_shared<BattleOnlyModeHeroSelector>(0, *this, Point(55, 90));
  152. heroSelector2 = std::make_shared<BattleOnlyModeHeroSelector>(1, *this, Point(55, 320));
  153. heroSelector1->setInputEnabled(GAME->server().isHost());
  154. onChange();
  155. }
  156. void BattleOnlyModeTab::init()
  157. {
  158. map = std::make_unique<CMap>(nullptr);
  159. map->version = EMapFormat::VCMI;
  160. map->creationDateTime = std::time(nullptr);
  161. map->width = 10;
  162. map->height = 10;
  163. map->mapLevels = 1;
  164. map->battleOnly = true;
  165. map->name = MetaString::createFromTextID("vcmi.lobby.battleOnlyMode");
  166. cb = std::make_unique<EditorCallback>(map.get());
  167. map->cb = cb.get();
  168. }
  169. void BattleOnlyModeTab::onChange()
  170. {
  171. GAME->server().setBattleOnlyModeStartInfo(startInfo);
  172. }
  173. void BattleOnlyModeTab::update()
  174. {
  175. setTerrainButtonText();
  176. setStartButtonEnabled();
  177. heroSelector1->setHeroIcon();
  178. heroSelector1->setCreatureIcons();
  179. heroSelector1->setSecSkillIcons();
  180. heroSelector1->setArtifactIcons();
  181. heroSelector1->spellBook->setSelectedSilent(startInfo->spellBook[0]);
  182. heroSelector1->warMachines->setSelectedSilent(startInfo->warMachines[0]);
  183. heroSelector2->setHeroIcon();
  184. heroSelector2->setCreatureIcons();
  185. heroSelector2->setSecSkillIcons();
  186. heroSelector2->setArtifactIcons();
  187. heroSelector2->spellBook->setSelectedSilent(startInfo->spellBook[1]);
  188. heroSelector2->warMachines->setSelectedSilent(startInfo->warMachines[1]);
  189. redraw();
  190. JsonNode node;
  191. JsonSerializer handler(nullptr, node);
  192. startInfo->serializeJson(handler);
  193. Settings storage = persistentStorage.write["lobby"]["battleModeSettings"];
  194. storage->Struct() = node.Struct();
  195. }
  196. void BattleOnlyModeTab::applyStartInfo(std::shared_ptr<BattleOnlyModeStartInfo> si)
  197. {
  198. startInfo = si;
  199. update();
  200. }
  201. void BattleOnlyModeTab::setTerrainButtonText()
  202. {
  203. battlefieldSelector->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefield") + ": " + (startInfo->selectedTerrain != TerrainId::NONE ? startInfo->selectedTerrain.toEntity(LIBRARY)->getNameTranslated() : startInfo->selectedTown.toEntity(LIBRARY)->getNameTranslated()), EFonts::FONT_SMALL, disabledColor);
  204. }
  205. void BattleOnlyModeTab::setStartButtonEnabled()
  206. {
  207. bool army2Empty = std::all_of(startInfo->selectedArmy[1].begin(), startInfo->selectedArmy[1].end(), [](const auto x) { return x.getId() == CreatureID::NONE; });
  208. bool canStart = (startInfo->selectedTerrain != TerrainId::NONE || startInfo->selectedTown != FactionID::ANY);
  209. canStart &= (startInfo->selectedHero[0] != HeroTypeID::NONE && ((startInfo->selectedHero[1] != HeroTypeID::NONE) || (startInfo->selectedTown != FactionID::ANY && !army2Empty)));
  210. (static_cast<CLobbyScreen *>(parent))->buttonStart->block(!canStart || GAME->server().isGuest());
  211. }
  212. std::shared_ptr<IImage> drawBlackBox(Point size, std::string text, ColorRGBA color)
  213. {
  214. auto image = ENGINE->renderHandler().createImage(size, CanvasScalingPolicy::AUTO);
  215. Canvas canvas = image->getCanvas();
  216. canvas.drawColor(Rect(0, 0, size.x, size.y), Colors::BLACK);
  217. std::vector<std::string> lines;
  218. boost::split(lines, text, boost::is_any_of("\n"));
  219. int lineH = ENGINE->renderHandler().loadFont(FONT_TINY)->getLineHeight();
  220. int totalH = lines.size() * lineH;
  221. int startY = (size.y - totalH) / 2 + lineH / 2;
  222. for (size_t i = 0; i < lines.size(); ++i)
  223. canvas.drawText(Point(size.x / 2, startY + i * lineH), FONT_TINY, color, ETextAlignment::CENTER, lines[i]);
  224. return image;
  225. }
  226. BattleOnlyModeHeroSelector::BattleOnlyModeHeroSelector(int id, BattleOnlyModeTab& p, Point position)
  227. : parent(p)
  228. , id(id)
  229. {
  230. OBJECT_CONSTRUCTION;
  231. pos.x += position.x;
  232. pos.y += position.y;
  233. backgroundImage = std::make_shared<CPicture>(ImagePath::builtin("heroSlotsBlue"), Point(3, 4));
  234. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  235. {
  236. auto image = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL32"), i, 0, 78 + i * 36, 26);
  237. primSkills.push_back(image);
  238. primSkillsBorder.push_back(std::make_shared<GraphicalPrimitiveCanvas>(Rect(78 + i * 36, 26, 32, 32)));
  239. primSkillsBorder.back()->addRectangle(Point(0, 0), Point(32, 32), ColorRGBA(44, 108, 255));
  240. primSkillsInput.push_back(std::make_shared<CTextInput>(Rect(78 + i * 36, 58, 32, 16), EFonts::FONT_SMALL, ETextAlignment::CENTER, false));
  241. primSkillsInput.back()->setColor(id == 1 ? Colors::WHITE : parent.disabledColor);
  242. primSkillsInput.back()->setFilterNumber(0, 100);
  243. primSkillsInput.back()->setText("0");
  244. primSkillsInput.back()->setCallback([this, i, id](const std::string & text){
  245. parent.startInfo->primSkillLevel[id][i] = std::stoi(primSkillsInput[i]->getText());
  246. parent.onChange();
  247. });
  248. }
  249. creatureImage.resize(GameConstants::ARMY_SIZE);
  250. for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
  251. {
  252. selectedArmyInput.push_back(std::make_shared<CTextInput>(Rect(5 + i * 36, 113, 32, 16), EFonts::FONT_SMALL, ETextAlignment::CENTER, false));
  253. selectedArmyInput.back()->setColor(id == 1 ? Colors::WHITE : parent.disabledColor);
  254. selectedArmyInput.back()->setFilterNumber(1, 10000000, 3);
  255. selectedArmyInput.back()->setText("1");
  256. selectedArmyInput.back()->setCallback([this, i, id](const std::string & text){
  257. if(parent.startInfo->selectedArmy[id][i].getId() != CreatureID::NONE)
  258. {
  259. parent.startInfo->selectedArmy[id][i].setCount(TextOperations::parseMetric<int>(text));
  260. parent.onChange();
  261. selectedArmyInput[i]->enable();
  262. }
  263. else
  264. selectedArmyInput[i]->disable();
  265. });
  266. }
  267. for(size_t i=0; i<8; i++)
  268. {
  269. bool isLeft = (i % 2 == 0);
  270. int line = (i / 2);
  271. Point textPos(261 + (isLeft ? 0 : 36), 41 + line * 54);
  272. selectedSecSkillInput.push_back(std::make_shared<CTextInput>(Rect(textPos, Point(32, 16)), EFonts::FONT_SMALL, ETextAlignment::CENTER, false));
  273. selectedSecSkillInput.back()->setColor(id == 1 ? Colors::WHITE : parent.disabledColor);
  274. selectedSecSkillInput.back()->setFilterNumber(0, 3);
  275. selectedSecSkillInput.back()->setText("3");
  276. selectedSecSkillInput.back()->setCallback([this, i, id](const std::string & text){
  277. if(parent.startInfo->secSkillLevel[id][i].second != MasteryLevel::NONE)
  278. {
  279. parent.startInfo->secSkillLevel[id][i].second = static_cast<MasteryLevel::Type>(std::stoi(text));
  280. parent.onChange();
  281. selectedSecSkillInput[i]->enable();
  282. }
  283. else
  284. selectedSecSkillInput[i]->disable();
  285. });
  286. }
  287. secSkillImage.resize(8);
  288. artifactImage.resize(14);
  289. auto tmpIcon = ENGINE->renderHandler().loadImage(AnimationPath::builtin("Artifact"), ArtifactID(ArtifactID::SPELLBOOK).toArtifact()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  290. tmpIcon->scaleTo(Point(16, 16), EScalingAlgorithm::NEAREST);
  291. addIcon.push_back(std::make_shared<CPicture>(tmpIcon, Point(220, 32)));
  292. addIcon.back()->addLClickCallback([this](){ manageSpells(); });
  293. addIcon.back()->addRClickCallback([this, id](){
  294. std::vector<std::shared_ptr<CComponent>> comps;
  295. for(auto & spell : parent.startInfo->spells[id])
  296. comps.push_back(std::make_shared<CComponent>(ComponentType::SPELL, spell, std::nullopt, CComponent::ESize::large));
  297. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("artifact.core.spellBook.name"), comps);
  298. });
  299. spellBook = std::make_shared<CToggleButton>(Point(235, 31), AnimationPath::builtin("lobby/checkboxSmall"), CButton::tooltip(), [this, id](bool enabled){
  300. parent.startInfo->spellBook[id] = enabled;
  301. parent.onChange();
  302. redraw();
  303. });
  304. spellBook->setSelectedSilent(parent.startInfo->spellBook[id]);
  305. tmpIcon = ENGINE->renderHandler().loadImage(AnimationPath::builtin("Artifact"), ArtifactID(ArtifactID::BALLISTA).toArtifact()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  306. tmpIcon->scaleTo(Point(16, 16), EScalingAlgorithm::NEAREST);
  307. addIcon.push_back(std::make_shared<CPicture>(tmpIcon, Point(220, 56)));
  308. warMachines = std::make_shared<CToggleButton>(Point(235, 55), AnimationPath::builtin("lobby/checkboxSmall"), CButton::tooltip(), [this, id](bool enabled){
  309. parent.startInfo->warMachines[id] = enabled;
  310. parent.onChange();
  311. redraw();
  312. });
  313. warMachines->setSelectedSilent(parent.startInfo->warMachines[id]);
  314. setHeroIcon();
  315. setCreatureIcons();
  316. setSecSkillIcons();
  317. setArtifactIcons();
  318. }
  319. void BattleOnlyModeHeroSelector::manageSpells()
  320. {
  321. std::vector<std::shared_ptr<CComponent>> resComps;
  322. for(auto & spellId : parent.startInfo->spells[id])
  323. resComps.push_back(std::make_shared<CComponent>(ComponentType::SPELL, spellId, std::nullopt, CComponent::ESize::large));
  324. std::vector<std::pair<AnimationPath, CFunctionList<void()>>> pom;
  325. for(int i = 0; i < 3; i++)
  326. pom.emplace_back(AnimationPath::builtin("settingsWindow/button80"), nullptr);
  327. auto allowedSet = LIBRARY->spellh->getDefaultAllowed();
  328. std::vector<SpellID> allSpells(allowedSet.begin(), allowedSet.end());
  329. allSpells.erase(std::remove_if(allSpells.begin(), allSpells.end(), [](const SpellID& spell) {
  330. return !spell.toSpell()->isCombat();
  331. }), allSpells.end());
  332. std::sort(allSpells.begin(), allSpells.end(), [](auto a, auto b) {
  333. auto A = a.toSpell();
  334. auto B = b.toSpell();
  335. if(A->getLevel() < B->getLevel())
  336. return true;
  337. if(A->getLevel() > B->getLevel())
  338. return false;
  339. for (const auto schoolId : LIBRARY->spellSchoolHandler->getAllObjects())
  340. {
  341. if(A->schools.count(schoolId) && !B->schools.count(schoolId))
  342. return true;
  343. if(!A->schools.count(schoolId) && B->schools.count(schoolId))
  344. return false;
  345. }
  346. return TextOperations::compareLocalizedStrings(A->getNameTranslated(), B->getNameTranslated());
  347. });
  348. std::vector<SpellID> toAdd;
  349. std::vector<SpellID> toRemove;
  350. for (const auto& spell : allSpells)
  351. {
  352. bool inCurrent = std::find(parent.startInfo->spells[id].begin(), parent.startInfo->spells[id].end(), spell) != parent.startInfo->spells[id].end();
  353. if (inCurrent)
  354. toRemove.push_back(spell);
  355. else
  356. toAdd.push_back(spell);
  357. }
  358. auto openList = [this](std::vector<SpellID> list, bool add){
  359. std::vector<std::string> texts;
  360. std::vector<std::shared_ptr<IImage>> images;
  361. for (const auto & s : list)
  362. {
  363. texts.push_back(s.toSpell()->getNameTranslated());
  364. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("SpellInt"), s.toSpell()->getIconIndex() + 1, 0, EImageBlitMode::OPAQUE);
  365. image->scaleTo(Point(35, 23), EScalingAlgorithm::NEAREST);
  366. images.push_back(image);
  367. }
  368. std::string title = LIBRARY->generaltexth->translate(add ? "vcmi.lobby.battleOnlySpellAdd" : "vcmi.lobby.battleOnlySpellRemove");
  369. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, title, title, [this, list, add](int index){
  370. auto & v = parent.startInfo->spells[id];
  371. if(add)
  372. v.push_back(list[index]);
  373. else
  374. v.erase(std::remove(v.begin(), v.end(), list[index]), v.end());
  375. parent.onChange();
  376. manageSpells();
  377. }, 0, images, true, true);
  378. window->onPopup = [list](int index) {
  379. std::shared_ptr<CComponent> comp = std::make_shared<CComponent>(ComponentType::SPELL, list[index]);
  380. CRClickPopup::createAndPush(list[index].toSpell()->getDescriptionTranslated(0), CInfoWindow::TCompsInfo(1, comp));
  381. };
  382. ENGINE->windows().pushWindow(window);
  383. };
  384. auto temp = std::make_shared<CInfoWindow>(LIBRARY->generaltexth->translate(parent.startInfo->spells[id].size() ? "vcmi.lobby.battleOnlySpellSelectCurrent" : "vcmi.lobby.battleOnlySpellSelect"), PlayerColor(0), resComps, pom);
  385. temp->buttons[0]->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("lobby/addChannel")));
  386. temp->buttons[0]->addCallback([openList, toAdd](){ openList(toAdd, true); });
  387. temp->buttons[0]->addPopupCallback([](){ CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlySpellAdd")); });
  388. temp->buttons[1]->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("lobby/removeChannel")));
  389. temp->buttons[1]->addCallback([openList, toRemove](){ openList(toRemove, false); });
  390. temp->buttons[1]->addPopupCallback([](){ CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlySpellRemove")); });
  391. temp->buttons[1]->setEnabled(parent.startInfo->spells[id].size());
  392. temp->buttons[2]->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("spellResearch/close")));
  393. temp->buttons[2]->addPopupCallback([](){ CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("core.genrltxt.600")); });
  394. ENGINE->windows().pushWindow(temp);
  395. }
  396. void BattleOnlyModeHeroSelector::setHeroIcon()
  397. {
  398. OBJECT_CONSTRUCTION;
  399. if(parent.startInfo->selectedHero[id] == HeroTypeID::NONE)
  400. {
  401. heroImage = std::make_shared<CPicture>(drawBlackBox(Point(58, 64), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSelectHero"), id == 1 ? parent.boxColor : parent.disabledBoxColor), Point(6, 7));
  402. heroLabel = std::make_shared<CLabel>(160, 16, FONT_SMALL, ETextAlignment::CENTER, id == 1 ? Colors::WHITE : parent.disabledColor, LIBRARY->generaltexth->translate("core.genrltxt.507"));
  403. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  404. primSkillsInput[i]->setText("0");
  405. }
  406. else
  407. {
  408. heroImage = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("PortraitsLarge"), EImageBlitMode::COLORKEY)->getImage(parent.startInfo->selectedHero[id].toHeroType()->imageIndex), Point(6, 7));
  409. heroLabel = std::make_shared<CLabel>(160, 16, FONT_SMALL, ETextAlignment::CENTER, id == 1 ? Colors::WHITE : parent.disabledColor, parent.startInfo->selectedHero[id].toHeroType()->getNameTranslated());
  410. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  411. primSkillsInput[i]->setText(std::to_string(parent.startInfo->primSkillLevel[id][i]));
  412. }
  413. heroImage->addLClickCallback([this](){
  414. auto allowedSet = LIBRARY->heroh->getDefaultAllowed();
  415. std::vector<HeroTypeID> heroes(allowedSet.begin(), allowedSet.end());
  416. std::sort(heroes.begin(), heroes.end(), [](auto a, auto b) {
  417. auto heroA = a.toHeroType();
  418. auto heroB = b.toHeroType();
  419. if(heroA->heroClass->faction != heroB->heroClass->faction)
  420. return heroA->heroClass->faction < heroB->heroClass->faction;
  421. if(heroA->heroClass->getId() != heroB->heroClass->getId())
  422. return heroA->heroClass->getId() < heroB->heroClass->getId();
  423. return heroA->getNameTranslated() < heroB->getNameTranslated();
  424. });
  425. int selectedIndex = parent.startInfo->selectedHero[id] == HeroTypeID::NONE ? 0 : (1 + std::distance(heroes.begin(), std::find_if(heroes.begin(), heroes.end(), [this](auto heroID) {
  426. return heroID == parent.startInfo->selectedHero[id];
  427. })));
  428. std::vector<std::string> texts;
  429. std::vector<std::shared_ptr<IImage>> images;
  430. // Add "no hero" option
  431. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  432. images.push_back(nullptr);
  433. for (const auto & h : heroes)
  434. {
  435. texts.push_back(h.toHeroType()->getNameTranslated());
  436. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("PortraitsSmall"), h.toHeroType()->imageIndex, 0, EImageBlitMode::OPAQUE);
  437. image->scaleTo(Point(35, 23), EScalingAlgorithm::NEAREST);
  438. images.push_back(image);
  439. }
  440. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeHeroSelect"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeHeroSelect"), [this, heroes](int index){
  441. if(index == 0)
  442. {
  443. parent.startInfo->selectedHero[id] = HeroTypeID::NONE;
  444. parent.onChange();
  445. return;
  446. }
  447. index--;
  448. parent.startInfo->selectedHero[id] = heroes[index];
  449. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  450. parent.startInfo->primSkillLevel[id][i] = 0;
  451. parent.onChange();
  452. }, selectedIndex, images, true, true);
  453. window->onPopup = [heroes](int index) {
  454. if(index == 0)
  455. return;
  456. index--;
  457. ENGINE->windows().createAndPushWindow<CHeroOverview>(heroes.at(index));
  458. };
  459. ENGINE->windows().pushWindow(window);
  460. });
  461. heroImage->addRClickCallback([this](){
  462. if(parent.startInfo->selectedHero[id] == HeroTypeID::NONE)
  463. return;
  464. ENGINE->windows().createAndPushWindow<CHeroOverview>(parent.startInfo->selectedHero[id].toHeroType()->getId());
  465. });
  466. }
  467. void BattleOnlyModeHeroSelector::setCreatureIcons()
  468. {
  469. OBJECT_CONSTRUCTION;
  470. for(int i = 0; i < creatureImage.size(); i++)
  471. {
  472. if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
  473. {
  474. MetaString str;
  475. str.appendTextID("vcmi.lobby.battleOnlyModeSelectUnit");
  476. str.replaceNumber(i + 1);
  477. creatureImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), str.toString(), id == 1 ? parent.boxColor : parent.disabledBoxColor), Point(6 + i * 36, 78));
  478. selectedArmyInput[i]->disable();
  479. }
  480. else
  481. {
  482. auto unit = parent.startInfo->selectedArmy[id][i];
  483. auto creatureID = unit.getId();
  484. creatureImage[i] = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"), EImageBlitMode::COLORKEY)->getImage(LIBRARY->creh->objects.at(creatureID)->getIconIndex()), Point(6 + i * 36, 78));
  485. selectedArmyInput[i]->setText(TextOperations::formatMetric(unit.getCount(), 3));
  486. selectedArmyInput[i]->enable();
  487. }
  488. creatureImage[i]->addLClickCallback([this, i](){
  489. auto allowedSet = LIBRARY->creh->getDefaultAllowed();
  490. std::vector<CreatureID> creatures(allowedSet.begin(), allowedSet.end());
  491. std::sort(creatures.begin(), creatures.end(), [](auto a, auto b) {
  492. auto creatureA = a.toCreature();
  493. auto creatureB = b.toCreature();
  494. if(creatureA->getFactionID() != creatureB->getFactionID())
  495. return creatureA->getFactionID() < creatureB->getFactionID();
  496. if(creatureA->getLevel() != creatureB->getLevel())
  497. return creatureA->getLevel() < creatureB->getLevel();
  498. if(creatureA->upgrades.size() != creatureB->upgrades.size())
  499. return creatureA->upgrades.size() > creatureB->upgrades.size();
  500. return creatureA->getNameSingularTranslated() < creatureB->getNameSingularTranslated();
  501. });
  502. int selectedIndex = parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE ? 0 : (1 + std::distance(creatures.begin(), std::find_if(creatures.begin(), creatures.end(), [this, i](auto creatureID) {
  503. return creatureID == parent.startInfo->selectedArmy[id][i].getId();
  504. })));
  505. std::vector<std::string> texts;
  506. std::vector<std::shared_ptr<IImage>> images;
  507. // Add "no creature" option
  508. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  509. images.push_back(nullptr);
  510. for (const auto & c : creatures)
  511. {
  512. texts.push_back(c.toCreature()->getNameSingularTranslated());
  513. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("CPRSMALL"), c.toCreature()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  514. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  515. images.push_back(image);
  516. }
  517. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeCreatureSelect"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeCreatureSelect"), [this, creatures, i](int index){
  518. if(index == 0)
  519. {
  520. parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(CreatureID::NONE, 1);
  521. parent.onChange();
  522. return;
  523. }
  524. index--;
  525. auto creature = creatures.at(index).toCreature();
  526. parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(creature->getId(), 100);
  527. parent.onChange();
  528. }, selectedIndex, images, true, true);
  529. window->onPopup = [creatures](int index) {
  530. if(index == 0)
  531. return;
  532. index--;
  533. ENGINE->windows().createAndPushWindow<CStackWindow>(creatures.at(index).toCreature(), true);
  534. };
  535. ENGINE->windows().pushWindow(window);
  536. });
  537. creatureImage[i]->addRClickCallback([this, i](){
  538. if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
  539. return;
  540. ENGINE->windows().createAndPushWindow<CStackWindow>(LIBRARY->creh->objects.at(parent.startInfo->selectedArmy[id][i].getId()).get(), true);
  541. });
  542. }
  543. }
  544. void BattleOnlyModeHeroSelector::setSecSkillIcons()
  545. {
  546. OBJECT_CONSTRUCTION;
  547. for(int i = 0; i < secSkillImage.size(); i++)
  548. {
  549. bool isLeft = (i % 2 == 0);
  550. int line = (i / 2);
  551. Point imgPos(261 + (isLeft ? 0 : 36), 7 + line * 54);
  552. auto skillInfo = parent.startInfo->secSkillLevel[id][i];
  553. if(skillInfo.second == MasteryLevel::NONE)
  554. {
  555. MetaString str;
  556. str.appendTextID("vcmi.lobby.battleOnlyModeSelectSkill");
  557. str.replaceNumber(i + 1);
  558. secSkillImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), str.toString(), id == 1 ? parent.boxColor : parent.disabledBoxColor), imgPos);
  559. selectedSecSkillInput[i]->disable();
  560. }
  561. else
  562. {
  563. secSkillImage[i] = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("SECSK32"), EImageBlitMode::COLORKEY)->getImage(skillInfo.first.toSkill()->getIconIndex(skillInfo.second - 1)), imgPos);
  564. selectedSecSkillInput[i]->setText(std::to_string(skillInfo.second));
  565. selectedSecSkillInput[i]->enable();
  566. }
  567. secSkillImage[i]->addLClickCallback([this, i](){
  568. auto allowedSet = LIBRARY->skillh->getDefaultAllowed();
  569. std::vector<SecondarySkill> skills(allowedSet.begin(), allowedSet.end());
  570. skills.erase( // remove already added skills from selection
  571. std::remove_if(
  572. skills.begin(),
  573. skills.end(),
  574. [this, i](auto & skill) {
  575. return std::any_of(
  576. parent.startInfo->secSkillLevel[id].begin(), parent.startInfo->secSkillLevel[id].end(),
  577. [&skill](auto & s) { return s.first == skill; }
  578. ) && parent.startInfo->secSkillLevel[id][i].first != skill;
  579. }
  580. ),
  581. skills.end()
  582. );
  583. std::sort(skills.begin(), skills.end(), [](auto a, auto b) {
  584. auto skillA = a.toSkill();
  585. auto skillB = b.toSkill();
  586. return skillA->getNameTranslated() < skillB->getNameTranslated();
  587. });
  588. int selectedIndex = parent.startInfo->secSkillLevel[id][i].second == MasteryLevel::NONE ? 0 : (1 + std::distance(skills.begin(), std::find_if(skills.begin(), skills.end(), [this, i](auto skillID) {
  589. return skillID == parent.startInfo->secSkillLevel[id][i].first;
  590. })));
  591. std::vector<std::string> texts;
  592. std::vector<std::shared_ptr<IImage>> images;
  593. // Add "no skill" option
  594. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  595. images.push_back(nullptr);
  596. for (const auto & c : skills)
  597. {
  598. texts.push_back(c.toSkill()->getNameTranslated());
  599. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("SECSK32"), c.toSkill()->getIconIndex(0), 0, EImageBlitMode::OPAQUE);
  600. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  601. images.push_back(image);
  602. }
  603. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSecSkillSelect"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSecSkillSelect"), [this, skills, i](int index){
  604. if(index == 0)
  605. {
  606. parent.startInfo->secSkillLevel[id][i] = std::make_pair(SecondarySkill::NONE, MasteryLevel::NONE);
  607. parent.onChange();
  608. return;
  609. }
  610. index--;
  611. auto skill = skills.at(index).toSkill();
  612. parent.startInfo->secSkillLevel[id][i] = std::make_pair(skill->getId(), MasteryLevel::EXPERT);
  613. parent.onChange();
  614. }, selectedIndex, images, true, true);
  615. window->onPopup = [skills](int index) {
  616. if(index == 0)
  617. return;
  618. index--;
  619. auto skillId = skills.at(index);
  620. std::shared_ptr<CComponent> comp = std::make_shared<CComponent>(ComponentType::SEC_SKILL, skillId, MasteryLevel::EXPERT);
  621. CRClickPopup::createAndPush(skillId.toSkill()->getDescriptionTranslated(MasteryLevel::EXPERT), CInfoWindow::TCompsInfo(1, comp));
  622. };
  623. ENGINE->windows().pushWindow(window);
  624. });
  625. secSkillImage[i]->addRClickCallback([this, i](){
  626. auto skillId = parent.startInfo->secSkillLevel[id][i].first;
  627. auto skillLevel = parent.startInfo->secSkillLevel[id][i].second;
  628. if(skillLevel == MasteryLevel::NONE)
  629. return;
  630. std::shared_ptr<CComponent> comp = std::make_shared<CComponent>(ComponentType::SEC_SKILL, skillId, skillLevel);
  631. CRClickPopup::createAndPush(skillId.toSkill()->getDescriptionTranslated(skillLevel), CInfoWindow::TCompsInfo(1, comp));
  632. });
  633. }
  634. }
  635. void BattleOnlyModeHeroSelector::setArtifactIcons()
  636. {
  637. OBJECT_CONSTRUCTION;
  638. std::vector<ArtifactPosition> artPos = {
  639. ArtifactPosition::HEAD, ArtifactPosition::SHOULDERS, ArtifactPosition::NECK, ArtifactPosition::RIGHT_HAND, ArtifactPosition::LEFT_HAND, ArtifactPosition::TORSO, ArtifactPosition::FEET,
  640. ArtifactPosition::RIGHT_RING, ArtifactPosition::LEFT_RING, ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5
  641. };
  642. for(int i = 0; i < artifactImage.size(); i++)
  643. {
  644. int xPos = i % 7;
  645. int yPos = i / 7;
  646. Point imgPos(6 + xPos * 36, 137 + yPos * 36);
  647. auto artifactId = parent.startInfo->artifacts[id][artPos[i]];
  648. if(artifactId == ArtifactID::NONE)
  649. {
  650. MetaString str;
  651. str.appendTextID("vcmi.lobby.battleOnlyModeSelectArtifact");
  652. str.replaceTextID("vcmi.lobby.battleOnlyModeSelectArtifact." + std::to_string(artPos[i]));
  653. artifactImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), str.toString(), id == 1 ? parent.boxColor : parent.disabledBoxColor), imgPos);
  654. }
  655. else
  656. {
  657. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("Artifact"), artifactId.toArtifact()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  658. image->scaleTo(Point(32, 32), EScalingAlgorithm::NEAREST);
  659. artifactImage[i] = std::make_shared<CPicture>(image, imgPos);
  660. }
  661. artifactImage[i]->addLClickCallback([this, i, artPos, artifactId](){
  662. auto allowedSet = LIBRARY->arth->getDefaultAllowed();
  663. std::vector<ArtifactID> artifacts(allowedSet.begin(), allowedSet.end());
  664. artifacts.erase( // remove already added and not for that slot allowed artifacts from selection
  665. std::remove_if(
  666. artifacts.begin(),
  667. artifacts.end(),
  668. [this, i, artPos](auto & artifact) {
  669. auto possibleSlots = artifact.toArtifact()->getPossibleSlots();
  670. std::vector<ArtifactPosition> allowedSlots;
  671. if(possibleSlots.find(ArtBearer::HERO) != possibleSlots.end() && !possibleSlots.at(ArtBearer::HERO).empty())
  672. allowedSlots = possibleSlots.at(ArtBearer::HERO);
  673. return (std::any_of(parent.startInfo->artifacts[id].begin(), parent.startInfo->artifacts[id].end(), [&artifact](auto & a) {return a.second == artifact;})
  674. && parent.startInfo->artifacts[id][artPos[i]] != artifact)
  675. || !std::any_of(allowedSlots.begin(), allowedSlots.end(),[i, artPos](auto & p){ return p == artPos[i]; });
  676. }
  677. ),
  678. artifacts.end()
  679. );
  680. std::sort(artifacts.begin(), artifacts.end(), [](auto a, auto b) {
  681. auto artifactA = a.toArtifact();
  682. auto artifactB = b.toArtifact();
  683. return artifactA->getNameTranslated() < artifactB->getNameTranslated();
  684. });
  685. int selectedIndex = artifactId == ArtifactID::NONE ? 0 : (1 + std::distance(artifacts.begin(), std::find_if(artifacts.begin(), artifacts.end(), [artifactId](auto artID) {
  686. return artID == artifactId;
  687. })));
  688. std::vector<std::string> texts;
  689. std::vector<std::shared_ptr<IImage>> images;
  690. // Add "no artifact" option
  691. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  692. images.push_back(nullptr);
  693. for (const auto & a : artifacts)
  694. {
  695. texts.push_back(a.toArtifact()->getNameTranslated());
  696. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("Artifact"), a.toArtifact()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  697. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  698. images.push_back(image);
  699. }
  700. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeArtifactSelect"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeArtifactSelect"), [this, artifacts, i, artPos](int index){
  701. if(index == 0)
  702. {
  703. parent.startInfo->artifacts[id][artPos[i]] = ArtifactID::NONE;
  704. parent.onChange();
  705. return;
  706. }
  707. index--;
  708. auto artifact = artifacts.at(index);
  709. parent.startInfo->artifacts[id][artPos[i]] = artifact;
  710. parent.onChange();
  711. }, selectedIndex, images, true, true);
  712. window->onPopup = [artifacts](int index) {
  713. if(index == 0)
  714. return;
  715. index--;
  716. auto artifactId = artifacts.at(index);
  717. std::shared_ptr<CComponent> comp = std::make_shared<CComponent>(ComponentType::ARTIFACT, artifactId);
  718. CRClickPopup::createAndPush(artifactId.toArtifact()->getDescriptionTranslated(), CInfoWindow::TCompsInfo(1, comp));
  719. };
  720. ENGINE->windows().pushWindow(window);
  721. });
  722. artifactImage[i]->addRClickCallback([this, i, artPos](){
  723. auto artId = parent.startInfo->artifacts[id][artPos[i]];
  724. if(artId == ArtifactID::NONE)
  725. return;
  726. std::shared_ptr<CComponent> comp = std::make_shared<CComponent>(ComponentType::ARTIFACT, artId);
  727. CRClickPopup::createAndPush(artId.toArtifact()->getDescriptionTranslated(), CInfoWindow::TCompsInfo(1, comp));
  728. });
  729. }
  730. }
  731. void BattleOnlyModeTab::startBattle()
  732. {
  733. auto rng = &CRandomGenerator::getDefault();
  734. map->initTerrain();
  735. map->getEditManager()->clearTerrain(rng);
  736. map->getEditManager()->getTerrainSelection().selectAll();
  737. map->getEditManager()->drawTerrain(startInfo->selectedTerrain == TerrainId::NONE ? TerrainId::DIRT : startInfo->selectedTerrain, 0, rng);
  738. map->players[0].canComputerPlay = true;
  739. map->players[0].canHumanPlay = true;
  740. map->players[1] = map->players[0];
  741. auto knownHeroes = LIBRARY->objtypeh->knownSubObjects(Obj::HERO);
  742. auto addHero = [&, this](int sel, PlayerColor color, const int3 & position)
  743. {
  744. auto factory = LIBRARY->objtypeh->getHandlerFor(Obj::HERO, startInfo->selectedHero[sel].toHeroType()->heroClass->getId());
  745. auto templates = factory->getTemplates();
  746. auto obj = std::dynamic_pointer_cast<CGHeroInstance>(factory->create(cb.get(), templates.front()));
  747. obj->setHeroType(startInfo->selectedHero[sel]);
  748. obj->setOwner(color);
  749. obj->pos = position;
  750. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  751. obj->pushPrimSkill(PrimarySkill(i), startInfo->primSkillLevel[sel][i]);
  752. obj->clearSlots();
  753. for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
  754. if(startInfo->selectedArmy[sel][slot].getId() != CreatureID::NONE)
  755. obj->setCreature(SlotID(slot), startInfo->selectedArmy[sel][slot].getId(), startInfo->selectedArmy[sel][slot].getCount());
  756. // give spellbook
  757. if(!obj->getArt(ArtifactPosition::SPELLBOOK) && startInfo->spellBook[sel])
  758. obj->putArtifact(ArtifactPosition::SPELLBOOK, map->createArtifact(ArtifactID::SPELLBOOK));
  759. else if(obj->getArt(ArtifactPosition::SPELLBOOK) && !startInfo->spellBook[sel])
  760. obj->removeArtifact(ArtifactPosition::SPELLBOOK);
  761. if(startInfo->warMachines[sel])
  762. {
  763. obj->putArtifact(ArtifactPosition::MACH1, map->createArtifact(ArtifactID::BALLISTA));
  764. obj->putArtifact(ArtifactPosition::MACH2, map->createArtifact(ArtifactID::AMMO_CART));
  765. obj->putArtifact(ArtifactPosition::MACH3, map->createArtifact(ArtifactID::FIRST_AID_TENT));
  766. }
  767. for(const auto & spell : startInfo->spells[sel])
  768. obj->addSpellToSpellbook(spell);
  769. for(auto & artifact : startInfo->artifacts[sel])
  770. if(artifact.second != ArtifactID::NONE)
  771. obj->putArtifact(artifact.first, map->createArtifact(artifact.second));
  772. bool skillSetted = std::any_of(startInfo->secSkillLevel[sel].begin(), startInfo->secSkillLevel[sel].end(),[](const auto& p){ return p.second != MasteryLevel::NONE; });
  773. if(skillSetted)
  774. {
  775. for(const auto & skill : LIBRARY->skillh->objects) // reset all standard skills
  776. obj->setSecSkillLevel(SecondarySkill(skill->getId()), MasteryLevel::NONE, ChangeValueMode::ABSOLUTE);
  777. for(int skillSlot = 0; skillSlot < 8; skillSlot++)
  778. obj->setSecSkillLevel(startInfo->secSkillLevel[sel][skillSlot].first, startInfo->secSkillLevel[sel][skillSlot].second, ChangeValueMode::ABSOLUTE);
  779. }
  780. map->getEditManager()->insertObject(obj);
  781. };
  782. addHero(0, PlayerColor(0), int3(5, 6, 0));
  783. if(startInfo->selectedTown == FactionID::ANY)
  784. addHero(1, PlayerColor(1), int3(5, 5, 0));
  785. else
  786. {
  787. auto factory = LIBRARY->objtypeh->getHandlerFor(Obj::TOWN, startInfo->selectedTown);
  788. auto templates = factory->getTemplates();
  789. auto obj = factory->create(cb.get(), templates.front());
  790. auto townObj = std::dynamic_pointer_cast<CGTownInstance>(obj);
  791. obj->setOwner(PlayerColor(1));
  792. obj->pos = int3(5, 5, 0);
  793. for (const auto & building : townObj->getTown()->getAllBuildings())
  794. townObj->addBuilding(building);
  795. if(startInfo->selectedHero[1] == HeroTypeID::NONE)
  796. {
  797. for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
  798. if(startInfo->selectedArmy[1][slot].getId() != CreatureID::NONE)
  799. townObj->getArmy()->setCreature(SlotID(slot), startInfo->selectedArmy[1][slot].getId(), startInfo->selectedArmy[1][slot].getCount());
  800. }
  801. else
  802. addHero(1, PlayerColor(1), int3(5, 5, 0));
  803. map->getEditManager()->insertObject(townObj);
  804. }
  805. auto path = VCMIDirs::get().userDataPath() / "Maps";
  806. boost::filesystem::create_directories(path);
  807. const std::string fileName = "BattleOnlyMode.vmap";
  808. const auto fullPath = path / fileName;
  809. CMapService mapService;
  810. mapService.saveMap(map, fullPath);
  811. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount) { return true; });
  812. auto mapInfo = std::make_shared<CMapInfo>();
  813. mapInfo->mapInit("Maps/BattleOnlyMode");
  814. GAME->server().setMapInfo(mapInfo);
  815. ExtraOptionsInfo extraOptions;
  816. extraOptions.unlimitedReplay = true;
  817. GAME->server().setExtraOptionsInfo(extraOptions);
  818. GAME->server().sendStartGame();
  819. }