CGameStateTest.cpp 10 KB

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