Skip to content

Commit e12ce7b

Browse files
committed
Only stop own soldiers from leaving attacked building
The idea is to potentially use those for defense. However e.g. harbors might contain soldiers that belong to another building and are about to leave. Don't consider those as this complicates the logic too much. Currently it crashed because adding "active" soldiers is assumed to be either the defender or a soldier on a mission from this building. Other soldiers will get lost in the accounting of their home building. Fixes #1912
1 parent 60542a4 commit e12ce7b

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

libs/s25main/buildings/nobBaseMilitary.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,8 @@ bool nobBaseMilitary::CallDefender(nofAttacker& attacker)
224224
// Stop attacking soldiers (including aggressive defenders) from leaving this building
225225
for(auto it = leave_house.begin(); it != leave_house.end();)
226226
{
227-
if(!dynamic_cast<nofActiveSoldier*>(it->get()))
228-
++it;
229-
else
227+
const auto* sldPtr = dynamic_cast<nofActiveSoldier*>(it->get());
228+
if(sldPtr && helpers::contains(troops_on_mission, sldPtr))
230229
{
231230
auto soldier = boost::static_pointer_cast<nofActiveSoldier>(std::move(*it));
232231
// At this point there shouldn't be a defender leaving as we are just requesting one
@@ -236,6 +235,12 @@ bool nobBaseMilitary::CallDefender(nofAttacker& attacker)
236235

237236
soldier->InformTargetsAboutCancelling();
238237
AddActiveSoldier(std::move(soldier));
238+
} else
239+
{
240+
// If a leaving active soldier is not on a mission he doesn't belong to this building.
241+
// E.g. soldiers in harbors could be about to go home.
242+
RTTR_Assert(!sldPtr || sldPtr->GetHomeBld() != this);
243+
++it;
239244
}
240245
}
241246
// Use existing defender (e.g. just going back in) if possible

libs/s25main/figures/nofSoldier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class nofSoldier : public noFigure
4141
unsigned char GetRank() const;
4242
unsigned char GetHitpoints() const;
4343
bool HasNoHome() const { return homeBld == nullptr; }
44+
const nobBaseMilitary* GetHomeBld() const { return homeBld; }
4445
};
4546

4647
/// Comparator to sort soldiers by rank and armor (and ID for ties), weak ones first

0 commit comments

Comments
 (0)