Files
OpenRCT2-Web/src/openrct2/ride/TrackDesign.cpp
T

2132 lines
75 KiB
C++
Raw Normal View History

2016-05-04 18:54:33 +01:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2016-05-04 18:54:33 +01: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-04 18:54:33 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-05-04 18:54:33 +01:00
*****************************************************************************/
2018-06-22 23:14:18 +02:00
#include "TrackDesign.h"
2017-12-13 13:02:24 +01:00
#include "../Cheats.h"
#include "../Context.h"
2018-06-22 23:14:18 +02:00
#include "../Game.h"
#include "../OpenRCT2.h"
2019-04-08 19:34:24 +01:00
#include "../TrackImporter.h"
#include "../actions/FootpathPlaceFromTrackAction.h"
#include "../actions/FootpathRemoveAction.h"
#include "../actions/LargeSceneryPlaceAction.h"
#include "../actions/LargeSceneryRemoveAction.h"
#include "../actions/MazePlaceTrackAction.h"
#include "../actions/RideCreateAction.h"
#include "../actions/RideEntranceExitPlaceAction.h"
#include "../actions/RideSetNameAction.h"
#include "../actions/RideSetSettingAction.h"
#include "../actions/RideSetVehicleAction.h"
#include "../actions/SmallSceneryPlaceAction.h"
#include "../actions/SmallSceneryRemoveAction.h"
#include "../actions/TrackPlaceAction.h"
#include "../actions/TrackRemoveAction.h"
#include "../actions/WallPlaceAction.h"
#include "../actions/WallRemoveAction.h"
2018-06-22 23:14:18 +02:00
#include "../audio/audio.h"
2019-12-11 14:12:33 +00:00
#include "../core/DataSerialiser.h"
2018-05-02 13:15:26 +01:00
#include "../core/File.h"
#include "../core/Numerics.hpp"
#include "../core/String.hpp"
#include "../drawing/X8DrawingEngine.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Localisation.h"
#include "../localisation/StringIds.h"
2017-10-06 22:37:06 +02:00
#include "../management/Finance.h"
#include "../network/network.h"
#include "../object/FootpathObject.h"
2018-02-13 15:20:55 +01:00
#include "../object/ObjectList.h"
#include "../object/ObjectManager.h"
#include "../object/ObjectRepository.h"
2018-01-09 10:40:42 +01:00
#include "../rct1/RCT1.h"
#include "../rct1/Tables.h"
2017-12-13 13:02:24 +01:00
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
2018-01-11 10:59:26 +01:00
#include "../world/Footpath.h"
2018-03-19 23:28:40 +01:00
#include "../world/Park.h"
2018-01-11 10:59:26 +01:00
#include "../world/Scenery.h"
2017-12-04 20:09:38 +01:00
#include "../world/SmallScenery.h"
2018-05-01 16:33:16 +02:00
#include "../world/Surface.h"
#include "../world/Wall.h"
2018-06-22 23:14:18 +02:00
#include "Ride.h"
#include "RideData.h"
#include "Track.h"
#include "TrackData.h"
2021-09-09 00:06:34 +03:00
#include "TrackDesign.h"
2018-06-22 23:14:18 +02:00
#include "TrackDesignRepository.h"
2021-02-22 22:13:06 +01:00
#include "Vehicle.h"
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-11-21 23:16:04 +01:00
#include <iterator>
2020-06-15 14:47:07 -04:00
#include <memory>
2018-08-12 12:50:40 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
2021-08-22 09:42:27 -04:00
using namespace OpenRCT2::TrackMetaData;
2018-06-22 23:14:18 +02:00
bool gTrackDesignSceneryToggle;
2020-04-19 21:58:27 +02:00
static CoordsXYZ _trackPreviewMin;
static CoordsXYZ _trackPreviewMax;
static CoordsXYZ _trackPreviewOrigin;
2021-08-08 18:10:52 +02:00
bool _trackDesignDrawingPreview;
2018-06-22 23:14:18 +02:00
static uint8_t _trackDesignPlaceOperation;
static int16_t _trackDesignPlaceZ;
static int16_t _trackDesignPlaceSceneryZ;
2016-05-02 02:40:56 +01:00
// Previously all flags in byte_F4414E
static bool _trackDesignPlaceStateEntranceExitPlaced = false;
bool _trackDesignPlaceStateSceneryUnavailable = false;
2018-06-22 23:14:18 +02:00
static bool _trackDesignPlaceStateHasScenery = false;
static bool _trackDesignPlaceStatePlaceScenery = true;
2020-04-05 08:28:07 +01:00
static bool _trackDesignPlaceIsReplay = false;
2016-04-28 22:57:04 +01:00
static void track_design_preview_clear_map();
2019-06-30 11:23:06 +01:00
rct_string_id TrackDesign::CreateTrackDesign(const Ride& ride)
{
type = ride.type;
2021-09-12 21:25:22 +02:00
auto object = object_entry_get_object(ObjectType::Ride, ride.subtype);
if (object != nullptr)
{
auto entry = object->GetObjectEntry();
if (entry.IsEmpty())
{
// TODO create a new error message for `JSON objects are unsupported`
return STR_UNKNOWN_OBJECT_TYPE;
}
vehicle_object = ObjectEntryDescriptor(entry);
}
2019-06-30 11:23:06 +01:00
ride_mode = ride.mode;
colour_scheme = ride.colour_scheme_type & 3;
for (int32_t i = 0; i < RCT2_MAX_CARS_PER_TRAIN; i++)
2019-06-30 11:23:06 +01:00
{
vehicle_colours[i].body_colour = ride.vehicle_colours[i].Body;
vehicle_colours[i].trim_colour = ride.vehicle_colours[i].Trim;
vehicle_additional_colour[i] = ride.vehicle_colours[i].Ternary;
}
for (int32_t i = 0; i < RCT12_NUM_COLOUR_SCHEMES; i++)
{
track_spine_colour[i] = ride.track_colour[i].main;
track_rail_colour[i] = ride.track_colour[i].additional;
track_support_colour[i] = ride.track_colour[i].supports;
}
depart_flags = ride.depart_flags;
number_of_trains = ride.num_vehicles;
number_of_cars_per_train = ride.num_cars_per_train;
min_waiting_time = ride.min_waiting_time;
max_waiting_time = ride.max_waiting_time;
operation_setting = ride.operation_option;
lift_hill_speed = ride.lift_hill_speed;
num_circuits = ride.num_circuits;
entrance_style = ride.entrance_style;
2020-04-17 14:53:33 -03:00
max_speed = static_cast<int8_t>(ride.max_speed / 65536);
average_speed = static_cast<int8_t>(ride.average_speed / 65536);
2019-06-30 11:23:06 +01:00
ride_length = ride_get_total_length(&ride) / 65536;
max_positive_vertical_g = ride.max_positive_vertical_g / 32;
max_negative_vertical_g = ride.max_negative_vertical_g / 32;
max_lateral_g = ride.max_lateral_g / 32;
inversions = ride.holes & 0x1F;
inversions = ride.inversions & 0x1F;
inversions |= (ride.sheltered_eighths << 5);
drops = ride.drops;
highest_drop_height = ride.highest_drop_height;
uint16_t totalAirTime = (ride.total_air_time * 123) / 1024;
if (totalAirTime > 255)
{
totalAirTime = 0;
}
2020-04-17 14:53:33 -03:00
total_air_time = static_cast<uint8_t>(totalAirTime);
2019-06-30 11:23:06 +01:00
excitement = ride.ratings.Excitement / 10;
intensity = ride.ratings.Intensity / 10;
nausea = ride.ratings.Nausea / 10;
2019-06-30 11:23:06 +01:00
upkeep_cost = ride.upkeep_cost;
flags = 0;
flags2 = 0;
if (type == RIDE_TYPE_MAZE)
{
return CreateTrackDesignMaze(ride);
}
else
{
return CreateTrackDesignTrack(ride);
}
}
rct_string_id TrackDesign::CreateTrackDesignTrack(const Ride& ride)
{
CoordsXYE trackElement;
if (!ride_try_get_origin_element(&ride, &trackElement))
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
ride_get_start_of_track(&trackElement);
2019-12-22 22:14:02 +01:00
int32_t z = trackElement.element->GetBaseZ();
auto trackType = trackElement.element->AsTrack()->GetTrackType();
2019-06-30 11:23:06 +01:00
uint8_t direction = trackElement.element->GetDirection();
_saveDirection = direction;
2021-09-01 19:00:18 -07:00
auto newCoords = GetTrackElementOriginAndApplyChanges(
{ trackElement, z, direction }, trackType, 0, &trackElement.element, 0);
2019-06-30 11:23:06 +01:00
2021-09-13 19:47:13 +03:00
if (!newCoords.has_value())
2019-06-30 11:23:06 +01:00
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
trackElement.x = newCoords->x;
trackElement.y = newCoords->y;
z = newCoords->z;
2019-06-30 11:23:06 +01:00
2021-08-27 17:49:32 -04:00
const auto& ted = GetTrackElementDescriptor(trackElement.element->AsTrack()->GetTrackType());
const rct_track_coordinates* trackCoordinates = &ted.Coordinates;
const auto* trackBlock = ted.Block;
2019-06-30 11:23:06 +01:00
// Used in the following loop to know when we have
// completed all of the elements and are back at the
// start.
TileElement* initialMap = trackElement.element;
CoordsXYZ startPos = { trackElement.x, trackElement.y, z + trackCoordinates->z_begin - trackBlock->z };
2020-04-19 21:58:27 +02:00
_trackPreviewOrigin = startPos;
2019-06-30 11:23:06 +01:00
do
{
2019-10-07 21:30:24 -03:00
TrackDesignTrackElement track{};
2019-06-30 11:23:06 +01:00
track.type = trackElement.element->AsTrack()->GetTrackType();
uint8_t trackFlags;
2020-08-10 23:48:18 +02:00
if (TrackTypeHasSpeedSetting(track.type))
2019-06-30 11:23:06 +01:00
{
trackFlags = trackElement.element->AsTrack()->GetBrakeBoosterSpeed() >> 1;
}
else
{
trackFlags = trackElement.element->AsTrack()->GetSeatRotation();
}
if (trackElement.element->AsTrack()->HasChain())
2019-10-19 10:00:42 +02:00
trackFlags |= RCT12_TRACK_ELEMENT_TYPE_FLAG_CHAIN_LIFT;
2019-06-30 11:23:06 +01:00
trackFlags |= trackElement.element->AsTrack()->GetColourScheme() << 4;
if (ride.GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_ALTERNATIVE_TRACK_TYPE)
2019-06-30 11:23:06 +01:00
&& trackElement.element->AsTrack()->IsInverted())
{
2019-08-31 13:14:19 +02:00
trackFlags |= TD6_TRACK_ELEMENT_FLAG_INVERTED;
2019-06-30 11:23:06 +01:00
}
track.flags = trackFlags;
track_elements.push_back(track);
if (!track_block_get_next(&trackElement, &trackElement, nullptr, nullptr))
{
break;
}
2019-12-22 22:14:02 +01:00
z = trackElement.element->GetBaseZ();
2019-06-30 11:23:06 +01:00
direction = trackElement.element->GetDirection();
trackType = trackElement.element->AsTrack()->GetTrackType();
2021-09-01 19:00:18 -07:00
newCoords = GetTrackElementOriginAndApplyChanges(
{ trackElement, z, direction }, trackType, 0, &trackElement.element, 0);
2019-06-30 11:23:06 +01:00
2021-09-13 19:47:13 +03:00
if (!newCoords.has_value())
2019-06-30 11:23:06 +01:00
{
break;
}
trackElement.x = newCoords->x;
trackElement.y = newCoords->y;
z = newCoords->z;
2019-06-30 11:23:06 +01:00
if (track_elements.size() > TD6MaxTrackElements)
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
} while (trackElement.element != initialMap);
// First entrances, second exits
for (int32_t i = 0; i < 2; i++)
{
for (StationIndex station_index = 0; station_index < RCT12_MAX_STATIONS_PER_RIDE; station_index++)
2019-06-30 11:23:06 +01:00
{
z = ride.stations[station_index].GetBaseZ();
2019-06-30 11:23:06 +01:00
TileCoordsXYZD location;
if (i == 0)
{
location = ride_get_entrance_location(&ride, station_index);
}
else
{
location = ride_get_exit_location(&ride, station_index);
}
if (location.IsNull())
2019-06-30 11:23:06 +01:00
{
continue;
}
CoordsXY mapLocation = location.ToCoordsXY();
2019-06-30 11:23:06 +01:00
TileElement* tileElement = map_get_first_element_at(mapLocation);
if (tileElement == nullptr)
continue;
2019-06-30 11:23:06 +01:00
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE)
continue;
if (tileElement->GetBaseZ() == z)
2019-06-30 11:23:06 +01:00
break;
} while (!(tileElement++)->IsLastForTile());
// Add something that stops this from walking off the end
Direction entranceDirection = tileElement->GetDirection();
entranceDirection -= _saveDirection;
entranceDirection &= TILE_ELEMENT_DIRECTION_MASK;
TrackDesignEntranceElement entrance{};
2019-06-30 11:23:06 +01:00
entrance.direction = entranceDirection;
2020-04-19 21:58:27 +02:00
mapLocation -= _trackPreviewOrigin;
2019-06-30 11:23:06 +01:00
// Rotate entrance coordinates backwards to the correct direction
2019-12-04 23:30:36 -03:00
auto rotatedMapLocation = mapLocation.Rotate(0 - _saveDirection);
entrance.x = rotatedMapLocation.x;
entrance.y = rotatedMapLocation.y;
2019-06-30 11:23:06 +01:00
2020-04-19 21:58:27 +02:00
z -= _trackPreviewOrigin.z;
2019-06-30 11:23:06 +01:00
z /= 8;
if (z > 127 || z < -126)
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
entrance.z = z;
// If this is the exit version
if (i == 1)
{
entrance.isExit = true;
2019-06-30 11:23:06 +01:00
}
entrance_elements.push_back(entrance);
}
}
2021-09-09 00:06:34 +03:00
place_virtual_track(this, PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(PreviewRideId), { 4096, 4096, 0 });
2019-06-30 11:23:06 +01:00
// Resave global vars for scenery reasons.
2020-04-19 21:58:27 +02:00
_trackPreviewOrigin = startPos;
2019-06-30 11:23:06 +01:00
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_CONSTRUCT;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
gMapSelectFlags &= ~MAP_SELECT_FLAG_GREEN;
2020-04-19 21:58:27 +02:00
space_required_x = ((_trackPreviewMax.x - _trackPreviewMin.x) / 32) + 1;
space_required_y = ((_trackPreviewMax.y - _trackPreviewMin.y) / 32) + 1;
2019-06-30 11:23:06 +01:00
return STR_NONE;
}
rct_string_id TrackDesign::CreateTrackDesignMaze(const Ride& ride)
{
auto startLoc = MazeGetFirstElement(ride);
if (startLoc.element == nullptr)
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
2020-04-19 21:58:27 +02:00
_trackPreviewOrigin = { startLoc.x, startLoc.y, startLoc.element->GetBaseZ() };
2019-06-30 11:23:06 +01:00
// x is defined here as we can start the search
// on tile start_x, start_y but then the next row
// must restart on 0
2020-08-05 17:17:49 +02:00
for (int32_t y = startLoc.y, x = startLoc.x; y < MAXIMUM_MAP_SIZE_BIG; y += COORDS_XY_STEP)
2019-06-30 11:23:06 +01:00
{
for (; x < MAXIMUM_MAP_SIZE_BIG; x += COORDS_XY_STEP)
2019-06-30 11:23:06 +01:00
{
auto tileElement = map_get_first_element_at({ x, y });
2019-06-30 11:23:06 +01:00
do
{
if (tileElement == nullptr)
break;
if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
continue;
if (tileElement->AsTrack()->GetRideIndex() != ride.id)
continue;
2019-10-07 21:39:18 -03:00
TrackDesignMazeElement maze{};
2019-06-30 11:23:06 +01:00
maze.maze_entry = tileElement->AsTrack()->GetMazeEntry();
2020-03-13 12:03:43 +01:00
maze.x = (x - startLoc.x) / COORDS_XY_STEP;
maze.y = (y - startLoc.y) / COORDS_XY_STEP;
2019-06-30 11:23:06 +01:00
_saveDirection = tileElement->GetDirection();
maze_elements.push_back(maze);
if (maze_elements.size() >= 2000)
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
} while (!(tileElement++)->IsLastForTile());
}
x = 0;
}
auto location = ride_get_entrance_location(&ride, 0);
if (location.IsNull())
2019-06-30 11:23:06 +01:00
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
CoordsXY entranceLoc = location.ToCoordsXY();
auto tileElement = map_get_first_element_at(entranceLoc);
2019-06-30 11:23:06 +01:00
do
{
if (tileElement == nullptr)
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
2019-06-30 11:23:06 +01:00
if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE)
continue;
if (tileElement->AsEntrance()->GetEntranceType() != ENTRANCE_TYPE_RIDE_ENTRANCE)
continue;
if (tileElement->AsEntrance()->GetRideIndex() == ride.id)
break;
} while (!(tileElement++)->IsLastForTile());
// Add something that stops this from walking off the end
uint8_t entranceDirection = tileElement->GetDirection();
2019-10-07 21:39:18 -03:00
TrackDesignMazeElement mazeEntrance{};
2019-06-30 11:23:06 +01:00
mazeEntrance.direction = entranceDirection;
mazeEntrance.type = 8;
2020-04-17 14:53:33 -03:00
mazeEntrance.x = static_cast<int8_t>((entranceLoc.x - startLoc.x) / 32);
mazeEntrance.y = static_cast<int8_t>((entranceLoc.y - startLoc.y) / 32);
2019-06-30 11:23:06 +01:00
maze_elements.push_back(mazeEntrance);
location = ride_get_exit_location(&ride, 0);
if (location.IsNull())
2019-06-30 11:23:06 +01:00
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
CoordsXY exitLoc = location.ToCoordsXY();
tileElement = map_get_first_element_at(exitLoc);
if (tileElement == nullptr)
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
2019-06-30 11:23:06 +01:00
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE)
continue;
if (tileElement->AsEntrance()->GetEntranceType() != ENTRANCE_TYPE_RIDE_EXIT)
continue;
if (tileElement->AsEntrance()->GetRideIndex() == ride.id)
break;
} while (!(tileElement++)->IsLastForTile());
// Add something that stops this from walking off the end
uint8_t exit_direction = tileElement->GetDirection();
2019-10-07 21:39:18 -03:00
TrackDesignMazeElement mazeExit{};
2019-06-30 11:23:06 +01:00
mazeExit.direction = exit_direction;
mazeExit.type = 0x80;
2020-04-17 14:53:33 -03:00
mazeExit.x = static_cast<int8_t>((exitLoc.x - startLoc.x) / 32);
mazeExit.y = static_cast<int8_t>((exitLoc.y - startLoc.y) / 32);
2019-06-30 11:23:06 +01:00
maze_elements.push_back(mazeExit);
// Save global vars as they are still used by scenery????
2020-04-19 21:58:27 +02:00
int32_t startZ = _trackPreviewOrigin.z;
2021-09-09 00:06:34 +03:00
place_virtual_track(this, PTD_OPERATION_DRAW_OUTLINES, true, GetOrAllocateRide(PreviewRideId), { 4096, 4096, 0 });
2020-04-19 21:58:27 +02:00
_trackPreviewOrigin = { startLoc.x, startLoc.y, startZ };
2019-06-30 11:23:06 +01:00
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_CONSTRUCT;
gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE_ARROW;
gMapSelectFlags &= ~MAP_SELECT_FLAG_GREEN;
2020-04-19 21:58:27 +02:00
space_required_x = ((_trackPreviewMax.x - _trackPreviewMin.x) / 32) + 1;
space_required_y = ((_trackPreviewMax.y - _trackPreviewMin.y) / 32) + 1;
2019-06-30 11:23:06 +01:00
return STR_NONE;
}
CoordsXYE TrackDesign::MazeGetFirstElement(const Ride& ride)
{
CoordsXYE tile{};
for (tile.y = 0; tile.y < MAXIMUM_MAP_SIZE_BIG; tile.y += COORDS_XY_STEP)
2019-06-30 11:23:06 +01:00
{
for (tile.x = 0; tile.x < MAXIMUM_MAP_SIZE_BIG; tile.x += COORDS_XY_STEP)
2019-06-30 11:23:06 +01:00
{
tile.element = map_get_first_element_at({ tile.x, tile.y });
2019-06-30 11:23:06 +01:00
do
{
if (tile.element == nullptr)
break;
if (tile.element->GetType() != TILE_ELEMENT_TYPE_TRACK)
continue;
if (tile.element->AsTrack()->GetRideIndex() == ride.id)
{
return tile;
}
} while (!(tile.element++)->IsLastForTile());
}
}
tile.element = nullptr;
return tile;
}
rct_string_id TrackDesign::CreateTrackDesignScenery()
{
scenery_elements = _trackSavedTileElementsDesc;
// Run an element loop
for (auto& scenery : scenery_elements)
{
switch (scenery.scenery_object.GetType())
2019-06-30 11:23:06 +01:00
{
2020-11-09 10:36:37 +01:00
case ObjectType::Paths:
2019-06-30 11:23:06 +01:00
{
uint8_t slope = (scenery.flags & 0x60) >> 5;
slope -= _saveDirection;
scenery.flags &= 0x9F;
scenery.flags |= ((slope & 3) << 5);
// Direction of connection on path
uint8_t direction = scenery.flags & 0xF;
// Rotate the direction by the track direction
direction = ((direction << 4) >> _saveDirection);
scenery.flags &= 0xF0;
scenery.flags |= (direction & 0xF) | (direction >> 4);
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::Walls:
2019-06-30 11:23:06 +01:00
{
uint8_t direction = scenery.flags & 3;
direction -= _saveDirection;
scenery.flags &= 0xFC;
scenery.flags |= (direction & 3);
break;
}
default:
{
uint8_t direction = scenery.flags & 3;
uint8_t quadrant = (scenery.flags & 0x0C) >> 2;
direction -= _saveDirection;
quadrant -= _saveDirection;
scenery.flags &= 0xF0;
scenery.flags |= (direction & 3) | ((quadrant & 3) << 2);
break;
}
}
// Cast the value into a uint8_t as this value is not signed yet.
auto sceneryPos = TileCoordsXY(static_cast<uint8_t>(scenery.x), static_cast<uint8_t>(scenery.y)).ToCoordsXY();
2020-04-19 21:58:27 +02:00
CoordsXY sceneryMapPos = sceneryPos - _trackPreviewOrigin;
2019-12-04 23:30:36 -03:00
CoordsXY rotatedSceneryMapPos = sceneryMapPos.Rotate(0 - _saveDirection);
TileCoordsXY sceneryTilePos{ rotatedSceneryMapPos };
2019-06-30 11:23:06 +01:00
2019-12-04 23:30:36 -03:00
if (sceneryTilePos.x > 127 || sceneryTilePos.y > 127 || sceneryTilePos.x < -126 || sceneryTilePos.y < -126)
2019-06-30 11:23:06 +01:00
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
2019-12-04 23:30:36 -03:00
scenery.x = static_cast<int8_t>(sceneryTilePos.x);
scenery.y = static_cast<int8_t>(sceneryTilePos.y);
2019-06-30 11:23:06 +01:00
2020-04-19 21:58:27 +02:00
int32_t z = scenery.z * COORDS_Z_STEP - _trackPreviewOrigin.z;
2020-03-13 12:03:43 +01:00
z /= COORDS_Z_STEP;
2019-06-30 11:23:06 +01:00
if (z > 127 || z < -126)
{
return STR_TRACK_TOO_LARGE_OR_TOO_MUCH_SCENERY;
}
scenery.z = z;
}
return STR_NONE;
}
void TrackDesign::Serialise(DataSerialiser& stream)
{
2019-12-15 09:18:02 +00:00
if (stream.IsLogging())
{
stream << DS_TAG(name);
// There is too much information logged.
// See sub actions for this information if required.
return;
}
stream << DS_TAG(type);
stream << DS_TAG(vehicle_type);
stream << DS_TAG(cost);
stream << DS_TAG(flags);
stream << DS_TAG(ride_mode);
stream << DS_TAG(track_flags);
stream << DS_TAG(colour_scheme);
stream << DS_TAG(vehicle_colours);
stream << DS_TAG(entrance_style);
stream << DS_TAG(total_air_time);
stream << DS_TAG(depart_flags);
stream << DS_TAG(number_of_trains);
stream << DS_TAG(number_of_cars_per_train);
stream << DS_TAG(min_waiting_time);
stream << DS_TAG(max_waiting_time);
stream << DS_TAG(operation_setting);
stream << DS_TAG(max_speed);
stream << DS_TAG(average_speed);
stream << DS_TAG(ride_length);
stream << DS_TAG(max_positive_vertical_g);
stream << DS_TAG(max_negative_vertical_g);
stream << DS_TAG(max_lateral_g);
stream << DS_TAG(inversions);
stream << DS_TAG(holes);
stream << DS_TAG(drops);
stream << DS_TAG(highest_drop_height);
stream << DS_TAG(excitement);
stream << DS_TAG(intensity);
stream << DS_TAG(nausea);
stream << DS_TAG(upkeep_cost);
stream << DS_TAG(track_spine_colour);
stream << DS_TAG(track_rail_colour);
stream << DS_TAG(track_support_colour);
stream << DS_TAG(flags2);
2021-09-12 21:25:22 +02:00
stream << DS_TAG(vehicle_object.Entry);
2019-12-15 09:18:02 +00:00
stream << DS_TAG(space_required_x);
stream << DS_TAG(space_required_y);
stream << DS_TAG(vehicle_additional_colour);
stream << DS_TAG(lift_hill_speed);
stream << DS_TAG(num_circuits);
2019-12-15 09:18:02 +00:00
stream << DS_TAG(maze_elements);
stream << DS_TAG(track_elements);
stream << DS_TAG(entrance_elements);
stream << DS_TAG(scenery_elements);
stream << DS_TAG(name);
}
2019-04-08 19:34:24 +01:00
std::unique_ptr<TrackDesign> track_design_open(const utf8* path)
2016-04-28 22:57:04 +01:00
{
2018-05-02 13:15:26 +01:00
try
{
auto trackImporter = TrackImporter::Create(path);
trackImporter->Load(path);
2019-06-30 11:23:06 +01:00
return trackImporter->Import();
2017-06-06 23:24:18 +02:00
}
2018-05-02 13:15:26 +01:00
catch (const std::exception& e)
{
log_error("Unable to load track design: %s", e.what());
}
2019-04-08 19:34:24 +01:00
log_verbose("track_design_open(\"%s\")", path);
2017-10-03 00:00:32 +02:00
return nullptr;
2016-04-28 22:57:04 +01:00
}
/**
*
* rct2: 0x006ABDB0
*/
2019-04-08 19:34:24 +01:00
static void track_design_load_scenery_objects(TrackDesign* td6)
{
2021-09-12 21:25:22 +02:00
auto& objectManager = OpenRCT2::GetContext()->GetObjectManager();
objectManager.UnloadAll();
2017-06-06 23:24:18 +02:00
// Load ride object
2021-09-12 21:25:22 +02:00
if (td6->vehicle_object.HasValue())
{
objectManager.LoadObject(td6->vehicle_object);
}
2017-06-06 23:24:18 +02:00
// Load scenery objects
2019-04-08 19:34:24 +01:00
for (const auto& scenery : td6->scenery_elements)
{
2019-04-08 19:34:24 +01:00
const rct_object_entry* sceneryEntry = &scenery.scenery_object;
2017-06-06 23:24:18 +02:00
object_manager_load_object(sceneryEntry);
}
}
/**
*
* rct2: 0x006D247A
*/
2019-04-08 19:34:24 +01:00
static void track_design_mirror_scenery(TrackDesign* td6)
2016-04-30 19:55:13 +01:00
{
for (auto& scenery : td6->scenery_elements)
{
ObjectType entry_type{ 0 };
2020-03-20 19:28:39 +01:00
ObjectEntryIndex entryIndex{ 0 };
if (!find_object_in_entry_group(&scenery.scenery_object, &entry_type, &entryIndex))
{
entry_type = scenery.scenery_object.GetType();
2020-11-09 10:36:37 +01:00
if (entry_type != ObjectType::Paths)
{
2017-06-06 23:24:18 +02:00
continue;
}
2020-03-20 19:28:39 +01:00
entryIndex = 0;
2017-06-06 23:24:18 +02:00
}
switch (entry_type)
2017-06-06 23:24:18 +02:00
{
2020-11-09 10:36:37 +01:00
case ObjectType::LargeScenery:
{
auto* sceneryEntry = static_cast<LargeSceneryEntry*>(object_entry_get_chunk(entry_type, entryIndex));
2018-06-22 23:14:18 +02:00
int16_t x1 = 0, x2 = 0, y1 = 0, y2 = 0;
for (rct_large_scenery_tile* tile = sceneryEntry->tiles; tile->x_offset != -1; tile++)
{
if (x1 > tile->x_offset)
{
x1 = tile->x_offset;
}
if (x2 < tile->x_offset)
{
x2 = tile->x_offset;
}
if (y1 > tile->y_offset)
{
y1 = tile->y_offset;
}
2019-07-31 19:58:21 +01:00
if (y2 < tile->y_offset)
{
y2 = tile->y_offset;
}
}
2019-04-08 19:34:24 +01:00
switch (scenery.flags & 3)
{
2018-06-22 23:14:18 +02:00
case 0:
2019-04-08 19:34:24 +01:00
scenery.y = (-(scenery.y * 32 + y1) - y2) / 32;
2018-06-22 23:14:18 +02:00
break;
case 1:
2019-04-08 19:34:24 +01:00
scenery.x = (scenery.x * 32 + y2 + y1) / 32;
scenery.y = (-(scenery.y * 32)) / 32;
scenery.flags ^= (1 << 1);
break;
case 2:
2019-04-08 19:34:24 +01:00
scenery.y = (-(scenery.y * 32 - y2) + y1) / 32;
break;
case 3:
2019-04-08 19:34:24 +01:00
scenery.x = (scenery.x * 32 - y2 - y1) / 32;
scenery.y = (-(scenery.y * 32)) / 32;
scenery.flags ^= (1 << 1);
2018-06-22 23:14:18 +02:00
break;
}
2017-06-06 23:24:18 +02:00
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::SmallScenery:
{
2021-06-04 02:14:41 +01:00
auto* sceneryEntry = static_cast<SmallSceneryEntry*>(object_entry_get_chunk(entry_type, entryIndex));
2019-04-08 19:34:24 +01:00
scenery.y = -scenery.y;
2021-06-04 02:14:41 +01:00
if (scenery_small_entry_has_flag(sceneryEntry, SMALL_SCENERY_FLAG_DIAGONAL))
{
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 0);
2021-06-04 02:14:41 +01:00
if (!scenery_small_entry_has_flag(sceneryEntry, SMALL_SCENERY_FLAG_FULL_TILE))
2018-06-22 23:14:18 +02:00
{
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 2);
2018-06-22 23:14:18 +02:00
}
break;
}
2019-04-08 19:34:24 +01:00
if (scenery.flags & (1 << 0))
2018-06-22 23:14:18 +02:00
{
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 1);
2018-06-22 23:14:18 +02:00
}
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 2);
2018-06-22 23:14:18 +02:00
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::Walls:
{
2019-04-08 19:34:24 +01:00
scenery.y = -scenery.y;
if (scenery.flags & (1 << 0))
2018-06-22 23:14:18 +02:00
{
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 1);
2017-06-06 23:24:18 +02:00
}
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::Paths:
{
2019-04-08 19:34:24 +01:00
scenery.y = -scenery.y;
2019-04-08 19:34:24 +01:00
if (scenery.flags & (1 << 5))
2018-06-22 23:14:18 +02:00
{
2019-04-08 19:34:24 +01:00
scenery.flags ^= (1 << 6);
2018-06-22 23:14:18 +02:00
}
2019-04-08 19:34:24 +01:00
uint8_t flags = scenery.flags;
2018-06-22 23:14:18 +02:00
flags = ((flags & (1 << 3)) >> 2) | ((flags & (1 << 1)) << 2);
2019-04-08 19:34:24 +01:00
scenery.flags &= 0xF5;
scenery.flags |= flags;
break;
}
2020-11-11 10:51:10 +01:00
default:
// This switch processes only ObjectType for Scenery items.
break;
2017-06-06 23:24:18 +02:00
}
}
}
/**
*
* rct2: 0x006D2443
*/
2019-04-08 19:34:24 +01:00
static void track_design_mirror_ride(TrackDesign* td6)
2016-04-30 19:55:13 +01:00
{
for (auto& track : td6->track_elements)
{
2021-08-27 17:49:32 -04:00
const auto& ted = GetTrackElementDescriptor(track.type);
track.type = ted.MirrorElement;
2017-06-06 23:24:18 +02:00
}
2019-04-08 19:34:24 +01:00
for (auto& entrance : td6->entrance_elements)
{
2019-04-08 19:34:24 +01:00
entrance.y = -entrance.y;
if (entrance.direction & 1)
{
2019-04-08 19:34:24 +01:00
entrance.direction = direction_reverse(entrance.direction);
2017-06-06 23:24:18 +02:00
}
}
}
2016-08-06 16:05:37 +02:00
/** rct2: 0x00993EDC */
static constexpr const uint8_t maze_segment_mirror_map[] = {
5, 4, 2, 7, 1, 0, 14, 3, 13, 12, 10, 15, 9, 8, 6, 11,
};
2016-08-06 16:05:37 +02:00
/**
*
* rct2: 0x006D25FA
*/
2019-04-08 19:34:24 +01:00
static void track_design_mirror_maze(TrackDesign* td6)
2016-04-30 19:55:13 +01:00
{
for (auto& maze : td6->maze_elements)
{
2019-04-08 19:34:24 +01:00
maze.y = -maze.y;
2019-04-08 19:34:24 +01:00
if (maze.type == 0x8 || maze.type == 0x80)
{
2019-04-08 19:34:24 +01:00
if (maze.direction & 1)
{
2019-04-08 19:34:24 +01:00
maze.direction = direction_reverse(maze.direction);
2017-06-06 23:24:18 +02:00
}
continue;
}
2019-04-08 19:34:24 +01:00
uint16_t maze_entry = maze.maze_entry;
2018-06-22 23:14:18 +02:00
uint16_t new_entry = 0;
for (uint8_t position = bitscanforward(maze_entry); position != 0xFF; position = bitscanforward(maze_entry))
{
2017-06-06 23:24:18 +02:00
maze_entry &= ~(1 << position);
new_entry |= (1 << maze_segment_mirror_map[position]);
}
2019-04-08 19:34:24 +01:00
maze.maze_entry = new_entry;
2017-06-06 23:24:18 +02:00
}
}
/**
*
* rct2: 0x006D2436
*/
2019-04-08 19:34:24 +01:00
void track_design_mirror(TrackDesign* td6)
{
if (td6->type == RIDE_TYPE_MAZE)
{
2017-06-06 23:24:18 +02:00
track_design_mirror_maze(td6);
}
else
{
2017-06-06 23:24:18 +02:00
track_design_mirror_ride(td6);
}
track_design_mirror_scenery(td6);
}
static void track_design_add_selection_tile(const CoordsXY& coords)
{
auto tileIterator = std::find(gMapSelectionTiles.begin(), gMapSelectionTiles.end(), coords);
if (tileIterator == gMapSelectionTiles.end())
{
gMapSelectionTiles.push_back(coords);
2017-06-06 23:24:18 +02:00
}
}
static void track_design_update_max_min_coordinates(const CoordsXYZ& coords)
2016-04-30 22:29:13 +01:00
{
_trackPreviewMin = { std::min(_trackPreviewMin.x, coords.x), std::min(_trackPreviewMin.y, coords.y),
std::min(_trackPreviewMin.z, coords.z) };
_trackPreviewMax = { std::max(_trackPreviewMax.x, coords.x), std::max(_trackPreviewMax.y, coords.y),
std::max(_trackPreviewMax.z, coords.z) };
2016-04-30 22:29:13 +01:00
}
2019-04-08 19:34:24 +01:00
static bool TrackDesignPlaceSceneryElementGetEntry(
ObjectType& entry_type, ObjectEntryIndex& entry_index, const TrackDesignSceneryElement& scenery)
2019-03-28 18:29:51 +00:00
{
2019-04-08 19:34:24 +01:00
if (!find_object_in_entry_group(&scenery.scenery_object, &entry_type, &entry_index))
2019-03-28 18:29:51 +00:00
{
entry_type = scenery.scenery_object.GetType();
2020-11-09 10:36:37 +01:00
if (entry_type != ObjectType::Paths)
2019-03-28 18:29:51 +00:00
{
_trackDesignPlaceStateSceneryUnavailable = true;
return true;
}
if (gScreenFlags & SCREEN_FLAGS_TRACK_DESIGNER)
{
_trackDesignPlaceStateSceneryUnavailable = true;
return true;
}
entry_index = 0;
for (; entry_index < object_entry_group_counts[EnumValue(ObjectType::Paths)]; entry_index++)
2019-03-28 18:29:51 +00:00
{
const auto* legacyPathEntry = GetLegacyFootpathEntry(entry_index);
if (legacyPathEntry == nullptr)
2019-03-28 18:29:51 +00:00
{
return true;
}
const auto& surfaceDescriptor = legacyPathEntry->GetPathSurfaceDescriptor();
if (surfaceDescriptor.IsEditorOnly())
2019-03-28 18:29:51 +00:00
{
return true;
}
}
if (entry_index == object_entry_group_counts[EnumValue(ObjectType::Paths)])
2019-03-28 18:29:51 +00:00
{
_trackDesignPlaceStateSceneryUnavailable = true;
return true;
}
}
return false;
}
static bool TrackDesignPlaceSceneryElementRemoveGhost(
CoordsXY mapCoord, const TrackDesignSceneryElement& scenery, uint8_t rotation, int32_t originZ)
2019-03-27 20:01:57 +00:00
{
ObjectType entry_type;
2020-03-20 19:28:39 +01:00
ObjectEntryIndex entry_index;
2019-03-28 18:29:51 +00:00
if (TrackDesignPlaceSceneryElementGetEntry(entry_type, entry_index, scenery))
2019-03-27 20:01:57 +00:00
{
2019-03-28 18:02:02 +00:00
return true;
2019-03-27 20:01:57 +00:00
}
2019-03-28 18:02:02 +00:00
if (_trackDesignPlaceStateSceneryUnavailable)
{
return true;
}
2020-03-07 21:07:18 +01:00
int32_t z = (scenery.z * COORDS_Z_STEP) + originZ;
2019-04-08 19:34:24 +01:00
uint8_t sceneryRotation = (rotation + scenery.flags) & TILE_ELEMENT_DIRECTION_MASK;
2019-04-01 18:58:16 +01:00
const uint32_t flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2019-03-27 20:01:57 +00:00
| GAME_COMMAND_FLAG_GHOST;
std::unique_ptr<GameAction> ga;
2019-03-27 20:01:57 +00:00
switch (entry_type)
{
2020-11-09 10:36:37 +01:00
case ObjectType::SmallScenery:
2019-03-27 20:01:57 +00:00
{
2019-04-08 19:34:24 +01:00
uint8_t quadrant = (scenery.flags >> 2) + _currentTrackPieceDirection;
2019-03-27 20:01:57 +00:00
quadrant &= 3;
2021-06-04 02:14:41 +01:00
auto* sceneryEntry = get_small_scenery_entry(entry_index);
if (!(!scenery_small_entry_has_flag(sceneryEntry, SMALL_SCENERY_FLAG_FULL_TILE)
&& scenery_small_entry_has_flag(sceneryEntry, SMALL_SCENERY_FLAG_DIAGONAL))
2019-03-27 20:01:57 +00:00
&& scenery_small_entry_has_flag(
2021-06-04 02:14:41 +01:00
sceneryEntry,
2019-11-04 14:02:30 +01:00
SMALL_SCENERY_FLAG_DIAGONAL | SMALL_SCENERY_FLAG_HALF_SPACE | SMALL_SCENERY_FLAG_THREE_QUARTERS))
2019-03-27 20:01:57 +00:00
{
quadrant = 0;
}
2020-03-07 21:07:18 +01:00
ga = std::make_unique<SmallSceneryRemoveAction>(CoordsXYZ{ mapCoord.x, mapCoord.y, z }, quadrant, entry_index);
2019-03-27 20:01:57 +00:00
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::LargeScenery:
2020-03-07 21:07:18 +01:00
ga = std::make_unique<LargeSceneryRemoveAction>(CoordsXYZD{ mapCoord.x, mapCoord.y, z, sceneryRotation }, 0);
2019-03-27 20:01:57 +00:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Walls:
2020-03-07 21:07:18 +01:00
ga = std::make_unique<WallRemoveAction>(CoordsXYZD{ mapCoord.x, mapCoord.y, z, sceneryRotation });
2019-03-27 20:01:57 +00:00
break;
2020-11-09 10:36:37 +01:00
case ObjectType::Paths:
2020-03-07 21:07:18 +01:00
ga = std::make_unique<FootpathRemoveAction>(CoordsXYZ{ mapCoord.x, mapCoord.y, z });
2019-03-27 20:01:57 +00:00
break;
default:
return true;
2019-03-27 20:01:57 +00:00
}
ga->SetFlags(flags);
GameActions::ExecuteNested(ga.get());
2019-03-27 20:01:57 +00:00
return true;
}
static bool TrackDesignPlaceSceneryElementGetPlaceZ(const TrackDesignSceneryElement& scenery)
2019-03-27 20:01:57 +00:00
{
2020-03-13 12:03:43 +01:00
int32_t z = scenery.z * COORDS_Z_STEP + _trackDesignPlaceZ;
2019-03-27 20:01:57 +00:00
if (z < _trackDesignPlaceSceneryZ)
{
_trackDesignPlaceSceneryZ = z;
}
ObjectType entry_type;
2020-03-20 19:28:39 +01:00
ObjectEntryIndex entry_index;
2019-03-28 18:29:51 +00:00
TrackDesignPlaceSceneryElementGetEntry(entry_type, entry_index, scenery);
2019-03-27 20:01:57 +00:00
return true;
}
2021-08-08 12:27:06 +02:00
static std::optional<money32> TrackDesignPlaceSceneryElement(
2021-08-15 13:39:04 +02:00
CoordsXY mapCoord, uint8_t mode, const TrackDesignSceneryElement& scenery, uint8_t rotation, int32_t originZ)
2019-03-27 20:01:57 +00:00
{
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES && mode == 0)
{
track_design_add_selection_tile(mapCoord);
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST && mode == 0)
{
2021-08-08 12:27:06 +02:00
if (TrackDesignPlaceSceneryElementRemoveGhost(mapCoord, scenery, rotation, originZ))
2021-08-15 13:39:04 +02:00
return 0;
2021-08-08 12:27:06 +02:00
return std::nullopt;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
2021-08-08 12:27:06 +02:00
if (TrackDesignPlaceSceneryElementGetPlaceZ(scenery))
2021-08-15 13:39:04 +02:00
return 0;
2021-08-08 12:27:06 +02:00
return std::nullopt;
2019-03-27 20:01:57 +00:00
}
2021-08-15 13:39:04 +02:00
money32 totalCost = 0;
2019-03-27 20:01:57 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY || _trackDesignPlaceOperation == PTD_OPERATION_PLACE
|| _trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST
|| _trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
ObjectType entry_type;
2020-03-20 19:28:39 +01:00
ObjectEntryIndex entry_index;
2019-03-28 18:29:51 +00:00
if (TrackDesignPlaceSceneryElementGetEntry(entry_type, entry_index, scenery))
2019-03-27 20:01:57 +00:00
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
money32 cost;
int16_t z;
uint8_t flags;
uint8_t quadrant;
switch (entry_type)
{
2020-11-09 10:36:37 +01:00
case ObjectType::SmallScenery:
2019-03-27 20:01:57 +00:00
{
if (mode != 0)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
2019-04-08 19:34:24 +01:00
rotation += scenery.flags;
2019-03-27 20:01:57 +00:00
rotation &= 3;
2020-03-13 12:03:43 +01:00
z = scenery.z * COORDS_Z_STEP + originZ;
2019-04-08 19:34:24 +01:00
quadrant = ((scenery.flags >> 2) + _currentTrackPieceDirection) & 3;
2019-03-27 20:01:57 +00:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY;
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
2019-04-01 18:58:16 +01:00
| GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
{
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
2019-04-01 18:58:16 +01:00
| GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{
flags = GAME_COMMAND_FLAG_PATH_SCENERY;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2019-03-27 20:01:57 +00:00
gGameCommandErrorTitle = STR_CANT_POSITION_THIS_HERE;
auto smallSceneryPlace = SmallSceneryPlaceAction(
2019-04-08 19:34:24 +01:00
{ mapCoord.x, mapCoord.y, z, rotation }, quadrant, entry_index, scenery.primary_colour,
scenery.secondary_colour);
2019-03-27 20:01:57 +00:00
smallSceneryPlace.SetFlags(flags);
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&smallSceneryPlace)
: GameActions::QueryNested(&smallSceneryPlace);
cost = res->Error == GameActions::Status::Ok ? res->Cost : 0;
2019-03-27 20:01:57 +00:00
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::LargeScenery:
2019-04-06 08:01:22 +01:00
{
2019-03-27 20:01:57 +00:00
if (mode != 0)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
2019-04-08 19:34:24 +01:00
rotation += scenery.flags;
2019-03-27 20:01:57 +00:00
rotation &= 3;
2020-03-07 21:07:18 +01:00
z = scenery.z * COORDS_Z_STEP + originZ;
2019-03-27 20:01:57 +00:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY;
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
2019-04-01 18:58:16 +01:00
| GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
{
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
2019-04-01 18:58:16 +01:00
| GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{
flags = GAME_COMMAND_FLAG_PATH_SCENERY;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2019-04-05 19:16:23 +01:00
auto sceneryPlaceAction = LargeSceneryPlaceAction(
2019-04-08 19:34:24 +01:00
{ mapCoord.x, mapCoord.y, z, rotation }, entry_index, scenery.primary_colour, scenery.secondary_colour);
2019-04-05 19:16:23 +01:00
sceneryPlaceAction.SetFlags(flags);
2020-04-05 08:28:07 +01:00
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&sceneryPlaceAction)
: GameActions::QueryNested(&sceneryPlaceAction);
2019-03-27 20:01:57 +00:00
2019-04-05 19:16:23 +01:00
cost = res->Cost;
2019-03-27 20:01:57 +00:00
break;
2019-04-06 08:01:22 +01:00
}
2020-11-09 10:36:37 +01:00
case ObjectType::Walls:
2019-03-27 20:01:57 +00:00
{
if (mode != 0)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
2020-03-07 21:07:18 +01:00
z = scenery.z * COORDS_Z_STEP + originZ;
2019-04-08 19:34:24 +01:00
rotation += scenery.flags;
2019-03-27 20:01:57 +00:00
rotation &= 3;
flags = GAME_COMMAND_FLAG_APPLY;
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_PATH_SCENERY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
2019-04-01 18:58:16 +01:00
| GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2019-03-27 20:01:57 +00:00
| GAME_COMMAND_FLAG_GHOST;
}
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{
flags = 0;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2019-03-27 20:01:57 +00:00
auto wallPlaceAction = WallPlaceAction(
2019-04-08 19:34:24 +01:00
entry_index, { mapCoord.x, mapCoord.y, z }, rotation, scenery.primary_colour, scenery.secondary_colour,
(scenery.flags & 0xFC) >> 2);
wallPlaceAction.SetFlags(flags);
2019-04-06 18:06:40 +01:00
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&wallPlaceAction)
: GameActions::QueryNested(&wallPlaceAction);
2019-03-27 20:01:57 +00:00
cost = res->Cost;
break;
}
2020-11-09 10:36:37 +01:00
case ObjectType::Paths:
2019-03-27 20:01:57 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
2020-03-07 21:07:18 +01:00
z = (scenery.z * COORDS_Z_STEP + originZ) / COORDS_Z_STEP;
2019-03-27 20:01:57 +00:00
if (mode == 0)
{
auto isQueue = scenery.IsQueue();
2019-03-27 20:01:57 +00:00
2019-04-08 19:34:24 +01:00
uint8_t bh = ((scenery.flags & 0xF) << rotation);
2019-03-27 20:01:57 +00:00
flags = bh >> 4;
bh = (bh | flags) & 0xF;
2019-04-08 19:34:24 +01:00
flags = (((scenery.flags >> 5) + rotation) & 3) << 5;
2019-03-27 20:01:57 +00:00
bh |= flags;
2019-04-08 19:34:24 +01:00
bh |= scenery.flags & 0x90;
2019-03-27 20:01:57 +00:00
flags = GAME_COMMAND_FLAG_APPLY;
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2019-03-27 20:01:57 +00:00
| GAME_COMMAND_FLAG_GHOST;
}
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{
flags = 0;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2019-03-27 20:01:57 +00:00
uint8_t slope = ((bh >> 5) & 0x3) | ((bh >> 2) & 0x4);
uint8_t edges = bh & 0xF;
PathConstructFlags constructFlags = PathConstructFlag::IsLegacyPathObject;
if (isQueue)
constructFlags |= PathConstructFlag::IsQueue;
2019-03-27 20:01:57 +00:00
auto footpathPlaceAction = FootpathPlaceFromTrackAction(
{ mapCoord.x, mapCoord.y, z * COORDS_Z_STEP }, slope, entry_index, OBJECT_ENTRY_INDEX_NULL, edges,
constructFlags);
2019-03-27 20:01:57 +00:00
footpathPlaceAction.SetFlags(flags);
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&footpathPlaceAction)
: GameActions::QueryNested(&footpathPlaceAction);
// Ignore failures
cost = res->Error == GameActions::Status::Ok ? res->Cost : 0;
2019-03-27 20:01:57 +00:00
}
else
{
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
auto* pathElement = map_get_path_element_at({ mapCoord.x / 32, mapCoord.y / 32, z });
2019-03-27 20:01:57 +00:00
if (pathElement == nullptr)
2019-03-27 20:01:57 +00:00
{
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
footpath_queue_chain_reset();
2020-01-04 08:33:19 -03:00
footpath_remove_edges_at(mapCoord, reinterpret_cast<TileElement*>(pathElement));
2019-03-27 20:01:57 +00:00
flags = GAME_COMMAND_FLAG_APPLY;
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2019-03-27 20:01:57 +00:00
| GAME_COMMAND_FLAG_GHOST;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2020-01-04 07:31:02 -03:00
footpath_connect_edges(mapCoord, reinterpret_cast<TileElement*>(pathElement), flags);
2019-03-27 20:01:57 +00:00
footpath_update_queue_chains();
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
break;
default:
_trackDesignPlaceStateSceneryUnavailable = true;
2021-08-15 13:39:04 +02:00
return 0;
2019-03-27 20:01:57 +00:00
}
2021-07-31 22:18:15 +02:00
totalCost = add_clamp_money32(totalCost, cost);
2019-03-27 20:01:57 +00:00
if (_trackDesignPlaceOperation != PTD_OPERATION_PLACE)
{
if (cost == MONEY32_UNDEFINED)
{
2021-07-31 22:18:15 +02:00
totalCost = MONEY32_UNDEFINED;
2019-03-27 20:01:57 +00:00
}
}
2021-07-31 22:18:15 +02:00
if (totalCost != MONEY32_UNDEFINED)
2019-03-27 20:01:57 +00:00
{
2021-08-08 12:27:06 +02:00
return totalCost;
2019-03-27 20:01:57 +00:00
}
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE)
{
2021-08-08 12:27:06 +02:00
return totalCost;
2019-03-27 20:01:57 +00:00
}
2021-08-08 12:27:06 +02:00
return std::nullopt;
2019-03-27 20:01:57 +00:00
}
2021-08-08 12:27:06 +02:00
return totalCost;
2019-03-27 20:01:57 +00:00
}
/**
*
* rct2: 0x006D0964
*/
2021-08-08 12:27:06 +02:00
static std::optional<money32> track_design_place_all_scenery(
2021-08-15 13:39:04 +02:00
const std::vector<TrackDesignSceneryElement>& sceneryList, const CoordsXYZ& origin)
2016-04-30 19:55:13 +01:00
{
2021-08-15 13:39:04 +02:00
money32 cost = 0;
for (uint8_t mode = 0; mode <= 1; mode++)
{
2019-04-08 19:34:24 +01:00
if (!sceneryList.empty())
{
_trackDesignPlaceStateHasScenery = true;
2017-06-06 23:24:18 +02:00
}
if (!_trackDesignPlaceStatePlaceScenery)
{
2017-06-06 23:24:18 +02:00
continue;
}
2019-04-08 19:34:24 +01:00
for (const auto& scenery : sceneryList)
{
2018-06-22 23:14:18 +02:00
uint8_t rotation = _currentTrackPieceDirection;
TileCoordsXY tileCoords = TileCoordsXY(origin);
2019-04-08 19:34:24 +01:00
TileCoordsXY offsets = { scenery.x, scenery.y };
tileCoords += offsets.Rotate(rotation);
auto mapCoord = CoordsXYZ{ tileCoords.ToCoordsXY(), origin.z };
track_design_update_max_min_coordinates(mapCoord);
2021-08-15 13:39:04 +02:00
auto placementCost = TrackDesignPlaceSceneryElement(mapCoord, mode, scenery, rotation, origin.z);
2021-08-08 12:27:06 +02:00
if (!placementCost.has_value() || placementCost == MONEY32_UNDEFINED)
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2017-06-06 23:24:18 +02:00
}
2021-08-15 13:39:04 +02:00
cost += placementCost.value();
2017-06-06 23:24:18 +02:00
}
}
2021-08-15 13:39:04 +02:00
2021-08-08 12:27:06 +02:00
return cost;
}
2021-08-08 12:27:06 +02:00
static std::optional<money32> track_design_place_maze(TrackDesign* td6, const CoordsXYZ& coords, Ride* ride)
{
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
{
2019-03-28 18:29:51 +00:00
gMapSelectionTiles.clear();
gMapSelectArrowPosition = CoordsXYZ{ coords, tile_element_height(coords) };
2017-06-06 23:24:18 +02:00
gMapSelectArrowDirection = _currentTrackPieceDirection;
}
2018-06-22 23:14:18 +02:00
_trackDesignPlaceZ = 0;
2021-08-08 12:27:06 +02:00
money32 totalCost = 0;
2019-04-08 19:34:24 +01:00
for (const auto& maze_element : td6->maze_elements)
{
2018-06-22 23:14:18 +02:00
uint8_t rotation = _currentTrackPieceDirection & 3;
2020-03-13 12:03:43 +01:00
CoordsXY mazeMapPos = TileCoordsXY(maze_element.x, maze_element.y).ToCoordsXY();
2019-12-04 23:30:36 -03:00
auto mapCoord = mazeMapPos.Rotate(rotation);
mapCoord += coords;
track_design_update_max_min_coordinates({ mapCoord, coords.z });
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
{
track_design_add_selection_tile(mapCoord);
2017-06-06 23:24:18 +02:00
}
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY || _trackDesignPlaceOperation == PTD_OPERATION_PLACE
2019-03-27 20:01:57 +00:00
|| _trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST
|| _trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
2018-06-22 23:14:18 +02:00
uint8_t flags;
2017-06-06 23:24:18 +02:00
money32 cost = 0;
2018-06-22 23:14:18 +02:00
uint16_t maze_entry;
2019-04-08 19:34:24 +01:00
switch (maze_element.type)
{
2018-06-22 23:14:18 +02:00
case MAZE_ELEMENT_TYPE_ENTRANCE:
// entrance
2019-04-08 19:34:24 +01:00
rotation += maze_element.direction;
2018-06-22 23:14:18 +02:00
rotation &= 3;
flags = GAME_COMMAND_FLAG_APPLY;
2018-06-22 23:14:18 +02:00
gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
auto res = RideEntranceExitPlaceAction::TrackPlaceQuery({ mapCoord, coords.z }, false);
cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2018-06-22 23:14:18 +02:00
}
else
{
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
2018-06-22 23:14:18 +02:00
{
2019-04-02 18:17:37 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
| GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
2018-06-22 23:14:18 +02:00
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2018-06-22 23:14:18 +02:00
| GAME_COMMAND_FLAG_GHOST;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(mapCoord, rotation, ride->id, 0, false);
rideEntranceExitPlaceAction.SetFlags(flags);
auto res = GameActions::ExecuteNested(&rideEntranceExitPlaceAction);
cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2018-06-22 23:14:18 +02:00
}
if (cost != MONEY32_UNDEFINED)
{
_trackDesignPlaceStateEntranceExitPlaced = true;
}
break;
case MAZE_ELEMENT_TYPE_EXIT:
// exit
2019-04-08 19:34:24 +01:00
rotation += maze_element.direction;
2018-06-22 23:14:18 +02:00
rotation &= 3;
2018-06-22 23:14:18 +02:00
flags = GAME_COMMAND_FLAG_APPLY;
gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
auto res = RideEntranceExitPlaceAction::TrackPlaceQuery({ mapCoord, coords.z }, true);
cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2018-06-22 23:14:18 +02:00
}
else
{
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
2018-06-22 23:14:18 +02:00
{
2019-04-02 18:17:37 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
| GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
2018-06-22 23:14:18 +02:00
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2018-06-22 23:14:18 +02:00
| GAME_COMMAND_FLAG_GHOST;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(mapCoord, rotation, ride->id, 0, true);
rideEntranceExitPlaceAction.SetFlags(flags);
auto res = GameActions::ExecuteNested(&rideEntranceExitPlaceAction);
cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2018-06-22 23:14:18 +02:00
}
if (cost != MONEY32_UNDEFINED)
{
_trackDesignPlaceStateEntranceExitPlaced = true;
}
break;
default:
maze_entry = Numerics::rol16(maze_element.maze_entry, rotation * 4);
2018-06-22 23:14:18 +02:00
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
2018-06-22 23:14:18 +02:00
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
2018-06-22 23:14:18 +02:00
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
2018-06-22 23:14:18 +02:00
| GAME_COMMAND_FLAG_GHOST;
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
flags = 0;
}
else
{
flags = GAME_COMMAND_FLAG_APPLY;
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2018-06-22 23:14:18 +02:00
gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
auto mazePlace = MazePlaceTrackAction({ mapCoord, coords.z }, ride->id, maze_entry);
2019-10-20 15:21:49 +01:00
mazePlace.SetFlags(flags);
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&mazePlace)
: GameActions::QueryNested(&mazePlace);
cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2018-06-22 23:14:18 +02:00
break;
2017-06-06 23:24:18 +02:00
}
2021-07-31 22:18:15 +02:00
totalCost += cost;
if (cost == MONEY32_UNDEFINED)
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2017-06-06 23:24:18 +02:00
}
}
if (_trackDesignPlaceOperation == PTD_OPERATION_GET_PLACE_Z)
{
if (!map_is_location_valid(mapCoord))
{
continue;
}
auto surfaceElement = map_get_surface_element_at(mapCoord);
if (surfaceElement == nullptr)
continue;
int16_t surfaceZ = surfaceElement->GetBaseZ();
if (surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_ALL_CORNERS_UP)
{
2020-03-07 21:07:18 +01:00
surfaceZ += LAND_HEIGHT_STEP;
if (surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_DOUBLE_HEIGHT)
{
2020-03-07 21:07:18 +01:00
surfaceZ += LAND_HEIGHT_STEP;
2017-06-06 23:24:18 +02:00
}
}
2020-01-19 16:12:48 +01:00
int16_t waterZ = surfaceElement->GetWaterHeight();
if (waterZ > 0 && waterZ > surfaceZ)
{
surfaceZ = waterZ;
2017-06-06 23:24:18 +02:00
}
int16_t temp_z = coords.z + _trackDesignPlaceZ - surfaceZ;
if (temp_z < 0)
{
2017-06-06 23:24:18 +02:00
_trackDesignPlaceZ -= temp_z;
}
}
}
2019-03-27 19:33:27 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST)
{
2018-06-22 23:14:18 +02:00
ride_action_modify(
2019-01-20 14:54:59 +00:00
ride, RIDE_MODIFY_DEMOLISH,
2019-04-02 18:17:37 +01:00
GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
| GAME_COMMAND_FLAG_GHOST);
2017-06-06 23:24:18 +02:00
}
_trackPreviewOrigin = coords;
2021-08-08 12:27:06 +02:00
return totalCost;
}
2021-08-08 12:27:06 +02:00
static std::optional<money32> track_design_place_ride(TrackDesign* td6, const CoordsXYZ& origin, Ride* ride)
{
_trackPreviewOrigin = origin;
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
{
2019-03-28 18:29:51 +00:00
gMapSelectionTiles.clear();
gMapSelectArrowPosition = CoordsXYZ{ origin, tile_element_height(origin) };
2017-06-06 23:24:18 +02:00
gMapSelectArrowDirection = _currentTrackPieceDirection;
}
2018-06-22 23:14:18 +02:00
_trackDesignPlaceZ = 0;
2021-08-08 12:27:06 +02:00
money32 totalCost = 0;
uint8_t rotation = _currentTrackPieceDirection;
2017-06-06 23:24:18 +02:00
// Track elements
auto newCoords = origin;
2019-04-08 19:34:24 +01:00
for (const auto& track : td6->track_elements)
{
2021-01-22 11:33:55 +01:00
auto trackType = track.type;
2021-08-27 17:49:32 -04:00
const auto& ted = GetTrackElementDescriptor(trackType);
track_design_update_max_min_coordinates(newCoords);
switch (_trackDesignPlaceOperation)
{
2018-06-22 23:14:18 +02:00
case PTD_OPERATION_DRAW_OUTLINES:
2021-08-27 17:49:32 -04:00
for (const rct_preview_track* trackBlock = ted.Block; trackBlock->index != 0xFF; trackBlock++)
{
auto tile = CoordsXY{ newCoords } + CoordsXY{ trackBlock->x, trackBlock->y }.Rotate(rotation);
track_design_update_max_min_coordinates({ tile, newCoords.z });
track_design_add_selection_tile(tile);
2018-06-22 23:14:18 +02:00
}
break;
2019-03-27 19:33:27 +00:00
case PTD_OPERATION_REMOVE_GHOST:
2018-06-22 23:14:18 +02:00
{
2021-08-27 17:49:32 -04:00
const rct_track_coordinates* trackCoordinates = &ted.Coordinates;
const rct_preview_track* trackBlock = ted.Block;
int32_t tempZ = newCoords.z - trackCoordinates->z_begin + trackBlock->z;
auto trackRemoveAction = TrackRemoveAction(
trackType, 0, { newCoords, tempZ, static_cast<Direction>(rotation & 3) });
2019-02-20 10:49:25 +00:00
trackRemoveAction.SetFlags(
2019-04-01 18:58:16 +01:00
GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND | GAME_COMMAND_FLAG_GHOST);
2019-02-20 10:49:25 +00:00
GameActions::ExecuteNested(&trackRemoveAction);
2018-06-22 23:14:18 +02:00
break;
}
2019-03-27 19:21:13 +00:00
case PTD_OPERATION_PLACE_QUERY:
case PTD_OPERATION_PLACE:
case PTD_OPERATION_PLACE_GHOST:
case PTD_OPERATION_PLACE_TRACK_PREVIEW:
2018-06-22 23:14:18 +02:00
{
2021-08-27 17:49:32 -04:00
const rct_track_coordinates* trackCoordinates = &ted.Coordinates;
2018-06-22 23:14:18 +02:00
// di
int16_t tempZ = newCoords.z - trackCoordinates->z_begin;
2019-04-08 19:34:24 +01:00
uint32_t trackColour = (track.flags >> 4) & 0x3;
uint32_t brakeSpeed = (track.flags & 0x0F) * 2;
uint32_t seatRotation = track.flags & 0x0F;
2018-06-22 23:14:18 +02:00
2019-02-15 14:10:48 +00:00
int32_t liftHillAndAlternativeState = 0;
2019-08-31 13:14:19 +02:00
if (track.flags & RCT12_TRACK_ELEMENT_TYPE_FLAG_CHAIN_LIFT)
2018-06-22 23:14:18 +02:00
{
2019-02-15 14:10:48 +00:00
liftHillAndAlternativeState |= 1;
2018-06-22 23:14:18 +02:00
}
2019-08-31 13:14:19 +02:00
if (track.flags & TD6_TRACK_ELEMENT_FLAG_INVERTED)
2018-06-22 23:14:18 +02:00
{
2019-02-15 14:10:48 +00:00
liftHillAndAlternativeState |= 2;
2017-06-06 23:24:18 +02:00
}
2018-06-22 23:14:18 +02:00
uint8_t flags = GAME_COMMAND_FLAG_APPLY;
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
{
2018-06-22 23:14:18 +02:00
flags |= GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED;
2019-04-01 18:58:16 +01:00
flags |= GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
2018-06-22 23:14:18 +02:00
{
flags |= GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED;
2019-04-01 18:58:16 +01:00
flags |= GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
flags |= GAME_COMMAND_FLAG_GHOST;
}
2019-03-27 19:21:13 +00:00
else if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
flags = GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2018-06-22 23:14:18 +02:00
gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
2019-02-15 14:10:48 +00:00
auto trackPlaceAction = TrackPlaceAction(
_currentRideIndex, trackType, { newCoords, tempZ, static_cast<uint8_t>(rotation) }, brakeSpeed, trackColour,
seatRotation, liftHillAndAlternativeState, true);
2019-02-15 14:10:48 +00:00
trackPlaceAction.SetFlags(flags);
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&trackPlaceAction)
: GameActions::QueryNested(&trackPlaceAction);
money32 cost = res->Error == GameActions::Status::Ok ? res->Cost : MONEY32_UNDEFINED;
2019-02-15 14:10:48 +00:00
2021-07-31 22:18:15 +02:00
totalCost += cost;
2018-06-22 23:14:18 +02:00
if (cost == MONEY32_UNDEFINED)
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2017-06-06 23:24:18 +02:00
}
2018-06-22 23:14:18 +02:00
break;
}
case PTD_OPERATION_GET_PLACE_Z:
{
2021-08-27 17:49:32 -04:00
int32_t tempZ = newCoords.z - ted.Coordinates.z_begin;
for (const rct_preview_track* trackBlock = ted.Block; trackBlock->index != 0xFF; trackBlock++)
{
auto tile = CoordsXY{ newCoords } + CoordsXY{ trackBlock->x, trackBlock->y }.Rotate(rotation);
if (!map_is_location_valid(tile))
2018-06-22 23:14:18 +02:00
{
continue;
}
auto surfaceElement = map_get_surface_element_at(tile);
if (surfaceElement == nullptr)
2018-06-22 23:14:18 +02:00
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2018-06-22 23:14:18 +02:00
}
int32_t surfaceZ = surfaceElement->GetBaseZ();
if (surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_ALL_CORNERS_UP)
{
2020-03-07 21:07:18 +01:00
surfaceZ += LAND_HEIGHT_STEP;
if (surfaceElement->GetSlope() & TILE_ELEMENT_SLOPE_DOUBLE_HEIGHT)
2018-06-22 23:14:18 +02:00
{
2020-03-07 21:07:18 +01:00
surfaceZ += LAND_HEIGHT_STEP;
2018-06-22 23:14:18 +02:00
}
}
2020-03-31 20:56:47 +02:00
auto waterZ = surfaceElement->GetWaterHeight();
if (waterZ > 0 && waterZ > surfaceZ)
2018-06-22 23:14:18 +02:00
{
surfaceZ = waterZ;
2018-06-22 23:14:18 +02:00
}
int32_t heightDifference = tempZ + _trackDesignPlaceZ + trackBlock->z - surfaceZ;
2018-06-22 23:14:18 +02:00
if (heightDifference < 0)
{
_trackDesignPlaceZ -= heightDifference;
2017-06-06 23:24:18 +02:00
}
}
2018-06-22 23:14:18 +02:00
break;
2017-06-06 23:24:18 +02:00
}
}
2021-08-27 17:49:32 -04:00
const rct_track_coordinates& track_coordinates = ted.Coordinates;
auto offsetAndRotatedTrack = CoordsXY{ newCoords }
2021-08-27 17:44:33 -04:00
+ CoordsXY{ track_coordinates.x, track_coordinates.y }.Rotate(rotation);
2021-08-27 17:44:33 -04:00
newCoords = { offsetAndRotatedTrack, newCoords.z - track_coordinates.z_begin + track_coordinates.z_end };
rotation = (rotation + track_coordinates.rotation_end - track_coordinates.rotation_begin) & 3;
if (track_coordinates.rotation_end & (1 << 2))
{
2017-06-06 23:24:18 +02:00
rotation |= (1 << 2);
}
else
{
newCoords += CoordsDirectionDelta[rotation];
2017-06-06 23:24:18 +02:00
}
}
2017-06-06 23:24:18 +02:00
// Entrance elements
for (const auto& entrance : td6->entrance_elements)
{
2017-06-06 23:24:18 +02:00
rotation = _currentTrackPieceDirection & 3;
2019-12-04 23:30:36 -03:00
CoordsXY entranceMapPos{ entrance.x, entrance.y };
auto rotatedEntranceMapPos = entranceMapPos.Rotate(rotation);
newCoords = { rotatedEntranceMapPos + _trackPreviewOrigin, newCoords.z };
track_design_update_max_min_coordinates(newCoords);
switch (_trackDesignPlaceOperation)
{
2018-06-22 23:14:18 +02:00
case PTD_OPERATION_DRAW_OUTLINES:
track_design_add_selection_tile(newCoords);
2018-06-22 23:14:18 +02:00
break;
2019-03-27 19:21:13 +00:00
case PTD_OPERATION_PLACE_QUERY:
case PTD_OPERATION_PLACE:
case PTD_OPERATION_PLACE_GHOST:
case PTD_OPERATION_PLACE_TRACK_PREVIEW:
{
2019-04-08 19:34:24 +01:00
rotation = (rotation + entrance.direction) & 3;
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation != PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
auto tile = CoordsXY{ newCoords } + CoordsDirectionDelta[rotation];
TileElement* tile_element = map_get_first_element_at(tile);
newCoords.z = _trackPreviewOrigin.z / COORDS_Z_STEP;
newCoords.z += entrance.z;
if (tile_element == nullptr)
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
}
2018-06-22 23:14:18 +02:00
do
2017-07-24 18:43:57 +02:00
{
2018-06-22 23:14:18 +02:00
if (tile_element->GetType() != TILE_ELEMENT_TYPE_TRACK)
{
continue;
}
if (tile_element->base_height != newCoords.z)
2018-06-22 23:14:18 +02:00
{
continue;
}
auto stationIndex = tile_element->AsTrack()->GetStationIndex();
uint8_t flags = GAME_COMMAND_FLAG_APPLY;
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_TRACK_PREVIEW)
2018-06-22 23:14:18 +02:00
{
2019-04-02 18:17:37 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED
| GAME_COMMAND_FLAG_NO_SPEND;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_GHOST)
2018-06-22 23:14:18 +02:00
{
2019-04-01 18:58:16 +01:00
flags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND
| GAME_COMMAND_FLAG_GHOST;
2018-06-22 23:14:18 +02:00
}
2019-03-27 19:21:13 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_PLACE_QUERY)
2018-06-22 23:14:18 +02:00
{
flags = 0;
2018-06-22 23:14:18 +02:00
}
2020-04-05 08:28:07 +01:00
if (_trackDesignPlaceIsReplay)
{
flags |= GAME_COMMAND_FLAG_REPLAY;
}
2018-06-22 23:14:18 +02:00
gGameCommandErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(
newCoords, rotation, ride->id, stationIndex, entrance.isExit);
rideEntranceExitPlaceAction.SetFlags(flags);
auto res = flags & GAME_COMMAND_FLAG_APPLY ? GameActions::ExecuteNested(&rideEntranceExitPlaceAction)
: GameActions::QueryNested(&rideEntranceExitPlaceAction);
2018-06-22 23:14:18 +02:00
2021-07-31 22:18:15 +02:00
totalCost += res->Cost;
if (res->Error != GameActions::Status::Ok)
2018-06-22 23:14:18 +02:00
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2018-06-22 23:14:18 +02:00
}
_trackDesignPlaceStateEntranceExitPlaced = true;
break;
} while (!(tile_element++)->IsLastForTile());
}
else
{
newCoords.z = entrance.z * COORDS_Z_STEP;
newCoords.z += _trackPreviewOrigin.z;
auto res = RideEntranceExitPlaceAction::TrackPlaceQuery(newCoords, false);
if (res->Error != GameActions::Status::Ok)
2017-07-24 18:43:57 +02:00
{
2021-08-08 12:27:06 +02:00
return std::nullopt;
2017-06-06 23:24:18 +02:00
}
2018-06-22 23:14:18 +02:00
else
{
2021-07-31 22:18:15 +02:00
totalCost += res->Cost;
2018-06-22 23:14:18 +02:00
_trackDesignPlaceStateEntranceExitPlaced = true;
}
}
2018-06-22 23:14:18 +02:00
break;
}
2017-06-06 23:24:18 +02:00
}
}
2019-03-27 19:33:27 +00:00
if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST)
2017-07-24 18:43:57 +02:00
{
2021-09-01 19:00:18 -07:00
ride->ValidateStations();
2019-03-20 20:05:25 +01:00
ride->Delete();
2017-06-06 23:24:18 +02:00
}
2021-08-08 12:27:06 +02:00
return totalCost;
}
/**
2018-06-22 23:14:18 +02:00
* Places a virtual track. This can involve highlighting the surface tiles and showing the track layout. It is also used by
* the track preview window to place the whole track.
* Depending on the value of bl it modifies the function.
* bl == 0, Draw outlines on the ground
* bl == 1,
* bl == 2,
* bl == 3, Returns the z value of a successful placement. Only lower 16 bits are the value, the rest may be garbage?
* bl == 4,
* bl == 5, Returns cost to create the track. All 32 bits are used. Places the track. (used by the preview)
* bl == 6, Clear white outlined track.
* rct2: 0x006D01B3
*/
2021-07-31 22:18:15 +02:00
money32 place_virtual_track(TrackDesign* td6, uint8_t ptdOperation, bool placeScenery, Ride* ride, const CoordsXYZ& coords)
{
// Previously byte_F4414E was cleared here
2018-06-22 23:14:18 +02:00
_trackDesignPlaceStatePlaceScenery = placeScenery;
_trackDesignPlaceStateEntranceExitPlaced = false;
_trackDesignPlaceStateSceneryUnavailable = false;
2018-06-22 23:14:18 +02:00
_trackDesignPlaceStateHasScenery = false;
2020-04-05 08:28:07 +01:00
_trackDesignPlaceIsReplay = ptdOperation & PTD_OPERATION_FLAG_IS_REPLAY;
ptdOperation &= ~PTD_OPERATION_FLAG_IS_REPLAY;
2017-07-24 18:43:57 +02:00
_trackDesignPlaceOperation = ptdOperation;
if (gTrackDesignSceneryToggle)
{
_trackDesignPlaceStatePlaceScenery = false;
2017-06-06 23:24:18 +02:00
}
2019-01-20 14:54:59 +00:00
_currentRideIndex = ride->id;
_trackPreviewMin = coords;
_trackPreviewMax = coords;
_trackDesignPlaceSceneryZ = 0;
2021-08-08 12:27:06 +02:00
std::optional<money32> trackPlaceCost;
if (td6->type == RIDE_TYPE_MAZE)
{
2021-08-08 12:27:06 +02:00
trackPlaceCost = track_design_place_maze(td6, coords, ride);
}
else
{
2021-08-08 12:27:06 +02:00
trackPlaceCost = track_design_place_ride(td6, coords, ride);
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Scenery elements
2021-08-08 12:27:06 +02:00
if (trackPlaceCost.has_value())
{
2021-08-15 13:39:04 +02:00
auto sceneryCost = track_design_place_all_scenery(td6->scenery_elements, _trackPreviewOrigin);
if (!sceneryCost.has_value())
{
2021-07-31 22:18:15 +02:00
return MONEY32_UNDEFINED;
2017-06-06 23:24:18 +02:00
}
2021-08-08 12:27:06 +02:00
2021-08-15 13:39:04 +02:00
trackPlaceCost = trackPlaceCost.value() + sceneryCost.value();
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// 0x6D0FE6
if (_trackDesignPlaceOperation == PTD_OPERATION_DRAW_OUTLINES)
{
2017-06-06 23:24:18 +02:00
gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE_CONSTRUCT;
gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE_ARROW;
gMapSelectFlags &= ~MAP_SELECT_FLAG_GREEN;
map_invalidate_map_selection_tiles();
}
2017-07-24 18:43:57 +02:00
if (ptdOperation == PTD_OPERATION_GET_PLACE_Z)
{
// Change from vanilla: originally, _trackDesignPlaceSceneryZ was not subtracted
// from _trackDesignPlaceZ, causing bug #259.
return _trackDesignPlaceZ - _trackDesignPlaceSceneryZ;
2017-06-06 23:24:18 +02:00
}
2021-08-08 12:27:06 +02:00
return trackPlaceCost.has_value() ? trackPlaceCost.value() : MONEY32_UNDEFINED;
}
2019-12-12 08:38:04 +00:00
static money32 track_design_ride_create_command(int32_t type, int32_t subType, int32_t flags, ride_id_t* outRideIndex)
2019-12-11 20:33:58 +00:00
{
// Don't set colours as will be set correctly later.
auto gameAction = RideCreateAction(type, subType, 0, 0);
gameAction.SetFlags(flags);
auto r = GameActions::ExecuteNested(&gameAction);
const RideCreateGameActionResult* res = static_cast<RideCreateGameActionResult*>(r.get());
// Callee's of this function expect MONEY32_UNDEFINED in case of failure.
if (res->Error != GameActions::Status::Ok)
2019-12-11 20:33:58 +00:00
{
return MONEY32_UNDEFINED;
}
*outRideIndex = res->rideIndex;
return res->Cost;
}
/**
*
* rct2: 0x006D2189
* ebx = ride_id
* cost = edi
*/
2019-04-08 19:34:24 +01:00
static bool track_design_place_preview(TrackDesign* td6, money32* cost, Ride** outRide, uint8_t* flags)
{
2019-02-13 20:16:42 +00:00
*outRide = nullptr;
2018-06-22 23:14:18 +02:00
*flags = 0;
2021-09-12 21:25:22 +02:00
auto& objManager = GetContext()->GetObjectManager();
auto entry_index = objManager.GetLoadedObjectEntryIndex(td6->vehicle_object);
2019-01-12 10:11:55 +00:00
ride_id_t rideIndex;
2019-04-01 18:58:16 +01:00
uint8_t rideCreateFlags = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_NO_SPEND;
2019-12-11 20:33:58 +00:00
if (track_design_ride_create_command(td6->type, entry_index, rideCreateFlags, &rideIndex) == MONEY32_UNDEFINED)
{
2017-06-06 23:24:18 +02:00
return false;
}
2019-01-20 14:54:59 +00:00
auto ride = get_ride(rideIndex);
if (ride == nullptr)
return false;
2019-07-21 02:22:03 +01:00
ride->custom_name = {};
2017-06-06 23:24:18 +02:00
ride->entrance_style = td6->entrance_style;
for (int32_t i = 0; i < RCT12_NUM_COLOUR_SCHEMES; i++)
{
2019-01-01 20:33:59 +00:00
ride->track_colour[i].main = td6->track_spine_colour[i];
ride->track_colour[i].additional = td6->track_rail_colour[i];
ride->track_colour[i].supports = td6->track_support_colour[i];
2017-06-06 23:24:18 +02:00
}
2018-01-02 17:21:40 +00:00
// Flat rides need their vehicle colours loaded for display
// in the preview window
if (!GetRideTypeDescriptor(td6->type).HasFlag(RIDE_TYPE_FLAG_HAS_TRACK))
2018-01-02 17:21:40 +00:00
{
for (int32_t i = 0; i < RCT12_MAX_VEHICLE_COLOURS; i++)
2018-01-02 17:21:40 +00:00
{
ride->vehicle_colours[i].Body = td6->vehicle_colours[i].body_colour;
ride->vehicle_colours[i].Trim = td6->vehicle_colours[i].trim_colour;
ride->vehicle_colours[i].Ternary = td6->vehicle_additional_colour[i];
2018-01-02 17:21:40 +00:00
}
}
2021-08-08 18:10:52 +02:00
_trackDesignDrawingPreview = true;
2018-06-22 23:14:18 +02:00
uint8_t backup_rotation = _currentTrackPieceDirection;
uint32_t backup_park_flags = gParkFlags;
2017-06-06 23:24:18 +02:00
gParkFlags &= ~PARK_FLAGS_FORBID_HIGH_CONSTRUCTION;
int32_t mapSize = gMapSize << 4;
2017-06-06 23:24:18 +02:00
_currentTrackPieceDirection = 0;
2021-08-02 07:24:52 +03:00
int32_t z = place_virtual_track(
2021-09-09 00:06:34 +03:00
td6, PTD_OPERATION_GET_PLACE_Z, true, GetOrAllocateRide(PreviewRideId), { mapSize, mapSize, 16 });
if (_trackDesignPlaceStateHasScenery)
{
2017-06-06 23:24:18 +02:00
*flags |= TRACK_DESIGN_FLAG_HAS_SCENERY;
}
2016-11-13 20:17:49 +01:00
z += 16 - _trackDesignPlaceSceneryZ;
2017-07-24 18:43:57 +02:00
bool placeScenery = true;
if (_trackDesignPlaceStateSceneryUnavailable)
{
2017-07-24 18:43:57 +02:00
placeScenery = false;
2017-06-06 23:24:18 +02:00
*flags |= TRACK_DESIGN_FLAG_SCENERY_UNAVAILABLE;
}
money32 resultCost = place_virtual_track(
td6, PTD_OPERATION_PLACE_TRACK_PREVIEW, placeScenery, ride, { mapSize, mapSize, z });
2017-06-06 23:24:18 +02:00
gParkFlags = backup_park_flags;
if (resultCost != MONEY32_UNDEFINED)
{
2021-09-12 21:25:22 +02:00
if (entry_index == OBJECT_ENTRY_INDEX_NULL)
{
*flags |= TRACK_DESIGN_FLAG_VEHICLE_UNAVAILABLE;
}
2017-07-23 06:09:29 -04:00
else if (!ride_entry_is_invented(entry_index) && !gCheatsIgnoreResearchStatus)
{
2017-06-06 23:24:18 +02:00
*flags |= TRACK_DESIGN_FLAG_VEHICLE_UNAVAILABLE;
}
2017-06-06 23:24:18 +02:00
_currentTrackPieceDirection = backup_rotation;
2021-08-08 18:10:52 +02:00
_trackDesignDrawingPreview = false;
2018-06-22 23:14:18 +02:00
*cost = resultCost;
2019-02-13 20:16:42 +00:00
*outRide = ride;
2017-06-06 23:24:18 +02:00
return true;
}
else
{
2017-06-06 23:24:18 +02:00
_currentTrackPieceDirection = backup_rotation;
2019-07-21 02:22:03 +01:00
ride->Delete();
2021-08-08 18:10:52 +02:00
_trackDesignDrawingPreview = false;
2017-06-06 23:24:18 +02:00
return false;
}
}
2016-04-28 22:57:04 +01:00
#pragma region Track Design Preview
2016-04-30 22:29:13 +01:00
/**
*
* rct2: 0x006D1EF0
*/
2019-04-08 19:34:24 +01:00
void track_design_draw_preview(TrackDesign* td6, uint8_t* pixels)
2016-04-30 22:29:13 +01:00
{
2021-04-09 02:09:08 +01:00
StashMap();
2017-06-06 23:24:18 +02:00
track_design_preview_clear_map();
2016-04-30 22:29:13 +01:00
if (gScreenFlags & SCREEN_FLAGS_TRACK_MANAGER)
{
2017-06-06 23:24:18 +02:00
track_design_load_scenery_objects(td6);
}
2016-04-30 22:29:13 +01:00
2017-06-06 23:24:18 +02:00
money32 cost;
2019-02-13 20:16:42 +00:00
Ride* ride;
2018-06-22 23:14:18 +02:00
uint8_t flags;
2019-02-13 20:16:42 +00:00
if (!track_design_place_preview(td6, &cost, &ride, &flags))
{
std::fill_n(pixels, TRACK_PREVIEW_IMAGE_SIZE * 4, 0x00);
2021-04-09 02:09:08 +01:00
UnstashMap();
2017-06-06 23:24:18 +02:00
return;
}
2018-06-22 23:14:18 +02:00
td6->cost = cost;
2017-06-06 23:24:18 +02:00
td6->track_flags = flags & 7;
2016-04-30 22:29:13 +01:00
2020-04-19 21:58:27 +02:00
CoordsXYZ centre = { (_trackPreviewMin.x + _trackPreviewMax.x) / 2 + 16, (_trackPreviewMin.y + _trackPreviewMax.y) / 2 + 16,
(_trackPreviewMin.z + _trackPreviewMax.z) / 2 };
2016-04-30 22:29:13 +01:00
2020-04-19 21:58:27 +02:00
int32_t size_x = _trackPreviewMax.x - _trackPreviewMin.x;
int32_t size_y = _trackPreviewMax.y - _trackPreviewMin.y;
int32_t size_z = _trackPreviewMax.z - _trackPreviewMin.z;
2016-04-30 22:29:13 +01:00
// Special case for flat rides - Z-axis info is irrelevant
// and must be zeroed out lest the preview be off-centre
if (!GetRideTypeDescriptor(td6->type).HasFlag(RIDE_TYPE_FLAG_HAS_TRACK))
{
centre.z = 0;
size_z = 0;
}
int32_t zoom_level = 1;
if (size_x < size_y)
{
2017-06-06 23:24:18 +02:00
size_x = size_y;
}
if (size_x > 1000 || size_z > 280)
{
2017-06-06 23:24:18 +02:00
zoom_level = 2;
}
if (size_x > 1600 || size_z > 1000)
{
2017-06-06 23:24:18 +02:00
zoom_level = 3;
}
2016-04-30 22:29:13 +01:00
2017-06-06 23:24:18 +02:00
size_x = 370 << zoom_level;
size_y = 217 << zoom_level;
2016-04-30 22:29:13 +01:00
2017-06-06 23:24:18 +02:00
rct_viewport view;
2018-06-22 23:14:18 +02:00
view.width = 370;
view.height = 217;
view.view_width = size_x;
2017-06-06 23:24:18 +02:00
view.view_height = size_y;
2020-03-01 09:56:14 -03:00
view.pos = { 0, 0 };
2018-06-22 23:14:18 +02:00
view.zoom = zoom_level;
view.flags = VIEWPORT_FLAG_HIDE_BASE | VIEWPORT_FLAG_INVISIBLE_SPRITES;
2016-04-30 22:29:13 +01:00
2017-06-06 23:24:18 +02:00
rct_drawpixelinfo dpi;
dpi.zoom_level = zoom_level;
2018-06-22 23:14:18 +02:00
dpi.x = 0;
dpi.y = 0;
dpi.width = 370;
dpi.height = 217;
dpi.pitch = 0;
dpi.bits = pixels;
2016-04-30 22:29:13 +01:00
auto drawingEngine = std::make_unique<X8DrawingEngine>(GetContext()->GetUiContext());
dpi.DrawingEngine = drawingEngine.get();
2018-06-22 23:14:18 +02:00
CoordsXY offset = { size_x / 2, size_y / 2 };
for (uint8_t i = 0; i < 4; i++)
{
2017-06-06 23:24:18 +02:00
gCurrentRotation = i;
2016-04-30 22:29:13 +01:00
auto screenCoords = translate_3d_to_2d_with_z(i, centre);
screenCoords.x -= offset.x;
screenCoords.y -= offset.y;
2016-04-30 22:29:13 +01:00
int32_t left = screenCoords.x;
int32_t top = screenCoords.y;
2018-06-22 23:14:18 +02:00
int32_t right = left + size_x;
int32_t bottom = top + size_y;
2016-04-30 22:29:13 +01:00
2020-03-01 09:56:14 -03:00
view.viewPos = { left, top };
2017-06-06 23:24:18 +02:00
viewport_paint(&view, &dpi, left, top, right, bottom);
2016-04-30 22:29:13 +01:00
2017-06-06 23:24:18 +02:00
dpi.bits += TRACK_PREVIEW_IMAGE_SIZE;
}
2016-04-30 22:29:13 +01:00
2019-03-20 20:05:25 +01:00
ride->Delete();
2021-04-09 02:09:08 +01:00
UnstashMap();
2016-04-28 22:57:04 +01:00
}
/**
* Resets all the map elements to surface tiles for track preview.
* rct2: 0x006D1D9A
*/
static void track_design_preview_clear_map()
{
2021-04-09 02:09:08 +01:00
auto numTiles = MAXIMUM_MAP_SIZE_TECHNICAL * MAXIMUM_MAP_SIZE_TECHNICAL;
2018-06-22 23:14:18 +02:00
gMapSize = 256;
2016-04-28 22:57:04 +01:00
2021-04-09 02:09:08 +01:00
// Reserve ~8 elements per tile
std::vector<TileElement> tileElements;
tileElements.reserve(numTiles * 8);
for (int32_t i = 0; i < numTiles; i++)
{
2021-04-09 02:09:08 +01:00
auto* element = &tileElements.emplace_back();
element->ClearAs(TILE_ELEMENT_TYPE_SURFACE);
element->SetLastForTile(true);
element->AsSurface()->SetSlope(TILE_ELEMENT_SLOPE_FLAT);
element->AsSurface()->SetWaterHeight(0);
element->AsSurface()->SetSurfaceStyle(0);
element->AsSurface()->SetEdgeStyle(0);
element->AsSurface()->SetGrassLength(GRASS_LENGTH_CLEAR_0);
element->AsSurface()->SetOwnership(OWNERSHIP_OWNED);
element->AsSurface()->SetParkFences(0);
2017-06-06 23:24:18 +02:00
}
2021-04-09 02:09:08 +01:00
SetTileElements(std::move(tileElements));
2016-04-28 22:57:04 +01:00
}
bool track_design_are_entrance_and_exit_placed()
{
return _trackDesignPlaceStateEntranceExitPlaced;
}
2016-04-28 22:57:04 +01:00
#pragma endregion