BattleOnlyMode.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * BattleOnlyMode.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 "BattleOnlyMode.h"
  12. #include "../CServerHandler.h"
  13. #include "../GameEngine.h"
  14. #include "../GameInstance.h"
  15. #include "../render/IRenderHandler.h"
  16. #include "../render/CAnimation.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/CanvasImage.h"
  19. #include "../gui/Shortcut.h"
  20. #include "../gui/WindowHandler.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/GraphicalPrimitiveCanvas.h"
  23. #include "../widgets/TextControls.h"
  24. #include "../widgets/CTextInput.h"
  25. #include "../widgets/Images.h"
  26. #include "../windows/GUIClasses.h"
  27. #include "../windows/CHeroOverview.h"
  28. #include "../windows/CCreatureWindow.h"
  29. #include "../../lib/GameLibrary.h"
  30. #include "../../lib/gameState/CGameState.h"
  31. #include "../../lib/networkPacks/PacksForLobby.h"
  32. #include "../../lib/StartInfo.h"
  33. #include "../../lib/VCMIDirs.h"
  34. #include "../../lib/CRandomGenerator.h"
  35. #include "../../lib/callback/EditorCallback.h"
  36. #include "../../lib/entities/hero/CHero.h"
  37. #include "../../lib/entities/hero/CHeroClass.h"
  38. #include "../../lib/entities/hero/CHeroHandler.h"
  39. #include "../../lib/entities/faction/CTown.h"
  40. #include "../../lib/entities/faction/CTownHandler.h"
  41. #include "../../lib/mapObjects/CGHeroInstance.h"
  42. #include "../../lib/mapObjects/CGTownInstance.h"
  43. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  44. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  45. #include "../../lib/mapping/CMap.h"
  46. #include "../../lib/mapping/CMapInfo.h"
  47. #include "../../lib/mapping/CMapEditManager.h"
  48. #include "../../lib/mapping/CMapService.h"
  49. #include "../../lib/mapping/MapFormat.h"
  50. #include "../../lib/texts/CGeneralTextHandler.h"
  51. #include "../../lib/texts/MetaString.h"
  52. #include "../../lib/texts/TextOperations.h"
  53. #include "../../lib/filesystem/Filesystem.h"
  54. void BattleOnlyMode::openBattleWindow()
  55. {
  56. GAME->server().sendGuiAction(LobbyGuiAction::BATTLE_MODE);
  57. ENGINE->windows().createAndPushWindow<BattleOnlyModeWindow>();
  58. }
  59. BattleOnlyModeWindow::BattleOnlyModeWindow()
  60. : CWindowObject(BORDERED)
  61. , startInfo(std::make_shared<BattleOnlyModeStartInfo>())
  62. , disabledColor(GAME->server().isHost() ? Colors::WHITE : Colors::ORANGE)
  63. {
  64. OBJECT_CONSTRUCTION;
  65. pos.w = 519;
  66. pos.h = 238;
  67. updateShadow();
  68. center();
  69. init();
  70. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  71. backgroundTexture->setPlayerColor(PlayerColor(1));
  72. buttonOk = std::make_shared<CButton>(Point(191, 203), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ startBattle(); }, EShortcut::GLOBAL_ACCEPT);
  73. buttonOk->block(true);
  74. buttonAbort = std::make_shared<CButton>(Point(265, 203), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){
  75. GAME->server().sendGuiAction(LobbyGuiAction::NO_TAB);
  76. close();
  77. }, EShortcut::GLOBAL_CANCEL);
  78. buttonAbort->block(true);
  79. title = std::make_shared<CLabel>(260, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyMode"));
  80. battlefieldSelector = std::make_shared<CButton>(Point(29, 174), AnimationPath::builtin("GSPButtonClear"), CButton::tooltip(), [this](){
  81. std::vector<std::string> texts;
  82. std::vector<std::shared_ptr<IImage>> images;
  83. auto & terrains = LIBRARY->terrainTypeHandler->objects;
  84. for (const auto & terrain : terrains)
  85. {
  86. if(!terrain->isPassable())
  87. continue;
  88. texts.push_back(terrain->getNameTranslated());
  89. const auto & patterns = LIBRARY->terviewh->getTerrainViewPatterns(terrain->getId());
  90. TerrainViewPattern pattern;
  91. for(auto & p : patterns)
  92. if(p[0].id == "n1")
  93. pattern = p[0];
  94. auto image = ENGINE->renderHandler().loadImage(terrain->tilesFilename, pattern.mapping[0].first, 0, EImageBlitMode::OPAQUE);
  95. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  96. images.push_back(image);
  97. }
  98. auto factions = LIBRARY->townh->getDefaultAllowed();
  99. for (const auto & faction : factions)
  100. {
  101. texts.push_back(faction.toFaction()->getNameTranslated());
  102. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("ITPA"), faction.toFaction()->town->clientInfo.icons[true][false] + 2, 0, EImageBlitMode::OPAQUE);
  103. image->scaleTo(Point(35, 23), EScalingAlgorithm::NEAREST);
  104. images.push_back(image);
  105. }
  106. ENGINE->windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefield"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefieldSelect"), [this, terrains, factions](int index){
  107. if(terrains.size() > index)
  108. {
  109. startInfo->selectedTerrain = terrains[index]->getId();
  110. startInfo->selectedTown = std::nullopt;
  111. }
  112. else
  113. {
  114. startInfo->selectedTerrain = std::nullopt;
  115. auto it = std::next(factions.begin(), index - terrains.size());
  116. if (it != factions.end())
  117. startInfo->selectedTown = *it;
  118. }
  119. onChange();
  120. }, (startInfo->selectedTerrain ? static_cast<int>(*startInfo->selectedTerrain) : static_cast<int>(*startInfo->selectedTown + terrains.size())), images, true, true);
  121. });
  122. battlefieldSelector->block(GAME->server().isGuest());
  123. buttonReset = std::make_shared<CButton>(Point(289, 174), AnimationPath::builtin("GSPButtonClear"), CButton::tooltip(), [this](){
  124. if(GAME->server().isHost())
  125. {
  126. startInfo->selectedTerrain = TerrainId::DIRT;
  127. startInfo->selectedTown = std::nullopt;
  128. startInfo->selectedHero[0] = std::nullopt;
  129. startInfo->selectedArmy[0].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
  130. for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
  131. heroSelector1->selectedArmyInput.at(i)->disable();
  132. }
  133. startInfo->selectedHero[1] = std::nullopt;
  134. startInfo->selectedArmy[1].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
  135. for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
  136. heroSelector2->selectedArmyInput.at(i)->disable();
  137. onChange();
  138. });
  139. buttonReset->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeReset"), EFonts::FONT_SMALL, Colors::WHITE);
  140. heroSelector1 = std::make_shared<BattleOnlyModeHeroSelector>(0, *this, Point(0, 40));
  141. heroSelector2 = std::make_shared<BattleOnlyModeHeroSelector>(1, *this, Point(260, 40));
  142. heroSelector1->setInputEnabled(GAME->server().isHost());
  143. onChange();
  144. }
  145. void BattleOnlyModeWindow::init()
  146. {
  147. map = std::make_unique<CMap>(nullptr);
  148. map->version = EMapFormat::VCMI;
  149. map->creationDateTime = std::time(nullptr);
  150. map->width = 10;
  151. map->height = 10;
  152. map->mapLevels = 1;
  153. map->battleOnly = true;
  154. map->name = MetaString::createFromTextID("vcmi.lobby.battleOnlyMode");
  155. cb = std::make_unique<EditorCallback>(map.get());
  156. }
  157. void BattleOnlyModeWindow::onChange()
  158. {
  159. GAME->server().setBattleOnlyModeStartInfo(startInfo);
  160. }
  161. void BattleOnlyModeWindow::update()
  162. {
  163. setTerrainButtonText();
  164. setOkButtonEnabled();
  165. heroSelector1->setHeroIcon();
  166. heroSelector1->setCreatureIcons();
  167. heroSelector2->setHeroIcon();
  168. heroSelector2->setCreatureIcons();
  169. redraw();
  170. }
  171. void BattleOnlyModeWindow::applyStartInfo(std::shared_ptr<BattleOnlyModeStartInfo> si)
  172. {
  173. startInfo = si;
  174. update();
  175. }
  176. void BattleOnlyModeWindow::setTerrainButtonText()
  177. {
  178. battlefieldSelector->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeBattlefield") + ": " + (startInfo->selectedTerrain ? (*startInfo->selectedTerrain).toEntity(LIBRARY)->getNameTranslated() : (*startInfo->selectedTown).toEntity(LIBRARY)->getNameTranslated()), EFonts::FONT_SMALL, disabledColor);
  179. }
  180. void BattleOnlyModeWindow::setOkButtonEnabled()
  181. {
  182. bool army2Empty = std::all_of(startInfo->selectedArmy[1].begin(), startInfo->selectedArmy[1].end(), [](const auto x) { return x.getId() == CreatureID::NONE; });
  183. bool canStart = (startInfo->selectedTerrain || startInfo->selectedTown);
  184. canStart &= (startInfo->selectedHero[0] && ((startInfo->selectedHero[1]) || (startInfo->selectedTown && !army2Empty)));
  185. buttonOk->block(!canStart || GAME->server().isGuest());
  186. buttonAbort->block(GAME->server().isGuest());
  187. }
  188. std::shared_ptr<IImage> drawBlackBox(Point size, std::string text, ColorRGBA color)
  189. {
  190. auto image = ENGINE->renderHandler().createImage(size, CanvasScalingPolicy::AUTO);
  191. Canvas canvas = image->getCanvas();
  192. canvas.drawColor(Rect(0, 0, size.x, size.y), Colors::BLACK);
  193. canvas.drawText(Point(size.x / 2, size.y / 2), FONT_TINY, color, ETextAlignment::CENTER, text);
  194. return image;
  195. }
  196. BattleOnlyModeHeroSelector::BattleOnlyModeHeroSelector(int id, BattleOnlyModeWindow& p, Point position)
  197. : parent(p)
  198. , id(id)
  199. {
  200. OBJECT_CONSTRUCTION;
  201. pos.x += position.x;
  202. pos.y += position.y;
  203. backgroundImage = std::make_shared<CPicture>(ImagePath::builtin("heroSlotsBlue"), Point(3, 4));
  204. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  205. {
  206. auto image = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL32"), i, 0, 78 + i * 36, 26);
  207. primSkills.push_back(image);
  208. primSkillsBorder.push_back(std::make_shared<GraphicalPrimitiveCanvas>(Rect(78 + i * 36, 26, 32, 32)));
  209. primSkillsBorder.back()->addRectangle(Point(0, 0), Point(32, 32), ColorRGBA(44, 108, 255));
  210. primSkillsInput.push_back(std::make_shared<CTextInput>(Rect(78 + i * 36, 58, 32, 16), EFonts::FONT_SMALL, ETextAlignment::CENTER, false));
  211. primSkillsInput.back()->setColor(id == 1 ? Colors::WHITE : parent.disabledColor);
  212. primSkillsInput.back()->setFilterNumber(0, 100);
  213. primSkillsInput.back()->setText("0");
  214. primSkillsInput.back()->setCallback([this, i, id](const std::string & text){
  215. parent.startInfo->primSkillLevel[id][i] = std::stoi(primSkillsInput[i]->getText());
  216. parent.onChange();
  217. });
  218. }
  219. creatureImage.resize(GameConstants::ARMY_SIZE);
  220. for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
  221. {
  222. selectedArmyInput.push_back(std::make_shared<CTextInput>(Rect(5 + i * 36, 113, 32, 16), EFonts::FONT_SMALL, ETextAlignment::CENTER, false));
  223. selectedArmyInput.back()->setColor(id == 1 ? Colors::WHITE : parent.disabledColor);
  224. selectedArmyInput.back()->setFilterNumber(1, 10000000, 3);
  225. selectedArmyInput.back()->setText("1");
  226. selectedArmyInput.back()->setCallback([this, i, id](const std::string & text){
  227. if(parent.startInfo->selectedArmy[id][i].getId() != CreatureID::NONE)
  228. {
  229. parent.startInfo->selectedArmy[id][i].setCount(TextOperations::parseMetric<int>(text));
  230. parent.onChange();
  231. selectedArmyInput[i]->enable();
  232. }
  233. else
  234. selectedArmyInput[i]->disable();
  235. });
  236. }
  237. setHeroIcon();
  238. setCreatureIcons();
  239. }
  240. void BattleOnlyModeHeroSelector::setHeroIcon()
  241. {
  242. OBJECT_CONSTRUCTION;
  243. if(!parent.startInfo->selectedHero[id])
  244. {
  245. heroImage = std::make_shared<CPicture>(drawBlackBox(Point(58, 64), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSelect"), id == 1 ? Colors::WHITE : parent.disabledColor), Point(6, 7));
  246. heroLabel = std::make_shared<CLabel>(160, 16, FONT_SMALL, ETextAlignment::CENTER, id == 1 ? Colors::WHITE : parent.disabledColor, LIBRARY->generaltexth->translate("core.genrltxt.507"));
  247. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  248. primSkillsInput[i]->setText("0");
  249. }
  250. else
  251. {
  252. heroImage = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("PortraitsLarge"), EImageBlitMode::COLORKEY)->getImage((*parent.startInfo->selectedHero[id]).toHeroType()->imageIndex), Point(6, 7));
  253. heroLabel = std::make_shared<CLabel>(160, 16, FONT_SMALL, ETextAlignment::CENTER, id == 1 ? Colors::WHITE : parent.disabledColor, (*parent.startInfo->selectedHero[id]).toHeroType()->getNameTranslated());
  254. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  255. primSkillsInput[i]->setText(std::to_string(parent.startInfo->primSkillLevel[id][i]));
  256. }
  257. heroImage->addLClickCallback([this](){
  258. auto allowedSet = LIBRARY->heroh->getDefaultAllowed();
  259. std::vector<HeroTypeID> heroes(allowedSet.begin(), allowedSet.end());
  260. std::sort(heroes.begin(), heroes.end(), [](auto a, auto b) {
  261. auto heroA = a.toHeroType();
  262. auto heroB = b.toHeroType();
  263. if(heroA->heroClass->faction != heroB->heroClass->faction)
  264. return heroA->heroClass->faction < heroB->heroClass->faction;
  265. if(heroA->heroClass->getId() != heroB->heroClass->getId())
  266. return heroA->heroClass->getId() < heroB->heroClass->getId();
  267. return heroA->getNameTranslated() < heroB->getNameTranslated();
  268. });
  269. int selectedIndex = !parent.startInfo->selectedHero[id] ? 0 : (1 + std::distance(heroes.begin(), std::find_if(heroes.begin(), heroes.end(), [this](auto heroID) {
  270. return heroID == (*parent.startInfo->selectedHero[id]);
  271. })));
  272. std::vector<std::string> texts;
  273. std::vector<std::shared_ptr<IImage>> images;
  274. // Add "no hero" option
  275. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  276. images.push_back(nullptr);
  277. for (const auto & h : heroes)
  278. {
  279. texts.push_back(h.toHeroType()->getNameTranslated());
  280. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("PortraitsSmall"), h.toHeroType()->imageIndex, 0, EImageBlitMode::OPAQUE);
  281. image->scaleTo(Point(35, 23), EScalingAlgorithm::NEAREST);
  282. images.push_back(image);
  283. }
  284. auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeHeroSelect"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeHeroSelect"), [this, heroes](int index){
  285. if(index == 0)
  286. {
  287. parent.startInfo->selectedHero[id] = std::nullopt;
  288. parent.onChange();
  289. return;
  290. }
  291. index--;
  292. parent.startInfo->selectedHero[id] = heroes[index];
  293. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  294. parent.startInfo->primSkillLevel[id][i] = 0;
  295. parent.onChange();
  296. }, selectedIndex, images, true, true);
  297. window->onPopup = [heroes](int index) {
  298. if(index == 0)
  299. return;
  300. index--;
  301. ENGINE->windows().createAndPushWindow<CHeroOverview>(heroes.at(index));
  302. };
  303. ENGINE->windows().pushWindow(window);
  304. });
  305. heroImage->addRClickCallback([this](){
  306. if(!parent.startInfo->selectedHero[id])
  307. return;
  308. ENGINE->windows().createAndPushWindow<CHeroOverview>(parent.startInfo->selectedHero[id]->toHeroType()->getId());
  309. });
  310. }
  311. void BattleOnlyModeHeroSelector::setCreatureIcons()
  312. {
  313. OBJECT_CONSTRUCTION;
  314. for(int i = 0; i < creatureImage.size(); i++)
  315. {
  316. if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
  317. {
  318. creatureImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSelect"), id == 1 ? Colors::WHITE : parent.disabledColor), Point(6 + i * 36, 78));
  319. selectedArmyInput[i]->disable();
  320. }
  321. else
  322. {
  323. auto unit = parent.startInfo->selectedArmy[id][i];
  324. auto creatureID = unit.getId();
  325. 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));
  326. selectedArmyInput[i]->setText(TextOperations::formatMetric(unit.getCount(), 3));
  327. selectedArmyInput[i]->enable();
  328. }
  329. creatureImage[i]->addLClickCallback([this, i](){
  330. auto allowedSet = LIBRARY->creh->getDefaultAllowed();
  331. std::vector<CreatureID> creatures(allowedSet.begin(), allowedSet.end());
  332. std::sort(creatures.begin(), creatures.end(), [](auto a, auto b) {
  333. auto creatureA = a.toCreature();
  334. auto creatureB = b.toCreature();
  335. if(creatureA->getFactionID() != creatureB->getFactionID())
  336. return creatureA->getFactionID() < creatureB->getFactionID();
  337. if(creatureA->getLevel() != creatureB->getLevel())
  338. return creatureA->getLevel() < creatureB->getLevel();
  339. if(creatureA->upgrades.size() != creatureB->upgrades.size())
  340. return creatureA->upgrades.size() > creatureB->upgrades.size();
  341. return creatureA->getNameSingularTranslated() < creatureB->getNameSingularTranslated();
  342. });
  343. 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) {
  344. return creatureID == parent.startInfo->selectedArmy[id][i].getId();
  345. })));
  346. std::vector<std::string> texts;
  347. std::vector<std::shared_ptr<IImage>> images;
  348. // Add "no creature" option
  349. texts.push_back(LIBRARY->generaltexth->translate("core.genrltxt.507"));
  350. images.push_back(nullptr);
  351. for (const auto & c : creatures)
  352. {
  353. texts.push_back(c.toCreature()->getNameSingularTranslated());
  354. auto image = ENGINE->renderHandler().loadImage(AnimationPath::builtin("CPRSMALL"), c.toCreature()->getIconIndex(), 0, EImageBlitMode::OPAQUE);
  355. image->scaleTo(Point(23, 23), EScalingAlgorithm::NEAREST);
  356. images.push_back(image);
  357. }
  358. 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){
  359. if(index == 0)
  360. {
  361. parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(CreatureID::NONE, 1);
  362. parent.onChange();
  363. return;
  364. }
  365. index--;
  366. auto creature = creatures.at(index).toCreature();
  367. parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(creature->getId(), 100);
  368. parent.onChange();
  369. }, selectedIndex, images, true, true);
  370. window->onPopup = [creatures](int index) {
  371. if(index == 0)
  372. return;
  373. index--;
  374. ENGINE->windows().createAndPushWindow<CStackWindow>(creatures.at(index).toCreature(), true);
  375. };
  376. ENGINE->windows().pushWindow(window);
  377. });
  378. creatureImage[i]->addRClickCallback([this, i](){
  379. if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
  380. return;
  381. ENGINE->windows().createAndPushWindow<CStackWindow>(LIBRARY->creh->objects.at(parent.startInfo->selectedArmy[id][i].getId()).get(), true);
  382. });
  383. }
  384. }
  385. void BattleOnlyModeWindow::startBattle()
  386. {
  387. auto rng = &CRandomGenerator::getDefault();
  388. map->initTerrain();
  389. map->getEditManager()->clearTerrain(rng);
  390. map->getEditManager()->getTerrainSelection().selectAll();
  391. map->getEditManager()->drawTerrain(!startInfo->selectedTerrain ? TerrainId::DIRT : *startInfo->selectedTerrain, 0, rng);
  392. map->players[0].canComputerPlay = true;
  393. map->players[0].canHumanPlay = true;
  394. map->players[1] = map->players[0];
  395. auto knownHeroes = LIBRARY->objtypeh->knownSubObjects(Obj::HERO);
  396. auto addHero = [&, this](int sel, PlayerColor color, const int3 & position)
  397. {
  398. auto factory = LIBRARY->objtypeh->getHandlerFor(Obj::HERO, (*startInfo->selectedHero[sel]).toHeroType()->heroClass->getId());
  399. auto templates = factory->getTemplates();
  400. auto obj = std::dynamic_pointer_cast<CGHeroInstance>(factory->create(cb.get(), templates.front()));
  401. obj->setHeroType(*startInfo->selectedHero[sel]);
  402. obj->setOwner(color);
  403. obj->pos = position;
  404. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  405. obj->pushPrimSkill(PrimarySkill(i), startInfo->primSkillLevel[sel][i]);
  406. obj->clearSlots();
  407. for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
  408. if(startInfo->selectedArmy[sel][slot].getId() != CreatureID::NONE)
  409. obj->setCreature(SlotID(slot), startInfo->selectedArmy[sel][slot].getId(), startInfo->selectedArmy[sel][slot].getCount());
  410. map->getEditManager()->insertObject(obj);
  411. };
  412. addHero(0, PlayerColor(0), int3(5, 6, 0));
  413. if(!startInfo->selectedTown)
  414. addHero(1, PlayerColor(1), int3(5, 5, 0));
  415. else
  416. {
  417. auto factory = LIBRARY->objtypeh->getHandlerFor(Obj::TOWN, *startInfo->selectedTown);
  418. auto templates = factory->getTemplates();
  419. auto obj = factory->create(cb.get(), templates.front());
  420. auto townObj = std::dynamic_pointer_cast<CGTownInstance>(obj);
  421. obj->setOwner(PlayerColor(1));
  422. obj->pos = int3(5, 5, 0);
  423. for (const auto & building : townObj->getTown()->getAllBuildings())
  424. townObj->addBuilding(building);
  425. if(!startInfo->selectedHero[1])
  426. {
  427. for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
  428. if(startInfo->selectedArmy[1][slot].getId() != CreatureID::NONE)
  429. townObj->getArmy()->setCreature(SlotID(slot), startInfo->selectedArmy[1][slot].getId(), startInfo->selectedArmy[1][slot].getCount());
  430. }
  431. else
  432. addHero(1, PlayerColor(1), int3(5, 5, 0));
  433. map->getEditManager()->insertObject(townObj);
  434. }
  435. auto path = VCMIDirs::get().userDataPath() / "Maps";
  436. boost::filesystem::create_directories(path);
  437. const std::string fileName = "BattleOnlyMode.vmap";
  438. const auto fullPath = path / fileName;
  439. CMapService mapService;
  440. mapService.saveMap(map, fullPath);
  441. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount) { return true; });
  442. auto mapInfo = std::make_shared<CMapInfo>();
  443. mapInfo->mapInit("Maps/BattleOnlyMode");
  444. GAME->server().setMapInfo(mapInfo);
  445. ExtraOptionsInfo extraOptions;
  446. extraOptions.unlimitedReplay = true;
  447. GAME->server().setExtraOptionsInfo(extraOptions);
  448. GAME->server().sendStartGame();
  449. }