Skip to content

Commit 5a46da3

Browse files
committed
Add noShip::Sink
1 parent 7280303 commit 5a46da3

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

libs/s25main/nodeObjs/noShip.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,3 +1223,25 @@ void noShip::NewHarborBuilt(nobHarborBuilding* hb)
12231223
break;
12241224
}
12251225
}
1226+
1227+
void noShip::Sink()
1228+
{
1229+
for(auto& figure : figures)
1230+
{
1231+
figure->Abrogate();
1232+
figure->SetGoalTonullptr();
1233+
figure->RemoveFromInventory();
1234+
}
1235+
figures.clear();
1236+
1237+
for(auto& ware : wares)
1238+
{
1239+
ware->WareLost(ownerId_);
1240+
ware->Destroy();
1241+
}
1242+
wares.clear();
1243+
1244+
GetEvMgr().RemoveEvent(current_ev);
1245+
GetEvMgr().AddToKillList(world->RemoveFigure(pos, *this));
1246+
world->RecalcVisibilitiesAroundPoint(pos, GetVisualRange(), ownerId_, nullptr);
1247+
}

libs/s25main/nodeObjs/noShip.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
1+
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -224,4 +224,7 @@ class noShip : public noMovable
224224
void HarborDestroyed(nobHarborBuilding* hb);
225225
/// Sagt dem Schiff, dass ein neuer Hafen erbaut wurde
226226
void NewHarborBuilt(nobHarborBuilding* hb);
227+
228+
/// Destroy the ship immediately
229+
void Sink();
227230
};

tests/s25Main/integration/testSeafaring.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,4 +825,39 @@ BOOST_FIXTURE_TEST_CASE(GoToNeighborHarbor, ShipReadyFixture<1>)
825825
BOOST_TEST_REQUIRE(ship.IsMoving());
826826
}
827827

828+
BOOST_FIXTURE_TEST_CASE(SinkShipLoosesCargo, ShipAndHarborsReadyFixture<1>)
829+
{
830+
const GamePlayer& player = world.GetPlayer(curPlayer);
831+
noShip& ship = *player.GetShipByID(0);
832+
833+
const auto& harbors = player.GetBuildingRegister().GetHarbors();
834+
BOOST_TEST_REQUIRE(harbors.size() >= 2u);
835+
nobHarborBuilding& harbor1 = *harbors.front();
836+
nobHarborBuilding& harbor2 = **(++harbors.begin());
837+
838+
// Transport something
839+
BOOST_TEST_REQUIRE(harbor1.OrderJob(Job::Woodcutter, harbor2, false));
840+
BOOST_TEST_REQUIRE(harbor1.OrderWare(GoodType::Wood, harbor2));
841+
BOOST_TEST_REQUIRE(harbor1.OrderWare(GoodType::Wood, harbor2));
842+
843+
RTTR_EXEC_TILL(90, ship.IsLoading());
844+
RTTR_EXEC_TILL(200, ship.IsMoving());
845+
BOOST_TEST(ship.GetWares().size() == 2u);
846+
BOOST_TEST(ship.GetFigures().size() == 1u);
847+
848+
const auto shipPos = ship.GetPos();
849+
BOOST_TEST_REQUIRE(player.GetNumShips() == 1u);
850+
BOOST_TEST_REQUIRE(world.GetFigures(shipPos).size() == 1u);
851+
BOOST_TEST_REQUIRE(dynamic_cast<noShip*>(&world.GetFigures(shipPos).front()));
852+
853+
const auto numWood = player.GetInventory()[GoodType::Wood];
854+
const auto numWoodcutters = player.GetInventory()[Job::Woodcutter];
855+
ship.Sink();
856+
RTTR_SKIP_GFS(1); // Handle delayed destruction
857+
BOOST_TEST(player.GetNumShips() == 0u);
858+
BOOST_TEST(world.GetFigures(shipPos).size() == 0u);
859+
BOOST_TEST(player.GetInventory()[GoodType::Wood] == numWood - 2);
860+
BOOST_TEST(player.GetInventory()[Job::Woodcutter] == numWoodcutters - 1);
861+
}
862+
828863
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)