Skip to content

Commit

Permalink
Automap: Add minimap display option (diasurgical#6612)
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 authored and wkdgmr committed Oct 3, 2023
1 parent 470172c commit 47816d6
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 34 deletions.
131 changes: 99 additions & 32 deletions Source/automap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ void DrawLavaRiver(const Surface &out, Point center, uint8_t color, bool hasBrid
out.SetPixel(center + AmOffset(AmWidthOffset::QuarterTileRight, AmHeightOffset::QuarterTileUp), color);
}
if constexpr (IsAnyOf(Direction::NorthWest, TDir1, TDir2) || IsAnyOf(Direction::NorthEast, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::None, AmHeightOffset::None), color);
SetMapPixel(out, center + AmOffset(AmWidthOffset::None, AmHeightOffset::None), color);

Check failure on line 487 in Source/automap.cpp

View workflow job for this annotation

GitHub Actions / build

'SetMapPixel': identifier not found
}
if constexpr (IsAnyOf(Direction::SouthWest, TDir1, TDir2) || IsAnyOf(Direction::NorthWest, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::QuarterTileLeft, AmHeightOffset::QuarterTileDown), color);
SetMapPixel(out, center + AmOffset(AmWidthOffset::QuarterTileLeft, AmHeightOffset::QuarterTileDown), color);

Check failure on line 490 in Source/automap.cpp

View workflow job for this annotation

GitHub Actions / build

'SetMapPixel': identifier not found
}
if constexpr (IsAnyOf(Direction::SouthWest, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::HalfTileLeft, AmHeightOffset::HalfTileDown), color);
Expand All @@ -499,10 +499,10 @@ void DrawLavaRiver(const Surface &out, Point center, uint8_t color, bool hasBrid
out.SetPixel(center + AmOffset(AmWidthOffset::HalfTileRight, AmHeightOffset::None), color);
}
if constexpr (IsAnyOf(Direction::NorthEast, TDir1, TDir2) || IsAnyOf(Direction::SouthEast, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::QuarterTileRight, AmHeightOffset::QuarterTileDown), color);
SetMapPixel(out, center + AmOffset(AmWidthOffset::QuarterTileRight, AmHeightOffset::QuarterTileDown), color);

Check failure on line 502 in Source/automap.cpp

View workflow job for this annotation

GitHub Actions / build

'SetMapPixel': identifier not found
}
if constexpr (IsAnyOf(Direction::SouthWest, TDir1, TDir2) || IsAnyOf(Direction::SouthEast, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::None, AmHeightOffset::HalfTileDown), color);
SetMapPixel(out, center + AmOffset(AmWidthOffset::None, AmHeightOffset::HalfTileDown), color);

Check failure on line 505 in Source/automap.cpp

View workflow job for this annotation

GitHub Actions / build

'SetMapPixel': identifier not found
}
if constexpr (IsAnyOf(Direction::SouthWest, TDir1, TDir2)) {
out.SetPixel(center + AmOffset(AmWidthOffset::QuarterTileLeft, AmHeightOffset::ThreeQuartersTileDown), color);
Expand Down Expand Up @@ -914,7 +914,7 @@ bool HasAutomapFlag(Point position, AutomapTile::Flags type)
/**
* @brief Returns the automap shape at the given coordinate.
*/
AutomapTile GetAutomapType(Point position)
AutomapTile GetAutomapTileType(Point position)
{
if (position.x < 0 || position.x >= DMAXX || position.y < 0 || position.y >= DMAXX) {
return {};
Expand Down Expand Up @@ -961,7 +961,7 @@ AutomapTile GetAutomapTypeView(Point map)
return {};
}

return GetAutomapType(map);
return GetAutomapTileType(map);
}

/**
Expand Down Expand Up @@ -1230,6 +1230,26 @@ void DrawAutomapTile(const Surface &out, Point center, Point map)
}
}

Displacement GetAutomapScreen()
{
Displacement screen = {};

if (GetAutomapType() == AutomapType::Minimap) {
screen = {
MinimapRect.position.x + MinimapRect.size.width / 2,
MinimapRect.position.y + MinimapRect.size.height / 2
};
} else {
screen = {
gnScreenWidth / 2,
(gnScreenHeight - GetMainPanel().size.height) / 2
};
}
screen += AmOffset(AmWidthOffset::None, AmHeightOffset::HalfTileDown);

return screen;
}

void SearchAutomapItem(const Surface &out, const Displacement &myPlayerOffset, int searchRadius, tl::function_ref<bool(Point position)> highlightTile)
{
const Player &player = *MyPlayer;
Expand All @@ -1245,8 +1265,10 @@ void SearchAutomapItem(const Surface &out, const Displacement &myPlayerOffset, i
const int startX = clamp(tile.x - searchRadius, 0, MAXDUNX);
const int startY = clamp(tile.y - searchRadius, 0, MAXDUNY);

const int endX = clamp(tile.x + searchRadius, 0, MAXDUNX);
const int endY = clamp(tile.y + searchRadius, 0, MAXDUNY);
const int endX = std::clamp(tile.x + searchRadius, 0, MAXDUNX);
const int endY = std::clamp(tile.y + searchRadius, 0, MAXDUNY);

int scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

for (int i = startX; i < endX; i++) {
for (int j = startY; j < endY; j++) {
Expand All @@ -1257,8 +1279,8 @@ void SearchAutomapItem(const Surface &out, const Displacement &myPlayerOffset, i
int py = j - 2 * AutomapOffset.deltaY - ViewPosition.y;

Point screen = {
(myPlayerOffset.deltaX * AutoMapScale / 100 / 2) + (px - py) * AmLine(AmLineLength::DoubleTile) + gnScreenWidth / 2,
(myPlayerOffset.deltaY * AutoMapScale / 100 / 2) + (px + py) * AmLine(AmLineLength::FullTile) + (gnScreenHeight - GetMainPanel().size.height) / 2
(myPlayerOffset.deltaX * scale / 100 / 2) + (px - py) * AmLine(AmLineLength::DoubleTile) + gnScreenWidth / 2,
(myPlayerOffset.deltaY * scale / 100 / 2) + (px + py) * AmLine(AmLineLength::FullTile) + (gnScreenHeight - GetMainPanel().size.height) / 2
};

if (CanPanelsCoverView()) {
Expand Down Expand Up @@ -1293,11 +1315,15 @@ void DrawAutomapPlr(const Surface &out, const Displacement &myPlayerOffset, int
if (player.isWalking())
playerOffset = GetOffsetForWalking(player.AnimInfo, player._pdir);

int scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

Point base = {
((playerOffset.deltaX + myPlayerOffset.deltaX) * AutoMapScale / 100 / 2) + (px - py) * AmLine(AmLineLength::DoubleTile) + gnScreenWidth / 2,
((playerOffset.deltaY + myPlayerOffset.deltaY) * AutoMapScale / 100 / 2) + (px + py) * AmLine(AmLineLength::FullTile) + (gnScreenHeight - GetMainPanel().size.height) / 2 + AmOffset(AmWidthOffset::None, AmHeightOffset::FullTileDown).deltaY
((playerOffset.deltaX + myPlayerOffset.deltaX) * scale / 100 / 2) + (px - py) * AmLine(AmLineLength::DoubleTile),
((playerOffset.deltaY + myPlayerOffset.deltaY) * scale / 100 / 2) + (px + py) * AmLine(AmLineLength::FullTile) + AmOffset(AmWidthOffset::None, AmHeightOffset::FullTileDown).deltaY
};

base += GetAutomapScreen();

if (CanPanelsCoverView()) {
if (IsRightPanelOpen())
base.x -= gnScreenWidth / 4;
Expand Down Expand Up @@ -1453,14 +1479,34 @@ std::unique_ptr<AutomapTile[]> LoadAutomapData(size_t &tileCount)
} // namespace

bool AutomapActive;
AutomapType CurrentAutomapType = AutomapType::Opaque;
uint8_t AutomapView[DMAXX][DMAXY];
int AutoMapScale;
int MinimapScale;
Displacement AutomapOffset;
Rectangle MinimapRect {};

void InitAutomapOnce()
{
AutomapActive = false;
AutoMapScale = 50;

// Set the dimensions and screen position of the minimap relative to the screen dimensions
int minimapWidth = gnScreenWidth / 4;
Size minimapSize { minimapWidth, minimapWidth / 2 };
int minimapPadding = gnScreenWidth / 128;
MinimapRect = Rectangle { { gnScreenWidth - minimapPadding - minimapSize.width, minimapPadding }, minimapSize };

// Set minimap scale
int height = 480;
int scale = 25;
int factor = gnScreenHeight / height;

if (factor >= 8) {
MinimapScale = scale * 8;
} else {
MinimapScale = scale * factor;
}
}

void InitAutomap()
Expand Down Expand Up @@ -1618,18 +1664,22 @@ void AutomapRight()

void AutomapZoomIn()
{
if (AutoMapScale >= 200)
int &scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

if (scale >= 200)
return;

AutoMapScale += 25;
scale += 25;
}

void AutomapZoomOut()
{
if (AutoMapScale <= 25)
int &scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

if (scale <= 25)
return;

AutoMapScale -= 25;
scale -= 25;
}

void DrawAutomap(const Surface &out)
Expand All @@ -1655,21 +1705,38 @@ void DrawAutomap(const Surface &out)
if (myPlayer.isWalking())
myPlayerOffset = GetOffsetForWalking(myPlayer.AnimInfo, myPlayer._pdir, true);

int d = (AutoMapScale * 64) / 100;
int scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;
int d = (scale * 64) / 100;
int cells = 2 * (gnScreenWidth / 2 / d) + 1;
if (((gnScreenWidth / 2) % d) != 0)
cells++;
if (((gnScreenWidth / 2) % d) >= (AutoMapScale * 32) / 100)
if (((gnScreenWidth / 2) % d) >= (scale * 32) / 100)
cells++;
if ((myPlayerOffset.deltaX + myPlayerOffset.deltaY) != 0)
cells++;

Point screen {
gnScreenWidth / 2,
(gnScreenHeight - GetMainPanel().size.height) / 2
};
if (GetAutomapType() == AutomapType::Minimap) {
// Background fill
DrawHalfTransparentRectTo(out, MinimapRect.position.x, MinimapRect.position.y, MinimapRect.size.width, MinimapRect.size.height);

screen += AmOffset(AmWidthOffset::None, AmHeightOffset::HalfTileDown);
uint8_t frameShadowColor = PAL16_YELLOW + 12;

// Shadow
DrawHorizontalLine(out, MinimapRect.position + Displacement { -1, -1 }, MinimapRect.size.width + 1, frameShadowColor);
DrawHorizontalLine(out, MinimapRect.position + Displacement { -2, MinimapRect.size.height + 1 }, MinimapRect.size.width + 4, frameShadowColor);
DrawVerticalLine(out, MinimapRect.position + Displacement { -1, 0 }, MinimapRect.size.height, frameShadowColor);
DrawVerticalLine(out, MinimapRect.position + Displacement { MinimapRect.size.width + 1, -2 }, MinimapRect.size.height + 3, frameShadowColor);

// Frame
DrawHorizontalLine(out, MinimapRect.position + Displacement { -2, -2 }, MinimapRect.size.width + 3, MapColorsDim);
DrawHorizontalLine(out, MinimapRect.position + Displacement { -2, MinimapRect.size.height }, MinimapRect.size.width + 3, MapColorsDim);
DrawVerticalLine(out, MinimapRect.position + Displacement { -2, -1 }, MinimapRect.size.height + 1, MapColorsDim);
DrawVerticalLine(out, MinimapRect.position + Displacement { MinimapRect.size.width, -1 }, MinimapRect.size.height + 1, MapColorsDim);
}

Point screen = {};

screen += GetAutomapScreen();

if ((cells & 1) != 0) {
screen.x -= AmOffset(AmWidthOffset::DoubleTileRight, AmHeightOffset::None).deltaX * ((cells - 1) / 2);
Expand All @@ -1688,8 +1755,8 @@ void DrawAutomap(const Surface &out)
screen.y -= AmOffset(AmWidthOffset::None, AmHeightOffset::HalfTileDown).deltaY;
}

screen.x += AutoMapScale * myPlayerOffset.deltaX / 100 / 2;
screen.y += AutoMapScale * myPlayerOffset.deltaY / 100 / 2;
screen.x += scale * myPlayerOffset.deltaX / 100 / 2;
screen.y += scale * myPlayerOffset.deltaY / 100 / 2;

if (CanPanelsCoverView()) {
if (IsRightPanelOpen()) {
Expand Down Expand Up @@ -1752,13 +1819,13 @@ void SetAutomapView(Point position, MapExplorationType explorer)

UpdateAutomapExplorer(map, explorer);

AutomapTile tile = GetAutomapType(map);
AutomapTile tile = GetAutomapTileType(map);
bool solid = tile.hasFlag(AutomapTile::Flags::Dirt);

switch (tile.type) {
case AutomapTile::Types::Vertical:
if (solid) {
auto tileSW = GetAutomapType({ map.x, map.y + 1 });
auto tileSW = GetAutomapTileType({ map.x, map.y + 1 });
if (tileSW.type == AutomapTile::Types::Corner && tileSW.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x, map.y + 1 }, explorer);
} else if (HasAutomapFlag({ map.x - 1, map.y }, AutomapTile::Flags::Dirt)) {
Expand All @@ -1767,7 +1834,7 @@ void SetAutomapView(Point position, MapExplorationType explorer)
break;
case AutomapTile::Types::Horizontal:
if (solid) {
auto tileSE = GetAutomapType({ map.x + 1, map.y });
auto tileSE = GetAutomapTileType({ map.x + 1, map.y });
if (tileSE.type == AutomapTile::Types::Corner && tileSE.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x + 1, map.y }, explorer);
} else if (HasAutomapFlag({ map.x, map.y - 1 }, AutomapTile::Flags::Dirt)) {
Expand All @@ -1776,10 +1843,10 @@ void SetAutomapView(Point position, MapExplorationType explorer)
break;
case AutomapTile::Types::Cross:
if (solid) {
auto tileSW = GetAutomapType({ map.x, map.y + 1 });
auto tileSW = GetAutomapTileType({ map.x, map.y + 1 });
if (tileSW.type == AutomapTile::Types::Corner && tileSW.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x, map.y + 1 }, explorer);
auto tileSE = GetAutomapType({ map.x + 1, map.y });
auto tileSE = GetAutomapTileType({ map.x + 1, map.y });
if (tileSE.type == AutomapTile::Types::Corner && tileSE.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x + 1, map.y }, explorer);
} else {
Expand All @@ -1795,7 +1862,7 @@ void SetAutomapView(Point position, MapExplorationType explorer)
if (solid) {
if (HasAutomapFlag({ map.x, map.y - 1 }, AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x, map.y - 1 }, explorer);
auto tileSW = GetAutomapType({ map.x, map.y + 1 });
auto tileSW = GetAutomapTileType({ map.x, map.y + 1 });
if (tileSW.type == AutomapTile::Types::Corner && tileSW.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x, map.y + 1 }, explorer);
} else if (HasAutomapFlag({ map.x - 1, map.y }, AutomapTile::Flags::Dirt)) {
Expand All @@ -1806,7 +1873,7 @@ void SetAutomapView(Point position, MapExplorationType explorer)
if (solid) {
if (HasAutomapFlag({ map.x - 1, map.y }, AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x - 1, map.y }, explorer);
auto tileSE = GetAutomapType({ map.x + 1, map.y });
auto tileSE = GetAutomapTileType({ map.x + 1, map.y });
if (tileSE.type == AutomapTile::Types::Corner && tileSE.hasFlag(AutomapTile::Flags::Dirt))
UpdateAutomapExplorer({ map.x + 1, map.y }, explorer);
} else if (HasAutomapFlag({ map.x, map.y - 1 }, AutomapTile::Flags::Dirt)) {
Expand Down
48 changes: 46 additions & 2 deletions Source/automap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ extern DVL_API_FOR_TEST bool AutomapActive;
extern uint8_t AutomapView[DMAXX][DMAXY];
/** Specifies the scale of the automap. */
extern DVL_API_FOR_TEST int AutoMapScale;
extern DVL_API_FOR_TEST int MinimapScale;
extern DVL_API_FOR_TEST Displacement AutomapOffset;
extern Rectangle MinimapRect;

/** Defines the offsets used for Automap lines */
enum class AmWidthOffset : int8_t {
Expand Down Expand Up @@ -78,16 +80,53 @@ enum class AmLineLength : uint8_t {
OctupleTile = 64,
};

enum class AutomapType : uint8_t {
Opaque,
FIRST = Opaque,
Transparent,
Minimap,
LAST = Minimap
};

extern DVL_API_FOR_TEST AutomapType CurrentAutomapType;

/**
* @brief Sets the map type. Does not change `AutomapActive`.
*/
inline void SetAutomapType(AutomapType type)
{
CurrentAutomapType = type;
}

/**
* @brief Sets the map type. Does not change `AutomapActive`.
*/
inline AutomapType GetAutomapType()
{
return CurrentAutomapType;
}

inline Displacement AmOffset(AmWidthOffset x, AmHeightOffset y)
{
return { AutoMapScale * static_cast<int>(x) / 100, AutoMapScale * static_cast<int>(y) / 100 };
int scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

return { scale * static_cast<int>(x) / 100, scale * static_cast<int>(y) / 100 };
}

inline int AmLine(AmLineLength l)
{
return AutoMapScale * static_cast<int>(l) / 100;
int scale = (GetAutomapType() == AutomapType::Minimap) ? MinimapScale : AutoMapScale;

return scale * static_cast<int>(l) / 100;
}

/**
* @brief Sets the map type. Does not change `AutomapActive`.
*/
void SetAutomapType(AutomapType type);

AutomapType GetAutomapType();

/**
* @brief Initializes the automap.
*/
Expand All @@ -103,6 +142,11 @@ void InitAutomap();
*/
void StartAutomap();

/**
* @brief Displays the minimap.
*/
void StartMinimap();

/**
* @brief Scrolls the automap upwards.
*/
Expand Down
14 changes: 14 additions & 0 deletions Source/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,20 @@ void DoAutoMap()
AutomapActive = false;
}

void CycleAutomapType()
{
if (!AutomapActive) {
StartAutomap();
return;
}
const AutomapType newType { static_cast<std::underlying_type_t<AutomapType>>(
(static_cast<unsigned>(GetAutomapType()) + 1) % enum_size<AutomapType>::value) };
SetAutomapType(newType);
if (newType == AutomapType::FIRST) {
AutomapActive = false;
}
}

void CheckPanelInfo()
{
panelflag = false;
Expand Down
Loading

0 comments on commit 47816d6

Please sign in to comment.