1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

added API for sleeping heroes access

This commit is contained in:
Ivan Savenko
2023-04-17 13:26:28 +03:00
parent 6a46138617
commit 3eac6f323e
4 changed files with 38 additions and 20 deletions

View File

@@ -127,3 +127,26 @@ void PlayerLocalState::setSelection(const CArmedInstance *selection)
{
currentSelection = selection;
}
bool PlayerLocalState::isHeroSleeping(const CGHeroInstance * hero) const
{
return vstd::contains(sleepingHeroes, hero);
}
void PlayerLocalState::setHeroAsleep(const CGHeroInstance *hero)
{
assert(hero);
assert(vstd::contains(wanderingHeroes, hero));
assert(!vstd::contains(sleepingHeroes, hero));
sleepingHeroes.push_back(hero);
}
void PlayerLocalState::setHeroAwaken(const CGHeroInstance * hero)
{
assert(hero);
assert(vstd::contains(wanderingHeroes, hero));
assert(vstd::contains(sleepingHeroes, hero));
vstd::erase(sleepingHeroes, hero);
}

View File

@@ -30,6 +30,7 @@ class PlayerLocalState
const CArmedInstance * currentSelection;
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
void saveHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
void loadHeroPaths(std::map<const CGHeroInstance *, int3> & paths);
@@ -53,10 +54,13 @@ public:
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
std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping
explicit PlayerLocalState(CPlayerInterface & owner);
bool isHeroSleeping(const CGHeroInstance * hero) const;
void setHeroAsleep(const CGHeroInstance * hero);
void setHeroAwaken(const CGHeroInstance * hero);
void setPath(const CGHeroInstance *h, const CGPath & path);
bool setPath(const CGHeroInstance *h, const int3 & destination);

View File

@@ -291,7 +291,7 @@ void CAdventureMapInterface::fsleepWake()
const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
if (!h)
return;
bool newSleep = !isHeroSleeping(h);
bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
setHeroSleeping(h, newSleep);
updateSleepWake(h);
if (newSleep)
@@ -354,7 +354,7 @@ void CAdventureMapInterface::fendTurn()
{
for(auto hero : LOCPLINT->localState->wanderingHeroes)
{
if(!isHeroSleeping(hero) && hero->movement > 0)
if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movement > 0)
{
// Only show hero reminder if conditions met:
// - There still movement points
@@ -384,7 +384,7 @@ void CAdventureMapInterface::updateSleepWake(const CGHeroInstance *h)
sleepWake->block(!h);
if (!h)
return;
bool state = isHeroSleeping(h);
bool state = LOCPLINT->localState->isHeroSleeping(h);
sleepWake->setIndex(state ? 1 : 0, true);
sleepWake->assignedKeys.clear();
sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
@@ -408,9 +408,9 @@ int CAdventureMapInterface::getNextHeroIndex(int startIndex)
if (i >= LOCPLINT->localState->wanderingHeroes.size())
i = 0;
}
while (((LOCPLINT->localState->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->localState->wanderingHeroes[i])) && (i != startIndex));
while (((LOCPLINT->localState->wanderingHeroes[i]->movement == 0) || LOCPLINT->localState->isHeroSleeping(LOCPLINT->localState->wanderingHeroes[i])) && (i != startIndex));
if ((LOCPLINT->localState->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->localState->wanderingHeroes[i]))
if ((LOCPLINT->localState->wanderingHeroes[i]->movement != 0) && !LOCPLINT->localState->isHeroSleeping(LOCPLINT->localState->wanderingHeroes[i]))
return i;
else
return -1;
@@ -431,7 +431,7 @@ void CAdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
return;
}
const CGHeroInstance *nextH = LOCPLINT->localState->wanderingHeroes[next];
bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || LOCPLINT->localState->isHeroSleeping(nextH));
nextHero->block(noActiveHeroes);
if(!h)
@@ -534,17 +534,9 @@ void CAdventureMapInterface::showAll(SDL_Surface * to)
LOCPLINT->cingconsole->show(to);
}
bool CAdventureMapInterface::isHeroSleeping(const CGHeroInstance *hero)
{
if (!hero)
return false;
return vstd::contains(LOCPLINT->localState->sleepingHeroes, hero);
}
void CAdventureMapInterface::onHeroWokeUp(const CGHeroInstance * hero)
{
if (!isHeroSleeping(hero))
if (!LOCPLINT->localState->isHeroSleeping(hero))
return;
sleepWake->clickLeft(true, false);
@@ -557,9 +549,9 @@ void CAdventureMapInterface::onHeroWokeUp(const CGHeroInstance * hero)
void CAdventureMapInterface::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
{
if (sleep)
LOCPLINT->localState->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
LOCPLINT->localState->setHeroAsleep(hero);
else
LOCPLINT->localState->sleepingHeroes -= hero;
LOCPLINT->localState->setHeroAwaken(hero);
onHeroChanged(hero);
}
@@ -981,7 +973,7 @@ void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
// find first non-sleeping hero
for (auto hero : LOCPLINT->localState->wanderingHeroes)
{
if (boost::range::find(LOCPLINT->localState->sleepingHeroes, hero) == LOCPLINT->localState->sleepingHeroes.end())
if (!LOCPLINT->localState->isHeroSleeping(hero))
{
heroToSelect = hero;
break;

View File

@@ -136,7 +136,6 @@ private:
std::optional<Point> keyToMoveDirection(const SDL_Keycode & key);
bool isHeroSleeping(const CGHeroInstance *hero);
void setHeroSleeping(const CGHeroInstance *hero, bool sleep);
int getNextHeroIndex(int startIndex); //for Next Hero button - cycles awake heroes with movement only
void endingTurn();