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

Return-To-The-Roots / s25client / 24043417400

06 Apr 2026 05:53PM UTC coverage: 50.372% (+0.04%) from 50.337%
24043417400

Pull #1910

github

web-flow
Merge 9a7a79f07 into e4146df45
Pull Request #1910: Fix wrongly shown soldiers & Refactor HQ start wares and inventory handling

197 of 409 new or added lines in 22 files covered. (48.17%)

96 existing lines in 25 files now uncovered.

23068 of 45795 relevant lines covered (50.37%)

43384.86 hits per line

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

77.97
/libs/s25main/world/MapLoader.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 "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 "buildings/nobHQ.h"
13
#include "factories/BuildingFactory.h"
14
#include "helpers/IdRange.h"
15
#include "lua/GameDataLoader.h"
16
#include "pathfinding/PathConditionShip.h"
17
#include "random/Random.h"
18
#include "world/World.h"
19
#include "nodeObjs/noAnimal.h"
20
#include "nodeObjs/noEnvObject.h"
21
#include "nodeObjs/noGranite.h"
22
#include "nodeObjs/noStaticObject.h"
23
#include "nodeObjs/noTree.h"
24
#include "gameTypes/ShipDirection.h"
25
#include "gameData/MaxPlayers.h"
26
#include "gameData/TerrainDesc.h"
27
#include "libsiedler2/Archiv.h"
28
#include "libsiedler2/ArchivItem_Map.h"
29
#include "libsiedler2/ArchivItem_Map_Header.h"
30
#include "libsiedler2/prototypen.h"
31
#include "s25util/Log.h"
32
#include <boost/filesystem/operations.hpp>
33
#include <algorithm>
34
#include <map>
35
#include <queue>
36

37
class noBase;
38

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

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

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

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

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

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

66
    return true;
4✔
67
}
68

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

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

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

80
    if(!Load(map, world_.GetGGS().exploration))
2✔
81
        return false;
×
82
    std::vector<MapPoint> hqPositions = hqPositions_;
4✔
83
    if(world_.GetGGS().randomStartPosition)
2✔
NEW
84
        RANDOM_SHUFFLE2(hqPositions, 0);
×
85
    if(!PlaceHQs())
2✔
UNCOV
86
        return false;
×
87

88
    world_.CreateTradeGraphs();
2✔
89

90
    return true;
2✔
91
}
92

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

104
bool MapLoader::PlaceHQs(bool addStartWares)
2✔
105
{
106
    return PlaceHQs(world_, hqPositions_, addStartWares);
2✔
107
}
108

109
void MapLoader::InitShadows(World& world)
4✔
110
{
111
    RTTR_FOREACH_PT(MapPoint, world.GetSize())
56,644✔
112
        world.RecalcShadow(pt);
56,320✔
113
}
4✔
114

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

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

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

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

149
        // Hafenplatz?
150
        if((t1 & libsiedler2::HARBOR_MASK) != 0)
56,320✔
151
            world_.harborData.push_back(HarborPos(pt));
×
152

153
        // Will be set later
154
        node.harborId.reset();
56,320✔
155

156
        node.t1 = getTerrainFromS2(t1 & 0x3F); // Only lower 6 bits
56,320✔
157
        node.t2 = getTerrainFromS2(t2 & 0x3F); // Only lower 6 bits
56,320✔
158
        if(!node.t1 || !node.t2)
56,320✔
159
            return false;
×
160

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

178
        node.reserved = false;
56,320✔
179
        node.owner = 0;
56,320✔
180
        std::fill(node.boundary_stones.begin(), node.boundary_stones.end(), 0);
56,320✔
181
        node.seaId.reset();
56,320✔
182

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

193
        // FOW-Zeug initialisieren
194
        for(auto& fow : node.fow)
506,880✔
195
        {
196
            fow = FoWNode();
450,560✔
197
            fow.visibility = fowVisibility;
450,560✔
198
        }
199

200
        RTTR_Assert(node.figures.empty());
56,320✔
201
    }
202
    return true;
4✔
203
}
204

205
void MapLoader::PlaceObjects(const libsiedler2::ArchivItem_Map& map)
4✔
206
{
207
    hqPositions_.clear();
4✔
208

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

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

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

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

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

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

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

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

352
            // Nichts
353
            case 0: break;
36,988✔
354

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

363
        world_.GetNodeInt(pt).obj = obj;
56,320✔
364
    }
365
}
4✔
366

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

396
        world_.AddFigure(pt, std::make_unique<noAnimal>(species, pt)).StartLiving();
420✔
397
    }
398
}
4✔
399

400
bool MapLoader::PlaceHQs(GameWorldBase& world, const std::vector<MapPoint>& hqPositions, const bool addStartWares)
175✔
401
{
402
    for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
543✔
403
    {
404
        // Skip unused slots
405
        if(!world.GetPlayer(i).isUsed())
368✔
406
            continue;
13✔
407

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

415
        auto* hq = checkedCast<nobHQ*>(BuildingFactory::CreateBuilding(world, BuildingType::Headquarters,
355✔
416
                                                                       hqPositions[i], i, world.GetPlayer(i).nation));
355✔
417
        if(addStartWares)
355✔
418
            hq->addStartWares();
1✔
419
    }
420
    return true;
175✔
421
}
422

423
bool MapLoader::InitSeasAndHarbors(World& world, const std::vector<MapPoint>& additionalHarbors)
26✔
424
{
425
    for(MapPoint pt : additionalHarbors)
184✔
426
        world.harborData.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.harborData.begin(); it != world.harborData.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.harborData.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.harborData.size()))
200✔
491
    {
492
        const HarborPos& startHbPos = world.harborData[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.harborData)
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.harborData.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.harborData.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.harborData.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.harborData[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.harborData[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.harborData[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.harborData[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✔
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