Repository navigation
Expand file tree
/
Copy pathgenerator.js
More file actions
1070 lines (1041 loc) · 54.3 KB
/
Copy pathgenerator.js
File metadata and controls
1070 lines (1041 loc) · 54.3 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
(function () {
var STORE_KEY = 'luaslice-wiki-generator';
function q(s) {
return "'" + String(s == null ? '' : s).replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n') + "'";
}
function n(v, d) {
var x = parseFloat(v);
if (!isFinite(x)) x = d || 0;
return String(Math.round(x * 10000) / 10000);
}
function int(v, d) {
var x = parseInt(v, 10);
return String(isFinite(x) ? x : (d || 0));
}
function hex(v) { return String(v || '#FFFFFF').replace(/^#/, '').toUpperCase(); }
function hash(v) { return '#' + hex(v); }
function ident(v, d) {
var s = String(v || '').trim().replace(/[^A-Za-z0-9_]/g, '');
if (!s || /^[0-9]/.test(s)) s = d;
return s;
}
function clean(v, d) {
var s = String(v == null ? '' : v).trim();
return s || d;
}
function list(v) {
return String(v || '').split(',').map(function (s) { return s.trim(); }).filter(Boolean);
}
var EASES = ['linear', 'quadIn', 'quadOut', 'quadInOut', 'cubeIn', 'cubeOut', 'cubeInOut', 'sineIn', 'sineOut', 'sineInOut', 'expoOut', 'backOut', 'elasticOut', 'bounceOut'];
var LAYERS = ['background', 'stage', 'characters', 'foreground', 'hud', 'top'];
var GAMEPLAY = ['song', 'stage', 'character', 'global'];
var APIS = {
luaslice: {
name: 'LuaSlice Lua API',
doc: '#api-overview',
order: ['onCreate', 'onCreatePost', 'onSongLoaded', 'onSongStart', 'onUpdate', 'onBeatHit', 'onStepHit', 'onNoteHit', 'onNoteMiss', 'onLuaMenuAccept', 'onPause', 'onResume', 'onSongEnd', 'onDestroy'],
sigs: {
onUpdate: 'elapsed', onBeatHit: 'beat', onStepHit: 'step', onNoteHit: 'event', onNoteMiss: 'event',
onLuaMenuAccept: 'menuId, index, label'
},
scopes: {
song: { name: 'Song', fields: ['song'], global: true, path: function (s, ext) { return 'mods/' + s.mod + '/gameplay/songs/' + s.song + '/script' + ext; } },
stage: { name: 'Stage', fields: ['stage'], path: function (s) { return 'mods/' + s.mod + '/stages/' + s.stage + '.lua'; } },
character: { name: 'Character', fields: ['character'], global: true, path: function (s, ext) { return 'mods/scripts/characters/' + s.character + ext; } },
global: { name: 'Every song', fields: ['name'], global: true, path: function (s, ext) { return 'mods/' + s.mod + '/scripts/' + (ext === '.luag' ? 'luag/' : 'lua/') + s.name + ext; } },
menu: { name: 'Main menu', fields: ['name'], path: function (s, ext) { return 'mods/' + s.mod + '/scripts/menu/' + s.name + ext; } }
},
ext: function (g) { return g ? '.luag' : '.lua'; },
features: [
{
id: 'guard', name: 'Version guard', desc: 'Stop the script on older LuaSlice builds.',
fields: [{ k: 'version', label: 'Minimum version', value: '0.1.0' }],
build: function (v, o) {
var ver = clean(v.version, '0.1.0');
o.first('onCreate', [
'if not LuaSlice.versionAtLeast(' + q(ver) + ') then',
' debug.error(' + q('This mod needs LuaSlice v' + ver + ' or newer.') + ')',
' script.stop()',
' return',
'end'
]);
}
},
{
id: 'hello', name: 'Log and window title', desc: 'Print a message when the script loads.',
fields: [{ k: 'msg', label: 'Message', value: 'Script loaded' }, { k: 'title', label: 'Window title (optional)', value: '' }],
build: function (v, o, s) {
var lines = [];
lines.push(GAMEPLAY.indexOf(s.scope) >= 0 ? 'debug.info(' + q(clean(v.msg, 'Script loaded') + ': ') + ' .. songName)' : 'debug.info(' + q(clean(v.msg, 'Script loaded')) + ')');
if (clean(v.title, '')) lines.push('window.setTitle(' + q(v.title) + ')');
o.hook('onCreate', lines);
}
},
{
id: 'sprite', name: 'Image sprite', desc: 'A static image on a named layer.',
fields: [
{ k: 'tag', label: 'Tag', value: 'backdrop' },
{ k: 'image', label: 'Image key', value: 'my-mod/images/backdrop' },
{ k: 'x', label: 'X', type: 'number', value: -100 },
{ k: 'y', label: 'Y', type: 'number', value: -50 },
{ k: 'scale', label: 'Scale', type: 'number', value: 1, step: 0.05 },
{ k: 'alpha', label: 'Alpha', type: 'number', value: 1, step: 0.05, min: 0, max: 1 },
{ k: 'layer', label: 'Layer', type: 'select', options: LAYERS, value: 'background' },
{ k: 'scroll', label: 'Scroll factor', type: 'number', value: 1, step: 0.05 }
],
build: function (v, o) {
var tag = clean(v.tag, 'backdrop');
var lines = [
'sprite.create(' + q(tag) + ', {',
' image = ' + q(clean(v.image, 'my-mod/images/backdrop')) + ',',
' x = ' + n(v.x) + ',',
' y = ' + n(v.y) + ',',
' alpha = ' + n(v.alpha, 1) + ',',
' scale = ' + n(v.scale, 1) + ',',
' camera = ' + q(v.layer === 'hud' || v.layer === 'top' ? 'hud' : 'game') + ',',
' layer = ' + q(v.layer || 'background'),
'})'
];
if (n(v.scroll, 1) !== '1') lines.push('setObjectScrollFactor(' + q(tag) + ', ' + n(v.scroll, 1) + ', ' + n(v.scroll, 1) + ')');
o.hook('onCreate', lines);
}
},
{
id: 'anim', name: 'Animated sprite', desc: 'A Sparrow atlas that dances on the beat.',
fields: [
{ k: 'tag', label: 'Tag', value: 'dancer' },
{ k: 'image', label: 'Atlas key', value: 'my-mod/images/dancer' },
{ k: 'x', label: 'X', type: 'number', value: 500 },
{ k: 'y', label: 'Y', type: 'number', value: 250 },
{ k: 'prefix', label: 'Idle prefix', value: 'Dancer Idle' },
{ k: 'fps', label: 'FPS', type: 'number', value: 24 },
{ k: 'every', label: 'Dance every N beats', type: 'number', value: 2, min: 1 },
{ k: 'layer', label: 'Layer', type: 'select', options: LAYERS, value: 'characters' }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'dancer'));
o.hook('onCreate', [
'loadSparrow(' + tag + ', ' + q(clean(v.image, 'my-mod/images/dancer')) + ', ' + n(v.x) + ', ' + n(v.y) + ')',
'addAnimByPrefix(' + tag + ", 'idle', " + q(clean(v.prefix, 'Dancer Idle')) + ', ' + int(v.fps, 24) + ', false)',
'sprite.setLayer(' + tag + ', ' + q(v.layer || 'characters') + ')',
'playAnim(' + tag + ", 'idle', true)"
]);
o.hook('onBeatHit', ['if beat % ' + Math.max(1, +int(v.every, 2)) + ' == 0 then playAnim(' + tag + ", 'idle', true) end"]);
}
},
{
id: 'panel', name: 'HUD panel', desc: 'A solid color rectangle on the HUD.',
fields: [
{ k: 'tag', label: 'Tag', value: 'panel' },
{ k: 'x', label: 'X', type: 'number', value: 40 },
{ k: 'y', label: 'Y', type: 'number', value: 40 },
{ k: 'w', label: 'Width', type: 'number', value: 320 },
{ k: 'h', label: 'Height', type: 'number', value: 120 },
{ k: 'color', label: 'Color', type: 'color', value: '#14243A' },
{ k: 'alpha', label: 'Alpha', type: 'number', value: 0.8, step: 0.05, min: 0, max: 1 }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'panel'));
o.hook('onCreate', [
'makeSolidSprite(' + tag + ', ' + n(v.x) + ', ' + n(v.y) + ', ' + n(v.w, 320) + ', ' + n(v.h, 120) + ', ' + q(hash(v.color)) + ", 'hud', 100)",
'setObjectAlpha(' + tag + ', ' + n(v.alpha, 1) + ')'
]);
o.hook('onDestroy', ['removeSprite(' + tag + ')']);
}
},
{
id: 'bop', name: 'Camera bop', desc: 'Zoom the camera every few beats.', gameplay: true,
fields: [
{ k: 'every', label: 'Every N beats', type: 'number', value: 4, min: 1 },
{ k: 'zoom', label: 'Zoom', type: 'number', value: 1.03, step: 0.01 },
{ k: 'cam', label: 'Camera', type: 'select', options: ['game', 'hud'], value: 'game' }
],
build: function (v, o) {
o.hook('onBeatHit', ['if beat % ' + Math.max(1, +int(v.every, 4)) + ' == 0 then camera.setZoom(' + q(v.cam || 'game') + ', ' + n(v.zoom, 1.03) + ') end']);
}
},
{
id: 'flash', name: 'Flash and shake on a beat', desc: 'Camera hit at one moment of the song.', gameplay: true,
fields: [
{ k: 'beat', label: 'Beat', type: 'number', value: 16, min: 0 },
{ k: 'cam', label: 'Camera', type: 'select', options: ['game', 'hud'], value: 'game' },
{ k: 'color', label: 'Flash color', type: 'color', value: '#FFFFFF' },
{ k: 'dur', label: 'Flash seconds', type: 'number', value: 0.5, step: 0.05 },
{ k: 'shake', label: 'Shake intensity (0 = off)', type: 'number', value: 0.01, step: 0.005 },
{ k: 'shakeDur', label: 'Shake seconds', type: 'number', value: 0.3, step: 0.05 }
],
build: function (v, o) {
var cam = q(v.cam || 'game');
var body = [' camera.flash(' + cam + ', ' + q(hash(v.color)) + ', ' + n(v.dur, 0.5) + ')'];
if (+v.shake > 0) body.push(' camera.shake(' + cam + ', ' + n(v.shake) + ', ' + n(v.shakeDur, 0.3) + ')');
o.hook('onCreate', ['song.atBeat(' + int(v.beat, 16) + ', function()'].concat(body, ['end)']));
}
},
{
id: 'hudfade', name: 'Fade the HUD', desc: 'Tween the HUD alpha after the song starts.', gameplay: true,
fields: [
{ k: 'delay', label: 'Delay seconds', type: 'number', value: 1, step: 0.1 },
{ k: 'alpha', label: 'Target alpha', type: 'number', value: 0.5, step: 0.05, min: 0, max: 1 },
{ k: 'dur', label: 'Duration', type: 'number', value: 0.4, step: 0.05 },
{ k: 'ease', label: 'Ease', type: 'select', options: EASES, value: 'quadOut' }
],
build: function (v, o) {
o.hook('onSongStart', [
'timer.after(' + n(v.delay, 1) + ', function()',
" tween.to('camHUD', { alpha = " + n(v.alpha, 0.5) + ' }, ' + n(v.dur, 0.4) + ", { tag = 'hudFade', ease = " + q(v.ease || 'quadOut') + ' })',
'end)'
]);
}
},
{
id: 'notekind', name: 'Note kind behavior', desc: 'Change health when a custom note kind is hit or missed.', gameplay: true,
fields: [
{ k: 'kind', label: 'Note kind', value: 'Heal Note' },
{ k: 'hit', label: 'Health on hit', type: 'number', value: 0.08, step: 0.01 },
{ k: 'miss', label: 'Health on miss', type: 'number', value: -0.12, step: 0.01 },
{ k: 'score', label: 'Score on hit', type: 'number', value: 0 }
],
build: function (v, o) {
var cond = 'if event.note ~= nil and event.note.kind == ' + q(clean(v.kind, 'Heal Note')) + ' then';
var hit = [cond, ' addHealth(' + n(v.hit) + ')'];
if (+v.score) hit.push(' addScore(' + int(v.score) + ')');
hit.push('end');
o.hook('onNoteHit', hit);
o.hook('onNoteMiss', [cond, ' addHealth(' + n(v.miss) + ')', 'end']);
}
},
{
id: 'sound', name: 'Looping sound', desc: 'Ambience that pauses with the game.',
fields: [
{ k: 'tag', label: 'Tag', value: 'ambience' },
{ k: 'key', label: 'Sound key', value: 'my-mod/sounds/rain' },
{ k: 'vol', label: 'Volume', type: 'number', value: 0.4, step: 0.05, min: 0, max: 1 },
{ k: 'loop', label: 'Loop', type: 'check', value: true }
],
build: function (v, o, s) {
var tag = q(clean(v.tag, 'ambience'));
o.hook('onCreate', ['sound.play(' + tag + ', ' + q(clean(v.key, 'my-mod/sounds/rain')) + ', ' + n(v.vol, 1) + ', ' + (v.loop ? 'true' : 'false') + ')']);
if (s.scope !== 'menu') {
o.hook('onPause', ['pauseSound(' + tag + ')']);
o.hook('onResume', ['resumeSound(' + tag + ')']);
}
o.hook('onDestroy', ['sound.stop(' + tag + ')']);
}
},
{
id: 'shader', name: 'Camera shader', desc: 'Load a fragment shader and tween one of its floats.', gameplay: true,
fields: [
{ k: 'tag', label: 'Tag', value: 'gray' },
{ k: 'path', label: 'Shader file', value: 'my-mod/shaders/grayscale.frag' },
{ k: 'cam', label: 'Camera', type: 'select', options: ['game', 'hud'], value: 'game' },
{ k: 'prop', label: 'Float uniform', value: 'amount' },
{ k: 'from', label: 'From', type: 'number', value: 0, step: 0.1 },
{ k: 'to', label: 'To', type: 'number', value: 1, step: 0.1 },
{ k: 'dur', label: 'Tween seconds', type: 'number', value: 1, step: 0.1 },
{ k: 'ease', label: 'Ease', type: 'select', options: EASES, value: 'quadInOut' }
],
build: function (v, o) {
var tag = clean(v.tag, 'gray'), cam = q(v.cam || 'game'), prop = q(clean(v.prop, 'amount'));
o.hook('onCreate', [
'if shader.make(' + q(tag) + ', ' + q(clean(v.path, 'my-mod/shaders/grayscale.frag')) + ') then',
' setShaderFloat(' + q(tag) + ', ' + prop + ', ' + n(v.from) + ')',
' setLuaCameraShader(' + q(tag) + ', ' + cam + ')',
'end'
]);
o.hook('onSongStart', ['shader.tween(' + q(tag) + ', ' + prop + ', ' + n(v.from) + ', ' + n(v.to, 1) + ', ' + n(v.dur, 1) + ', ' + q(v.ease || 'quadInOut') + ', ' + q(tag + 'Tween') + ')']);
o.hook('onDestroy', ['removeLuaCameraShader(' + cam + ')', 'destroyShader(' + q(tag) + ')']);
}
},
{
id: 'charanim', name: 'Character animation on a beat', desc: 'Play an animation, like "hey", at one beat.', gameplay: true,
fields: [
{ k: 'beat', label: 'Beat', type: 'number', value: 32, min: 0 },
{ k: 'target', label: 'Character', type: 'select', options: ['player', 'dad', 'gf'], value: 'dad' },
{ k: 'anim', label: 'Animation', value: 'hey' }
],
build: function (v, o) {
var t = q(v.target || 'dad');
o.hook('onCreate', [
'song.atBeat(' + int(v.beat, 32) + ', function()',
' if character.exists(' + t + ') then character.playAnim(' + t + ', ' + q(clean(v.anim, 'hey')) + ', true) end',
'end)'
]);
}
},
{
id: 'stagechange', name: 'Change stage on a beat', desc: 'Swap to another stage mid-song.', gameplay: true,
fields: [{ k: 'beat', label: 'Beat', type: 'number', value: 64, min: 0 }, { k: 'stage', label: 'Stage id', value: 'mainStage' }],
build: function (v, o) {
var id = q(clean(v.stage, 'mainStage'));
o.hook('onCreate', [
'song.atBeat(' + int(v.beat, 64) + ', function()',
' if stage.exists(' + id + ') then stage.change(' + id + ') end',
'end)'
]);
}
},
{
id: 'scroll', name: 'Scroll speed', desc: 'Set the note speed when the song starts.', gameplay: true,
fields: [{ k: 'speed', label: 'Speed', type: 'number', value: 1.15, step: 0.05 }, { k: 'rate', label: 'Playback rate', type: 'number', value: 1, step: 0.05 }],
build: function (v, o) {
var lines = ['setScrollSpeed(' + n(v.speed, 1) + ", 'both')"];
if (n(v.rate, 1) !== '1') lines.unshift('setPlaybackRate(' + n(v.rate, 1) + ')');
o.hook('onSongStart', lines);
}
},
{
id: 'strum', name: 'Strumline layout', desc: 'Move the player strumline and fade the opponent.', gameplay: true,
fields: [
{ k: 'x', label: 'Player X', type: 'number', value: 420 },
{ k: 'y', label: 'Player Y', type: 'number', value: 80 },
{ k: 'oppAlpha', label: 'Opponent alpha', type: 'number', value: 0.5, step: 0.05, min: 0, max: 1 }
],
build: function (v, o) {
o.hook('onCreate', [
"setStrumlinePosition('player', " + n(v.x) + ', ' + n(v.y) + ')',
"setStrumlineAlpha('opponent', " + n(v.oppAlpha, 1) + ')'
]);
}
},
{
id: 'save', name: 'Save counter', desc: 'Count something across sessions.',
fields: [{ k: 'key', label: 'Save key', value: 'visits' }],
build: function (v, o) {
var key = q(clean(v.key, 'visits'));
o.hook('onCreate', [
'local count = save.get(' + key + ', 0) + 1',
'save.set(' + key + ', count)',
'save.flush()',
"debug.info(" + key + " .. ': ' .. count)"
]);
}
},
{
id: 'options', name: 'Options page', desc: 'A custom page in the Options menu.',
fields: [
{ k: 'id', label: 'Page id', value: 'myModOptions' },
{ k: 'name', label: 'Page name', value: 'My Mod' },
{ k: 'key', label: 'Checkbox key', value: 'showParticles' },
{ k: 'label', label: 'Checkbox name', value: 'Particles' },
{ k: 'def', label: 'Default on', type: 'check', value: true }
],
build: function (v, o) {
o.hook('onCreate', [
'options.page(' + q(clean(v.id, 'myModOptions')) + ', {',
' name = ' + q(clean(v.name, 'My Mod')) + ',',
' position = 2,',
' items = {',
" { type = 'checkbox', key = " + q(clean(v.key, 'showParticles')) + ', name = ' + q(clean(v.label, 'Particles')) + ', default = ' + (v.def ? 'true' : 'false') + ' }',
' }',
'})'
]);
}
},
{
id: 'menu', name: 'Lua menu', desc: 'A simple text menu with a handler.',
fields: [
{ k: 'id', label: 'Menu id', value: 'myMenu' },
{ k: 'items', label: 'Items (comma separated)', value: 'About, Back' },
{ k: 'x', label: 'X', type: 'number', value: 80 },
{ k: 'y', label: 'Y', type: 'number', value: 120 },
{ k: 'spacing', label: 'Spacing', type: 'number', value: 44 }
],
build: function (v, o) {
var id = q(clean(v.id, 'myMenu')), items = list(v.items);
if (!items.length) items = ['Back'];
o.hook('onCreate', ['makeLuaMenuSimple(' + id + ', { ' + items.map(q).join(', ') + ' }, ' + n(v.x) + ', ' + n(v.y) + ', ' + n(v.spacing, 44) + ')']);
var body = ['if menuId == ' + id + ' then'];
items.forEach(function (it, i) {
var act = /^(back|close|exit)$/i.test(it) ? 'removeLuaMenu(' + id + ')' : 'debug.info(' + q(it + ' selected') + ')';
body.push(' ' + (i ? 'elseif' : 'if') + ' label == ' + q(it) + ' then', ' ' + act);
});
body.push(' end', 'end');
o.hook('onLuaMenuAccept', body);
}
},
{
id: 'key', name: 'Keyboard shortcut', desc: 'Do something when a key is pressed.',
fields: [
{ k: 'key', label: 'Key', value: 'F6' },
{ k: 'action', label: 'Action', type: 'select', options: ['toggle debug display', 'print mouse position', 'restart song'], value: 'toggle debug display' }
],
build: function (v, o, s) {
var act = v.action === 'restart song' && s.scope !== 'menu' ? 'restartSong()'
: v.action === 'print mouse position' ? "debug.print('Mouse: ' .. mouseX() .. ', ' .. mouseY())"
: 'setDebugDisplayVisible(not getDebugDisplayVisible())';
o.hook('onUpdate', ['if keyJustPressed(' + q(clean(v.key, 'F6').toUpperCase()) + ') then', ' ' + act, 'end']);
}
}
]
},
psych: {
name: 'Psych Lua API',
doc: '#psych-guide',
order: ['onCreate', 'onCreatePost', 'onSongStart', 'onUpdate', 'onBeatHit', 'onStepHit', 'goodNoteHit', 'opponentNoteHit', 'noteMiss', 'onEvent', 'onTimerCompleted', 'onTweenCompleted', 'onCustomSubstateCreate', 'onCustomSubstateUpdate', 'onPause', 'onResume', 'onEndSong', 'onDestroy'],
sigs: {
onUpdate: 'elapsed', goodNoteHit: 'id, direction, noteType, isSustainNote', opponentNoteHit: 'id, direction, noteType, isSustainNote',
noteMiss: 'id, direction, noteType, isSustainNote', onEvent: 'name, value1, value2, time', onTimerCompleted: 'tag, loops, loopsLeft',
onTweenCompleted: 'tag, target', onCustomSubstateCreate: 'name', onCustomSubstateUpdate: 'name, elapsed'
},
scopes: {
song: { name: 'Song', fields: ['song', 'name'], path: function (s) { return 'mods/' + s.mod + '/gameplay/songs/' + s.song + '/' + s.name + '.luap'; } },
stage: { name: 'Stage', fields: ['stage'], path: function (s) { return 'mods/' + s.mod + '/gameplay/stages/' + s.stage + '/' + s.stage + '.luap'; } },
character: { name: 'Character', fields: ['character'], path: function (s) { return 'mods/' + s.mod + '/gameplay/characters/' + s.character + '/' + s.character + '.luap'; } },
global: { name: 'Every song', fields: ['name'], path: function (s) { return 'mods/' + s.mod + '/scripts/' + s.name + '.luapg'; } },
notetype: { name: 'Note type', fields: ['kind'], path: function (s) { return 'mods/' + s.mod + '/custom_notetypes/' + s.kind + '.luap'; } },
event: { name: 'Chart event', fields: ['event'], path: function (s) { return 'mods/' + s.mod + '/custom_events/' + s.event + '.luap'; } }
},
ext: function () { return '.luap'; },
features: [
{
id: 'hello', name: 'Debug message', desc: 'Print a message when the script loads.',
fields: [{ k: 'msg', label: 'Message', value: 'Script loaded' }],
build: function (v, o) { o.hook('onCreate', ['debugPrint(' + q(clean(v.msg, 'Script loaded')) + ', songName)']); }
},
{
id: 'sprite', name: 'Image sprite', desc: 'makeLuaSprite with scale, scroll and alpha.',
fields: [
{ k: 'tag', label: 'Tag', value: 'backdrop' },
{ k: 'image', label: 'Image', value: 'backdrop' },
{ k: 'x', label: 'X', type: 'number', value: -100 },
{ k: 'y', label: 'Y', type: 'number', value: -50 },
{ k: 'scale', label: 'Scale', type: 'number', value: 1, step: 0.05 },
{ k: 'alpha', label: 'Alpha', type: 'number', value: 1, step: 0.05, min: 0, max: 1 },
{ k: 'scroll', label: 'Scroll factor', type: 'number', value: 1, step: 0.05 },
{ k: 'front', label: 'In front of characters', type: 'check', value: false }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'backdrop'));
var lines = ['makeLuaSprite(' + tag + ', ' + q(clean(v.image, 'backdrop')) + ', ' + n(v.x) + ', ' + n(v.y) + ')'];
if (n(v.scale, 1) !== '1') lines.push('scaleObject(' + tag + ', ' + n(v.scale, 1) + ', ' + n(v.scale, 1) + ')');
if (n(v.scroll, 1) !== '1') lines.push('setScrollFactor(' + tag + ', ' + n(v.scroll, 1) + ', ' + n(v.scroll, 1) + ')');
if (n(v.alpha, 1) !== '1') lines.push('setProperty(' + q(clean(v.tag, 'backdrop') + '.alpha') + ', ' + n(v.alpha, 1) + ')');
lines.push('addLuaSprite(' + tag + ', ' + (v.front ? 'true' : 'false') + ')');
o.hook('onCreate', lines);
}
},
{
id: 'anim', name: 'Animated sprite', desc: 'A Sparrow atlas that dances on the beat.',
fields: [
{ k: 'tag', label: 'Tag', value: 'dancer' },
{ k: 'image', label: 'Atlas', value: 'dancer' },
{ k: 'x', label: 'X', type: 'number', value: 500 },
{ k: 'y', label: 'Y', type: 'number', value: 250 },
{ k: 'prefix', label: 'Idle prefix', value: 'Dancer Idle' },
{ k: 'fps', label: 'FPS', type: 'number', value: 24 },
{ k: 'every', label: 'Dance every N beats', type: 'number', value: 2, min: 1 },
{ k: 'front', label: 'In front of characters', type: 'check', value: false }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'dancer'));
o.hook('onCreate', [
'makeAnimatedLuaSprite(' + tag + ', ' + q(clean(v.image, 'dancer')) + ', ' + n(v.x) + ', ' + n(v.y) + ')',
'addAnimationByPrefix(' + tag + ", 'idle', " + q(clean(v.prefix, 'Dancer Idle')) + ', ' + int(v.fps, 24) + ', false)',
'addLuaSprite(' + tag + ', ' + (v.front ? 'true' : 'false') + ')',
'playAnim(' + tag + ", 'idle', true)"
]);
o.hook('onBeatHit', ['if curBeat % ' + Math.max(1, +int(v.every, 2)) + ' == 0 then playAnim(' + tag + ", 'idle', true) end"]);
}
},
{
id: 'panel', name: 'HUD panel', desc: 'A solid color rectangle on the HUD.',
fields: [
{ k: 'tag', label: 'Tag', value: 'panel' },
{ k: 'x', label: 'X', type: 'number', value: 24 },
{ k: 'y', label: 'Y', type: 'number', value: 24 },
{ k: 'w', label: 'Width', type: 'number', value: 380 },
{ k: 'h', label: 'Height', type: 'number', value: 80 },
{ k: 'color', label: 'Color', type: 'color', value: '#14243A' },
{ k: 'alpha', label: 'Alpha', type: 'number', value: 1, step: 0.05, min: 0, max: 1 }
],
build: function (v, o) {
var t = clean(v.tag, 'panel'), tag = q(t);
var lines = [
'makeLuaSprite(' + tag + ', nil, ' + n(v.x) + ', ' + n(v.y) + ')',
'makeGraphic(' + tag + ', ' + int(v.w, 380) + ', ' + int(v.h, 80) + ', ' + q(hex(v.color)) + ')',
'setObjectCamera(' + tag + ", 'hud')"
];
if (n(v.alpha, 1) !== '1') lines.push('setProperty(' + q(t + '.alpha') + ', ' + n(v.alpha, 1) + ')');
lines.push('addLuaSprite(' + tag + ', true)');
o.hook('onCreate', lines);
}
},
{
id: 'text', name: 'HUD text', desc: 'Text drawn on the HUD camera.',
fields: [
{ k: 'tag', label: 'Tag', value: 'message' },
{ k: 'text', label: 'Text', value: 'Hello from Lua!' },
{ k: 'x', label: 'X', type: 'number', value: 40 },
{ k: 'y', label: 'Y', type: 'number', value: 45 },
{ k: 'w', label: 'Width', type: 'number', value: 350 },
{ k: 'size', label: 'Size', type: 'number', value: 24 },
{ k: 'color', label: 'Color', type: 'color', value: '#A7E8FF' }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'message'));
o.hook('onCreate', [
'makeLuaText(' + tag + ', ' + q(v.text == null ? '' : v.text) + ', ' + int(v.w, 350) + ', ' + n(v.x) + ', ' + n(v.y) + ')',
'setTextSize(' + tag + ', ' + int(v.size, 24) + ')',
'setTextColor(' + tag + ', ' + q(hex(v.color)) + ')',
'setObjectCamera(' + tag + ", 'hud')",
'addLuaText(' + tag + ')'
]);
}
},
{
id: 'bop', name: 'Camera bop', desc: 'Add zoom every few beats.',
fields: [
{ k: 'every', label: 'Every N beats', type: 'number', value: 4, min: 1 },
{ k: 'game', label: 'Game zoom add', type: 'number', value: 0.015, step: 0.005 },
{ k: 'hud', label: 'HUD zoom add', type: 'number', value: 0.03, step: 0.005 }
],
build: function (v, o) {
var lines = ['if curBeat % ' + Math.max(1, +int(v.every, 4)) + ' == 0 then'];
if (+v.game) lines.push(" setProperty('camGame.zoom', getProperty('camGame.zoom') + " + n(v.game) + ')');
if (+v.hud) lines.push(" setProperty('camHUD.zoom', getProperty('camHUD.zoom') + " + n(v.hud) + ')');
lines.push('end');
o.hook('onBeatHit', lines);
}
},
{
id: 'flash', name: 'Flash and shake on a beat', desc: 'Camera hit at one moment of the song.',
fields: [
{ k: 'beat', label: 'Beat', type: 'number', value: 16, min: 0 },
{ k: 'cam', label: 'Camera', type: 'select', options: ['game', 'hud'], value: 'game' },
{ k: 'color', label: 'Flash color', type: 'color', value: '#FFFFFF' },
{ k: 'dur', label: 'Flash seconds', type: 'number', value: 0.5, step: 0.05 },
{ k: 'shake', label: 'Shake intensity (0 = off)', type: 'number', value: 0.01, step: 0.005 },
{ k: 'shakeDur', label: 'Shake seconds', type: 'number', value: 0.3, step: 0.05 }
],
build: function (v, o) {
var cam = q(v.cam || 'game');
var lines = ['if curBeat == ' + int(v.beat, 16) + ' then', ' cameraFlash(' + cam + ', ' + q(hex(v.color)) + ', ' + n(v.dur, 0.5) + ', true)'];
if (+v.shake > 0) lines.push(' cameraShake(' + cam + ', ' + n(v.shake) + ', ' + n(v.shakeDur, 0.3) + ')');
lines.push('end');
o.hook('onBeatHit', lines);
}
},
{
id: 'hudfade', name: 'Fade the HUD', desc: 'Timer, then an alpha tween on camHUD.',
fields: [
{ k: 'delay', label: 'Delay seconds', type: 'number', value: 1, step: 0.1 },
{ k: 'alpha', label: 'Target alpha', type: 'number', value: 0.5, step: 0.05, min: 0, max: 1 },
{ k: 'dur', label: 'Duration', type: 'number', value: 0.4, step: 0.05 },
{ k: 'ease', label: 'Ease', type: 'select', options: EASES, value: 'quadOut' }
],
build: function (v, o) {
o.hook('onSongStart', ["runTimer('hudFadeDelay', " + n(v.delay, 1) + ')']);
o.hook('onTimerCompleted', ["if tag == 'hudFadeDelay' then", " doTweenAlpha('hudFade', 'camHUD', " + n(v.alpha, 0.5) + ', ' + n(v.dur, 0.4) + ', ' + q(v.ease || 'quadOut') + ')', 'end']);
}
},
{
id: 'notetype', name: 'Note type behavior', desc: 'Health and score for a custom note type.',
fields: [
{ k: 'kind', label: 'Note type', value: 'Bonus' },
{ k: 'hit', label: 'Health on hit', type: 'number', value: 0.04, step: 0.01 },
{ k: 'score', label: 'Score on hit', type: 'number', value: 50 },
{ k: 'miss', label: 'Health on miss', type: 'number', value: -0.08, step: 0.01 },
{ k: 'opp', label: 'Drain when the opponent hits it', type: 'number', value: 0, step: 0.01 }
],
build: function (v, o, s) {
var kind = q(s.scope === 'notetype' ? s.kind : clean(v.kind, 'Bonus'));
var hit = ['if noteType == ' + kind + ' and not isSustainNote then'];
if (+v.hit) hit.push(' addHealth(' + n(v.hit) + ')');
if (+v.score) hit.push(' addScore(' + int(v.score) + ')');
hit.push('end');
o.hook('goodNoteHit', hit);
if (+v.miss) o.hook('noteMiss', ['if noteType == ' + kind + ' then', ' addHealth(' + n(v.miss) + ')', 'end']);
if (+v.opp) o.hook('opponentNoteHit', ['if noteType == ' + kind + ' and getHealth() > ' + n(Math.abs(+v.opp) + 0.1) + ' then', ' addHealth(-' + n(Math.abs(+v.opp)) + ')', 'end']);
}
},
{
id: 'event', name: 'Chart event handler', desc: 'React to a chart event using its two values.',
fields: [
{ k: 'name', label: 'Event name', value: 'My Event' },
{ k: 'action', label: 'Action', type: 'select', options: ['flash (value1 = seconds)', 'shake (value1 = intensity, value2 = seconds)', 'print values'], value: 'flash (value1 = seconds)' }
],
build: function (v, o, s) {
var nm = q(s.scope === 'event' ? s.event : clean(v.name, 'My Event'));
var act = /^flash/.test(v.action) ? " cameraFlash('game', 'FFFFFF', tonumber(value1) or 0.5, true)"
: /^shake/.test(v.action) ? " cameraShake('game', tonumber(value1) or 0.01, tonumber(value2) or 0.3)"
: " debugPrint(name, value1, value2)";
o.hook('onEvent', ['if name == ' + nm + ' then', act, 'end']);
}
},
{
id: 'focus', name: 'Focus camera on a beat', desc: 'Trigger the V-Slice FocusCamera event.',
fields: [
{ k: 'beat', label: 'Beat', type: 'number', value: 16, min: 0 },
{ k: 'char', label: 'Target', type: 'select', options: ['0 player', '1 opponent', '2 girlfriend'], value: '0 player' },
{ k: 'x', label: 'X offset', type: 'number', value: 0 },
{ k: 'y', label: 'Y offset', type: 'number', value: -40 },
{ k: 'dur', label: 'Duration (steps)', type: 'number', value: 4 },
{ k: 'ease', label: 'Ease', type: 'select', options: ['linear', 'quad', 'cube', 'sine', 'expo', 'back'], value: 'quad' }
],
build: function (v, o) {
o.hook('onBeatHit', [
'if curBeat == ' + int(v.beat, 16) + ' then',
" triggerVSliceEvent('FocusCamera', {",
' char = ' + int(String(v.char || '0').charAt(0)) + ',',
' x = ' + n(v.x) + ',',
' y = ' + n(v.y) + ',',
' duration = ' + n(v.dur, 4) + ',',
' ease = ' + q(v.ease || 'quad') + ',',
" easeDir = 'Out'",
' })',
'end'
]);
}
},
{
id: 'character', name: 'Change character on a beat', desc: 'Preload and swap a V-Slice character.',
fields: [
{ k: 'beat', label: 'Beat', type: 'number', value: 64, min: 0 },
{ k: 'role', label: 'Role', type: 'select', options: ['bf', 'dad', 'gf'], value: 'dad' },
{ k: 'id', label: 'Character id', value: 'pico' }
],
build: function (v, o) {
var id = q(clean(v.id, 'pico')), role = q(v.role || 'dad');
o.hook('onCreate', ['addCharacterToList(' + id + ', ' + role + ')']);
o.hook('onBeatHit', ['if curBeat == ' + int(v.beat, 64) + ' then', " triggerEvent('Change Character', " + role + ', ' + id + ')', 'end']);
}
},
{
id: 'sound', name: 'Looping sound', desc: 'Ambience that pauses with the game.',
fields: [
{ k: 'tag', label: 'Tag', value: 'ambience' },
{ k: 'key', label: 'Sound', value: 'rain' },
{ k: 'vol', label: 'Volume', type: 'number', value: 0.4, step: 0.05, min: 0, max: 1 },
{ k: 'loop', label: 'Loop', type: 'check', value: true }
],
build: function (v, o) {
var tag = q(clean(v.tag, 'ambience'));
o.hook('onCreate', ['playSound(' + q(clean(v.key, 'rain')) + ', ' + n(v.vol, 1) + ', ' + tag + ', ' + (v.loop ? 'true' : 'false') + ')']);
o.hook('onPause', ['pauseSound(' + tag + ')']);
o.hook('onResume', ['resumeSound(' + tag + ')']);
o.hook('onDestroy', ['stopSound(' + tag + ')']);
}
},
{
id: 'save', name: 'Save counter', desc: 'Count something across sessions.',
fields: [{ k: 'file', label: 'Save name', value: 'settings' }, { k: 'key', label: 'Field', value: 'visits' }],
build: function (v, o) {
var f = q(clean(v.file, 'settings')), k = q(clean(v.key, 'visits'));
o.hook('onCreate', [
'initSaveData(' + f + ')',
'local count = getDataFromSave(' + f + ', ' + k + ', 0) + 1',
'setDataFromSave(' + f + ', ' + k + ', count)',
'flushSaveData(' + f + ')'
]);
}
},
{
id: 'overlay', name: 'Pause-style overlay', desc: 'A custom substate opened with a key.',
fields: [
{ k: 'key', label: 'Open key', value: 'F7' },
{ k: 'name', label: 'Substate name', value: 'info' },
{ k: 'text', label: 'Text', value: 'Press Escape to return' }
],
build: function (v, o) {
var nm = q(clean(v.name, 'info')), tag = q(ident(v.name, 'info') + 'Text');
o.hook('onUpdate', ['if keyboardJustPressed(' + q(clean(v.key, 'F7').toUpperCase()) + ') then', ' openCustomSubstate(' + nm + ', true)', 'end']);
o.hook('onCustomSubstateCreate', [
'if name == ' + nm + ' then',
' makeLuaText(' + tag + ', ' + q(v.text == null ? '' : v.text) + ', 600, 80, 80)',
' setObjectCamera(' + tag + ", 'other')",
' addToCustomSubstate(' + tag + ')',
'end'
]);
o.hook('onCustomSubstateUpdate', ['if name == ' + nm + " and keyboardJustPressed('ESCAPE') then", ' closeCustomSubstate()', 'end']);
}
},
{
id: 'haxe', name: 'Haxe callback', desc: 'Expose an HScript function to Lua with createCallback.',
fields: [{ k: 'fn', label: 'Function name', value: 'addExtraPoints' }, { k: 'bonus', label: 'Bonus', type: 'number', value: 25 }],
build: function (v, o) {
var fn = ident(v.fn, 'addExtraPoints');
o.hook('onCreatePost', [
'runHaxeCode([[',
" setVar('extraPoints', " + int(v.bonus, 25) + ');',
" createCallback('" + fn + "', function(points) {",
" return points + getVar('extraPoints', 0);",
' });',
']])',
'debugPrint(' + fn + '(100))'
]);
}
}
]
}
};
var PRESETS = {
luaslice: [
{ name: 'Song starter', scope: 'song', on: ['guard', 'hello', 'bop', 'flash'] },
{ name: 'Stage', scope: 'stage', on: ['sprite', 'anim', 'sound'] },
{ name: 'Heal note', scope: 'global', on: ['notekind'] },
{ name: 'Menu mod', scope: 'menu', on: ['menu', 'options'] }
],
psych: [
{ name: 'Song starter', scope: 'song', on: ['hello', 'bop', 'flash', 'hudfade'] },
{ name: 'Stage', scope: 'stage', on: ['sprite', 'anim', 'sound'] },
{ name: 'Note type', scope: 'notetype', on: ['notetype'] },
{ name: 'Chart event', scope: 'event', on: ['event'] }
]
};
var SCOPE_FIELDS = {
song: { label: 'Song id', value: 'tutorial' },
stage: { label: 'Stage id', value: 'mainStage' },
character: { label: 'Character id', value: 'bf' },
name: { label: 'Script name', value: 'script' },
kind: { label: 'Note type', value: 'Bonus' },
event: { label: 'Event name', value: 'My Event' }
};
function defaults() {
var st = { api: 'luaslice', scope: 'song', mod: 'my-mod', global: false, indent: 4, f: {} };
Object.keys(SCOPE_FIELDS).forEach(function (k) { st[k] = SCOPE_FIELDS[k].value; });
Object.keys(APIS).forEach(function (a) {
st.f[a] = {};
APIS[a].features.forEach(function (ft) {
var vals = { on: false };
ft.fields.forEach(function (fd) { vals[fd.k] = fd.value; });
st.f[a][ft.id] = vals;
});
});
st.f.luaslice.hello.on = true;
st.f.luaslice.bop.on = true;
st.f.psych.hello.on = true;
return st;
}
function b64e(str) {
var bytes = new TextEncoder().encode(str), bin = '';
for (var i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
function b64d(s) {
s = s.replace(/-/g, '+').replace(/_/g, '/');
while (s.length % 4) s += '=';
var bin = atob(s), bytes = new Uint8Array(bin.length);
for (var i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return new TextDecoder().decode(bytes);
}
function diff(st) {
var d = defaults(), out = {};
Object.keys(st).forEach(function (k) { if (k !== 'f' && st[k] !== d[k]) out[k] = st[k]; });
var f = {};
Object.keys(st.f[st.api]).forEach(function (id) {
var cur = st.f[st.api][id], def = d.f[st.api][id], ch = {};
Object.keys(cur).forEach(function (k) { if (cur[k] !== def[k]) ch[k] = cur[k]; });
if (Object.keys(ch).length) f[id] = ch;
});
out.f = f;
out.api = st.api;
return out;
}
function fromShared(src) {
var st = defaults();
if (!src || typeof src !== 'object') return st;
if (APIS[src.api]) st.api = src.api;
Object.keys(st).forEach(function (k) { if (k !== 'f' && k !== 'api' && src[k] != null) st[k] = src[k]; });
var f = src.f || {};
Object.keys(st.f[st.api]).forEach(function (id) {
var cur = st.f[st.api][id], v = f[id] || {};
cur.on = v.on === true;
Object.keys(cur).forEach(function (k) { if (k !== 'on' && v[k] != null) cur[k] = v[k]; });
});
if (!APIS[st.api].scopes[st.scope]) st.scope = 'song';
return st;
}
function load() {
var st = defaults();
var shared = /^#generator\/c=([A-Za-z0-9_-]+)/.exec(location.hash);
if (shared) {
try { return fromShared(JSON.parse(b64d(shared[1]))); } catch (e) {}
}
try {
var saved = JSON.parse(localStorage.getItem(STORE_KEY) || 'null');
if (saved && typeof saved === 'object') {
Object.keys(st).forEach(function (k) { if (k !== 'f' && saved[k] != null) st[k] = saved[k]; });
if (saved.f) Object.keys(st.f).forEach(function (a) {
Object.keys(st.f[a]).forEach(function (id) {
var src = saved.f[a] && saved.f[a][id];
if (src) Object.keys(st.f[a][id]).forEach(function (k) { if (src[k] != null) st.f[a][id][k] = src[k]; });
});
});
}
} catch (e) {}
if (!APIS[st.api]) st.api = 'luaslice';
if (!APIS[st.api].scopes[st.scope]) st.scope = 'song';
return st;
}
function slugPart(v, d) { return String(v || '').trim().replace(/[\\/:*?"<>|]+/g, '-') || d; }
function generate(st) {
var api = APIS[st.api], scope = api.scopes[st.scope];
var hooks = {}, firsts = {};
var ctx = {
scope: st.scope,
kind: clean(st.kind, 'Bonus'),
event: clean(st.event, 'My Event')
};
var out = {
hook: function (name, lines) { (hooks[name] = hooks[name] || []).push(lines); },
first: function (name, lines) { (firsts[name] = firsts[name] || []).push(lines); }
};
var used = [];
api.features.forEach(function (ft) {
var v = st.f[st.api][ft.id];
if (!v || !v.on || !featureAllowed(st, ft)) return;
used.push(ft.name);
ft.build(v, out, ctx);
});
var pad = st.indent === 2 ? ' ' : st.indent === 't' ? '\t' : ' ';
var names = api.order.slice();
Object.keys(hooks).concat(Object.keys(firsts)).forEach(function (h) { if (names.indexOf(h) < 0) names.push(h); });
var blocks = [];
names.forEach(function (h) {
var parts = (firsts[h] || []).concat(hooks[h] || []);
if (!parts.length) return;
var body = [];
parts.forEach(function (lines, i) {
if (i) body.push('');
lines.forEach(function (l) { body.push(l ? pad + l.replace(/^( {4})+/, function (m) { return new Array(m.length / 4 + 1).join(pad); }) : ''); });
});
blocks.push('function ' + h + '(' + (api.sigs[h] || '') + ')\n' + body.join('\n') + '\nend');
});
var code = blocks.length ? blocks.join('\n\n') + '\n' : 'function onCreate()\nend\n';
var path = scope.path({
mod: slugPart(st.mod, 'my-mod'),
song: slugPart(st.song, 'tutorial'),
stage: slugPart(st.stage, 'mainStage'),
character: slugPart(st.character, 'bf'),
name: slugPart(st.name, 'script'),
kind: slugPart(st.kind, 'Bonus'),
event: slugPart(st.event, 'My Event')
}, api.ext(scope.global && st.global));
return { code: code, path: path, file: path.split('/').pop(), used: used };
}
function featureAllowed(st, ft) {
if (st.api === 'luaslice' && st.scope === 'menu' && ft.gameplay) return false;
return true;
}
function el(tag, attrs, kids) {
var e = document.createElement(tag);
if (attrs) Object.keys(attrs).forEach(function (k) {
if (k === 'text') e.textContent = attrs[k];
else if (k === 'cls') e.className = attrs[k];
else if (k.slice(0, 2) === 'on') e.addEventListener(k.slice(2), attrs[k]);
else if (attrs[k] === true) e.setAttribute(k, '');
else if (attrs[k] !== false && attrs[k] != null) e.setAttribute(k, attrs[k]);
});
(kids || []).forEach(function (c) { if (c) e.appendChild(typeof c === 'string' ? document.createTextNode(c) : c); });
return e;
}
var uid = 0;
function field(def, value, onChange) {
var id = 'g' + (++uid);
var input;
if (def.type === 'select') {
input = el('select', { id: id }, def.options.map(function (o) { return el('option', { value: o, text: o, selected: o === value }); }));
} else if (def.type === 'check') {
input = el('input', { id: id, type: 'checkbox', checked: !!value });
} else if (def.type === 'color') {
input = el('input', { id: id, type: 'color', value: hash(value).toLowerCase() });
} else {
input = el('input', { id: id, type: def.type === 'number' ? 'number' : 'text', value: value == null ? '' : value, step: def.step || (def.type === 'number' ? 'any' : null), min: def.min, max: def.max, spellcheck: 'false', autocomplete: 'off' });
}
var handler = function () {
var val = def.type === 'check' ? input.checked : def.type === 'color' ? input.value.toUpperCase() : input.value;
onChange(val);
};
input.addEventListener(def.type === 'check' || def.type === 'select' ? 'change' : 'input', handler);
return el('label', { cls: 'gen-field' + (def.type === 'check' ? ' gen-check' : ''), for: id }, def.type === 'check' ? [input, el('span', { text: def.label })] : [el('span', { text: def.label }), input]);
}
function mount(root, helpers) {
var st = load();
var saveTimer = 0;
function persist() {
clearTimeout(saveTimer);
saveTimer = setTimeout(function () { try { localStorage.setItem(STORE_KEY, JSON.stringify(st)); } catch (e) {} }, 250);
}
root.innerHTML = '';
var intro = el('div', { cls: 'gen-intro' }, [
el('h1', { text: 'Script generator' }),
el('p', { text: 'Pick an API, where the script runs, and the pieces you want. The code updates as you type, then copy it or download the file with the right name and folder.' })
]);
var apiRow = el('div', { cls: 'gen-tabs', role: 'tablist', 'aria-label': 'API' });
var presetRow = el('div', { cls: 'gen-presets' });
var scopeBox = el('div', { cls: 'gen-grid' });
var featBox = el('div', { cls: 'gen-features' });
var pathEl = el('code', { cls: 'gen-path' });
var codeEl = el('code', { cls: 'language-lua' });
var pre = el('pre', null, [codeEl]);
var usedEl = el('p', { cls: 'gen-used' });
var docLink = el('a', { cls: 'gen-doc', href: '#api-overview', text: 'API docs' });
var copyBtn = el('button', { type: 'button', cls: 'text-btn', text: 'Copy' });
var dlBtn = el('button', { type: 'button', cls: 'text-btn solid', text: 'Download' });
var pathBtn = el('button', { type: 'button', cls: 'text-btn', text: 'Copy path' });
var resetBtn = el('button', { type: 'button', cls: 'link-btn', text: 'Reset everything' });
var tryBtn = el('button', { type: 'button', cls: 'text-btn', text: 'Try in playground ▶' });
var shareBtn = el('button', { type: 'button', cls: 'text-btn', text: 'Share link' });
var left = el('div', { cls: 'gen-form' }, [
el('h2', { text: 'API' }), apiRow,
el('h3', { text: 'Quick start' }), presetRow,
el('h2', { text: 'Where it runs' }), scopeBox,
el('h2', { text: 'Features' }), featBox,
resetBtn
]);
var right = el('div', { cls: 'gen-out' }, [
el('div', { cls: 'gen-out-head' }, [el('span', { text: 'Save as' }), pathEl]),
el('div', { cls: 'code gen-code' }, [pre]),
el('div', { cls: 'gen-actions' }, [copyBtn, pathBtn, dlBtn]),
el('div', { cls: 'gen-actions gen-actions-2' }, [tryBtn, shareBtn]),
usedEl, docLink
]);
var jump = el('button', { type: 'button', cls: 'gen-jump', text: 'View code ↓' });
jump.addEventListener('click', function () { right.scrollIntoView({ behavior: 'smooth', block: 'start' }); });
root.appendChild(intro);
root.appendChild(el('div', { cls: 'gen-wrap' }, [left, right]));
root.appendChild(jump);
if ('IntersectionObserver' in window) {
var seen = new IntersectionObserver(function (entries) {
entries.forEach(function (en) { jump.hidden = en.isIntersecting; });
});
seen.observe(right);
var stop = new MutationObserver(function () {
if (!root.contains(right)) { seen.disconnect(); stop.disconnect(); }
});
stop.observe(root, { childList: true });
}
var current = null;
var raf = 0;
function update() {
cancelAnimationFrame(raf);
raf = requestAnimationFrame(function () {
current = generate(st);
pathEl.textContent = current.path;
codeEl.textContent = current.code;
codeEl.className = 'language-lua';
codeEl.removeAttribute('data-highlighted');
if (window.hljs) try { hljs.highlightElement(codeEl); } catch (e) {}
usedEl.textContent = current.used.length ? current.used.length + ' feature' + (current.used.length > 1 ? 's' : '') + ': ' + current.used.join(', ') : 'No features selected. Pick some on the left.';
docLink.href = APIS[st.api].doc;
docLink.textContent = APIS[st.api].name + ' docs';
persist();
});
}
function drawApi() {
apiRow.innerHTML = '';
Object.keys(APIS).forEach(function (a) {
apiRow.appendChild(el('button', {
type: 'button', role: 'tab', cls: 'gen-tab' + (st.api === a ? ' on' : ''), 'aria-selected': st.api === a ? 'true' : 'false',
text: APIS[a].name + (a === 'luaslice' ? ' (.lua)' : ' (.luap)'),
onclick: function () {
if (st.api === a) return;
st.api = a;
if (!APIS[a].scopes[st.scope]) st.scope = 'song';
drawAll();
}
}));
});
}
function drawPresets() {
presetRow.innerHTML = '';
PRESETS[st.api].forEach(function (p) {
presetRow.appendChild(el('button', {
type: 'button', cls: 'chip-btn', text: p.name,
onclick: function () {
st.scope = p.scope;
Object.keys(st.f[st.api]).forEach(function (id) { st.f[st.api][id].on = p.on.indexOf(id) >= 0; });
drawAll();
helpers.toast(p.name + ' preset applied');
}
}));
});
}
function drawScope() {
scopeBox.innerHTML = '';
var api = APIS[st.api];
scopeBox.appendChild(field({ label: 'Runs in', type: 'select', options: Object.keys(api.scopes).map(function (k) { return api.scopes[k].name; }) }, api.scopes[st.scope].name, function (val) {
Object.keys(api.scopes).forEach(function (k) { if (api.scopes[k].name === val) st.scope = k; });
drawScope();
drawFeatures();
update();
}));
scopeBox.appendChild(field({ label: 'Mod folder' }, st.mod, function (v) { st.mod = v; update(); }));
api.scopes[st.scope].fields.forEach(function (k) {
scopeBox.appendChild(field(SCOPE_FIELDS[k], st[k], function (v) { st[k] = v; update(); }));
});
if (api.scopes[st.scope].global) {
scopeBox.appendChild(field({ label: 'Global shared environment (.luag)', type: 'check' }, st.global, function (v) { st.global = v; update(); }));
}
scopeBox.appendChild(field({ label: 'Indent', type: 'select', options: ['4 spaces', '2 spaces', 'tabs'] }, st.indent === 2 ? '2 spaces' : st.indent === 't' ? 'tabs' : '4 spaces', function (v) {
st.indent = v === '2 spaces' ? 2 : v === 'tabs' ? 't' : 4;
update();
}));
}
function drawFeatures() {