BattleOnlyMode.cpp 20 KB

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