forked from OpenLoco/OpenLoco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackObject.cpp
183 lines (168 loc) · 6.04 KB
/
TrackObject.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
#include "TrackObject.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "ObjectImageTable.h"
#include "ObjectManager.h"
#include "ObjectStringTable.h"
#include <OpenLoco/Interop/Interop.hpp>
namespace OpenLoco
{
// 0x004A6CBA
void TrackObject::drawPreviewImage(Gfx::RenderTarget& rt, const int16_t x, const int16_t y) const
{
auto colourImage = Gfx::recolour(image, Colour::mutedDarkRed);
auto& drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(&rt, x, y, colourImage + 18);
drawingCtx.drawImage(&rt, x, y, colourImage + 20);
drawingCtx.drawImage(&rt, x, y, colourImage + 22);
}
// 0x004A6C6C
bool TrackObject::validate() const
{
if (var_06 >= 3)
{
return false;
}
// vanilla missed this check
if (costIndex > 32)
{
return false;
}
if (-sellCostFactor > buildCostFactor)
{
return false;
}
if (buildCostFactor <= 0)
{
return false;
}
if (tunnelCostFactor <= 0)
{
return false;
}
if (hasPieceFlags(TrackObjectPieceFlags::diagonal | TrackObjectPieceFlags::largeCurve)
&& hasPieceFlags(TrackObjectPieceFlags::oneSided | TrackObjectPieceFlags::verySmallCurve))
{
return false;
}
if (numBridges > 7)
{
return false;
}
return numStations <= 7;
}
// 0x004A6A5F
void TrackObject::load(const LoadedObjectHandle& handle, stdx::span<const std::byte> data, ObjectManager::DependentObjects* dependencies)
{
auto remainingData = data.subspan(sizeof(TrackObject));
auto strRes = ObjectManager::loadStringTable(remainingData, handle, 0);
name = strRes.str;
remainingData = remainingData.subspan(strRes.tableLength);
// NOTE: These aren't dependent (objects unless otherwise stated) as this object can load without the
// related object.
// Load compatible roads/tracks
compatibleTracks = 0;
compatibleRoads = 0;
for (auto i = 0; i < numCompatible; ++i)
{
ObjectHeader modHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
auto res = ObjectManager::findObjectHandle(modHeader);
if (res.has_value())
{
if (res->type == ObjectType::track)
{
compatibleTracks |= 1U << res->id;
}
else if (res->type == ObjectType::road)
{
compatibleRoads |= 1U << res->id;
}
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
// Load Extra
std::fill(std::begin(mods), std::end(mods), 0xFF);
for (auto i = 0U, index = 0U; i < numMods; ++i)
{
ObjectHeader modHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
auto res = ObjectManager::findObjectHandle(modHeader);
if (res.has_value() && res->type == ObjectType::trackExtra)
{
mods[index++] = res->id;
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
// Load Signals
signals = 0;
for (auto i = 0; i < numSignals; ++i)
{
ObjectHeader modHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
auto res = ObjectManager::findObjectHandle(modHeader);
if (res.has_value() && res->type == ObjectType::trackSignal)
{
signals |= 1U << res->id;
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
// Load Tunnel (DEPENDENT OBJECT)
tunnel = 0xFF;
{
ObjectHeader unkHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
if (dependencies != nullptr)
{
dependencies->required.push_back(unkHeader);
}
auto res = ObjectManager::findObjectHandle(unkHeader);
if (res.has_value())
{
tunnel = res->id;
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
// Load bridges (DEPENDENT OBJECT)
std::fill(std::begin(bridges), std::end(bridges), 0xFF);
for (auto i = 0U; i < numBridges; ++i)
{
ObjectHeader bridgeHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
if (dependencies != nullptr)
{
dependencies->required.push_back(bridgeHeader);
}
auto res = ObjectManager::findObjectHandle(bridgeHeader);
if (res.has_value())
{
bridges[i] = res->id;
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
// Load stations
std::fill(std::begin(stations), std::end(stations), 0xFF);
for (auto i = 0U; i < numStations; ++i)
{
ObjectHeader stationHeader = *reinterpret_cast<const ObjectHeader*>(remainingData.data());
auto res = ObjectManager::findObjectHandle(stationHeader);
if (res.has_value())
{
stations[i] = res->id;
}
remainingData = remainingData.subspan(sizeof(ObjectHeader));
}
auto imgRes = ObjectManager::loadImageTable(remainingData);
image = imgRes.imageOffset;
assert(remainingData.size() == imgRes.tableLength);
}
// 0x004A6C2D
void TrackObject::unload()
{
name = 0;
compatibleTracks = 0;
compatibleRoads = 0;
std::fill(std::begin(mods), std::end(mods), 0);
signals = 0;
tunnel = 0;
image = 0;
std::fill(std::begin(bridges), std::end(bridges), 0);
std::fill(std::begin(stations), std::end(stations), 0);
}
}