-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
oppai.c
2943 lines (2582 loc) · 68.8 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/
* -- usage: ---------------------------------------------------------------
* #define OPPAI_IMPLEMENTATION
* #include "../oppai.c"
*
* int main() {
* ezpp_t ez = ezpp_new();
* ezpp_set_mods(ez, MODS_HD | MODS_DT);
* ezpp(ez, "-");
* printf("%gpp\n", ezpp_pp(ez));
* return 0;
* }
* ------------------------------------------------------------------------
* $ gcc test.c
* $ cat /path/to/file.osu | ./a.out
*/
#if defined(_WIN32) && !defined(OPPAI_IMPLEMENTATION)
#ifdef OPPAI_EXPORT
#define OPPAIAPI __declspec(dllexport)
#elif defined(OPPAI_STATIC_HEADER)
#define OPPAIAPI
#else
#define OPPAIAPI __declspec(dllimport)
#endif
#else
#define OPPAIAPI
#endif
typedef struct ezpp *ezpp_t; /* opaque handle */
OPPAIAPI ezpp_t ezpp_new(void);
OPPAIAPI void ezpp_free(ezpp_t ez);
OPPAIAPI int ezpp(ezpp_t ez, char *map);
OPPAIAPI float ezpp_pp(ezpp_t ez);
OPPAIAPI float ezpp_stars(ezpp_t ez);
/*
* the above is all you need for basic usage. below are some advanced api's
* and usage examples
*
* - if map is "-" the map is read from standard input
* - you can use ezpp_data if you already have raw beatmap data in memory
* - if autocalc is set to 1, the results will be automatically refreshed
* when you change parameters. if reparsing is required, the last passed
* map or map data will be used
* - if map is 0 (NULL), difficulty calculation and map parsing are skipped
* and you must set at least mode, aim_stars, speed_stars, nobjects,
* base_ar, base_od, max_combo, nsliders, ncircles
* - if aim_stars or speed_stars are set difficulty calculation is also
* skipped but values are taken from map
* - setting mods or cs resets aim_stars and speed_stars, set those last
* = setting end resets accuracy_percent
* - if mode_override is set, std maps are converted to other modes
* - mode defaults to MODE_STD or the map's mode
* - mods default to MODS_NOMOD
* - combo defaults to full combo
* - nmiss defaults to 0
* - score_version defaults to scorev1
* - 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
* - if end is set, the map will be cut to this object index
* - if base_ar/od/cs are set, they will override the map's values
* - when you change map and you're reusing the handle, you should reset
* ar/od/cs/hp to -1 otherwise it will override them with the previous
* map's values
* - in autocalc mode, calling ezpp with a non-NULL map always resets
* ar/od/cs/hp overrides to -1 so you don't have to
*/
OPPAIAPI void ezpp_set_autocalc(ezpp_t ez, int autocalc);
OPPAIAPI int ezpp_autocalc(ezpp_t ez);
OPPAIAPI int ezpp_data(ezpp_t ez, char *data, int data_size);
OPPAIAPI float ezpp_aim_stars(ezpp_t ez);
OPPAIAPI float ezpp_speed_stars(ezpp_t ez);
OPPAIAPI float ezpp_aim_pp(ezpp_t ez);
OPPAIAPI float ezpp_speed_pp(ezpp_t ez);
OPPAIAPI float ezpp_acc_pp(ezpp_t ez);
OPPAIAPI float ezpp_accuracy_percent(ezpp_t ez);
OPPAIAPI int ezpp_n300(ezpp_t ez);
OPPAIAPI int ezpp_n100(ezpp_t ez);
OPPAIAPI int ezpp_n50(ezpp_t ez);
OPPAIAPI int ezpp_nmiss(ezpp_t ez);
OPPAIAPI float ezpp_ar(ezpp_t ez);
OPPAIAPI float ezpp_cs(ezpp_t ez);
OPPAIAPI float ezpp_od(ezpp_t ez);
OPPAIAPI float ezpp_hp(ezpp_t ez);
OPPAIAPI char *ezpp_artist(ezpp_t ez);
OPPAIAPI char *ezpp_artist_unicode(ezpp_t ez);
OPPAIAPI char *ezpp_title(ezpp_t ez);
OPPAIAPI char *ezpp_title_unicode(ezpp_t ez);
OPPAIAPI char *ezpp_version(ezpp_t ez);
OPPAIAPI char *ezpp_creator(ezpp_t ez);
OPPAIAPI int ezpp_ncircles(ezpp_t ez);
OPPAIAPI int ezpp_nsliders(ezpp_t ez);
OPPAIAPI int ezpp_nspinners(ezpp_t ez);
OPPAIAPI int ezpp_nobjects(ezpp_t ez);
OPPAIAPI float ezpp_odms(ezpp_t ez);
OPPAIAPI int ezpp_mode(ezpp_t ez);
OPPAIAPI int ezpp_combo(ezpp_t ez);
OPPAIAPI int ezpp_max_combo(ezpp_t ez);
OPPAIAPI int ezpp_mods(ezpp_t ez);
OPPAIAPI int ezpp_score_version(ezpp_t ez);
OPPAIAPI float ezpp_time_at(ezpp_t ez, int i); /* milliseconds */
OPPAIAPI float ezpp_strain_at(ezpp_t ez, int i, int difficulty_type);
OPPAIAPI int ezpp_ntiming_points(ezpp_t ez);
OPPAIAPI float ezpp_timing_time(ezpp_t ez, int i); /* milliseconds */
OPPAIAPI float ezpp_timing_ms_per_beat(ezpp_t ez, int i);
OPPAIAPI int ezpp_timing_change(ezpp_t ez, int i);
OPPAIAPI void ezpp_set_aim_stars(ezpp_t ez, float aim_stars);
OPPAIAPI void ezpp_set_speed_stars(ezpp_t ez, float speed_stars);
OPPAIAPI void ezpp_set_base_ar(ezpp_t ez, float ar);
OPPAIAPI void ezpp_set_base_od(ezpp_t ez, float od);
OPPAIAPI void ezpp_set_base_cs(ezpp_t ez, float cs);
OPPAIAPI void ezpp_set_base_hp(ezpp_t ez, float hp);
OPPAIAPI void ezpp_set_mode_override(ezpp_t ez, int mode_override);
OPPAIAPI void ezpp_set_mode(ezpp_t ez, int mode);
OPPAIAPI void ezpp_set_mods(ezpp_t ez, int mods);
OPPAIAPI void ezpp_set_combo(ezpp_t ez, int combo);
OPPAIAPI void ezpp_set_nmiss(ezpp_t ez, int nmiss);
OPPAIAPI void ezpp_set_score_version(ezpp_t ez, int score_version);
OPPAIAPI void ezpp_set_accuracy_percent(ezpp_t ez, float accuracy_percent);
OPPAIAPI void ezpp_set_accuracy(ezpp_t ez, int n100, int n50);
OPPAIAPI void ezpp_set_end(ezpp_t ez, int end);
OPPAIAPI void ezpp_set_end_time(ezpp_t ez, float end);
/*
* these will make a copy of mapfile/data and free it automatically. this
* is slow but useful when working with bindings in other langs where
* pointers to strings aren't guaranteed to persist like python3
*/
OPPAIAPI int ezpp_dup(ezpp_t ez, char *mapfile);
OPPAIAPI int ezpp_data_dup(ezpp_t ez, char *data, int data_size);
/* 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);
/* version info -------------------------------------------------------- */
OPPAIAPI void oppai_version(int *major, int *minor, int *patch);
OPPAIAPI char *oppai_version_str(void);
/* --------------------------------------------------------------------- */
#define MODE_STD 0
#define MODE_TAIKO 1
#define DIFF_SPEED 0
#define DIFF_AIM 1
#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)
/* this is all you need to know for normal usage. internals below */
/* ##################################################################### */
/* ##################################################################### */
/* ##################################################################### */
#ifdef OPPAI_EXPORT
#define OPPAI_IMPLEMENTATION
#endif
#ifdef OPPAI_IMPLEMENTATION
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define OPPAI_VERSION_MAJOR 4
#define OPPAI_VERSION_MINOR 1
#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;
return 0;
}
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 ---------------------------------------------------------------- */
#define log10f (float)log10
#define al_round(x) (float)floor((x) + 0.5f)
#define al_min(a, b) ((a) < (b) ? (a) : (b))
#define al_max(a, b) ((a) > (b) ? (a) : (b))
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
float get_inf(void)
{
static unsigned raw = 0x7F800000;
float *p = (float *)&raw;
return *p;
}
float get_nan(void)
{
static unsigned raw = 0x7FFFFFFF;
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]);
}
float v2f_dot(float *a, float *b)
{
return a[0] * b[0] + a[1] * b[1];
}
/* https://www.doc.ic.ac.uk/%7Eeedwards/compsys/float/nan.html */
int is_nan(float b)
{
unsigned *p = (void *)&b;
return (
(*p > 0x7F800000 && *p < 0x80000000) ||
(*p > 0x7FBFFFFF && *p <= 0xFFFFFFFF));
}
/* https://www.doc.ic.ac.uk/%7Eeedwards/compsys/float/nan.html */
int is_inf(float b)
{
int *p = (int *)&b;
return *p == 0x7F800000 || *p == 0xFF800000;
}
/* string utils -------------------------------------------------------- */
int whitespace(char c)
{
switch (c)
{
case '\r':
case '\n':
case '\t':
case ' ':
return 1;
}
return 0;
}
/* non-null terminated string, used internally for parsing */
typedef struct slice
{
char *start;
char *end; /* *(end - 1) is the last character */
} slice_t;
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 --------------------------------------------------------------- */
#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])
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;
}
void array_free_i(int *cap, int *len, void **data, int esize)
{
(void)esize;
free(*data);
*cap = 0;
*len = 0;
*data = 0;
}
/* --------------------------------------------------------------------- */
#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)
typedef struct timing
{
float time; /* milliseconds */
float ms_per_beat;
int change; /* if 0, ms_per_beat is -100.0f * sv_multiplier */
float px_per_beat;
/* taiko stuff */
float beat_len;
float velocity;
} timing_t;
typedef struct object
{
float time; /* milliseconds */
int type;
/* only for taiko maps */
int nsound_types;
int *sound_types;
/* only used by d_calc */
float normpos[2];
float angle;
float strains[2];
int is_single; /* 1 if diff calc sees this as a singletap */
float delta_time;
float d_distance;
int timing_point;
float pos[2];
float distance; /* only for sliders */
int repetitions;
/* taiko stuff */
float duration;
float tick_spacing;
int slider_is_drum_roll;
} object_t;
/*
* exposing the struct would cut down lines of code but makes it harder
* to use from langs that aren't c/c++ or don't have the same memory
* alignment etc
*/
#define AUTOCALC_BIT (1 << 0)
#define OWNS_MAP_BIT (1 << 1) /* map/data freed on ezpp{,_data}, ezpp_free */
struct ezpp
{
char *map;
char *data;
int data_size;
int flags;
int format_version;
int mode, mode_override, original_mode;
int score_version;
int mods, combo;
float accuracy_percent;
int n300, n100, n50, nmiss;
int end;
float end_time;
float base_ar, base_cs, base_od, base_hp;
int max_combo;
char *title;
char *title_unicode;
char *artist;
char *artist_unicode;
char *creator;
char *version;
int ncircles, nsliders, nspinners, nobjects;
float ar, od, cs, hp, odms, sv, tick_rate, speed_mul;
float stars;
float aim_stars, aim_difficulty, aim_length_bonus;
float speed_stars, speed_difficulty, speed_length_bonus;
float pp, aim_pp, speed_pp, acc_pp;
/* parser */
char section[64];
char buf[0xFFFF];
int p_flags;
array_t(object_t) objects;
array_t(timing_t) timing_points;
/* diffcalc */
float interval_end;
float max_strain;
array_t(float) highest_strains;
/* allocator */
char *block;
char *end_of_block;
array_t(char *) blocks;
};
/* memory arena (allocator) -------------------------------------------- */
#define M_ALIGN sizeof(void *)
#define M_BLOCK_SIZE 4096
/* 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)
int m_reserve(ezpp_t ez, int min_size)
{
int size;
char *new_block;
if (ez->end_of_block - ez->block >= min_size)
{
return 1;
}
size = bit_align_up(al_max(min_size, M_BLOCK_SIZE), M_ALIGN);
new_block = malloc(size);
if (!new_block)
{
return 0;
}
ez->block = new_block;
ez->end_of_block = new_block + size;
array_append(&ez->blocks, ez->block);
return 1;
}
void *m_alloc(ezpp_t ez, int size)
{
void *res;
if (!m_reserve(ez, size))
{
return 0;
}
size = bit_align_up(size, M_ALIGN);
res = ez->block;
ez->block += size;
return res;
}
char *m_strndup(ezpp_t ez, char *s, int n)
{
char *res = m_alloc(ez, n + 1);
if (res)
{
memcpy(res, s, n);
res[n] = 0;
}
return res;
}
void m_free(ezpp_t ez)
{
int i;
for (i = 0; i < ez->blocks.len; ++i)
{
free(ez->blocks.data[i]);
}
array_free(&ez->blocks);
ez->block = 0;
ez->end_of_block = 0;
}
/* mods ---------------------------------------------------------------- */
float od10_ms[] = {20, 20}; /* std, taiko */
float od0_ms[] = {80, 50};
#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 */
/*
* 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
*/
int mods_apply(ezpp_t ez)
{
float od_ar_hp_multiplier, cs_multiplier, arms;
switch (ez->mode)
{
case MODE_STD:
case MODE_TAIKO:
break;
default:
info("this gamemode is not yet supported for mods calc\n");
return ERR_NOTIMPLEMENTED;
}
ez->speed_mul = 1;
if (!(ez->mods & MODS_MAP_CHANGING))
{
ez->odms = od0_ms[ez->mode] - (float)ceil(od_ms_step[ez->mode] * ez->od);
return 0;
}
if (ez->mods & (MODS_DT | MODS_NC))
{
ez->speed_mul *= 1.5f;
}
if (ez->mods & MODS_HT)
{
ez->speed_mul *= 0.75f;
}
/* global multipliers */
od_ar_hp_multiplier = 1;
if (ez->mods & MODS_HR)
od_ar_hp_multiplier *= 1.4f;
if (ez->mods & MODS_EZ)
od_ar_hp_multiplier *= 0.5f;
ez->od *= od_ar_hp_multiplier;
ez->odms = od0_ms[ez->mode] - (float)ceil(od_ms_step[ez->mode] * ez->od);
ez->odms = al_min(od0_ms[ez->mode], al_max(od10_ms[ez->mode], ez->odms));
ez->odms /= ez->speed_mul;
ez->od = (od0_ms[ez->mode] - ez->odms) / od_ms_step[ez->mode];
ez->ar *= od_ar_hp_multiplier;
arms = ez->ar <= 5
? (AR0_MS - AR_MS_STEP1 * (ez->ar - 0))
: (AR5_MS - AR_MS_STEP2 * (ez->ar - 5));
arms = al_min(AR0_MS, al_max(AR10_MS, arms));
arms /= ez->speed_mul;
ez->ar = arms > AR5_MS
? (0 + (AR0_MS - arms) / AR_MS_STEP1)
: (5 + (AR5_MS - arms) / AR_MS_STEP2);
cs_multiplier = 1;
if (ez->mods & MODS_HR)
cs_multiplier = 1.3f;
if (ez->mods & MODS_EZ)
cs_multiplier = 0.5f;
ez->cs *= cs_multiplier;
ez->cs = al_max(0.0f, al_min(10.0f, ez->cs));
ez->hp = al_min(ez->hp * od_ar_hp_multiplier, 10);
return 0;
}
/* beatmap parser ------------------------------------------------------ */
/*
* comments in beatmaps can only be an entire line because
* some properties such as author can contain //
*
* all p_* functions expect s to be a single line and trimmed
* on errors, p_* functions return < 0 error codes otherwise they
* return n bytes consumed
*/
#define P_OVERRIDE_MODE (1 << 0) /* mode_override */
#define P_FOUND_AR (1 << 1)
#define CIRCLESIZE_BUFF_TRESHOLD 30.0f /* non-normalized diameter */
#define PLAYFIELD_WIDTH 512.0f /* in osu!pixels */
#define PLAYFIELD_HEIGHT 384.0f
float playfield_center[] = {
PLAYFIELD_WIDTH / 2.0f, PLAYFIELD_HEIGHT / 2.0f};
void print_line(slice_t *line)
{
info("in line: ");
slice_write(line, stderr);
info("\n");
}
int p_warn(char *e, slice_t *line)
{
info(e);
info("\n");
print_line(line);
return 0;
}
/* consume until any of the characters in separators is found */
int p_consume_til(slice_t *s, char *separators, slice_t *dst)
{
char *p = s->start;
dst->start = s->start;
for (; p < s->end; ++p)
{
char *sep;
for (sep = separators; *sep; ++sep)
{
if (*p == *sep)
{
dst->start = s->start;
dst->end = p;
return (int)(p - s->start);
}
}
}
dst->end = p;
return ERR_MORE;
}
float p_float(slice_t *value)
{
float res;
char *p = value->start;
if (*p == '-')
{
res = -1;
++p;
}
else
{
res = 1;
}
/* infinity symbol */
if (!strncmp(p, "\xe2\x88\x9e", 3))
{
res *= get_inf();
}
else
{
if (sscanf(value->start, "%f", &res) != 1)
{
info("W: failed to parse float ");
slice_write(value, stderr);
info("\n");
res = 0;
}
}
return res;
}
/* [name] */
int p_section_name(slice_t *s, slice_t *name)
{
int n;
slice_t p = *s;
if (*p.start++ != '[')
{
return ERR_SYNTAX;
}
n = p_consume_til(&p, "]", name);
if (n < 0)
{
return n;
}
p.start += n;
if (p.start != p.end - 1)
{ /* must end in ] */
return ERR_SYNTAX;
}
return (int)(p.start - s->start);
}
/* name: value (results are trimmed) */
int p_property(slice_t *s, slice_t *name, slice_t *value)
{
int n;
char *p = s->start;
n = p_consume_til(s, ":", name);
if (n < 0)
{
return n;
}
p += n;
++p; /* skip : */
value->start = p;
value->end = s->end;
slice_trim(name);
slice_trim(value);
return (int)(s->end - s->start);
}
char *p_slicedup(ezpp_t ez, slice_t *s)
{
return m_strndup(ez, s->start, slice_len(s));
}
int p_metadata(ezpp_t ez, slice_t *line)
{
slice_t name, value;
int n = p_property(line, &name, &value);
if (n < 0)
{
return p_warn("W: malformed metadata line", line);
}
if (!slice_cmp(&name, "Title"))
{
ez->title = p_slicedup(ez, &value);
}
else if (!slice_cmp(&name, "TitleUnicode"))
{
ez->title_unicode = p_slicedup(ez, &value);
}
else if (!slice_cmp(&name, "Artist"))
{
ez->artist = p_slicedup(ez, &value);
}
else if (!slice_cmp(&name, "ArtistUnicode"))
{
ez->artist_unicode = p_slicedup(ez, &value);
}
else if (!slice_cmp(&name, "Creator"))
{
ez->creator = p_slicedup(ez, &value);
}
else if (!slice_cmp(&name, "Version"))
{
ez->version = p_slicedup(ez, &value);
}
return n;
}
int p_general(ezpp_t ez, slice_t *line)
{
slice_t name, value;
int n;
n = p_property(line, &name, &value);
if (n < 0)
{
return p_warn("W: malformed general line", line);
}
if (!slice_cmp(&name, "Mode"))
{
if (sscanf(value.start, "%d", &ez->original_mode) != 1)
{
return ERR_SYNTAX;
}
if (ez->p_flags & P_OVERRIDE_MODE)
{
ez->mode = ez->mode_override;
}
else
{
ez->mode = ez->original_mode;
}
switch (ez->mode)
{
case MODE_STD:
case MODE_TAIKO:
break;
default:
return ERR_NOTIMPLEMENTED;
}
}
return n;
}
int p_difficulty(ezpp_t ez, slice_t *line)
{
slice_t name, value;
int n = p_property(line, &name, &value);
if (n < 0)
{
return p_warn("W: malformed difficulty line", line);
}
if (!slice_cmp(&name, "CircleSize"))
{
ez->cs = p_float(&value);
}
else if (!slice_cmp(&name, "OverallDifficulty"))
{
ez->od = p_float(&value);