BattleOnlyMode.cpp 19 KB

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