This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathojsama.js
1804 lines (1551 loc) · 46.1 KB
/
ojsama.js
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// this is free and unencumbered software released into the public
// domain. refer to the attached UNLICENSE or http://unlicense.org/
//
// [![Build Status](
// https://travis-ci.org/Francesco149/ojsama.svg?branch=master)](
// https://travis-ci.org/Francesco149/ojsama)
//
// pure javascript implementation of
// https://github.com/Francesco149/oppai-ng intended to be easier
// to use and set up for js developers as well as more portable
// than straight up bindings at the cost of some performance
//
// installation:
// ----------------------------------------------------------------
// since this is a single-file library, you can just drop the file
// into your project:
// ```sh
// cd my/project
// curl https://waa.ai/ojsama > ojsama.js
// ```
//
// or include it directly in a html page:
// ```html
// <script type="text/javascript" src="ojsama.min.js"></script>
// ```
//
// it's also available as a npm package:
// ```sh
// npm install ojsama
// ```
//
// you can find full documentation of the code at
// http://hnng.moe/stuff/ojsama.html or simply read ojsama.js
//
// usage (nodejs):
// ----------------------------------------------------------------
// (change ./ojsama to ojsama if you installed through npm)
//
// ```js
// var readline = require("readline");
// var osu = require("./ojsama");
//
// var parser = new osu.parser();
// readline.createInterface({
// input: process.stdin, terminal: falsei
// })
// .on("line", parser.feed_line.bind(parser))
// .on("close", function() {
// console.log(osu.ppv2({map: parser.map}).toString());
// });
// ```
//
// ```sh
// $ curl https://osu.ppy.sh/osu/67079 | node minexample.js
// 133.24 pp (36.23 aim, 40.61 speed, 54.42 acc)
// ```
//
// advanced usage (nodejs with acc, mods, combo...):
// ----------------------------------------------------------------
// ```js
// var readline = require("readline");
// var osu = require("./ojsama");
//
// var mods = osu.modbits.none;
// var acc_percent;
// var combo;
// var nmiss;
//
// // get mods, acc, combo, misses from command line arguments
// // format: +HDDT 95% 300x 1m
// var argv = process.argv;
//
// for (var i = 2; i < argv.length; ++i)
// {
// if (argv[i].startsWith("+")) {
// mods = osu.modbits.from_string(argv[i].slice(1) || "");
// }
//
// else if (argv[i].endsWith("%")) {
// acc_percent = parseFloat(argv[i]);
// }
//
// else if (argv[i].endsWith("x")) {
// combo = parseInt(argv[i]);
// }
//
// else if (argv[i].endsWith("m")) {
// nmiss = parseInt(argv[i]);
// }
// }
//
// var parser = new osu.parser();
// readline.createInterface({
// input: process.stdin, terminal: false
// })
// .on("line", parser.feed_line.bind(parser))
// .on("close", function() {
// var map = parser.map;
// console.log(map.toString());
//
// if (mods) {
// console.log("+" + osu.modbits.string(mods));
// }
//
// var stars = new osu.diff().calc({map: map, mods: mods});
// console.log(stars.toString());
//
// var pp = osu.ppv2({
// stars: stars,
// combo: combo,
// nmiss: nmiss,
// acc_percent: acc_percent,
// });
//
// var max_combo = map.max_combo();
// combo = combo || max_combo;
//
// console.log(pp.computed_accuracy.toString());
// console.log(combo + "/" + max_combo + "x");
//
// console.log(pp.toString());
// });
// ```
//
// ```sh
// $ curl https://osu.ppy.sh/osu/67079 | node example.js
// TERRA - Tenjou no Hoshi ~Reimeiki~ [BMax] mapped by ouranhshc
//
// AR5 OD8 CS4 HP8
// 262 circles, 69 sliders, 5 spinners
// 469 max combo
//
// 4.33 stars (2.09 aim, 2.19 speed)
// 100.00% 0x100 0x50 0xmiss
// 469/469x
// 133.24 pp (36.23 aim, 40.61 speed, 54.42 acc)
//
// $ curl https://osu.ppy.sh/osu/67079 \
// | node example.js +HDDT 98% 400x 1m
// ...
// +HDDT
// 6.13 stars (2.92 aim, 3.11 speed)
// 97.92% 9x100 0x50 1xmiss
// 400/469x
// 266.01 pp (99.70 aim, 101.68 speed, 60.41 acc)
// ```
//
// usage (in the browser)
// ----------------------------------------------------------------
// ```html
// <!DOCTYPE html>
// <html>
// <head>
// <meta charset="utf-8" />
// <script type="text/javascript" src="ojsama.min.js"></script>
// <script type="text/javascript">
// function load_file()
// {
// var frame = document.getElementById("osufile");
// var contents = frame.contentWindow
// .document.body.childNodes[0].innerHTML;
//
// var parser = new osu.parser().feed(contents);
// console.log(parser.toString());
//
// var str = parser.map.toString();
// str += osu.ppv2({map: parser.map}).toString();
//
// document.getElementById("result").innerHTML = str;
// }
// </script>
// </head>
// <body>
// <iframe id="osufile" src="test.osu" onload="load_file();"
// style="display: none;">
// </iframe>
// <blockquote><pre id="result">calculating...</pre></blockquote>
// </body>
// </html>
// ```
//
// (this example assumes you have a test.osu beatmap in the same
// directory)
//
// performance
// ----------------------------------------------------------------
// this is around 50-60% slower than the C implementation and uses
// ~10 times more memory.
// ```sh
// $ busybox time -v node --use_strict test.js
// ...
// User time (seconds): 16.58
// System time (seconds): 0.43
// Percent of CPU this job got: 101%
// Elapsed (wall clock) time (h:mm:ss or m:ss): 0m 16.70s
// ...
// Maximum resident set size (kbytes): 314080
// Minor (reclaiming a frame) page faults: 20928
// Voluntary context switches: 72138
// Involuntary context switches: 16689
// ```
// # code documentation
// when used outside of node, a osu namespace will be exposed
// without polluting the global scope
var osu = {};
if (typeof exports !== "undefined") {
osu = exports;
}
(function() {
osu.VERSION_MAJOR = 1;
osu.VERSION_MINOR = 2;
osu.VERSION_PATCH = 1;
// internal utilities
// ----------------------------------------------------------------
// override console with nop when running in a browser
var log = {warn: Function.prototype};
if (typeof exports !== "undefined") {
log = console;
}
var array_toFixed = function(arr, n) {
var res = new Array(arr.length);
for (var i = 0; i < res.length; ++i) {
res[i] = arr[i].toFixed(n);
}
return res;
};
function isUndefined(val) {
return typeof val === "undefined";
}
// timing point
// ----------------------------------------------------------------
// defines parameters such as timing and sampleset for an interval.
// for pp calculation we only need time and ms_per_beat
//
// it can inherit from its preceeding point by having
// change = false and setting ms_per_beat to a negative value which
// represents the bpm multiplier as ```-100 * bpm_multiplier```
function timing(values) {
this.time = values.time || 0.0;
this.ms_per_beat = values.ms_per_beat;
if (this.ms_per_beat === undefined) {
this.ms_per_beat = 600.0;
}
this.change = values.change;
if (this.change === undefined) {
this.change = true;
}
}
timing.prototype.toString = function() {
return "{ time: " + this.time.toFixed(2) + ", "
+ "ms_per_beat: " + this.ms_per_beat.toFixed(2) + " }";
}
// hit objects
// ----------------------------------------------------------------
// partial structure of osu! hitobjects with just enough data for
// pp calculation
// bitmask constants for object types. note that the type can
// contain other flags so you should always check type with
// ```if (type & objtypes.circle) { ... }```
var objtypes = {
circle: 1<<0,
slider: 1<<1,
spinner: 1<<3,
};
// all we need from circles is their position. all positions
// stored in the objects are in playfield coordinates (512*384
// rect)
function circle(values) {
this.pos = values.pos || [0, 0];
}
circle.prototype.toString = function() {
return "pos: [" + array_toFixed(this.pos, 2) + "]";
};
// to calculate max combo we need to compute slider ticks
//
// the beatmap stores the distance travelled in one repetition and
// the number of repetitions. this is enough to calculate distance
// per tick using timing information and slider velocity.
//
// note that 1 repetition means no repeats (1 loop)
function slider(values) {
this.pos = values.pos || [0, 0];
this.distance = values.distance || 0.0;
this.repetitions = values.repetitions || 1;
}
slider.prototype.toString = function() {
return (
"pos: " + array_toFixed(this.pos, 2) + ", " +
"distance: " + this.distance.toFixed(2) + ", " +
"repetitions: " + this.repetitions
);
};
// generic hitobject
//
// the only common property is start time (in millisecond).
// object-specific properties are stored in data, which can be
// an instance of circle, slider, or null
function hitobject(values) {
this.time = values.time || 0.0;
this.type = values.type || 0;
if (!isUndefined(values.data)) this.data = values.data;
}
hitobject.prototype.typestr = function() {
var res = "";
if (this.type & objtypes.circle) res += "circle | ";
if (this.type & objtypes.slider) res += "slider | ";
if (this.type & objtypes.spinner) res += "spinner | ";
return res.substring(0, Math.max(0, res.length - 3));
};
hitobject.prototype.toString = function() {
return (
"{ time: " + this.time.toFixed(2) + ", " +
"type: " + this.typestr() +
(this.data ? ", " + this.data.toString() : "") +
" }"
);
};
// beatmap
// ----------------------------------------------------------------
// gamemode constants
var modes = {
std: 0,
};
// partial beatmap structure with just enough data for pp
// calculation
function beatmap() {
this.reset();
}
beatmap.prototype.reset = function() {
this.format_version = 1;
this.mode = modes.std;
this.title = this.title_unicode = "";
this.artist = this.artist_unicode = "";
this.creator = "";
this.version = "";
this.ar = undefined;
this.cs = this.od = this.hp = 5.0;
this.sv = this.tick_rate = 1.0;
this.ncircles = this.nsliders = this.nspinners = 0;
if (!this.objects) {
this.objects = [];
} else {
this.objects.length = 0;
}
if (!this.timing_points) {
this.timing_points = [];
} else {
this.timing_points.length = 0;
}
return this;
};
// max combo calculation
//
// this is given by ncircles + nspinners + nsliders * 2
// (heads and tails) + nsliderticks
//
// we approximate slider ticks by calculating the
// playfield pixels per beat for the current section
// and dividing the total distance travelled by
// pixels per beat. this gives us the number of beats,
// which multiplied by the tick rate gives use the
// tick count.
beatmap.prototype.max_combo = function() {
var res = this.ncircles + this.nspinners;
var tindex = -1;
var tnext = Number.NEGATIVE_INFINITY;
var px_per_beat = 0.0;
for (var i = 0; i < this.objects.length; ++i) {
var obj = this.objects[i];
if (!(obj.type & objtypes.slider)) {
continue;
}
// keep track of the current timing point without
// looping through all of them for every object
while (obj.time >= tnext) {
++tindex;
if (this.timing_points.length > tindex + 1) {
tnext = this.timing_points[tindex + 1].time;
} else {
tnext = Number.POSITIVE_INFINITY;
}
var t = this.timing_points[tindex];
var sv_multiplier = 1.0;
if (!t.change && t.ms_per_beat < 0) {
sv_multiplier = -100.0 / t.ms_per_beat;
}
// beatmaps older than format v8 don't apply
// the bpm multiplier to slider ticks
if (this.format_version < 8) {
px_per_beat = this.sv * 100.0;
} else {
px_per_beat = this.sv * 100.0 * sv_multiplier;
}
}
var sl = obj.data;
var num_beats = (sl.distance * sl.repetitions) / px_per_beat;
// subtract an epsilon to prevent accidental
// ceiling of whole values such as 2.00....1 -> 3 due
// to rounding errors
var ticks = Math.ceil(
(num_beats - 0.1) / sl.repetitions
* this.tick_rate
);
--ticks;
ticks *= sl.repetitions;
ticks += sl.repetitions + 1;
res += Math.max(0, ticks);
}
return res;
};
beatmap.prototype.toString = function() {
var res = this.artist + " - " + this.title + " [";
if (this.title_unicode || this.artist_unicode) {
res += "(" + this.artist_unicode + " - "
+ this.title_unicode + ")";
}
res += (
this.version + "] mapped by " + this.creator + "\n"
+ "\n"
+ "AR" + parseFloat(this.ar.toFixed(2)) + " "
+ "OD" + parseFloat(this.od.toFixed(2)) + " "
+ "CS" + parseFloat(this.cs.toFixed(2)) + " "
+ "HP" + parseFloat(this.hp.toFixed(2)) + "\n"
+ this.ncircles + " circles, "
+ this.nsliders + " sliders, "
+ this.nspinners + " spinners" + "\n"
+ this.max_combo() + " max combo" + "\n"
);
return res;
};
// beatmap parser
// ----------------------------------------------------------------
// partial .osu file parser built around pp calculation
function parser() {
// once you're done feeding data to the parser, you will find
// the parsed beatmap in this object
this.map = new beatmap();
this.reset();
}
parser.prototype.reset = function() {
this.map.reset();
// parser state: number of lines fed, last touched line,
// last touched substring and the current section name
//
// these can be used to debug syntax errors
this.nline = 0;
this.curline = "";
this.lastpos = "";
this.section = "";
return this;
};
// you can feed a single line or a whole block of text which
// will be split into lines. partial lines are not allowed
//
// both feed functions return the parser instance for easy
// chaining
parser.prototype.feed_line = function(line) {
this.curline = this.lastpos = line;
++this.nline;
// comments
if (line.startsWith(" ") || line.startsWith("_")) {
return this;
}
// now that we've handled space comments we can trim space
line = this.curline = line.trim();
if (line.length <= 0) {
return this;
}
// c++ style comments
if (line.startsWith("//")) {
return this;
}
// [SectionName]
if (line.startsWith("[")) {
// on old maps there's no ar and ar = od
if (this.section == "Difficulty" && isUndefined(this.map.ar)) {
this.map.ar = this.map.od;
}
this.section = line.substring(1, line.length - 1);
return this;
}
if (!line) {
return this;
}
switch (this.section) {
case "Metadata": this._metadata(); break;
case "General": this._general(); break;
case "Difficulty": this._difficulty(); break;
case "TimingPoints": this._timing_points(); break;
case "HitObjects": this._objects(); break;
default:
var fmtpos = line.indexOf("file format v");
if (fmtpos< 0) {
break;
}
this.map.format_version = parseInt(line.substring(fmtpos + 13));
break;
}
return this;
};
parser.prototype.feed = function(str) {
var lines = lines = str.split("\n");
for (var i = 0; i < lines.length; ++i) {
this.feed_line(lines[i]);
}
return this;
};
// returns the parser state formatted into a string. useful
// for debugging syntax errors
parser.prototype.toString = function() {
return (
"at line " + this.nline + "\n" +
this.curline + "\n" +
"-> " + this.lastpos + " <-"
);
};
// _(internal)_ parser utilities
parser.prototype._setpos = function(str) {
this.lastpos = str.trim();
return this.lastpos;
};
parser.prototype._warn = function() {
log.warn.apply(null, Array.prototype.slice.call(arguments));
log.warn(this.toString());
};
parser.prototype._property = function() {
var s = this.curline.split(":", 2);
s[0] = this._setpos(s[0]);
s[1] = this._setpos(s[1]);
return s;
};
// _(internal)_ line parsers for each section
parser.prototype._metadata = function() {
var p = this._property();
switch (p[0]) {
case "Title":
this.map.title = p[1];
break;
case "TitleUnicode":
this.map.title_unicode = p[1];
break;
case "Artist":
this.map.artist = p[1];
break;
case "ArtistUnicode":
this.map.artist_unicode = p[1];
break;
case "Creator":
this.map.creator = p[1];
break;
case "Version":
this.map.version = p[1];
break;
}
};
parser.prototype._general = function() {
var p = this._property();
if (p[0] !== "Mode") {
return;
}
this.map.mode = parseInt(this._setpos(p[1]));
};
parser.prototype._difficulty = function() {
var p = this._property();
switch (p[0]) {
case "CircleSize":
this.map.cs = parseFloat(this._setpos(p[1]));
break;
case "OverallDifficulty":
this.map.od = parseFloat(this._setpos(p[1]));
break;
case "ApproachRate":
this.map.ar = parseFloat(this._setpos(p[1]));
break;
case "HPDrainRate":
this.map.hp = parseFloat(this._setpos(p[1]));
break;
case "SliderMultiplier":
this.map.sv = parseFloat(this._setpos(p[1]));
break;
case "SliderTickRate":
this.map.tick_rate = parseFloat(this._setpos(p[1]));
break;
}
};
parser.prototype._timing_points = function() {
var s = this.curline.split(",");
if (s.length > 8) {
this._warn("timing point with trailing values");
} else if (s.length < 2) {
this._warn("ignoring malformed timing point");
return;
}
var t = new timing({
time: parseFloat(this._setpos(s[0])),
ms_per_beat: parseFloat(this._setpos(s[1])),
});
if (s.length >= 7) {
t.change = s[6].trim() !== "0";
}
this.map.timing_points.push(t);
};
parser.prototype._objects = function() {
var s = this.curline.split(",");
var d;
if (s.length > 11) {
this._warn("object with trailing values");
} else if (s.length < 4) {
this._warn("ignoring malformed hitobject");
return;
}
var obj = new hitobject({
time: parseFloat(this._setpos(s[2])),
type: parseInt(this._setpos(s[3])),
});
if (isNaN(obj.time) || isNaN(obj.type)) {
this._warn("ignoring malformed hitobject");
return;
}
if ((obj.type & objtypes.circle) != 0) {
++this.map.ncircles;
d = obj.data = new circle({
pos: [
parseFloat(this._setpos(s[0])),
parseFloat(this._setpos(s[1])),
]
});
if (isNaN(d.pos[0]) || isNaN(d.pos[1])) {
this._warn("ignoring malformed circle");
return;
}
} else if ((obj.type & osu.objtypes.spinner) != 0) {
++this.map.nspinners;
} else if ((obj.type & osu.objtypes.slider) != 0) {
if (s.length < 8) {
this._warn("ignoring malformed slider");
return;
}
++this.map.nsliders;
d = obj.data = new slider({
pos: [
parseFloat(this._setpos(s[0])),
parseFloat(this._setpos(s[1])),
],
repetitions: parseInt(this._setpos(s[6])),
distance: parseFloat(this._setpos(s[7])),
});
if (isNaN(d.pos[0]) || isNaN(d.pos[1]) ||
isNaN(d.repetitions) || isNaN(d.distance))
{
this._warn("ignoring malformed slider");
return;
}
}
this.map.objects.push(obj);
}
// difficulty calculation
// ----------------------------------------------------------------
// mods bitmask constants
// NOTE: td is touch device, but it's also the value for the
// legacy no video mod
var modbits = {
nomod: 0,
nf: 1<<0,
ez: 1<<1,
td: 1<<2,
hd: 1<<3,
hr: 1<<4,
dt: 1<<6,
ht: 1<<8,
nc: 1<<9,
fl: 1<<10,
so: 1<<12,
};
// construct the mods bitmask from a string such as "HDHR"
modbits.from_string = function(str) {
var mask = 0;
str = str.toLowerCase();
while (str != "") {
var nchars = 1;
for (var property in modbits) {
if (property.length != 2) {
continue;
}
if (!modbits.hasOwnProperty(property)) {
continue;
}
if (str.startsWith(property)) {
mask |= modbits[property];
nchars = 2;
break;
}
}
str = str.slice(nchars);
}
return mask;
};
// convert mods bitmask into a string, such as "HDHR"
modbits.string = function(mods) {
var res = "";
for (var property in modbits) {
if (property.length != 2) {
continue;
}
if (!modbits.hasOwnProperty(property)) {
continue;
}
if (mods & modbits[property]) {
res += property.toUpperCase();
}
}
if (res.indexOf("DT") >= 0 && res.indexOf("NC") >= 0) {
res = res.replace("DT", "");
}
return res;
};
modbits.speed_changing = modbits.dt | modbits.ht | modbits.nc;
modbits.map_changing = modbits.hr | modbits.ez | modbits.speed_changing;
// _(internal)_
// osu!standard stats constants
var OD0_MS = 80;
var OD10_MS = 20;
var AR0_MS = 1800.0;
var AR5_MS = 1200.0;
var AR10_MS = 450.0;
var OD_MS_STEP = (OD0_MS - OD10_MS) / 10.0;
var AR_MS_STEP1 = (AR0_MS - AR5_MS) / 5.0;
var AR_MS_STEP2 = (AR5_MS - AR10_MS) / 5.0;
// _(internal)_
// utility functions to apply speed and flat multipliers to
// stats where speed changes apply (ar and od)
function modify_ar(base_ar, speed_mul, multiplier) {
var ar = base_ar;
ar *= multiplier;
// convert AR into milliseconds window
var arms = (
ar < 5.0 ?
AR0_MS - AR_MS_STEP1 * ar
: AR5_MS - AR_MS_STEP2 * (ar - 5.0)
);
// stats must be capped to 0-10 before HT/DT which
// brings them to a range of -4.42->11.08 for OD and
// -5->11 for AR
arms = Math.min(AR0_MS, Math.max(AR10_MS, arms));
arms /= speed_mul;
ar = (
arms > AR5_MS ?
(AR0_MS - arms) / AR_MS_STEP1
: 5.0 + (AR5_MS - arms) / AR_MS_STEP2
);
return ar;
}
function modify_od(base_od, speed_mul, multiplier) {
var od = base_od;
od *= multiplier;
var odms = OD0_MS - Math.ceil(OD_MS_STEP * od);
odms = Math.min(OD0_MS, Math.max(OD10_MS, odms));
odms /= speed_mul;
od = (OD0_MS - odms) / OD_MS_STEP;
return od;
}
// stores osu!standard beatmap stats
function std_beatmap_stats(values) {
this.ar = values.ar;
this.od = values.od;
this.hp = values.hp;
this.cs = values.cs;
this.speed_mul = 1.0;
// previously calculated mod combinations are cached in a map
this._mods_cache = {};
}
// applies difficulty modifiers to a map's ar, od, cs, hp and
// returns the modified stats and the speed multiplier.
//
// unspecified stats are ignored and not returned
std_beatmap_stats.prototype.with_mods = function(mods) {
if (this._mods_cache[mods]) {
return this._mods_cache[mods];
}
var stats = this._mods_cache[mods] = new std_beatmap_stats(this);
if (!(mods & modbits.map_changing)) {
return stats;
}
if (mods & (modbits.dt|modbits.nc)) {
stats.speed_mul = 1.5;
}
if (mods & modbits.ht) {
stats.speed_mul *= 0.75;
}
var od_ar_hp_multiplier = 1.0;
if (mods & modbits.hr) od_ar_hp_multiplier = 1.4;
if (mods & modbits.ez) od_ar_hp_multiplier *= 0.5;
if (stats.ar) {
stats.ar = modify_ar(stats.ar, stats.speed_mul, od_ar_hp_multiplier);
}
if (stats.od) {
stats.od = modify_od(stats.od, stats.speed_mul, od_ar_hp_multiplier);
}
if (stats.cs) {
if (mods & modbits.hr) stats.cs *= 1.3;
if (mods & modbits.ez) stats.cs *= 0.5;
stats.cs = Math.min(10.0, stats.cs);
}
if (stats.hp) {
stats.hp *= od_ar_hp_multiplier;
stats.hp = Math.min(10.0, stats.hp);
}
return stats;
};
// osu! standard hit object with difficulty calculation values
// obj is the underlying hitobject
function std_diff_hitobject(obj) {
this.obj = obj;
this.reset();
}
std_diff_hitobject.prototype.reset = function() {
this.strains = [ 0.0, 0.0 ];
this.normpos = [ 0.0, 0.0 ];
this.angle = 0.0;
this.is_single = false;
this.delta_time = 0.0;
this.d_distance = 0.0;
return this;
};
std_diff_hitobject.prototype.toString = function() {
return (
"{ strains: [" + array_toFixed(this.strains, 2)
+ "], normpos: [" + array_toFixed(this.normpos, 2)
+ "], is_single: " + this.is_single + " }"
);
};
// _(internal)_
// 2D point operations
function vec_sub(a, b) { return [a[0] - b[0], a[1] - b[1]]; }
function vec_mul(a, b) { return [a[0] * b[0], a[1] * b[1]]; }
function vec_len(v) { return Math.sqrt(v[0] * v[0] + v[1] * v[1]); }
function vec_dot(a, b) { return a[0] * b[0] + a[1] * b[1]; }
// _(internal)_
// difficulty calculation constants
var DIFF_SPEED = 0;
var DIFF_AIM = 1;
var SINGLE_SPACING = 125.0;
var DECAY_BASE = [ 0.3, 0.15 ];
var WEIGHT_SCALING = [ 1400.0, 26.25 ];
var DECAY_WEIGHT = 0.9;
var STRAIN_STEP = 400.0;
var CIRCLESIZE_BUFF_THRESHOLD = 30.0;
var STAR_SCALING_FACTOR = 0.0675;
var PLAYFIELD_SIZE = [512.0, 384.0];
var PLAYFIELD_CENTER = vec_mul(PLAYFIELD_SIZE, [0.5, 0.5]);
var EXTREME_SCALING_FACTOR = 0.5;
// osu!standard difficulty calculator
//
// does not account for sliders because slider calculations are
// expensive and not worth the small accuracy increase
function std_diff() {
this.objects = [];
this.reset();
// make some parameters persist so they can be
// re-used in subsequent calls if no new value is specified
this.map = undefined;
this.mods = modbits.nomod;
this.singletap_threshold = 125.0;
}
std_diff.prototype.reset = function() {
// star rating
this.total = 0.0;
this.aim = 0.0;
this.aim_difficulty = 0.0;
this.aim_length_bonus = 0.0;
this.speed = 0.0;
this.speed_difficulty = 0.0;
this.speed_length_bonus = 0.0;
// number of notes that are seen as singletaps by the
// difficulty calculator
this.nsingles = 0;
// number of notes that are faster than the interval given
// in calc(). these singletap statistic are not required in
// star rating, but they are a free byproduct of the
// calculation which could be useful
this.nsingles_threshold = 0;