CGameStateTest.cpp 11 KB

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