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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
#include "visual_script_builtin_funcs.h"
#include "math_funcs.h"
#include "object_type_db.h"
#include "reference.h"
#include "func_ref.h"
#include "os/os.h"
#include "variant_parser.h"
#include "io/marshalls.h"
const char* VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX]={
"sin",
"cos",
"tan",
"sinh",
"cosh",
"tanh",
"asin",
"acos",
"atan",
"atan2",
"sqrt",
"fmod",
"fposmod",
"floor",
"ceil",
"round",
"abs",
"sign",
"pow",
"log",
"exp",
"is_nan",
"is_inf",
"ease",
"decimals",
"stepify",
"lerp",
"dectime",
"randomize",
"randi",
"randf",
"rand_range",
"seed",
"rand_seed",
"deg2rad",
"rad2deg",
"linear2db",
"db2linear",
"max",
"min",
"clamp",
"nearest_po2",
"weakref",
"funcref",
"convert",
"typeof",
"type_exists",
"str",
"print",
"printerr",
"printraw",
"var2str",
"str2var",
"var2bytes",
"bytes2var",
};
int VisualScriptBuiltinFunc::get_output_sequence_port_count() const {
return has_input_sequence_port() ? 1 : 0;
}
bool VisualScriptBuiltinFunc::has_input_sequence_port() const{
switch(func) {
case MATH_RANDOMIZE:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
return true;
default:
return false;
}
}
int VisualScriptBuiltinFunc::get_input_value_port_count() const{
switch(func) {
case MATH_RANDOMIZE:
case MATH_RAND:
case MATH_RANDF:
return 0;
case MATH_SIN:
case MATH_COS:
case MATH_TAN:
case MATH_SINH:
case MATH_COSH:
case MATH_TANH:
case MATH_ASIN:
case MATH_ACOS:
case MATH_ATAN:
case MATH_SQRT:
case MATH_FLOOR:
case MATH_CEIL:
case MATH_ROUND:
case MATH_ABS:
case MATH_SIGN:
case MATH_LOG:
case MATH_EXP:
case MATH_ISNAN:
case MATH_ISINF:
case MATH_DECIMALS:
case MATH_SEED:
case MATH_RANDSEED:
case MATH_DEG2RAD:
case MATH_RAD2DEG:
case MATH_LINEAR2DB:
case MATH_DB2LINEAR:
case LOGIC_NEAREST_PO2:
case OBJ_WEAKREF:
case TYPE_OF:
case TEXT_STR:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
case VAR_TO_STR:
case STR_TO_VAR:
case VAR_TO_BYTES:
case BYTES_TO_VAR:
case TYPE_EXISTS:
return 1;
case MATH_ATAN2:
case MATH_FMOD:
case MATH_FPOSMOD:
case MATH_POW:
case MATH_EASE:
case MATH_STEPIFY:
case MATH_RANDOM:
case LOGIC_MAX:
case LOGIC_MIN:
case FUNC_FUNCREF:
case TYPE_CONVERT:
return 2;
case MATH_LERP:
case MATH_DECTIME:
case LOGIC_CLAMP:
return 3;
case FUNC_MAX:{}
}
return 0;
}
int VisualScriptBuiltinFunc::get_output_value_port_count() const{
switch(func) {
case MATH_RANDOMIZE:
case TEXT_PRINT:
case TEXT_PRINTERR:
case TEXT_PRINTRAW:
case MATH_SEED:
return 0;
case MATH_RANDSEED:
return 2;
default:
return 1;
}
return 1;
}
String VisualScriptBuiltinFunc::get_output_sequence_port_text(int p_port) const {
return String();
}
PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const{
switch(func) {
case MATH_SIN:
case MATH_COS:
case MATH_TAN:
case MATH_SINH:
case MATH_COSH:
case MATH_TANH:
case MATH_ASIN:
case MATH_ACOS:
case MATH_ATAN:
case MATH_ATAN2:
case MATH_SQRT: {
return PropertyInfo(Variant::REAL,"num");
} break;
case MATH_FMOD:
case MATH_FPOSMOD: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"x");
else
return PropertyInfo(Variant::REAL,"y");
} break;
case MATH_FLOOR:
case MATH_CEIL:
case MATH_ROUND:
case MATH_ABS:
case MATH_SIGN: {
return PropertyInfo(Variant::REAL,"num");
} break;
case MATH_POW: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"x");
else
return PropertyInfo(Variant::REAL,"y");
} break;
case MATH_LOG:
case MATH_EXP:
case MATH_ISNAN:
case MATH_ISINF: {
return PropertyInfo(Variant::REAL,"num");
} break;
case MATH_EASE: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"s");
else
return PropertyInfo(Variant::REAL,"curve");
} break;
case MATH_DECIMALS: {
return PropertyInfo(Variant::REAL,"step");
} break;
case MATH_STEPIFY: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"s");
else
return PropertyInfo(Variant::REAL,"steps");
} break;
case MATH_LERP: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"from");
else if (p_idx==1)
return PropertyInfo(Variant::REAL,"to");
else
return PropertyInfo(Variant::REAL,"weight");
} break;
case MATH_DECTIME: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"value");
else if (p_idx==1)
return PropertyInfo(Variant::REAL,"amount");
else
return PropertyInfo(Variant::REAL,"step");
} break;
case MATH_RANDOMIZE: {
} break;
case MATH_RAND: {
} break;
case MATH_RANDF: {
} break;
case MATH_RANDOM: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"from");
else
return PropertyInfo(Variant::REAL,"to");
} break;
case MATH_SEED: {
return PropertyInfo(Variant::INT,"seed");
} break;
case MATH_RANDSEED: {
return PropertyInfo(Variant::INT,"seed");
} break;
case MATH_DEG2RAD: {
return PropertyInfo(Variant::REAL,"deg");
} break;
case MATH_RAD2DEG: {
return PropertyInfo(Variant::REAL,"rad");
} break;
case MATH_LINEAR2DB: {
return PropertyInfo(Variant::REAL,"nrg");
} break;
case MATH_DB2LINEAR: {
return PropertyInfo(Variant::REAL,"db");
} break;
case LOGIC_MAX: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"a");
else
return PropertyInfo(Variant::REAL,"b");
} break;
case LOGIC_MIN: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"a");
else
return PropertyInfo(Variant::REAL,"b");
} break;
case LOGIC_CLAMP: {
if (p_idx==0)
return PropertyInfo(Variant::REAL,"a");
else if (p_idx==0)
return PropertyInfo(Variant::REAL,"min");
else
return PropertyInfo(Variant::REAL,"max");
} break;
case LOGIC_NEAREST_PO2: {
return PropertyInfo(Variant::INT,"num");
} break;
case OBJ_WEAKREF: {
return PropertyInfo(Variant::OBJECT,"source");
} break;
case FUNC_FUNCREF: {
if (p_idx==0)
return PropertyInfo(Variant::OBJECT,"instance");
else
return PropertyInfo(Variant::STRING,"funcname");
} break;
case TYPE_CONVERT: {
if (p_idx==0)
return PropertyInfo(Variant::NIL,"what");
else
return PropertyInfo(Variant::STRING,"type");
} break;
case TYPE_OF: {
return PropertyInfo(Variant::NIL,"what");
} break;
case TYPE_EXISTS: {
return PropertyInfo(Variant::STRING,"type");
} break;
case TEXT_STR: {
return PropertyInfo(Variant::NIL,"value");
} break;
case TEXT_PRINT: {
return PropertyInfo(Variant::NIL,"value");
} break;
case TEXT_PRINTERR: {
return PropertyInfo(Variant::NIL,"value");
} break;
case TEXT_PRINTRAW: {
return PropertyInfo(Variant::NIL,"value");
} break;
case VAR_TO_STR: {
return PropertyInfo(Variant::NIL,"var");
} break;
case STR_TO_VAR: {
return PropertyInfo(Variant::STRING,"string");
} break;
case VAR_TO_BYTES: {
return PropertyInfo(Variant::NIL,"var");
} break;
case BYTES_TO_VAR: {
return PropertyInfo(Variant::RAW_ARRAY,"bytes");
} break;
case FUNC_MAX:{}
}
return PropertyInfo();
}
PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) const{
Variant::Type t=Variant::NIL;
switch(func) {
case MATH_SIN:
case MATH_COS:
case MATH_TAN:
case MATH_SINH:
case MATH_COSH:
case MATH_TANH:
case MATH_ASIN:
case MATH_ACOS:
case MATH_ATAN:
case MATH_ATAN2:
case MATH_SQRT:
case MATH_FMOD:
case MATH_FPOSMOD:
case MATH_FLOOR:
case MATH_CEIL: {
t=Variant::REAL;
} break;
case MATH_ROUND: {
t=Variant::INT;
} break;
case MATH_ABS: {
t=Variant::NIL;
} break;
case MATH_SIGN: {
t=Variant::NIL;
} break;
case MATH_POW:
case MATH_LOG:
case MATH_EXP: {
t=Variant::REAL;
} break;
case MATH_ISNAN:
case MATH_ISINF: {
t=Variant::BOOL;
} break;
case MATH_EASE: {
t=Variant::REAL;
} break;
case MATH_DECIMALS: {
t=Variant::INT;
} break;
case MATH_STEPIFY:
case MATH_LERP:
case MATH_DECTIME: {
t=Variant::REAL;
} break;
case MATH_RANDOMIZE: {
} break;
case MATH_RAND: {
t=Variant::INT;
} break;
case MATH_RANDF:
case MATH_RANDOM: {
t=Variant::REAL;
} break;
case MATH_SEED: {
} break;
case MATH_RANDSEED: {
if (p_idx==0)
return PropertyInfo(Variant::INT,"rnd");
else
return PropertyInfo(Variant::INT,"seed");
} break;
case MATH_DEG2RAD:
case MATH_RAD2DEG:
case MATH_LINEAR2DB:
case MATH_DB2LINEAR: {
t=Variant::REAL;
} break;
case LOGIC_MAX:
case LOGIC_MIN:
case LOGIC_CLAMP: {
} break;
case LOGIC_NEAREST_PO2: {
t=Variant::NIL;
} break;
case OBJ_WEAKREF: {
t=Variant::OBJECT;
} break;
case FUNC_FUNCREF: {
t=Variant::OBJECT;
} break;
case TYPE_CONVERT: {
} break;
case TYPE_OF: {
t=Variant::INT;
} break;
case TYPE_EXISTS: {
t=Variant::BOOL;
} break;
case TEXT_STR: {
t=Variant::STRING;
} break;
case TEXT_PRINT: {
} break;
case TEXT_PRINTERR: {
} break;
case TEXT_PRINTRAW: {
} break;
case VAR_TO_STR: {
t=Variant::STRING;
} break;
case STR_TO_VAR: {
} break;
case VAR_TO_BYTES: {
t=Variant::RAW_ARRAY;
} break;
case BYTES_TO_VAR: {
} break;
case FUNC_MAX:{}
}
return PropertyInfo(t,"");
}
String VisualScriptBuiltinFunc::get_caption() const {
return "BuiltinFunc";
}
String VisualScriptBuiltinFunc::get_text() const {
return func_name[func];
}
void VisualScriptBuiltinFunc::set_func(BuiltinFunc p_which) {
ERR_FAIL_INDEX(p_which,FUNC_MAX);
func=p_which;
_change_notify();
ports_changed_notify();
}
VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::get_func() {
return func;
}
#define VALIDATE_ARG_NUM(m_arg) \
if (!p_inputs[m_arg]->is_num()) {\
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;\
r_error.argument=m_arg;\
r_error.expected=Variant::REAL;\
return 0;\
}
class VisualScriptNodeInstanceBuiltinFunc : public VisualScriptNodeInstance {
public:
VisualScriptBuiltinFunc *node;
VisualScriptInstance *instance;
VisualScriptBuiltinFunc::BuiltinFunc func;
//virtual int get_working_memory_size() const { return 0; }
//virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
//virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
virtual int step(const Variant** p_inputs,Variant** p_outputs,StartMode p_start_mode,Variant* p_working_mem,Variant::CallError& r_error,String& r_error_str) {
switch(func) {
case VisualScriptBuiltinFunc::MATH_SIN: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::sin(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_COS: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::cos(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_TAN: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::tan(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_SINH: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::sinh(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_COSH: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::cosh(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_TANH: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::tanh(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ASIN: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::asin(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ACOS: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::acos(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ATAN: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::atan(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ATAN2: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::atan2(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_SQRT: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::sqrt(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_FMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::fmod(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_FPOSMOD: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::fposmod(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_FLOOR: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::floor(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_CEIL: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::ceil(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ROUND: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::round(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ABS: {
if (p_inputs[0]->get_type()==Variant::INT) {
int64_t i = *p_inputs[0];
*p_outputs[0]=ABS(i);
} else if (p_inputs[0]->get_type()==Variant::REAL) {
real_t r = *p_inputs[0];
*p_outputs[0]=Math::abs(r);
} else {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::REAL;
}
} break;
case VisualScriptBuiltinFunc::MATH_SIGN: {
if (p_inputs[0]->get_type()==Variant::INT) {
int64_t i = *p_inputs[0];
*p_outputs[0]= i < 0 ? -1 : ( i > 0 ? +1 : 0);
} else if (p_inputs[0]->get_type()==Variant::REAL) {
real_t r = *p_inputs[0];
*p_outputs[0]= r < 0.0 ? -1.0 : ( r > 0.0 ? +1.0 : 0.0);
} else {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::REAL;
}
} break;
case VisualScriptBuiltinFunc::MATH_POW: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::pow(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_LOG: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::log(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_EXP: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::exp(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ISNAN: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::is_nan(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_ISINF: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::is_inf(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_EASE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::ease(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_DECIMALS: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::step_decimals(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_STEPIFY: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::stepify(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_LERP: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*p_outputs[0]=Math::lerp(*p_inputs[0],*p_inputs[1],*p_inputs[2]);
} break;
case VisualScriptBuiltinFunc::MATH_DECTIME: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
*p_outputs[0]=Math::dectime(*p_inputs[0],*p_inputs[1],*p_inputs[2]);
} break;
case VisualScriptBuiltinFunc::MATH_RANDOMIZE: {
Math::randomize();
} break;
case VisualScriptBuiltinFunc::MATH_RAND: {
*p_outputs[0]=Math::rand();
} break;
case VisualScriptBuiltinFunc::MATH_RANDF: {
*p_outputs[0]=Math::randf();
} break;
case VisualScriptBuiltinFunc::MATH_RANDOM: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*p_outputs[0]=Math::random(*p_inputs[0],*p_inputs[1]);
} break;
case VisualScriptBuiltinFunc::MATH_SEED: {
VALIDATE_ARG_NUM(0);
uint32_t seed=*p_inputs[0];
Math::seed(seed);
} break;
case VisualScriptBuiltinFunc::MATH_RANDSEED: {
VALIDATE_ARG_NUM(0);
uint32_t seed=*p_inputs[0];
int ret = Math::rand_from_seed(&seed);
Array reta;
reta.push_back(ret);
reta.push_back(seed);
*p_outputs[0]=reta;
} break;
case VisualScriptBuiltinFunc::MATH_DEG2RAD: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::deg2rad(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_RAD2DEG: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::rad2deg(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_LINEAR2DB: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::linear2db(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::MATH_DB2LINEAR: {
VALIDATE_ARG_NUM(0);
*p_outputs[0]=Math::db2linear(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::LOGIC_MAX: {
if (p_inputs[0]->get_type()==Variant::INT && p_inputs[1]->get_type()==Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
*p_outputs[0]=MAX(a,b);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
*p_outputs[0]=MAX(a,b);
}
} break;
case VisualScriptBuiltinFunc::LOGIC_MIN: {
if (p_inputs[0]->get_type()==Variant::INT && p_inputs[1]->get_type()==Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
*p_outputs[0]=MIN(a,b);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
*p_outputs[0]=MIN(a,b);
}
} break;
case VisualScriptBuiltinFunc::LOGIC_CLAMP: {
if (p_inputs[0]->get_type()==Variant::INT && p_inputs[1]->get_type()==Variant::INT && p_inputs[2]->get_type()==Variant::INT) {
int64_t a = *p_inputs[0];
int64_t b = *p_inputs[1];
int64_t c = *p_inputs[2];
*p_outputs[0]=CLAMP(a,b,c);
} else {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
VALIDATE_ARG_NUM(2);
real_t a = *p_inputs[0];
real_t b = *p_inputs[1];
real_t c = *p_inputs[2];
*p_outputs[0]=CLAMP(a,b,c);
}
} break;
case VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2: {
VALIDATE_ARG_NUM(0);
int64_t num = *p_inputs[0];
*p_outputs[0] = nearest_power_of_2(num);
} break;
case VisualScriptBuiltinFunc::OBJ_WEAKREF: {
if (p_inputs[0]->get_type()!=Variant::OBJECT) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
return 0;
}
if (p_inputs[0]->is_ref()) {
REF r = *p_inputs[0];
if (!r.is_valid()) {
return 0;
}
Ref<WeakRef> wref = memnew( WeakRef );
wref->set_ref(r);
*p_outputs[0]=wref;
} else {
Object *obj = *p_inputs[0];
if (!obj) {
return 0;
}
Ref<WeakRef> wref = memnew( WeakRef );
wref->set_obj(obj);
*p_outputs[0]=wref;
}
} break;
case VisualScriptBuiltinFunc::FUNC_FUNCREF: {
if (p_inputs[0]->get_type()!=Variant::OBJECT) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::OBJECT;
return 0;
}
if (p_inputs[1]->get_type()!=Variant::STRING && p_inputs[1]->get_type()!=Variant::NODE_PATH) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=1;
r_error.expected=Variant::STRING;
return 0;
}
Ref<FuncRef> fr = memnew( FuncRef);
fr->set_instance(*p_inputs[0]);
fr->set_function(*p_inputs[1]);
*p_outputs[0]=fr;
} break;
case VisualScriptBuiltinFunc::TYPE_CONVERT: {
VALIDATE_ARG_NUM(1);
int type=*p_inputs[1];
if (type<0 || type>=Variant::VARIANT_MAX) {
*p_outputs[0]=RTR("Invalid type argument to convert(), use TYPE_* constants.");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::INT;
return 0;
} else {
*p_outputs[0]=Variant::construct(Variant::Type(type),p_inputs,1,r_error);
}
} break;
case VisualScriptBuiltinFunc::TYPE_OF: {
*p_outputs[0] = p_inputs[0]->get_type();
} break;
case VisualScriptBuiltinFunc::TYPE_EXISTS: {
*p_outputs[0] = ObjectTypeDB::type_exists(*p_inputs[0]);
} break;
case VisualScriptBuiltinFunc::TEXT_STR: {
String str = *p_inputs[0];
*p_outputs[0]=str;
} break;
case VisualScriptBuiltinFunc::TEXT_PRINT: {
String str = *p_inputs[0];
print_line(str);
} break;
case VisualScriptBuiltinFunc::TEXT_PRINTERR: {
String str = *p_inputs[0];
//str+="\n";
OS::get_singleton()->printerr("%s\n",str.utf8().get_data());
} break;
case VisualScriptBuiltinFunc::TEXT_PRINTRAW: {
String str = *p_inputs[0];
//str+="\n";
OS::get_singleton()->print("%s",str.utf8().get_data());
} break;
case VisualScriptBuiltinFunc::VAR_TO_STR: {
String vars;
VariantWriter::write_to_string(*p_inputs[0],vars);
*p_outputs[0]=vars;
} break;
case VisualScriptBuiltinFunc::STR_TO_VAR: {
if (p_inputs[0]->get_type()!=Variant::STRING) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::STRING;
return 0;
}
VariantParser::StreamString ss;
ss.s=*p_inputs[0];
String errs;
int line;
Error err = VariantParser::parse(&ss,*p_outputs[0],errs,line);
if (err!=OK) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::STRING;
*p_outputs[0]="Parse error at line "+itos(line)+": "+errs;
return 0;
}
} break;
case VisualScriptBuiltinFunc::VAR_TO_BYTES: {
ByteArray barr;
int len;
Error err = encode_variant(*p_inputs[0],NULL,len);
if (err) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::NIL;
*p_outputs[0]="Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
return 0;
}
barr.resize(len);
{
ByteArray::Write w = barr.write();
encode_variant(*p_inputs[0],w.ptr(),len);
}
*p_outputs[0]=barr;
} break;
case VisualScriptBuiltinFunc::BYTES_TO_VAR: {
if (p_inputs[0]->get_type()!=Variant::RAW_ARRAY) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::RAW_ARRAY;
return 0;
}
ByteArray varr=*p_inputs[0];
Variant ret;
{
ByteArray::Read r=varr.read();
Error err = decode_variant(ret,r.ptr(),varr.size(),NULL);
if (err!=OK) {
*p_outputs[0]=RTR("Not enough bytes for decoding bytes, or invalid format.");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::RAW_ARRAY;
return 0;
}
}
*p_outputs[0]=ret;
} break;
default: {}
}
return 0;
}
};
VisualScriptNodeInstance* VisualScriptBuiltinFunc::instance(VisualScriptInstance* p_instance) {
VisualScriptNodeInstanceBuiltinFunc * instance = memnew(VisualScriptNodeInstanceBuiltinFunc );
instance->node=this;
instance->instance=p_instance;
instance->func=func;
return instance;
}
void VisualScriptBuiltinFunc::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_func","which"),&VisualScriptBuiltinFunc::set_func);
ObjectTypeDB::bind_method(_MD("get_func"),&VisualScriptBuiltinFunc::get_func);
String cc;
for(int i=0;i<FUNC_MAX;i++) {
if (i>0)
cc+=",";
cc+=func_name[i];
}
ADD_PROPERTY(PropertyInfo(Variant::INT,"function",PROPERTY_HINT_ENUM,cc),_SCS("set_func"),_SCS("get_func"));
}
VisualScriptBuiltinFunc::VisualScriptBuiltinFunc() {
func=MATH_SIN;
}
template<VisualScriptBuiltinFunc::BuiltinFunc func>
static Ref<VisualScriptNode> create_builtin_func_node(const String& p_name) {
Ref<VisualScriptBuiltinFunc> node;
node.instance();
node->set_func(func);
return node;
}
void register_visual_script_builtin_func_node() {
VisualScriptLanguage::singleton->add_register_func("functions/built_in/sin",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/cos",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/tan",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TAN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/sinh",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SINH>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/cosh",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COSH>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/tanh",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TANH>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/asin",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ASIN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/acos",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ACOS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan2",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN2>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/sqrt",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SQRT>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/fmod",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FMOD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/fposmod",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FPOSMOD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/floor",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FLOOR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/ceil",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CEIL>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/round",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ROUND>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/abs",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ABS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/sign",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIGN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/pow",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POW>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/log",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LOG>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/exp",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EXP>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/isnan",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISNAN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/isinf",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISINF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/ease",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EASE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/decimals",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECIMALS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/stepify",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_STEPIFY>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/dectime",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECTIME>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randomize",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOMIZE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/rand",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAND>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/random",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOM>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/seed",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SEED>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/randseed",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDSEED>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/deg2rad",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DEG2RAD>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/rad2deg",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAD2DEG>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/linear2db",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LINEAR2DB>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/db2linear",create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DB2LINEAR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/max",create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MAX>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/min",create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MIN>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/clamp",create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_CLAMP>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/nearest_po2",create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/weakref",create_builtin_func_node<VisualScriptBuiltinFunc::OBJ_WEAKREF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/funcref",create_builtin_func_node<VisualScriptBuiltinFunc::FUNC_FUNCREF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/convert",create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_CONVERT>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/typeof",create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_OF>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/type_exists",create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_EXISTS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/str",create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_STR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/print",create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr",create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTERR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw",create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTRAW>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str",create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_STR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var",create_builtin_func_node<VisualScriptBuiltinFunc::STR_TO_VAR>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes",create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_BYTES>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/bytes2var",create_builtin_func_node<VisualScriptBuiltinFunc::BYTES_TO_VAR>);
}
|