/
MazePlaceTrackAction.cpp
190 lines (158 loc) 路 6.07 KB
/
MazePlaceTrackAction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "MazePlaceTrackAction.h"
#include "../management/Finance.h"
#include "../ride/RideData.h"
#include "../ride/TrackData.h"
#include "../world/ConstructionClearance.h"
using namespace OpenRCT2::TrackMetaData;
MazePlaceTrackAction::MazePlaceTrackAction(const CoordsXYZ& location, RideId rideIndex, uint16_t mazeEntry)
: _loc(location)
, _rideIndex(rideIndex)
, _mazeEntry(mazeEntry)
{
}
void MazePlaceTrackAction::AcceptParameters(GameActionParameterVisitor& visitor)
{
visitor.Visit(_loc);
visitor.Visit("ride", _rideIndex);
visitor.Visit("mazeEntry", _mazeEntry);
}
void MazePlaceTrackAction::Serialise(DataSerialiser& stream)
{
GameAction::Serialise(stream);
stream << DS_TAG(_loc) << DS_TAG(_rideIndex) << DS_TAG(_mazeEntry);
}
GameActions::Result MazePlaceTrackAction::Query() const
{
auto res = GameActions::Result();
res.Position = _loc + CoordsXYZ{ 8, 8, 0 };
res.Expenditure = ExpenditureType::RideConstruction;
res.ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
if ((_loc.z & 0xF) != 0)
{
res.Error = GameActions::Status::Unknown;
res.ErrorMessage = STR_CONSTRUCTION_ERR_UNKNOWN;
return res;
}
if (!LocationValid(_loc) || (!map_is_location_owned(_loc) && !gCheatsSandboxMode))
{
res.Error = GameActions::Status::NotOwned;
res.ErrorMessage = STR_LAND_NOT_OWNED_BY_PARK;
return res;
}
if (!MapCheckCapacityAndReorganise(_loc))
{
res.Error = GameActions::Status::NoFreeElements;
res.ErrorMessage = STR_TILE_ELEMENT_LIMIT_REACHED;
return res;
}
auto surfaceElement = map_get_surface_element_at(_loc);
if (surfaceElement == nullptr)
{
res.Error = GameActions::Status::Unknown;
res.ErrorMessage = STR_INVALID_SELECTION_OF_OBJECTS;
return res;
}
auto baseHeight = _loc.z;
auto clearanceHeight = _loc.z + MAZE_CLEARANCE_HEIGHT;
auto heightDifference = baseHeight - surfaceElement->GetBaseZ();
if (heightDifference >= 0 && !gCheatsDisableSupportLimits)
{
heightDifference /= COORDS_Z_PER_TINY_Z;
if (heightDifference > GetRideTypeDescriptor(RIDE_TYPE_MAZE).Heights.MaxHeight)
{
res.Error = GameActions::Status::TooHigh;
res.ErrorMessage = STR_TOO_HIGH_FOR_SUPPORTS;
return res;
}
}
auto canBuild = MapCanConstructWithClearAt(
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &map_place_non_scenery_clear_func, { 0b1111, 0 }, GetFlags());
if (canBuild.Error != GameActions::Status::Ok)
{
canBuild.ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
return canBuild;
}
const auto clearanceData = canBuild.GetData<ConstructClearResult>();
if (clearanceData.GroundFlags & ELEMENT_IS_UNDERWATER)
{
res.Error = GameActions::Status::NoClearance;
res.ErrorMessage = STR_RIDE_CANT_BUILD_THIS_UNDERWATER;
return res;
}
if (clearanceData.GroundFlags & ELEMENT_IS_UNDERGROUND)
{
res.Error = GameActions::Status::NoClearance;
res.ErrorMessage = STR_CAN_ONLY_BUILD_THIS_ABOVE_GROUND;
return res;
}
auto ride = get_ride(_rideIndex);
if (ride == nullptr || ride->type == RIDE_TYPE_NULL)
{
res.Error = GameActions::Status::InvalidParameters;
res.ErrorMessage = STR_INVALID_SELECTION_OF_OBJECTS;
return res;
}
const auto& ted = GetTrackElementDescriptor(TrackElemType::Maze);
money32 price = (((ride->GetRideTypeDescriptor().BuildCosts.TrackPrice * ted.Price) >> 16));
res.Cost = ((canBuild.Cost + price) / 2) * 10;
return res;
}
GameActions::Result MazePlaceTrackAction::Execute() const
{
auto res = GameActions::Result();
res.Position = _loc + CoordsXYZ{ 8, 8, 0 };
res.Expenditure = ExpenditureType::RideConstruction;
res.ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
auto ride = get_ride(_rideIndex);
if (ride == nullptr)
{
res.Error = GameActions::Status::InvalidParameters;
res.ErrorMessage = STR_NONE;
return res;
}
uint32_t flags = GetFlags();
if (!(flags & GAME_COMMAND_FLAG_GHOST))
{
footpath_remove_litter(_loc);
wall_remove_at({ _loc.ToTileStart(), _loc.z, _loc.z + 32 });
}
auto baseHeight = _loc.z;
auto clearanceHeight = _loc.z + MAZE_CLEARANCE_HEIGHT;
auto canBuild = MapCanConstructWithClearAt(
{ _loc.ToTileStart(), baseHeight, clearanceHeight }, &map_place_non_scenery_clear_func, { 0b1111, 0 },
GetFlags() | GAME_COMMAND_FLAG_APPLY);
if (canBuild.Error != GameActions::Status::Ok)
{
canBuild.ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
return canBuild;
}
const auto& ted = GetTrackElementDescriptor(TrackElemType::Maze);
money32 price = (((ride->GetRideTypeDescriptor().BuildCosts.TrackPrice * ted.Price) >> 16));
res.Cost = ((canBuild.Cost + price) / 2) * 10;
auto startLoc = _loc.ToTileStart();
auto* trackElement = TileElementInsert<TrackElement>(_loc, 0b1111);
Guard::Assert(trackElement != nullptr);
trackElement->SetClearanceZ(clearanceHeight);
trackElement->SetTrackType(TrackElemType::Maze);
trackElement->SetRideType(ride->type);
trackElement->SetRideIndex(_rideIndex);
trackElement->SetMazeEntry(_mazeEntry);
trackElement->SetGhost(flags & GAME_COMMAND_FLAG_GHOST);
map_invalidate_tile_full(startLoc);
ride->maze_tiles++;
ride->GetStation().SetBaseZ(trackElement->GetBaseZ());
ride->GetStation().Start = { 0, 0 };
if (ride->maze_tiles == 1)
{
ride->overall_view = startLoc;
}
return res;
}