1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Fix behavior for hero bought in town tavern

This commit is contained in:
nordsoft 2023-05-02 04:44:09 +04:00
parent 18efde3b02
commit c4cf2a100b
6 changed files with 128 additions and 90 deletions

View File

@ -579,6 +579,7 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons
auto * ret = new CBuilding(); auto * ret = new CBuilding();
ret->bid = getMappedValue<BuildingID, std::string>(stringID, BuildingID::NONE, MappedKeys::BUILDING_NAMES_TO_TYPES, false); ret->bid = getMappedValue<BuildingID, std::string>(stringID, BuildingID::NONE, MappedKeys::BUILDING_NAMES_TO_TYPES, false);
ret->subId = BuildingSubID::NONE;
if(ret->bid == BuildingID::NONE && !source["id"].isNull()) if(ret->bid == BuildingID::NONE && !source["id"].isNull())
{ {

View File

@ -14,6 +14,7 @@
#include "../CGeneralTextHandler.h" #include "../CGeneralTextHandler.h"
#include "../NetPacks.h" #include "../NetPacks.h"
#include "../IGameCallback.h" #include "../IGameCallback.h"
#include "../CGameState.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -284,12 +285,47 @@ void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) con
town->addHeroToStructureVisitors(h, indexOnTV); town->addHeroToStructureVisitors(h, indexOnTV);
} }
CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown) CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown, CRandomGenerator & rand)
{ {
bID = index; bID = index;
bType = subId; bType = subId;
town = cgTown; town = cgTown;
indexOnTV = static_cast<si32>(town->bonusingBuildings.size()); indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
initObj(rand);
}
void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
{
assert(town && town->town);
town->town->buildings.at(bID)->rewardableObjectInfo.configureObject(configuration, rand);
for(auto & rewardInfo : configuration.info)
{
for (auto & bonus : rewardInfo.reward.bonuses)
{
bonus.source = Bonus::TOWN_STRUCTURE;
bonus.sid = bID;
if (bonus.type == Bonus::MORALE)
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::MORALE, 0, bonus.val, 0);
if (bonus.type == Bonus::LUCK)
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::LUCK, 0, bonus.val, 0);
}
}
}
void CTownRewardableBuilding::newTurn(CRandomGenerator & rand) const
{
if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
{
if(configuration.resetParameters.rewards)
{
cb->setObjProperty(town->id, ObjProperty::REWARD_RANDOMIZE, indexOnTV);
}
if(configuration.resetParameters.visitors)
{
cb->setObjProperty(town->id, ObjProperty::REWARD_CLEARED, indexOnTV);
cb->setObjProperty(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, indexOnTV);
}
}
} }
void CTownRewardableBuilding::setProperty(ui8 what, ui32 val) void CTownRewardableBuilding::setProperty(ui8 what, ui32 val)
@ -299,12 +335,18 @@ void CTownRewardableBuilding::setProperty(ui8 what, ui32 val)
case ObjProperty::VISITORS: case ObjProperty::VISITORS:
visitors.insert(ObjectInstanceID(val)); visitors.insert(ObjectInstanceID(val));
break; break;
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
visitors.clear();
break;
case ObjProperty::REWARD_RANDOMIZE: case ObjProperty::REWARD_RANDOMIZE:
//initObj(cb->gameState()->getRandomGenerator()); initObj(cb->gameState()->getRandomGenerator());
break; break;
case ObjProperty::REWARD_SELECT: case ObjProperty::REWARD_SELECT:
selectedReward = val; selectedReward = val;
break; break;
case ObjProperty::REWARD_CLEARED:
onceVisitableObjectCleared = val;
break;
} }
} }
@ -355,7 +397,7 @@ bool CTownRewardableBuilding::wasVisitedBefore(const CGHeroInstance * contextHer
case Rewardable::VISIT_UNLIMITED: case Rewardable::VISIT_UNLIMITED:
return false; return false;
case Rewardable::VISIT_ONCE: case Rewardable::VISIT_ONCE:
return false; //not supported return onceVisitableObjectCleared;
case Rewardable::VISIT_PLAYER: case Rewardable::VISIT_PLAYER:
return false; //not supported return false; //not supported
case Rewardable::VISIT_BONUS: case Rewardable::VISIT_BONUS:
@ -439,19 +481,12 @@ void CTownRewardableBuilding::onHeroVisit(const CGHeroInstance *h) const
grantRewardWithMessage(rewards.front()); grantRewardWithMessage(rewards.front());
break; break;
case Rewardable::SELECT_RANDOM: // give random case Rewardable::SELECT_RANDOM: // give random
//TODO: support grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
//grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
break; break;
} }
break; break;
} }
} }
/*if(getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
{
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_TEAM, id, h->id);
cb->sendAndApply(&cov);
}*/
} }
else else
{ {

View File

@ -16,7 +16,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class CGTownInstance; class CGTownInstance;
class CBuilding;
class DLL_LINKAGE CGTownBuilding : public IObjectInterface class DLL_LINKAGE CGTownBuilding : public IObjectInterface
{ {
@ -110,6 +110,8 @@ class DLL_LINKAGE CTownRewardableBuilding : public CGTownBuilding, public Reward
/// reward selected by player, no serialize /// reward selected by player, no serialize
ui16 selectedReward = 0; ui16 selectedReward = 0;
bool onceVisitableObjectCleared = false;
std::set<ObjectInstanceID> visitors; std::set<ObjectInstanceID> visitors;
bool wasVisitedBefore(const CGHeroInstance * contextHero) const; bool wasVisitedBefore(const CGHeroInstance * contextHero) const;
@ -120,19 +122,25 @@ public:
void setProperty(ui8 what, ui32 val) override; void setProperty(ui8 what, ui32 val) override;
void onHeroVisit(const CGHeroInstance * h) const override; void onHeroVisit(const CGHeroInstance * h) const override;
void newTurn(CRandomGenerator & rand) const override;
/// gives second part of reward after hero level-ups for proper granting of spells/mana /// gives second part of reward after hero level-ups for proper granting of spells/mana
void heroLevelUpDone(const CGHeroInstance *hero) const override; void heroLevelUpDone(const CGHeroInstance *hero) const override;
void initObj(CRandomGenerator & rand) override;
/// applies player selection of reward /// applies player selection of reward
void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override; void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * TOWN); CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * town, CRandomGenerator & rand);
CTownRewardableBuilding() = default; CTownRewardableBuilding() = default;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
h & static_cast<CGTownBuilding&>(*this); h & static_cast<CGTownBuilding&>(*this);
h & static_cast<Rewardable::Interface&>(*this); h & static_cast<Rewardable::Interface&>(*this);
h & onceVisitableObjectCleared;
h & visitors;
} }
}; };

View File

@ -54,13 +54,13 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
switch (what) switch (what)
{ {
case ObjProperty::STRUCTURE_ADD_VISITING_HERO: case ObjProperty::STRUCTURE_ADD_VISITING_HERO:
bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id.getNum()); bonusingBuildings[val]->setProperty(ObjProperty::VISITORS, visitingHero->id.getNum());
break; break;
case ObjProperty::STRUCTURE_CLEAR_VISITORS: case ObjProperty::STRUCTURE_CLEAR_VISITORS:
bonusingBuildings[val]->setProperty (ObjProperty::STRUCTURE_CLEAR_VISITORS, 0); bonusingBuildings[val]->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, 0);
break; break;
case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors case ObjProperty::STRUCTURE_ADD_GARRISONED_HERO: //add garrisoned hero to visitors
bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id.getNum()); bonusingBuildings[val]->setProperty(ObjProperty::VISITORS, garrisonHero->id.getNum());
break; break;
case ObjProperty::BONUS_VALUE_FIRST: case ObjProperty::BONUS_VALUE_FIRST:
bonusValue.first = val; bonusValue.first = val;
@ -68,6 +68,12 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
case ObjProperty::BONUS_VALUE_SECOND: case ObjProperty::BONUS_VALUE_SECOND:
bonusValue.second = val; bonusValue.second = val;
break; break;
case ObjProperty::REWARD_RANDOMIZE:
bonusingBuildings[val]->setProperty(ObjProperty::REWARD_RANDOMIZE, 0);
break;
case ObjProperty::REWARD_CLEARED:
bonusingBuildings[val]->setProperty(ObjProperty::REWARD_CLEARED, false);
break;
} }
} }
CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
@ -386,24 +392,7 @@ void CGTownInstance::addTownBonuses(CRandomGenerator & rand)
bonusingBuildings.push_back(new COPWBonus(kvp.second->bid, kvp.second->subId, this)); bonusingBuildings.push_back(new COPWBonus(kvp.second->bid, kvp.second->subId, this));
if(kvp.second->subId == BuildingSubID::CONFIGURABLE_REWARD) if(kvp.second->subId == BuildingSubID::CONFIGURABLE_REWARD)
{ bonusingBuildings.push_back(new CTownRewardableBuilding(kvp.second->bid, kvp.second->subId, this, rand));
auto * newBuilding = new CTownRewardableBuilding(kvp.second->bid, kvp.second->subId, this);
kvp.second->rewardableObjectInfo.configureObject(newBuilding->configuration, rand);
for(auto & rewardInfo : newBuilding->configuration.info)
{
for (auto & bonus : rewardInfo.reward.bonuses)
{
bonus.source = Bonus::TOWN_STRUCTURE;
bonus.sid = kvp.second->bid;
//TODO: bonus.description = object->getObjectName();
if (bonus.type == Bonus::MORALE)
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::MORALE, 0, bonus.val, 0);
if (bonus.type == Bonus::LUCK)
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::LUCK, 0, bonus.val, 0);
}
}
bonusingBuildings.push_back(newBuilding);
}
} }
} }
@ -514,72 +503,73 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const
cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID); cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID);
cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal); cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal);
} }
const auto * manaVortex = getBonusingBuilding(BuildingSubID::MANA_VORTEX); for(const auto * manaVortex : getBonusingBuildings(BuildingSubID::MANA_VORTEX))
if (manaVortex != nullptr)
cb->setObjProperty(id, ObjProperty::STRUCTURE_CLEAR_VISITORS, manaVortex->indexOnTV); //reset visitors for Mana Vortex cb->setObjProperty(id, ObjProperty::STRUCTURE_CLEAR_VISITORS, manaVortex->indexOnTV); //reset visitors for Mana Vortex
//get Mana Vortex or Stables bonuses //get Mana Vortex or Stables bonuses
//same code is in the CGameHandler::buildStructure method //same code is in the CGameHandler::buildStructure method
if (visitingHero != nullptr) if (visitingHero != nullptr)
cb->visitCastleObjects(this, visitingHero); cb->visitCastleObjects(this, visitingHero);
if (garrisonHero != nullptr) if (garrisonHero != nullptr)
cb->visitCastleObjects(this, garrisonHero); cb->visitCastleObjects(this, garrisonHero);
if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns
{
std::vector<SlotID> nativeCrits; //slots
for(const auto & elem : Slots())
{ {
std::vector<SlotID> nativeCrits; //slots if (elem.second->type->getFaction() == subID) //native
for(const auto & elem : Slots())
{ {
if (elem.second->type->getFaction() == subID) //native nativeCrits.push_back(elem.first); //collect matching slots
{
nativeCrits.push_back(elem.first); //collect matching slots
}
} }
if(!nativeCrits.empty()) }
if(!nativeCrits.empty())
{
SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand);
StackLocation sl(this, pos);
const CCreature *c = getCreature(pos);
if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
{ {
SlotID pos = *RandomGeneratorUtil::nextItem(nativeCrits, rand); cb->changeStackCount(sl, c->getGrowth());
StackLocation sl(this, pos);
const CCreature *c = getCreature(pos);
if (rand.nextInt(99) < 90 || c->upgrades.empty()) //increase number if no upgrade available
{
cb->changeStackCount(sl, c->getGrowth());
}
else //upgrade
{
cb->changeStackType(sl, VLC->creh->objects[*c->upgrades.begin()]);
}
} }
if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack else //upgrade
{ {
int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1); cb->changeStackType(sl, VLC->creh->objects[*c->upgrades.begin()]);
if (!town->creatures[i].empty()) }
{ }
CreatureID c = town->creatures[i][0]; if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack
SlotID n; {
int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1);
TQuantity count = creatureGrowth(i); if (!town->creatures[i].empty())
if (!count) // no dwelling {
count = VLC->creh->objects[c]->getGrowth(); CreatureID c = town->creatures[i][0];
SlotID n;
{//no lower tiers or above current month
TQuantity count = creatureGrowth(i);
if ((n = getSlotFor(c)).validSlot()) if (!count) // no dwelling
{ count = VLC->creh->objects[c]->getGrowth();
StackLocation sl(this, n);
if (slotEmpty(n)) {//no lower tiers or above current month
cb->insertNewStack(sl, VLC->creh->objects[c], count);
else //add to existing if ((n = getSlotFor(c)).validSlot())
cb->changeStackCount(sl, count); {
} StackLocation sl(this, n);
if (slotEmpty(n))
cb->insertNewStack(sl, VLC->creh->objects[c], count);
else //add to existing
cb->changeStackCount(sl, count);
} }
} }
} }
} }
}
} }
for(const auto * rewardableBuilding : getBonusingBuildings(BuildingSubID::CONFIGURABLE_REWARD))
rewardableBuilding->newTurn(rand);
} }
/* /*
int3 CGTownInstance::getSightCenter() const int3 CGTownInstance::getSightCenter() const
@ -832,7 +822,9 @@ void CGTownInstance::recreateBuildingsBonuses()
void CGTownInstance::setVisitingHero(CGHeroInstance *h) void CGTownInstance::setVisitingHero(CGHeroInstance *h)
{ {
assert(!!visitingHero == !h); if(visitingHero.get() == h)
return;
if(h) if(h)
{ {
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner); PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
@ -855,7 +847,9 @@ void CGTownInstance::setVisitingHero(CGHeroInstance *h)
void CGTownInstance::setGarrisonedHero(CGHeroInstance *h) void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
{ {
assert(!!garrisonHero == !h); if(garrisonHero.get() == h)
return;
if(h) if(h)
{ {
PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner); PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner);
@ -933,14 +927,15 @@ const CArmedInstance * CGTownInstance::getUpperArmy() const
return this; return this;
} }
const CGTownBuilding * CGTownInstance::getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const std::vector<const CGTownBuilding *> CGTownInstance::getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const
{ {
std::vector<const CGTownBuilding *> ret;
for(auto * const building : bonusingBuildings) for(auto * const building : bonusingBuildings)
{ {
if(building->getBuildingSubtype() == subId) if(building->getBuildingSubtype() == subId)
return building; ret.push_back(building);
} }
return nullptr; return ret;
} }

View File

@ -150,7 +150,7 @@ public:
GrowthInfo getGrowthInfo(int level) const; GrowthInfo getGrowthInfo(int level) const;
bool hasFort() const; bool hasFort() const;
bool hasCapitol() const; bool hasCapitol() const;
const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const; std::vector<const CGTownBuilding *> getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const;
bool hasBuiltSomeTradeBuilding() const; bool hasBuiltSomeTradeBuilding() const;
//checks if special building with type buildingID is constructed //checks if special building with type buildingID is constructed
bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const; bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const;

View File

@ -4440,10 +4440,9 @@ bool CGameHandler::hireHero(const CGObjectInstance *obj, ui8 hid, PlayerColor pl
giveResource(player, EGameResID::GOLD, -GameConstants::HERO_GOLD_COST); giveResource(player, EGameResID::GOLD, -GameConstants::HERO_GOLD_COST);
if (t) if(t)
{ {
visitCastleObjects(t, nh); objectVisited(t, nh);
giveSpells (t,nh);
} }
return true; return true;
} }