From 9c6bd201594eac73d67877fad231dcba0161c24a Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:32:28 +0200 Subject: [PATCH] code review --- client/windows/CCastleInterface.cpp | 5 ++--- lib/mapObjects/CGTownInstance.cpp | 3 ++- lib/mapObjects/CGTownInstance.h | 8 ++++---- lib/networkPacks/NetPacksLib.cpp | 7 +++++-- server/CGameHandler.cpp | 9 +++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 9bc5e1ae6..d8f069e56 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -2041,8 +2041,7 @@ void CMageGuildScreen::Scroll::clickPressed(const Point & cursorPosition) if(vstd::find_pos(town->spells[i], spell->id) != -1) level = i; - int today = LOCPLINT->cb->getDate(Date::DAY); - if(town->spellResearchActionsPerDay.find(today) != town->spellResearchActionsPerDay.end() && town->spellResearchActionsPerDay.at(today) >= LOCPLINT->cb->getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_PER_DAY).Vector()[level].Float()) + if(town->spellResearchCounterDay >= LOCPLINT->cb->getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_PER_DAY).Vector()[level].Float()) { LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.spellResearch.comeAgain")); return; @@ -2050,7 +2049,7 @@ void CMageGuildScreen::Scroll::clickPressed(const Point & cursorPosition) auto costBase = TResources(LOCPLINT->cb->getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_COST).Vector()[level]); auto costExponent = LOCPLINT->cb->getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_COST_EXPONENT_PER_RESEARCH).Vector()[level].Float(); - auto cost = costBase * std::pow(town->spellResearchCounter + 1, costExponent); + auto cost = costBase * std::pow(town->spellResearchAcceptedCounter + 1, costExponent); std::vector> resComps; auto newSpell = town->spells[level].at(town->spellsAtLevel(level, false)); diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 76dc2a254..fc968ffb5 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -269,7 +269,8 @@ CGTownInstance::CGTownInstance(IGameCallback *cb): destroyed(0), identifier(0), alignmentToPlayer(PlayerColor::NEUTRAL), - spellResearchCounter(0), + spellResearchCounterDay(0), + spellResearchAcceptedCounter(0), spellResearchAllowed(true) { this->setNodeType(CBonusSystemNode::TOWN); diff --git a/lib/mapObjects/CGTownInstance.h b/lib/mapObjects/CGTownInstance.h index 6e4bc7af3..fe143c6fd 100644 --- a/lib/mapObjects/CGTownInstance.h +++ b/lib/mapObjects/CGTownInstance.h @@ -73,8 +73,8 @@ public: std::vector > spells; //spells[level] -> vector of spells, first will be available in guild std::vector events; std::pair bonusValue;//var to store town bonuses (rampart = resources from mystic pond, factory = save debts); - std::map spellResearchActionsPerDay; - int spellResearchCounter; + int spellResearchCounterDay; + int spellResearchAcceptedCounter; bool spellResearchAllowed; ////////////////////////////////////////////////////////////////////////// @@ -98,8 +98,8 @@ public: if (h.version >= Handler::Version::SPELL_RESEARCH) { - h & spellResearchActionsPerDay; - h & spellResearchCounter; + h & spellResearchCounterDay; + h & spellResearchAcceptedCounter; h & spellResearchAllowed; } diff --git a/lib/networkPacks/NetPacksLib.cpp b/lib/networkPacks/NetPacksLib.cpp index fb37de483..623437b03 100644 --- a/lib/networkPacks/NetPacksLib.cpp +++ b/lib/networkPacks/NetPacksLib.cpp @@ -944,9 +944,9 @@ void SetResearchedSpells::applyGs(CGameState *gs) CGTownInstance *town = gs->getTown(tid); town->spells[level] = spells; - town->spellResearchActionsPerDay[gs->getDate(Date::DAY)]++; + town->spellResearchCounterDay++; if(accepted) - town->spellResearchCounter++; + town->spellResearchAcceptedCounter++; } void SetMana::applyGs(CGameState *gs) @@ -1941,7 +1941,10 @@ void NewTurn::applyGs(CGameState *gs) creatureSet.applyGs(gs); for(CGTownInstance* t : gs->map->towns) + { t->built = 0; + t->spellResearchCounterDay = 0; + } if(newRumor) gs->currentRumor = *newRumor; diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 8d1046398..8448ca6fc 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -2247,7 +2247,9 @@ bool CGameHandler::spellResearch(ObjectInstanceID tid, SpellID spellAtSlot, bool { CGTownInstance *t = gs->getTown(tid); - if(!(getSettings().getBoolean(EGameSettings::TOWNS_SPELL_RESEARCH) && t->spellResearchAllowed) && complain("Spell research not allowed!")) + if(!getSettings().getBoolean(EGameSettings::TOWNS_SPELL_RESEARCH) && complain("Spell research not allowed!")) + return false; + if (!t->spellResearchAllowed && complain("Spell research not allowed in this town!")) return false; int level = -1; @@ -2260,8 +2262,7 @@ bool CGameHandler::spellResearch(ObjectInstanceID tid, SpellID spellAtSlot, bool auto spells = t->spells.at(level); - int today = getDate(Date::DAY); - bool researchLimitExceeded = t->spellResearchActionsPerDay.find(today) != t->spellResearchActionsPerDay.end() && t->spellResearchActionsPerDay.at(today) >= getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_PER_DAY).Vector()[level].Float(); + bool researchLimitExceeded = t->spellResearchCounterDay >= getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_PER_DAY).Vector()[level].Float(); if(researchLimitExceeded && complain("Already researched today!")) return false; @@ -2275,7 +2276,7 @@ bool CGameHandler::spellResearch(ObjectInstanceID tid, SpellID spellAtSlot, bool auto costBase = TResources(getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_COST).Vector()[level]); auto costExponent = getSettings().getValue(EGameSettings::TOWNS_SPELL_RESEARCH_COST_EXPONENT_PER_RESEARCH).Vector()[level].Float(); - auto cost = costBase * std::pow(t->spellResearchCounter + 1, costExponent); + auto cost = costBase * std::pow(t->spellResearchAcceptedCounter + 1, costExponent); if(!getPlayerState(t->getOwner())->resources.canAfford(cost) && complain("Spell replacement cannot be afforded!")) return false;