/
oppai.c
2859 lines (2414 loc) · 68.6 KB
/
oppai.c
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/
* ------------------------------------------------------------------------
* this is a pure C89 rewrite of oppai, my osu! difficulty and pp
* calculator. it's meant to be tiny and easy to include in your projects
* without pulling in dependencies.
* ------------------------------------------------------------------------
* usage:
*
* #define OPPAI_IMPLEMENTATION and include this file.
* if multiple compilation units need to include oppai, only define
* OPPAI_IMPLEMENTATION in one of them
*
* see the interface below this comment for detailed documentation
* ------------------------------------------------------------------------
* #define OPPAI_IMPLEMENTATION
* #include "../oppai.c"
*
* int main() {
* ezpp_t ez;
* ezpp_init(&ez);
* ez.mods = MODS_HD | MODS_DT;
* ezpp(&ez, "-");
* printf("%gpp\n", ez.pp);
* return 0;
* }
* ------------------------------------------------------------------------
* $ gcc test.c
* $ cat /path/to/file.osu | ./a.out
* ...
*/
#include <stdio.h>
#if defined(_WIN32) && !defined(OPPAI_IMPLEMENTATION)
#ifdef OPPAI_EXPORT
#define OPPAIAPI __declspec(dllexport)
#else
#define OPPAIAPI __declspec(dllimport)
#endif
#else
#define OPPAIAPI
#endif
#ifdef OPPAI_EXPORT
#define OPPAI_IMPLEMENTATION
#endif
OPPAIAPI void oppai_version(int* major, int* minor, int* patch);
OPPAIAPI char* oppai_version_str();
/* simple interface ---------------------------------------------------- */
struct ezpp;
typedef struct ezpp ezpp_t;
/* populate ezpp_t with default settings */
OPPAIAPI void ezpp_init(ezpp_t* ez);
/*
* parse map and calculate difficulty and pp with advanced parameters,
* see struct pp_params
*
* - if map is "-" the map is read from standard input
* - if data_size is specified in ez, map is interpreted as raw beatmap
* data in memory
*/
OPPAIAPI int ezpp(ezpp_t* ez, char* map);
/*
* - if data_size is set, ezpp will interpret map as raw .osu file data
* - mode defaults to MODE_STD
* - mods default to MODS_NOMOD
* - combo defaults to full combo
* - nmiss defaults to 0
* - score_version defaults to PP_DEFAULT_SCORING
* - if accuracy_percent is set, n300/100/50 are automatically
* calculated and stored
* - if n300/100/50 are set, accuracy_percent is automatically
* calculated and stored
* - if none of the above are set, SS (100%) is assumed
*/
struct ezpp {
/* inputs */
int data_size;
float ar_override, od_override, cs_override;
int mode_override;
int mode;
int mods;
int combo;
int nmiss;
int score_version;
float accuracy_percent;
int n300, n100, n50;
/* outputs */
float stars;
float aim_stars;
float speed_stars;
float pp, aim_pp, speed_pp, acc_pp;
};
/* errors -------------------------------------------------------------- */
/*
* all functions that return int can return errors in the form
* of a negative value. check if the return value is < 0 and call
* errstr to get the error message
*/
#define ERR_MORE (-1)
#define ERR_SYNTAX (-2)
#define ERR_TRUNCATED (-3)
#define ERR_NOTIMPLEMENTED (-4)
#define ERR_IO (-5)
#define ERR_FORMAT (-6)
#define ERR_OOM (-7)
OPPAIAPI char* errstr(int err);
/* array --------------------------------------------------------------- */
/*
* array_t(mytype) is a type-safe resizable array with mytype elements
* you can use array_* macros to operate on it
*
* in case of out-of-memory, operations that can grow the array don't do
* anything
*/
#define array_t(type) \
struct { \
int cap; \
int len; \
type* data; \
}
#define array_reserve(arr, n) \
array_reserve_i(n, array_unpack(arr))
#define array_free(arr) \
array_free_i(array_unpack(arr))
#define array_alloc(arr) \
(array_reserve((arr), (arr)->len + 1) \
? &(arr)->data[(arr)->len++] \
: 0)
#define array_append(arr, x) \
(array_reserve((arr), (arr)->len + 1) \
? ((arr)->data[(arr)->len++] = (x), 1) \
: 0)
/* internal helpers, not to be used directly */
#define array_unpack(arr) \
&(arr)->cap, \
&(arr)->len, \
(void**)&(arr)->data, \
(int)sizeof((arr)->data[0])
OPPAIAPI int array_reserve_i(int n, int* cap, int* len, void** data,
int esize);
OPPAIAPI void array_free_i(int* cap, int* len, void** data, int esize);
/* memory arena -------------------------------------------------------- */
/*
* very simple allocator for when you want to allocate a bunch of stuff
* and free it all at once. reduces malloc overhead by pre-allocating big
* contiguous chunks of memory
*
* arena_t must be initialized to zero
* arena_reserve and arena_alloc will return 0 on failure (out of memory)
*/
#define ARENA_ALIGN sizeof(void*)
#define ARENA_BLOCK_SIZE 4096
typedef struct {
char* block;
char* end_of_block;
array_t(char*) blocks;
} arena_t;
/* ensures that there are at least min_size bytes reserved */
OPPAIAPI int arena_reserve(arena_t* arena, int min_size);
OPPAIAPI void* arena_alloc(arena_t* arena, int size);
OPPAIAPI char* arena_strndup(arena_t* m, char* s, int n);
OPPAIAPI void arena_free(arena_t* arena);
/* beatmap utils ------------------------------------------------------- */
/* object types used in struct object */
#define OBJ_CIRCLE (1<<0)
#define OBJ_SLIDER (1<<1)
#define OBJ_SPINNER (1<<3)
#define SOUND_NONE 0
#define SOUND_NORMAL (1<<0)
#define SOUND_WHISTLE (1<<1)
#define SOUND_FINISH (1<<2)
#define SOUND_CLAP (1<<3)
/* data about a single hitobject */
typedef struct object {
float time; /* milliseconds */
int type;
/* only parsed for taiko maps */
int nsound_types;
int* sound_types;
/* only used by d_calc */
float normpos[2];
float strains[2];
int is_single; /* 1 if diff calc sees this as a singletap */
float pos[2];
float distance; /* only for sliders */
int repetitions;
} object_t;
/* timing point */
typedef struct timing {
float time; /* milliseconds */
float ms_per_beat;
int change; /* if 0, ms_per_beat is -100.0f * sv_multiplier */
} timing_t;
#define MODE_STD 0
#define MODE_TAIKO 1
typedef struct beatmap {
int format_version;
int mode;
int original_mode; /* the mode the beatmap was meant for */
char* title;
char* title_unicode;
char* artist;
char* artist_unicode;
char* creator;
char* version;
int nobjects;
object_t* objects;
int ntiming_points;
timing_t* timing_points;
int ncircles, nsliders, nspinners;
float hp, cs, od, ar, sv;
float tick_rate;
} beatmap_t;
/* beatmap parser ------------------------------------------------------ */
/* non-null terminated string, used internally for parsing */
typedef struct slice {
char* start;
char* end; /* *(end - 1) is the last character */
} slice_t;
#define PARSER_OVERRIDE_MODE (1<<0) /* mode_override */
#define PARSER_FOUND_AR (1<<1)
/* beatmap parser's state */
typedef struct parser {
int flags;
int mode_override;
/*
* if a parsing error occurs last line and portion of the line
* that was being parsed are stored in these two slices
*/
slice_t lastpos;
slice_t lastline;
char buf[65536]; /* used to buffer data from the beatmap file */
char section[64]; /* current section */
/* internal allocators */
arena_t arena;
array_t(object_t) objects;
array_t(timing_t) timing_points;
beatmap_t* b;
} parser_t;
OPPAIAPI int p_init(parser_t* pa);
OPPAIAPI void p_free(parser_t* pa);
/*
* parses a beatmap file and stores results in b.
*
* NOTE: b is valid only as long as pa is not deallocated or
* reused. if you need to store maps for longer than the
* parser's lifetime, you will have to manually copy.
*
* returns n. bytes processed on success, < 0 on failure
*/
OPPAIAPI int p_map(parser_t* pa, beatmap_t* b, FILE* f);
OPPAIAPI int p_map_mem(parser_t* pa, beatmap_t* b, char* data,
int data_size);
/* mods utils ---------------------------------------------------------- */
#define MODS_NOMOD 0
#define MODS_NF (1<<0)
#define MODS_EZ (1<<1)
#define MODS_TD (1<<2)
#define MODS_HD (1<<3)
#define MODS_HR (1<<4)
#define MODS_SD (1<<5)
#define MODS_DT (1<<6)
#define MODS_RX (1<<7)
#define MODS_HT (1<<8)
#define MODS_NC (1<<9)
#define MODS_FL (1<<10)
#define MODS_AT (1<<11)
#define MODS_SO (1<<12)
#define MODS_AP (1<<13)
#define MODS_PF (1<<14)
#define MODS_KEY4 (1<<15) /* TODO: what are these abbreviated to? */
#define MODS_KEY5 (1<<16)
#define MODS_KEY6 (1<<17)
#define MODS_KEY7 (1<<18)
#define MODS_KEY8 (1<<19)
#define MODS_FADEIN (1<<20)
#define MODS_RANDOM (1<<21)
#define MODS_CINEMA (1<<22)
#define MODS_TARGET (1<<23)
#define MODS_KEY9 (1<<24)
#define MODS_KEYCOOP (1<<25)
#define MODS_KEY1 (1<<26)
#define MODS_KEY3 (1<<27)
#define MODS_KEY2 (1<<28)
#define MODS_SCOREV2 (1<<29)
#define MODS_TOUCH_DEVICE MODS_TD
#define MODS_NOVIDEO MODS_TD /* never forget */
#define MODS_SPEED_CHANGING (MODS_DT | MODS_HT | MODS_NC)
#define MODS_MAP_CHANGING (MODS_HR | MODS_EZ | MODS_SPEED_CHANGING)
/* beatmap stats after applying mods to them */
typedef struct beatmap_stats {
float ar, od, cs, hp;
float speed; /* multiplier */
float odms;
} beatmap_stats_t;
/* flags bits for mods_apply */
#define APPLY_AR (1<<0)
#define APPLY_OD (1<<1)
#define APPLY_CS (1<<2)
#define APPLY_HP (1<<3)
#define APPLY_ALL (~0)
/*
* calculates beatmap stats with mods applied.
* * s should initially contain the base stats
* * flags specifies which stats are touched
* * initial speed will always be automatically set to 1
*
* returns 0 on success, or < 0 for errors
*
* example:
*
* beatmap_stats_t s;
* s.ar = 9;
* mods_apply_m(MODE_STD, MODS_DT, &s, APPLY_AR);
* // s.ar is now 10.33f, s.speed is now 1.5f
*/
OPPAIAPI
int mods_apply_m(int mode, int mods, beatmap_stats_t* s, int flags);
/* legacy function, calls mods_apply(MODE_STD, mods, s, flags) */
OPPAIAPI void mods_apply(int mods, beatmap_stats_t* s, int flags);
/* diff calc ----------------------------------------------------------- */
/*
* difficulty calculation state. just like with the parser, each
* instance can be re-used in subsequent calls to d_calc
*/
typedef struct diff_calc {
float speed_mul;
float interval_end;
float max_strain;
array_t(float) highest_strains;
beatmap_t* b;
/*
* set this to the milliseconds interval for the maximum bpm
* you consider singletappable. defaults to 125 = 240 bpm 1/2
* ((60000 / 240) / 2)
*/
float singletap_threshold;
/* calls to d_calc will store results here */
float total;
float aim;
float speed;
int nsingles;
int nsingles_threshold;
} diff_calc_t;
OPPAIAPI int d_init(diff_calc_t* d);
OPPAIAPI void d_free(diff_calc_t* d);
OPPAIAPI int d_calc(diff_calc_t* d, beatmap_t* b, int mods);
/* pp calc ------------------------------------------------------------- */
typedef struct pp_calc {
/* ppv2 will store results here */
float total, aim, speed, acc;
float accuracy; /* 0.0f - 1.0f */
} pp_calc_t;
/* default scoring system used by ppv2() and ppv2p() */
#define PP_DEFAULT_SCORING 1
/*
* simplest possible call, calculates ppv2 for SS
*
* this also works for other modes by ignoring some parameters:
* - taiko only uses pp, mode, speed, max_combo, base_od, mods
*/
OPPAIAPI
int ppv2(pp_calc_t* pp, int mode, float aim, float speed,
float base_ar, float base_od, int max_combo, int nsliders, int ncircles,
int nobjects, int mods);
/* simplest possible call for taiko ppv2 SS */
OPPAIAPI
int taiko_ppv2(pp_calc_t* pp, float speed, int max_combo,
float base_od, int mods);
/* parameters for ppv2p */
typedef struct pp_params {
/* required parameters */
float aim, speed;
float base_ar, base_od;
int max_combo;
int nsliders; /* required for scorev1 only */
int ncircles; /* ^ */
int nobjects;
/* optional parameters */
int mode; /* defaults to MODE_STD */
int mods; /* defaults to MODS_NOMOD */
int combo; /* defaults to FC */
int n300, n100, n50; /* defaults to SS */
int nmiss; /* defaults to 0 */
int score_version; /* defaults to PP_DEFAULT_SCORING */
} pp_params_t;
/*
* initialize struct pp_params with the default values.
* required values are left untouched
*/
OPPAIAPI void pp_init(pp_params_t* p);
/* calculate ppv2 with advanced parameters, see struct pp_params */
OPPAIAPI int ppv2p(pp_calc_t* pp, pp_params_t* p);
/*
* same as ppv2p but fills params automatically with the map's
* base_ar, base_od, max_combo, nsliders, ncircles, nobjects
* so you only need to provide aim and speed
*/
OPPAIAPI int b_ppv2p(beatmap_t* map, pp_calc_t* pp, pp_params_t* p);
/* same as ppv2 but fills params like b_ppv2p */
OPPAIAPI
int b_ppv2(beatmap_t* map, pp_calc_t* pp, float aim, float speed,
int mods);
/* --------------------------------------------------------------------- */
/* calculate accuracy (0.0f - 1.0f) */
OPPAIAPI float acc_calc(int n300, int n100, int n50, int misses);
/* calculate taiko accuracy (0.0f - 1.0f) */
OPPAIAPI float taiko_acc_calc(int n300, int n150, int nmisses);
/* round percent accuracy to closest amount of 300s, 100s, 50s */
OPPAIAPI
void acc_round(float acc_percent, int nobjects, int nmisses, int* n300,
int* n100, int* n50);
/* round percent accuracy to closest amount of 300s and 150s (taiko) */
OPPAIAPI
void taiko_acc_round(float acc_percent, int nobjects, int nmisses,
int* n300, int* n150);
/* --------------------------------------------------------------------- */
#define round_oppai(x) (float)floor((x) + 0.5f)
#define mymin(a, b) ((a) < (b) ? (a) : (b))
#define mymax(a, b) ((a) > (b) ? (a) : (b))
#define al_min mymin
#define al_max mymax
/* ##################################################################### */
/* ##################################################################### */
/* ##################################################################### */
#ifdef OPPAI_IMPLEMENTATION
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define OPPAI_VERSION_MAJOR 2
#define OPPAI_VERSION_MINOR 2
#define OPPAI_VERSION_PATCH 0
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define OPPAI_VERSION_STRING \
STRINGIFY(OPPAI_VERSION_MAJOR) "." \
STRINGIFY(OPPAI_VERSION_MINOR) "." \
STRINGIFY(OPPAI_VERSION_PATCH)
OPPAIAPI void oppai_version(int* major, int* minor, int* patch) {
*major = OPPAI_VERSION_MAJOR;
*minor = OPPAI_VERSION_MINOR;
*patch = OPPAI_VERSION_PATCH;
}
OPPAIAPI char* oppai_version_str() {
return OPPAI_VERSION_STRING;
}
/* error utils --------------------------------------------------------- */
int info(char* fmt, ...) {
int res;
va_list va;
va_start(va, fmt);
res = vfprintf(stderr, fmt, va);
va_end(va);
return res;
}
OPPAIAPI
char* errstr(int err) {
switch (err) {
case ERR_MORE: return "call me again with more data";
case ERR_SYNTAX: return "syntax error";
case ERR_TRUNCATED:
return "data was truncated, possibly because it was too big";
case ERR_NOTIMPLEMENTED:
return "requested a feature that isn't implemented";
case ERR_IO: return "i/o error";
case ERR_FORMAT: return "invalid input format";
case ERR_OOM: return "out of memory";
}
info("W: got unknown error %d\n", err);
return "unknown error";
}
/* math ---------------------------------------------------------------- */
float get_inf() {
static unsigned raw = 0x7F800000;
float* p = (float*)&raw;
return *p;
}
/* dst = a - b */
void v2f_sub(float* dst, float* a, float* b) {
dst[0] = a[0] - b[0];
dst[1] = a[1] - b[1];
}
float v2f_len(float* v) {
return (float)sqrt(v[0] * v[0] + v[1] * v[1]);
}
/* string utils -------------------------------------------------------- */
int whitespace(char c) {
switch (c) {
case '\r':
case '\n':
case '\t':
case ' ':
return 1;
}
return 0;
}
int slice_write(slice_t* s, FILE* f) {
return (int)fwrite(s->start, 1, s->end - s->start, f);
}
int slice_whitespace(slice_t* s) {
char* p = s->start;
for (; p < s->end; ++p) {
if (!whitespace(*p)) {
return 0;
}
}
return 1;
}
/* trims leading and trailing whitespace */
void slice_trim(slice_t* s) {
for (; s->start < s->end && whitespace(*s->start); ++s->start);
for (; s->end > s->start && whitespace(*(s->end-1)); --s->end);
}
int slice_cmp(slice_t* s, char* str) {
int len = (int)strlen(str);
int s_len = (int)(s->end - s->start);
if (len < s_len) {
return -1;
}
if (len > s_len) {
return 1;
}
return strncmp(s->start, str, len);
}
int slice_len(slice_t* s) {
return (int)(s->end - s->start);
}
/*
* splits s at any of the separators in separator_list and stores
* pointers to the strings in arr.
* returns the number of elements written to arr.
* if more elements than nmax are found, err is set to
* ERR_TRUNCATED
*/
int slice_split(slice_t* s, char* separator_list, slice_t* arr,
int nmax, int* err)
{
int res = 0;
char* p = s->start;
char* pprev = p;
if (!nmax) {
return 0;
}
if (!*separator_list) {
*arr = *s;
return 1;
}
for (; p <= s->end; ++p) {
char* sep = separator_list;
for (; *sep; ++sep) {
if (p >= s->end || *sep == *p) {
if (res >= nmax) {
*err = ERR_TRUNCATED;
goto exit;
}
arr[res].start = pprev;
arr[res].end = p;
pprev = p + 1;
++res;
break;
}
}
}
exit:
return res;
}
/* array --------------------------------------------------------------- */
/*
* these don't always use all params but we always pass all of them to
* ensure that we get a compiler error on things that don't have the same
* fields as an array struct
*/
OPPAIAPI
int array_reserve_i(int n, int* cap, int* len, void** data, int esize) {
(void)len;
if (*cap <= n) {
void* newdata;
int newcap = *cap ? *cap * 2 : 16;
newdata = realloc(*data, esize * newcap);
if (!newdata) {
return 0;
}
*data = newdata;
*cap = newcap;
}
return 1;
}
OPPAIAPI
void array_free_i(int* cap, int* len, void** data, int esize) {
(void)esize;
free(*data);
*cap = 0;
*len = 0;
*data = 0;
}
/* memory arena -------------------------------------------------------- */
/* aligns x down to a power-of-two value a */
#define bit_align_down(x, a) \
((x) & ~((a) - 1))
/* aligns x up to a power-of-two value a */
#define bit_align_up(x, a) \
bit_align_down((x) + (a) - 1, a)
OPPAIAPI
int arena_reserve(arena_t* arena, int min_size) {
int size;
char* new_block;
if (arena->end_of_block - arena->block >= min_size) {
return 1;
}
size = bit_align_up(al_max(min_size, ARENA_BLOCK_SIZE), ARENA_ALIGN);
new_block = malloc(size);
if (!new_block) {
return 0;
}
arena->block = new_block;
arena->end_of_block = new_block + size;
array_append(&arena->blocks, arena->block);
return 1;
}
OPPAIAPI
void* arena_alloc(arena_t* arena, int size) {
void* res;
if (!arena_reserve(arena, size)) {
return 0;
}
size = bit_align_up(size, ARENA_ALIGN);
res = arena->block;
arena->block += size;
return res;
}
OPPAIAPI
char* arena_strndup(arena_t* m, char* s, int n) {
char* res = arena_alloc(m, n + 1);
if (res) {
memcpy(res, s, n);
res[n] = 0;
}
return res;
}
OPPAIAPI
void arena_free(arena_t* arena) {
int i;
for (i = 0; i < arena->blocks.len; ++i) {
free(arena->blocks.data[i]);
}
array_free(&arena->blocks);
arena->block = 0;
arena->end_of_block = 0;
}
/* mods ---------------------------------------------------------------- */
float od10_ms[] = { 19.5f, 19.5f }; /* std, taiko */
float od0_ms[] = { 79.5f, 49.5f };
#define AR0_MS 1800.0f
#define AR5_MS 1200.0f
#define AR10_MS 450.0f
float od_ms_step[] = { 6.0f, 3.0f };
#define AR_MS_STEP1 120.f /* ar0-5 */
#define AR_MS_STEP2 150.f /* ar5-10 */
OPPAIAPI
int mods_apply_m(int mode, int mods, beatmap_stats_t* s, int flags) {
float od_ar_hp_multiplier;
switch (mode) {
case MODE_STD:
case MODE_TAIKO:
break;
default:
info("this gamemode is not yet supported for mods calc\n");
return ERR_NOTIMPLEMENTED;
}
s->speed = 1;
if (!(mods & MODS_MAP_CHANGING)) {
int m = mode;
if (flags & APPLY_OD) {
s->odms = od0_ms[m] - (float)ceil(od_ms_step[m] * s->od);
}
return 0;
}
/* speed */
if (mods & (MODS_DT | MODS_NC)) {
s->speed *= 1.5f;
}
if (mods & MODS_HT) {
s->speed *= 0.75f;
}
if (!flags) {
return 0;
}
/* global multipliers */
od_ar_hp_multiplier = 1;
if (mods & MODS_HR) {
od_ar_hp_multiplier *= 1.4f;
}
if (mods & MODS_EZ) {
od_ar_hp_multiplier *= 0.5f;
}
/*
* stats must be capped to 0-10 before HT/DT which brings them to a range
* of -4.42f to 11.08f for OD and -5 to 11 for AR
*/
/* od */
if (flags & APPLY_OD) {
int m = mode;
s->od *= od_ar_hp_multiplier;
s->odms = od0_ms[m] - (float)ceil(od_ms_step[m] * s->od);
s->odms = mymin(od0_ms[m], mymax(od10_ms[m], s->odms));
s->odms /= s->speed; /* apply speed-changing mods */
s->od = (od0_ms[m] - s->odms) / od_ms_step[m]; /* back to stat */
}
/* ar */
if (flags & APPLY_AR) {
float arms;
s->ar *= od_ar_hp_multiplier;
/* convert AR into its milliseconds value */
arms = s->ar <= 5
? (AR0_MS - AR_MS_STEP1 * (s->ar - 0))
: (AR5_MS - AR_MS_STEP2 * (s->ar - 5));
arms = mymin(AR0_MS, mymax(AR10_MS, arms));
arms /= s->speed;
s->ar = arms > AR5_MS
? (0 + (AR0_MS - arms) / AR_MS_STEP1)
: (5 + (AR5_MS - arms) / AR_MS_STEP2);
}
/* cs */
if (flags & APPLY_CS) {
float cs_multiplier = 1;
if (mods & MODS_HR) {
cs_multiplier = 1.3f;
}
if (mods & MODS_EZ) {
cs_multiplier = 0.5f;
}
s->cs *= cs_multiplier;
s->cs = mymax(0.0f, mymin(10.0f, s->cs));
}
/* hp */
if (flags & APPLY_HP) {
s->hp = mymin(s->hp * od_ar_hp_multiplier, 10);
}
return 0;
}
OPPAIAPI
void mods_apply(int mods, beatmap_stats_t* s, int flags) {
int n;
n = mods_apply_m(MODE_STD, mods, s, flags);
if (n < 0) {
info("W: mods_apply failed: %s\n", errstr(n));
}
}
/* beatmap ------------------------------------------------------------- */
/*
* sliders get 2 + ticks combo (head, tail and ticks) each repetition adds
* an extra combo and an extra set of ticks
*
* calculate the number of slider ticks for one repetition
* ---
* example: a 3.75f beats slider at 1x tick rate will go:
* beat0 (head), beat1 (tick), beat2(tick), beat3(tick),
* beat3.75f(tail)
* so all we have to do is ceil the number of beats and subtract 1 to take
* out the tail
* ---
* the -0.1f is there to prevent ceil from ceiling whole values like 1.0f to
* 2.0f randomly
*/
OPPAIAPI
int b_max_combo(beatmap_t* b) {
int res = b->nobjects;
int i;
float infinity = get_inf();
float tnext = -infinity;
int tindex = -1;
float px_per_beat = infinity; /* for std sliders */
/* taiko */
float ms_per_beat = 0; /* last timing change */
float beat_len = infinity; /* beat spacing */
float duration = 0; /* duration of the hit object */
float tick_spacing = -infinity; /* slider tick spacing */
if (!b->ntiming_points) {
info("beatmap has no timing points\n");
return ERR_FORMAT;
}
/* spinners don't give combo in taiko */
if (b->mode == MODE_TAIKO) {
res -= b->nspinners + b->nsliders;
}
/* slider ticks */
for (i = 0; i < b->nobjects; ++i) {
object_t* o = &b->objects[i];
int ticks;
float num_beats;
if (!(o->type & OBJ_SLIDER)) {
continue;
}
while (o->time >= tnext) {
float sv_multiplier;
timing_t* t;
++tindex;
if (b->ntiming_points > tindex + 1) {
tnext = b->timing_points[tindex + 1].time;
} else {
tnext = infinity;
}
t = &b->timing_points[tindex];
sv_multiplier = 1.0f;
if (!t->change && t->ms_per_beat < 0) {
sv_multiplier = -100.0f / t->ms_per_beat;
}
switch (b->mode) {
case MODE_STD:
px_per_beat = b->sv * 100.0f * sv_multiplier;
if (b->format_version < 8) {
px_per_beat /= sv_multiplier;
}
break;
case MODE_TAIKO: {
/* see d_taiko for details on what this does */
float velocity;
if (b->original_mode == MODE_TAIKO) {
/* no slider conversion for taiko -> taiko */
continue;
}
if (t->change) {
ms_per_beat = t->ms_per_beat;
}
beat_len = ms_per_beat;
if (b->format_version < 8) {
beat_len *= sv_multiplier;
}
velocity = 100.0f * b->sv / beat_len;
duration = o->distance * o->repetitions / velocity;
tick_spacing = mymin(beat_len / b->tick_rate,
duration / o->repetitions);
break;
}
default:
return ERR_NOTIMPLEMENTED;
}
}
if (b->mode == MODE_TAIKO) {
if (tick_spacing > 0 && duration < 2 * beat_len) {
res += (int)ceil((duration + tick_spacing / 8) / tick_spacing);
}
continue;
}