forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModValue.cpp
1970 lines (1827 loc) · 45.4 KB
/
ModValue.cpp
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
#include "global.h"
#include "CubicSpline.h"
#include "EnumHelper.h"
#include "GameState.h"
#include "ModValue.h"
#include "RageLog.h"
#include "RageMath.h"
#include "LuaBinding.h"
enum mut
{
mut_never,
mut_frame,
mut_note
};
struct mod_operand
{
virtual ~mod_operand() {}
virtual mut get_update_type()= 0;
virtual double evaluate(mod_val_inputs& input)= 0;
virtual bool needs_beat()= 0;
virtual bool needs_second()= 0;
virtual bool needs_y_offset()= 0;
};
struct mod_input_number : mod_operand
{
double m_value;
virtual double evaluate(mod_val_inputs&)
{ return m_value; }
virtual mut get_update_type()
{ return mut_never; }
virtual bool needs_beat()
{ return false; }
virtual bool needs_second()
{ return false; }
virtual bool needs_y_offset()
{ return false; }
};
struct mod_input_music_rate : mod_operand
{
virtual double evaluate(mod_val_inputs&)
{ return GAMESTATE->get_hasted_music_rate(); }
virtual mut get_update_type()
{ return mut_frame; }
virtual bool needs_beat()
{ return false; }
virtual bool needs_second()
{ return false; }
virtual bool needs_y_offset()
{ return false; }
};
#define SIMPLE_MOD_INPUT_TYPE(field_name, update, beat, second, yoff) \
struct mod_input_##field_name : mod_operand \
{ \
virtual double evaluate(mod_val_inputs& input) \
{ return input.field_name; } \
virtual mut get_update_type() \
{ return update; } \
virtual bool needs_beat() \
{ return beat; } \
virtual bool needs_second() \
{ return second; } \
virtual bool needs_y_offset() \
{ return yoff; } \
};
SIMPLE_MOD_INPUT_TYPE(column, mut_never, false, false, false);
SIMPLE_MOD_INPUT_TYPE(y_offset, mut_note, false, false, true);
SIMPLE_MOD_INPUT_TYPE(note_id_in_chart, mut_note, false, false, false);
SIMPLE_MOD_INPUT_TYPE(note_id_in_column, mut_note, false, false, false);
SIMPLE_MOD_INPUT_TYPE(row_id, mut_note, false, false, false);
SIMPLE_MOD_INPUT_TYPE(eval_beat, mut_note, true, false, false);
SIMPLE_MOD_INPUT_TYPE(eval_second, mut_note, false, true, false);
SIMPLE_MOD_INPUT_TYPE(music_beat, mut_frame, true, false, false);
SIMPLE_MOD_INPUT_TYPE(music_second, mut_frame, false, true, false);
SIMPLE_MOD_INPUT_TYPE(dist_beat, mut_note, true, false, false);
SIMPLE_MOD_INPUT_TYPE(dist_second, mut_note, false, true, false);
// start dist and end dist can be done with:
// {"subtract", "music_beat" "start_beat"}
SIMPLE_MOD_INPUT_TYPE(start_beat, mut_never, true, false, false);
SIMPLE_MOD_INPUT_TYPE(start_second, mut_never, false, true, false);
SIMPLE_MOD_INPUT_TYPE(end_beat, mut_never, true, false, false);
SIMPLE_MOD_INPUT_TYPE(end_second, mut_never, false, true, false);
SIMPLE_MOD_INPUT_TYPE(prefunres, mut_note, false, false, false);
#undef SIMPLE_MOD_INPUT_TYPE
enum mit
{
mit_number,
mit_music_rate,
mit_column,
mit_y_offset,
mit_note_id_in_chart,
mit_note_id_in_column,
mit_row_id,
mit_eval_beat,
mit_eval_second,
mit_music_beat,
mit_music_second,
mit_dist_beat,
mit_dist_second,
mit_start_beat,
mit_start_second,
mit_end_beat,
mit_end_second,
mit_prefunres
};
struct mod_operator : mod_operand
{
virtual ~mod_operator()
{
for(auto&& op : m_operands)
{
delete op;
op= nullptr;
}
}
virtual double evaluate(mod_val_inputs& input)= 0;
virtual void load_from_lua(mod_function* parent, lua_State* L, int index);
mut get_update_type() { return m_update_type; }
virtual void per_frame_update(mod_val_inputs& input);
virtual void per_note_update(mod_val_inputs& input);
virtual bool needs_beat();
virtual bool needs_second();
virtual bool needs_y_offset();
mut m_update_type;
vector<mod_operand*> m_operands;
vector<double> m_operand_results;
vector<size_t> m_per_note_operands;
vector<size_t> m_per_frame_operands;
};
static mod_operand* load_single_operand(mod_function* parent, lua_State* L, int operator_index,
size_t operand_index);
static void load_operands_into_container(mod_function* parent, lua_State* L, int index,
vector<mod_operand*>& container);
static void organize_simple_operands(mod_function* parent, mod_operator* self);
static mod_operand* create_simple_input(std::string const& input_type, double value);
#define PER_NOTE_UPDATE_IF if(!m_per_note_operands.empty()) { per_note_update(input); }
#define EVAL_RESULT_EVAL \
virtual double evaluate(mod_val_inputs& input) \
{ \
PER_NOTE_UPDATE_IF; \
return m_eval_result; \
}
struct mod_operator_replace : mod_operator
{
virtual void load_from_lua(mod_function*, lua_State*, int)
{
throw std::string("mod operator replace really just exists to be used as a sum type.");
}
};
#define plus_wrap(left, right) (left + right)
#define subtract_wrap(left, right) (left - right)
#define multiply_wrap(left, right) (left * right)
static double divide_wrap(double left, double right)
{
if(right == 0.0)
{
return 0.0;
}
return left / right;
}
#define SIMPLE_OPERATOR(op_name, eval) \
struct mod_operator_##op_name : mod_operator \
{ \
virtual double evaluate(mod_val_inputs& input) \
{ \
PER_NOTE_UPDATE_IF; \
double ret= m_operand_results[0]; \
for(size_t cur= 1; cur < m_operand_results.size(); ++cur) \
{ \
ret= eval(ret, m_operand_results[cur]); \
} \
return ret; \
} \
void add_simple_operand(std::string const& input_type, double value) \
{ m_operands.push_back(create_simple_input(input_type, value)); } \
};
static double log_wrapper(double left, double right)
{
return log(left) / log(right);
}
SIMPLE_OPERATOR(add, plus_wrap);
SIMPLE_OPERATOR(subtract, subtract_wrap);
SIMPLE_OPERATOR(multiply, multiply_wrap);
SIMPLE_OPERATOR(divide, divide_wrap);
SIMPLE_OPERATOR(exp, pow);
SIMPLE_OPERATOR(log, log_wrapper);
SIMPLE_OPERATOR(min, std::min);
SIMPLE_OPERATOR(max, std::max);
#undef plus_wrap
#undef subtract_wrap
#undef multiply_wrap
#undef SIMPLE_OPERATOR
#define PAIR_OPERATOR(op_name, eval) \
struct mod_operator_##op_name : mod_operator \
{ \
virtual double evaluate(mod_val_inputs& input) \
{ \
PER_NOTE_UPDATE_IF; \
return eval(m_operand_results[0], m_operand_results[1]); \
} \
virtual void load_from_lua(mod_function* parent, lua_State* L, int index) \
{ \
mod_operator::load_from_lua(parent, L, index); \
if(m_operands.size() != 2) \
{ \
throw std::string(#op_name " operator must have exactly 2 operands."); \
} \
} \
};
static double fmod_wrapper(double left, double right)
{
if(right == 0.0)
{
return 0.0;
}
return fmod(left, right);
}
// Pretty sure checking for 1.0 is slower than the divide and multiply.
#define OBLONG_WRAPPER(name) \
static double name##_wrapper(double left, double right) \
{ \
if(right == 0.0) { return left; } \
return std::name(left / right) * right; \
}
OBLONG_WRAPPER(floor);
OBLONG_WRAPPER(ceil);
OBLONG_WRAPPER(round);
#undef OBLONG_WRAPPER
PAIR_OPERATOR(mod, fmod_wrapper);
PAIR_OPERATOR(floor, floor_wrapper);
PAIR_OPERATOR(ceil, ceil_wrapper);
PAIR_OPERATOR(round, round_wrapper);
#undef PAIR_OPERATOR
#define WAVE_OPERATOR(op_name, wave_eval) \
struct mod_operator_##op_name : mod_operator \
{ \
EVAL_RESULT_EVAL; \
virtual void load_from_lua(mod_function* parent, lua_State* L, int index) \
{ \
mod_operator::load_from_lua(parent, L, index); \
if(m_operands.size() != 1) \
{ \
throw std::string("Wave functions only take one operand."); \
} \
if(m_update_type == mut_never) \
{ \
double angle= m_operand_results[0]; \
m_eval_result= (wave_eval); \
} \
} \
virtual void per_frame_update(mod_val_inputs& input) \
{ \
mod_operator::per_frame_update(input); \
double angle= m_operand_results[0]; \
m_eval_result= (wave_eval); \
} \
virtual void per_note_update(mod_val_inputs& input) \
{ \
mod_operator::per_note_update(input); \
double angle= m_operand_results[0]; \
m_eval_result= (wave_eval); \
} \
double m_eval_result; \
};
static double square_wave(double angle)
{
angle= fmod(angle, Rage::D_PI * 2.0);
if(angle < 0.0)
{
angle+= Rage::D_PI * 2.0;
}
return angle >= Rage::D_PI ? -1.0 : 1.0;
}
static double triangle_wave(double angle)
{
angle= fmod(angle, Rage::D_PI * 2.0);
if(angle < 0.0)
{
angle+= Rage::D_PI * 2.0;
}
double result= angle * Rage::D_PI_REC;
if(result < .5)
{
return result * 2.0;
}
else if(result < 1.5)
{
return 1.0 - ((result - .5) * 2.0);
}
else
{
return -4.0 + (result * 2.0);
}
}
WAVE_OPERATOR(sin, (Rage::FastSin(angle)));
WAVE_OPERATOR(cos, (Rage::FastCos(angle)));
WAVE_OPERATOR(tan, (tan(angle)));
WAVE_OPERATOR(square, (square_wave(angle)));
WAVE_OPERATOR(triangle, (triangle_wave(angle)));
#undef WAVE_OPERATOR
struct mod_operator_random : mod_operator
{
virtual void load_from_lua(mod_function* parent, lua_State* L, int index)
{
mod_operator::load_from_lua(parent, L, index);
if(m_operands.size() != 1)
{
throw std::string("Random operator only takes one operand.");
}
if(m_update_type == mut_never)
{
m_eval_result= GAMESTATE->simple_stage_frandom(m_operand_results[0]);
}
}
EVAL_RESULT_EVAL;
void per_frame_update(mod_val_inputs& input);
void per_note_update(mod_val_inputs& input);
double m_eval_result;
};
struct mod_operator_phase : mod_operator
{
EVAL_RESULT_EVAL;
virtual void load_from_lua(mod_function* parent, lua_State* L, int index);
struct phase
{
phase()
:start(0.0), finish(0.0), mult(1.0), offset(0.0)
{}
double start;
double finish;
double mult;
double offset;
};
phase const* find_phase(double input);
void phase_eval();
void per_frame_update(mod_val_inputs& input);
void per_note_update(mod_val_inputs& input);
double m_eval_result;
vector<phase> m_phases;
phase m_default_phase;
};
struct mod_operator_repeat : mod_operator
{
mod_operator_repeat()
:m_rep_begin(0.0), m_rep_end(0.0)
{}
EVAL_RESULT_EVAL;
void repeat_eval();
virtual void load_from_lua(mod_function* parent, lua_State* L, int index);
void per_frame_update(mod_val_inputs& input);
void per_note_update(mod_val_inputs& input);
double m_eval_result;
double m_rep_begin;
double m_rep_end;
};
struct mod_operator_spline : mod_operator
{
mod_operator_spline()
:m_loop(false), m_polygonal(false), m_has_per_frame_points(false),
m_has_per_note_points(false)
{}
EVAL_RESULT_EVAL;
void spline_eval();
virtual void load_from_lua(mod_function* parent, lua_State* L, int index);
void per_frame_update(mod_val_inputs& input);
void per_note_update(mod_val_inputs& input);
double m_eval_result;
CubicSpline m_spline;
bool m_loop;
bool m_polygonal;
bool m_has_per_frame_points;
bool m_has_per_note_points;
};
struct mod_operator_lua : mod_operator
{
EVAL_RESULT_EVAL;
void lua_eval();
virtual void load_from_lua(mod_function* parent, lua_State* L, int index);
void per_frame_update(mod_val_inputs& input);
void per_note_update(mod_val_inputs& input);
double m_eval_result;
LuaReference m_func;
};
#undef EVAL_RESULT_EVAL
enum mot
{
mot_replace,
mot_add,
mot_subtract,
mot_multiply,
mot_divide,
mot_exp,
mot_log,
mot_min,
mot_max,
mot_sin,
mot_cos,
mot_tan,
mot_square,
mot_triangle,
mot_random,
mot_phase,
mot_repeat,
mot_mod,
mot_floor,
mot_ceil,
mot_round,
mot_spline,
mot_lua
};
static mod_operand* create_mod_operator(mod_function* parent, lua_State* L, int index);
static mod_operand* create_mod_input(lua_State* L, int index);
static mod_operand* load_operand_on_stack(mod_function* parent, lua_State* L);
struct mod_function
{
mod_function(ModifiableValue* parent, double col)
:m_priority(0), m_sum_type(mot_add),
m_start_beat(invalid_modfunction_time),
m_start_second(invalid_modfunction_time),
m_end_beat(invalid_modfunction_time),
m_end_second(invalid_modfunction_time),
m_base_operand(nullptr), m_column(col),
m_parent(parent)
{}
~mod_function();
void change_parent(ModifiableValue* new_parent)
{
m_parent= new_parent;
}
double evaluate(mod_val_inputs& input);
double evaluate_with_time(mod_val_inputs& input);
void load_from_lua(lua_State* L, int index, uint32_t col);
void simple_load(std::string const& name, std::string const& input_type,
double value);
void add_per_frame_operator(mod_operator* op);
void calc_unprovided_times(TimingData const* timing);
bool needs_beat();
bool needs_second();
bool needs_y_offset();
std::string const& get_name() { return m_name; }
void set_input_fields(mod_val_inputs& input)
{
input.column= m_column;
input.set_time(m_start_beat, m_start_second, m_end_beat, m_end_second);
}
// needs_per_frame_update exists so that ModifiableValue can check after
// creating a mod_function to see if it needs to be added to the manager for
// solving every frame.
bool needs_per_frame_update();
void per_frame_update(mod_val_inputs& input);
int m_priority;
mot m_sum_type;
double m_start_beat;
double m_start_second;
double m_end_beat;
double m_end_second;
private:
// do not use mod_function::operator=, too much pointer handling.
mod_function& operator=(mod_function&)
{return *this;}
mod_operand* m_base_operand;
vector<mod_operator*> m_per_frame_operators;
double m_column;
std::string m_name;
ModifiableValue* m_parent;
};
void ModManager::update(double curr_beat, double curr_second)
{
double const time_diff= curr_second - m_prev_curr_second;
// Do not simply return early when time_diff is 0 because per frame
// mod_functions still need to be updated when they are changed while the
// game is paused. -Kyz
if(time_diff > 0)
{
// Time is moving forwards.
for(auto fap= m_present_funcs.begin(); fap != m_present_funcs.end();)
{
auto this_fap= fap++;
if(this_fap->func->m_end_second < curr_second)
{
insert_into_past(*this_fap);
remove_from_present(this_fap);
}
else
{
break;
}
}
for(auto fap= m_future_funcs.begin(); fap != m_future_funcs.end();)
{
auto this_fap= fap++;
if(this_fap->func->m_start_second <= curr_second)
{
if(this_fap->func->m_end_second < curr_second)
{
insert_into_past(*this_fap);
}
else
{
insert_into_present(*this_fap);
}
m_future_funcs.erase(this_fap);
}
else
{
break;
}
}
}
else if(time_diff < 0)
{
// Time is moving backwards.
for(auto fap= m_present_funcs.begin(); fap != m_present_funcs.end();)
{
auto this_fap= fap++;
if(this_fap->func->m_end_second < curr_second)
{
insert_into_past(*this_fap);
remove_from_present(this_fap);
}
else if(this_fap->func->m_start_second > curr_second)
{
insert_into_future(*this_fap);
remove_from_present(this_fap);
}
}
for(auto fap= m_past_funcs.begin(); fap != m_past_funcs.end();)
{
auto this_fap= fap++;
if(this_fap->func->m_end_second >= curr_second)
{
if(this_fap->func->m_start_second > curr_second)
{
insert_into_future(*this_fap);
}
else
{
insert_into_present(*this_fap);
}
m_past_funcs.erase(this_fap);
}
else
{
break;
}
}
}
m_prev_curr_second= curr_second;
if(!m_per_frame_update_funcs.empty())
{
mod_val_inputs input(curr_beat, curr_second);
for(auto&& func : m_per_frame_update_funcs)
{
func->per_frame_update(input);
}
}
}
void ModManager::add_mod(mod_function* func, ModifiableValue* parent)
{
if(func->m_start_second > m_prev_curr_second)
{
insert_into_future(func, parent);
}
else if(func->m_end_second < m_prev_curr_second)
{
insert_into_past(func, parent);
}
else
{
insert_into_present(func, parent);
}
}
void ModManager::remove_mod(mod_function* func)
{
for(auto&& managed_list : {&m_past_funcs, &m_present_funcs, &m_future_funcs})
{
for(auto fap= managed_list->begin(); fap != managed_list->end();)
{
auto this_fap= fap++;
if(this_fap->func == func)
{
managed_list->erase(this_fap);
}
}
}
}
void ModManager::remove_all_mods(ModifiableValue* parent)
{
for(auto&& managed_list : {&m_past_funcs, &m_present_funcs, &m_future_funcs})
{
for(auto fap= managed_list->begin(); fap != managed_list->end();)
{
auto this_fap= fap++;
if(this_fap->parent == parent)
{
managed_list->erase(this_fap);
}
}
}
}
void ModManager::add_to_per_frame_update(mod_function* func)
{
if(func->needs_per_frame_update())
{
m_per_frame_update_funcs.insert(func);
}
}
void ModManager::remove_from_per_frame_update(mod_function* func)
{
auto entry= m_per_frame_update_funcs.find(func);
if(entry != m_per_frame_update_funcs.end())
{
m_per_frame_update_funcs.erase(entry);
}
}
void ModManager::dump_list_status()
{
LOG->Trace("ModManager::dump_list_status:");
for(auto&& managed_list : {&m_past_funcs, &m_present_funcs, &m_future_funcs})
{
for(auto fap= managed_list->begin(); fap != managed_list->end(); ++fap)
{
LOG->Trace("%f, %f : %f, %f", fap->func->m_start_beat, fap->func->m_start_second, fap->func->m_end_beat, fap->func->m_end_second);
}
LOG->Trace("list over");
}
}
void ModManager::insert_into_past(mod_function* func, ModifiableValue* parent)
{
// m_past_funcs is sorted in descending end second order. Entries with the
// same end second are sorted in undefined order.
// This way, when time flows backwards, traversing from beginning to end
// gives the entries that should go into present.
// When time flows forwards, this ends up being inserting at the front.
for(auto fap= m_past_funcs.begin(); fap != m_past_funcs.end(); ++fap)
{
if(fap->func->m_end_second < func->m_end_second)
{
m_past_funcs.insert(fap, func_and_parent(func, parent));
return;
}
}
m_past_funcs.push_back(func_and_parent(func, parent));
}
void ModManager::insert_into_present(mod_function* func, ModifiableValue* parent)
{
add_to_per_frame_update(func);
parent->add_mod_to_active_list(func);
// m_present_funcs is sorted in ascending end second order. Entries with
// the same end second are sorted in ascending start second order.
for(auto fap= m_present_funcs.begin(); fap != m_present_funcs.end(); ++fap)
{
if(fap->func->m_end_second > func->m_end_second ||
(fap->func->m_end_second == func->m_end_second &&
fap->func->m_start_second > func->m_start_second))
{
m_present_funcs.insert(fap, func_and_parent(func, parent));
return;
}
}
m_present_funcs.push_back(func_and_parent(func, parent));
}
void ModManager::insert_into_future(mod_function* func, ModifiableValue* parent)
{
// m_future_funcs is sorted in ascending start second order. Entries with
// the same start second are sorted in undefined order.
for(auto fap= m_future_funcs.begin(); fap != m_future_funcs.end(); ++fap)
{
if(fap->func->m_start_second > func->m_start_second)
{
m_future_funcs.insert(fap, func_and_parent(func, parent));
return;
}
}
m_future_funcs.push_back(func_and_parent(func, parent));
}
void ModManager::remove_from_present(std::list<func_and_parent>::iterator fapi)
{
remove_from_per_frame_update(fapi->func);
fapi->parent->remove_mod_from_active_list(fapi->func);
m_present_funcs.erase(fapi);
}
#define CONVENT(field_name) {#field_name, mot_##field_name}
static std::unordered_map<std::string, mot> mot_conversion= {
{"+", mot_add},
{"-", mot_subtract},
{"*", mot_multiply},
{"/", mot_divide},
{"^", mot_exp},
{"v", mot_log},
{"%", mot_mod},
{"o", mot_round},
{"_", mot_floor},
CONVENT(replace),
CONVENT(add),
CONVENT(subtract),
CONVENT(multiply),
CONVENT(divide),
CONVENT(exp),
CONVENT(log),
CONVENT(min),
CONVENT(max),
CONVENT(sin),
CONVENT(cos),
CONVENT(tan),
CONVENT(square),
CONVENT(triangle),
CONVENT(random),
CONVENT(phase),
CONVENT(repeat),
CONVENT(mod),
CONVENT(floor),
CONVENT(ceil),
CONVENT(round),
CONVENT(spline),
CONVENT(lua)
};
#undef CONVENT
static mot str_to_mot(std::string const& str)
{
auto entry= mot_conversion.find(str);
if(entry == mot_conversion.end())
{
throw std::string(fmt::sprintf("Invalid mod operator type: %s", str.c_str()));
}
return entry->second;
}
static mod_operand* create_mod_operator(mod_function* parent, lua_State* L, int index)
{
lua_rawgeti(L, index, 1);
int op_field_type= lua_type(L, -1);
mod_operator* new_oper= nullptr;
if(op_field_type == LUA_TFUNCTION)
{
lua_pop(L, 1);
new_oper= new mod_operator_lua;
}
else if(op_field_type == LUA_TSTRING)
{
std::string str_type= lua_tostring(L, -1);
lua_pop(L, 1);
mot op_type= str_to_mot(str_type);
#define SET_NEW(op_name) \
case mot_##op_name: new_oper= new mod_operator_##op_name; break;
switch(op_type)
{
SET_NEW(add);
SET_NEW(subtract);
SET_NEW(multiply);
SET_NEW(divide);
SET_NEW(exp);
SET_NEW(log);
SET_NEW(min);
SET_NEW(max);
SET_NEW(sin);
SET_NEW(cos);
SET_NEW(tan);
SET_NEW(square);
SET_NEW(triangle);
SET_NEW(random);
SET_NEW(phase);
SET_NEW(repeat);
SET_NEW(mod);
SET_NEW(floor);
SET_NEW(ceil);
SET_NEW(round);
SET_NEW(spline);
default: break;
}
#undef SET_NEW
}
else
{
lua_pop(L, 1);
throw std::string("Mod operator type must be a string or function.");
}
if(new_oper == nullptr)
{
throw std::string("Unknown mod operator type.");
}
try
{
new_oper->load_from_lua(parent, L, index);
}
catch(std::string& err)
{
delete new_oper;
throw;
}
return new_oper;
}
#define CONVENT(field_name) {#field_name, mit_##field_name}
static std::unordered_map<std::string, mit> mit_conversion= {
CONVENT(music_rate),
CONVENT(column),
CONVENT(y_offset),
CONVENT(note_id_in_chart),
CONVENT(note_id_in_column),
CONVENT(row_id),
CONVENT(eval_beat),
CONVENT(eval_second),
CONVENT(music_beat),
CONVENT(music_second),
CONVENT(dist_beat),
CONVENT(dist_second),
CONVENT(start_beat),
CONVENT(start_second),
CONVENT(end_beat),
CONVENT(end_second),
CONVENT(prefunres)
};
#undef CONVENT
static mit str_to_mit(std::string const& str)
{
auto entry= mit_conversion.find(str);
if(entry == mit_conversion.end())
{
throw std::string(fmt::sprintf("Invalid mod input type: %s", str.c_str()));
}
return entry->second;
}
static mod_operand* create_mod_input(std::string const& str_type)
{
mit type= str_to_mit(str_type);
#define RET_ENTRY(field_name) \
case mit_##field_name: return new mod_input_##field_name;
#define ENTRY_BS_PAIR(base) RET_ENTRY(base##_beat); RET_ENTRY(base##_second);
switch(type)
{
RET_ENTRY(music_rate);
RET_ENTRY(column);
RET_ENTRY(y_offset);
RET_ENTRY(note_id_in_chart);
RET_ENTRY(note_id_in_column);
RET_ENTRY(row_id);
ENTRY_BS_PAIR(eval);
ENTRY_BS_PAIR(music);
ENTRY_BS_PAIR(dist);
ENTRY_BS_PAIR(start);
ENTRY_BS_PAIR(end);
RET_ENTRY(prefunres);
default: break;
}
#undef ENTRY_BS_PAIR
#undef RET_ENTRY
throw std::string(fmt::sprintf("Unknown mod input type: %s", str_type.c_str()));
}
static mod_operand* create_mod_input(lua_State* L, int index)
{
if(lua_type(L, index) == LUA_TNUMBER)
{
mod_input_number* ret= new mod_input_number;
ret->m_value= lua_tonumber(L, index);
return ret;
}
std::string str= lua_tostring(L, index);
return create_mod_input(str);
}
static mod_operand* create_simple_input(std::string const& input_type, double value)
{
if(input_type == "number")
{
mod_input_number* op= new mod_input_number;
op->m_value= value;
return op;
}
else
{
try
{
mod_operand* op= create_mod_input(input_type);
return op;
}
catch(std::string& err)
{
LuaHelpers::ReportScriptError("lol kyz messed up");
LuaHelpers::ReportScriptError(err);
mod_input_number* op= new mod_input_number;
op->m_value= value;
return op;
}
}
}
static mod_operand* load_operand_on_stack(mod_function* parent, lua_State* L)
{
int op_index= lua_gettop(L);
mod_operand* new_oper= nullptr;
try
{
switch(lua_type(L, op_index))
{
case LUA_TTABLE:
if(lua_objlen(L, op_index) < 2)
{
throw std::string("Mod operator table must have at least two elements.");
}
new_oper= create_mod_operator(parent, L, op_index);
break;
case LUA_TNUMBER:
case LUA_TSTRING:
new_oper= create_mod_input(L, op_index);
break;
default:
throw std::string("Invalid operand type.");
break;
}
}
catch(std::string& err)
{
lua_pop(L, 1);
throw;
}
lua_pop(L, 1);
return new_oper;
}
static mod_operand* load_single_operand(mod_function* parent, lua_State* L, int operator_index,
size_t operand_index)
{
lua_rawgeti(L, operator_index, operand_index);
return load_operand_on_stack(parent, L);
}
static void load_operands_into_container(mod_function* parent, lua_State* L, int index,
vector<mod_operand*>& container)
{
// index points to operator table.
// first entry in the table is the type of the operator.
size_t last_operand= lua_objlen(L, index);
container.reserve(last_operand - 1);