-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathMapLoader.cpp
More file actions
677 lines (597 loc) · 23.9 KB
/
Copy pathMapLoader.cpp
File metadata and controls
677 lines (597 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later
#include "world/MapLoader.h"
#include "Game.h"
#include "GamePlayer.h"
#include "GameWorldBase.h"
#include "GlobalGameSettings.h"
#include "PointOutput.h"
#include "RttrForeachPt.h"
#include "factories/BuildingFactory.h"
#include "lua/GameDataLoader.h"
#include "pathfinding/PathConditionShip.h"
#include "random/Random.h"
#include "world/World.h"
#include "nodeObjs/noAnimal.h"
#include "nodeObjs/noEnvObject.h"
#include "nodeObjs/noGranite.h"
#include "nodeObjs/noStaticObject.h"
#include "nodeObjs/noTree.h"
#include "gameTypes/ShipDirection.h"
#include "gameData/MaxPlayers.h"
#include "gameData/TerrainDesc.h"
#include "libsiedler2/Archiv.h"
#include "libsiedler2/ArchivItem_Map.h"
#include "libsiedler2/ArchivItem_Map_Header.h"
#include "libsiedler2/prototypen.h"
#include "s25util/Log.h"
#include <boost/filesystem/operations.hpp>
#include <algorithm>
#include <map>
#include <queue>
class noBase;
MapLoader::MapLoader(GameWorldBase& world) : world_(world) {}
bool MapLoader::Load(const libsiedler2::ArchivItem_Map& map, Exploration exploration)
{
GameDataLoader gdLoader(world_.GetDescriptionWriteable());
if(!gdLoader.Load())
return false;
uint8_t gfxSet = map.getHeader().getGfxSet();
DescIdx<LandscapeDesc> lt =
world_.GetDescription().landscapes.find([gfxSet](const LandscapeDesc& l) { return l.s2Id == gfxSet; });
world_.Init(MapExtent(map.getHeader().getWidth(), map.getHeader().getHeight()), lt); //-V807
if(!InitNodes(map, exploration))
return false;
PlaceObjects(map);
PlaceAnimals(map);
if(!InitSeasAndHarbors(world_))
return false;
/// Schatten
InitShadows(world_);
// If we have explored FoW, create the FoW objects
if(exploration == Exploration::FogOfWarExplored)
SetMapExplored(world_);
return true;
}
bool MapLoader::Load(const boost::filesystem::path& mapFilePath)
{
// Map laden
libsiedler2::Archiv mapArchiv;
// Karteninformationen laden
if(libsiedler2::loader::LoadMAP(mapFilePath, mapArchiv) != 0)
return false;
const libsiedler2::ArchivItem_Map& map = *static_cast<libsiedler2::ArchivItem_Map*>(mapArchiv[0]);
if(!Load(map, world_.GetGGS().exploration))
return false;
if(!PlaceHQs(world_.GetGGS().randomStartPosition))
return false;
world_.CreateTradeGraphs();
return true;
}
bool MapLoader::LoadLuaScript(Game& game, ILocalGameState& localgameState, const boost::filesystem::path& luaFilePath)
{
if(!bfs::exists(luaFilePath))
return false;
auto lua = std::make_unique<LuaInterfaceGame>(game, localgameState);
if(!lua->loadScript(luaFilePath) || !lua->CheckScriptVersion())
return false;
game.SetLua(std::move(lua));
return true;
}
bool MapLoader::PlaceHQs(bool randomStartPos)
{
return PlaceHQs(world_, hqPositions_, randomStartPos);
}
void MapLoader::InitShadows(World& world)
{
RTTR_FOREACH_PT(MapPoint, world.GetSize())
world.RecalcShadow(pt);
}
void MapLoader::SetMapExplored(World& world)
{
RTTR_FOREACH_PT(MapPoint, world.GetSize())
{
// For every player
for(unsigned i = 0; i < MAX_PLAYERS; ++i)
{
// If we have FoW here, save it
if(world.GetNode(pt).fow[i].visibility == Visibility::FogOfWar)
world.SaveFOWNode(pt, i, 0);
}
}
}
DescIdx<TerrainDesc> MapLoader::getTerrainFromS2(uint8_t s2Id) const
{
return world_.GetDescription().terrain.find([s2Id, landscape = world_.GetLandscapeType()](const TerrainDesc& t) {
return t.s2Id == s2Id && t.landscape == landscape;
});
}
bool MapLoader::InitNodes(const libsiedler2::ArchivItem_Map& map, Exploration exploration)
{
using libsiedler2::MapLayer;
// Init node data (everything except the objects, figures and BQ)
RTTR_FOREACH_PT(MapPoint, world_.GetSize())
{
MapNode& node = world_.GetNodeInt(pt);
std::fill(node.roads.begin(), node.roads.end(), PointRoad::None);
node.altitude = map.getMapDataAt(MapLayer::Altitude, pt.x, pt.y);
unsigned char t1 = map.getMapDataAt(MapLayer::Terrain1, pt.x, pt.y),
t2 = map.getMapDataAt(MapLayer::Terrain2, pt.x, pt.y);
// Hafenplatz?
if((t1 & libsiedler2::HARBOR_MASK) != 0)
world_.harbor_pos.push_back(HarborPos(pt));
// Will be set later
node.harborId = 0;
node.t1 = getTerrainFromS2(t1 & 0x3F); // Only lower 6 bits
node.t2 = getTerrainFromS2(t2 & 0x3F); // Only lower 6 bits
if(!node.t1 || !node.t2)
return false;
unsigned char mapResource = map.getMapDataAt(MapLayer::Resources, pt.x, pt.y);
Resource resource;
// Wasser?
if(mapResource == 0x20 || mapResource == 0x21)
resource = Resource(ResourceType::Water, 7);
else if(mapResource > 0x40 && mapResource < 0x48)
resource = Resource(ResourceType::Coal, mapResource - 0x40);
else if(mapResource > 0x48 && mapResource < 0x50)
resource = Resource(ResourceType::Iron, mapResource - 0x48);
else if(mapResource > 0x50 && mapResource < 0x58)
resource = Resource(ResourceType::Gold, mapResource - 0x50);
else if(mapResource > 0x58 && mapResource < 0x60)
resource = Resource(ResourceType::Granite, mapResource - 0x58);
else if(mapResource > 0x80 && mapResource < 0x90) // fish
resource = Resource(ResourceType::Fish, 4); // Use 4 fish
node.resources = resource;
node.reserved = false;
node.owner = 0;
std::fill(node.boundary_stones.begin(), node.boundary_stones.end(), 0);
node.seaId = 0;
Visibility fowVisibility;
switch(exploration)
{
case Exploration::Disabled: fowVisibility = Visibility::Visible; break;
case Exploration::Classic:
case Exploration::FogOfWar: fowVisibility = Visibility::Invisible; break;
case Exploration::FogOfWarExplored: fowVisibility = Visibility::FogOfWar; break;
default: throw std::invalid_argument("Visibility for FoW");
}
// FOW-Zeug initialisieren
for(auto& fow : node.fow)
{
fow = FoWNode();
fow.visibility = fowVisibility;
}
RTTR_Assert(node.figures.empty());
}
return true;
}
void MapLoader::PlaceObjects(const libsiedler2::ArchivItem_Map& map)
{
hqPositions_.clear();
RTTR_FOREACH_PT(MapPoint, world_.GetSize())
{
using libsiedler2::MapLayer;
unsigned char lc = map.getMapDataAt(MapLayer::ObjectIndex, pt.x, pt.y);
noBase* obj = nullptr;
switch(map.getMapDataAt(MapLayer::ObjectType, pt.x, pt.y))
{
// Player Startpos (provisorisch)
case 0x80:
{
if(lc < MAX_PLAYERS)
{
while(hqPositions_.size() <= lc)
hqPositions_.push_back(MapPoint::Invalid());
hqPositions_[lc] = pt;
}
}
break;
// Baum 1-4
case 0xC4:
{
if(lc >= 0x30 && lc <= 0x3D)
obj = new noTree(pt, 0, 3);
else if(lc >= 0x70 && lc <= 0x7D)
obj = new noTree(pt, 1, 3);
else if(lc >= 0xB0 && lc <= 0xBD)
obj = new noTree(pt, 2, 3);
else if(lc >= 0xF0 && lc <= 0xFD)
obj = new noTree(pt, 3, 3);
else
LOG.write(_("Unknown tree1-4 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Baum 5-8
case 0xC5:
{
if(lc >= 0x30 && lc <= 0x3D)
obj = new noTree(pt, 4, 3);
else if(lc >= 0x70 && lc <= 0x7D)
obj = new noTree(pt, 5, 3);
else if(lc >= 0xB0 && lc <= 0xBD)
obj = new noTree(pt, 6, 3);
else if(lc >= 0xF0 && lc <= 0xFD)
obj = new noTree(pt, 7, 3);
else
LOG.write(_("Unknown tree5-8 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Baum 9
case 0xC6:
{
if(lc >= 0x30 && lc <= 0x3D)
obj = new noTree(pt, 8, 3);
else
LOG.write(_("Unknown tree9 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Sonstiges Naturzeug ohne Funktion, nur zur Dekoration
case 0xC8:
case 0xC9: // Note: 0xC9 is actually a bug and should be 0xC8. But the random map generator produced that...
{
// "wasserstein" aus der map_?_z.lst
if(lc == 0x0B)
obj = new noStaticObject(pt, 500 + lc);
// Objekte aus der map_?_z.lst
else if(lc <= 0x0F)
obj = new noEnvObject(pt, 500 + lc);
// Objekte aus der map.lst
else if(lc <= 0x14)
obj = new noEnvObject(pt, 542 + lc - 0x10);
// exists in mis0bobs-mis5bobs -> take stranded ship
else if(lc == 0x15)
obj = new noStaticObject(pt, 0, 0);
// gate
else if(lc == 0x16)
obj = new noStaticObject(pt, 560);
// open gate
else if(lc == 0x17)
obj = new noStaticObject(pt, 561);
// Stalagmiten (mis1bobs)
else if(lc <= 0x1E)
obj = new noStaticObject(pt, (lc - 0x18) * 2, 1);
// toter Baum (mis1bobs)
else if(lc <= 0x20)
obj = new noStaticObject(pt, 20 + (lc - 0x1F) * 2, 1);
// Gerippe (mis1bobs)
else if(lc == 0x21)
obj = new noStaticObject(pt, 30, 1);
// Objekte aus der map.lst
else if(lc <= 0x2B)
obj = new noEnvObject(pt, 550 + lc - 0x22);
// tent, ruin of guardhouse, tower ruin, cross
else if(lc <= 0x2E || lc == 0x30)
obj = new noStaticObject(pt, (lc - 0x2C) * 2, 2);
// castle ruin
else if(lc == 0x2F)
obj = new noStaticObject(pt, (lc - 0x2C) * 2, 2, 2);
// small wiking with boat
else if(lc == 0x31)
obj = new noStaticObject(pt, 0, 3);
// Pile of wood
else if(lc == 0x32)
obj = new noStaticObject(pt, 0, 4);
// whale skeleton (head right)
else if(lc == 0x33)
obj = new noStaticObject(pt, 0, 5);
// The next 2 are non standard and only for access in RTTR (replaced in original by
// whale skeleton (head left)
else if(lc == 0x34)
obj = new noStaticObject(pt, 2, 5);
// Cave
else if(lc == 0x35)
obj = new noStaticObject(pt, 4, 5);
else
LOG.write(_("Unknown nature object at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Granit Typ 1
case 0xCC:
{
if(lc >= 0x01 && lc <= 0x06)
obj = new noGranite(GraniteType::One, lc - 1);
else
LOG.write(_("Unknown granite type2 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Granit Typ 2
case 0xCD:
{
if(lc >= 0x01 && lc <= 0x06)
obj = new noGranite(GraniteType::Two, lc - 1);
else
LOG.write(_("Unknown granite type2 at %1%: (0x%2$x)\n")) % pt % unsigned(lc);
}
break;
// Nichts
case 0: break;
default:
#ifndef NDEBUG
unsigned unknownObj = map.getMapDataAt(MapLayer::ObjectType, pt.x, pt.y);
LOG.write(_("Unknown object at %1%: (0x%2$x: 0x%3$x)\n")) % pt % unknownObj % unsigned(lc);
#endif // !NDEBUG
break;
}
world_.GetNodeInt(pt).obj = obj;
}
}
void MapLoader::PlaceAnimals(const libsiedler2::ArchivItem_Map& map)
{
// Tiere auslesen
RTTR_FOREACH_PT(MapPoint, world_.GetSize())
{
Species species;
using libsiedler2::MapLayer;
switch(map.getMapDataAt(MapLayer::Animals, pt.x, pt.y))
{
// TODO: Which id is the polar bear?
case 1:
species = (RANDOM.Rand(RANDOM_CONTEXT2(0), 2) == 0) ? Species::RabbitWhite : Species::RabbitGrey;
break; // Random rabbit
case 2: species = Species::Fox; break;
case 3: species = Species::Stag; break;
case 4: species = Species::Deer; break;
case 5: species = Species::Duck; break;
case 6: species = Species::Sheep; break;
case 0:
case 0xFF: // 0xFF is for (really) old S2 maps
continue;
default:
#ifndef NDEBUG
unsigned unknownAnimal = map.getMapDataAt(MapLayer::Animals, pt.x, pt.y);
LOG.write(_("Unknown animal species at %1%: (0x%2$x)\n")) % pt % unknownAnimal;
#endif // !NDEBUG
continue;
}
world_.AddFigure(pt, std::make_unique<noAnimal>(species, pt)).StartLiving();
}
}
bool MapLoader::PlaceHQs(GameWorldBase& world, std::vector<MapPoint> hqPositions, bool randomStartPos)
{
// random locations? -> randomize them :)
if(randomStartPos)
{
RANDOM_SHUFFLE2(hqPositions, 0);
}
for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
{
// Skip unused slots
if(!world.GetPlayer(i).isUsed())
continue;
// Does the HQ have a position?
if(i >= hqPositions.size() || !hqPositions[i].isValid())
{
LOG.write(_("Player %u does not have a valid start position!\n")) % i;
if(world.HasLua()) // HQ can be placed in the script, so don't signal error
continue;
else
return false;
}
BuildingFactory::CreateBuilding(world, BuildingType::Headquarters, hqPositions[i], i,
world.GetPlayer(i).nation);
}
return true;
}
bool MapLoader::InitSeasAndHarbors(World& world, const std::vector<MapPoint>& additionalHarbors)
{
for(MapPoint pt : additionalHarbors)
world.harbor_pos.push_back(HarborPos(pt));
// Clear current harbors and seas
RTTR_FOREACH_PT(MapPoint, world.GetSize()) //-V807
{
MapNode& node = world.GetNodeInt(pt);
node.seaId = 0u;
node.harborId = 0;
}
/// Weltmeere vermessen
world.seas.clear();
RTTR_FOREACH_PT(MapPoint, world.GetSize())
{
// Noch kein Meer an diesem Punkt Aber trotzdem Teil eines noch nicht vermessenen Meeres?
if(!world.GetNode(pt).seaId && world.IsSeaPoint(pt))
{
unsigned sea_size = MeasureSea(world, pt, world.seas.size() + 1);
world.seas.push_back(World::Sea(sea_size));
}
}
/// Die Meere herausfinden, an die die Hafenpunkte grenzen
unsigned curHarborId = 1;
for(auto it = world.harbor_pos.begin() + 1; it != world.harbor_pos.end();)
{
std::vector<bool> hasCoastAtSea(world.seas.size() + 1, false);
bool foundCoast = false;
for(const auto dir : helpers::EnumRange<Direction>{})
{
// Skip point at NW as often there is no path from it if the harbor is north of an island
unsigned short seaId =
(dir == Direction::NorthWest) ? 0 : world.GetSeaFromCoastalPoint(world.GetNeighbour(it->pos, dir));
// Only 1 coastal point per sea
if(hasCoastAtSea[seaId])
seaId = 0;
else
hasCoastAtSea[seaId] = true;
it->seaIds[dir] = seaId;
if(seaId)
foundCoast = true;
}
if(!foundCoast)
{
LOG.write("Map Bug: Found harbor without coast at %1%. Removing!\n") % it->pos;
it = world.harbor_pos.erase(it);
} else
{
world.GetNodeInt(it->pos).harborId = curHarborId++;
++it;
}
}
// Calculate the neighbors and distances
CalcHarborPosNeighbors(world);
// Validate
for(unsigned startHbId = 1; startHbId < world.harbor_pos.size(); ++startHbId)
{
const HarborPos& startHbPos = world.harbor_pos[startHbId];
for(const std::vector<HarborPos::Neighbor>& neighbors : startHbPos.neighbors)
{
for(const HarborPos::Neighbor& neighbor : neighbors)
{
if(world.CalcHarborDistance(neighbor.id, startHbId) != neighbor.distance)
{
LOG.write("Bug: Harbor distance mismatch for harbors %1%->%2%: %3% != %4%\n") % startHbId
% neighbor.id % world.CalcHarborDistance(neighbor.id, startHbId) % neighbor.distance;
return false;
}
}
}
}
return true;
}
// class for finding harbor neighbors
struct CalcHarborPosNeighborsNode
{
CalcHarborPosNeighborsNode() = default; //-V730
CalcHarborPosNeighborsNode(const MapPoint pt, unsigned distance) : pos(pt), distance(distance) {}
MapPoint pos;
unsigned distance;
};
/// Calculate the distance from each harbor to the others
void MapLoader::CalcHarborPosNeighbors(World& world)
{
for(HarborPos& harbor : world.harbor_pos)
{
for(const auto dir : helpers::EnumRange<ShipDirection>{})
harbor.neighbors[dir].clear();
}
PathConditionShip shipPathChecker(world);
// pre-calculate sea-points, as IsSeaPoint is rather expensive
std::vector<int8_t> ptIsSeaPt(world.nodes.size()); //-V656
RTTR_FOREACH_PT(MapPoint, world.GetSize())
{
if(shipPathChecker.IsNodeOk(pt))
ptIsSeaPt[world.GetIdx(pt)] = -1;
}
// FIFO queue used for a BFS
std::queue<CalcHarborPosNeighborsNode> todo_list;
for(unsigned startHbId = 1; startHbId < world.harbor_pos.size(); ++startHbId)
{
RTTR_Assert(todo_list.empty());
// Copy sea points to working flags. Possible values are
// -1 - sea point, not already visited
// 0 - visited or no sea point
// 1 - Coast to a harbor
std::vector<int8_t> ptToVisitOrHb(ptIsSeaPt);
std::vector<bool> hbFound(world.harbor_pos.size(), false);
// For each sea, store the coastal point indices and their harbor
std::vector<std::multimap<unsigned, unsigned>> coastToHarborPerSea(world.seas.size() + 1);
std::vector<MapPoint> ownCoastalPoints;
// mark coastal points around harbors
for(unsigned otherHbId = 1; otherHbId < world.harbor_pos.size(); ++otherHbId)
{
for(const auto dir : helpers::EnumRange<Direction>{})
{
unsigned seaId = world.GetSeaId(otherHbId, dir);
// No sea? -> Next
if(!seaId)
continue;
const MapPoint coastPt = world.GetNeighbour(world.GetHarborPoint(otherHbId), dir);
// This should not be marked for visit
unsigned idx = world.GetIdx(coastPt);
RTTR_Assert(ptToVisitOrHb[idx] != -1);
if(otherHbId == startHbId)
{
// This is our start harbor. Add the coast points around it to our todo list.
ownCoastalPoints.push_back(coastPt);
} else
{
ptToVisitOrHb[idx] = 1;
coastToHarborPerSea[seaId].insert(std::make_pair(idx, otherHbId));
}
}
}
for(const MapPoint& ownCoastPt : ownCoastalPoints)
{
// Special case: Get all harbors that share the coast point with us
unsigned short seaId = world.GetSeaFromCoastalPoint(ownCoastPt);
auto const coastToHbs = coastToHarborPerSea[seaId].equal_range(world.GetIdx(ownCoastPt));
for(auto it = coastToHbs.first; it != coastToHbs.second; ++it)
{
ShipDirection shipDir = world.GetShipDir(ownCoastPt, ownCoastPt);
world.harbor_pos[startHbId].neighbors[shipDir].push_back(HarborPos::Neighbor(it->second, 0));
hbFound[it->second] = true;
}
todo_list.push(CalcHarborPosNeighborsNode(ownCoastPt, 0));
}
while(!todo_list.empty()) // as long as there are sea points on our todo list...
{
CalcHarborPosNeighborsNode curNode = todo_list.front();
todo_list.pop();
for(const auto dir : helpers::EnumRange<Direction>{})
{
MapPoint curPt = world.GetNeighbour(curNode.pos, dir);
unsigned idx = world.GetIdx(curPt);
const int8_t ptValue = ptToVisitOrHb[idx];
// Already visited
if(ptValue == 0)
continue;
// Not reachable
if(!shipPathChecker.IsEdgeOk(curNode.pos, dir))
continue;
if(ptValue > 0) // found harbor(s)
{
ShipDirection shipDir = world.GetShipDir(world.harbor_pos[startHbId].pos, curPt);
unsigned seaId = world.GetSeaFromCoastalPoint(curPt);
auto const coastToHbs = coastToHarborPerSea[seaId].equal_range(idx);
for(auto it = coastToHbs.first; it != coastToHbs.second; ++it)
{
unsigned otherHbId = it->second;
if(hbFound[otherHbId])
continue;
hbFound[otherHbId] = true;
world.harbor_pos[startHbId].neighbors[shipDir].push_back(
HarborPos::Neighbor(otherHbId, curNode.distance + 1));
// Make this the only coastal point of this harbor for this sea
HarborPos& otherHb = world.harbor_pos[otherHbId];
RTTR_Assert(seaId);
for(const auto hbDir : helpers::EnumRange<Direction>{})
{
if(otherHb.seaIds[hbDir] == seaId && world.GetNeighbour(otherHb.pos, hbDir) != curPt)
otherHb.seaIds[hbDir] = 0;
}
}
}
todo_list.push(CalcHarborPosNeighborsNode(curPt, curNode.distance + 1));
ptToVisitOrHb[idx] = 0; // mark as visited, so we do not go here again
}
}
}
}
/// Vermisst ein neues Weltmeer von einem Punkt aus, indem es alle mit diesem Punkt verbundenen
/// Wasserpunkte mit der gleichen ID belegt und die Anzahl zur�ckgibt
unsigned MapLoader::MeasureSea(World& world, const MapPoint start, unsigned short seaId)
{
// Breitensuche von diesem Punkt aus durchf�hren
std::vector<bool> visited(world.GetWidth() * world.GetHeight(), false);
std::queue<MapPoint> todo;
todo.push(start);
visited[world.GetIdx(start)] = true;
// Count of nodes (including start node)
unsigned count = 0;
while(!todo.empty())
{
MapPoint p = todo.front();
todo.pop();
RTTR_Assert(visited[world.GetIdx(p)]);
world.GetNodeInt(p).seaId = seaId;
for(const MapPoint neighbourPt : world.GetNeighbours(p))
{
if(visited[world.GetIdx(neighbourPt)])
continue;
visited[world.GetIdx(neighbourPt)] = true;
// Ist das dort auch ein Meerespunkt?
if(world.IsSeaPoint(neighbourPt))
todo.push(neighbourPt);
}
++count;
}
return count;
}