Skip to content

Commit 7b55227

Browse files
authored
Merge pull request #30 from Idhrendur/vic3
Support Vic3 maps
2 parents 79f9536 + c5bf777 commit 7b55227

28 files changed

+387
-187
lines changed

ProvinceMapper.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<ClCompile Include="commonItems\WinUtils.cpp" />
2222
<ClCompile Include="ProvinceMapper\Source\Configuration\Configuration.cpp" />
2323
<ClCompile Include="ProvinceMapper\Source\Definitions\Definitions.cpp" />
24+
<ClCompile Include="ProvinceMapper\Source\Definitions\Vic3Definitions.cpp" />
2425
<ClCompile Include="ProvinceMapper\Source\Frames\Images\ImageCanvas.cpp" />
2526
<ClCompile Include="ProvinceMapper\Source\Frames\Images\ImageFrame.cpp" />
2627
<ClCompile Include="ProvinceMapper\Source\Frames\Images\StatusBar.cpp" />
@@ -51,6 +52,8 @@
5152
<ClInclude Include="commonItems\StringUtils.h" />
5253
<ClInclude Include="ProvinceMapper\Source\Configuration\Configuration.h" />
5354
<ClInclude Include="ProvinceMapper\Source\Definitions\Definitions.h" />
55+
<ClInclude Include="ProvinceMapper\Source\Definitions\DefinitionsInterface.h" />
56+
<ClInclude Include="ProvinceMapper\Source\Definitions\Vic3Definitions.h" />
5457
<ClInclude Include="ProvinceMapper\Source\Frames\Images\ImageCanvas.h" />
5558
<ClInclude Include="ProvinceMapper\Source\Frames\Images\ImageFrame.h" />
5659
<ClInclude Include="ProvinceMapper\Source\Frames\Images\StatusBar.h" />

ProvinceMapper.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<ClCompile Include="ProvinceMapper\Source\Frames\Unmapped\UnmappedTab.cpp">
130130
<Filter>ProvinceMapper\Frames\Unmapped</Filter>
131131
</ClCompile>
132+
<ClCompile Include="ProvinceMapper\Source\Definitions\Vic3Definitions.cpp">
133+
<Filter>ProvinceMapper\Definitions</Filter>
134+
</ClCompile>
132135
</ItemGroup>
133136
<ItemGroup>
134137
<ClInclude Include="commonItems\ParserHelpers.h">
@@ -215,6 +218,12 @@
215218
<ClInclude Include="ProvinceMapper\Source\Frames\Unmapped\UnmappedTab.h">
216219
<Filter>ProvinceMapper\Frames\Unmapped</Filter>
217220
</ClInclude>
221+
<ClInclude Include="ProvinceMapper\Source\Definitions\DefinitionsInterface.h">
222+
<Filter>ProvinceMapper\Definitions</Filter>
223+
</ClInclude>
224+
<ClInclude Include="ProvinceMapper\Source\Definitions\Vic3Definitions.h">
225+
<Filter>ProvinceMapper\Definitions</Filter>
226+
</ClInclude>
218227
</ItemGroup>
219228
<ItemGroup>
220229
<CopyFileToFolders Include="ProvinceMapper\Resources\converter.ico">

ProvinceMapper/Source/Definitions/Definitions.cpp

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,51 @@
44
#include "Provinces/Province.h"
55
#include <fstream>
66

7+
8+
9+
namespace
10+
{
11+
12+
std::optional<std::tuple<std::string, unsigned char, unsigned char, unsigned char, std::string>> parseLine(const std::string& line)
13+
{
14+
try
15+
{
16+
auto sepLoc = line.find(';');
17+
if (sepLoc == std::string::npos)
18+
return std::nullopt;
19+
auto sepLocSave = sepLoc;
20+
auto ID = line.substr(0, sepLoc);
21+
sepLoc = line.find(';', sepLocSave + 1);
22+
if (sepLoc == std::string::npos)
23+
return std::nullopt;
24+
auto r = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
25+
sepLocSave = sepLoc;
26+
sepLoc = line.find(';', sepLocSave + 1);
27+
if (sepLoc == std::string::npos)
28+
return std::nullopt;
29+
auto g = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
30+
sepLocSave = sepLoc;
31+
sepLoc = line.find(';', sepLocSave + 1);
32+
if (sepLoc == std::string::npos)
33+
return std::nullopt;
34+
auto b = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
35+
sepLocSave = sepLoc;
36+
sepLoc = line.find(';', sepLocSave + 1);
37+
if (sepLoc == std::string::npos)
38+
return std::nullopt;
39+
auto mapDataName = line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1);
40+
return std::make_tuple(ID, r, g, b, mapDataName);
41+
}
42+
catch (std::exception& e)
43+
{
44+
Log(LogLevel::Warning) << "Broken Definition Line: " << line << " - " << e.what();
45+
return std::nullopt;
46+
}
47+
}
48+
49+
} // namespace
50+
51+
752
void Definitions::loadDefinitions(const std::string& fileName, const LocalizationMapper& localizationMapper, LocalizationMapper::LocType locType)
853
{
954
if (!commonItems::DoesFileExist(fileName))
@@ -35,7 +80,7 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
3580
if (locType == LocalizationMapper::LocType::SOURCE)
3681
{
3782
// can we get a locname? Probe for PROV first.
38-
auto locName = localizationMapper.getLocForSourceKey("PROV" + std::to_string(ID));
83+
auto locName = localizationMapper.getLocForSourceKey("PROV" + ID);
3984
if (locName && !locName->empty())
4085
{
4186
province->locName = locName;
@@ -51,7 +96,7 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
5196
else
5297
{
5398
// ditto for the other defs.
54-
auto locName = localizationMapper.getLocForTargetKey("PROV" + std::to_string(ID));
99+
auto locName = localizationMapper.getLocForTargetKey("PROV" + ID);
55100
if (locName && !locName->empty())
56101
{
57102
province->locName = locName;
@@ -74,43 +119,6 @@ void Definitions::parseStream(std::istream& theStream, const LocalizationMapper&
74119
}
75120
}
76121

77-
std::optional<std::tuple<int, unsigned char, unsigned char, unsigned char, std::string>> Definitions::parseLine(const std::string& line) const
78-
{
79-
try
80-
{
81-
auto sepLoc = line.find(';');
82-
if (sepLoc == std::string::npos)
83-
return std::nullopt;
84-
auto sepLocSave = sepLoc;
85-
auto ID = std::stoi(line.substr(0, sepLoc));
86-
sepLoc = line.find(';', sepLocSave + 1);
87-
if (sepLoc == std::string::npos)
88-
return std::nullopt;
89-
auto r = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
90-
sepLocSave = sepLoc;
91-
sepLoc = line.find(';', sepLocSave + 1);
92-
if (sepLoc == std::string::npos)
93-
return std::nullopt;
94-
auto g = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
95-
sepLocSave = sepLoc;
96-
sepLoc = line.find(';', sepLocSave + 1);
97-
if (sepLoc == std::string::npos)
98-
return std::nullopt;
99-
auto b = static_cast<unsigned char>(std::stoi(line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1)));
100-
sepLocSave = sepLoc;
101-
sepLoc = line.find(';', sepLocSave + 1);
102-
if (sepLoc == std::string::npos)
103-
return std::nullopt;
104-
auto mapDataName = line.substr(sepLocSave + 1, sepLoc - sepLocSave - 1);
105-
return std::make_tuple(ID, r, g, b, mapDataName);
106-
}
107-
catch (std::exception& e)
108-
{
109-
Log(LogLevel::Warning) << "Broken Definition Line: " << line << " - " << e.what();
110-
return std::nullopt;
111-
}
112-
}
113-
114122
void Definitions::registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b)
115123
{
116124
Pixel pixel(x, y, r, g, b);
@@ -135,7 +143,7 @@ std::optional<std::string> Definitions::getNameForChroma(const int chroma)
135143
return std::nullopt;
136144
}
137145

138-
std::optional<int> Definitions::getIDForChroma(const int chroma)
146+
std::optional<std::string> Definitions::getIDForChroma(const int chroma)
139147
{
140148
if (const auto& chromaCacheItr = chromaCache.find(chroma); chromaCacheItr != chromaCache.end())
141149
return chromaCacheItr->second->ID;
@@ -151,7 +159,7 @@ std::shared_ptr<Province> Definitions::getProvinceForChroma(const int chroma)
151159
return nullptr;
152160
}
153161

154-
std::shared_ptr<Province> Definitions::getProvinceForID(const int ID)
162+
std::shared_ptr<Province> Definitions::getProvinceForID(const std::string& ID)
155163
{
156164
if (const auto& provinceItr = provinces.find(ID); provinceItr != provinces.end())
157165
return provinceItr->second;
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
#ifndef DEFINITIONS_H
22
#define DEFINITIONS_H
3-
#include "Localization/LocalizationMapper.h"
4-
#include <map>
5-
#include <memory>
6-
#include <optional>
7-
#include <string>
3+
4+
5+
6+
#include "DefinitionsInterface.h"
7+
8+
89

910
struct Province;
10-
class Definitions
11+
class Definitions: public DefinitionsInterface
1112
{
1213
public:
1314
void loadDefinitions(const std::string& fileName, const LocalizationMapper& localizationMapper, LocalizationMapper::LocType locType);
1415

15-
void registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b);
16-
void registerBorderPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b);
16+
void registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) override;
17+
void registerBorderPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) override;
1718

18-
[[nodiscard]] const auto& getProvinces() const { return provinces; }
19-
[[nodiscard]] std::optional<std::string> getNameForChroma(int chroma);
20-
[[nodiscard]] std::optional<int> getIDForChroma(int chroma);
21-
[[nodiscard]] std::shared_ptr<Province> getProvinceForChroma(int chroma);
22-
[[nodiscard]] std::shared_ptr<Province> getProvinceForID(int ID);
19+
[[nodiscard]] std::optional<std::string> getNameForChroma(int chroma) override;
20+
[[nodiscard]] std::optional<std::string> getIDForChroma(int chroma) override;
21+
[[nodiscard]] std::shared_ptr<Province> getProvinceForChroma(int chroma) override;
22+
[[nodiscard]] std::shared_ptr<Province> getProvinceForID(const std::string& ID) override;
2323

2424
private:
2525
void parseStream(std::istream& theStream, const LocalizationMapper& localizationMapper, LocalizationMapper::LocType locType);
26-
[[nodiscard]] std::optional<std::tuple<int, unsigned char, unsigned char, unsigned char, std::string>> parseLine(const std::string& line) const;
27-
28-
std::map<int, std::shared_ptr<Province>> provinces; // ID, province
29-
std::map<unsigned int, std::shared_ptr<Province>> chromaCache; // color, province
3026
};
3127

3228
#endif // DEFINITIONS_H
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef DEFINITIONS_INTERFACE_H
2+
#define DEFINITIONS_INTERFACE_H
3+
4+
5+
6+
#include "Localization/LocalizationMapper.h"
7+
#include <map>
8+
#include <memory>
9+
#include <optional>
10+
#include <string>
11+
12+
13+
struct Province;
14+
15+
16+
class DefinitionsInterface
17+
{
18+
public:
19+
virtual void registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) = 0;
20+
virtual void registerBorderPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) = 0;
21+
22+
[[nodiscard]] const auto& getProvinces() const { return provinces; }
23+
[[nodiscard]] virtual std::optional<std::string> getNameForChroma(int chroma) = 0;
24+
[[nodiscard]] virtual std::optional<std::string> getIDForChroma(int chroma) = 0;
25+
[[nodiscard]] virtual std::shared_ptr<Province> getProvinceForChroma(int chroma) = 0;
26+
[[nodiscard]] virtual std::shared_ptr<Province> getProvinceForID(const std::string& ID) = 0;
27+
28+
protected:
29+
std::map<std::string, std::shared_ptr<Province>> provinces; // ID, province
30+
std::map<unsigned int, std::shared_ptr<Province>> chromaCache; // color, province
31+
};
32+
33+
34+
35+
#endif // DEFINITIONS_INTERFACE_H
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "Vic3Definitions.h"
2+
#include "OSCompatibilityLayer.h"
3+
#include "Provinces/Pixel.h"
4+
#include "Provinces/Province.h"
5+
#include <fstream>
6+
#include <iomanip>
7+
8+
9+
10+
void Vic3Definitions::registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b)
11+
{
12+
Pixel pixel(x, y, r, g, b);
13+
const auto& chroma_itr = chromaCache.find(pixelPack(r, g, b));
14+
if (chroma_itr != chromaCache.end())
15+
{
16+
chroma_itr->second->innerPixels.emplace_back(pixel);
17+
}
18+
else
19+
{
20+
std::stringstream id;
21+
id << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(r) << static_cast<int>(g) << static_cast<int>(b);
22+
auto new_province = std::make_shared<Province>(id.str(), r, g, b, id.str());
23+
new_province->innerPixels.emplace_back(pixel);
24+
chromaCache.emplace(pixelPack(r, g, b), new_province);
25+
provinces.emplace(id.str(), new_province);
26+
}
27+
}
28+
29+
30+
void Vic3Definitions::registerBorderPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b)
31+
{
32+
Pixel pixel(x, y, r, g, b);
33+
const auto& chroma_itr = chromaCache.find(pixelPack(r, g, b));
34+
if (chroma_itr != chromaCache.end())
35+
{
36+
chroma_itr->second->borderPixels.emplace_back(pixel);
37+
}
38+
else
39+
{
40+
std::stringstream id;
41+
id << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(r) << static_cast<int>(g) << static_cast<int>(b);
42+
auto new_province = std::make_shared<Province>(id.str(), r, g, b, id.str());
43+
new_province->borderPixels.emplace_back(pixel);
44+
chromaCache.emplace(pixelPack(r, g, b), new_province);
45+
provinces.emplace(id.str(), new_province);
46+
}
47+
}
48+
49+
50+
std::optional<std::string> Vic3Definitions::getNameForChroma(const int chroma)
51+
{
52+
if (const auto& chroma_cache_itr = chromaCache.find(chroma); chroma_cache_itr != chromaCache.end())
53+
{
54+
return chroma_cache_itr->second->bespokeName();
55+
}
56+
else
57+
{
58+
return std::nullopt;
59+
}
60+
}
61+
62+
63+
std::optional<std::string> Vic3Definitions::getIDForChroma(const int chroma)
64+
{
65+
if (const auto& chroma_cache_itr = chromaCache.find(chroma); chroma_cache_itr != chromaCache.end())
66+
{
67+
return chroma_cache_itr->second->ID;
68+
}
69+
else
70+
{
71+
return std::nullopt;
72+
}
73+
}
74+
75+
76+
std::shared_ptr<Province> Vic3Definitions::getProvinceForChroma(const int chroma)
77+
{
78+
if (const auto& chroma_cache_itr = chromaCache.find(chroma); chroma_cache_itr != chromaCache.end())
79+
{
80+
return chroma_cache_itr->second;
81+
}
82+
else
83+
{
84+
return nullptr;
85+
}
86+
}
87+
88+
89+
std::shared_ptr<Province> Vic3Definitions::getProvinceForID(const std::string& ID)
90+
{
91+
if (const auto& province_itr = provinces.find(ID); province_itr != provinces.end())
92+
{
93+
return province_itr->second;
94+
}
95+
else
96+
{
97+
return nullptr;
98+
}
99+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef VIC3_DEFINITIONS_H
2+
#define VIC3_DEFINITIONS_H
3+
4+
5+
6+
#include "DefinitionsInterface.h"
7+
8+
9+
10+
class Vic3Definitions: public DefinitionsInterface
11+
{
12+
void loadDefinitions() {}
13+
14+
void registerPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) override;
15+
void registerBorderPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) override;
16+
17+
[[nodiscard]] std::optional<std::string> getNameForChroma(int chroma) override;
18+
[[nodiscard]] std::optional<std::string> getIDForChroma(int chroma) override;
19+
[[nodiscard]] std::shared_ptr<Province> getProvinceForChroma(int chroma) override;
20+
[[nodiscard]] std::shared_ptr<Province> getProvinceForID(const std::string& ID) override;
21+
};
22+
23+
24+
25+
#endif // VIC3_DEFINITIONS_H

0 commit comments

Comments
 (0)