1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-31 00:07:39 +02:00

Move UpgradeInfo class to header and source files

This commit is contained in:
MichalZr6
2024-12-19 11:29:57 +01:00
parent af9445c16f
commit f893c80192
16 changed files with 139 additions and 93 deletions

View File

@@ -19,6 +19,7 @@
#include "../../lib/CConfigHandler.h"
#include "../../lib/IGameSettings.h"
#include "../../lib/gameState/CGameState.h"
#include "../../lib/gameState/UpgradeInfo.h"
#include "../../lib/serializer/CTypeList.h"
#include "../../lib/networkPacks/PacksForClient.h"
#include "../../lib/networkPacks/PacksForClientBattle.h"
@@ -805,7 +806,7 @@ bool AIGateway::makePossibleUpgrades(const CArmedInstance * obj)
myCb->upgradeCreature(obj, SlotID(i), upgID);
upgraded = true;
logAi->debug("Upgraded %d %s to %s", s->count, upgradeInfo.oldID.toCreature()->getNamePluralTranslated(),
upgradeInfo.getNextUpgrade().toCreature()->getNamePluralTranslated());
upgradeInfo.getUpgrade().toCreature()->getNamePluralTranslated());
}
else
break;

View File

@@ -22,6 +22,7 @@
#include "../../lib/CConfigHandler.h"
#include "../../lib/IGameSettings.h"
#include "../../lib/gameState/CGameState.h"
#include "../../lib/gameState/UpgradeInfo.h"
#include "../../lib/bonuses/Limiters.h"
#include "../../lib/bonuses/Updaters.h"
#include "../../lib/bonuses/Propagators.h"
@@ -770,7 +771,7 @@ void makePossibleUpgrades(const CArmedInstance * obj)
{
cb->upgradeCreature(obj, SlotID(i), upgID);
logAi->debug("Upgraded %d %s to %s", s->count, upgradeInfo.oldID.toCreature()->getNamePluralTranslated(),
upgradeInfo.getNextUpgrade().toCreature()->getNamePluralTranslated());
upgradeInfo.getUpgrade().toCreature()->getNamePluralTranslated());
}
else
break;

View File

@@ -33,6 +33,7 @@
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../lib/gameState/CGameState.h"
#include "../../lib/gameState/UpgradeInfo.h"
void CGarrisonSlot::setHighlight(bool on)
{

View File

@@ -35,6 +35,7 @@
#include "../../lib/IGameSettings.h"
#include "../../lib/entities/hero/CHeroHandler.h"
#include "../../lib/gameState/CGameState.h"
#include "../../lib/gameState/UpgradeInfo.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/texts/TextOperations.h"
@@ -363,7 +364,7 @@ CStackWindow::ButtonsSection::ButtonsSection(CStackWindow * owner, int yOffset)
for(size_t buttonIndex = 0; buttonIndex < buttonsToCreate; buttonIndex++)
{
TResources totalCost = upgradeInfo.info.getUpgradeCosts().at(buttonIndex) * parent->info->creatureCount;
TResources totalCost = upgradeInfo.info.getAvailableUpgradeCosts().at(buttonIndex) * parent->info->creatureCount;
auto onUpgrade = [=]()
{

View File

@@ -53,6 +53,7 @@
#include "../lib/gameState/CGameState.h"
#include "../lib/gameState/SThievesGuildInfo.h"
#include "../lib/gameState/TavernHeroesPool.h"
#include "../lib/gameState/UpgradeInfo.h"
#include "../lib/texts/CGeneralTextHandler.h"
#include "../lib/IGameSettings.h"
#include "ConditionalWait.h"
@@ -1159,7 +1160,7 @@ void CHillFortWindow::updateGarrisons()
LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
if(info.canUpgrade()) //we have upgrades here - update costs
{
costs[i] = info.getNextUpgradeCosts() * hero->getStackCount(SlotID(i));
costs[i] = info.getUpgradeCosts() * hero->getStackCount(SlotID(i));
totalSum += costs[i];
}
}
@@ -1271,7 +1272,7 @@ void CHillFortWindow::makeDeal(SlotID slot)
{
UpgradeInfo info(s->getCreatureID());
LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.getNextUpgrade());
LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.getUpgrade());
}
}
}
@@ -1309,7 +1310,7 @@ CHillFortWindow::State CHillFortWindow::getState(SlotID slot)
if(!info.hasUpgrades())
return State::ALREADY_UPGRADED;
if(!(info.getNextUpgradeCosts() * hero->getStackCount(slot)).canBeAfforded(myRes))
if(!(info.getUpgradeCosts() * hero->getStackCount(slot)).canBeAfforded(myRes))
return State::UNAFFORDABLE;
return State::MAKE_UPGRADE;

View File

@@ -109,6 +109,7 @@ set(lib_MAIN_SRCS
gameState/RumorState.cpp
gameState/TavernHeroesPool.cpp
gameState/GameStatistics.cpp
gameState/UpgradeInfo.cpp
mapObjectConstructors/AObjectTypeHandler.cpp
mapObjectConstructors/CBankInstanceConstructor.cpp

View File

@@ -52,6 +52,7 @@
#include "../rmg/CMapGenerator.h"
#include "../serializer/CMemorySerializer.h"
#include "../spells/CSpellHandler.h"
#include "UpgradeInfo.h"
#include <vstd/RNG.h>
@@ -1779,23 +1780,4 @@ ArtifactID CGameState::pickRandomArtifact(vstd::RNG & rand, int flags)
return pickRandomArtifact(rand, flags, [](const ArtifactID &) { return true; });
}
void UpgradeInfo::addUpgrade(const CreatureID & upgradeID, const Creature * creature, int costModifier)
{
isAvailable = costModifier >= 0;
upgradesIDs.push_back(upgradeID);
ResourceSet upgradeCost = (upgradeID.toCreature()->getFullRecruitCost() - creature->getFullRecruitCost()) * costModifier / 100;
upgradeCost.positive(); //upgrade cost can't be negative, ignore missing resources
upgradesCosts.push_back(std::move(upgradeCost));
// sort from highest ID to smallest
size_t pos = upgradesIDs.size() - 1;
while(pos > 0 && upgradesIDs[pos] > upgradesIDs[pos - 1])
{
std::swap(upgradesIDs[pos], upgradesIDs[pos - 1]);
std::swap(upgradesCosts[pos], upgradesCosts[pos - 1]);
--pos;
}
}
VCMI_LIB_NAMESPACE_END

View File

@@ -38,71 +38,8 @@ class TavernHeroesPool;
struct SThievesGuildInfo;
class CRandomGenerator;
class GameSettings;
class UpgradeInfo
{
public:
UpgradeInfo() = delete;
UpgradeInfo(CreatureID base)
: oldID(base), isAvailable(true)
{ }
CreatureID oldID; //creature to be upgraded
const std::vector<CreatureID> & getAvailableUpgrades() const
{
return upgradesIDs;
}
const CreatureID & getNextUpgrade() const
{
return upgradesIDs.back();
}
const ResourceSet & getUpgradeCostsFor(CreatureID id) const
{
auto idIt = std::find(upgradesIDs.begin(), upgradesIDs.end(), id);
assert(idIt != upgradesIDs.end());
return upgradesCosts[std::distance(upgradesIDs.begin(), idIt)];
}
const std::vector<ResourceSet> & getUpgradeCosts() const
{
return upgradesCosts;
}
const ResourceSet & getNextUpgradeCosts() const
{
return upgradesCosts.back();
}
bool canUpgrade() const
{
return !upgradesIDs.empty() && isAvailable;
}
bool hasUpgrades() const
{
return !upgradesIDs.empty();
}
// Adds a new upgrade and ensures alignment and sorted order
void addUpgrade(const CreatureID & upgradeID, const Creature * creature, int costModifier = 100);
auto size() const
{
return upgradesIDs.size();
}
private:
std::vector<CreatureID> upgradesIDs; //possible upgrades
std::vector<ResourceSet> upgradesCosts; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
bool isAvailable; // flag for unavailableUpgrades like in miniHillFort from HoTA
};
class BattleInfo;
class UpgradeInfo;
DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const EVictoryLossCheckResult & victoryLossCheckResult);

View File

@@ -0,0 +1,36 @@
/*
* UpgradeInfo.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "UpgradeInfo.h"
#include "CCreatureHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
void UpgradeInfo::addUpgrade(const CreatureID & upgradeID, const Creature * creature, int costPercentageModifier)
{
isAvailable = costPercentageModifier >= 0;
upgradesIDs.push_back(upgradeID);
ResourceSet upgradeCost = (upgradeID.toCreature()->getFullRecruitCost() - creature->getFullRecruitCost()) * costPercentageModifier / 100;
upgradeCost.positive(); //upgrade cost can't be negative, ignore missing resources
upgradesCosts.push_back(std::move(upgradeCost));
// sort from highest ID to smallest
size_t pos = upgradesIDs.size() - 1;
while(pos > 0 && upgradesIDs[pos] > upgradesIDs[pos - 1])
{
std::swap(upgradesIDs[pos], upgradesIDs[pos - 1]);
std::swap(upgradesCosts[pos], upgradesCosts[pos - 1]);
--pos;
}
}
VCMI_LIB_NAMESPACE_END

View File

@@ -0,0 +1,82 @@
/*
* UpgradeInfo.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "../lib/constants/EntityIdentifiers.h"
#include "../lib/ResourceSet.h"
VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE UpgradeInfo
{
public:
UpgradeInfo() = delete;
UpgradeInfo(CreatureID base)
: oldID(base), isAvailable(true)
{
}
CreatureID oldID; //creature to be upgraded
const std::vector<CreatureID> & getAvailableUpgrades() const
{
return upgradesIDs;
}
const CreatureID & getUpgrade() const
{
return upgradesIDs.back();
}
const ResourceSet & getUpgradeCostsFor(CreatureID id) const
{
auto idIt = std::find(upgradesIDs.begin(), upgradesIDs.end(), id);
assert(idIt != upgradesIDs.end());
return upgradesCosts[std::distance(upgradesIDs.begin(), idIt)];
}
const std::vector<ResourceSet> & getAvailableUpgradeCosts() const
{
return upgradesCosts;
}
const ResourceSet & getUpgradeCosts() const
{
return upgradesCosts.back();
}
bool canUpgrade() const
{
return !upgradesIDs.empty() && isAvailable;
}
bool hasUpgrades() const
{
return !upgradesIDs.empty();
}
// Adds a new upgrade and ensures alignment and sorted order
void addUpgrade(const CreatureID & upgradeID, const Creature * creature, int costPercentageModifier = 100);
auto size() const
{
return upgradesIDs.size();
}
private:
std::vector<CreatureID> upgradesIDs; //possible upgrades
std::vector<ResourceSet> upgradesCosts; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
bool isAvailable; // flag for unavailableUpgrades like in miniHillFort from HoTA
};
VCMI_LIB_NAMESPACE_END

View File

@@ -25,6 +25,7 @@
#include "../CSkillHandler.h"
#include "../IGameCallback.h"
#include "../gameState/CGameState.h"
#include "../gameState/UpgradeInfo.h"
#include "../CCreatureHandler.h"
#include "../mapping/CMap.h"
#include "../StartInfo.h"

View File

@@ -23,6 +23,7 @@ class CHero;
class CGBoat;
class CGTownInstance;
class CMap;
class UpgradeInfo;
struct TerrainTile;
struct TurnInfo;

View File

@@ -20,6 +20,7 @@
#include "../texts/CGeneralTextHandler.h"
#include "../IGameCallback.h"
#include "../gameState/CGameState.h"
#include "../gameState/UpgradeInfo.h"
#include "../mapping/CMap.h"
#include "../CPlayerState.h"
#include "../StartInfo.h"

View File

@@ -32,6 +32,7 @@
#include "../networkPacks/PacksForClient.h"
#include "../networkPacks/PacksForClientBattle.h"
#include "../networkPacks/StackLocation.h"
#include "../lib/gameState/UpgradeInfo.h"
#include <vstd/RNG.h>
@@ -1323,12 +1324,9 @@ void HillFort::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack)
int costModifier = upgradeCostPercentage[index];
if(costModifier < 0) // upgrade not allowed
return;
for(const auto & nid : stack.getCreature()->upgrades)
{
info.addUpgrade(nid, stack.getType(), costModifier / 100);
info.addUpgrade(nid, stack.getType(), costModifier);
}
}

View File

@@ -16,6 +16,7 @@
VCMI_LIB_NAMESPACE_BEGIN
class CMap;
class UpgradeInfo;
// This one teleport-specific, but has to be available everywhere in callbacks and netpacks
// For now it's will be there till teleports code refactored and moved into own file

View File

@@ -53,6 +53,7 @@
#include "../lib/filesystem/Filesystem.h"
#include "../lib/gameState/CGameState.h"
#include "../lib/gameState/UpgradeInfo.h"
#include "../lib/mapping/CMap.h"
#include "../lib/mapping/CMapService.h"