CGameStateTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * CGameStateTest.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 "mock/mock_Services.h"
  12. #include "mock/mock_MapService.h"
  13. #include "mock/mock_IGameCallback.h"
  14. #include "mock/mock_spells_Problem.h"
  15. #include "../../lib/VCMIDirs.h"
  16. #include "../../lib/json/JsonUtils.h"
  17. #include "../../lib/gameState/CGameState.h"
  18. #include "../../lib/networkPacks/PacksForClient.h"
  19. #include "../../lib/networkPacks/PacksForClientBattle.h"
  20. #include "../../lib/networkPacks/SetStackEffect.h"
  21. #include "../../lib/StartInfo.h"
  22. #include "../../lib/TerrainHandler.h"
  23. #include "../../lib/battle/BattleInfo.h"
  24. #include "../../lib/battle/BattleLayout.h"
  25. #include "../../lib/CStack.h"
  26. #include "../../lib/filesystem/ResourcePath.h"
  27. #include "../../lib/mapping/CMap.h"
  28. #include "../../lib/spells/CSpellHandler.h"
  29. #include "../../lib/spells/ISpellMechanics.h"
  30. #include "../../lib/spells/AbilityCaster.h"
  31. class CGameStateTest : public ::testing::Test, public SpellCastEnvironment, public MapListener
  32. {
  33. public:
  34. CGameStateTest()
  35. : gameCallback(new GameCallbackMock(this)),
  36. mapService("test/MiniTest/", this),
  37. map(nullptr)
  38. {
  39. }
  40. void SetUp() override
  41. {
  42. gameState = std::make_shared<CGameState>();
  43. gameCallback->setGameState(gameState.get());
  44. gameState->preInit(&services, gameCallback.get());
  45. }
  46. void TearDown() override
  47. {
  48. gameState.reset();
  49. }
  50. bool describeChanges() const override
  51. {
  52. return true;
  53. }
  54. void apply(CPackForClient * pack) override
  55. {
  56. gameState->apply(pack);
  57. }
  58. void apply(BattleLogMessage * pack) override
  59. {
  60. gameState->apply(pack);
  61. }
  62. void apply(BattleStackMoved * pack) override
  63. {
  64. gameState->apply(pack);
  65. }
  66. void apply(BattleUnitsChanged * pack) override
  67. {
  68. gameState->apply(pack);
  69. }
  70. void apply(SetStackEffect * pack) override
  71. {
  72. gameState->apply(pack);
  73. }
  74. void apply(StacksInjured * pack) override
  75. {
  76. gameState->apply(pack);
  77. }
  78. void apply(BattleObstaclesChanged * pack) override
  79. {
  80. gameState->apply(pack);
  81. }
  82. void apply(CatapultAttack * pack) override
  83. {
  84. gameState->apply(pack);
  85. }
  86. void complain(const std::string & problem) override
  87. {
  88. FAIL() << "Server-side assertion: " << problem;
  89. };
  90. vstd::RNG * getRNG() override
  91. {
  92. return &gameState->getRandomGenerator();//todo: mock this
  93. }
  94. const CMap * getMap() const override
  95. {
  96. return map;
  97. }
  98. const CGameInfoCallback * getCb() const override
  99. {
  100. return gameState.get();
  101. }
  102. void createBoat(const int3 & visitablePosition, BoatId type, PlayerColor initiator) override
  103. {
  104. }
  105. bool moveHero(ObjectInstanceID hid, int3 dst, EMovementMode movementMode) override
  106. {
  107. return false;
  108. }
  109. void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) override
  110. {
  111. //todo:
  112. }
  113. void mapLoaded(CMap * map) override
  114. {
  115. EXPECT_EQ(this->map, nullptr);
  116. this->map = map;
  117. }
  118. void startTestGame()
  119. {
  120. StartInfo si;
  121. si.mapname = "anything";//does not matter, map service mocked
  122. si.difficulty = 0;
  123. si.mode = EStartMode::NEW_GAME;
  124. std::unique_ptr<CMapHeader> header = mapService.loadMapHeader(ResourcePath(si.mapname));
  125. ASSERT_NE(header.get(), nullptr);
  126. //FIXME: this has been copied from CPreGame, but should be part of StartInfo
  127. for(int i = 0; i < header->players.size(); i++)
  128. {
  129. const PlayerInfo & pinfo = header->players[i];
  130. //neither computer nor human can play - no player
  131. if (!(pinfo.canHumanPlay || pinfo.canComputerPlay))
  132. continue;
  133. PlayerSettings & pset = si.playerInfos[PlayerColor(i)];
  134. pset.color = PlayerColor(i);
  135. pset.connectedPlayerIDs.insert(i);
  136. pset.name = "Player";
  137. pset.castle = pinfo.defaultCastle();
  138. pset.hero = pinfo.defaultHero();
  139. if(pset.hero != HeroTypeID::RANDOM && pinfo.hasCustomMainHero())
  140. {
  141. pset.hero = pinfo.mainCustomHeroId;
  142. pset.heroNameTextId = pinfo.mainCustomHeroNameTextId;
  143. pset.heroPortrait = HeroTypeID(pinfo.mainCustomHeroPortrait);
  144. }
  145. }
  146. Load::ProgressAccumulator progressTracker;
  147. gameState->init(&mapService, &si, progressTracker, false);
  148. ASSERT_NE(map, nullptr);
  149. ASSERT_EQ(map->heroesOnMap.size(), 2);
  150. }
  151. void startTestBattle(const CGHeroInstance * attacker, const CGHeroInstance * defender)
  152. {
  153. BattleSideArray<const CGHeroInstance *> heroes = {attacker, defender};
  154. BattleSideArray<const CArmedInstance *> armedInstancies = {attacker, defender};
  155. int3 tile(4,4,0);
  156. const auto & t = *gameCallback->getTile(tile);
  157. auto terrain = t.terType->getId();
  158. BattleField terType(0);
  159. BattleLayout layout = BattleLayout::createDefaultLayout(gameState->callback, attacker, defender);
  160. //send info about battles
  161. BattleInfo * battle = BattleInfo::setupBattle(tile, terrain, terType, armedInstancies, heroes, layout, nullptr);
  162. BattleStart bs;
  163. bs.info = battle;
  164. ASSERT_EQ(gameState->currentBattles.size(), 0);
  165. gameCallback->sendAndApply(&bs);
  166. ASSERT_EQ(gameState->currentBattles.size(), 1);
  167. }
  168. std::shared_ptr<CGameState> gameState;
  169. std::shared_ptr<GameCallbackMock> gameCallback;
  170. MapServiceMock mapService;
  171. ServicesMock services;
  172. CMap * map;
  173. };
  174. //Issue #2765, Ghost Dragons can cast Age on Catapults
  175. TEST_F(CGameStateTest, DISABLED_issue2765)
  176. {
  177. startTestGame();
  178. CGHeroInstance * attacker = map->heroesOnMap[0];
  179. CGHeroInstance * defender = map->heroesOnMap[1];
  180. ASSERT_NE(attacker->tempOwner, defender->tempOwner);
  181. {
  182. NewArtifact na;
  183. na.artHolder = defender->id;
  184. na.artId = ArtifactID::BALLISTA;
  185. na.pos = ArtifactPosition::MACH1;
  186. gameCallback->sendAndApply(&na);
  187. }
  188. startTestBattle(attacker, defender);
  189. {
  190. battle::UnitInfo info;
  191. info.id = gameState->currentBattles.front()->battleNextUnitId();
  192. info.count = 1;
  193. info.type = CreatureID(69);
  194. info.side = BattleSide::ATTACKER;
  195. info.position = gameState->currentBattles.front()->getAvailableHex(info.type, info.side);
  196. info.summoned = false;
  197. BattleUnitsChanged pack;
  198. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  199. info.save(pack.changedStacks.back().data);
  200. gameCallback->sendAndApply(&pack);
  201. }
  202. const CStack * att = nullptr;
  203. const CStack * def = nullptr;
  204. for(const CStack * s : gameState->currentBattles.front()->stacks)
  205. {
  206. if(s->unitType()->getId() == CreatureID::BALLISTA && s->unitSide() == BattleSide::DEFENDER)
  207. def = s;
  208. else if(s->unitType()->getId() == CreatureID(69) && s->unitSide() == BattleSide::ATTACKER)
  209. att = s;
  210. }
  211. ASSERT_NE(att, nullptr);
  212. ASSERT_NE(def, nullptr);
  213. ASSERT_NE(att, def);
  214. EXPECT_NE(att->getMyHero(), defender);
  215. EXPECT_NE(def->getMyHero(), attacker);
  216. EXPECT_EQ(att->getMyHero(), attacker) << att->nodeName();
  217. EXPECT_EQ(def->getMyHero(), defender) << def->nodeName();
  218. {
  219. using namespace ::testing;
  220. spells::ProblemMock problemMock;
  221. // EXPECT_CALL(problemMock, add(_));
  222. const CSpell * age = SpellID(SpellID::AGE).toSpell();
  223. ASSERT_NE(age, nullptr);
  224. spells::AbilityCaster caster(att, 3);
  225. //here tested ballista, but this applied to all war machines
  226. spells::BattleCast cast(gameState->currentBattles.front().get(), &caster, spells::Mode::PASSIVE, age);
  227. spells::Target target;
  228. target.emplace_back(def);
  229. auto m = age->battleMechanics(&cast);
  230. EXPECT_FALSE(m->canBeCastAt(target, problemMock));
  231. EXPECT_TRUE(cast.castIfPossible(this, target));//should be possible, but with no effect (change to aimed cast check?)
  232. EXPECT_TRUE(def->activeSpells().empty());
  233. }
  234. }
  235. TEST_F(CGameStateTest, DISABLED_battleResurrection)
  236. {
  237. startTestGame();
  238. CGHeroInstance * attacker = map->heroesOnMap[0];
  239. CGHeroInstance * defender = map->heroesOnMap[1];
  240. ASSERT_NE(attacker->tempOwner, defender->tempOwner);
  241. attacker->setSecSkillLevel(SecondarySkill::EARTH_MAGIC, 3, true);
  242. attacker->addSpellToSpellbook(SpellID::RESURRECTION);
  243. attacker->setPrimarySkill(PrimarySkill::SPELL_POWER, 100, true);
  244. attacker->setPrimarySkill(PrimarySkill::KNOWLEDGE, 20, true);
  245. attacker->mana = attacker->manaLimit();
  246. {
  247. NewArtifact na;
  248. na.artHolder = attacker->id;
  249. na.artId = ArtifactID::SPELLBOOK;
  250. na.pos = ArtifactPosition::SPELLBOOK;
  251. gameCallback->sendAndApply(&na);
  252. }
  253. startTestBattle(attacker, defender);
  254. uint32_t unitId = gameState->currentBattles.front()->battleNextUnitId();
  255. {
  256. battle::UnitInfo info;
  257. info.id = unitId;
  258. info.count = 10;
  259. info.type = CreatureID(13);
  260. info.side = BattleSide::ATTACKER;
  261. info.position = gameState->currentBattles.front()->getAvailableHex(info.type, info.side);
  262. info.summoned = false;
  263. BattleUnitsChanged pack;
  264. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  265. info.save(pack.changedStacks.back().data);
  266. gameCallback->sendAndApply(&pack);
  267. }
  268. {
  269. battle::UnitInfo info;
  270. info.id = gameState->currentBattles.front()->battleNextUnitId();
  271. info.count = 10;
  272. info.type = CreatureID(13);
  273. info.side = BattleSide::DEFENDER;
  274. info.position = gameState->currentBattles.front()->getAvailableHex(info.type, info.side);
  275. info.summoned = false;
  276. BattleUnitsChanged pack;
  277. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  278. info.save(pack.changedStacks.back().data);
  279. gameCallback->sendAndApply(&pack);
  280. }
  281. CStack * unit = gameState->currentBattles.front()->getStack(unitId);
  282. ASSERT_NE(unit, nullptr);
  283. int64_t damage = unit->getMaxHealth() + 1;
  284. unit->damage(damage);
  285. EXPECT_EQ(unit->getCount(), 9);
  286. {
  287. using namespace ::testing;
  288. spells::ProblemMock problemMock;
  289. EXPECT_CALL(problemMock, add(_)).Times(AnyNumber()); //todo: do smth with problems of optional effects
  290. const CSpell * spell = SpellID(SpellID::RESURRECTION).toSpell();
  291. ASSERT_NE(spell, nullptr);
  292. spells::BattleCast cast(gameState->currentBattles.front().get(), attacker, spells::Mode::HERO, spell);
  293. spells::Target target;
  294. target.emplace_back(unit);
  295. auto m = spell->battleMechanics(&cast);
  296. EXPECT_TRUE(m->canBeCast(problemMock));
  297. EXPECT_TRUE(m->canBeCastAt(target, problemMock));
  298. cast.cast(this, target);
  299. //
  300. // std::vector<std::string> expLog;
  301. //
  302. // EXPECT_THAT(problemMock.log, ContainerEq(expLog));
  303. }
  304. EXPECT_EQ(unit->health.getCount(), 10);
  305. EXPECT_EQ(unit->health.getResurrected(), 0);
  306. }