BattleOnlyMode.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "../gui/Shortcut.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/Images.h"
  19. #include "../../lib/gameState/CGameState.h"
  20. #include "../../lib/StartInfo.h"
  21. #include "../../lib/VCMIDirs.h"
  22. #include "../../lib/CRandomGenerator.h"
  23. #include "../../lib/callback/EditorCallback.h"
  24. #include "../../lib/mapObjects/CGHeroInstance.h"
  25. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  26. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  27. #include "../../lib/mapping/CMap.h"
  28. #include "../../lib/mapping/CMapInfo.h"
  29. #include "../../lib/mapping/CMapEditManager.h"
  30. #include "../../lib/mapping/CMapService.h"
  31. #include "../../lib/mapping/MapFormat.h"
  32. #include "../../lib/filesystem/Filesystem.h"
  33. void BattleOnlyMode::openBattleWindow()
  34. {
  35. ENGINE->windows().createAndPushWindow<BattleOnlyModeWindow>();
  36. }
  37. BattleOnlyModeWindow::BattleOnlyModeWindow()
  38. : CWindowObject(BORDERED)
  39. {
  40. OBJECT_CONSTRUCTION;
  41. pos.w = 640;
  42. pos.h = 480;
  43. updateShadow();
  44. center();
  45. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  46. backgroundTexture->setPlayerColor(PlayerColor(1));
  47. buttonOk = std::make_shared<CButton>(Point(183, 388), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ startBattle(); }, EShortcut::GLOBAL_ACCEPT);
  48. buttonAbort = std::make_shared<CButton>(Point(250, 388), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_CANCEL);
  49. }
  50. void BattleOnlyModeWindow::startBattle()
  51. {
  52. auto map = std::make_unique<CMap>(nullptr);
  53. map->version = EMapFormat::VCMI;
  54. map->creationDateTime = std::time(nullptr);
  55. map->width = 4;
  56. map->height = 3;
  57. map->mapLevels = 1;
  58. map->battleOnly = true;
  59. map->initTerrain();
  60. map->getEditManager()->clearTerrain(&CRandomGenerator::getDefault());
  61. //auto terrain = LIBRARY->terrainTypeHandler->objects;
  62. map->getEditManager()->getTerrainSelection().selectAll();
  63. map->getEditManager()->drawTerrain(TerrainId::GRASS, 0, &CRandomGenerator::getDefault());
  64. map->players[0].canComputerPlay = true;
  65. map->players[0].canHumanPlay = true;
  66. map->players[1] = map->players[0];
  67. auto cb = std::make_unique<EditorCallback>(map.get());
  68. auto knownHeroes = LIBRARY->objtypeh->knownSubObjects(Obj::HERO);
  69. auto addHero = [&](MapObjectSubID heroType, PlayerColor color, const int3 & position)
  70. {
  71. auto it = knownHeroes.find(heroType);
  72. if (it == knownHeroes.end())
  73. return; // unknown hero type, nothing to add
  74. auto firstHeroType = *it;
  75. auto factory = LIBRARY->objtypeh->getHandlerFor(Obj::HERO, firstHeroType);
  76. auto templates = factory->getTemplates();
  77. auto obj = std::dynamic_pointer_cast<CGHeroInstance>(factory->create(cb.get(), templates.front()));
  78. obj->setOwner(color);
  79. obj->pos = position;
  80. /*if(heroType == MapObjectSubID(1))
  81. {*/
  82. obj->pushPrimSkill(PrimarySkill::ATTACK, 100);
  83. obj->getArmy()->setCreature(SlotID(0), CreatureID(1), 10000);
  84. //}
  85. map->getEditManager()->insertObject(obj);
  86. };
  87. addHero(MapObjectSubID(1), PlayerColor(0), int3(2, 1, 0));
  88. addHero(MapObjectSubID(2), PlayerColor(1), int3(3, 1, 0));
  89. auto path = VCMIDirs::get().userDataPath() / "Maps";
  90. boost::filesystem::create_directories(path);
  91. const std::string fileName = "BattleOnlyMode.vmap";
  92. const auto fullPath = path / fileName;
  93. CMapService mapService;
  94. mapService.saveMap(map, fullPath);
  95. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount) { return true; });
  96. auto mapInfo = std::make_shared<CMapInfo>();
  97. mapInfo->mapInit("Maps/BattleOnlyMode");
  98. GAME->server().setMapInfo(mapInfo);
  99. ExtraOptionsInfo extraOptions;
  100. extraOptions.unlimitedReplay = true;
  101. GAME->server().setExtraOptionsInfo(extraOptions);
  102. GAME->server().sendStartGame();
  103. }