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

Return-To-The-Roots / s25client / 22799099095

07 Mar 2026 12:31PM UTC coverage: 50.345% (+0.02%) from 50.327%
22799099095

Pull #1895

github

web-flow
Merge 8febfd5c1 into d2a3730c9
Pull Request #1895: Ships: Fix crash when doing expedition between adjacent harbor places

234 of 308 new or added lines in 23 files covered. (75.97%)

15 existing lines in 2 files now uncovered.

23056 of 45796 relevant lines covered (50.35%)

43163.47 hits per line

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

77.78
/libs/s25main/world/MapLoader.cpp
1
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "world/MapLoader.h"
6
#include "Game.h"
7
#include "GamePlayer.h"
8
#include "GameWorldBase.h"
9
#include "GlobalGameSettings.h"
10
#include "PointOutput.h"
11
#include "RttrForeachPt.h"
12
#include "factories/BuildingFactory.h"
13
#include "helpers/IdRange.h"
14
#include "lua/GameDataLoader.h"
15
#include "pathfinding/PathConditionShip.h"
16
#include "random/Random.h"
17
#include "world/World.h"
18
#include "nodeObjs/noAnimal.h"
19
#include "nodeObjs/noEnvObject.h"
20
#include "nodeObjs/noGranite.h"
21
#include "nodeObjs/noStaticObject.h"
22
#include "nodeObjs/noTree.h"
23
#include "gameTypes/ShipDirection.h"
24
#include "gameData/MaxPlayers.h"
25
#include "gameData/TerrainDesc.h"
26
#include "libsiedler2/Archiv.h"
27
#include "libsiedler2/ArchivItem_Map.h"
28
#include "libsiedler2/ArchivItem_Map_Header.h"
29
#include "libsiedler2/prototypen.h"
30
#include "s25util/Log.h"
31
#include <boost/filesystem/operations.hpp>
32
#include <algorithm>
33
#include <map>
34
#include <queue>
35

36
class noBase;
37

38
MapLoader::MapLoader(GameWorldBase& world) : world_(world) {}
6✔
39

40
bool MapLoader::Load(const libsiedler2::ArchivItem_Map& map, Exploration exploration)
4✔
41
{
42
    GameDataLoader gdLoader(world_.GetDescriptionWriteable());
8✔
43
    if(!gdLoader.Load())
4✔
44
        return false;
×
45

46
    uint8_t gfxSet = map.getHeader().getGfxSet();
4✔
47
    DescIdx<LandscapeDesc> lt =
48
      world_.GetDescription().landscapes.find([gfxSet](const LandscapeDesc& l) { return l.s2Id == gfxSet; });
8✔
49
    world_.Init(MapExtent(map.getHeader().getWidth(), map.getHeader().getHeight()), lt); //-V807
4✔
50

51
    if(!InitNodes(map, exploration))
4✔
52
        return false;
×
53
    PlaceObjects(map);
4✔
54
    PlaceAnimals(map);
4✔
55
    if(!InitSeasAndHarbors(world_))
4✔
56
        return false;
×
57

58
    /// Schatten
59
    InitShadows(world_);
4✔
60

61
    // If we have explored FoW, create the FoW objects
62
    if(exploration == Exploration::FogOfWarExplored)
4✔
63
        SetMapExplored(world_);
×
64

65
    return true;
4✔
66
}
67

68
bool MapLoader::Load(const boost::filesystem::path& mapFilePath)
2✔
69
{
70
    // Map laden
71
    libsiedler2::Archiv mapArchiv;
4✔
72

73
    // Karteninformationen laden
74
    if(libsiedler2::loader::LoadMAP(mapFilePath, mapArchiv) != 0)
2✔
75
        return false;
×
76

77
    const libsiedler2::ArchivItem_Map& map = *static_cast<libsiedler2::ArchivItem_Map*>(mapArchiv[0]);
2✔
78

79
    if(!Load(map, world_.GetGGS().exploration))
2✔
80
        return false;
×
81
    if(!PlaceHQs(world_.GetGGS().randomStartPosition))
2✔
82
        return false;
×
83

84
    world_.CreateTradeGraphs();
2✔
85

86
    return true;
2✔
87
}
88

89
bool MapLoader::LoadLuaScript(Game& game, ILocalGameState& localgameState, const boost::filesystem::path& luaFilePath)
3✔
90
{
91
    if(!bfs::exists(luaFilePath))
3✔
92
        return false;
×
93
    auto lua = std::make_unique<LuaInterfaceGame>(game, localgameState);
6✔
94
    if(!lua->loadScript(luaFilePath) || !lua->CheckScriptVersion())
3✔
95
        return false;
1✔
96
    game.SetLua(std::move(lua));
2✔
97
    return true;
2✔
98
}
99

100
bool MapLoader::PlaceHQs(bool randomStartPos)
2✔
101
{
102
    return PlaceHQs(world_, hqPositions_, randomStartPos);
2✔
103
}
104

105
void MapLoader::InitShadows(World& world)
4✔
106
{
107
    RTTR_FOREACH_PT(MapPoint, world.GetSize())
56,644✔
108
        world.RecalcShadow(pt);
56,320✔
109
}
4✔
110

111
void MapLoader::SetMapExplored(World& world)
×
112
{
113
    RTTR_FOREACH_PT(MapPoint, world.GetSize())
×
114
    {
115
        // For every player
116
        for(unsigned i = 0; i < MAX_PLAYERS; ++i)
×
117
        {
118
            // If we have FoW here, save it
119
            if(world.GetNode(pt).fow[i].visibility == Visibility::FogOfWar)
×
120
                world.SaveFOWNode(pt, i, 0);
×
121
        }
122
    }
123
}
×
124

125
DescIdx<TerrainDesc> MapLoader::getTerrainFromS2(uint8_t s2Id) const
112,640✔
126
{
127
    return world_.GetDescription().terrain.find([s2Id, landscape = world_.GetLandscapeType()](const TerrainDesc& t) {
112,640✔
128
        return t.s2Id == s2Id && t.landscape == landscape;
1,153,728✔
129
    });
112,640✔
130
}
131

132
bool MapLoader::InitNodes(const libsiedler2::ArchivItem_Map& map, Exploration exploration)
4✔
133
{
134
    using libsiedler2::MapLayer;
135
    // Init node data (everything except the objects, figures and BQ)
136
    RTTR_FOREACH_PT(MapPoint, world_.GetSize())
56,644✔
137
    {
138
        MapNode& node = world_.GetNodeInt(pt);
56,320✔
139

140
        std::fill(node.roads.begin(), node.roads.end(), PointRoad::None);
56,320✔
141
        node.altitude = map.getMapDataAt(MapLayer::Altitude, pt.x, pt.y);
56,320✔
142
        unsigned char t1 = map.getMapDataAt(MapLayer::Terrain1, pt.x, pt.y),
56,320✔
143
                      t2 = map.getMapDataAt(MapLayer::Terrain2, pt.x, pt.y);
56,320✔
144

145
        // Hafenplatz?
146
        if((t1 & libsiedler2::HARBOR_MASK) != 0)
56,320✔
147
            world_.harbor_pos.push_back(HarborPos(pt));
×
148

149
        // Will be set later
150
        node.harborId.reset();
56,320✔
151

152
        node.t1 = getTerrainFromS2(t1 & 0x3F); // Only lower 6 bits
56,320✔
153
        node.t2 = getTerrainFromS2(t2 & 0x3F); // Only lower 6 bits
56,320✔
154
        if(!node.t1 || !node.t2)
56,320✔
155
            return false;
×
156

157
        unsigned char mapResource = map.getMapDataAt(MapLayer::Resources, pt.x, pt.y);
56,320✔
158
        Resource resource;
56,320✔
159
        // Wasser?
160
        if(mapResource == 0x20 || mapResource == 0x21)
56,320✔
161
            resource = Resource(ResourceType::Water, 7);
29,300✔
162
        else if(mapResource > 0x40 && mapResource < 0x48)
27,020✔
163
            resource = Resource(ResourceType::Coal, mapResource - 0x40);
7,852✔
164
        else if(mapResource > 0x48 && mapResource < 0x50)
19,168✔
165
            resource = Resource(ResourceType::Iron, mapResource - 0x48);
2,020✔
166
        else if(mapResource > 0x50 && mapResource < 0x58)
17,148✔
167
            resource = Resource(ResourceType::Gold, mapResource - 0x50);
1,328✔
168
        else if(mapResource > 0x58 && mapResource < 0x60)
15,820✔
169
            resource = Resource(ResourceType::Granite, mapResource - 0x58);
440✔
170
        else if(mapResource > 0x80 && mapResource < 0x90) // fish
15,380✔
171
            resource = Resource(ResourceType::Fish, 4);   // Use 4 fish
1,936✔
172
        node.resources = resource;
56,320✔
173

174
        node.reserved = false;
56,320✔
175
        node.owner = 0;
56,320✔
176
        std::fill(node.boundary_stones.begin(), node.boundary_stones.end(), 0);
56,320✔
177
        node.seaId.reset();
56,320✔
178

179
        Visibility fowVisibility;
180
        switch(exploration)
56,320✔
181
        {
182
            case Exploration::Disabled: fowVisibility = Visibility::Visible; break;
×
183
            case Exploration::Classic:
56,320✔
184
            case Exploration::FogOfWar: fowVisibility = Visibility::Invisible; break;
56,320✔
185
            case Exploration::FogOfWarExplored: fowVisibility = Visibility::FogOfWar; break;
×
186
            default: throw std::invalid_argument("Visibility for FoW");
×
187
        }
188

189
        // FOW-Zeug initialisieren
190
        for(auto& fow : node.fow)
506,880✔
191
        {
192
            fow = FoWNode();
450,560✔
193
            fow.visibility = fowVisibility;
450,560✔
194
        }
195

196
        RTTR_Assert(node.figures.empty());
56,320✔
197
    }
198
    return true;
4✔
199
}
200

201
void MapLoader::PlaceObjects(const libsiedler2::ArchivItem_Map& map)
4✔
202
{
203
    hqPositions_.clear();
4✔
204

205
    RTTR_FOREACH_PT(MapPoint, world_.GetSize())
56,644✔
206
    {
207
        using libsiedler2::MapLayer;
208
        unsigned char lc = map.getMapDataAt(MapLayer::ObjectIndex, pt.x, pt.y);
56,320✔
209
        noBase* obj = nullptr;
56,320✔
210

211
        switch(map.getMapDataAt(MapLayer::ObjectType, pt.x, pt.y))
56,320✔
212
        {
213
            // Player Startpos (provisorisch)
214
            case 0x80:
16✔
215
            {
216
                if(lc < MAX_PLAYERS)
16✔
217
                {
218
                    while(hqPositions_.size() <= lc)
32✔
219
                        hqPositions_.push_back(MapPoint::Invalid());
16✔
220
                    hqPositions_[lc] = pt;
16✔
221
                }
222
            }
223
            break;
16✔
224

225
            // Baum 1-4
226
            case 0xC4:
14,112✔
227
            {
228
                if(lc >= 0x30 && lc <= 0x3D)
14,112✔
229
                    obj = new noTree(pt, 0, 3);
4,488✔
230
                else if(lc >= 0x70 && lc <= 0x7D)
9,624✔
231
                    obj = new noTree(pt, 1, 3);
4,688✔
232
                else if(lc >= 0xB0 && lc <= 0xBD)
4,936✔
233
                    obj = new noTree(pt, 2, 3);
4,444✔
234
                else if(lc >= 0xF0 && lc <= 0xFD)
492✔
235
                    obj = new noTree(pt, 3, 3);
492✔
236
                else
237
                    LOG.write(_("Unknown tree1-4 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
238
            }
239
            break;
14,112✔
240

241
            // Baum 5-8
242
            case 0xC5:
×
243
            {
244
                if(lc >= 0x30 && lc <= 0x3D)
×
245
                    obj = new noTree(pt, 4, 3);
×
246
                else if(lc >= 0x70 && lc <= 0x7D)
×
247
                    obj = new noTree(pt, 5, 3);
×
248
                else if(lc >= 0xB0 && lc <= 0xBD)
×
249
                    obj = new noTree(pt, 6, 3);
×
250
                else if(lc >= 0xF0 && lc <= 0xFD)
×
251
                    obj = new noTree(pt, 7, 3);
×
252
                else
253
                    LOG.write(_("Unknown tree5-8 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
254
            }
255
            break;
×
256

257
            // Baum 9
258
            case 0xC6:
×
259
            {
260
                if(lc >= 0x30 && lc <= 0x3D)
×
261
                    obj = new noTree(pt, 8, 3);
×
262
                else
263
                    LOG.write(_("Unknown tree9 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
264
            }
265
            break;
×
266

267
            // Sonstiges Naturzeug ohne Funktion, nur zur Dekoration
268
            case 0xC8:
2,992✔
269
            case 0xC9: // Note: 0xC9 is actually a bug and should be 0xC8. But the random map generator produced that...
270
            {
271
                // "wasserstein" aus der map_?_z.lst
272
                if(lc == 0x0B)
2,992✔
273
                    obj = new noStaticObject(pt, 500 + lc);
×
274
                // Objekte aus der map_?_z.lst
275
                else if(lc <= 0x0F)
2,992✔
276
                    obj = new noEnvObject(pt, 500 + lc);
1,292✔
277
                // Objekte aus der map.lst
278
                else if(lc <= 0x14)
1,700✔
279
                    obj = new noEnvObject(pt, 542 + lc - 0x10);
1,264✔
280
                // exists in mis0bobs-mis5bobs -> take stranded ship
281
                else if(lc == 0x15)
436✔
282
                    obj = new noStaticObject(pt, 0, 0);
×
283
                // gate
284
                else if(lc == 0x16)
436✔
285
                    obj = new noStaticObject(pt, 560);
×
286
                // open gate
287
                else if(lc == 0x17)
436✔
288
                    obj = new noStaticObject(pt, 561);
×
289
                // Stalagmiten (mis1bobs)
290
                else if(lc <= 0x1E)
436✔
291
                    obj = new noStaticObject(pt, (lc - 0x18) * 2, 1);
×
292
                // toter Baum (mis1bobs)
293
                else if(lc <= 0x20)
436✔
294
                    obj = new noStaticObject(pt, 20 + (lc - 0x1F) * 2, 1);
×
295
                // Gerippe (mis1bobs)
296
                else if(lc == 0x21)
436✔
297
                    obj = new noStaticObject(pt, 30, 1);
×
298
                // Objekte aus der map.lst
299
                else if(lc <= 0x2B)
436✔
300
                    obj = new noEnvObject(pt, 550 + lc - 0x22);
436✔
301
                // tent, ruin of guardhouse, tower ruin, cross
302
                else if(lc <= 0x2E || lc == 0x30)
×
303
                    obj = new noStaticObject(pt, (lc - 0x2C) * 2, 2);
×
304
                // castle ruin
305
                else if(lc == 0x2F)
×
306
                    obj = new noStaticObject(pt, (lc - 0x2C) * 2, 2, 2);
×
307
                // small wiking with boat
308
                else if(lc == 0x31)
×
309
                    obj = new noStaticObject(pt, 0, 3);
×
310
                // Pile of wood
311
                else if(lc == 0x32)
×
312
                    obj = new noStaticObject(pt, 0, 4);
×
313
                // whale skeleton (head right)
314
                else if(lc == 0x33)
×
315
                    obj = new noStaticObject(pt, 0, 5);
×
316
                // The next 2 are non standard and only for access in RTTR (replaced in original by
317
                // whale skeleton (head left)
318
                else if(lc == 0x34)
×
319
                    obj = new noStaticObject(pt, 2, 5);
×
320
                // Cave
321
                else if(lc == 0x35)
×
322
                    obj = new noStaticObject(pt, 4, 5);
×
323
                else
324
                    LOG.write(_("Unknown nature object at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
325
            }
326
            break;
2,992✔
327

328
            // Granit Typ 1
329
            case 0xCC:
1,096✔
330
            {
331
                if(lc >= 0x01 && lc <= 0x06)
1,096✔
332
                    obj = new noGranite(GraniteType::One, lc - 1);
1,096✔
333
                else
334
                    LOG.write(_("Unknown granite type2 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
335
            }
336
            break;
1,096✔
337

338
            // Granit Typ 2
339
            case 0xCD:
1,116✔
340
            {
341
                if(lc >= 0x01 && lc <= 0x06)
1,116✔
342
                    obj = new noGranite(GraniteType::Two, lc - 1);
1,116✔
343
                else
344
                    LOG.write(_("Unknown granite type2 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
×
345
            }
346
            break;
1,116✔
347

348
            // Nichts
349
            case 0: break;
36,988✔
350

351
            default:
×
352
#ifndef NDEBUG
353
                unsigned unknownObj = map.getMapDataAt(MapLayer::ObjectType, pt.x, pt.y);
×
354
                LOG.write(_("Unknown object at %1%: (0x%2$x: 0x%3$x)\n")) % pt % unknownObj % unsigned(lc);
×
355
#endif // !NDEBUG
356
                break;
×
357
        }
358

359
        world_.GetNodeInt(pt).obj = obj;
56,320✔
360
    }
361
}
4✔
362

363
void MapLoader::PlaceAnimals(const libsiedler2::ArchivItem_Map& map)
4✔
364
{
365
    // Tiere auslesen
366
    RTTR_FOREACH_PT(MapPoint, world_.GetSize())
56,644✔
367
    {
368
        Species species;
369
        using libsiedler2::MapLayer;
370
        switch(map.getMapDataAt(MapLayer::Animals, pt.x, pt.y))
56,320✔
371
        {
372
            // TODO: Which id is the polar bear?
373
            case 1:
140✔
374
                species = (RANDOM.Rand(RANDOM_CONTEXT2(0), 2) == 0) ? Species::RabbitWhite : Species::RabbitGrey;
140✔
375
                break; // Random rabbit
140✔
376
            case 2: species = Species::Fox; break;
112✔
377
            case 3: species = Species::Stag; break;
×
378
            case 4: species = Species::Deer; break;
112✔
379
            case 5: species = Species::Duck; break;
×
380
            case 6: species = Species::Sheep; break;
56✔
381
            case 0:
55,900✔
382
            case 0xFF: // 0xFF is for (really) old S2 maps
383
                continue;
55,900✔
384
            default:
×
385
#ifndef NDEBUG
386
                unsigned unknownAnimal = map.getMapDataAt(MapLayer::Animals, pt.x, pt.y);
×
387
                LOG.write(_("Unknown animal species at %1%: (0x%2$x)\n")) % pt % unknownAnimal;
×
388
#endif // !NDEBUG
389
                continue;
55,900✔
390
        }
391

392
        world_.AddFigure(pt, std::make_unique<noAnimal>(species, pt)).StartLiving();
420✔
393
    }
394
}
4✔
395

396
bool MapLoader::PlaceHQs(GameWorldBase& world, std::vector<MapPoint> hqPositions, bool randomStartPos)
170✔
397
{
398
    // random locations? -> randomize them :)
399
    if(randomStartPos)
170✔
400
    {
401
        RANDOM_SHUFFLE2(hqPositions, 0);
×
402
    }
403

404
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
528✔
405
    {
406
        // Skip unused slots
407
        if(!world.GetPlayer(i).isUsed())
358✔
408
            continue;
13✔
409

410
        // Does the HQ have a position?
411
        if(i >= hqPositions.size() || !hqPositions[i].isValid())
345✔
412
        {
413
            LOG.write(_("Player %u does not have a valid start position!")) % i;
×
414
            return false;
×
415
        }
416

417
        BuildingFactory::CreateBuilding(world, BuildingType::Headquarters, hqPositions[i], i,
345✔
418
                                        world.GetPlayer(i).nation);
345✔
419
    }
420
    return true;
170✔
421
}
422

423
bool MapLoader::InitSeasAndHarbors(World& world, const std::vector<MapPoint>& additionalHarbors)
26✔
424
{
425
    for(MapPoint pt : additionalHarbors)
184✔
426
        world.harbor_pos.push_back(HarborPos(pt));
158✔
427
    // Clear current harbors and seas
428
    RTTR_FOREACH_PT(MapPoint, world.GetSize()) //-V807
178,536✔
429
    {
430
        MapNode& node = world.GetNodeInt(pt);
176,204✔
431
        node.seaId.reset();
176,204✔
432
        node.harborId.reset();
176,204✔
433
    }
434

435
    /// Determine all seas
436
    world.seas.clear();
26✔
437
    SeaId curSeaId(1);
26✔
438
    RTTR_FOREACH_PT(MapPoint, world.GetSize())
178,536✔
439
    {
440
        // Point is not yet assigned a sea but should be
441
        if(!world.GetNode(pt).seaId && world.IsSeaPoint(pt))
176,204✔
442
        {
443
            const auto seaSize = MeasureSea(world, pt, curSeaId);
45✔
444
            world.seas.push_back(World::Sea(seaSize));
45✔
445
            curSeaId = curSeaId.next();
45✔
446
        }
447
    }
448

449
    /// Determine seas adjacent to the harbor places
450
    HarborId curHarborId(1);
26✔
451
    for(auto it = world.harbor_pos.begin(); it != world.harbor_pos.end();)
200✔
452
    {
453
        helpers::StrongIdVector<bool, SeaId> hasCoastAtSea(world.seas.size(), false);
348✔
454
        bool foundCoast = false;
174✔
455
        for(const auto dir : helpers::EnumRange<Direction>{})
2,784✔
456
        {
457
            SeaId seaId;
1,044✔
458
            // Skip point at NW as often there is no path from it if the harbor is north of an island
459
            if(dir != Direction::NorthWest)
1,044✔
460
            {
461
                seaId = world.GetSeaFromCoastalPoint(world.GetNeighbour(it->pos, dir));
870✔
462
                if(seaId)
870✔
463
                {
464
                    foundCoast = true;
226✔
465
                    // Only 1 coastal point per sea
466
                    if(hasCoastAtSea[seaId])
226✔
467
                        seaId.reset();
52✔
468
                    else
469
                        hasCoastAtSea[seaId] = true;
174✔
470
                }
471
            }
472
            it->seaIds[dir] = seaId;
1,044✔
473
        }
474
        if(!foundCoast)
174✔
475
        {
476
            LOG.write("Map Bug: Found harbor without coast at %1%. Removing!\n") % it->pos;
×
477
            it = world.harbor_pos.erase(it);
×
478
        } else
479
        {
480
            world.GetNodeInt(it->pos).harborId = curHarborId;
174✔
481
            curHarborId = curHarborId.next();
174✔
482
            ++it;
174✔
483
        }
484
    }
485

486
    // Calculate the neighbors and distances
487
    CalcHarborPosNeighbors(world);
26✔
488

489
    // Validate
490
    for(const auto startHbId : helpers::idRange<HarborId>(world.harbor_pos.size()))
200✔
491
    {
492
        const HarborPos& startHbPos = world.harbor_pos[startHbId];
174✔
493
        for(const std::vector<HarborPos::Neighbor>& neighbors : startHbPos.neighbors)
1,218✔
494
        {
495
            for(const HarborPos::Neighbor& neighbor : neighbors)
1,664✔
496
            {
497
                if(world.CalcHarborDistance(neighbor.id, startHbId) != neighbor.distance)
620✔
498
                {
499
                    LOG.write("Bug: Harbor distance mismatch for harbors %1%->%2%: %3% != %4%\n") % startHbId
×
500
                      % neighbor.id % world.CalcHarborDistance(neighbor.id, startHbId) % neighbor.distance;
×
501
                    return false;
×
502
                }
503
            }
504
        }
505
    }
506
    return true;
26✔
507
}
508

509
// class for finding harbor neighbors
510
struct CalcHarborPosNeighborsNode
511
{
512
    CalcHarborPosNeighborsNode() = default; //-V730
513
    CalcHarborPosNeighborsNode(const MapPoint pt, unsigned distance) : pos(pt), distance(distance) {}
232,032✔
514

515
    MapPoint pos;
516
    unsigned distance;
517
};
518

519
/// Calculate the distance from each harbor to the others
520
void MapLoader::CalcHarborPosNeighbors(World& world)
26✔
521
{
522
    for(HarborPos& harbor : world.harbor_pos)
200✔
523
    {
524
        for(const auto dir : helpers::EnumRange<ShipDirection>{})
2,784✔
525
            harbor.neighbors[dir].clear();
1,044✔
526
    }
527
    PathConditionShip shipPathChecker(world);
26✔
528

529
    // pre-calculate sea-points, as IsSeaPoint is rather expensive
530
    std::vector<int8_t> ptIsSeaPt(world.nodes.size()); //-V656
52✔
531

532
    RTTR_FOREACH_PT(MapPoint, world.GetSize())
178,536✔
533
    {
534
        if(shipPathChecker.IsNodeOk(pt))
176,204✔
535
            ptIsSeaPt[world.GetIdx(pt)] = -1;
62,361✔
536
    }
537

538
    // FIFO queue used for a BFS
539
    std::queue<CalcHarborPosNeighborsNode> todo_list;
52✔
540

541
    for(const auto startHbId : helpers::idRange<HarborId>(world.harbor_pos.size()))
200✔
542
    {
543
        RTTR_Assert(todo_list.empty());
174✔
544

545
        // Copy sea points to working flags. Possible values are
546
        // -1 - sea point, not already visited
547
        // 0 - visited or no sea point
548
        // 1 - Coast to a harbor
549
        std::vector<int8_t> ptToVisitOrHb(ptIsSeaPt);
348✔
550

551
        helpers::StrongIdVector<bool, HarborId> hbFound(world.harbor_pos.size(), false);
348✔
552
        // For each sea, store the coastal point indices and their harbor
553
        helpers::StrongIdVector<std::multimap<unsigned, HarborId>, SeaId> coastToHarborPerSea(world.seas.size());
348✔
554
        std::vector<MapPoint> ownCoastalPoints;
348✔
555

556
        // mark coastal points around harbors
557
        for(const auto otherHbId : helpers::idRange<HarborId>(world.harbor_pos.size()))
1,600✔
558
        {
559
            for(const auto dir : helpers::EnumRange<Direction>{})
22,816✔
560
            {
561
                SeaId seaId = world.GetSeaId(otherHbId, dir);
8,556✔
562
                // No sea? -> Next
563
                if(!seaId)
8,556✔
564
                    continue;
7,130✔
565
                const MapPoint coastPt = world.GetNeighbour(world.GetHarborPoint(otherHbId), dir);
1,426✔
566
                // This should not be marked for visit
567
                unsigned idx = world.GetIdx(coastPt);
1,426✔
568
                RTTR_Assert(ptToVisitOrHb[idx] != -1);
1,426✔
569
                if(otherHbId == startHbId)
1,426✔
570
                {
571
                    // This is our start harbor. Add the coast points around it to our todo list.
572
                    ownCoastalPoints.push_back(coastPt);
174✔
573
                } else
574
                {
575
                    ptToVisitOrHb[idx] = 1;
1,252✔
576
                    coastToHarborPerSea[seaId].insert(std::make_pair(idx, otherHbId));
1,252✔
577
                }
578
            }
579
        }
580

581
        for(const MapPoint& ownCoastPt : ownCoastalPoints)
348✔
582
        {
583
            // Special case: Get all harbors that share the coast point with us
584
            SeaId seaId = world.GetSeaFromCoastalPoint(ownCoastPt);
174✔
585
            auto const coastToHbs = coastToHarborPerSea[seaId].equal_range(world.GetIdx(ownCoastPt));
174✔
586
            for(auto it = coastToHbs.first; it != coastToHbs.second; ++it)
180✔
587
            {
588
                ShipDirection shipDir = world.GetShipDir(ownCoastPt, ownCoastPt);
6✔
589
                world.harbor_pos[startHbId].neighbors[shipDir].push_back(HarborPos::Neighbor(it->second, 0));
6✔
590
                hbFound[it->second] = true;
6✔
591
            }
592
            todo_list.push(CalcHarborPosNeighborsNode(ownCoastPt, 0));
174✔
593
        }
594

595
        while(!todo_list.empty()) // as long as there are sea points on our todo list...
232,206✔
596
        {
597
            CalcHarborPosNeighborsNode curNode = todo_list.front();
232,032✔
598
            todo_list.pop();
232,032✔
599

600
            for(const auto dir : helpers::EnumRange<Direction>{})
3,712,512✔
601
            {
602
                MapPoint curPt = world.GetNeighbour(curNode.pos, dir);
1,392,192✔
603
                unsigned idx = world.GetIdx(curPt);
1,392,192✔
604

605
                const int8_t ptValue = ptToVisitOrHb[idx];
1,392,192✔
606
                // Already visited
607
                if(ptValue == 0)
1,392,192✔
608
                    continue;
1,160,334✔
609
                // Not reachable
610
                if(!shipPathChecker.IsEdgeOk(curNode.pos, dir))
231,874✔
611
                    continue;
16✔
612

613
                if(ptValue > 0) // found harbor(s)
231,858✔
614
                {
615
                    ShipDirection shipDir = world.GetShipDir(world.harbor_pos[startHbId].pos, curPt);
597✔
616
                    SeaId seaId = world.GetSeaFromCoastalPoint(curPt);
597✔
617
                    auto const coastToHbs = coastToHarborPerSea[seaId].equal_range(idx);
597✔
618
                    for(auto it = coastToHbs.first; it != coastToHbs.second; ++it)
1,217✔
619
                    {
620
                        const HarborId otherHbId = it->second;
620✔
621
                        if(hbFound[otherHbId])
620✔
622
                            continue;
6✔
623

624
                        hbFound[otherHbId] = true;
614✔
625
                        world.harbor_pos[startHbId].neighbors[shipDir].push_back(
1,228✔
626
                          HarborPos::Neighbor(otherHbId, curNode.distance + 1));
614✔
627

628
                        // Make this the only coastal point of this harbor for this sea
629
                        HarborPos& otherHb = world.harbor_pos[otherHbId];
614✔
630
                        RTTR_Assert(seaId);
614✔
631
                        for(const auto hbDir : helpers::EnumRange<Direction>{})
9,824✔
632
                        {
633
                            if(otherHb.seaIds[hbDir] == seaId && world.GetNeighbour(otherHb.pos, hbDir) != curPt)
3,684✔
NEW
634
                                otherHb.seaIds[hbDir].reset();
×
635
                        }
636
                    }
637
                }
638
                todo_list.push(CalcHarborPosNeighborsNode(curPt, curNode.distance + 1));
231,858✔
639
                ptToVisitOrHb[idx] = 0; // mark as visited, so we do not go here again
231,858✔
640
            }
641
        }
642
    }
643
}
26✔
644

645
/// Vermisst ein neues Weltmeer von einem Punkt aus, indem es alle mit diesem Punkt verbundenen
646
/// Wasserpunkte mit der gleichen ID belegt und die Anzahl zur�ckgibt
647
unsigned MapLoader::MeasureSea(World& world, const MapPoint start, SeaId seaId)
45✔
648
{
649
    // Breitensuche von diesem Punkt aus durchf�hren
650
    std::vector<bool> visited(world.GetWidth() * world.GetHeight(), false);
90✔
651
    std::queue<MapPoint> todo;
45✔
652

653
    todo.push(start);
45✔
654
    visited[world.GetIdx(start)] = true;
45✔
655

656
    // Count of nodes (including start node)
657
    unsigned count = 0;
45✔
658

659
    while(!todo.empty())
62,406✔
660
    {
661
        MapPoint p = todo.front();
62,361✔
662
        todo.pop();
62,361✔
663

664
        RTTR_Assert(visited[world.GetIdx(p)]);
62,361✔
665
        world.GetNodeInt(p).seaId = seaId;
62,361✔
666

667
        for(const MapPoint neighbourPt : world.GetNeighbours(p))
436,527✔
668
        {
669
            if(visited[world.GetIdx(neighbourPt)])
374,166✔
670
                continue;
301,905✔
671
            visited[world.GetIdx(neighbourPt)] = true;
72,261✔
672

673
            // Ist das dort auch ein Meerespunkt?
674
            if(world.IsSeaPoint(neighbourPt))
72,261✔
675
                todo.push(neighbourPt);
62,316✔
676
        }
677

678
        ++count;
62,361✔
679
    }
680

681
    return count;
90✔
682
}
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