Skip to content

Commit 1077447

Browse files
past-duepull[bot]
authored andcommittedApr 11, 2024
wzmaplib: Improve map preview generation
Better output of structures (based on structure size and direction).
1 parent 790ab4f commit 1077447

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed
 

‎lib/wzmaplib/include/wzmaplib/map_preview.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <memory>
2424
#include <cinttypes>
2525
#include "map.h"
26+
#include "map_stats.h"
2627

2728
namespace WzMap {
2829

@@ -78,6 +79,6 @@ struct MapPreviewImage
7879
uint32_t channels; // ex. 3 for RGB
7980
};
8081

81-
std::unique_ptr<MapPreviewImage> generate2DMapPreview(WzMap::Map& wzMap, const MapPreviewColorScheme& colorScheme, WzMap::LoggingProtocol* pCustomLogger = nullptr);
82+
std::unique_ptr<MapPreviewImage> generate2DMapPreview(WzMap::Map& wzMap, const MapPreviewColorScheme& colorScheme, const MapStatsConfiguration& statsConfig = MapStatsConfiguration(), WzMap::LoggingProtocol* pCustomLogger = nullptr);
8283

8384
} // namespace WzMap

‎lib/wzmaplib/include/wzmaplib/map_stats.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ namespace WzMap {
169169
// - "type": "OIL DRUM"
170170
std::unordered_set<std::string> oilDrums;
171171

172+
public:
173+
bool isStructExpansionModule(const std::string& struct_id) const;
174+
172175
private:
173176
// [STRUCT SIZES]:
174177
typedef std::unordered_map<std::string, StructureSize> StructSizesMap;

‎lib/wzmaplib/src/map_preview.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of Warzone 2100.
3-
Copyright (C) 2013-2021 Warzone 2100 Project
3+
Copyright (C) 2013-2023 Warzone 2100 Project
44
55
Warzone 2100 is free software; you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -119,7 +119,7 @@ static inline T clip(T x, T min, T max)
119119
return x < min ? min : x > max ? max : x;
120120
}
121121

122-
static void plotBackdropPixel(MapPreviewImage& output, int32_t xx, int32_t yy, MapPreviewColor const &colour)
122+
static void plotBackdropPixel(MapPreviewImage& output, int32_t xx, int32_t yy, const MapPreviewColor &colour)
123123
{
124124
xx = clip(xx, 0, static_cast<int32_t>(output.width - 1));
125125
yy = clip(yy, 0, static_cast<int32_t>(output.height - 1));
@@ -129,6 +129,47 @@ static void plotBackdropPixel(MapPreviewImage& output, int32_t xx, int32_t yy, M
129129
pixel[2] = colour.b;
130130
}
131131

132+
// Round direction to nearest axis-aligned direction.
133+
static inline uint16_t snapDirection(uint16_t direction)
134+
{
135+
return (direction + 0x2000) & 0xC000;
136+
}
137+
138+
static void plotBackdropStructurePixels(MapPreviewImage& output, const Structure &structure, const MapStatsConfiguration& statsConfig, const MapPreviewColor &colour)
139+
{
140+
if (statsConfig.isStructExpansionModule(structure.name))
141+
{
142+
// Skip drawing expansion modules on the map preview
143+
return;
144+
}
145+
146+
MapStatsConfiguration::StructureSize structStatSize = statsConfig.getStructureSize(structure.name).value_or(MapStatsConfiguration::StructureSize(1,1));
147+
148+
std::pair<int32_t, int32_t> size = {structStatSize.baseWidth, structStatSize.baseBreadth};
149+
if ((snapDirection(structure.direction) & 0x4000) != 0)
150+
{
151+
// If building is rotated left or right by 90°, swap width and height
152+
std::swap(size.first, size.second);
153+
}
154+
155+
// Snap the WorldPos to a tile
156+
int32_t x = (structure.position.x & ~TILE_MASK) + size.first % 2 * TILE_UNITS / 2;
157+
int32_t y = (structure.position.y & ~TILE_MASK) + size.second % 2 * TILE_UNITS / 2;
158+
159+
// Calculate starting MapTilePos for output
160+
std::pair<int32_t, int32_t> map = { map_coord(x), map_coord(y) };
161+
map.first -= size.first / 2;
162+
map.second -= size.second / 2;
163+
164+
for (int32_t tileX = map.first; tileX < map.first + size.first; ++tileX)
165+
{
166+
for (int32_t tileY = map.second; tileY < map.second + size.second; ++tileY)
167+
{
168+
plotBackdropPixel(output, tileX, tileY, colour);
169+
}
170+
}
171+
}
172+
132173
static inline bool isOilResource(const Feature& feature)
133174
{
134175
return feature.name.rfind("OilResource", 0) == 0;
@@ -184,7 +225,7 @@ static void plotWzMapFeature(Map &wzMap, const MapPreviewColorScheme& colorSchem
184225
* present. Additionally we load the player's HQ location into playeridpos so
185226
* we know the player's starting location.
186227
*/
187-
bool plotStructurePreviewWzMap(Map &wzMap, const MapPreviewColorScheme& colorScheme, MapPreviewImage& output, LoggingProtocol* pCustomLogger)
228+
bool plotStructurePreviewWzMap(Map &wzMap, const MapPreviewColorScheme& colorScheme, const MapStatsConfiguration& statsConfig, MapPreviewImage& output, LoggingProtocol* pCustomLogger)
188229
{
189230
auto pStructures = wzMap.mapStructures();
190231
if (pStructures == nullptr)
@@ -219,7 +260,7 @@ bool plotStructurePreviewWzMap(Map &wzMap, const MapPreviewColorScheme& colorSch
219260
color = colorScheme.hqColor;
220261
}
221262
// and now we blit the color to the texture
222-
plotBackdropPixel(output, pos.first, pos.second, color);
263+
plotBackdropStructurePixels(output, structure, statsConfig, color);
223264
}
224265

225266
plotWzMapFeature(wzMap, colorScheme, output, pCustomLogger);
@@ -237,7 +278,7 @@ static inline TYPE_OF_TERRAIN terrainTypeWzMap(const MapData::MapTile& tile, con
237278
return data.terrainTypes[tileType];
238279
}
239280

240-
std::unique_ptr<MapPreviewImage> generate2DMapPreview(Map& wzMap, const MapPreviewColorScheme& colorScheme, LoggingProtocol* pCustomLogger /*= nullptr*/)
281+
std::unique_ptr<MapPreviewImage> generate2DMapPreview(Map& wzMap, const MapPreviewColorScheme& colorScheme, const MapStatsConfiguration& statsConfig /*= MapStatsConfiguration()*/, LoggingProtocol* pCustomLogger /*= nullptr*/)
241282
{
242283
auto mapData = wzMap.mapData();
243284
if (!mapData)
@@ -303,7 +344,7 @@ std::unique_ptr<MapPreviewImage> generate2DMapPreview(Map& wzMap, const MapPrevi
303344
}
304345

305346
// color our texture with clancolors @ correct position
306-
plotStructurePreviewWzMap(wzMap, colorScheme, *(result.get()), pCustomLogger);
347+
plotStructurePreviewWzMap(wzMap, colorScheme, statsConfig, *(result.get()), pCustomLogger);
307348

308349
return result;
309350
}

‎lib/wzmaplib/src/map_stats.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of Warzone 2100.
3-
Copyright (C) 2022 Warzone 2100 Project
3+
Copyright (C) 2022-2023 Warzone 2100 Project
44
55
Warzone 2100 is free software; you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -662,4 +662,11 @@ optional<MapStatsConfiguration::StructureSize> MapStatsConfiguration::getStructu
662662
return it->second;
663663
}
664664

665+
bool MapStatsConfiguration::isStructExpansionModule(const std::string& struct_id) const
666+
{
667+
return powerModules.count(struct_id)
668+
|| factoryModules.count(struct_id)
669+
|| researchModules.count(struct_id);
670+
}
671+
665672
} // namespace WzMap

‎src/multiint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ void loadMapPreview(bool hideInterface)
698698
}
699699

700700
std::unique_ptr<WzMap::LoggingProtocol> generatePreviewLogger(new WzMapDebugLogger());
701-
auto mapPreviewResult = WzMap::generate2DMapPreview(*data, previewColorScheme, generatePreviewLogger.get());
701+
auto mapPreviewResult = WzMap::generate2DMapPreview(*data, previewColorScheme, WzMap::MapStatsConfiguration(WzMap::MapType::SKIRMISH), generatePreviewLogger.get());
702702
if (!mapPreviewResult)
703703
{
704704
// Failed to generate map preview

0 commit comments

Comments
 (0)