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

ParadoxGameConverters / Vic3ToHoI4 / 13090188366

26 Jan 2025 07:59AM UTC coverage: 95.116% (-0.03%) from 95.146%
13090188366

push

github

web-flow
Eliminate warnings (#701)

* Externals in angle brackets.

* Commons from lib

* Update to preview versions of externals.

* Update fronter

* Switch to std::filesystem::path

* Unused variables

* Type conversion stuff.

* constness

* Missing headers

* Prefix increment, not postfix

* Check optional

* Remove a stray extern

* explicit

* Return an error case.

* Default value

* Enforce warnings.

* Disable some unfixable warnings.

* Update commons

* Formatting

* Fix a test

* Use fixed common

* Fix tests.

* Formatting

* Update to merged versions

* Make codefactor happier.

965 of 1008 new or added lines in 120 files covered. (95.73%)

2 existing lines in 2 files now uncovered.

20428 of 21477 relevant lines covered (95.12%)

61689.32 hits per line

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

98.64
/src/maps/map_data_importer.cpp
1
#include "src/maps/map_data_importer.h"
2

3
#include <external/fmt/include/fmt/format.h>
4

5
#include <external/bitmap/bitmap_image.hpp>
6

7
#include "src/support/progress_manager.h"
8

9

10
namespace
11
{
12

13
commonItems::Color GetCenterColor(const maps::Point position, const bitmap_image& province_map)
4,800,001✔
14
{
15
   rgb_t color{0, 0, 0};
4,800,001✔
16
   province_map.get_pixel(position.x, position.y, color);
4,800,001✔
17

18
   return commonItems::Color(std::array<int, 3>{color.red, color.green, color.blue});
9,600,002✔
19
}
20

21

22
commonItems::Color GetAboveColor(maps::Point position, const bitmap_image& province_map)
4,800,001✔
23
{
24
   if (position.y > 0)
4,800,001✔
25
   {
26
      position.y--;
4,792,000✔
27
   }
28

29
   rgb_t color{0, 0, 0};
4,800,001✔
30
   province_map.get_pixel(position.x, position.y, color);
4,800,001✔
31

32
   return commonItems::Color(std::array<int, 3>{color.red, color.green, color.blue});
9,600,002✔
33
}
34

35

36
commonItems::Color GetBelowColor(maps::Point position, int height, const bitmap_image& province_map)
4,800,001✔
37
{
38
   if (position.y < height - 1)
4,800,001✔
39
   {
40
      position.y++;
4,792,000✔
41
   }
42

43
   rgb_t color{0, 0, 0};
4,800,001✔
44
   province_map.get_pixel(position.x, position.y, color);
4,800,001✔
45

46
   return commonItems::Color(std::array<int, 3>{color.red, color.green, color.blue});
9,600,002✔
47
}
48

49

50
commonItems::Color GetLeftColor(maps::Point position, int width, const bitmap_image& province_map)
4,800,001✔
51
{
52
   if (position.x > 0)
4,800,001✔
53
   {
54
      position.x--;
4,794,000✔
55
   }
56
   else
57
   {
58
      position.x = width - 1;
6,001✔
59
   }
60

61
   rgb_t color{0, 0, 0};
4,800,001✔
62
   province_map.get_pixel(position.x, position.y, color);
4,800,001✔
63

64
   return commonItems::Color(std::array<int, 3>{color.red, color.green, color.blue});
9,600,002✔
65
}
66

67

68
commonItems::Color GetRightColor(maps::Point position, int width, const bitmap_image& province_map)
4,800,001✔
69
{
70
   if (position.x < width - 1)
4,800,001✔
71
   {
72
      position.x++;
4,794,000✔
73
   }
74
   else
75
   {
76
      position.x = 0;
6,001✔
77
   }
78

79
   rgb_t color{0, 0, 0};
4,800,001✔
80
   province_map.get_pixel(position.x, position.y, color);
4,800,001✔
81

82
   return commonItems::Color(std::array<int, 3>{color.red, color.green, color.blue});
9,600,002✔
83
}
84

85
}  // namespace
86

87

88

89
maps::MapData maps::MapDataImporter::ImportMapData(const commonItems::ModFilesystem& mod_filesystem)
12✔
90
{
91
   ImportProvinces(mod_filesystem);
12✔
92
   ImportAdjacencies(mod_filesystem);
11✔
93

94
   return maps::MapData({.province_neighbors = province_neighbors_,
10✔
95
       .borders = borders_,
10✔
96
       .the_province_points = the_province_points_,
10✔
97
       .province_definitions = province_definitions_,
10✔
98
       .points_to_provinces = points_to_provinces_});
10✔
99
}
10✔
100

101

102
void maps::MapDataImporter::ImportProvinces(const commonItems::ModFilesystem& mod_filesystem)
12✔
103
{
104
   const auto path = mod_filesystem.GetActualFileLocation("map/provinces.bmp");
12✔
105
   if (!path)
12✔
106
   {
107
      throw std::runtime_error("Could not find map/provinces.bmp");
1✔
108
   }
109

110
   bitmap_image province_map(path->string());
11✔
111
   if (!province_map)
11✔
112
   {
NEW
113
      throw std::runtime_error(fmt::format("Could not open {}/map/provinces.bmp", path->string()));
×
114
   }
115

116
   int prev_progress = 0;
11✔
117
   const int height = static_cast<int>(province_map.height());
11✔
118
   const int width = static_cast<int>(province_map.width());
11✔
119
   for (int y = 0; y < height; y++)
6,012✔
120
   {
121
      for (int x = 0; x < width; x++)
4,806,002✔
122
      {
123
         Point position = {x, y};
4,800,001✔
124

125
         auto center_color = GetCenterColor(position, province_map);
4,800,001✔
126
         auto above_color = GetAboveColor(position, province_map);
4,800,001✔
127
         auto below_color = GetBelowColor(position, height, province_map);
4,800,001✔
128
         auto left_color = GetLeftColor(position, width, province_map);
4,800,001✔
129
         auto right_color = GetRightColor(position, width, province_map);
4,800,001✔
130

131
         position.y = height - y - 1;
4,800,001✔
132
         if (center_color != above_color)
4,800,001✔
133
         {
134
            HandleNeighbor(center_color, above_color, position);
1,112✔
135
         }
136
         if (center_color != right_color)
4,800,001✔
137
         {
138
            HandleNeighbor(center_color, right_color, position);
1,224✔
139
         }
140
         if (center_color != below_color)
4,800,001✔
141
         {
142
            HandleNeighbor(center_color, below_color, position);
1,112✔
143
         }
144
         if (center_color != left_color)
4,800,001✔
145
         {
146
            HandleNeighbor(center_color, left_color, position);
1,224✔
147
         }
148

149
         if (auto province = province_definitions_.GetProvinceFromColor(center_color); province)
4,800,001✔
150
         {
151
            points_to_provinces_.emplace(position, *province);
3,303✔
152
            if (auto specific_province_points = the_province_points_.find(*province);
3,303✔
153
                specific_province_points != the_province_points_.end())
3,303✔
154
            {
155
               specific_province_points->second.AddPoint(position);
3,262✔
156
            }
157
            else
158
            {
159
               ProvincePoints the_new_points;
41✔
160
               the_new_points.AddPoint(position);
41✔
161
               the_province_points_.emplace(*province, the_new_points);
41✔
162
            }
41✔
163
         }
4,800,001✔
164
      }
165
      int current_progress = (10 * y / height);
6,001✔
166
      if (prev_progress != current_progress)
6,001✔
167
      {
168
         ProgressManager::AddProgress(1);
90✔
169
         prev_progress = current_progress;
90✔
170
      }
171
   }
172
}
12✔
173

174

175
void maps::MapDataImporter::HandleNeighbor(const commonItems::Color& center_color,
4,672✔
176
    const commonItems::Color& other_color,
177
    const Point& position)
178
{
179
   const auto center_province = province_definitions_.GetProvinceFromColor(center_color);
4,672✔
180
   const auto other_province = province_definitions_.GetProvinceFromColor(other_color);
4,672✔
181
   if (center_province && other_province)
4,672✔
182
   {
183
      AddNeighbor(*center_province, *other_province);
792✔
184
      AddPointToBorder(*center_province, *other_province, position);
792✔
185
   }
186
}
4,672✔
187

188

189
void maps::MapDataImporter::AddNeighbor(const std::string& main_province, const std::string& neighbor_province)
852✔
190
{
191
   if (const auto center_mapping = province_neighbors_.find(main_province); center_mapping != province_neighbors_.end())
852✔
192
   {
193
      center_mapping->second.insert(neighbor_province);
766✔
194
   }
195
   else
196
   {
197
      const std::set<std::string> neighbors = {neighbor_province};
172✔
198
      province_neighbors_[main_province] = neighbors;
86✔
199
   }
86✔
200
}
938✔
201

202

203
void maps::MapDataImporter::RemoveNeighbor(const std::string& main_province, const std::string& neighbor_province)
2✔
204
{
205
   if (const auto center_mapping = province_neighbors_.find(main_province); center_mapping != province_neighbors_.end())
2✔
206
   {
207
      center_mapping->second.erase(neighbor_province);
2✔
208
   }
209
}
2✔
210

211

212
void maps::MapDataImporter::AddPointToBorder(const std::string& main_province,
792✔
213
    const std::string& neighbor_province,
214
    const Point position)
215
{
216
   auto borders_with_neighbors = borders_.find(main_province);
792✔
217
   if (borders_with_neighbors == borders_.end())
792✔
218
   {
219
      BordersWith new_borders_with_neighbors;
40✔
220
      borders_.emplace(main_province, new_borders_with_neighbors);
40✔
221
      borders_with_neighbors = borders_.find(main_province);
40✔
222
   }
40✔
223

224
   auto border = borders_with_neighbors->second.find(neighbor_province);
792✔
225
   if (border == borders_with_neighbors->second.end())
792✔
226
   {
227
      BorderPoints new_border;
88✔
228
      borders_with_neighbors->second.emplace(neighbor_province, new_border);
88✔
229
      border = borders_with_neighbors->second.find(neighbor_province);
88✔
230
   }
88✔
231

232
   if (border->second.empty())
792✔
233
   {
234
      border->second.push_back(position);
88✔
235
   }
236
   else
237
   {
238
      if (const auto last_point = border->second.back(); (last_point.x != position.x) || (last_point.y != position.y))
704✔
239
      {
240
         border->second.push_back(position);
704✔
241
      }
242
   }
243
}
792✔
244

245

246
void maps::MapDataImporter::ImportAdjacencies(const commonItems::ModFilesystem& mod_filesystem)
11✔
247
{
248
   const auto path = mod_filesystem.GetActualFileLocation("map/adjacencies.csv");
11✔
249
   if (!path)
11✔
250
   {
251
      throw std::runtime_error("Could not find map/adjacencies.csv");
1✔
252
   }
253

254
   std::ifstream adjacencies_file(*path);
10✔
255
   if (!adjacencies_file.is_open())
10✔
256
   {
NEW
257
      throw std::runtime_error(fmt::format("Could not open {}", path->string()));
×
258
   }
259

260
   while (!adjacencies_file.eof())
57✔
261
   {
262
      std::string line;
47✔
263
      getline(adjacencies_file, line);
47✔
264
      if (line.starts_with('#'))
47✔
265
      {
266
         continue;
4✔
267
      }
268

269
      const std::regex pattern("([^;]*);([^;]*);([^;]*)(.*)\r?");
43✔
270
      if (std::smatch matches; regex_match(line, matches, pattern))
43✔
271
      {
272
         if (matches[1] == "From" || matches[1] == "-1")
41✔
273
         {
274
            continue;
10✔
275
         }
276

277
         if (matches[1].length() == 0)
33✔
278
         {
279
            continue;
1✔
280
         }
281
         const std::string first_province = matches[1];
32✔
282
         if (matches[2].length() == 0)
32✔
283
         {
284
            continue;
1✔
285
         }
286
         const std::string second_province = matches[2];
31✔
287
         if (matches[3] != "impassable")
31✔
288
         {
289
            AddNeighbor(first_province, second_province);
30✔
290
            AddNeighbor(second_province, first_province);
30✔
291
         }
292
         else
293
         {
294
            RemoveNeighbor(first_province, second_province);
1✔
295
            RemoveNeighbor(second_province, first_province);
1✔
296
         }
297
      }
75✔
298
   }
57✔
299
}
11✔
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