BattleOnlyMode.cpp 20 KB

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