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:
@@ -127,3 +127,26 @@ void PlayerLocalState::setSelection(const CArmedInstance *selection)
|
|||||||
{
|
{
|
||||||
currentSelection = 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);
|
||||||
|
}
|
||||||
|
@@ -30,6 +30,7 @@ class PlayerLocalState
|
|||||||
const CArmedInstance * currentSelection;
|
const CArmedInstance * currentSelection;
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
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,10 +54,13 @@ public:
|
|||||||
|
|
||||||
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
|
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);
|
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);
|
void setPath(const CGHeroInstance *h, const CGPath & path);
|
||||||
bool setPath(const CGHeroInstance *h, const int3 & destination);
|
bool setPath(const CGHeroInstance *h, const int3 & destination);
|
||||||
|
|
||||||
|
@@ -291,7 +291,7 @@ void CAdventureMapInterface::fsleepWake()
|
|||||||
const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
|
const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
|
||||||
if (!h)
|
if (!h)
|
||||||
return;
|
return;
|
||||||
bool newSleep = !isHeroSleeping(h);
|
bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
|
||||||
setHeroSleeping(h, newSleep);
|
setHeroSleeping(h, newSleep);
|
||||||
updateSleepWake(h);
|
updateSleepWake(h);
|
||||||
if (newSleep)
|
if (newSleep)
|
||||||
@@ -354,7 +354,7 @@ void CAdventureMapInterface::fendTurn()
|
|||||||
{
|
{
|
||||||
for(auto hero : LOCPLINT->localState->wanderingHeroes)
|
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:
|
// Only show hero reminder if conditions met:
|
||||||
// - There still movement points
|
// - There still movement points
|
||||||
@@ -384,7 +384,7 @@ void CAdventureMapInterface::updateSleepWake(const CGHeroInstance *h)
|
|||||||
sleepWake->block(!h);
|
sleepWake->block(!h);
|
||||||
if (!h)
|
if (!h)
|
||||||
return;
|
return;
|
||||||
bool state = isHeroSleeping(h);
|
bool state = LOCPLINT->localState->isHeroSleeping(h);
|
||||||
sleepWake->setIndex(state ? 1 : 0, true);
|
sleepWake->setIndex(state ? 1 : 0, true);
|
||||||
sleepWake->assignedKeys.clear();
|
sleepWake->assignedKeys.clear();
|
||||||
sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
|
sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
|
||||||
@@ -408,9 +408,9 @@ int CAdventureMapInterface::getNextHeroIndex(int startIndex)
|
|||||||
if (i >= LOCPLINT->localState->wanderingHeroes.size())
|
if (i >= LOCPLINT->localState->wanderingHeroes.size())
|
||||||
i = 0;
|
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;
|
return i;
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
@@ -431,7 +431,7 @@ void CAdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const CGHeroInstance *nextH = LOCPLINT->localState->wanderingHeroes[next];
|
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);
|
nextHero->block(noActiveHeroes);
|
||||||
|
|
||||||
if(!h)
|
if(!h)
|
||||||
@@ -534,17 +534,9 @@ void CAdventureMapInterface::showAll(SDL_Surface * to)
|
|||||||
LOCPLINT->cingconsole->show(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)
|
void CAdventureMapInterface::onHeroWokeUp(const CGHeroInstance * hero)
|
||||||
{
|
{
|
||||||
if (!isHeroSleeping(hero))
|
if (!LOCPLINT->localState->isHeroSleeping(hero))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
sleepWake->clickLeft(true, false);
|
sleepWake->clickLeft(true, false);
|
||||||
@@ -557,9 +549,9 @@ void CAdventureMapInterface::onHeroWokeUp(const CGHeroInstance * hero)
|
|||||||
void CAdventureMapInterface::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
|
void CAdventureMapInterface::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
|
||||||
{
|
{
|
||||||
if (sleep)
|
if (sleep)
|
||||||
LOCPLINT->localState->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
|
LOCPLINT->localState->setHeroAsleep(hero);
|
||||||
else
|
else
|
||||||
LOCPLINT->localState->sleepingHeroes -= hero;
|
LOCPLINT->localState->setHeroAwaken(hero);
|
||||||
|
|
||||||
onHeroChanged(hero);
|
onHeroChanged(hero);
|
||||||
}
|
}
|
||||||
@@ -981,7 +973,7 @@ void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
|
|||||||
// find first non-sleeping hero
|
// find first non-sleeping hero
|
||||||
for (auto hero : LOCPLINT->localState->wanderingHeroes)
|
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;
|
heroToSelect = hero;
|
||||||
break;
|
break;
|
||||||
|
@@ -136,7 +136,6 @@ private:
|
|||||||
|
|
||||||
std::optional<Point> keyToMoveDirection(const SDL_Keycode & key);
|
std::optional<Point> keyToMoveDirection(const SDL_Keycode & key);
|
||||||
|
|
||||||
bool isHeroSleeping(const CGHeroInstance *hero);
|
|
||||||
void setHeroSleeping(const CGHeroInstance *hero, bool sleep);
|
void setHeroSleeping(const CGHeroInstance *hero, bool sleep);
|
||||||
int getNextHeroIndex(int startIndex); //for Next Hero button - cycles awake heroes with movement only
|
int getNextHeroIndex(int startIndex); //for Next Hero button - cycles awake heroes with movement only
|
||||||
void endingTurn();
|
void endingTurn();
|
||||||
|
Reference in New Issue
Block a user