diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index e3fa5f75e..b0b94171c 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -356,7 +356,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/mapObjects/CArmedInstance.h ${MAIN_LIB_DIR}/mapObjects/CBank.h - ${MAIN_LIB_DIR}/mapObjects/CGDwelling.cpp + ${MAIN_LIB_DIR}/mapObjects/CGDwelling.h ${MAIN_LIB_DIR}/mapObjects/CGHeroInstance.h ${MAIN_LIB_DIR}/mapObjects/CGMarket.h ${MAIN_LIB_DIR}/mapObjects/CGPandoraBox.h diff --git a/lib/CTownHandler.cpp b/lib/CTownHandler.cpp index 839b7c8a3..190d4bad0 100644 --- a/lib/CTownHandler.cpp +++ b/lib/CTownHandler.cpp @@ -496,38 +496,34 @@ void CTownHandler::addBonusesForVanilaBuilding(CBuilding * building) const std::shared_ptr b; static TPropagatorPtr playerPropagator = std::make_shared(CBonusSystemNode::ENodeTypes::PLAYER); - if(building->subId == BuildingSubID::NONE) + if(building->bid == BuildingID::TAVERN) { - if(building->bid == BuildingID::TAVERN) - { - b = createBonus(building, BonusType::MORALE, +1); - } + b = createBonus(building, BonusType::MORALE, +1); } - else + + switch(building->subId) { - switch(building->subId) - { - case BuildingSubID::BROTHERHOOD_OF_SWORD: - b = createBonus(building, BonusType::MORALE, +2); - building->overrideBids.insert(BuildingID::TAVERN); - break; - case BuildingSubID::FOUNTAIN_OF_FORTUNE: - b = createBonus(building, BonusType::LUCK, +2); - break; - case BuildingSubID::SPELL_POWER_GARRISON_BONUS: - b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); - break; - case BuildingSubID::ATTACK_GARRISON_BONUS: - b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); - break; - case BuildingSubID::DEFENSE_GARRISON_BONUS: - b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); - break; - case BuildingSubID::LIGHTHOUSE: - b = createBonus(building, BonusType::MOVEMENT, +500, playerPropagator, 0); - break; - } + case BuildingSubID::BROTHERHOOD_OF_SWORD: + b = createBonus(building, BonusType::MORALE, +2); + building->overrideBids.insert(BuildingID::TAVERN); + break; + case BuildingSubID::FOUNTAIN_OF_FORTUNE: + b = createBonus(building, BonusType::LUCK, +2); + break; + case BuildingSubID::SPELL_POWER_GARRISON_BONUS: + b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); + break; + case BuildingSubID::ATTACK_GARRISON_BONUS: + b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); + break; + case BuildingSubID::DEFENSE_GARRISON_BONUS: + b = createBonus(building, BonusType::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); + break; + case BuildingSubID::LIGHTHOUSE: + b = createBonus(building, BonusType::MOVEMENT, +500, playerPropagator, 0); + break; } + if(b) building->addNewBonus(b, building->buildingBonuses); } @@ -584,23 +580,22 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons auto * ret = new CBuilding(); ret->bid = getMappedValue(stringID, BuildingID::NONE, MappedKeys::BUILDING_NAMES_TO_TYPES, false); + ret->subId = BuildingSubID::NONE; - if(ret->bid == BuildingID::NONE) + if(ret->bid == BuildingID::NONE && !source["id"].isNull()) + { + logMod->warn("Building %s: id field is deprecated", stringID); ret->bid = source["id"].isNull() ? BuildingID(BuildingID::NONE) : BuildingID(source["id"].Float()); + } if (ret->bid == BuildingID::NONE) - logMod->error("Error: Building '%s' has not internal ID and won't work properly. Correct the typo or update VCMI.", stringID); + logMod->error("Building '%s' isn't recognized and won't work properly. Correct the typo or update VCMI.", stringID); ret->mode = ret->bid == BuildingID::GRAIL ? CBuilding::BUILD_GRAIL : getMappedValue(source["mode"], CBuilding::BUILD_NORMAL, CBuilding::MODES); - ret->subId = getMappedValue(source["type"], BuildingSubID::NONE, MappedKeys::SPECIAL_BUILDINGS); - ret->height = CBuilding::HEIGHT_NO_TOWER; - - if(ret->subId == BuildingSubID::LOOKOUT_TOWER - || ret->bid == BuildingID::GRAIL) - ret->height = getMappedValue(source["height"], CBuilding::HEIGHT_NO_TOWER, CBuilding::TOWER_TYPES); + ret->height = getMappedValue(source["height"], CBuilding::HEIGHT_NO_TOWER, CBuilding::TOWER_TYPES); ret->identifier = stringID; ret->modScope = source.meta; @@ -619,7 +614,10 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons loadSpecialBuildingBonuses(source["bonuses"], ret->buildingBonuses, ret); if(ret->buildingBonuses.empty()) + { + ret->subId = getMappedValue(source["type"], BuildingSubID::NONE, MappedKeys::SPECIAL_BUILDINGS); addBonusesForVanilaBuilding(ret); + } loadSpecialBuildingBonuses(source["onVisitBonuses"], ret->onVisitBonuses, ret); @@ -631,6 +629,12 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons for(auto & bonus : ret->onVisitBonuses) bonus->sid = Bonus::getSid32(ret->town->faction->getIndex(), ret->bid); } + + if(source["type"].String() == "configurable" && ret->subId == BuildingSubID::NONE) + { + ret->subId = BuildingSubID::CUSTOM_VISITING_REWARD; + ret->rewardableObjectInfo.init(source); + } } //MODS COMPATIBILITY FOR 0.96 if(!ret->produce.nonZero()) @@ -688,11 +692,12 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons void CTownHandler::loadBuildings(CTown * town, const JsonNode & source) { - for(const auto & node : source.Struct()) + if(source.isStruct()) { - if (!node.second.isNull()) + for(const auto & node : source.Struct()) { - loadBuilding(town, node.first, node.second); + if (!node.second.isNull()) + loadBuilding(town, node.first, node.second); } } } diff --git a/lib/CTownHandler.h b/lib/CTownHandler.h index f14972d35..484844752 100644 --- a/lib/CTownHandler.h +++ b/lib/CTownHandler.h @@ -22,6 +22,7 @@ #include "bonuses/Bonus.h" #include "bonuses/BonusList.h" #include "Point.h" +#include "mapObjects/CRewardableConstructor.h" VCMI_LIB_NAMESPACE_BEGIN @@ -57,6 +58,8 @@ public: std::set overrideBids; /// the building which bonuses should be overridden with bonuses of the current building BonusList buildingBonuses; BonusList onVisitBonuses; + + Rewardable::Info rewardableObjectInfo; ///configurable rewards for special buildings enum EBuildMode { @@ -135,6 +138,7 @@ public: h & overrideBids; h & buildingBonuses; h & onVisitBonuses; + h & rewardableObjectInfo; } friend class CTownHandler; diff --git a/lib/GameConstants.h b/lib/GameConstants.h index ea58db8c8..cf912aedf 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -581,7 +581,8 @@ namespace BuildingSubID EXPERIENCE_VISITING_BONUS, LIGHTHOUSE, TREASURY, - CUSTOM_VISITING_BONUS + CUSTOM_VISITING_BONUS, + CUSTOM_VISITING_REWARD }; } @@ -604,16 +605,43 @@ namespace MappedKeys { "special2", BuildingID::SPECIAL_2 }, { "special3", BuildingID::SPECIAL_3 }, { "special4", BuildingID::SPECIAL_4 }, - { "grail", BuildingID::GRAIL } - }; - - static const std::map BUILDING_TYPES_TO_NAMES = - { - { BuildingID::SPECIAL_1, "special1", }, - { BuildingID::SPECIAL_2, "special2" }, - { BuildingID::SPECIAL_3, "special3" }, - { BuildingID::SPECIAL_4, "special4" }, - { BuildingID::GRAIL, "grail"} + { "grail", BuildingID::GRAIL }, + { "mageGuild1", BuildingID::MAGES_GUILD_1 }, + { "mageGuild2", BuildingID::MAGES_GUILD_2 }, + { "mageGuild3", BuildingID::MAGES_GUILD_3 }, + { "mageGuild4", BuildingID::MAGES_GUILD_4 }, + { "mageGuild5", BuildingID::MAGES_GUILD_5 }, + { "tavern", BuildingID::TAVERN }, + { "shipyard", BuildingID::SHIPYARD }, + { "fort", BuildingID::FORT }, + { "citadel", BuildingID::CITADEL }, + { "castle", BuildingID::CASTLE }, + { "villageHall", BuildingID::VILLAGE_HALL }, + { "townHall", BuildingID::TOWN_HALL }, + { "cityHall", BuildingID::CITY_HALL }, + { "capitol", BuildingID::CAPITOL }, + { "marketplace", BuildingID::MARKETPLACE }, + { "resourceSilo", BuildingID::RESOURCE_SILO }, + { "blacksmith", BuildingID::BLACKSMITH }, + { "horde1", BuildingID::HORDE_1 }, + { "horde1Upgr", BuildingID::HORDE_1_UPGR }, + { "horde2", BuildingID::HORDE_2 }, + { "horde2Upgr", BuildingID::HORDE_2_UPGR }, + { "ship", BuildingID::SHIP }, + { "dwellingLvl1", BuildingID::DWELL_LVL_1 }, + { "dwellingLvl2", BuildingID::DWELL_LVL_2 }, + { "dwellingLvl3", BuildingID::DWELL_LVL_3 }, + { "dwellingLvl4", BuildingID::DWELL_LVL_4 }, + { "dwellingLvl5", BuildingID::DWELL_LVL_5 }, + { "dwellingLvl6", BuildingID::DWELL_LVL_6 }, + { "dwellingLvl7", BuildingID::DWELL_LVL_7 }, + { "dwellingUpLvl1", BuildingID::DWELL_LVL_1_UP }, + { "dwellingUpLvl2", BuildingID::DWELL_LVL_2_UP }, + { "dwellingUpLvl3", BuildingID::DWELL_LVL_3_UP }, + { "dwellingUpLvl4", BuildingID::DWELL_LVL_4_UP }, + { "dwellingUpLvl5", BuildingID::DWELL_LVL_5_UP }, + { "dwellingUpLvl6", BuildingID::DWELL_LVL_6_UP }, + { "dwellingUpLvl7", BuildingID::DWELL_LVL_7_UP }, }; static const std::map SPECIAL_BUILDINGS = diff --git a/lib/mapObjects/CGTownBuilding.cpp b/lib/mapObjects/CGTownBuilding.cpp index 7adb01e56..b0c66ec2e 100644 --- a/lib/mapObjects/CGTownBuilding.cpp +++ b/lib/mapObjects/CGTownBuilding.cpp @@ -14,6 +14,7 @@ #include "../CGeneralTextHandler.h" #include "../NetPacks.h" #include "../IGameCallback.h" +#include "../CGameState.h" VCMI_LIB_NAMESPACE_BEGIN @@ -284,5 +285,210 @@ void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) con town->addHeroToStructureVisitors(h, indexOnTV); } +CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown, CRandomGenerator & rand) +{ + bID = index; + bType = subId; + town = cgTown; + indexOnTV = static_cast(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 = BonusSource::TOWN_STRUCTURE; + bonus.sid = bID; + if (bonus.type == BonusType::MORALE) + rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::MORALE, 0, bonus.val, 0); + if (bonus.type == BonusType::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::STRUCTURE_CLEAR_VISITORS, indexOnTV); + } + } +} + +void CTownRewardableBuilding::setProperty(ui8 what, ui32 val) +{ + switch (what) + { + case ObjProperty::VISITORS: + visitors.insert(ObjectInstanceID(val)); + break; + case ObjProperty::STRUCTURE_CLEAR_VISITORS: + visitors.clear(); + break; + case ObjProperty::REWARD_RANDOMIZE: + initObj(cb->gameState()->getRandomGenerator()); + break; + case ObjProperty::REWARD_SELECT: + selectedReward = val; + break; + } +} + +void CTownRewardableBuilding::heroLevelUpDone(const CGHeroInstance *hero) const +{ + grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero); +} + +void CTownRewardableBuilding::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const +{ + if(visitors.find(hero->id) != visitors.end()) + return; // query not for this building + + if(answer > 0 && answer-1 < configuration.info.size()) + { + auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT); + grantReward(list[answer - 1], hero); + } + else + { + throw std::runtime_error("Unhandled choice"); + } +} + +void CTownRewardableBuilding::grantReward(ui32 rewardID, const CGHeroInstance * hero) const +{ + town->addHeroToStructureVisitors(hero, indexOnTV); + + grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero); + + // hero is not blocked by levelup dialog - grant remainer immediately + if(!cb->isVisitCoveredByAnotherQuery(town, hero)) + { + grantRewardAfterLevelup(cb, configuration.info.at(rewardID), town, hero); + } +} + +bool CTownRewardableBuilding::wasVisitedBefore(const CGHeroInstance * contextHero) const +{ + switch (configuration.visitMode) + { + case Rewardable::VISIT_UNLIMITED: + return false; + case Rewardable::VISIT_ONCE: + return !visitors.empty(); + case Rewardable::VISIT_PLAYER: + return false; //not supported + case Rewardable::VISIT_BONUS: + return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, Bonus::getSid32(town->town->faction->getIndex(), bID)); + case Rewardable::VISIT_HERO: + return visitors.find(contextHero->id) != visitors.end(); + default: + return false; + } +} + +void CTownRewardableBuilding::onHeroVisit(const CGHeroInstance *h) const +{ + auto grantRewardWithMessage = [&](int index) -> void + { + auto vi = configuration.info.at(index); + logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString()); + + town->addHeroToStructureVisitors(h, indexOnTV); //adding to visitors + + InfoWindow iw; + iw.player = h->tempOwner; + iw.text = vi.message; + vi.reward.loadComponents(iw.components, h); + iw.type = EInfoWindowMode::MODAL; + if(!iw.components.empty() || !iw.text.toString().empty()) + cb->showInfoDialog(&iw); + + grantReward(index, h); + }; + auto selectRewardsMessage = [&](const std::vector & rewards, const MetaString & dialog) -> void + { + BlockingDialog sd(configuration.canRefuse, rewards.size() > 1); + sd.player = h->tempOwner; + sd.text = dialog; + + if (rewards.size() > 1) + for (auto index : rewards) + sd.components.push_back(configuration.info.at(index).reward.getDisplayedComponent(h)); + + if (rewards.size() == 1) + configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h); + + cb->showBlockingDialog(&sd); + }; + + if(!town->hasBuilt(bID) || cb->isVisitCoveredByAnotherQuery(town, h)) + return; + + if(!wasVisitedBefore(h)) + { + auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT); + + logGlobal->debug("Visiting object with %d possible rewards", rewards.size()); + switch (rewards.size()) + { + case 0: // no available rewards, e.g. visiting School of War without gold + { + auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE); + if (!emptyRewards.empty()) + grantRewardWithMessage(emptyRewards[0]); + else + logMod->warn("No applicable message for visiting empty object!"); + break; + } + case 1: // one reward. Just give it with message + { + if (configuration.canRefuse) + selectRewardsMessage(rewards, configuration.info.at(rewards.front()).message); + else + grantRewardWithMessage(rewards.front()); + break; + } + default: // multiple rewards. Act according to select mode + { + switch (configuration.selectMode) { + case Rewardable::SELECT_PLAYER: // player must select + selectRewardsMessage(rewards, configuration.onSelect); + break; + case Rewardable::SELECT_FIRST: // give first available + grantRewardWithMessage(rewards.front()); + break; + case Rewardable::SELECT_RANDOM: // give random + grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator())); + break; + } + break; + } + } + } + else + { + logGlobal->debug("Revisiting already visited object"); + + auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED); + if (!visitedRewards.empty()) + grantRewardWithMessage(visitedRewards[0]); + else + logMod->debug("No applicable message for visiting already visited object!"); + } +} + VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/CGTownBuilding.h b/lib/mapObjects/CGTownBuilding.h index 39233f532..998a52794 100644 --- a/lib/mapObjects/CGTownBuilding.h +++ b/lib/mapObjects/CGTownBuilding.h @@ -11,11 +11,12 @@ #pragma once #include "CObjectHandler.h" +#include "../rewardable/Interface.h" VCMI_LIB_NAMESPACE_BEGIN class CGTownInstance; - +class CBuilding; class DLL_LINKAGE CGTownBuilding : public IObjectInterface { @@ -104,4 +105,40 @@ private: void applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const; }; +class DLL_LINKAGE CTownRewardableBuilding : public CGTownBuilding, public Rewardable::Interface +{ + /// reward selected by player, no serialize + ui16 selectedReward = 0; + + std::set visitors; + + bool wasVisitedBefore(const CGHeroInstance * contextHero) const; + + void grantReward(ui32 rewardID, const CGHeroInstance * hero) const; + +public: + void setProperty(ui8 what, ui32 val) 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 + void heroLevelUpDone(const CGHeroInstance *hero) const override; + + void initObj(CRandomGenerator & rand) override; + + /// applies player selection of reward + void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override; + + CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * town, CRandomGenerator & rand); + CTownRewardableBuilding() = default; + + template void serialize(Handler &h, const int version) + { + h & static_cast(*this); + h & static_cast(*this); + h & visitors; + } +}; + VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 87671b37c..444d1398d 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -10,6 +10,7 @@ #include "StdInc.h" #include "CGTownInstance.h" +#include "CGTownBuilding.h" #include "CObjectClassesHandler.h" #include "../spells/CSpellHandler.h" #include "../bonuses/Bonus.h" @@ -52,14 +53,14 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val) ///this is freakin' overcomplicated solution switch (what) { - case ObjProperty::STRUCTURE_ADD_VISITING_HERO: - bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id.getNum()); + case ObjProperty::STRUCTURE_ADD_VISITING_HERO: + bonusingBuildings[val]->setProperty(ObjProperty::VISITORS, visitingHero->id.getNum()); break; case ObjProperty::STRUCTURE_CLEAR_VISITORS: - bonusingBuildings[val]->setProperty (ObjProperty::STRUCTURE_CLEAR_VISITORS, 0); + bonusingBuildings[val]->setProperty(ObjProperty::STRUCTURE_CLEAR_VISITORS, 0); break; 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; case ObjProperty::BONUS_VALUE_FIRST: bonusValue.first = val; @@ -67,6 +68,9 @@ void CGTownInstance::setPropertyDer(ui8 what, ui32 val) case ObjProperty::BONUS_VALUE_SECOND: bonusValue.second = val; break; + case ObjProperty::REWARD_RANDOMIZE: + bonusingBuildings[val]->setProperty(ObjProperty::REWARD_RANDOMIZE, 0); + break; } } CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle @@ -259,6 +263,12 @@ void CGTownInstance::setOwner(const PlayerColor & player) const cb->setOwner(this, player); } +void CGTownInstance::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const +{ + for (auto building : bonusingBuildings) + building->blockingDialogAnswered(hero, answer); +} + void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const { if(!cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ))//if this is enemy @@ -365,7 +375,7 @@ bool CGTownInstance::isBonusingBuildingAdded(BuildingID::EBuildingID bid) const return present != bonusingBuildings.end(); } -void CGTownInstance::addTownBonuses() +void CGTownInstance::addTownBonuses(CRandomGenerator & rand) { for(const auto & kvp : town->buildings) { @@ -377,6 +387,9 @@ void CGTownInstance::addTownBonuses() if(kvp.second->IsWeekBonus()) bonusingBuildings.push_back(new COPWBonus(kvp.second->bid, kvp.second->subId, this)); + + if(kvp.second->subId == BuildingSubID::CUSTOM_VISITING_REWARD) + bonusingBuildings.push_back(new CTownRewardableBuilding(kvp.second->bid, kvp.second->subId, this, rand)); } } @@ -465,7 +478,7 @@ void CGTownInstance::initObj(CRandomGenerator & rand) ///initialize town structu } } initOverriddenBids(); - addTownBonuses(); //add special bonuses from buildings to the bonusingBuildings vector. + addTownBonuses(rand); //add special bonuses from buildings to the bonusingBuildings vector. recreateBuildingsBonuses(); updateAppearance(); } @@ -487,10 +500,8 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const cb->setObjProperty (id, ObjProperty::BONUS_VALUE_FIRST, resID); cb->setObjProperty (id, ObjProperty::BONUS_VALUE_SECOND, resVal); } - - const auto * manaVortex = getBonusingBuilding(BuildingSubID::MANA_VORTEX); - - if (manaVortex != nullptr) + + for(const auto * manaVortex : getBonusingBuildings(BuildingSubID::MANA_VORTEX)) cb->setObjProperty(id, ObjProperty::STRUCTURE_CLEAR_VISITORS, manaVortex->indexOnTV); //reset visitors for Mana Vortex //get Mana Vortex or Stables bonuses @@ -502,57 +513,60 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const cb->visitCastleObjects(this, garrisonHero); if (tempOwner == PlayerColor::NEUTRAL) //garrison growth for neutral towns + { + std::vector nativeCrits; //slots + for(const auto & elem : Slots()) { - std::vector nativeCrits; //slots - for(const auto & elem : Slots()) + if (elem.second->type->getFaction() == subID) //native { - 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); - 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()]); - } + cb->changeStackCount(sl, c->getGrowth()); } - 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); - if (!town->creatures[i].empty()) - { - CreatureID c = town->creatures[i][0]; - SlotID n; - - TQuantity count = creatureGrowth(i); - if (!count) // no dwelling - count = VLC->creh->objects[c]->getGrowth(); - - {//no lower tiers or above current month - - if ((n = getSlotFor(c)).validSlot()) - { - StackLocation sl(this, n); - if (slotEmpty(n)) - cb->insertNewStack(sl, VLC->creh->objects[c], count); - else //add to existing - cb->changeStackCount(sl, count); - } + cb->changeStackType(sl, VLC->creh->objects[*c->upgrades.begin()]); + } + } + if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack + { + int i = rand.nextInt(std::min(GameConstants::CREATURES_PER_TOWN, cb->getDate(Date::MONTH) << 1) - 1); + if (!town->creatures[i].empty()) + { + CreatureID c = town->creatures[i][0]; + SlotID n; + + TQuantity count = creatureGrowth(i); + if (!count) // no dwelling + count = VLC->creatures()->getByIndex(c)->getGrowth(); + + {//no lower tiers or above current month + + if ((n = getSlotFor(c)).validSlot()) + { + 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::CUSTOM_VISITING_REWARD)) + rewardableBuilding->newTurn(rand); } /* int3 CGTownInstance::getSightCenter() const @@ -805,7 +819,9 @@ void CGTownInstance::recreateBuildingsBonuses() void CGTownInstance::setVisitingHero(CGHeroInstance *h) { - assert(!!visitingHero == !h); + if(visitingHero.get() == h) + return; + if(h) { PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner); @@ -828,7 +844,9 @@ void CGTownInstance::setVisitingHero(CGHeroInstance *h) void CGTownInstance::setGarrisonedHero(CGHeroInstance *h) { - assert(!!garrisonHero == !h); + if(garrisonHero.get() == h) + return; + if(h) { PlayerState *p = cb->gameState()->getPlayerState(h->tempOwner); @@ -906,14 +924,15 @@ const CArmedInstance * CGTownInstance::getUpperArmy() const return this; } -const CGTownBuilding * CGTownInstance::getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const +std::vector CGTownInstance::getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const { + std::vector ret; for(auto * const building : bonusingBuildings) { if(building->getBuildingSubtype() == subId) - return building; + ret.push_back(building); } - return nullptr; + return ret; } diff --git a/lib/mapObjects/CGTownInstance.h b/lib/mapObjects/CGTownInstance.h index dbc40e1af..f41c64870 100644 --- a/lib/mapObjects/CGTownInstance.h +++ b/lib/mapObjects/CGTownInstance.h @@ -89,6 +89,10 @@ public: h & spells; h & events; h & bonusingBuildings; + + for(auto * bonusingBuilding : bonusingBuildings) + bonusingBuilding->town = this; + h & town; h & townAndVis; BONUS_TREE_DESERIALIZATION_FIX @@ -150,7 +154,7 @@ public: GrowthInfo getGrowthInfo(int level) const; bool hasFort() const; bool hasCapitol() const; - const CGTownBuilding * getBonusingBuilding(BuildingSubID::EBuildingSubID subId) const; + std::vector getBonusingBuildings(BuildingSubID::EBuildingSubID subId) const; bool hasBuiltSomeTradeBuilding() const; //checks if special building with type buildingID is constructed bool hasBuilt(BuildingSubID::EBuildingSubID buildingID) const; @@ -206,6 +210,7 @@ public: protected: void setPropertyDer(ui8 what, ui32 val) override; void serializeJsonOptions(JsonSerializeFormat & handler) override; + void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override; private: void setOwner(const PlayerColor & owner) const; @@ -214,7 +219,7 @@ private: bool townEnvisagesBuilding(BuildingSubID::EBuildingSubID bid) const; bool isBonusingBuildingAdded(BuildingID::EBuildingID bid) const; void initOverriddenBids(); - void addTownBonuses(); + void addTownBonuses(CRandomGenerator & rand); }; VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/MapObjects.h b/lib/mapObjects/MapObjects.h index 475ddf9b2..01e38b4d8 100644 --- a/lib/mapObjects/MapObjects.h +++ b/lib/mapObjects/MapObjects.h @@ -18,7 +18,6 @@ #include "CGDwelling.h" #include "CGHeroInstance.h" #include "CGMarket.h" -#include "CGTownBuilding.h" #include "CGTownInstance.h" #include "CGPandoraBox.h" #include "CRewardableObject.h" diff --git a/lib/registerTypes/RegisterTypes.h b/lib/registerTypes/RegisterTypes.h index dc1115bb0..f1d1c50fb 100644 --- a/lib/registerTypes/RegisterTypes.h +++ b/lib/registerTypes/RegisterTypes.h @@ -21,6 +21,7 @@ #include "../mapObjects/CRewardableConstructor.h" #include "../mapObjects/CommonConstructors.h" #include "../mapObjects/MapObjects.h" +#include "../mapObjects/CGTownBuilding.h" #include "../battle/CObstacleInstance.h" #include "../bonuses/CBonusSystemNode.h" #include "../bonuses/Limiters.h" @@ -157,7 +158,8 @@ void registerTypesMapObjects2(Serializer &s) //Other object-related s.template registerType(); s.template registerType(); - s.template registerType(); + s.template registerType(); + s.template registerType(); s.template registerType(); diff --git a/lib/rewardable/Interface.cpp b/lib/rewardable/Interface.cpp index eceaa474c..b5debacf8 100644 --- a/lib/rewardable/Interface.cpp +++ b/lib/rewardable/Interface.cpp @@ -91,7 +91,6 @@ void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const Re for(const Bonus & bonus : info.reward.bonuses) { - assert(bonus.source == BonusSource::OBJECT); GiveBonus gb; gb.who = GiveBonus::ETarget::HERO; gb.bonus = bonus; diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 1082e33a2..7234863ed 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3612,7 +3612,7 @@ bool CGameHandler::buildStructure(ObjectInstanceID tid, BuildingID requestedID, sendAndApply(&fw); if(t->visitingHero) - visitCastleObjects(t, t->visitingHero); + objectVisited(t, t->visitingHero); if(t->garrisonHero) visitCastleObjects(t, t->garrisonHero); @@ -4440,10 +4440,9 @@ bool CGameHandler::hireHero(const CGObjectInstance *obj, ui8 hid, PlayerColor pl giveResource(player, EGameResID::GOLD, -GameConstants::HERO_GOLD_COST); - if (t) + if(t) { - visitCastleObjects(t, nh); - giveSpells (t,nh); + objectVisited(t, nh); } return true; }