-
Notifications
You must be signed in to change notification settings - Fork 1
/
CatchBeatmapProcessor.ts
273 lines (210 loc) · 7.96 KB
/
CatchBeatmapProcessor.ts
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
import { CatchBeatmap } from './CatchBeatmap';
import {
CatchHitObject,
PalpableHitObject,
Fruit,
JuiceStream,
JuiceDroplet,
JuiceTinyDroplet,
BananaShower,
Banana,
} from '../Objects';
import {
FastRandom,
BeatmapProcessor,
IHitObject,
ModBitwise,
HitType,
SortHelper,
MathUtils,
} from 'osu-classes';
import { CatchPlayfield } from '../UI/CatchPlayfield';
export class CatchBeatmapProcessor extends BeatmapProcessor {
static RNG_SEED = 1337;
static BASE_SPEED = 1;
private _lastX: number | null = null;
private _lastStartTime = 0;
private _rng = new FastRandom(CatchBeatmapProcessor.RNG_SEED);
postProcess(beatmap: CatchBeatmap): CatchBeatmap {
super.postProcess(beatmap);
this._applyXOffsets(beatmap);
return beatmap;
}
private _applyXOffsets(beatmap: CatchBeatmap) {
const shouldApplyHR = beatmap.mods.has(ModBitwise.HardRock);
const hitObjects = beatmap.hitObjects;
hitObjects.forEach((hitObject) => {
hitObject.offsetX = 0;
if (hitObject.hitType & HitType.Normal) {
const fruit = hitObject as Fruit;
if (shouldApplyHR) {
this._applyHardRockOffset(fruit);
}
}
if (hitObject.hitType & HitType.Slider) {
const juiceStream = hitObject as JuiceStream;
const controlPoints = juiceStream.path.controlPoints;
this._lastX = Math.fround(juiceStream.originalX
+ controlPoints[controlPoints.length - 1].position.x,
);
this._lastStartTime = juiceStream.startTime;
const nestedHitObjects = juiceStream.nestedHitObjects;
nestedHitObjects.forEach((nested) => {
const juice = (nested as IHitObject) as CatchHitObject;
juice.offsetX = 0;
if (nested instanceof JuiceTinyDroplet) {
juice.offsetX = MathUtils.clamp(
this._rng.nextInt(-20, 20),
-juice.originalX,
Math.fround(CatchPlayfield.PLAYFIELD_WIDTH - juice.originalX),
);
}
else if (nested instanceof JuiceDroplet) {
// Osu!stable retrieved a random droplet rotation.
this._rng.next();
}
});
}
if (hitObject.hitType & HitType.Spinner) {
const bananaShower = hitObject as BananaShower;
const bananas = bananaShower.nestedHitObjects as Banana[];
bananas.forEach((banana) => {
banana.offsetX = Math.fround(
this._rng.nextDouble() * CatchPlayfield.PLAYFIELD_WIDTH,
);
// Osu!stable retrieved a random banana type.
this._rng.next();
// Osu!stable retrieved a random banana rotation.
this._rng.next();
// Osu!stable retrieved a random banana colour.
this._rng.next();
});
}
});
this._initialiseHyperDash(beatmap);
}
private _applyHardRockOffset(hitObject: CatchHitObject): void {
let offsetPosition = hitObject.originalX;
const startTime = hitObject.startTime;
if (this._lastX === null) {
this._lastX = offsetPosition;
this._lastStartTime = startTime;
return;
}
const xDiff = Math.fround(offsetPosition - this._lastX);
const timeDiff = Math.trunc(startTime - this._lastStartTime);
if (timeDiff > 1000) {
this._lastX = offsetPosition;
this._lastStartTime = startTime;
return;
}
if (xDiff === 0) {
offsetPosition = this._applyRandomOffset(offsetPosition, timeDiff / 4);
hitObject.offsetX = Math.fround(offsetPosition - hitObject.originalX);
return;
}
if (Math.abs(xDiff) < Math.trunc(timeDiff / 3)) {
offsetPosition = this._applyOffset(offsetPosition, xDiff);
}
hitObject.offsetX = Math.fround(offsetPosition - hitObject.originalX);
this._lastX = offsetPosition;
this._lastStartTime = startTime;
}
/**
* Applies a random offset in a random direction to a position, ensuring
* that the final position remains within the boundary of the playfield.
* @param position The position which the offset should be applied to.
* @param maxOffset The maximum offset, cannot exceed 20px.
* @param rng The random number generator.
*/
private _applyRandomOffset(position: number, maxOffset: number): number {
const left = !this._rng.nextBool();
const rand = Math.min(
20,
Math.fround(this._rng.nextInt(0, Math.max(0, maxOffset))),
);
const positionPlusRand = Math.fround(position + rand);
const positionMinusRand = Math.fround(position - rand);
if (left) {
// Clamp to the left bound
return positionMinusRand >= 0 ? positionMinusRand : positionPlusRand;
}
// Clamp to the right bound
return positionPlusRand <= CatchPlayfield.PLAYFIELD_WIDTH
? positionPlusRand
: positionMinusRand;
}
/**
* Applies an offset to a position, ensuring that the
* final position remains within the boundary of the playfield.
* @param position The position which the offset should be applied to.
* @param amount The amount to offset by.
*/
private _applyOffset(position: number, amount: number): number {
const positionPlusAmount = Math.fround(position + amount);
if (amount > 0) {
// Clamp to the right bound
if (positionPlusAmount < CatchPlayfield.PLAYFIELD_WIDTH) {
return positionPlusAmount;
}
}
// Clamp to the left bound
else if (positionPlusAmount > 0) {
return positionPlusAmount;
}
return position;
}
private _initialiseHyperDash(beatmap: CatchBeatmap): void {
const palpableObjects: PalpableHitObject[] = [];
const hitObjects = beatmap.hitObjects;
hitObjects.forEach((hitObject) => {
if (hitObject.hitType & HitType.Normal) {
palpableObjects.push(hitObject as PalpableHitObject);
}
else if (hitObject.hitType & HitType.Slider) {
const juiceStream = hitObject as JuiceStream;
const nestedHitObjects = juiceStream.nestedHitObjects;
nestedHitObjects.forEach((nested) => {
if (!(nested instanceof JuiceTinyDroplet)) {
palpableObjects.push((nested as IHitObject) as PalpableHitObject);
}
});
}
});
SortHelper.introSort(palpableObjects, (a, b) => a.startTime - b.startTime);
let halfCatcherWidth = CatchPlayfield.calculateCatcherWidth(beatmap.difficulty) / 2;
/**
* TODO: This is wrong. osu!stable calculated hyperdashes
* using the full catcher size, excluding the margins.
* This should theoretically cause impossible scenarios, but practically,
* likely due to the size of the playfield, it doesn't seem possible.
* For now, to bring gameplay (and diffcalc!) completely in-line with stable,
* this code also uses the full catcher size.
*/
halfCatcherWidth /= CatchPlayfield.ALLOWED_CATCH_RANGE;
let lastDirection = 0;
let lastExcess = halfCatcherWidth;
for (let i = 0, len = palpableObjects.length - 1; i < len; ++i) {
const current = palpableObjects[i];
const next = palpableObjects[i + 1];
// Reset variables in-case values have changed (e.g. after applying HR)
current.hyperDashTarget = null;
current.distanceToHyperDash = 0;
const thisDirection = next.effectiveX > current.effectiveX ? 1 : -1;
// 1/4th of a frame of grace time, taken } from osu-stable
const timeToNext = next.startTime - current.startTime - Math.fround(Math.fround(1000 / 60) / 4);
const distanceToNext = Math.abs(Math.fround(next.effectiveX - current.effectiveX)) -
(lastDirection === thisDirection ? lastExcess : halfCatcherWidth);
const distanceToHyper = Math.fround(timeToNext * CatchBeatmapProcessor.BASE_SPEED - distanceToNext);
if (distanceToHyper < 0) {
current.hyperDashTarget = next;
lastExcess = halfCatcherWidth;
}
else {
current.distanceToHyperDash = distanceToHyper;
lastExcess = Math.max(0, Math.min(distanceToHyper, halfCatcherWidth));
}
lastDirection = thisDirection;
}
}
}