-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheasy_phi.hpp
More file actions
7771 lines (6380 loc) · 292 KB
/
easy_phi.hpp
File metadata and controls
7771 lines (6380 loc) · 292 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
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
#ifndef EASY_PHI_HPP
#define EASY_PHI_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <optional>
#include <variant>
#include <unordered_map>
#include <cmath>
#include <stdarg.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
#include <numbers>
#include <random>
#include <filesystem>
#include <ranges>
#include <unordered_set>
#include <string_view>
#include <utility>
#include <atomic>
#include <thread>
#include <map>
#include <cstring>
#include <intrin.h>
#include <cstdlib>
#include <malloc.h>
namespace easy_phi {
using ep_u8 = uint8_t;
using ep_u16 = uint16_t;
using ep_u32 = uint32_t;
using ep_u64 = uint64_t;
using ep_i8 = int8_t;
using ep_i16 = int16_t;
using ep_i32 = int32_t;
using ep_i64 = int64_t;
using ep_f32 = float;
using ep_f64 = double;
using ep_bool = bool;
static ep_u64 globalCounter = 1;
ep_u64 reqGlobalCounter() {
return globalCounter++;
}
bool cpuHasSSE2() {
int cpuInfo[4];
__cpuid(cpuInfo, 1);
return (cpuInfo[3] & (1 << 26)) != 0;
}
bool ptrIsAligned16(void* ptr) {
return (ep_u64)ptr % 16 == 0;
}
#ifdef _WIN32
inline void* ep_aligned_alloc(size_t alignment, size_t size) { return _aligned_malloc(size, alignment); }
inline void ep_aligned_free(void* ptr) { _aligned_free(ptr); }
#else
inline void* ep_aligned_alloc(size_t alignment, size_t size) { return std::aligned_alloc(alignment, size); }
inline void ep_aligned_free(void* ptr) { free(ptr); }
#endif
struct HashBucket {
ep_u64 hash = 0xcbf29ce484222325ULL;
static constexpr ep_u64 FNV_PRIME = 0x100000001b3ULL;
void mix(ep_u64 value) {
hash ^= value;
hash *= FNV_PRIME;
}
template <typename T>
void submitNumber(T v) {
static_assert(std::is_arithmetic_v<T>, "T must be numeric");
if constexpr (std::is_floating_point_v<T>) {
if (v == 0) v = 0;
}
const ep_u8* bytes = reinterpret_cast<const ep_u8*>(&v);
for (ep_u64 i = 0; i < sizeof(T); i++) {
mix(bytes[i]);
}
}
void submitBool(ep_bool b) {
mix(b ? 1 : 0);
}
template <typename T>
void submitOptionalNumber(std::optional<T> v) {
if (v.has_value()) {
submitBool(true);
submitNumber(v.value());
} else submitBool(false);
}
ep_u64 getHash() const {
return hash;
}
};
struct Data {
std::vector<ep_u8> data;
static Data FromPtr(ep_u8* ptr, ep_u64 size) {
return Data {
.data = std::vector<ep_u8>(ptr, ptr + size)
};
}
static ep_bool FromFile(Data* dst, const std::string& fn) {
std::ifstream file(std::filesystem::path((const char8_t*)fn.c_str()), std::ios::binary | std::ios::ate);
if (!file) return false;
ep_u64 size = file.tellg();
file.seekg(0);
std::vector<ep_u8> buffer(size);
if (!file.read((char*)buffer.data(), size)) return false;
file.close();
*dst = Data { .data = buffer };
return true;
}
std::string toString() const {
return std::string((char*)data.data(), data.size());
}
ep_u64 getHash() const {
HashBucket bucket;
for (ep_u8 byte : data) bucket.submitNumber(byte);
return bucket.getHash();
}
};
struct Vec2 {
ep_f64 x, y;
Vec2() = default;
template <typename A, typename B> Vec2(A a, B b) : x((ep_f64)a), y((ep_f64)b) {}
ep_f64 sum() const { return x + y; }
ep_f64 length() const { return std::sqrt(x * x + y * y); }
ep_f64 lengthSquared() const { return x * x + y * y; }
ep_f64 xyDiff() const { return std::abs(x - y); }
Vec2 operator+(const Vec2& v) const { return Vec2 { x + v.x, y + v.y }; }
Vec2 operator-(const Vec2& v) const { return Vec2 { x - v.x, y - v.y }; }
Vec2 operator*(const Vec2& v) const { return Vec2 { x * v.x, y * v.y }; }
Vec2 operator/(const Vec2& v) const { return Vec2 { x / v.x, y / v.y }; }
Vec2 operator+(ep_f64 v) const { return Vec2 { x + v, y + v }; }
Vec2 operator-(ep_f64 v) const { return Vec2 { x - v, y - v }; }
Vec2 operator*(ep_f64 v) const { return Vec2 { x * v, y * v }; }
Vec2 operator/(ep_f64 v) const { return Vec2 { x / v, y / v }; }
Vec2 operator-() const { return Vec2 { -x, -y }; }
Vec2& operator+=(const Vec2& v) { x += v.x; y += v.y; return *this; }
Vec2& operator-=(const Vec2& v) { x -= v.x; y -= v.y; return *this; }
Vec2& operator*=(const Vec2& v) { x *= v.x; y *= v.y; return *this; }
Vec2& operator/=(const Vec2& v) { x /= v.x; y /= v.y; return *this; }
Vec2& operator+=(ep_f64 v) { x += v; y += v; return *this; }
Vec2& operator-=(ep_f64 v) { x -= v; y -= v; return *this; }
Vec2& operator*=(ep_f64 v) { x *= v; y *= v; return *this; }
Vec2& operator/=(ep_f64 v) { x /= v; y /= v; return *this; }
ep_bool operator==(const Vec2& v) const { return x == v.x && y == v.y; }
ep_bool operator!=(const Vec2& v) const { return x != v.x || y != v.y; }
Vec2 rotate(ep_f64 angle, ep_f64 length) const {
ep_f64 c = std::cos(angle);
ep_f64 s = std::sin(angle);
return Vec2 {
x + c * length,
y + s * length
};
}
Vec2 rotateDegrees(ep_f64 angle, ep_f64 length) const {
return rotate(angle / 180.0 * std::numbers::pi, length);
}
ep_bool isZeroZone() const {
return x == y;
}
ep_bool include(ep_f64 v) const {
return x <= v && v <= y;
}
std::pair<ep_f64, ep_f64> toPair() const {
return { x, y };
}
};
static const ep_f64 INF_TIME = 99999.0;
static const Vec2 INF_TZ = { -INF_TIME, INF_TIME };
static const ep_f64 INF_EV = 1e9;
struct Rect {
ep_f64 x, y, w, h;
Vec2 position() const {
return { x, y };
}
Vec2 size() const {
return { w, h };
}
Rect extend(ep_f64 padding) const {
return Rect {
.x = x - padding,
.y = y - padding,
.w = w + padding * 2,
.h = h + padding * 2
};
}
Vec2 center() const {
return { x + w / 2, y + h / 2 };
}
};
struct Color {
ep_f64 r, g, b, a;
static Color White() {
return Color { 1.0, 1.0, 1.0, 1.0 };
}
static Color Black() {
return Color { 0.0, 0.0, 0.0, 1.0 };
}
static Color Red() {
return Color { 1.0, 0.0, 0.0, 1.0 };
}
static Color Green() {
return Color { 0.0, 1.0, 0.0, 1.0 };
}
static Color Blue() {
return Color { 0.0, 0.0, 1.0, 1.0 };
}
static Color Transparent() {
return Color { 0.0, 0.0, 0.0, 0.0 };
}
Color applyAlpha(ep_f64 alpha) {
return Color { r, g, b, a * alpha };
}
Color operator*(const Color& c) const {
return Color { r * c.r, g * c.g, b * c.b, a * c.a };
}
Color operator*(ep_f64 v) const {
return Color { r * v, g * v, b * v, a * v };
}
Color operator+(const Color& c) const {
return Color { r + c.r, g + c.g, b + c.b, a + c.a };
}
Color operator+(ep_f64 v) const {
return Color { r + v, g + v, b + v, a + v };
}
Color operator-(const Color& c) const {
return Color { r - c.r, g - c.g, b - c.b, a - c.a };
}
Color operator-(ep_f64 v) const {
return Color { r - v, g - v, b - v, a - v };
}
Color operator/(const Color& c) const {
return Color { r / c.r, g / c.g, b / c.b, a / c.a };
}
Color operator/(ep_f64 v) const {
return Color { r / v, g / v, b / v, a / v };
}
Color& operator*=(const Color& c) {
r *= c.r; g *= c.g; b *= c.b; a *= c.a;
return *this;
}
Color& operator*=(ep_f64 v) {
r *= v; g *= v; b *= v; a *= v;
return *this;
}
Color& operator+=(const Color& c) {
r += c.r; g += c.g; b += c.b; a += c.a;
return *this;
}
Color& operator+=(ep_f64 v) {
r += v; g += v; b += v; a += v;
return *this;
}
Color& operator-=(const Color& c) {
r -= c.r; g -= c.g; b -= c.b; a -= c.a;
return *this;
}
Color& operator-=(ep_f64 v) {
r -= v; g -= v; b -= v; a -= v;
return *this;
}
Color& operator/=(const Color& c) {
r /= c.r; g /= c.g; b /= c.b; a /= c.a;
return *this;
}
Color& operator/=(ep_f64 v) {
r /= v; g /= v; b /= v; a /= v;
return *this;
}
ep_bool operator==(const Color& c) const {
return r == c.r && g == c.g && b == c.b && a == c.a;
}
ep_bool operator!=(const Color& c) const {
return !(*this == c);
}
};
struct Transform2D {
ep_f64 matrix[6];
Transform2D(ep_f64 a, ep_f64 b, ep_f64 c, ep_f64 d, ep_f64 e, ep_f64 f) {
matrix[0] = a; matrix[1] = b;
matrix[2] = c; matrix[3] = d;
matrix[4] = e; matrix[5] = f;
}
Transform2D() {
matrix[0] = 1.0; matrix[1] = 0.0;
matrix[2] = 0.0; matrix[3] = 1.0;
matrix[4] = 0.0; matrix[5] = 0.0;
}
Transform2D& set(ep_f64 a, ep_f64 b, ep_f64 c, ep_f64 d, ep_f64 e, ep_f64 f) {
matrix[0] = a; matrix[1] = b;
matrix[2] = c; matrix[3] = d;
matrix[4] = e; matrix[5] = f;
return *this;
}
Transform2D& transform(ep_f64 a, ep_f64 b, ep_f64 c, ep_f64 d, ep_f64 e, ep_f64 f) {
set(
matrix[0] * a + matrix[2] * b,
matrix[1] * a + matrix[3] * b,
matrix[0] * c + matrix[2] * d,
matrix[1] * c + matrix[3] * d,
matrix[0] * e + matrix[2] * f + matrix[4],
matrix[1] * e + matrix[3] * f + matrix[5]
);
return *this;
}
Transform2D& transform(const Transform2D& o) {
transform(
o.matrix[0], o.matrix[1],
o.matrix[2], o.matrix[3],
o.matrix[4], o.matrix[5]
);
return *this;
}
Transform2D& scale(ep_f64 x, ep_f64 y) {
transform(x, 0.0, 0.0, y, 0.0, 0.0);
return *this;
}
Transform2D& scale(const Vec2& v) {
scale(v.x, v.y);
return *this;
}
Transform2D& translate(ep_f64 x, ep_f64 y) {
transform(1.0, 0.0, 0.0, 1.0, x, y);
return *this;
}
Transform2D& translate(const Vec2& v) {
translate(v.x, v.y);
return *this;
}
Transform2D& rotate(ep_f64 angle) {
ep_f64 c = std::cos(angle);
ep_f64 s = std::sin(angle);
transform(c, s, -s, c, 0.0, 0.0);
return *this;
}
Transform2D& rotateDegrees(ep_f64 angle) {
rotate(angle / 180.0 * std::numbers::pi);
return *this;
}
Vec2 transformPoint(ep_f64 x, ep_f64 y) const {
return Vec2 {
matrix[0] * x + matrix[2] * y + matrix[4],
matrix[1] * x + matrix[3] * y + matrix[5]
};
}
Vec2 transformPoint(const Vec2& v) const {
return transformPoint(v.x, v.y);
}
Transform2D getInverse() const {
ep_f64 det = matrix[0] * matrix[3] - matrix[1] * matrix[2];
ep_f64 invDet = det != 0 ? 1.0 / det : 1e9;
return Transform2D(
matrix[3] * invDet, -matrix[1] * invDet,
-matrix[2] * invDet, matrix[0] * invDet,
(matrix[2] * matrix[5] - matrix[3] * matrix[4]) * invDet,
(matrix[1] * matrix[4] - matrix[0] * matrix[5]) * invDet
);
}
};
ep_bool pointStrictlyInConvexQuad(const Vec2& p, const Vec2 quad[4]) {
auto cross = [](ep_f64 ax, ep_f64 ay, ep_f64 bx, ep_f64 by) {
return ax * by - ay * bx;
};
auto x = p.x, y = p.y;
auto cross0 = cross(quad[1].x - quad[0].x, quad[1].y - quad[0].y, x - quad[0].x, y - quad[0].y);
auto cross1 = cross(quad[2].x - quad[1].x, quad[2].y - quad[1].y, x - quad[1].x, y - quad[1].y);
auto cross2 = cross(quad[3].x - quad[2].x, quad[3].y - quad[2].y, x - quad[2].x, y - quad[2].y);
auto cross3 = cross(quad[0].x - quad[3].x, quad[0].y - quad[3].y, x - quad[3].x, y - quad[3].y);
if (cross0 < 0 && cross1 < 0 && cross2 < 0 && cross3 < 0) return true;
if (cross0 > 0 && cross1 > 0 && cross2 > 0 && cross3 > 0) return true;
return false;
}
ep_bool pointStrictlyInRect(const Vec2& p, const Rect& r) {
return r.x < p.x && p.x < r.x + r.w &&
r.y < p.y && p.y < r.y + r.h;
}
ep_bool quadStrictlyIntersectRect(const Vec2 quad[4], const Rect& r) {
return pointStrictlyInRect(quad[0], r) ||
pointStrictlyInRect(quad[1], r) ||
pointStrictlyInRect(quad[2], r) ||
pointStrictlyInRect(quad[3], r) ||
pointStrictlyInConvexQuad(Vec2 {r.x, r.y}, quad) ||
pointStrictlyInConvexQuad(Vec2 {r.x + r.w, r.y}, quad) ||
pointStrictlyInConvexQuad(Vec2 {r.x + r.w, r.y + r.h}, quad) ||
pointStrictlyInConvexQuad(Vec2 {r.x, r.y + r.h}, quad);
}
ep_bool lineIsIntersectLineSeg(const Vec2& linePoint, ep_f64 lineDeg, const Vec2 seg[2]) {
ep_f64 angle = lineDeg / 180.0 * std::numbers::pi;
Vec2 dir = { std::cos(angle), std::sin(angle) };
Vec2 s = seg[1] - seg[0];
Vec2 q = seg[0] - linePoint;
ep_f64 rxs = dir.x * s.y - dir.y * s.x;
ep_f64 qxs = q.x * s.y - q.y * s.x;
constexpr ep_f64 eps = 1e-9;
if (std::abs(rxs) < eps) {
if (std::abs(qxs) >= eps) return false;
return true;
}
ep_f64 u = (q.x * dir.y - q.y * dir.x) / rxs;
return u >= -eps && u <= 1.0 + eps;
}
ep_bool lineIsIntersectRect(const Vec2& linePoint, ep_f64 lineDeg, const Rect& r) {
return lineIsIntersectLineSeg(linePoint, lineDeg, (Vec2[2]) { Vec2 { r.x, r.y }, Vec2 { r.x + r.w, r.y } }) ||
lineIsIntersectLineSeg(linePoint, lineDeg, (Vec2[2]) { Vec2 { r.x + r.w, r.y }, Vec2 { r.x + r.w, r.y + r.h } }) ||
lineIsIntersectLineSeg(linePoint, lineDeg, (Vec2[2]) { Vec2 { r.x + r.w, r.y + r.h }, Vec2 { r.x, r.y + r.h } }) ||
lineIsIntersectLineSeg(linePoint, lineDeg, (Vec2[2]) { Vec2 { r.x, r.y + r.h }, Vec2 { r.x, r.y } });
}
ep_bool pointIsLeavingPoint(const Vec2& point, ep_f64 deg, const Vec2& targetPoint) {
ep_f64 eps = 1.0;
return (
(point.rotateDegrees(deg + 90, -eps) - targetPoint).lengthSquared() -
(point - targetPoint).lengthSquared()
) > 0;
}
ep_bool lineIsLeavingScreen(const Vec2& linePoint, ep_f64 lineDeg, const Rect& screenArea) {
return !lineIsIntersectRect(linePoint, lineDeg, screenArea) && pointIsLeavingPoint(linePoint, lineDeg, screenArea.center());
}
template <typename T1, typename T2>
struct SKVCache {
T1 key;
T2 value;
template <typename F>
[[gnu::always_inline, gnu::hot]]
const T2& get(const T1& k, F&& reseter) {
if (__builtin_expect(key != k, 0)) {
key = k;
value = reseter();
}
return value;
}
};
struct EaseSet {
struct Milthm {
static ep_f64 easing_in(ep_u64 press, ep_f64 p) {
switch (press) {
case 0: return p;
case 1: return (1.0 - cos(((p * std::numbers::pi) / 2.0)));
case 2: return pow(p, 2.0);
case 3: return pow(p, 3.0);
case 4: return pow(p, 4.0);
case 5: return pow(p, 5.0);
case 6: return ((p == 0.0) ? 0.0 : pow(2.0, ((10.0 * p) - 10.0)));
case 7: return (1.0 - pow((1.0 - pow(p, 2.0)), 0.5));
case 8: return ((2.70158 * pow(p, 3.0)) - (1.70158 * pow(p, 2.0)));
case 9: return ((p == 0.0) ? 0.0 : ((p == 1.0) ? 1.0 : ((-pow(2.0, ((10.0 * p) - 10.0))) * sin((((p * 10.0) - 10.75) * ((2.0 * std::numbers::pi) / 3.0))))));
case 10: return (1.0 - (((1.0 - p) < (1.0 / 2.75)) ? (7.5625 * pow((1.0 - p), 2.0)) : (((1.0 - p) < (2.0 / 2.75)) ? (((7.5625 * ((1.0 - p) - (1.5 / 2.75))) * ((1.0 - p) - (1.5 / 2.75))) + 0.75) : (((1.0 - p) < (2.5 / 2.75)) ? (((7.5625 * ((1.0 - p) - (2.25 / 2.75))) * ((1.0 - p) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * ((1.0 - p) - (2.625 / 2.75))) * ((1.0 - p) - (2.625 / 2.75))) + 0.984375)))));
default: return p;
}
}
static ep_f64 easing_out(ep_u64 press, ep_f64 p) {
switch (press) {
case 0: return p;
case 1: return sin(((p * std::numbers::pi) / 2.0));
case 2: return (1.0 - ((1.0 - p) * (1.0 - p)));
case 3: return (1.0 - pow((1.0 - p), 3.0));
case 4: return (1.0 - pow((1.0 - p), 4.0));
case 5: return (1.0 - pow((1.0 - p), 5.0));
case 6: return ((p == 1.0) ? 1.0 : (1.0 - pow(2.0, ((-10.0) * p))));
case 7: return pow((1.0 - pow((p - 1.0), 2.0)), 0.5);
case 8: return ((1.0 + (2.70158 * pow((p - 1.0), 3.0))) + (1.70158 * pow((p - 1.0), 2.0)));
case 9: return ((p == 0.0) ? 0.0 : ((p == 1.0) ? 1.0 : ((pow(2.0, ((-10.0) * p)) * sin((((p * 10.0) - 0.75) * ((2.0 * std::numbers::pi) / 3.0)))) + 1.0)));
case 10: return ((p < (1.0 / 2.75)) ? (7.5625 * pow(p, 2.0)) : ((p < (2.0 / 2.75)) ? (((7.5625 * (p - (1.5 / 2.75))) * (p - (1.5 / 2.75))) + 0.75) : ((p < (2.5 / 2.75)) ? (((7.5625 * (p - (2.25 / 2.75))) * (p - (2.25 / 2.75))) + 0.9375) : (((7.5625 * (p - (2.625 / 2.75))) * (p - (2.625 / 2.75))) + 0.984375))));
default: return p;
}
}
static ep_f64 easing_in_out(ep_u64 press, ep_f64 p) {
switch (press) {
case 0: return p;
case 1: return ((-(cos((std::numbers::pi * p)) - 1.0)) / 2.0);
case 2: return ((p < 0.5) ? (2.0 * pow(p, 2.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 2.0) / 2.0)));
case 3: return ((p < 0.5) ? (4.0 * pow(p, 3.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 3.0) / 2.0)));
case 4: return ((p < 0.5) ? (8.0 * pow(p, 4.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 4.0) / 2.0)));
case 5: return ((p < 0.5) ? (16.0 * pow(p, 5.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 5.0) / 2.0)));
case 6: return ((p == 0.0) ? 0.0 : ((p == 1.0) ? 1.0 : (((p < 0.5) ? pow(2.0, ((20.0 * p) - 10.0)) : (2.0 - pow(2.0, (((-20.0) * p) + 10.0)))) / 2.0)));
case 7: return ((p < 0.5) ? ((1.0 - pow((1.0 - pow((2.0 * p), 2.0)), 0.5)) / 2.0) : ((pow((1.0 - pow((((-2.0) * p) + 2.0), 2.0)), 0.5) + 1.0) / 2.0));
case 8: return ((p < 0.5) ? ((pow((2.0 * p), 2.0) * ((((2.5949095 + 1.0) * 2.0) * p) - 2.5949095)) / 2.0) : (((pow(((2.0 * p) - 2.0), 2.0) * (((2.5949095 + 1.0) * ((p * 2.0) - 2.0)) + 2.5949095)) + 2.0) / 2.0));
case 9: return ((p == 0.0) ? 0.0 : ((p == 0.0) ? 1.0 : ((p < 0.5) ? (((-pow(2.0, ((20.0 * p) - 10.0))) * sin((((20.0 * p) - 11.125) * ((2.0 * std::numbers::pi) / 4.5)))) / 2.0) : (((pow(2.0, (((-20.0) * p) + 10.0)) * sin((((20.0 * p) - 11.125) * ((2.0 * std::numbers::pi) / 4.5)))) / 2.0) + 1.0))));
case 10: return ((p < 0.5) ? ((1.0 - (((1.0 - (2.0 * p)) < (1.0 / 2.75)) ? (7.5625 * pow((1.0 - (2.0 * p)), 2.0)) : (((1.0 - (2.0 * p)) < (2.0 / 2.75)) ? (((7.5625 * ((1.0 - (2.0 * p)) - (1.5 / 2.75))) * ((1.0 - (2.0 * p)) - (1.5 / 2.75))) + 0.75) : (((1.0 - (2.0 * p)) < (2.5 / 2.75)) ? (((7.5625 * ((1.0 - (2.0 * p)) - (2.25 / 2.75))) * ((1.0 - (2.0 * p)) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * ((1.0 - (2.0 * p)) - (2.625 / 2.75))) * ((1.0 - (2.0 * p)) - (2.625 / 2.75))) + 0.984375))))) / 2.0) : ((1.0 + ((((2.0 * p) - 1.0) < (1.0 / 2.75)) ? (7.5625 * pow(((2.0 * p) - 1.0), 2.0)) : ((((2.0 * p) - 1.0) < (2.0 / 2.75)) ? (((7.5625 * (((2.0 * p) - 1.0) - (1.5 / 2.75))) * (((2.0 * p) - 1.0) - (1.5 / 2.75))) + 0.75) : ((((2.0 * p) - 1.0) < (2.5 / 2.75)) ? (((7.5625 * (((2.0 * p) - 1.0) - (2.25 / 2.75))) * (((2.0 * p) - 1.0) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * (((2.0 * p) - 1.0) - (2.625 / 2.75))) * (((2.0 * p) - 1.0) - (2.625 / 2.75))) + 0.984375))))) / 2.0));
default: return p;
}
}
static ep_f64 easing(ep_u64 ease, ep_u64 press, ep_f64 p) {
switch (ease) {
case 0: return easing_in(press, p);
case 1: return easing_out(press, p);
case 2: return easing_in_out(press, p);
default: return p;
}
}
};
struct Phigros {
struct Official {
static ep_f64 easing(ep_u64 ease, ep_f64 p) {
switch (ease) {
case 0: return p;
case 1: return 1.0 - cos(p * std::numbers::pi / 2.0);
case 2: return sin(p * std::numbers::pi / 2.0);
case 3: return (1.0 - cos(p * std::numbers::pi)) / 2.0;
case 4: return pow(p, 2.0);
case 5: return 1.0 - pow(p - 1.0, 2.0);
case 6: return ((p *= 2.0) < 1.0 ? pow(p, 2.0) : (-(pow(p - 2.0, 2.0) - 2.0))) / 2.0;
case 7: return pow(p, 3.0);
case 8: return 1.0 + pow(p - 1.0, 3.0);
case 9: return ((p *= 2.0) < 1.0 ? pow(p, 3.0) : (2.0 * pow(p - 2.0, 3.0) + 2.0)) / 2.0;
case 10: return pow(p, 4.0);
case 11: return 1.0 - pow(p - 1.0, 4.0);
case 12: return ((p *= 2.0) < 1.0 ? pow(p, 4.0) : (-(pow(p - 2.0, 4.0) - 2.0))) / 2.0;
case 13: return 0.0;
case 14: return 1.0;
default: return p;
}
}
};
struct RePhiEdit {
static ep_f64 easing(ep_u64 ease, ep_f64 p) {
switch (ease) {
case 1: return p;
case 2: return sin(((p * std::numbers::pi) / 2.0));
case 3: return (1.0 - cos(((p * std::numbers::pi) / 2.0)));
case 4: return (1.0 - ((1.0 - p) * (1.0 - p)));
case 5: return pow(p, 2.0);
case 6: return ((-(cos((std::numbers::pi * p)) - 1.0)) / 2.0);
case 7: return ((p < 0.5) ? (2.0 * pow(p, 2.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 2.0) / 2.0)));
case 8: return (1.0 - pow((1.0 - p), 3.0));
case 9: return pow(p, 3.0);
case 10: return (1.0 - pow((1.0 - p), 4.0));
case 11: return pow(p, 4.0);
case 12: return ((p < 0.5) ? (4.0 * pow(p, 3.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 3.0) / 2.0)));
case 13: return ((p < 0.5) ? (8.0 * pow(p, 4.0)) : (1.0 - (pow((((-2.0) * p) + 2.0), 4.0) / 2.0)));
case 14: return (1.0 - pow((1.0 - p), 5.0));
case 15: return pow(p, 5.0);
case 16: return ((p == 1.0) ? 1.0 : (1.0 - pow(2.0, ((-10.0) * p))));
case 17: return ((p == 0.0) ? 0.0 : pow(2.0, ((10.0 * p) - 10.0)));
case 18: return pow((1.0 - pow((p - 1.0), 2.0)), 0.5);
case 19: return (1.0 - pow((1.0 - pow(p, 2.0)), 0.5));
case 20: return ((1.0 + (2.70158 * pow((p - 1.0), 3.0))) + (1.70158 * pow((p - 1.0), 2.0)));
case 21: return ((2.70158 * pow(p, 3.0)) - (1.70158 * pow(p, 2.0)));
case 22: return ((p < 0.5) ? ((1.0 - pow((1.0 - pow((2.0 * p), 2.0)), 0.5)) / 2.0) : ((pow((1.0 - pow((((-2.0) * p) + 2.0), 2.0)), 0.5) + 1.0) / 2.0));
case 23: return ((p < 0.5) ? ((pow((2.0 * p), 2.0) * ((((2.5949095 + 1.0) * 2.0) * p) - 2.5949095)) / 2.0) : (((pow(((2.0 * p) - 2.0), 2.0) * (((2.5949095 + 1.0) * ((p * 2.0) - 2.0)) + 2.5949095)) + 2.0) / 2.0));
case 24: return ((p == 0.0) ? 0.0 : ((p == 1.0) ? 1.0 : ((pow(2.0, ((-10.0) * p)) * sin((((p * 10.0) - 0.75) * ((2.0 * std::numbers::pi) / 3.0)))) + 1.0)));
case 25: return ((p == 0.0) ? 0.0 : ((p == 1.0) ? 1.0 : ((-pow(2.0, ((10.0 * p) - 10.0))) * sin((((p * 10.0) - 10.75) * ((2.0 * std::numbers::pi) / 3.0))))));
case 26: return ((p < (1.0 / 2.75)) ? (7.5625 * pow(p, 2.0)) : ((p < (2.0 / 2.75)) ? (((7.5625 * (p - (1.5 / 2.75))) * (p - (1.5 / 2.75))) + 0.75) : ((p < (2.5 / 2.75)) ? (((7.5625 * (p - (2.25 / 2.75))) * (p - (2.25 / 2.75))) + 0.9375) : (((7.5625 * (p - (2.625 / 2.75))) * (p - (2.625 / 2.75))) + 0.984375))));
case 27: return (1.0 - (((1.0 - p) < (1.0 / 2.75)) ? (7.5625 * pow((1.0 - p), 2.0)) : (((1.0 - p) < (2.0 / 2.75)) ? (((7.5625 * ((1.0 - p) - (1.5 / 2.75))) * ((1.0 - p) - (1.5 / 2.75))) + 0.75) : (((1.0 - p) < (2.5 / 2.75)) ? (((7.5625 * ((1.0 - p) - (2.25 / 2.75))) * ((1.0 - p) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * ((1.0 - p) - (2.625 / 2.75))) * ((1.0 - p) - (2.625 / 2.75))) + 0.984375)))));
case 28: return ((p < 0.5) ? ((1.0 - (((1.0 - (2.0 * p)) < (1.0 / 2.75)) ? (7.5625 * pow((1.0 - (2.0 * p)), 2.0)) : (((1.0 - (2.0 * p)) < (2.0 / 2.75)) ? (((7.5625 * ((1.0 - (2.0 * p)) - (1.5 / 2.75))) * ((1.0 - (2.0 * p)) - (1.5 / 2.75))) + 0.75) : (((1.0 - (2.0 * p)) < (2.5 / 2.75)) ? (((7.5625 * ((1.0 - (2.0 * p)) - (2.25 / 2.75))) * ((1.0 - (2.0 * p)) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * ((1.0 - (2.0 * p)) - (2.625 / 2.75))) * ((1.0 - (2.0 * p)) - (2.625 / 2.75))) + 0.984375))))) / 2.0) : ((1.0 + ((((2.0 * p) - 1.0) < (1.0 / 2.75)) ? (7.5625 * pow(((2.0 * p) - 1.0), 2.0)) : ((((2.0 * p) - 1.0) < (2.0 / 2.75)) ? (((7.5625 * (((2.0 * p) - 1.0) - (1.5 / 2.75))) * (((2.0 * p) - 1.0) - (1.5 / 2.75))) + 0.75) : ((((2.0 * p) - 1.0) < (2.5 / 2.75)) ? (((7.5625 * (((2.0 * p) - 1.0) - (2.25 / 2.75))) * (((2.0 * p) - 1.0) - (2.25 / 2.75))) + 0.9375) : (((7.5625 * (((2.0 * p) - 1.0) - (2.625 / 2.75))) * (((2.0 * p) - 1.0) - (2.625 / 2.75))) + 0.984375))))) / 2.0));
case 29: return ((p == 0.0) ? 0.0 : ((p == 0.0) ? 1.0 : ((p < 0.5) ? (((-pow(2.0, ((20.0 * p) - 10.0))) * sin((((20.0 * p) - 11.125) * ((2.0 * std::numbers::pi) / 4.5)))) / 2.0) : (((pow(2.0, (((-20.0) * p) + 10.0)) * sin((((20.0 * p) - 11.125) * ((2.0 * std::numbers::pi) / 4.5)))) / 2.0) + 1.0))));
default: return p;
}
}
};
};
struct Rizline {
using Official = Phigros::Official;
};
};
enum class EnumPhiEventType : ep_u64 {
PositionX, PositionY,
SelfRotation, AxisRotation,
MultiplyAlpha, AdditiveAlpha,
Color,
ScaleX, ScaleY,
Speed, SpeedCoefficient,
Text,
ShaderUniform,
MAX = ShaderUniform + 1
};
enum class EnumPhiNoteType {
Tap, Drag, Flick, Hold
};
enum class EnumTextAlign {
Left, Center, Right
};
enum class EnumTextBaseline {
Top, Middle, Bottom
};
enum class EnumPhiLineAttachUI {
Pause, Bar,
ComboNumber, Combo, Score,
Name, Level,
None
};
struct PhiNoteTypeHelper {
static EnumPhiNoteType FromOfficial(ep_u64 n) {
if (n == 1) return EnumPhiNoteType::Tap;
if (n == 2) return EnumPhiNoteType::Drag;
if (n == 3) return EnumPhiNoteType::Hold;
if (n == 4) return EnumPhiNoteType::Flick;
return EnumPhiNoteType::Tap;
}
static EnumPhiNoteType FromRPE(ep_u64 n) {
if (n == 1) return EnumPhiNoteType::Tap;
if (n == 2) return EnumPhiNoteType::Hold;
if (n == 3) return EnumPhiNoteType::Flick;
if (n == 4) return EnumPhiNoteType::Drag;
return EnumPhiNoteType::Tap;
}
static EnumPhiNoteType FromPEC(const std::string& s) {
if (s == "n1") return EnumPhiNoteType::Tap;
if (s == "n2") return EnumPhiNoteType::Hold;
if (s == "n3") return EnumPhiNoteType::Flick;
if (s == "n4") return EnumPhiNoteType::Drag;
return EnumPhiNoteType::Tap;
}
};
struct PhiLineAttachUIHelper {
static EnumPhiLineAttachUI FromString(const std::string& s) {
if (s == "pause") return EnumPhiLineAttachUI::Pause;
if (s == "bar") return EnumPhiLineAttachUI::Bar;
if (s == "combo") return EnumPhiLineAttachUI::Combo;
if (s == "combonumber") return EnumPhiLineAttachUI::ComboNumber;
if (s == "score") return EnumPhiLineAttachUI::Score;
if (s == "name") return EnumPhiLineAttachUI::Name;
if (s == "level") return EnumPhiLineAttachUI::Level;
return EnumPhiLineAttachUI::None;
}
};
ep_bool isMultiplyEventType(EnumPhiEventType type) {
return (
type == EnumPhiEventType::MultiplyAlpha ||
type == EnumPhiEventType::ScaleX ||
type == EnumPhiEventType::ScaleY ||
type == EnumPhiEventType::SpeedCoefficient
);
}
struct PhiMeta {
ep_f64 offset;
std::string title;
std::string composer;
std::string artist;
std::string charter;
std::string difficulty;
ep_u64 rpeVersion = 0;
ep_bool isHoldCoverAtHead;
ep_bool isZeroLengthHoldHidden;
ep_bool isHighNoteHidden;
ep_bool isRegLineAlphaNoteHidden;
Vec2 lineWidthUnit, lineHeightUnit;
ep_f64 coverEllipsis = 1e-5;
ep_f64 maxViewRatio = (ep_f64)16 / 9;
Vec2 worldOrigin;
Vec2 worldViewport;
};
struct PhiBPMEvent {
ep_f64 time; // beats
ep_f64 bpm;
static void SortBpmEvents(std::vector<PhiBPMEvent>& events) {
std::sort(events.begin(), events.end(), [](const auto& a, const auto& b) {
return a.time < b.time;
});
}
};
struct PhiEventLayerIndexs {
static constexpr ep_u64 RPE_MAX = 5;
static constexpr ep_u64 UNIT = 1000000;
static constexpr ep_u64 NOTE_ATTRS = UNIT * 1;
static constexpr ep_u64 NOTE_ATTRS_2 = UNIT * 2;
static constexpr ep_u64 LINE_DEFAULT = UNIT * 3;
static constexpr ep_u64 SHADER_UNIFORM_DEFAULT = UNIT * 4;
};
struct PhiEvent {
Vec2 timeZone, valueZone;
EnumPhiEventType type;
ep_f64 (* easingFunc)(void*, ep_f64);
void* easingFuncContext;
Vec2 easingZone = { 0.0, 1.0 };
ep_u64 layerIndex;
ep_f64 cumulativeValueAtStart;
ep_f64 valueAtTime(ep_f64 t) {
// if (t < timeZone.x) return 0.0;
ep_f64 p = std::clamp((t - timeZone.x) / (timeZone.y - timeZone.x), 0.0, 1.0);
if (easingFunc) {
if (easingZone == Vec2 { 0.0, 1.0 }) {
p = easingFunc(easingFuncContext, p);
} else if (easingZone.x < easingZone.y) {
ep_f64 s = easingFunc(easingFuncContext, easingZone.x);
ep_f64 e = easingFunc(easingFuncContext, easingZone.y);
if (s != e) {
p = (easingFunc(easingFuncContext, p * (easingZone.y - easingZone.x) + easingZone.x) - s) / (e - s);
}
}
}
if (type == EnumPhiEventType::Color || type == EnumPhiEventType::Text || type == EnumPhiEventType::ShaderUniform) {
p = std::clamp(p, 0.0, 1.0);
}
return valueZone.x + p * (valueZone.y - valueZone.x);
}
static ep_f64 GetDefaultValue(EnumPhiEventType type) {
return isMultiplyEventType(type) ? 1.0 : 0.0;
}
};
struct PhiAnimLayer {
std::vector<PhiEvent> events[(ep_u64)EnumPhiEventType::MAX];
void addEvent(const PhiEvent& e) {
events[(ep_u64)e.type].push_back(e);
}
std::vector<PhiEvent>& getEvents(EnumPhiEventType type) {
return events[(ep_u64)type];
}
void init() {
for (auto& typed_events : events) {
std::sort(typed_events.begin(), typed_events.end(), [](const auto& a, const auto& b) {
return a.timeZone.x < b.timeZone.x;
});
}
initSpeedCumul();
}
void updateType(ep_u64 type, ep_f64 t) {
auto& typed_events = getEvents((EnumPhiEventType)type);
if (typed_events.empty()) return;
if (lastUpdatedTimes[type] == t) return;
if (lastUpdatedTimes[type] > t) currentIndexs[type] = 0;
while (
currentIndexs[type] < typed_events.size() - 1
&& typed_events[currentIndexs[type] + 1].timeZone.x <= t
) currentIndexs[type]++;
auto& e = typed_events[currentIndexs[type]];
currentValues[type] = e.valueAtTime(t);
if (type == (ep_u64)EnumPhiEventType::Speed) {
currentValues[type] = e.cumulativeValueAtStart + (
(e.valueZone.x + currentValues[type])
* (std::min(t, e.timeZone.y) - e.timeZone.x) / 2
+ std::max(t - e.timeZone.y, 0.0) * e.valueZone.y
);
}
lastUpdatedTimes[type] = t;
}
void updateType(EnumPhiEventType type, ep_f64 t) {
updateType((ep_u64)type, t);
}
void update(ep_f64 t) {
for (ep_u64 type = 0; type < (ep_u64)EnumPhiEventType::MAX; type++) {
updateType(type, t);
}
}
ep_f64 get(EnumPhiEventType type) {
if (events[(ep_u64)type].empty()) return PhiEvent::GetDefaultValue(type);
return currentValues[(ep_u64)type];
}
std::optional<ep_f64> getAlwaysValue(EnumPhiEventType type) {
auto& typedEvents = getEvents(type);
if (typedEvents.empty()) return PhiEvent::GetDefaultValue(type);
if (type == EnumPhiEventType::Speed) {
if (typedEvents.size() == 1 && typedEvents[0].valueZone.isZeroZone() && typedEvents[0].timeZone.x <= -INF_TIME / 2) {
return typedEvents[0].valueZone.x;
}
return std::nullopt;
}
ep_f64 fixedValue = typedEvents[0].valueZone.x;
for (auto& e : typedEvents) {
if (!e.valueZone.isZeroZone() || fixedValue != e.valueZone.x) {
return std::nullopt;
}
}
return fixedValue;
}
private:
ep_f64 lastUpdatedTimes[(ep_u64)EnumPhiEventType::MAX];
ep_u64 currentIndexs[(ep_u64)EnumPhiEventType::MAX];
ep_f64 currentValues[(ep_u64)EnumPhiEventType::MAX];
void initSpeedCumul() {
auto& speedEvents = getEvents(EnumPhiEventType::Speed);
ep_f64 cumulativeValue = 0.0;
for (ep_u64 i = 0; i < speedEvents.size(); i++) {
auto& e = speedEvents[i];
e.cumulativeValueAtStart = cumulativeValue;
cumulativeValue += e.valueZone.sum() * (e.timeZone.y - e.timeZone.x) / 2;
if (i < speedEvents.size() - 1) {
cumulativeValue += e.valueZone.y * (speedEvents[i + 1].timeZone.x - e.timeZone.y);
}
}
}
};
struct PhiAnimGroup {
std::unordered_map<ep_u64, ep_u64> layerIndexMap;
std::vector<PhiAnimLayer> layers;
void addEvent(const PhiEvent& e) {
if (!layerIndexMap.contains(e.layerIndex)) {
layerIndexMap[e.layerIndex] = layers.size();
layers.push_back({});
}
layers[layerIndexMap[e.layerIndex]].addEvent(e);
}
void init() {
for (auto& layer : layers) {
layer.init();
}
}
void updateType(EnumPhiEventType type, ep_f64 t) {
for (auto& layer : layers) {
layer.updateType(type, t);
}
}
void update(ep_f64 t) {
for (auto& layer : layers) {
layer.update(t);
}
}
ep_f64 get_based(EnumPhiEventType type, ep_f64 baseValue) {
ep_f64 value = baseValue;
for (auto& layer : layers) {
if (isMultiplyEventType(type)) value *= layer.get(type);
else value += layer.get(type);
}
return value;
}
std::optional<ep_f64> getAlwaysHashValue(EnumPhiEventType type) {
ep_f64 result = PhiEvent::GetDefaultValue(type);
for (auto& layer : layers) {
auto v = layer.getAlwaysValue(type);
if (!v.has_value()) return std::nullopt;
if (isMultiplyEventType(type)) result *= v.value();
else result += v.value();
}
return result;
}
};
struct PhiAnimator {
std::unordered_map<ep_u64, PhiAnimGroup> groups;
PhiAnimGroup& requestGroup(ep_u64 index) {
return groups.try_emplace(index, PhiAnimGroup {}).first->second;
}
template <typename T>
PhiAnimGroup& requestGroup(T& obj) {
return requestGroup(obj.indexer.get());
}