Files
OpenRCT2-Web/src/openrct2/paint/tile_element/Paint.TileElement.cpp
T

412 lines
13 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2016-05-05 20:29:49 +02:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-05-05 20:29:49 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-05-05 20:29:49 +02:00
*****************************************************************************/
2018-06-22 23:03:53 +02:00
#include "Paint.TileElement.h"
#include "../../Game.h"
#include "../../Input.h"
2017-12-14 10:34:12 +01:00
#include "../../config/Config.h"
#include "../../core/Numerics.hpp"
2018-01-05 21:17:33 +00:00
#include "../../drawing/Drawing.h"
2018-01-05 23:45:53 +00:00
#include "../../interface/Viewport.h"
2018-01-06 18:32:25 +01:00
#include "../../localisation/Localisation.h"
2022-01-29 05:29:08 -08:00
#include "../../profiling/Profiling.h"
2018-01-10 00:00:09 +01:00
#include "../../ride/RideData.h"
2017-10-16 12:02:23 +02:00
#include "../../ride/TrackData.h"
2018-01-10 00:00:09 +01:00
#include "../../ride/TrackPaint.h"
2018-06-22 23:03:53 +02:00
#include "../../sprites.h"
2017-12-14 10:34:12 +01:00
#include "../../world/Banner.h"
#include "../../world/Entrance.h"
2018-01-11 10:59:26 +01:00
#include "../../world/Footpath.h"
2021-10-21 03:51:06 +01:00
#include "../../world/Map.h"
2018-01-11 10:59:26 +01:00
#include "../../world/Scenery.h"
2018-05-01 16:33:16 +02:00
#include "../../world/Surface.h"
2022-11-03 22:50:36 -07:00
#include "../Paint.SessionFlags.h"
2017-12-14 10:34:12 +01:00
#include "../Paint.h"
#include "../VirtualFloor.h"
2024-02-19 21:59:27 +01:00
#include "../support/WoodenSupports.h"
#include "Paint.Surface.h"
2017-12-14 10:34:12 +01:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
static void BlankTilesPaint(PaintSession& session, int32_t x, int32_t y);
static void PaintTileElementBase(PaintSession& session, const CoordsXY& origCoords);
/**
*
* rct2: 0x0068B35F
*/
void TileElementPaintSetup(PaintSession& session, const CoordsXY& mapCoords, bool isTrackPiecePreview)
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
2022-10-12 06:35:20 +01:00
if (!MapIsEdge(mapCoords))
2018-06-22 23:03:53 +02:00
{
2024-03-11 00:21:45 +01:00
PaintUtilSetSegmentSupportHeight(session, kSegmentsAll, 0xFFFF, 0);
PaintUtilForceSetGeneralSupportHeight(session, -1, 0);
session.Flags = isTrackPiecePreview ? PaintSessionFlags::IsTrackPiecePreview : 0;
2021-12-19 15:48:00 +01:00
session.WaterHeight = 0xFFFF;
2022-01-23 14:53:03 +01:00
PaintTileElementBase(session, mapCoords);
2018-06-22 23:03:53 +02:00
}
2021-12-19 15:48:00 +01:00
else if (!(session.ViewFlags & VIEWPORT_FLAG_TRANSPARENT_BACKGROUND))
2018-06-22 23:03:53 +02:00
{
BlankTilesPaint(session, mapCoords.x, mapCoords.y);
2017-06-06 23:24:18 +02:00
}
}
/**
*
* rct2: 0x0068B60E
*/
static void BlankTilesPaint(PaintSession& session, int32_t x, int32_t y)
{
int32_t dx = 0;
2021-12-19 15:48:00 +01:00
switch (session.CurrentRotation)
{
2018-06-22 23:03:53 +02:00
case 0:
dx = x + y;
break;
case 1:
x += 32;
dx = y - x;
break;
case 2:
x += 32;
y += 32;
dx = -(x + y);
break;
case 3:
y += 32;
dx = x - y;
break;
2017-06-06 23:24:18 +02:00
}
dx /= 2;
dx -= 16;
int32_t bx = dx + 32;
2017-09-02 22:15:09 +01:00
2023-04-03 21:54:13 +02:00
if (bx <= session.DPI.y)
2018-06-22 23:03:53 +02:00
return;
2017-06-06 23:24:18 +02:00
dx -= 20;
2023-04-03 21:54:13 +02:00
dx -= session.DPI.height;
if (dx >= session.DPI.y)
2018-06-22 23:03:53 +02:00
return;
2017-09-02 22:15:09 +01:00
2021-12-19 15:48:00 +01:00
session.SpritePosition.x = x;
session.SpritePosition.y = y;
session.InteractionType = ViewportInteractionItem::None;
PaintAddImageAsParent(session, ImageId(SPR_BLANK_TILE), { 0, 0, 16 }, { 32, 32, -1 });
}
bool gShowSupportSegmentHeights = false;
/**
*
* rct2: 0x0068B3FB
*/
static void PaintTileElementBase(PaintSession& session, const CoordsXY& origCoords)
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
2022-01-23 14:53:03 +01:00
CoordsXY coords = origCoords;
2021-12-19 15:48:00 +01:00
if ((session.ViewFlags & VIEWPORT_FLAG_CLIP_VIEW))
2018-05-08 01:25:05 +02:00
{
2022-01-23 14:53:03 +01:00
if (coords.x < gClipSelectionA.x || coords.x > gClipSelectionB.x)
2018-05-08 01:25:05 +02:00
return;
2022-01-23 14:53:03 +01:00
if (coords.y < gClipSelectionA.y || coords.y > gClipSelectionB.y)
2018-05-08 01:25:05 +02:00
return;
}
2021-12-19 15:48:00 +01:00
session.LeftTunnelCount = 0;
session.RightTunnelCount = 0;
session.LeftTunnels[0] = { 0xFF, 0xFF };
session.RightTunnels[0] = { 0xFF, 0xFF };
session.VerticalTunnelHeight = 0xFF;
2022-01-23 14:53:03 +01:00
session.MapPosition.x = coords.x;
session.MapPosition.y = coords.y;
auto* tile_element = MapGetFirstElementAt(session.MapPosition);
if (tile_element == nullptr)
return;
2021-12-19 15:48:00 +01:00
uint8_t rotation = session.CurrentRotation;
2017-10-06 19:21:58 +02:00
bool partOfVirtualFloor = false;
2022-06-06 10:07:07 +02:00
2022-10-16 17:27:01 +02:00
if (gConfigGeneral.VirtualFloorStyle != VirtualFloorStyles::Off)
{
partOfVirtualFloor = VirtualFloorTileIsFloor(session.MapPosition);
}
2017-10-06 19:21:58 +02:00
2018-06-22 23:03:53 +02:00
switch (rotation)
{
case 0:
break;
case 1:
2022-01-23 14:53:03 +01:00
coords.x += COORDS_XY_STEP;
2018-06-22 23:03:53 +02:00
break;
case 2:
2022-01-23 14:53:03 +01:00
coords.x += COORDS_XY_STEP;
coords.y += COORDS_XY_STEP;
2018-06-22 23:03:53 +02:00
break;
case 3:
2022-01-23 14:53:03 +01:00
coords.y += COORDS_XY_STEP;
2018-06-22 23:03:53 +02:00
break;
2017-06-06 23:24:18 +02:00
}
2021-10-21 03:51:06 +01:00
int32_t screenMinY = Translate3DTo2DWithZ(rotation, { coords, 0 }).y;
2021-10-21 03:51:06 +01:00
2017-06-06 23:24:18 +02:00
// Display little yellow arrow when building footpaths?
2021-12-19 15:48:00 +01:00
if ((gMapSelectFlags & MAP_SELECT_FLAG_ENABLE_ARROW) && session.MapPosition.x == gMapSelectArrowPosition.x
&& session.MapPosition.y == gMapSelectArrowPosition.y)
2018-06-22 23:03:53 +02:00
{
uint8_t arrowRotation = (rotation + (gMapSelectArrowDirection & 3)) & 3;
2022-09-28 21:36:56 +02:00
uint32_t imageIndex = arrowRotation + (gMapSelectArrowDirection & 0xFC) + PEEP_SPAWN_ARROW_0;
ImageId imageId = ImageId(imageIndex, COLOUR_YELLOW);
int32_t arrowZ = gMapSelectArrowPosition.z;
2022-01-23 14:53:03 +01:00
session.SpritePosition.x = coords.x;
session.SpritePosition.y = coords.y;
2021-12-19 15:48:00 +01:00
session.InteractionType = ViewportInteractionItem::None;
PaintAddImageAsParent(session, imageId, { 0, 0, arrowZ }, { { 0, 0, arrowZ + 18 }, { 32, 32, -1 } });
2017-06-06 23:24:18 +02:00
}
2023-04-03 21:54:13 +02:00
if (screenMinY + 52 <= session.DPI.y)
2017-06-06 23:24:18 +02:00
return;
2018-11-01 13:53:50 +01:00
const TileElement* element = tile_element; // push tile_element
uint16_t max_height = 0;
2018-06-22 23:03:53 +02:00
do
{
2020-04-22 17:09:29 +02:00
max_height = std::max(max_height, static_cast<uint16_t>(element->GetClearanceZ()));
} while (!(element++)->IsLastForTile());
2017-06-06 23:24:18 +02:00
element--;
2021-12-11 00:39:39 +01:00
if (element->GetType() == TileElementType::Surface && (element->AsSurface()->GetWaterHeight() > 0))
2017-07-27 17:15:56 +02:00
{
2020-01-19 16:12:48 +01:00
max_height = element->AsSurface()->GetWaterHeight();
2017-06-06 23:24:18 +02:00
}
2017-10-06 19:21:58 +02:00
if (partOfVirtualFloor)
{
// We must pretend this tile is at least as tall as the virtual floor
max_height = std::max(max_height, VirtualFloorGetHeight());
2017-10-06 19:21:58 +02:00
}
2023-04-03 21:54:13 +02:00
if (screenMinY - (max_height + 32) >= session.DPI.y + session.DPI.height)
2017-06-06 23:24:18 +02:00
return;
2022-01-23 14:53:03 +01:00
session.SpritePosition.x = coords.x;
session.SpritePosition.y = coords.y;
session.Flags &= ~PaintSessionFlags::PassedSurface;
int32_t previousBaseZ = 0;
2018-06-22 23:03:53 +02:00
do
{
2021-10-27 14:21:14 +02:00
if (tile_element->IsInvisible())
{
continue;
}
2017-10-31 12:57:40 +01:00
// Only paint tile_elements below the clip height.
2021-12-19 15:48:00 +01:00
if ((session.ViewFlags & VIEWPORT_FLAG_CLIP_VIEW) && (tile_element->GetBaseZ() > gClipHeight * COORDS_Z_STEP))
2018-03-27 22:00:58 +02:00
continue;
2016-10-21 09:46:59 +02:00
2019-08-26 11:25:57 +01:00
Direction direction = tile_element->GetDirectionWithOffset(rotation);
int32_t baseZ = tile_element->GetBaseZ();
// If we are on a new baseZ level, look through elements on the
// same baseZ and store any types might be relevant to others
if (baseZ != previousBaseZ)
{
previousBaseZ = baseZ;
2021-12-19 15:48:00 +01:00
session.PathElementOnSameHeight = nullptr;
session.TrackElementOnSameHeight = nullptr;
const TileElement* tile_element_sub_iterator = tile_element;
while (!(tile_element_sub_iterator++)->IsLastForTile())
{
2021-10-27 14:21:14 +02:00
if (tile_element->IsInvisible())
{
continue;
}
if (tile_element_sub_iterator->GetBaseZ() != tile_element->GetBaseZ())
{
break;
}
2021-12-11 00:39:39 +01:00
auto type = tile_element_sub_iterator->GetType();
if (type == TileElementType::Path)
2021-12-19 15:48:00 +01:00
session.PathElementOnSameHeight = tile_element_sub_iterator;
2021-12-11 00:39:39 +01:00
else if (type == TileElementType::Track)
2021-12-19 15:48:00 +01:00
session.TrackElementOnSameHeight = tile_element_sub_iterator;
}
}
2021-12-19 15:48:00 +01:00
CoordsXY mapPosition = session.MapPosition;
2022-03-04 01:33:08 +00:00
session.CurrentlyDrawnTileElement = tile_element;
2017-06-06 23:24:18 +02:00
// Setup the painting of for example: the underground, signs, rides, scenery, etc.
2021-12-11 00:39:39 +01:00
switch (tile_element->GetType())
2017-06-06 23:24:18 +02:00
{
2021-12-11 00:39:39 +01:00
case TileElementType::Surface:
PaintSurface(session, direction, baseZ, *(tile_element->AsSurface()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::Path:
PaintPath(session, baseZ, *(tile_element->AsPath()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::Track:
PaintTrack(session, direction, baseZ, *(tile_element->AsTrack()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::SmallScenery:
PaintSmallScenery(session, direction, baseZ, *(tile_element->AsSmallScenery()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::Entrance:
PaintEntrance(session, direction, baseZ, *(tile_element->AsEntrance()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::Wall:
PaintWall(session, direction, baseZ, *(tile_element->AsWall()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::LargeScenery:
PaintLargeScenery(session, direction, baseZ, *(tile_element->AsLargeScenery()));
2018-06-22 23:03:53 +02:00
break;
2021-12-11 00:39:39 +01:00
case TileElementType::Banner:
PaintBanner(session, direction, baseZ, *(tile_element->AsBanner()));
2018-06-22 23:03:53 +02:00
break;
2017-06-06 23:24:18 +02:00
}
2021-12-19 15:48:00 +01:00
session.MapPosition = mapPosition;
} while (!(tile_element++)->IsLastForTile());
2022-10-16 17:27:01 +02:00
if (gConfigGeneral.VirtualFloorStyle != VirtualFloorStyles::Off && partOfVirtualFloor)
2017-10-06 19:21:58 +02:00
{
VirtualFloorPaint(session);
2017-10-06 19:21:58 +02:00
}
2017-09-28 21:48:53 +02:00
2018-06-22 23:03:53 +02:00
if (!gShowSupportSegmentHeights)
{
2017-06-06 23:24:18 +02:00
return;
}
2021-12-11 00:39:39 +01:00
if ((tile_element - 1)->GetType() == TileElementType::Surface)
2018-06-22 23:03:53 +02:00
{
2017-06-06 23:24:18 +02:00
return;
}
static constexpr int32_t segmentPositions[][3] = {
2018-06-22 23:03:53 +02:00
{ 0, 6, 2 },
{ 5, 4, 8 },
{ 1, 7, 3 },
2017-06-06 23:24:18 +02:00
};
for (std::size_t sy = 0; sy < std::size(segmentPositions); sy++)
2018-06-22 23:03:53 +02:00
{
for (std::size_t sx = 0; sx < std::size(segmentPositions[sy]); sx++)
2018-06-22 23:03:53 +02:00
{
2021-12-19 15:48:00 +01:00
uint16_t segmentHeight = session.SupportSegments[segmentPositions[sy][sx]].height;
auto imageColourFlats = ImageId(SPR_LAND_TOOL_SIZE_1).WithTransparency(FilterPaletteID::PaletteGlassBlack);
2018-06-22 23:03:53 +02:00
if (segmentHeight == 0xFFFF)
{
2021-12-19 15:48:00 +01:00
segmentHeight = session.Support.height;
2017-06-06 23:24:18 +02:00
// white: 0b101101
2022-09-29 00:56:09 +02:00
imageColourFlats = ImageId(SPR_LAND_TOOL_SIZE_1)
2022-10-04 12:28:15 -04:00
.WithTransparency(FilterPaletteID::PaletteTranslucentBordeauxRedHighlight);
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Only draw supports below the clipping height.
2021-12-19 15:48:00 +01:00
if ((session.ViewFlags & VIEWPORT_FLAG_CLIP_VIEW) && (segmentHeight > gClipHeight))
2018-06-22 23:03:53 +02:00
continue;
2016-10-21 09:46:59 +02:00
int32_t xOffset = static_cast<int32_t>(sy) * 10;
int32_t yOffset = -22 + static_cast<int32_t>(sx) * 10;
PaintAddImageAsParent(
session, imageColourFlats, { xOffset, yOffset, segmentHeight },
{ { xOffset + 1, yOffset + 16, segmentHeight }, { 10, 10, 1 } });
2017-06-06 23:24:18 +02:00
}
}
}
2016-05-17 19:10:25 +02:00
void PaintUtilPushTunnelLeft(PaintSession& session, uint16_t height, uint8_t type)
2016-05-17 19:10:25 +02:00
{
2021-12-19 15:48:00 +01:00
session.LeftTunnels[session.LeftTunnelCount] = { static_cast<uint8_t>((height / 16)), type };
if (session.LeftTunnelCount < TUNNEL_MAX_COUNT - 1)
2018-06-22 23:03:53 +02:00
{
2021-12-19 15:48:00 +01:00
session.LeftTunnels[session.LeftTunnelCount + 1] = { 0xFF, 0xFF };
session.LeftTunnelCount++;
2017-07-09 22:48:21 +02:00
}
2016-05-17 19:10:25 +02:00
}
void PaintUtilPushTunnelRight(PaintSession& session, uint16_t height, uint8_t type)
2016-05-17 19:10:25 +02:00
{
2021-12-19 15:48:00 +01:00
session.RightTunnels[session.RightTunnelCount] = { static_cast<uint8_t>((height / 16)), type };
if (session.RightTunnelCount < TUNNEL_MAX_COUNT - 1)
2018-06-22 23:03:53 +02:00
{
2021-12-19 15:48:00 +01:00
session.RightTunnels[session.RightTunnelCount + 1] = { 0xFF, 0xFF };
session.RightTunnelCount++;
2017-07-09 22:48:21 +02:00
}
2016-05-17 19:10:25 +02:00
}
void PaintUtilSetVerticalTunnel(PaintSession& session, uint16_t height)
2016-09-09 18:16:40 +02:00
{
2021-12-19 15:48:00 +01:00
session.VerticalTunnelHeight = height / 16;
2016-09-09 18:16:40 +02:00
}
void PaintUtilSetGeneralSupportHeight(PaintSession& session, int16_t height, uint8_t slope)
2016-05-17 19:10:25 +02:00
{
2021-12-19 15:48:00 +01:00
if (session.Support.height >= height)
2018-06-22 23:03:53 +02:00
{
2017-06-06 23:24:18 +02:00
return;
}
2016-05-17 19:10:25 +02:00
PaintUtilForceSetGeneralSupportHeight(session, height, slope);
2016-05-17 19:10:25 +02:00
}
void PaintUtilForceSetGeneralSupportHeight(PaintSession& session, int16_t height, uint8_t slope)
2016-05-17 19:10:25 +02:00
{
2021-12-19 15:48:00 +01:00
session.Support.height = height;
session.Support.slope = slope;
2016-05-17 19:10:25 +02:00
}
2021-10-17 15:31:56 +02:00
const uint16_t segment_offsets[9] = {
2024-03-11 01:12:35 +01:00
EnumToFlag(PaintSegment::topCorner), EnumToFlag(PaintSegment::leftCorner), EnumToFlag(PaintSegment::rightCorner),
EnumToFlag(PaintSegment::bottomCorner), EnumToFlag(PaintSegment::centre), EnumToFlag(PaintSegment::topLeftSide),
EnumToFlag(PaintSegment::topRightSide), EnumToFlag(PaintSegment::bottomLeftSide), EnumToFlag(PaintSegment::bottomRightSide),
2021-10-17 15:31:56 +02:00
};
2016-05-17 19:10:25 +02:00
void PaintUtilSetSegmentSupportHeight(PaintSession& session, int32_t segments, uint16_t height, uint8_t slope)
2016-05-17 19:10:25 +02:00
{
SupportHeight* supportSegments = session.SupportSegments;
for (std::size_t s = 0; s < std::size(segment_offsets); s++)
2018-06-22 23:03:53 +02:00
{
if (segments & segment_offsets[s])
{
supportSegments[s].height = height;
2018-06-22 23:03:53 +02:00
if (height != 0xFFFF)
{
supportSegments[s].slope = slope;
2017-06-06 23:24:18 +02:00
}
}
}
2016-05-18 00:44:44 +02:00
}
uint16_t PaintUtilRotateSegments(uint16_t segments, uint8_t rotation)
2016-05-18 00:44:44 +02:00
{
2024-03-11 01:12:35 +01:00
// Only the value representing PaintSegment::centre falls beyond 0xFF, so this will be kept in place.
uint8_t temp = segments & 0xFF;
temp = Numerics::rol8(temp, rotation * 2);
2016-05-18 00:44:44 +02:00
2017-06-06 23:24:18 +02:00
return (segments & 0xFF00) | temp;
2016-05-18 00:44:44 +02:00
}
bool PaintShouldShowHeightMarkers(const PaintSession& session, const uint32_t viewportFlag)
{
2021-12-19 15:48:00 +01:00
auto dpi = &session.DPI;
return (session.ViewFlags & viewportFlag) && (dpi->zoom_level <= ZoomLevel{ 0 });
}