• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 25985372463

17 May 2026 08:03AM UTC coverage: 50.362% (+0.08%) from 50.284%
25985372463

Pull #1917

github

web-flow
Merge 5d7afa219 into 57b082981
Pull Request #1917: Fix sea path finding (for ships)

118 of 166 new or added lines in 13 files covered. (71.08%)

5 existing lines in 3 files now uncovered.

23215 of 46096 relevant lines covered (50.36%)

47651.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.24
/libs/s25main/nodeObjs/noShip.cpp
1
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "noShip.h"
6
#include "EventManager.h"
7
#include "GameEvent.h"
8
#include "GamePlayer.h"
9
#include "GlobalGameSettings.h"
10
#include "Loader.h"
11
#include "SerializedGameData.h"
12
#include "Ware.h"
13
#include "addons/const_addons.h"
14
#include "buildings/nobHarborBuilding.h"
15
#include "figures/noFigure.h"
16
#include "figures/nofAttacker.h"
17
#include "helpers/EnumArray.h"
18
#include "helpers/containerUtils.h"
19
#include "helpers/pointerContainerUtils.h"
20
#include "network/GameClient.h"
21
#include "notifications/ExpeditionNote.h"
22
#include "notifications/ShipNote.h"
23
#include "ogl/glArchivItem_Bitmap.h"
24
#include "ogl/glArchivItem_Bitmap_Player.h"
25
#include "pathfinding/ShipPathData.h"
26
#include "postSystem/ShipPostMsg.h"
27
#include "random/Random.h"
28
#include "world/GameWorld.h"
29
#include "gameData/BuildingConsts.h"
30
#include "gameData/ShipNames.h"
31
#include "s25util/Log.h"
32
#include <array>
33

34
/// Zeit zum Beladen des Schiffes
35
const unsigned LOADING_TIME = 200;
36
/// Zeit zum Entladen des Schiffes
37
const unsigned UNLOADING_TIME = 200;
38

39
/// Maximaler Weg, der zurückgelegt werden kann bei einem Erkundungsschiff
40
const unsigned MAX_EXPLORATION_EXPEDITION_DISTANCE = 100;
41
/// Zeit (in GF), die das Schiff bei der Erkundungs-Expedition jeweils an einem Punkt ankert
42
const unsigned EXPLORATION_EXPEDITION_WAITING_TIME = 300;
43

44
/// Positionen der Flaggen am Schiff für die 6 unterschiedlichen Richtungen jeweils
45
constexpr std::array<helpers::EnumArray<DrawPoint, Direction>, 2> SHIPS_FLAG_POS = {{
46
  {{{-3, -77}, {-6, -71}, {-3, -71}, {-1, -71}, {5, -63}, {-1, -70}}}, // Standing (sails down)
47
  {{{3, -70}, {0, -64}, {3, -64}, {-1, -70}, {5, -63}, {5, -63}}}      // Driving
48
}};
49

50
noShip::noShip(const MapPoint pos, const unsigned char player)
23✔
51
    : noMovable(NodalObjectType::Ship, pos), ownerId_(player), state(State::Idle), goal_dir(0),
52
      name(RANDOM_ELEMENT(ship_names[world->GetPlayer(player).nation])), curRouteIdx(0), lost(false),
23✔
53
      remaining_sea_attackers(0), covered_distance(0)
46✔
54
{
55
    // Meer ermitteln, auf dem dieses Schiff fährt
56
    for(const auto dir : helpers::EnumRange<Direction>{})
368✔
57
    {
58
        SeaId seaId = world->GetNeighbourNode(pos, dir).seaId;
138✔
59
        if(seaId)
138✔
60
            this->seaId_ = seaId;
130✔
61
    }
62

63
    // Auf irgendeinem Meer müssen wir ja sein
64
    RTTR_Assert(seaId_);
23✔
65
}
23✔
66

67
noShip::~noShip() = default;
46✔
68

69
void noShip::Serialize(SerializedGameData& sgd) const
×
70
{
71
    noMovable::Serialize(sgd);
×
72

73
    sgd.PushUnsignedChar(ownerId_);
×
74
    sgd.PushEnum<uint8_t>(state);
×
75
    sgd.PushUnsignedShort(seaId_.value());
×
76
    sgd.PushUnsignedInt(goalHarbor.value());
×
77
    sgd.PushUnsignedChar(goal_dir);
×
78
    sgd.PushString(name);
×
79
    sgd.PushUnsignedInt(curRouteIdx);
×
80
    sgd.PushBool(lost);
×
81
    sgd.PushUnsignedInt(remaining_sea_attackers);
×
82
    sgd.PushUnsignedInt(homeHarbor.value());
×
83
    sgd.PushUnsignedInt(covered_distance);
×
84
    helpers::pushContainer(sgd, route_);
×
85
    sgd.PushObjectContainer(figures);
×
86
    sgd.PushObjectContainer(wares, true);
×
87
}
×
88

89
noShip::noShip(SerializedGameData& sgd, const unsigned obj_id)
×
90
    : noMovable(sgd, obj_id), ownerId_(sgd.PopUnsignedChar()), state(sgd.Pop<State>()), seaId_(sgd.PopUnsignedShort()),
×
91
      goalHarbor(sgd.PopUnsignedInt()), goal_dir(sgd.PopUnsignedChar()),
×
92
      name(sgd.GetGameDataVersion() < 2 ? sgd.PopLongString() : sgd.PopString()), curRouteIdx(sgd.PopUnsignedInt()),
×
93
      route_(sgd.GetGameDataVersion() < 7 ? sgd.PopUnsignedInt() : 0), lost(sgd.PopBool()),
×
94
      remaining_sea_attackers(sgd.PopUnsignedInt()), homeHarbor(sgd.PopUnsignedInt()),
×
95
      covered_distance(sgd.PopUnsignedInt())
×
96
{
97
    helpers::popContainer(sgd, route_, sgd.GetGameDataVersion() < 7);
×
98
    sgd.PopObjectContainer(figures);
×
99
    sgd.PopObjectContainer(wares, GO_Type::Ware);
×
100
}
×
101

102
void noShip::Destroy()
×
103
{
104
    RTTR_Assert(figures.empty());
×
105
    RTTR_Assert(wares.empty());
×
106
    world->GetNotifications().publish(ShipNote(ShipNote::Destroyed, ownerId_, pos));
×
107
    // Schiff wieder abmelden
108
    world->GetPlayer(ownerId_).RemoveShip(this);
×
109
}
×
110

111
void noShip::Draw(DrawPoint drawPt)
×
112
{
113
    unsigned flag_drawing_type = 1;
×
114

115
    // Sind wir verloren? Dann immer stehend zeichnen
116
    if(lost)
×
117
    {
118
        DrawFixed(drawPt, true);
×
119
        return;
×
120
    }
121

122
    switch(state)
×
123
    {
124
        default: break;
×
125
        case State::Idle:
×
126
        case State::SeaattackWaiting:
127
        {
128
            DrawFixed(drawPt, false);
×
129
            flag_drawing_type = 0;
×
130
        }
131
        break;
×
132

133
        case State::Gotoharbor:
×
134
        {
135
            DrawDriving(drawPt);
×
136
        }
137
        break;
×
138
        case State::ExpeditionLoading:
×
139
        case State::ExpeditionUnloading:
140
        case State::TransportLoading:
141
        case State::TransportUnloading:
142
        case State::SeaattackLoading:
143
        case State::SeaattackUnloading:
144
        case State::ExplorationexpeditionLoading:
145
        case State::ExplorationexpeditionUnloading:
146
        {
147
            DrawFixed(drawPt, false);
×
148
        }
149
        break;
×
150
        case State::ExplorationexpeditionWaiting:
×
151
        case State::ExpeditionWaiting:
152
        {
153
            DrawFixed(drawPt, true);
×
154
        }
155
        break;
×
156
        case State::ExpeditionDriving:
×
157
        case State::TransportDriving:
158
        case State::SeaattackDrivingToDestination:
159
        case State::ExplorationexpeditionDriving:
160
        {
161
            DrawDrivingWithWares(drawPt);
×
162
        }
163
        break;
×
164
        case State::SeaattackReturnDriving:
×
165
        {
166
            if(!figures.empty() || !wares.empty())
×
167
                DrawDrivingWithWares(drawPt);
×
168
            else
169
                DrawDriving(drawPt);
×
170
        }
171
        break;
×
172
    }
173

174
    LOADER.GetPlayerImage("boot_z", 40 + GAMECLIENT.GetGlobalAnimation(6, 1, 1, GetObjId()))
×
175
      ->DrawFull(drawPt + SHIPS_FLAG_POS[flag_drawing_type][GetCurMoveDir()], COLOR_WHITE,
×
176
                 world->GetPlayer(ownerId_).color);
×
177
    // Second, white flag, only when on expedition, always swinging in the opposite direction
178
    if(state >= State::ExpeditionLoading && state <= State::ExpeditionDriving)
×
179
        LOADER.GetPlayerImage("boot_z", 40 + GAMECLIENT.GetGlobalAnimation(6, 1, 1, GetObjId() + 4))
×
180
          ->DrawFull(drawPt + SHIPS_FLAG_POS[flag_drawing_type][GetCurMoveDir()]);
×
181
}
182

183
/// Zeichnet das Schiff stehend mit oder ohne Waren
184
void noShip::DrawFixed(DrawPoint drawPt, const bool draw_wares)
×
185
{
186
    LOADER.GetImageN("boot_z", rttr::enum_cast(GetCurMoveDir() + 3u) * 2 + 1)->DrawFull(drawPt, COLOR_SHADOW);
×
187
    LOADER.GetImageN("boot_z", rttr::enum_cast(GetCurMoveDir() + 3u) * 2)->DrawFull(drawPt);
×
188

189
    if(draw_wares)
×
190
        /// Waren zeichnen
191
        LOADER.GetImageN("boot_z", 30 + rttr::enum_cast(GetCurMoveDir() + 3u))->DrawFull(drawPt);
×
192
}
×
193

194
/// Zeichnet normales Fahren auf dem Meer ohne irgendwelche Güter
195
void noShip::DrawDriving(DrawPoint& drawPt)
×
196
{
197
    // Interpolieren zwischen beiden Knotenpunkten
198
    drawPt += CalcWalkingRelative();
×
199

200
    LOADER.GetImageN("boot_z", 13 + rttr::enum_cast(GetCurMoveDir() + 3u) * 2)->DrawFull(drawPt, COLOR_SHADOW);
×
201
    LOADER.GetImageN("boot_z", 12 + rttr::enum_cast(GetCurMoveDir() + 3u) * 2)->DrawFull(drawPt);
×
202
}
×
203

204
/// Zeichnet normales Fahren auf dem Meer mit Gütern
205
void noShip::DrawDrivingWithWares(DrawPoint& drawPt)
×
206
{
207
    DrawDriving(drawPt);
×
208
    /// Waren zeichnen
209
    LOADER.GetImageN("boot_z", 30 + rttr::enum_cast(GetCurMoveDir() + 3u))->DrawFull(drawPt);
×
210
}
×
211

212
void noShip::HandleEvent(const unsigned id)
509✔
213
{
214
    RTTR_Assert(current_ev);
509✔
215
    RTTR_Assert(current_ev->id == id);
509✔
216
    current_ev = nullptr;
509✔
217

218
    if(id == 0)
509✔
219
    {
220
        // Move event
221
        // neue Position einnehmen
222
        Walk();
476✔
223
        // entscheiden, was als nächstes zu tun ist
224
        Driven();
476✔
225
    } else
226
    {
227
        switch(state)
33✔
228
        {
229
            default:
×
230
                RTTR_Assert(false);
×
231
                LOG.write("Bug detected: Invalid state in ship event");
232
                break;
233
            case State::ExpeditionLoading:
3✔
234
                // Schiff ist nun bereit und Expedition kann beginnen
235
                state = State::ExpeditionWaiting;
3✔
236

237
                // Spieler benachrichtigen
238
                SendPostMessage(ownerId_, std::make_unique<ShipPostMsg>(GetEvMgr().GetCurrentGF(),
3✔
239
                                                                        _("A ship is ready for an expedition."),
3✔
240
                                                                        PostCategory::Economy, *this));
3✔
241
                world->GetNotifications().publish(ExpeditionNote(ExpeditionNote::Waiting, ownerId_, pos));
3✔
242
                break;
3✔
243
            case State::ExplorationexpeditionLoading:
8✔
244
            case State::ExplorationexpeditionWaiting:
245
                // Schiff ist nun bereit und Expedition kann beginnen
246
                ContinueExplorationExpedition();
8✔
247
                break;
8✔
248
            case State::ExpeditionUnloading:
1✔
249
            {
250
                // Hafen herausfinden
251
                noBase* hb = goalHarbor ? world->GetNO(world->GetHarborPoint(goalHarbor)) : nullptr;
1✔
252

253
                if(hb && hb->GetGOT() == GO_Type::NobHarborbuilding)
1✔
254
                {
255
                    GoodsAndPeopleCounts goods;
1✔
256
                    goods.goods[GoodType::Boards] = BUILDING_COSTS[BuildingType::HarborBuilding].boards;
1✔
257
                    goods.goods[GoodType::Stones] = BUILDING_COSTS[BuildingType::HarborBuilding].stones;
1✔
258
                    goods.people[Job::Builder] = 1;
1✔
259
                    static_cast<nobBaseWarehouse*>(hb)->AddToInventory(goods, false);
1✔
260
                    // Wieder idlen und ggf. neuen Job suchen
261
                    StartIdling();
1✔
262
                    world->GetPlayer(ownerId_).GetJobForShip(*this);
1✔
263
                } else
264
                {
265
                    // target harbor for unloading doesnt exist anymore -> set state to driving and handle the new state
266
                    state = State::ExpeditionDriving;
×
267
                    HandleState_ExpeditionDriving();
×
268
                }
269
                break;
1✔
270
            }
271
            case State::ExplorationexpeditionUnloading:
3✔
272
            {
273
                // Hafen herausfinden
274
                noBase* hb = goalHarbor ? world->GetNO(world->GetHarborPoint(goalHarbor)) : nullptr;
3✔
275

276
                unsigned old_visual_range = GetVisualRange();
3✔
277

278
                if(hb && hb->GetGOT() == GO_Type::NobHarborbuilding)
3✔
279
                {
280
                    // Späher wieder entladen
281
                    const auto people = PeopleCounts::make(Job::Scout, world->GetGGS().GetNumScoutsExpedition());
3✔
282
                    static_cast<nobBaseWarehouse*>(hb)->AddToInventory(people, false);
3✔
283
                    // Wieder idlen und ggf. neuen Job suchen
284
                    StartIdling();
3✔
285
                    world->GetPlayer(ownerId_).GetJobForShip(*this);
3✔
286
                } else
287
                {
288
                    // target harbor for unloading doesnt exist anymore -> set state to driving and handle the new state
289
                    state = State::ExplorationexpeditionDriving;
×
290
                    HandleState_ExplorationExpeditionDriving();
×
291
                }
292

293
                // Sichtbarkeiten neu berechnen
294
                world->RecalcVisibilitiesAroundPoint(pos, old_visual_range, ownerId_, nullptr);
3✔
295

296
                break;
3✔
297
            }
298
            case State::TransportLoading: StartTransport(); break;
5✔
299
            case State::TransportUnloading:
5✔
300
            case State::SeaattackUnloading:
301
            {
302
                // Hafen herausfinden
303
                RTTR_Assert(state == State::SeaattackUnloading || remaining_sea_attackers == 0);
5✔
304
                noBase* hb = goalHarbor ? world->GetNO(world->GetHarborPoint(goalHarbor)) : nullptr;
5✔
305
                if(hb && hb->GetGOT() == GO_Type::NobHarborbuilding)
5✔
306
                {
307
                    static_cast<nobHarborBuilding*>(hb)->ReceiveGoodsFromShip(figures, wares);
5✔
308
                    figures.clear();
5✔
309
                    wares.clear();
5✔
310

311
                    state = State::TransportUnloading;
5✔
312
                    // Hafen bescheid sagen, dass er das Schiff nun nutzen kann
313
                    static_cast<nobHarborBuilding*>(hb)->ShipArrived(*this);
5✔
314

315
                    // Hafen hat keinen Job für uns?
316
                    if(state == State::TransportUnloading)
5✔
317
                    {
318
                        // Wieder idlen und ggf. neuen Job suchen
319
                        StartIdling();
5✔
320
                        world->GetPlayer(ownerId_).GetJobForShip(*this);
5✔
321
                    }
322
                } else
323
                {
324
                    // target harbor for unloading doesnt exist anymore -> set state to driving and handle the new state
325
                    if(state == State::TransportUnloading)
×
326
                        FindUnloadGoal(State::TransportDriving);
×
327
                    else
328
                        FindUnloadGoal(State::SeaattackReturnDriving);
×
329
                }
330
                break;
5✔
331
            }
332
            case State::SeaattackLoading: StartSeaAttack(); break;
2✔
333
            case State::SeaattackWaiting:
6✔
334
            {
335
                // Nächsten Soldaten nach draußen beordern
336
                if(figures.empty())
6✔
337
                    break;
2✔
338

339
                // Evtl. ist ein Angreifer schon fertig und wieder an Board gegangen
340
                // der darf dann natürlich nicht noch einmal raus, sonst kann die schöne Reise
341
                // böse enden
342
                if(static_cast<nofAttacker&>(*figures.front()).IsSeaAttackCompleted())
4✔
343
                    break;
×
344

345
                auto& attacker = world->AddFigure(pos, std::move(figures.front()));
4✔
346
                figures.pop_front();
4✔
347

348
                current_ev = GetEvMgr().AddEvent(this, 30, 1);
4✔
349
                static_cast<nofAttacker&>(attacker).StartAttackOnOtherIsland(pos, GetObjId());
4✔
350
                break;
4✔
351
            }
352
        }
353
    }
354
}
509✔
355

356
void noShip::StartDriving(const Direction dir)
482✔
357
{
358
    const std::array<unsigned, 5> SHIP_SPEEDS = {35, 25, 20, 10, 5};
482✔
359

360
    StartMoving(dir, SHIP_SPEEDS[world->GetGGS().getSelection(AddonId::SHIP_SPEED)]);
482✔
361
}
482✔
362

363
void noShip::Driven()
479✔
364
{
365
    MapPoint enemy_territory_discovered(MapPoint::Invalid());
479✔
366
    world->RecalcMovingVisibilities(pos, ownerId_, GetVisualRange(), GetCurMoveDir(), &enemy_territory_discovered);
479✔
367

368
    // Feindliches Territorium entdeckt?
369
    if(enemy_territory_discovered.isValid())
479✔
370
    {
371
        // Send message if necessary
372
        if(world->GetPlayer(ownerId_).ShipDiscoveredHostileTerritory(enemy_territory_discovered))
25✔
373
            SendPostMessage(ownerId_, std::make_unique<PostMsg>(GetEvMgr().GetCurrentGF(),
1✔
374
                                                                _("A ship disovered an enemy territory"),
1✔
375
                                                                PostCategory::Military, enemy_territory_discovered));
2✔
376
    }
377

378
    switch(state)
479✔
379
    {
380
        case State::Gotoharbor: HandleState_GoToHarbor(); break;
44✔
381
        case State::ExpeditionDriving: HandleState_ExpeditionDriving(); break;
58✔
382
        case State::ExplorationexpeditionDriving: HandleState_ExplorationExpeditionDriving(); break;
189✔
383
        case State::TransportDriving: HandleState_TransportDriving(); break;
143✔
384
        case State::SeaattackDrivingToDestination: HandleState_SeaAttackDriving(); break;
31✔
385
        case State::SeaattackReturnDriving: HandleState_SeaAttackReturn(); break;
14✔
386
        default: RTTR_Assert(false); break;
×
387
    }
388
}
479✔
389

390
bool noShip::IsLoading() const
15✔
391
{
392
    return state == State::ExpeditionLoading || state == State::ExplorationexpeditionLoading
15✔
393
           || state == State::TransportLoading || state == State::SeaattackLoading;
30✔
394
}
395

396
bool noShip::IsUnloading() const
100✔
397
{
398
    return state == State::ExpeditionUnloading || state == State::ExplorationexpeditionUnloading
100✔
399
           || state == State::TransportUnloading || state == State::SeaattackUnloading;
200✔
400
}
401

402
bool noShip::IsOnBoard(const noFigure& figure) const
×
403
{
404
    return helpers::containsPtr(figures, &figure);
×
405
}
406

407
/// Gibt Sichtradius dieses Schiffes zurück
408
unsigned noShip::GetVisualRange() const
5,837✔
409
{
410
    // Erkundungsschiffe haben einen größeren Sichtbereich
411
    if(state >= State::ExplorationexpeditionLoading && state <= State::ExplorationexpeditionDriving)
5,837✔
412
        return VISUALRANGE_EXPLORATION_SHIP;
3,974✔
413
    else
414
        return VISUALRANGE_SHIP;
1,863✔
415
}
416

417
/// Fährt zum Hafen, um dort eine Mission (Expedition) zu erledigen
418
void noShip::GoToHarbor(const nobHarborBuilding& hb, const std::vector<Direction>& route)
11✔
419
{
420
    RTTR_Assert(state == State::Idle); // otherwise we might carry wares etc
11✔
421
    RTTR_Assert(figures.empty());
11✔
422
    RTTR_Assert(wares.empty());
11✔
423
    RTTR_Assert(remaining_sea_attackers == 0);
11✔
424

425
    state = State::Gotoharbor;
11✔
426

427
    goalHarbor = world->GetNode(hb.GetPos()).harborId;
11✔
428
    RTTR_Assert(goalHarbor);
11✔
429

430
    // Route merken
431
    this->route_ = route;
11✔
432
    curRouteIdx = 1;
11✔
433

434
    // losfahren
435
    StartDriving(route[0]);
11✔
436
}
11✔
437

438
/// Startet eine Expedition
439
void noShip::StartExpedition(HarborId homeHarborId)
3✔
440
{
441
    /// Schiff wird "beladen", also kurze Zeit am Hafen stehen, bevor wir bereit sind
442
    state = State::ExpeditionLoading;
3✔
443
    current_ev = GetEvMgr().AddEvent(this, LOADING_TIME, 1);
3✔
444
    RTTR_Assert(homeHarborId);
3✔
445
    RTTR_Assert(pos == world->GetCoastalPoint(homeHarborId, seaId_));
3✔
446
    homeHarbor = homeHarborId;
3✔
447
    goalHarbor = homeHarborId; // This is current goal (commands are relative to current goal)
3✔
448
}
3✔
449

450
/// Startet eine Erkundungs-Expedition
451
void noShip::StartExplorationExpedition(HarborId homeHarborId)
4✔
452
{
453
    /// Schiff wird "beladen", also kurze Zeit am Hafen stehen, bevor wir bereit sind
454
    state = State::ExplorationexpeditionLoading;
4✔
455
    current_ev = GetEvMgr().AddEvent(this, LOADING_TIME, 1);
4✔
456
    covered_distance = 0;
4✔
457
    RTTR_Assert(homeHarborId);
4✔
458
    RTTR_Assert(pos == world->GetCoastalPoint(homeHarborId, seaId_));
4✔
459
    homeHarbor = homeHarborId;
4✔
460
    goalHarbor = homeHarborId; // This is current goal (commands are relative to current goal)
4✔
461
    // Sichtbarkeiten neu berechnen
462
    world->MakeVisibleAroundPoint(pos, GetVisualRange(), ownerId_);
4✔
463
}
4✔
464

465
/// Fährt weiter zu einem Hafen
466
noShip::Result noShip::DriveToHarbour()
326✔
467
{
468
    if(!goalHarbor)
326✔
469
        return Result::HarborDoesntExist;
4✔
470
    const MapPoint goal = world->GetHarborPoint(goalHarbor);
322✔
471

472
    // Existiert der Hafen überhaupt noch?
473
    if(world->GetGOT(goal) != GO_Type::NobHarborbuilding)
322✔
474
        return Result::HarborDoesntExist;
×
475

476
    return DriveToHarbourPlace();
322✔
477
}
478

479
/// Fährt weiter zu Hafenbauplatz
480
noShip::Result noShip::DriveToHarbourPlace()
500✔
481
{
482
    if(!goalHarbor)
500✔
483
        return Result::HarborDoesntExist;
×
484

485
    // Sind wir schon da?
486
    if(curRouteIdx == route_.size())
500✔
487
        return Result::GoalReached;
29✔
488

489
    MapPoint goalRoutePos;
471✔
490

491
    // Route überprüfen
492
    if(!world->CheckShipRoute(pos, route_, curRouteIdx, &goalRoutePos))
471✔
493
    {
494
        // Route kann nicht mehr passiert werden --> neue Route suchen
495
        if(!world->FindShipPathToHarbor(pos, goalHarbor, seaId_, &route_, nullptr))
×
496
        {
497
            // Wieder keine gefunden -> raus
498
            return Result::NoRouteFound;
×
499
        }
500

501
        // Wir fangen bei der neuen Route wieder von vorne an
502
        curRouteIdx = 0;
×
503
    } else if(goalRoutePos != world->GetCoastalPoint(goalHarbor, seaId_))
471✔
504
    {
505
        // Our goal point of the current route has changed
506
        // If we are close to it, recalculate the route
507
        RTTR_Assert(route_.size() >= curRouteIdx);
×
508
        if(route_.size() - curRouteIdx < 10)
×
509
        {
510
            if(!world->FindShipPathToHarbor(pos, goalHarbor, seaId_, &route_, nullptr))
×
511
                // Keiner gefunden -> raus
512
                return Result::NoRouteFound;
×
513

514
            curRouteIdx = 0;
×
515
        }
516
    }
517

518
    RTTR_Assert(curRouteIdx < route_.size());
471✔
519
    StartDriving(route_[curRouteIdx++]);
471✔
520
    return Result::Driving;
471✔
521
}
522

523
HarborId noShip::GetCurrentHarbor() const
13✔
524
{
525
    RTTR_Assert(state == State::ExpeditionWaiting);
13✔
526
    return goalHarbor;
13✔
527
}
528

529
HarborId noShip::GetTargetHarbor() const
51✔
530
{
531
    return goalHarbor;
51✔
532
}
533

534
HarborId noShip::GetHomeHarbor() const
13✔
535
{
536
    return homeHarbor;
13✔
537
}
538

539
/// Weist das Schiff an, in einer bestimmten Richtung die Expedition fortzusetzen
540
void noShip::ContinueExpedition(const ShipDirection dir)
8✔
541
{
542
    if(state != State::ExpeditionWaiting)
8✔
543
        return;
3✔
544

545
    // Nächsten Hafenpunkt in dieser Richtung suchen
546
    HarborId new_goal = world->GetNextFreeHarborPoint(pos, goalHarbor, dir, ownerId_);
6✔
547

548
    // Auch ein Ziel gefunden?
549
    if(!new_goal)
6✔
550
        return;
1✔
551

552
    // Versuchen, Weg zu finden
553
    if(!world->FindShipPathToHarbor(pos, new_goal, seaId_, &route_, nullptr))
5✔
554
        return;
×
555

556
    // Dann fahren wir da mal hin
557
    curRouteIdx = 0;
5✔
558
    goalHarbor = new_goal;
5✔
559
    state = State::ExpeditionDriving;
5✔
560

561
    HandleState_ExpeditionDriving();
5✔
562
}
563

564
/// Weist das Schiff an, eine Expedition abzubrechen (nur wenn es steht) und zum
565
/// Hafen zurückzukehren
566
void noShip::CancelExpedition()
3✔
567
{
568
    // Protect against double execution
569
    if(state != State::ExpeditionWaiting)
3✔
570
        return;
2✔
571

572
    // We are waiting. There should be no event!
573
    RTTR_Assert(!current_ev);
1✔
574

575
    // Zum Heimathafen zurückkehren
576
    // Oder sind wir schon dort?
577
    if(goalHarbor == homeHarbor)
1✔
578
    {
579
        route_.clear();
×
580
        curRouteIdx = 0;
×
581
        state = State::ExpeditionDriving; // just in case the home harbor was destroyed
×
582
        HandleState_ExpeditionDriving();
×
583
    } else
584
    {
585
        state = State::ExpeditionDriving;
1✔
586
        goalHarbor = homeHarbor;
1✔
587
        StartDrivingToHarborPlace();
1✔
588
        HandleState_ExpeditionDriving();
1✔
589
    }
590
}
591

592
/// Weist das Schiff an, an der aktuellen Position einen Hafen zu gründen
593
void noShip::FoundColony()
4✔
594
{
595
    if(state != State::ExpeditionWaiting)
4✔
596
        return;
2✔
597

598
    // Kolonie gründen
599
    if(world->FoundColony(goalHarbor, ownerId_, seaId_))
2✔
600
    {
601
        // For checks
602
        state = State::ExpeditionUnloading;
1✔
603
        // Dann idlen wir wieder
604
        StartIdling();
1✔
605
        // Neue Arbeit suchen
606
        world->GetPlayer(ownerId_).GetJobForShip(*this);
1✔
607
    } else // colony founding FAILED
608
        world->GetNotifications().publish(ExpeditionNote(ExpeditionNote::Waiting, ownerId_, pos));
1✔
609
}
610

611
void noShip::HandleState_GoToHarbor()
44✔
612
{
613
    // Hafen schon zerstört?
614
    if(!goalHarbor)
44✔
615
    {
616
        StartIdling();
1✔
617
        return;
1✔
618
    }
619

620
    Result res = DriveToHarbour();
43✔
621
    switch(res)
43✔
622
    {
623
        case Result::Driving: return; // Continue
34✔
624
        case Result::GoalReached:
9✔
625
        {
626
            const MapPoint goal = world->GetHarborPoint(goalHarbor);
9✔
627
            // Go idle here (if harbor does not need it)
628
            StartIdling();
9✔
629
            // Hafen Bescheid sagen, dass wir da sind (falls er überhaupt noch existiert)
630
            noBase* hb = goal.isValid() ? world->GetNO(goal) : nullptr;
9✔
631
            if(hb && hb->GetGOT() == GO_Type::NobHarborbuilding)
9✔
632
                static_cast<nobHarborBuilding*>(hb)->ShipArrived(*this);
9✔
633
        }
634
        break;
9✔
635
        case Result::NoRouteFound:
×
636
        {
637
            MapPoint goal(world->GetHarborPoint(goalHarbor));
×
638
            RTTR_Assert(goal.isValid());
×
639
            // Dem Hafen Bescheid sagen
640
            world->GetSpecObj<nobHarborBuilding>(goal)->ShipLost(this);
×
641
            StartIdling();
×
642
        }
643
        break;
×
644
        case Result::HarborDoesntExist: StartIdling(); break;
×
645
    }
646
}
647

648
void noShip::HandleState_ExpeditionDriving()
64✔
649
{
650
    Result res;
651
    // Zum Heimathafen fahren?
652
    if(homeHarbor == goalHarbor)
64✔
653
        res = DriveToHarbour();
15✔
654
    else
655
        res = DriveToHarbourPlace();
49✔
656

657
    switch(res)
64✔
658
    {
659
        case Result::Driving: return;
59✔
660
        case Result::GoalReached:
5✔
661
        {
662
            // Haben wir unsere Expedition beendet?
663
            if(homeHarbor == goalHarbor)
5✔
664
            {
665
                // Sachen wieder in den Hafen verladen
666
                state = State::ExpeditionUnloading;
1✔
667
                current_ev = GetEvMgr().AddEvent(this, UNLOADING_TIME, 1);
1✔
668
            } else
669
            {
670
                // Warten auf weitere Anweisungen
671
                state = State::ExpeditionWaiting;
4✔
672

673
                // Spieler benachrichtigen
674
                SendPostMessage(
8✔
675
                  ownerId_, std::make_unique<ShipPostMsg>(GetEvMgr().GetCurrentGF(),
12✔
676
                                                          _("A ship has reached the destination of its expedition."),
4✔
677
                                                          PostCategory::Economy, *this));
4✔
678
                world->GetNotifications().publish(ExpeditionNote(ExpeditionNote::Waiting, ownerId_, pos));
4✔
679
            }
680
        }
681
        break;
5✔
682
        case Result::NoRouteFound:
×
683
        case Result::HarborDoesntExist: // should only happen when an expedition is cancelled and the home harbor no
684
                                        // longer exists
685
        {
686
            if(homeHarbor != goalHarbor && homeHarbor)
×
687
            {
688
                // Try to go back
689
                goalHarbor = homeHarbor;
×
690
                HandleState_ExpeditionDriving();
×
691
            } else
692
                FindUnloadGoal(State::ExpeditionDriving); // Unload anywhere!
×
693
        }
694
        break;
×
695
    }
696
}
697

698
void noShip::HandleState_ExplorationExpeditionDriving()
197✔
699
{
700
    Result res;
701
    // Zum Heimathafen fahren?
702
    if(homeHarbor == goalHarbor)
197✔
703
        res = DriveToHarbour();
101✔
704
    else
705
        res = DriveToHarbourPlace();
96✔
706

707
    switch(res)
197✔
708
    {
709
        case Result::Driving: return;
189✔
710
        case Result::GoalReached:
7✔
711
        {
712
            // Haben wir unsere Expedition beendet?
713
            if(homeHarbor == goalHarbor)
7✔
714
            {
715
                // Dann sind wir fertig -> wieder entladen
716
                state = State::ExplorationexpeditionUnloading;
3✔
717
                current_ev = GetEvMgr().AddEvent(this, UNLOADING_TIME, 1);
3✔
718
            } else
719
            {
720
                // Strecke, die wir gefahren sind, draufaddieren
721
                covered_distance += route_.size();
4✔
722
                // Erstmal kurz ausruhen an diesem Punkt und das Rohr ausfahren, um ein bisschen
723
                // auf der Insel zu gucken
724
                state = State::ExplorationexpeditionWaiting;
4✔
725
                current_ev = GetEvMgr().AddEvent(this, EXPLORATION_EXPEDITION_WAITING_TIME, 1);
4✔
726
            }
727
        }
728
        break;
7✔
729
        case Result::NoRouteFound:
1✔
730
        case Result::HarborDoesntExist:
731
            if(homeHarbor != goalHarbor && homeHarbor)
1✔
732
            {
733
                // Try to go back
734
                goalHarbor = homeHarbor;
×
735
                HandleState_ExplorationExpeditionDriving();
×
736
            } else
737
                FindUnloadGoal(State::ExplorationexpeditionDriving); // Unload anywhere!
1✔
738
            break;
1✔
739
    }
740
}
741

742
void noShip::HandleState_TransportDriving()
151✔
743
{
744
    Result res = DriveToHarbour();
151✔
745
    switch(res)
151✔
746
    {
747
        case Result::Driving: return;
143✔
748
        case Result::GoalReached:
5✔
749
        {
750
            // Waren abladen, dafür wieder kurze Zeit hier ankern
751
            state = State::TransportUnloading;
5✔
752
            current_ev = GetEvMgr().AddEvent(this, UNLOADING_TIME, 1);
5✔
753
        }
754
        break;
5✔
755
        case Result::NoRouteFound:
3✔
756
        case Result::HarborDoesntExist:
757
        {
758
            RTTR_Assert(!remaining_sea_attackers);
3✔
759
            // Kein Hafen mehr?
760
            // Dann müssen alle Leute ihren Heimatgebäuden Bescheid geben, dass sie
761
            // nun nicht mehr kommen
762
            // Das Schiff muss einen Notlandeplatz ansteuern
763
            // LOG.write(("transport goal harbor doesnt exist player %i state %i pos %u,%u \n",player,state,x,y);
764
            for(auto& figure : figures)
3✔
765
            {
766
                figure->Abrogate();
×
767
                figure->SetGoalTonullptr();
×
768
            }
769

770
            for(auto& ware : wares)
6✔
771
            {
772
                ware->NotifyGoalAboutLostWare();
3✔
773
            }
774

775
            FindUnloadGoal(State::TransportDriving);
3✔
776
        }
777
        break;
3✔
778
    }
779
}
780

781
void noShip::HandleState_SeaAttackDriving()
33✔
782
{
783
    Result res = DriveToHarbourPlace();
33✔
784
    switch(res)
33✔
785
    {
786
        case Result::Driving: return; // OK
31✔
787
        case Result::GoalReached:
2✔
788
            // Ziel erreicht, dann stellen wir das Schiff hier hin und die Soldaten laufen nacheinander raus zum Ziel
789
            state = State::SeaattackWaiting;
2✔
790
            current_ev = GetEvMgr().AddEvent(this, 15, 1);
2✔
791
            remaining_sea_attackers = figures.size();
2✔
792
            break;
2✔
793
        case Result::NoRouteFound:
×
794
        case Result::HarborDoesntExist:
795
            RTTR_Assert(goalHarbor != homeHarbor || !homeHarbor);
×
796
            AbortSeaAttack();
×
797
            break;
×
798
    }
799
}
800

801
void noShip::HandleState_SeaAttackReturn()
16✔
802
{
803
    Result res = DriveToHarbour();
16✔
804
    switch(res)
16✔
805
    {
806
        case Result::Driving: return;
15✔
807
        case Result::GoalReached:
1✔
808
            // Entladen
809
            state = State::SeaattackUnloading;
1✔
810
            this->current_ev = GetEvMgr().AddEvent(this, UNLOADING_TIME, 1);
1✔
811
            break;
1✔
812
        case Result::HarborDoesntExist:
×
813
        case Result::NoRouteFound: AbortSeaAttack(); break;
×
814
    }
815
}
816

817
/// Gibt zurück, ob das Schiff jetzt in der Lage wäre, eine Kolonie zu gründen
818
bool noShip::IsAbleToFoundColony() const
×
819
{
820
    // Warten wir gerade?
821
    if(state == State::ExpeditionWaiting)
×
822
    {
823
        // We must always have a goal harbor
824
        RTTR_Assert(goalHarbor);
×
825
        // Ist der Punkt, an dem wir gerade ankern, noch frei?
826
        if(world->IsHarborPointFree(goalHarbor, ownerId_))
×
827
            return true;
×
828
    }
829

830
    return false;
×
831
}
832

833
/// Gibt zurück, ob das Schiff einen bestimmten Hafen ansteuert
834
bool noShip::IsGoingToHarbor(const nobHarborBuilding& hb) const
99✔
835
{
836
    if(goalHarbor != hb.GetHarborPosID())
99✔
837
        return false;
87✔
838
    // Explicit switch to check all states
839
    switch(state)
12✔
840
    {
841
        case State::Idle:
1✔
842
        case State::ExpeditionLoading:
843
        case State::ExpeditionUnloading:
844
        case State::ExpeditionWaiting:
845
        case State::ExpeditionDriving:
846
        case State::ExplorationexpeditionLoading:
847
        case State::ExplorationexpeditionUnloading:
848
        case State::ExplorationexpeditionWaiting:
849
        case State::ExplorationexpeditionDriving:
850
        case State::SeaattackLoading:
851
        case State::SeaattackDrivingToDestination:
852
        case State::SeaattackWaiting: return false;
1✔
853
        case State::Gotoharbor:
11✔
854
        case State::TransportDriving:       // Driving to this harbor
855
        case State::TransportLoading:       // Loading at home harbor and going to goal
856
        case State::TransportUnloading:     // Unloading at this harbor
857
        case State::SeaattackUnloading:     // Unloading attackers at this harbor
858
        case State::SeaattackReturnDriving: // Returning attackers to this harbor
859
            return true;
11✔
860
    }
861
    RTTR_Assert(false);
×
862
    return false;
863
}
864

865
/// Belädt das Schiff mit Waren und Figuren, um eine Transportfahrt zu starten
866
void noShip::PrepareTransport(HarborId homeHarborId, MapPoint goal, std::list<std::unique_ptr<noFigure>> figures,
6✔
867
                              std::list<std::unique_ptr<Ware>> wares)
868
{
869
    RTTR_Assert(homeHarborId);
6✔
870
    RTTR_Assert(pos == world->GetCoastalPoint(homeHarborId, seaId_));
6✔
871
    this->homeHarbor = homeHarborId;
6✔
872
    // ID von Zielhafen herausfinden
873
    noBase* nb = world->GetNO(goal);
6✔
874
    RTTR_Assert(nb->GetGOT() == GO_Type::NobHarborbuilding);
6✔
875
    this->goalHarbor = static_cast<nobHarborBuilding*>(nb)->GetHarborPosID();
6✔
876

877
    this->figures = std::move(figures);
6✔
878
    this->wares = std::move(wares);
6✔
879

880
    state = State::TransportLoading;
6✔
881
    current_ev = GetEvMgr().AddEvent(this, LOADING_TIME, 1);
6✔
882
}
6✔
883

884
/// Belädt das Schiff mit Schiffs-Angreifern
885
void noShip::PrepareSeaAttack(HarborId homeHarborId, MapPoint goal, std::vector<std::unique_ptr<nofAttacker>> attackers)
2✔
886
{
887
    // Heimathafen merken
888
    RTTR_Assert(homeHarborId);
2✔
889
    RTTR_Assert(pos == world->GetCoastalPoint(homeHarborId, seaId_));
2✔
890
    homeHarbor = homeHarborId;
2✔
891
    goalHarbor = world->GetHarborPointID(goal);
2✔
892
    RTTR_Assert(goalHarbor);
2✔
893
    figures.clear();
2✔
894
    for(auto& attacker : attackers)
6✔
895
    {
896
        attacker->StartShipJourney();
4✔
897
        attacker->SeaAttackStarted();
4✔
898
        figures.push_back(std::move(attacker));
4✔
899
    }
900
    state = State::SeaattackLoading;
2✔
901
    current_ev = GetEvMgr().AddEvent(this, LOADING_TIME, 1);
2✔
902
}
2✔
903

904
/// Startet Schiffs-Angreiff
905
void noShip::StartSeaAttack()
2✔
906
{
907
    state = State::SeaattackDrivingToDestination;
2✔
908
    StartDrivingToHarborPlace();
2✔
909
    HandleState_SeaAttackDriving();
2✔
910
}
2✔
911

912
void noShip::AbortSeaAttack()
×
913
{
914
    RTTR_Assert(state != State::SeaattackWaiting); // figures are not aboard if this fails!
×
915
    RTTR_Assert(remaining_sea_attackers == 0);     // Some soldiers are still not aboard
×
916

917
    if((state == State::SeaattackLoading || state == State::SeaattackDrivingToDestination) && goalHarbor != homeHarbor
×
918
       && homeHarbor)
×
919
    {
920
        // We did not start the attack yet and we can (possibly) go back to our home harbor
921
        // -> tell the soldiers we go back (like after an attack)
922
        goalHarbor = homeHarbor;
×
923
        for(auto& figure : figures)
×
924
            checkedCast<nofAttacker*>(figure.get())->StartReturnViaShip(*this);
×
925
        if(state == State::SeaattackLoading)
×
926
        {
927
            // We are still loading (loading event must be active)
928
            // -> Use it to unload
929
            RTTR_Assert(current_ev);
×
930
            state = State::SeaattackUnloading;
×
931
        } else
932
        {
933
            // Else start driving back
934
            state = State::SeaattackReturnDriving;
×
935
            HandleState_SeaAttackReturn();
×
936
        }
937
    } else
938
    {
939
        // attack failed and we cannot go back to our home harbor
940
        // -> Tell figures that they won't go to their planned destination
941
        for(auto& figure : figures)
×
942
            checkedCast<nofAttacker*>(figure.get())->CancelSeaAttack();
×
943

944
        if(state == State::SeaattackLoading)
×
945
        {
946
            // Abort loading
947
            RTTR_Assert(current_ev);
×
948
            GetEvMgr().RemoveEvent(current_ev);
×
949
        }
950

951
        // Das Schiff muss einen Notlandeplatz ansteuern
952
        FindUnloadGoal(State::SeaattackReturnDriving);
×
953
    }
954
}
×
955

956
void noShip::StartDrivingToHarborPlace()
21✔
957
{
958
    if(!goalHarbor)
21✔
959
    {
960
        route_.clear();
1✔
961
        curRouteIdx = 0;
1✔
962
        return;
1✔
963
    }
964

965
    MapPoint coastalPos = world->GetCoastalPoint(goalHarbor, seaId_);
20✔
966
    if(pos == coastalPos)
20✔
967
        route_.clear();
1✔
968
    else
969
    {
970
        // if we still have and are at the home harbor get route directly
971
        if(homeHarbor && pos == world->GetCoastalPoint(homeHarbor, seaId_))
19✔
972
        {
973
            route_ = world->GetShipPathData().getHarborConnection(homeHarbor, goalHarbor, seaId_);
9✔
974
            RTTR_Assert(!route_.empty());
9✔
975
        } else
976
        {
977
            if(!world->FindShipPathToHarbor(pos, goalHarbor, seaId_, &route_, nullptr))
10✔
978
            {
979
                // todo
NEW
980
                RTTR_Assert(false);
×
981
                LOG.write(
982
                  "WARNING: Bug detected (GF: %u). Please report this with the savegame and "
983
                  "replay.\nnoShip::StartDrivingToHarborPlace: Schiff hat keinen Weg gefunden!\nplayer %i state %i "
984
                  "pos %u,%u goal "
985
                  "coastal %u,%u goal-id %i goalpos %u,%u \n")
986
                  % GetEvMgr().GetCurrentGF() % unsigned(ownerId_) % unsigned(state) % pos.x % pos.y % coastalPos.x
987
                  % coastalPos.y % goalHarbor % world->GetHarborPoint(goalHarbor).x
988
                  % world->GetHarborPoint(goalHarbor).y;
989
                goalHarbor.reset();
990
                return;
991
            }
992
        }
993
    }
994
    curRouteIdx = 0;
20✔
995
}
996

997
/// Startet die eigentliche Transportaktion, nachdem das Schiff beladen wurde
998
void noShip::StartTransport()
5✔
999
{
1000
    state = State::TransportDriving;
5✔
1001

1002
    StartDrivingToHarborPlace();
5✔
1003
    // Einfach weiterfahren
1004
    HandleState_TransportDriving();
5✔
1005
}
5✔
1006

1007
void noShip::FindUnloadGoal(State newState)
6✔
1008
{
1009
    state = newState;
6✔
1010
    // Das Schiff muss einen Notlandeplatz ansteuern
1011
    // Neuen Hafen suchen
1012
    if(world->GetPlayer(ownerId_).FindHarborForUnloading(this, pos, &goalHarbor, &route_, nullptr))
6✔
1013
    {
1014
        curRouteIdx = 0;
3✔
1015
        homeHarbor = goalHarbor; // To allow unloading here
3✔
1016
        if(state == State::ExpeditionDriving)
3✔
1017
            HandleState_ExpeditionDriving();
×
1018
        else if(state == State::ExplorationexpeditionDriving)
3✔
1019
            HandleState_ExplorationExpeditionDriving();
×
1020
        else if(state == State::TransportDriving)
3✔
1021
            HandleState_TransportDriving();
3✔
1022
        else if(state == State::SeaattackReturnDriving)
×
1023
            HandleState_SeaAttackReturn();
×
1024
        else
1025
        {
1026
            RTTR_Assert(false);
×
1027
            LOG.write("Bug detected: Invalid state for FindUnloadGoal");
1028
            FindUnloadGoal(State::TransportDriving);
1029
        }
1030
    } else
1031
    {
1032
        // Ansonsten als verloren markieren, damit uns später Bescheid gesagt wird
1033
        // wenn es einen neuen Hafen gibt
1034
        homeHarbor.reset();
3✔
1035
        goalHarbor.reset();
3✔
1036
        lost = true;
3✔
1037
    }
1038
}
6✔
1039

1040
/// Sagt dem Schiff, das ein bestimmter Hafen zerstört wurde
1041
void noShip::HarborDestroyed(nobHarborBuilding* hb)
14✔
1042
{
1043
    const HarborId destroyedHarborId = hb->GetHarborPosID();
14✔
1044
    // Almost every case of a destroyed harbor is handled when the ships event fires (the handler detects the destroyed
1045
    // harbor) So mostly we just reset the corresponding id
1046

1047
    if(destroyedHarborId == homeHarbor)
14✔
1048
        homeHarbor.reset();
5✔
1049

1050
    // Ist unser Ziel betroffen?
1051
    if(destroyedHarborId != goalHarbor)
14✔
1052
        return;
11✔
1053

1054
    State oldState = state;
8✔
1055

1056
    switch(state)
8✔
1057
    {
1058
        default:
5✔
1059
            // Just reset goal, but not for expeditions
1060
            if(!IsOnExpedition() && !IsOnExplorationExpedition())
5✔
1061
                goalHarbor.reset();
4✔
1062
            return; // Skip the rest
5✔
1063
        case State::TransportLoading:
3✔
1064
        case State::TransportUnloading:
1065
            // Tell wares and figures that they won't reach their goal
1066
            for(auto& figure : figures)
3✔
1067
            {
1068
                figure->Abrogate();
×
1069
                figure->SetGoalTonullptr();
×
1070
            }
1071
            for(auto& ware : wares)
6✔
1072
            {
1073
                // Notify goal only, if it is not the destroyed harbor. It already knows about that ;)
1074
                if(ware->GetGoal() != hb)
3✔
1075
                    ware->NotifyGoalAboutLostWare();
×
1076
                else
1077
                    ware->SetGoal(nullptr);
3✔
1078
            }
1079
            break;
3✔
1080
        case State::SeaattackLoading:
×
1081
            // We could also just set the goal harbor id to 0 but this can reuse the event
1082
            AbortSeaAttack();
×
1083
            break;
×
1084
        case State::SeaattackUnloading: break;
×
1085
    }
1086

1087
    // Are we currently getting the wares?
1088
    if(oldState == State::TransportLoading)
3✔
1089
    {
1090
        RTTR_Assert(current_ev);
1✔
1091
        if(homeHarbor)
1✔
1092
        {
1093
            // Then save us some time and unload immediately
1094
            // goal is now the start harbor (if it still exists)
1095
            goalHarbor = homeHarbor;
1✔
1096
            state = State::TransportUnloading;
1✔
1097
        } else
1098
        {
1099
            GetEvMgr().RemoveEvent(current_ev);
×
1100
            FindUnloadGoal(State::TransportDriving);
×
1101
        }
1102
    } else if(oldState == State::TransportUnloading || oldState == State::SeaattackUnloading)
2✔
1103
    {
1104
        // Remove current unload event
1105
        GetEvMgr().RemoveEvent(current_ev);
2✔
1106

1107
        if(oldState == State::SeaattackUnloading)
2✔
1108
            AbortSeaAttack();
×
1109
        else
1110
            FindUnloadGoal(State::TransportDriving);
2✔
1111
    }
1112
}
1113

1114
/// Fängt an mit idlen und setzt nötigen Sachen auf nullptr
1115
void noShip::StartIdling()
20✔
1116
{
1117
    // If those are not empty, then we are lost, not idling!
1118
    RTTR_Assert(figures.empty());
20✔
1119
    RTTR_Assert(wares.empty());
20✔
1120
    RTTR_Assert(remaining_sea_attackers == 0);
20✔
1121
    // Implicit contained wares/figures on expeditions
1122
    RTTR_Assert(!IsOnExplorationExpedition() || state == State::ExplorationexpeditionUnloading);
20✔
1123
    RTTR_Assert(!IsOnExpedition() || state == State::ExpeditionUnloading);
20✔
1124

1125
    homeHarbor.reset();
20✔
1126
    goalHarbor.reset();
20✔
1127
    state = State::Idle;
20✔
1128
}
20✔
1129

1130
/// Sagt Bescheid, dass ein Schiffsangreifer nicht mehr mit nach Hause fahren will
1131
void noShip::SeaAttackerWishesNoReturn()
4✔
1132
{
1133
    RTTR_Assert(remaining_sea_attackers);
4✔
1134
    RTTR_Assert(state == State::SeaattackWaiting);
4✔
1135

1136
    --remaining_sea_attackers;
4✔
1137
    // Alle Soldaten an Bord
1138
    if(remaining_sea_attackers == 0)
4✔
1139
    {
1140
        // Andere Events ggf. erstmal abmelden
1141
        GetEvMgr().RemoveEvent(current_ev);
2✔
1142
        if(!figures.empty())
2✔
1143
        {
1144
            // Go back home. Note: home_harbor can be 0 if it was destroyed, allow this and let the state handlers
1145
            // handle that case later
1146
            goalHarbor = homeHarbor;
2✔
1147
            state = State::SeaattackReturnDriving;
2✔
1148
            StartDrivingToHarborPlace();
2✔
1149
            HandleState_SeaAttackReturn();
2✔
1150
        } else
1151
        {
1152
            // Wenn keine Soldaten mehr da sind können wir auch erstmal idlen
1153
            StartIdling();
×
1154
            world->GetPlayer(ownerId_).GetJobForShip(*this);
×
1155
        }
1156
    }
1157
}
4✔
1158

1159
/// Schiffs-Angreifer sind nach dem Angriff wieder zurückgekehrt
1160
void noShip::AddReturnedAttacker(std::unique_ptr<nofAttacker> attacker)
4✔
1161
{
1162
    RTTR_Assert(!helpers::containsPtr(figures, attacker.get()));
4✔
1163

1164
    figures.push_back(std::move(attacker));
4✔
1165
    // Nun brauchen wir quasi einen Angreifer weniger
1166
    SeaAttackerWishesNoReturn();
4✔
1167
}
4✔
1168

1169
/// Weist das Schiff an, seine Erkundungs-Expedition fortzusetzen
1170
void noShip::ContinueExplorationExpedition()
8✔
1171
{
1172
    // Sind wir schon über unserem Limit, also zu weit gefahren
1173
    if(covered_distance >= MAX_EXPLORATION_EXPEDITION_DISTANCE)
8✔
1174
    {
1175
        // Dann steuern wir unseren Heimathafen an!
1176
        goalHarbor = homeHarbor;
×
1177
    } else
1178
    {
1179
        // Find the next harbor spot to explore
1180
        std::vector<HarborId> hps;
16✔
1181
        if(goalHarbor)
8✔
1182
            hps = world->GetUnexploredHarborPoints(goalHarbor, seaId_, GetPlayerId());
8✔
1183

1184
        // No possible spots? -> Go home
1185
        if(hps.empty())
8✔
1186
            goalHarbor = homeHarbor;
4✔
1187
        else
1188
        {
1189
            // Choose one randomly
1190
            goalHarbor = RANDOM_ELEMENT(hps);
4✔
1191
        }
1192
    }
1193

1194
    StartDrivingToHarborPlace();
8✔
1195
    state = State::ExplorationexpeditionDriving;
8✔
1196
    HandleState_ExplorationExpeditionDriving();
8✔
1197
}
8✔
1198

1199
/// Sagt dem Schiff, dass ein neuer Hafen erbaut wurde
1200
void noShip::NewHarborBuilt(nobHarborBuilding* hb)
13✔
1201
{
1202
    if(!lost)
13✔
1203
        return;
10✔
1204
    // Liegt der Hafen auch am Meer von diesem Schiff?
1205
    if(!world->IsHarborAtSea(hb->GetHarborPosID(), seaId_))
3✔
1206
        return;
×
1207

1208
    // LOG.write(("lost ship has new goal harbor player %i state %i pos %u,%u \n",player,state,x,y);
1209
    homeHarbor = goalHarbor = hb->GetHarborPosID();
3✔
1210
    lost = false;
3✔
1211

1212
    StartDrivingToHarborPlace();
3✔
1213

1214
    switch(state)
3✔
1215
    {
1216
        case State::ExplorationexpeditionDriving:
3✔
1217
        case State::ExpeditionDriving:
1218
        case State::TransportDriving:
1219
        case State::SeaattackReturnDriving: Driven(); break;
3✔
1220
        default:
×
1221
            RTTR_Assert(false); // Das darf eigentlich nicht passieren
×
1222
            LOG.write("Bug detected: Invalid state in NewHarborBuilt");
1223
            break;
1224
    }
1225
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc