-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.cpp
More file actions
314 lines (244 loc) · 7.02 KB
/
background.cpp
File metadata and controls
314 lines (244 loc) · 7.02 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "background.h"
template<std::bidirectional_iterator Iter>
void InsertionSort(Iter begin, Iter end, auto&& cmp)
{
if (begin == end) return;
Iter firstUnsorted = begin;
while (++firstUnsorted != end)
{
Iter insertPos = firstUnsorted;
while (insertPos > begin && cmp(*insertPos, *std::prev(insertPos)))
{
std::swap(*insertPos, *std::prev(insertPos));
--insertPos;
}
}
}
template<std::ranges::bidirectional_range Range, class Cmp>
void InsertionSort(Range&& range, Cmp&& cmp)
{
InsertionSort(std::ranges::begin(range), std::ranges::end(range), std::forward<Cmp>(cmp));
}
constexpr Fix12i length = 140000._f;
constexpr Fix12i radius = 5000._f;
constexpr Fix12i vertOffset = 500._f;
constexpr Vector3 scale = {radius, radius, length};
constexpr unsigned numClouds = std::tuple_size<decltype(Background::cloudPositions)>();
constexpr Fix12i spawnDist = 0x10000'000_f;
constexpr Fix12i interval = 2 * spawnDist / numClouds;
constinit SharedFilePtr Background::cloudModelFile = 0x02fc; // data/normal_obj/obj_kumo/obj_kumo.bmd
constinit bool Background::renderInInterior = false;
SpawnInfo Background::spawnData =
{
+[]() -> ActorBase* { return new Background; },
0x0100, // behavPriority
0x0100, // renderPriority
UPDATE_DURING_DIALOGUE | UPDATE_DURING_CUTSCENES | UPDATE_DURING_STAR_SPAWNING,
0._f, // rangeOffsetY
-1._f, // range
0._f, // drawDist
0._f // unkc0
};
static void RandomizeXY(Vector3& v)
{
while (true)
{
v.x.val = RandomInt() >> 7;
v.y.val = RandomInt() >> 7;
const int distSquared = int(v.x) * int(v.x) + int(v.y) * int(v.y);
static constexpr int maxDistSquared = 0x1000 * 0x1000;
static constexpr int minDistSquared = 0x600 * 0x600;
if (distSquared <= maxDistSquared && distSquared >= minDistSquared)
break;
}
v.y += vertOffset;
}
int Background::InitResources()
{
wingCtrl.SetFile();
model.SetFile(modelFile.LoadBMD(), true, 0);
model.mat4x3 = Matrix4x3::Translation(0._f, vertOffset, 0._f);
model.data.materials[0].SetTransformMode(Material::TEXCOORD_SOURCE);
model.data.materials[1].SetTransformMode(Material::TEXCOORD_SOURCE);
model.data.materials[1].SetAlpha(9);
cloudModel.SetFile(cloudModelFile.LoadBMD(), false);
*reinterpret_cast<uint16_t*>(cloudModelFile.filePtr + 0xb0) = Color5Bit(0x70, 0x40, 0xf8);
for (Fix12i zPos = spawnDist; Vector3& cloudPos : cloudPositions)
{
RandomizeXY(cloudPos);
cloudPos.z = zPos;
zPos -= interval;
}
return 1;
}
int Background::CleanupResources()
{
wingCtrl.ReleaseFile();
modelFile.Release();
cloudModelFile.Release();
return 1;
}
bool Background::BeforeBehavior()
{
areaID = AREA_ID;
Actor::BeforeBehavior();
return true;
}
bool Background::BeforeRender()
{
Actor::BeforeRender();
return true;
}
constexpr int zStart = 2800;
constexpr int zEnd = -3200;
constexpr int z3rd = (zStart - zEnd) / 3;
constexpr std::array<Vector3, 4> wingOffsets =
{{
{2450._f, -920._f, zStart - z3rd * 0},
{2770._f, -1010._f, zStart - z3rd * 1},
{2885._f, -1030._f, zStart - z3rd * 2},
{2780._f, -960._f, zStart - z3rd * 3}
}};
constexpr unsigned numAreas = 5;
constexpr Fix12i hatchX = 2730._f;
constinit bool gamePrevPaused = false;
constinit bool renderInInteriorBeforePause = false;
constinit std::array<bool, numAreas> areasShowingBeforePause;
int Background::Behavior()
{
if (GAME_PAUSED)
{
if (!gamePrevPaused)
{
for (unsigned i = 0; i < numAreas; ++i)
areasShowingBeforePause[i] = AREAS[i].showing;
renderInInteriorBeforePause = renderInInterior;
renderInInterior = true;
gamePrevPaused = true;
}
AREAS[0].showing = true;
AREAS[1].showing = false;
AREAS[2].showing = false;
AREAS[3].showing = true;
AREAS[4].showing = true;
return 1;
}
else if (gamePrevPaused)
{
for (unsigned i = 0; i < numAreas; ++i)
AREAS[i].showing = areasShowingBeforePause[i];
renderInInterior = renderInInteriorBeforePause;
gamePrevPaused = false;
}
Player& player = *PLAYER_ARR[0];
const Camera& cam = *CAMERA;
if (cam.flags & Camera::UNDERWATER)
{
wingCtrl.Update(0);
}
else if (areaID == 0 || !renderInInterior && cam.pos.z > -3900._f)
{
const Vector3 soundOffset =
{
areaID == 0 ? 2720._f : 7000._f,
-980._f,
std::clamp(cam.pos.z, wingOffsets.back().z, wingOffsets.front().z)
};
unsigned wingFlags = Wing::mirrorSound;
if (areaID != 0)
wingFlags |= Wing::noWingSound;
wingCtrl.Update(wingFlags, soundOffset);
}
else
{
static constexpr Vector3 soundOffset = {3600._f, -1100._f, -2400._f};
wingCtrl.Update(0, soundOffset);
}
auto& scroll0 = model.data.materials[0].texTransY;
auto& scroll1 = model.data.materials[1].texTransY;
scroll0 -= 0.02_f;
scroll1 += 0.013_f;
scroll0.val &= 0xfff;
scroll1.val &= 0xfff;
for (Vector3& cloudPos : cloudPositions)
{
cloudPos.z -= 128._f;
if (cloudPos.z < -spawnDist)
{
cloudPos.z = spawnDist;
RandomizeXY(cloudPos);
}
}
if (areaID == 1)
renderInInterior = false;
if (areaID == 0)
{
if ( hatchX < player.pos.x &&
-1450._f < player.pos.y && player.pos.y < -200._f &&
-3400._f < player.pos.z && player.pos.z < -1000._f)
{
player.ChangeArea(2);
renderInInterior = true;
areaID = 2;
}
}
if (areaID == 2)
{
if (player.pos.z < -3800._f)
{
const bool currSide = player.pos.x > -300._f;
const bool prevSide = player.prevPos.x > -300._f;
if (currSide && !prevSide) renderInInterior = true;
if (prevSide && !currSide) renderInInterior = false;
}
if (renderInInterior && player.pos.y < -1450._f && !player.isUnderwater)
{
player.ChangeArea(0);
areaID = 0;
}
else
AREAS[0].showing = CAMERA->pos.x > hatchX;
}
AREAS[3].showing = areaID == 0 && cam.pos.x > hatchX;
AREAS[4].showing = areaID == 0 &&
(cam.pos.z < 11000._f || cam.pos.y < 4000._f) &&
(cam.pos.z < -4000._f || cam.pos.y < 100._f || Abs(cam.pos.x) > 3700._f);
return 1;
}
int Background::Render()
{
if (areaID != 0 && !renderInInterior)
{
const bool launchStarSpawing =
NEXT_ACTOR_UPDATE_FLAGS & Actor::UPDATE_DURING_STAR_SPAWNING;
if (!launchStarSpawing)
return 1;
}
wingCtrl.Render<3._f>(wingOffsets.begin(), wingOffsets.end());
model.Render(::scale);
cloudModel.mat4x3.Linear() = VIEW_MATRIX_ASR_3.Linear();
InsertionSort(cloudPositions, [](const Vector3& v0, const Vector3& v1)
{
const auto& zAxis = INV_VIEW_MATRIX_ASR_3.c2;
const auto& pos = INV_VIEW_MATRIX_ASR_3.c3;
return zAxis.Dot(v0 - pos) < zAxis.Dot(v1 - pos);
});
for (unsigned polygonID = 1; const Vector3& cloudPos : cloudPositions)
{
const int dist = std::abs(cloudPos.z.val);
static constexpr int fullAlphaDist = spawnDist.val - 0x2000000;
static constexpr int maxAlpha = 14;
if (dist > fullAlphaDist)
{
const int alpha = std::min(33 - ((dist - fullAlphaDist) >> 20), maxAlpha);
cloudModel.data.materials[0].SetAlpha(alpha);
}
else
cloudModel.data.materials[0].SetAlpha(maxAlpha);
cloudModel.data.materials[0].SetPolygonID(polygonID++);
cloudModel.mat4x3.c3 = VIEW_MATRIX_ASR_3(cloudPos);
static constinit Vector3 scale = {1._f, 8._f, 1._f};
cloudModel.data.Render(&cloudModel.mat4x3, &scale);
}
return 1;
}