1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-29 21:56:54 +02:00

Added API for owned towns access

This commit is contained in:
Ivan Savenko 2023-04-17 15:32:18 +03:00
parent 5cbd0f8fc8
commit 365f552fa1
7 changed files with 55 additions and 21 deletions

View File

@ -1351,9 +1351,9 @@ void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
auto town = static_cast<const CGTownInstance *>(obj); auto town = static_cast<const CGTownInstance *>(obj);
if(obj->tempOwner == playerID) if(obj->tempOwner == playerID)
localState->ownedTowns.push_back(town); localState->addOwnedTown(town);
else else
localState->ownedTowns -= obj; localState->removeOwnedTown(town);
adventureInt->onTownChanged(town); adventureInt->onTownChanged(town);
} }
@ -1362,7 +1362,7 @@ void CPlayerInterface::objectPropertyChanged(const SetObjectProperty * sop)
std::unordered_set<int3> upos(pos.begin(), pos.end()); std::unordered_set<int3> upos(pos.begin(), pos.end());
adventureInt->onMapTilesChanged(upos); adventureInt->onMapTilesChanged(upos);
assert(cb->getTownsInfo().size() == localState->ownedTowns.size()); assert(cb->getTownsInfo().size() == localState->getOwnedTowns().size());
} }
} }
@ -1370,16 +1370,18 @@ void CPlayerInterface::initializeHeroTownList()
{ {
if(localState->getWanderingHeroes().empty()) if(localState->getWanderingHeroes().empty())
{ {
std::vector<const CGHeroInstance*> heroes = cb->getHeroesInfo(); for(auto & hero : cb->getHeroesInfo())
for(auto & hero : heroes)
{ {
if(!hero->inTownGarrison) if(!hero->inTownGarrison)
localState->addWanderingHero(hero); localState->addWanderingHero(hero);
} }
} }
if(!localState->ownedTowns.size()) if(localState->getOwnedTowns().empty())
localState->ownedTowns = cb->getTownsInfo(); {
for(auto & town : cb->getTownsInfo())
localState->addOwnedTown(town);
}
if(adventureInt) if(adventureInt)
adventureInt->onHeroChanged(nullptr); adventureInt->onHeroChanged(nullptr);

View File

@ -177,3 +177,29 @@ void PlayerLocalState::removeWanderingHero(const CGHeroInstance * hero)
vstd::erase(wanderingHeroes, hero); vstd::erase(wanderingHeroes, hero);
vstd::erase(sleepingHeroes, hero); vstd::erase(sleepingHeroes, hero);
} }
const std::vector<const CGTownInstance *> & PlayerLocalState::getOwnedTowns()
{
return ownedTowns;
}
const CGTownInstance * PlayerLocalState::getOwnedTown(size_t index)
{
if (index < ownedTowns.size())
return ownedTowns[index];
return nullptr;
}
void PlayerLocalState::addOwnedTown(const CGTownInstance * town)
{
assert(town);
assert(!vstd::contains(ownedTowns, town));
ownedTowns.push_back(town);
}
void PlayerLocalState::removeOwnedTown(const CGTownInstance * town)
{
assert(town);
assert(vstd::contains(ownedTowns, town));
vstd::erase(ownedTowns, town);
}

View File

@ -32,6 +32,8 @@ class PlayerLocalState
std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping
std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones) std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
std::vector<const CGTownInstance *> ownedTowns; //our towns on the adventure map
void saveHeroPaths(std::map<const CGHeroInstance *, int3> & paths); void saveHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
void loadHeroPaths(std::map<const CGHeroInstance *, int3> & paths); void loadHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
@ -53,14 +55,17 @@ public:
} }
} spellbookSettings; } spellbookSettings;
std::vector<const CGTownInstance *> ownedTowns; //our towns on the adventure map
explicit PlayerLocalState(CPlayerInterface & owner); explicit PlayerLocalState(CPlayerInterface & owner);
bool isHeroSleeping(const CGHeroInstance * hero) const; bool isHeroSleeping(const CGHeroInstance * hero) const;
void setHeroAsleep(const CGHeroInstance * hero); void setHeroAsleep(const CGHeroInstance * hero);
void setHeroAwaken(const CGHeroInstance * hero); void setHeroAwaken(const CGHeroInstance * hero);
const std::vector<const CGTownInstance *> & getOwnedTowns();
const CGTownInstance * getOwnedTown(size_t index);
void addOwnedTown(const CGTownInstance * hero);
void removeOwnedTown(const CGTownInstance * hero);
const std::vector<const CGHeroInstance *> & getWanderingHeroes(); const std::vector<const CGHeroInstance *> & getWanderingHeroes();
const CGHeroInstance * getWanderingHero(size_t index); const CGHeroInstance * getWanderingHero(size_t index);
void addWanderingHero(const CGHeroInstance * hero); void addWanderingHero(const CGHeroInstance * hero);

View File

@ -75,7 +75,8 @@ CAdventureMapInterface::CAdventureMapInterface():
pos.h = GH.screenDimensions().y; pos.h = GH.screenDimensions().y;
strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
townList->onSelect = [this](){ townList->onSelect = [this](){
const CGTownInstance * selectedTown = LOCPLINT->localState->ownedTowns[townList->getSelectedIndex()]; const CGTownInstance * selectedTown = LOCPLINT->localState->getOwnedTown(townList->getSelectedIndex());
assert(selectedTown);
LOCPLINT->setSelection(selectedTown); LOCPLINT->setSelection(selectedTown);
}; };
@ -683,12 +684,12 @@ void CAdventureMapInterface::keyPressed(const SDL_Keycode & key)
{ {
//find first town with tavern //find first town with tavern
auto itr = range::find_if(LOCPLINT->localState->ownedTowns, [](const CGTownInstance * town) auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
{ {
return town->hasBuilt(BuildingID::TAVERN); return town->hasBuilt(BuildingID::TAVERN);
}); });
if(itr != LOCPLINT->localState->ownedTowns.end()) if(itr != LOCPLINT->localState->getOwnedTowns().end())
LOCPLINT->showThievesGuildWindow(*itr); LOCPLINT->showThievesGuildWindow(*itr);
else else
LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern")); LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
@ -991,9 +992,9 @@ void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
{ {
LOCPLINT->setSelection(heroToSelect, centerView); LOCPLINT->setSelection(heroToSelect, centerView);
} }
else if (LOCPLINT->localState->ownedTowns.size()) else if (LOCPLINT->localState->getOwnedTowns().size())
{ {
LOCPLINT->setSelection(LOCPLINT->localState->ownedTowns.front(), centerView); LOCPLINT->setSelection(LOCPLINT->localState->getOwnedTown(0), centerView);
} }
else else
{ {

View File

@ -115,7 +115,7 @@ CInfoBar::VisibleGameStatusInfo::VisibleGameStatusInfo()
OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
//get amount of halls of each level //get amount of halls of each level
std::vector<int> halls(4, 0); std::vector<int> halls(4, 0);
for(auto town : LOCPLINT->localState->ownedTowns) for(auto town : LOCPLINT->localState->getOwnedTowns())
{ {
int hallLevel = town->hallLevel(); int hallLevel = town->hallLevel();
//negative value means no village hall, unlikely but possible //negative value means no village hall, unlikely but possible

View File

@ -263,8 +263,8 @@ void CHeroList::update(const CGHeroInstance * hero)
std::shared_ptr<CIntObject> CTownList::createTownItem(size_t index) std::shared_ptr<CIntObject> CTownList::createTownItem(size_t index)
{ {
if (LOCPLINT->localState->ownedTowns.size() > index) if (LOCPLINT->localState->getOwnedTowns().size() > index)
return std::make_shared<CTownItem>(this, LOCPLINT->localState->ownedTowns[index]); return std::make_shared<CTownItem>(this, LOCPLINT->localState->getOwnedTown(index));
return std::make_shared<CAnimImage>("ITPA", 0); return std::make_shared<CAnimImage>("ITPA", 0);
} }
@ -313,20 +313,20 @@ std::string CTownList::CTownItem::getHoverText()
} }
CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown): CTownList::CTownList(int size, Point position, std::string btnUp, std::string btnDown):
CList(size, position, btnUp, btnDown, LOCPLINT->localState->ownedTowns.size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1)) CList(size, position, btnUp, btnDown, LOCPLINT->localState->getOwnedTowns().size(), 306, 307, std::bind(&CTownList::createTownItem, this, _1))
{ {
} }
void CTownList::select(const CGTownInstance * town) void CTownList::select(const CGTownInstance * town)
{ {
selectIndex(vstd::find_pos(LOCPLINT->localState->ownedTowns, town)); selectIndex(vstd::find_pos(LOCPLINT->localState->getOwnedTowns(), town));
} }
void CTownList::update(const CGTownInstance *) void CTownList::update(const CGTownInstance *)
{ {
//simplest solution for now: reset list and restore selection //simplest solution for now: reset list and restore selection
listBox->resize(LOCPLINT->localState->ownedTowns.size()); listBox->resize(LOCPLINT->localState->getOwnedTowns().size());
if (LOCPLINT->localState->getCurrentTown()) if (LOCPLINT->localState->getCurrentTown())
select(LOCPLINT->localState->getCurrentTown()); select(LOCPLINT->localState->getCurrentTown());

View File

@ -1248,7 +1248,7 @@ void CCastleInterface::castleTeleport(int where)
void CCastleInterface::townChange() void CCastleInterface::townChange()
{ {
//TODO: do not recreate window //TODO: do not recreate window
const CGTownInstance * dest = LOCPLINT->localState->ownedTowns[townlist->getSelectedIndex()]; const CGTownInstance * dest = LOCPLINT->localState->getOwnedTown(townlist->getSelectedIndex());
const CGTownInstance * town = this->town;// "this" is going to be deleted const CGTownInstance * town = this->town;// "this" is going to be deleted
if ( dest == town ) if ( dest == town )
return; return;