bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const { if(canGetFullInfo(obj)) return true; const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last return visitor->ID == Obj::HERO && canGetFullInfo(visitor); //owned or allied hero is a visitor }
void CGameInfoCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos, UpgradeInfo &out) const { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_IF(!canGetFullInfo(obj), "Cannot get info about not owned object!"); ERROR_RET_IF(!obj->hasStackAtSlot(stackPos), "There is no such stack!"); out = gs->getUpgradeInfo(obj->getStack(stackPos)); //return gs->getUpgradeInfo(obj->getStack(stackPos)); }
int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1); //if there is a battle if(gs->curB) return gs->curB->battleGetSpellCost(sp, caster); //if there is no battle return caster->getSpellCost(sp); }
int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1); if (hero) //we see hero's spellbook return sp->calculateDamage(hero, nullptr, hero->getEffectLevel(sp), hero->getEffectPower(sp)); else return 0; //mage guild }
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID ) { ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED); CBuilding * pom = t->town->buildings[ID]; if(!pom) return EBuildingState::BUILDING_ERROR; if(t->hasBuilt(ID)) //already built return EBuildingState::ALREADY_PRESENT; //can we build it? if(vstd::contains(t->forbiddenBuildings, ID)) return EBuildingState::FORBIDDEN; //forbidden //checking for requirements std::set<BuildingID> reqs = getBuildingRequiments(t, ID);//getting all requirements bool notAllBuilt = false; for( std::set<BuildingID>::iterator ri = reqs.begin(); ri != reqs.end(); ri++ ) { if(!t->hasBuilt(*ri)) //lack of requirements - cannot build { if(vstd::contains(t->forbiddenBuildings, *ri)) // not built requirement forbidden - same goes to this build return EBuildingState::FORBIDDEN; else notAllBuilt = true; // no return here - we need to check if any required builds are forbidden } } if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN) return EBuildingState::CANT_BUILD_TODAY; //building limit if (notAllBuilt) return EBuildingState::PREREQUIRES; if(ID == BuildingID::CAPITOL) { const PlayerState *ps = getPlayer(t->tempOwner); if(ps) { BOOST_FOREACH(const CGTownInstance *t, ps->towns) { if(t->hasBuilt(BuildingID::CAPITOL)) { return EBuildingState::HAVE_CAPITAL; //no more than one capitol } } } } else if(ID == BuildingID::SHIPYARD)
void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj) { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_IF(!obj, "No guild object!"); ERROR_RET_IF(obj->ID == Obj::TOWN && !canGetFullInfo(obj), "Cannot get info about town guild object!"); //TODO: advmap object -> check if they're visited by our hero if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN) { gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size()); } else if(obj->ID == Obj::DEN_OF_THIEVES) { gs->obtainPlayersStats(thi, 20); } }
int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1); if(!gs->curB) //no battle { if (hero) //but we see hero's spellbook return gs->curB->calculateSpellDmg( sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(PrimarySkill::SPELL_POWER)); else return 0; //mage guild } //gs->getHero(gs->currentPlayer) //const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1]; const CGHeroInstance * ourHero = hero; return gs->curB->calculateSpellDmg( sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(PrimarySkill::SPELL_POWER)); }
void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj) { //boost::shared_lock<boost::shared_mutex> lock(*gs->mx); ERROR_RET_IF(!obj, "No guild object!"); ERROR_RET_IF(obj->ID == Obj::TOWN && !canGetFullInfo(obj), "Cannot get info about town guild object!"); //TODO: advmap object -> check if they're visited by our hero if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN) { int taverns = 0; for(auto town : gs->players[*player].towns) { if(town->hasBuilt(BuildingID::TAVERN)) taverns++; } gs->obtainPlayersStats(thi, taverns); } else if(obj->ID == Obj::DEN_OF_THIEVES) { gs->obtainPlayersStats(thi, 20); } }
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID ) { ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED); if(!t->town->buildings.count(ID)) return EBuildingState::BUILDING_ERROR; const CBuilding * building = t->town->buildings.at(ID); if(t->hasBuilt(ID)) //already built return EBuildingState::ALREADY_PRESENT; //can we build it? if(vstd::contains(t->forbiddenBuildings, ID)) return EBuildingState::FORBIDDEN; //forbidden auto possiblyNotBuiltTest = [&](BuildingID id) -> bool { return ((id == BuildingID::CAPITOL) ? true : !t->hasBuilt(id)); }; std::function<bool(BuildingID id)> allowedTest = [&](BuildingID id) -> bool { return !vstd::contains(t->forbiddenBuildings, id); }; if (!t->genBuildingRequirements(ID, true).satisfiable(allowedTest, possiblyNotBuiltTest)) return EBuildingState::FORBIDDEN; if(ID == BuildingID::CAPITOL) { const PlayerState *ps = getPlayer(t->tempOwner, false); if(ps) { for(const CGTownInstance *t : ps->towns) { if(t->hasBuilt(BuildingID::CAPITOL)) { return EBuildingState::HAVE_CAPITAL; //no more than one capitol } } } } else if(ID == BuildingID::SHIPYARD) { const TerrainTile *tile = getTile(t->bestLocation(), false); if(!tile || tile->terType != ETerrainType::WATER) return EBuildingState::NO_WATER; //lack of water } auto buildTest = [&](BuildingID id) -> bool { return t->hasBuilt(id); }; if (!t->genBuildingRequirements(ID).test(buildTest)) return EBuildingState::PREREQUIRES; if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN) return EBuildingState::CANT_BUILD_TODAY; //building limit //checking resources if(!building->resources.canBeAfforded(getPlayer(t->tempOwner)->resources)) return EBuildingState::NO_RESOURCES; //lack of res return EBuildingState::ALLOWED; }