From ee8adbe85f01b8494d902ddd3eb9c1d2d89d8079 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 24 Aug 2023 18:53:58 +0300 Subject: [PATCH] Update tavern on end of 7th turn of player in question Allows removal of "retreat after 7th day" workaround and as result - more straightforward code --- lib/gameState/TavernHeroesPool.cpp | 9 ------- lib/gameState/TavernSlot.h | 6 +---- server/CGameHandler.cpp | 11 +++++--- server/processors/HeroPoolProcessor.cpp | 34 +++++-------------------- 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/lib/gameState/TavernHeroesPool.cpp b/lib/gameState/TavernHeroesPool.cpp index f5e5a6138..70f441f98 100644 --- a/lib/gameState/TavernHeroesPool.cpp +++ b/lib/gameState/TavernHeroesPool.cpp @@ -124,15 +124,6 @@ void TavernHeroesPool::onNewDay() hero.second->setMovementPoints(hero.second->movementPointsLimit(true)); hero.second->mana = hero.second->manaLimit(); } - - for (auto & slot : currentTavern) - { - if (slot.role == TavernSlotRole::RETREATED_TODAY) - slot.role = TavernSlotRole::RETREATED; - - if (slot.role == TavernSlotRole::SURRENDERED_TODAY) - slot.role = TavernSlotRole::SURRENDERED; - } } void TavernHeroesPool::addHeroToPool(CGHeroInstance * hero) diff --git a/lib/gameState/TavernSlot.h b/lib/gameState/TavernSlot.h index 192fd047d..698168cff 100644 --- a/lib/gameState/TavernSlot.h +++ b/lib/gameState/TavernSlot.h @@ -24,12 +24,8 @@ enum class TavernSlotRole : int8_t SINGLE_UNIT, // hero was added after buying hero from this slot, and only has 1 creature in army FULL_ARMY, // hero was added to tavern on new week and still has full army - RETREATED, // hero was owned by player before, but have retreated from battle and only has 1 creature in army - RETREATED_TODAY, - - SURRENDERED, // hero was owned by player before, but have surrendered in battle and kept some troops - SURRENDERED_TODAY, + SURRENDERED // hero was owned by player before, but have surrendered in battle and kept some troops }; VCMI_LIB_NAMESPACE_END diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 4ae780090..b8ca99c58 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -611,6 +611,11 @@ void CGameHandler::onPlayerTurnEnded(PlayerColor which) { // 7 days without castle checkVictoryLossConditionsForPlayer(which); + + bool newWeek = getDate(Date::DAY_OF_WEEK) == 7; // end of 7th day + + if (newWeek) //new heroes in tavern + heroPool->onNewWeek(which); } void CGameHandler::onNewTurn() @@ -702,13 +707,13 @@ void CGameHandler::onNewTurn() { if (elem.first == PlayerColor::NEUTRAL) continue; - else if (elem.first >= PlayerColor::PLAYER_LIMIT) - assert(0); //illegal player number! + + assert(elem.first.isValidPlayer());//illegal player number! std::pair playerGold(elem.first, elem.second.resources[EGameResID::GOLD]); hadGold.insert(playerGold); - if (newWeek) //new heroes in tavern + if (firstTurn) heroPool->onNewWeek(elem.first); n.res[elem.first] = elem.second.resources; diff --git a/server/processors/HeroPoolProcessor.cpp b/server/processors/HeroPoolProcessor.cpp index 47c592876..1881f8083 100644 --- a/server/processors/HeroPoolProcessor.cpp +++ b/server/processors/HeroPoolProcessor.cpp @@ -68,10 +68,7 @@ TavernHeroSlot HeroPoolProcessor::selectSlotForRole(const PlayerColor & player, void HeroPoolProcessor::onHeroSurrendered(const PlayerColor & color, const CGHeroInstance * hero) { SetAvailableHero sah; - if (gameHandler->turnOrder->playerAwaitsNewDay(color)) - sah.roleID = TavernSlotRole::SURRENDERED_TODAY; - else - sah.roleID = TavernSlotRole::SURRENDERED; + sah.roleID = TavernSlotRole::SURRENDERED; sah.slotID = selectSlotForRole(color, sah.roleID); sah.player = color; @@ -82,10 +79,7 @@ void HeroPoolProcessor::onHeroSurrendered(const PlayerColor & color, const CGHer void HeroPoolProcessor::onHeroEscaped(const PlayerColor & color, const CGHeroInstance * hero) { SetAvailableHero sah; - if (gameHandler->turnOrder->playerAwaitsNewDay(color)) - sah.roleID = TavernSlotRole::RETREATED_TODAY; - else - sah.roleID = TavernSlotRole::RETREATED; + sah.roleID = TavernSlotRole::RETREATED; sah.slotID = selectSlotForRole(color, sah.roleID); sah.player = color; @@ -139,26 +133,10 @@ void HeroPoolProcessor::selectNewHeroForSlot(const PlayerColor & color, TavernHe void HeroPoolProcessor::onNewWeek(const PlayerColor & color) { - const auto & heroesPool = gameHandler->gameState()->heroesPool; - const auto & heroes = heroesPool->getHeroesFor(color); - - const auto nativeSlotRole = heroes.size() < 1 ? TavernSlotRole::NONE : heroesPool->getSlotRole(heroes[0]->type->getId()); - const auto randomSlotRole = heroes.size() < 2 ? TavernSlotRole::NONE : heroesPool->getSlotRole(heroes[1]->type->getId()); - - bool resetNativeSlot = nativeSlotRole != TavernSlotRole::RETREATED_TODAY && nativeSlotRole != TavernSlotRole::SURRENDERED_TODAY; - bool resetRandomSlot = randomSlotRole != TavernSlotRole::RETREATED_TODAY && randomSlotRole != TavernSlotRole::SURRENDERED_TODAY; - - if (resetNativeSlot) - clearHeroFromSlot(color, TavernHeroSlot::NATIVE); - - if (resetRandomSlot) - clearHeroFromSlot(color, TavernHeroSlot::RANDOM); - - if (resetNativeSlot) - selectNewHeroForSlot(color, TavernHeroSlot::NATIVE, true, true); - - if (resetRandomSlot) - selectNewHeroForSlot(color, TavernHeroSlot::RANDOM, false, true); + clearHeroFromSlot(color, TavernHeroSlot::NATIVE); + clearHeroFromSlot(color, TavernHeroSlot::RANDOM); + selectNewHeroForSlot(color, TavernHeroSlot::NATIVE, true, true); + selectNewHeroForSlot(color, TavernHeroSlot::RANDOM, false, true); } bool HeroPoolProcessor::hireHero(const ObjectInstanceID & objectID, const HeroTypeID & heroToRecruit, const PlayerColor & player)