forked from itsyourbedtime/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorca.lua
More file actions
1687 lines (1594 loc) · 52.1 KB
/
orca.lua
File metadata and controls
1687 lines (1594 loc) · 52.1 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
-- ORCA for norns
--
-- its your bedtime
--
-- X
--
-- hundred rabbits
--
function unrequire(name)
package.loaded[name] = nil
_G[name] = nil
end
unrequire("timber/lib/timber_engine")
engine.name = "Timber"
Timber = require "timber/lib/timber_engine"
local music = require "musicutil"
local NUM_SAMPLES = 35
local keyb = hid.connect()
local keycodes = include("orca/lib/keycodes")
local transpose_table = include("orca/lib/transpose")
local tab = require 'tabutil'
local fileselect = require "fileselect"
local textentry = require "textentry"
local music = require 'musicutil'
local BeatClock = require 'beatclock'
local mode = #music.SCALES
local scale = music.generate_scale_of_length(60,music.SCALES[mode].name,16)
local midi_out_device
local clk = BeatClock.new()
local notes_off_metro = metro.init()
local active_notes = {}
local keyinput = ""
local XSIZE = 101
local YSIZE = 33
local x_index = 1
local y_index = 1
local bar = false
local help = false
local field_grid = 1
local field_offset_y = 0
local field_offset_x = 0
local bounds_x = 25
local bounds_y = 8
local sc_ops = 0
local max_sc_ops = 6
local frame = 1
local ops = {}
local field = {}
field.cell = {}
field.cell.params = {}
field.active = {}
local vars = {}
local map = false
local main_menu = false
local load_menu = false
local projects = {}
local menu_index = 1
local menu_entries = {{'New', function() init() main_menu = false menu_index = 1 end},
{'Load', function() main_menu = false load_menu = true end},
{'Save', function() textentry.enter(ops.save_project, 'untitled' ) end}
}
local function all_notes_off(ch)
for _, a in pairs(active_notes) do
midi_out_device:note_off(a, nil, ch)
end
active_notes = {}
end
ops.load_project = function(pth)
if string.find(pth, 'orca') ~= nil then
saved = tab.load(pth)
if saved ~= nil then
print("data found")
field = saved
local name = string.sub(string.gsub(pth, '%w+/',''),2,-6)
softcut.buffer_read_mono(norns.state.data .. name .. '_buffer.aif', 0, 0, #ops.chars, 1, 1)
params:read(norns.state.data .. name ..".pset")
print ('loaded ' .. norns.state.data .. name .. '_buffer.aif')
load_menu = false
main_menu = false
else
print("no data")
end
end
end
ops.save_project = function(txt)
if txt then
tab.save(field, norns.state.data .. txt ..".orca")
softcut.buffer_write_mono(norns.state.data..txt .."_buffer.aif",0,#ops.chars, 1)
params:write(norns.state.data .. txt .. ".pset")
print ('saved ' .. norns.state.data .. txt .. '_buffer.aif')
else
print("save cancel")
end
end
ops.list = {
["*"] = '*',
[':'] = ':',
["'"] = "'",
['/'] = '/',
['\\']= '\\',
["A"] = 'A',
["B"] = 'B',
["C"] = 'C',
["D"] = 'D',
["E"] = 'E',
["F"] = 'F',
-- ['G']
['H'] = 'H',
["I"] = 'I',
["J"] = 'J',
['K'] = 'K',
["L"] = 'L',
["M"] = 'M',
["N"] = 'N',
['O'] = 'O',
["P"] = 'P',
-- ['Q']
["R"] = 'R',
["S"] = 'S',
['T'] = 'T',
-- ['U']
["V"] = 'V',
["W"] = 'W',
["X"] = 'X',
['Y'] = 'Y',
["Z"] = 'Z'
}
ops.bangs ={
["E"] = 'E',
["W"] = 'W',
["S"] = 'S',
["N"] = 'N',
['Z'] = 'Z'
}
ops.names = {
["*"] = 'bang',
[':'] = 'midi',
["'"] = 'engine',
['/'] = 'softcut',
['\\']= 'r note',
["A"] = 'add',
["B"] = 'bounce',
["C"] = 'clock',
["D"] = 'delay',
["E"] = 'east',
["F"] = 'if',
-- ['G']
['H'] = 'halt',
["I"] = 'increment',
["J"] = 'jumper',
['K'] = 'konkat',
["L"] = 'loop',
["M"] = 'modulo',
["N"] = 'north',
['O'] = 'offset',
["P"] = 'push',
-- ['Q']
["R"] = 'random',
["S"] = 'south',
['T'] = 'track',
-- ['U']
["V"] = 'variable',
["W"] = 'west',
["X"] = 'write',
['Y'] = 'jymper',
["Z"] = 'zoom'
}
ops.info = {
['*'] = 'Bangs neighboring operators.',
[':'] = 'Midi 1-channel 2-octave 3-note 4-velocity 5-length',
["'"] = 'Engine 1-sample 2-pitch 3-pitch 4-level 5-pos',
['/'] = 'Softcut 1-plhead 2-rec 3-play 4-level 5-pos',
['\\']= 'Outputs random note within octave',
['='] = 'Sends a OSC message',
['A'] = 'Outputs the sum of inputs.',
['B'] = 'Bounces between two values based on the runtime frame.',
['C'] = 'Outputs a constant value based on the runtime frame.',
['D'] = 'Bangs on a fraction of the runtime frame.',
['E'] = 'Moves eastward, or bangs.',
['F'] = 'Bangs if both inputs are equal.',
['G'] = '',
['H'] = 'Stops southward operator from operating.',
['I'] = 'Increments southward operator.',
['J'] = 'Outputs the northward operator.',
['K'] = '',
['L'] = 'Loops a number of eastward operators.',
['M'] = 'Outputs the modulo of input.',
['N'] = 'Moves northward, or bangs.',
['O'] = 'Reads a distant operator with offset.',
['P'] = 'Writes an eastward operator with offset.',
['Q'] = '',
['R'] = 'Outputs a random value.',
['S'] = 'Moves southward, or bangs.',
['T'] = 'Reads an eastward operator with offset',
['U'] = '',
['V'] = 'Reads and writes globally available variable',
['W'] = 'Moves westward, or bangs.',
['X'] = 'Writes a distant operator with offset',
['Y'] = 'Outputs the westward operator',
['Z'] = 'Moves easwardly, respawns west on collision',
}
ops.ports = {
[':'] = {{1, 0, 'input_op'}, {2, 0, 'input_op'}, {3, 0 , 'input_op'}, {4, 0 , 'input_op'}, {5, 0 , 'input_op'}},
["'"] = {{1, 0, 'input_op'}, {2, 0, 'input_op'}, {3, 0 , 'input_op'}, {4, 0 , 'input_op'}, {5,0, 'input_op'}},
['/'] = {{1, 0, 'input_op'}, {2, 0, 'input_op'}, {3, 0 , 'input_op'}, {4, 0 , 'input_op'}, {5, 0 , 'input_op'}, {6, 0 , 'input_op'}},
['\\']= {{1, 0, 'input'}, {-1, 0, 'input'}, {0, 1 , 'output_op'}},
['A'] = {{1, 0, 'input_op'}, {-1, 0, 'input_op'}, {0, 1 , 'output_op'}},
['B'] = {{1, 0, 'input'}, {-1, 0, 'input'}, {0, 1 , 'output'}},
['C'] = {{1, 0, 'input_op'}, {-1, 0, 'input'}, {0, 1 , 'output'}},
['D'] = {{1, 0, 'input_op'}, {-1, 0, 'input'}, {0, 1 , 'output'}},
['F'] = {{1, 0, 'input'}, {-1, 0, 'input'}, {0, 1 , 'output_op'}},
['H'] = {{0, 1, 'output'}},
['J'] = {{0, -1, 'input'}, {0, 1, 'output_op'}},
['K'] = {{-1, 0, 'input'}},
['L'] = {{-1, 0, 'input'}, {-2, 0, 'input'}},
['I'] = {{1, 0, 'input'}, {-1, 0, 'input'}, {0, 1 , 'output'}},
['O'] = {{-1, 0, 'input'}, {-2, 0, 'input'}, {0, 1 , 'input_op'}},
['M'] = {{-1, 0, 'input'}, {1, 0, 'input'}, {0, 1 , 'output'}},
['P'] = {{1, 0, 'input_op'}, {-1, 0, 'input'}, {-2, 0, 'input'}},
['T'] = {{-1,0, 'input_op'}, {-2, 0, 'input_op'}, {1, 0, 'input_op'}, {0, 1 , 'output_op'}},
['R'] = {{-1, 0, 'input'}, {1, 0, 'input'}, {0, 1 , 'output_op'}},
['X'] = {{1, 0, 'input_op'}, {-1, 0, 'input'}, {-2, 0 , 'input'}},
['V'] = {{-1,0, 'input_op'}, {1, 0, 'input_op'}},
['Y'] = {{-1, 0, 'input'}, {1, 0, 'output_op'}},
['W'] = {},
['S'] = {},
['E'] = {},
['N'] = {},
['Z'] = {},
['*'] = {},
}
ops.notes = {"C", "c", "D", "d", "E", "F", "f", "G", "g", "A", "a", "B"}
ops.chars = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
ops.chars[0] = '0'
function ops.normalize(n)
return n == 'e' and 'F' or n == 'b' and 'C' or n
end
function ops.transpose(n, o)
if n == nil or n == 'null' then n = 'C' else n = tostring(n) end
if o == nil or o == 'null' then o = 0 end
local note = ops.normalize(string.sub(transpose_table[n], 1, 1))
local octave = util.clamp(ops.normalize(string.sub(transpose_table[n], 2)) + o,0,8)
local value = tab.key(ops.notes, note)
local id = util.clamp((octave * 12) + value, 0, 127)
local real = id < 89 and tab.key(transpose_table, id - 45) or nil -- ??
return {id, value, note, octave, real}
end
function ops:input(x,y, default)
local value = string.lower(tostring(field.cell[y][x]))
if value == '0' then
return 0
elseif value ~= nil or value ~= 'null' then
return tab.key(ops.chars, value)
else
return false
end
end
function ops.is_op(x,y)
x = util.clamp(x,0,XSIZE)
y = util.clamp(y,0,YSIZE)
if (field.cell[y][x] ~= 'null' and field.cell[y][x] ~= nil) then
if (ops.list[string.upper(field.cell[y][x])] and field.cell.params[y][x].op) then
if ops.list[string.upper(field.cell[y][x])] and field.cell.params[y][x].act == true then
return true
end
end
else
return false
end
end
function ops.is_bang(x,y)
if (field.cell[y][x] ~= 'null' and field.cell[y][x] ~= nil) then
if (ops.bangs[string.upper(field.cell[y][x])] and field.cell.params[y][x].op) then
return true
end
else
return false
end
end
function ops.banged(x,y)
if field.cell[y][x - 1] == '*' then -- or ops.is_bang(x - 1, y) then
return true
elseif field.cell[y][x + 1] == '*' then --or ops.is_bang(x + 1, y) then
return true
elseif field.cell[y - 1][x] == '*' then --or ops.is_bang(x, y - 1) then
return true
elseif field.cell[y + 1][x] == '*' then -- or ops.is_bang(x, y + 1) then
return true
else
return false
end
end
function ops:active()
if field.cell.params[self.y][self.x].op == true then
if field.cell[self.y][self.x] == string.upper(field.cell[self.y][self.x]) then
return true
elseif field.cell[self.y][self.x] == string.lower(field.cell[self.y][self.x]) then
return false
end
end
end
function ops:replace(i)
field.cell[self.y][self.x] = i
end
function ops:shift(s, e)
local data = field.cell[self.y][self.x + s]
local params_data = field.cell.params[self.y][self.x + s]
table.remove(field.cell[self.y], self.x + s)
table.remove(field.cell.params[self.y], self.x + s)
table.insert(field.cell[self.y], self.x + e, data)
table.insert(field.cell.params[self.y], self.x + e, params_data)
end
function ops:cleanup()
if ops.is_op(self.x, self.y) then self:clean_ports(ops.ports[string.upper(field.cell[self.y][self.x])]) end
field.cell.params[self.y][self.x] = {op = true, lit = false, lit_out = false, act = true, cursor = false, dot_cursor = false, dot_port = false, dot = false, placeholder = nil}
if field.cell.params[self.y+1][self.x].cursor == true then field.cell.params[self.y+1][self.x].cursor = false end
if field.cell.params[self.y][self.x + 1].op == false then field.cell.params[self.y][self.x + 1].op = true end
if field.cell.params[self.y + 1][self.x].lit_out == true then field.cell.params[self.y + 1][self.x].lit_out = false end
-- specific ops cleanup
if (field.cell[self.y][self.x] == 'P' or field.cell[self.y][self.x] == 'p') then
local seqlen = tonumber(ops:input(self.x - 1, self.y)) or 1
for i=0, seqlen do
field.cell.params[self.y + 1][self.x + i].op = true
field.cell.params[self.y + 1][self.x + i].lit = false
field.cell.params[self.y + 1][self.x + i].cursor = false
field.cell.params[self.y + 1][self.x + i].dot = false
end
elseif (field.cell[self.y][self.x] == 'T' or field.cell[self.y][self.x] == 't') then
local seqlen = tonumber(ops:input(self.x - 1, self.y)) or 1
field.cell.params[self.y+1][self.x].lit_out = false
for i=1, seqlen do
field.cell.params[self.y][self.x + i].op = true
field.cell.params[self.y][self.x + i].cursor = false
field.cell.params[self.y][self.x + i].dot = false
end
elseif (field.cell[self.y][self.x] == 'K' or field.cell[self.y][self.x] == 'k') then
local seqlen = tonumber(ops:input(self.x - 1, self.y)) or 1
for i=1,seqlen do
field.cell.params[self.y][self.x + i].dot = false
field.cell.params[self.y][(self.x + i)].op = true
field.cell.params[self.y][(self.x + i)].act = true
field.cell.params[self.y + 1][(self.x + i)].act = true
end
elseif (field.cell[self.y][self.x] == 'L' or field.cell[self.y][self.x] == 'l') then
local seqlen = tonumber(ops:input(self.x - 1, self.y)) or 1
for i=1,seqlen do
field.cell.params[self.y][self.x + i].dot = false
field.cell.params[self.y][(self.x + i)].op = true
end
elseif (field.cell[self.y][self.x] == 'H' or field.cell[self.y][self.x] == 'h') then
field.cell.params[self.y + 1][self.x].act = true
elseif (field.cell[self.y][self.x] == 'X' or field.cell[self.y][self.x] == 'x') then
local a = tonumber(field.cell[self.y][self.x - 2]) or 0 -- x
local b = util.clamp(tonumber(field.cell[self.y][self.x - 1]) or 1, 1, 9) -- y
field.cell.params[self.y + b][self.x + a].dot_cursor = false
field.cell.params[self.y + b][self.x + a].cursor = false
field.cell.params[self.y + b][self.x + a].placeholder = nil
elseif (field.cell[self.y][self.x] == "'" or field.cell[self.y][self.x] == ':' or field.cell[self.y][self.x] == '/') then
if field.cell[self.y][self.x] == '/' then sc_ops = util.clamp(sc_ops - 1,1,max_sc_ops) softcut.play(field.cell[self.y][self.x + 1]~= 'null' and field.cell[self.y][self.x + 1] or sc_ops,0) end
if field.cell[self.y][self.x] == "'" then local sample = ops:input(self.x + 1, self.y) or 0 engine.noteOff(sample) end
end
end
function ops:erase(x,y)
self.x = x
self.y = y
if self:active() then
self:cleanup()
end
ops:remove_from_queue(self.x,self.y)
self:replace('null')
end
function ops:explode()
self:replace('*')
self:add_to_queue(self.x,self.y)
end
function ops:id(x, y)
return tostring(x .. ":" .. y)
end
function ops:add_to_queue(x,y)
x = util.clamp(x,1,XSIZE + 1)
y = util.clamp(y,1,YSIZE + 1)
field.active[ops:id(x,y)] = {x, y, field.cell[y][x]}
end
function ops.removeKey(t, k_to_remove)
local new = {}
for k, v in pairs(t) do
new[k] = v
end
new[k_to_remove] = nil
return new
end
function ops:remove_from_queue(x,y)
self.x = x
self.y = y
field.active = ops.removeKey(field.active, ops:id(self.x,self.y))
end
function ops:exec_queue()
frame = (frame + 1) % 999
for k,v in pairs(field.active) do
if k ~= nil then
local x = field.active[k][1]
local y = field.active[k][2]
local op = field.active[k][3]
if op ~= 'null' and ops.is_op(x,y) then
ops[string.upper(op)](self, x, y, frame)
end
end
end
end
function ops:move(x,y)
a = self.y + y
b = self.x + x
local collider = field.cell[a][b]
-- collide rules
if collider ~= 'null' then
if field.cell[a][b] ~= nil then
if collider == '*' then
ops:move_cell(b,a)
if field.cell[self.y][self.x] == 'Z' then
ops:move_cell(1,a)
self:explode()
end
self:erase(self.x,self.y)
elseif field.cell.params[a][b].op == false or (collider == '.' and field.cell.params[a][b].op == false) then
ops:move_cell(b,a)
elseif (
(collider == '.' and field.cell.params[a][b].op == true)
or
collider == field.cell[self.y][self.x]
or
(ops:active() and ops.list[string.upper(field.cell[a][b])])
or
collider == tonumber(collider)
or
collider ~= ops.is_op(a,b)
or
ops.is_bang(a,b)
)
then
if field.cell[self.y][self.x] == 'Z' then
ops:move_cell(1,a)
end
self:explode()
-- L fix
elseif field.cell.params[a][b].op == false or (collider == '.' and field.cell.params[a][b].op == false ) then
ops:move_cell(b,a)
self:erase(self.x,self.y)
end
else
self:explode()
end
else
ops:move_cell(b,a)
end
end
function ops:move_cell(x,y)
field.cell[y][x] = field.cell[self.y][self.x]
self:erase(self.x,self.y)
ops:add_to_queue(x,y)
end
function ops:clean_ports(t, x1, y1)
for i=1,#t do
if t[i] ~= nil then
for l=1,#t[i]-2 do
local x = x1 ~= nil and x1 + t[i][l] or self.x + t[i][l]
local y = y1 ~= nil and y1 + t[i][l+1] or self.y + t[i][l+1]
field.cell.params[self.y][self.x].lit = false
if field.cell[y][x] ~= nil then
if t[i][l + 2] == 'output' then
field.cell.params[y][x].lit_out = false
field.cell.params[y][x].dot_port = false
elseif t[i][l + 2] == 'input' then
field.cell.params[y][x].dot_port = false
elseif t[i][l + 2] == 'input_op' then
field.cell.params[y][x].op = true
field.cell.params[y][x].dot_port = false
elseif t[i][l + 2] == 'output_op' then
field.cell.params[y][x].op = true
field.cell.params[y][x].dot_port = false
field.cell.params[y][x].lit_out = false
end
end
end
end
end
end
function ops:spawn(t)
for i=1,#t do
for l= 1, #t[i] - 2 do
local x = self.x + t[i][l]
local y = self.y + t[i][l+1]
local existing = field.cell[y][x] == ops.list[field.cell[y][x]] and field.cell[y][x] or nil
local port_type = t[i][l + 2]
-- draw frame
if field.cell[self.y][self.x] ~= string.lower(field.cell[self.y][self.x]) then
field.cell.params[self.y][self.x].lit = true
elseif (field.cell[self.y][self.x] == "'" or field.cell[self.y][self.x] == ':' or field.cell[self.y][self.x] == '/') then
field.cell.params[self.y][self.x].lit = true
end
-- draw inputs / outputs
if field.cell[y][x] ~= nil then
if port_type == 'output' then
field.cell.params[y][x].lit_out = true
field.cell.params[y][x].dot_port = true
elseif port_type == 'input' then
field.cell.params[y][x].dot_port = true
elseif port_type == 'input_op' then
field.cell.params[y][x].op = false
field.cell.params[y][x].dot_port = true
if existing ~= nil then
ops:clean_ports(existing, x,y)
else
end
elseif port_type == 'output_op' then
field.cell.params[y][x].lit_out = true
field.cell.params[y][x].op = false
field.cell.params[y][x].dot_port = true
end
end
end
end
end
-----
ops["*"] = function(self, x,y,f)
self.x = x
self.y = y
if field.cell.params[self.y][self.x].act == true then self:erase(self.x, self.y) end
end
ops.V = function (self,x,y,frame)
self.name = 'V'
self.y = y
self.x = x
local a = tonumber(ops:input(x - 1, y, 0)) ~= nil and tonumber(ops:input(x - 1, y, 0)) or 0
local b = tonumber(ops:input(x + 1, y, 0)) ~= nil and tonumber(ops:input(x + 1, y, 0)) or 0
if self:active() then
self:spawn(ops.ports[self.name])
if (b ~= 0 and vars[b] ~= nil and a == 0) then
if vars[b] ~= nil then
field.cell.params[y + 1][x].lit_out = true
field.cell[y + 1][x] = vars[b]
end
elseif self:active() and b ~= 0 and a ~= 0 then
vars[a] = field.cell[y][x + 1]
else
field.cell.params[y + 1][x].lit_out = false
end
elseif not self:active() then
if ops.banged(x,y) then
end
end
end
ops.A = function (self,x,y,frame)
self.name = 'A'
self.y = y
self.x = x
local b = tonumber(ops:input(x + 1, y, 0)) ~= nil and tonumber(ops:input(x + 1, y, 0)) or 0
local a = tonumber(ops:input(x - 1, y, 0)) ~= nil and tonumber(ops:input(x - 1, y, 0)) or 0
local sum
if (a ~= 0 or b ~= 0) then sum = ops.chars[util.clamp(math.ceil((a+b)),0,#ops.chars)]
else sum = 0 end
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = sum
elseif not self:active() then
if ops.banged(x,y) then
field.cell[y+1][x] = sum
end
end
end
ops.B = function (self, x,y, frame)
self.name = 'B'
self.y = y
self.x = x
local to = tonumber(ops:input(x + 1, y)) or 1
local rate = tonumber(ops:input(x - 1, y)) or 1
if to == 0 or to == nil then to = 1 end
if rate == 0 or rate == nil then rate = 1 end
local key = math.floor(frame / rate) % (to * 2)
local val = key <= to and key or to - (key - to)
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y + 1][x] = ops.chars[val]
elseif not self:active() then
if ops.banged(x,y) then
field.cell[y + 1][x] = ops.chars[val]
end
end
end
ops.C = function (self, x, y, frame)
self.name = 'C'
self.y = y
self.x = x
local modulus = tonumber(ops:input(x + 1, y)) or 9
local rate = tonumber(ops:input(x - 1, y)) or 1
if modulus == 0 or modulus == nil then modulus = 1 end
if rate == 0 or rate == nil then rate = 1 end
local val = (math.floor(frame / rate) % modulus) + 1
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = ops.chars[val]
elseif not self:active() then
if ops.banged(x,y) then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = ops.chars[val]
end
end
end
ops.D = function (self, x, y, frame)
self.name = 'D'
self.y = y
self.x = x
local modulus = tonumber(ops:input(x + 1, y)) -- only int
local rate = tonumber(ops:input(x - 1, y)) -- only int
if modulus == 0 then modulus = 1 end
local val = (frame % (modulus or 9)) * (rate or 1)
local out = (val == 0 or modulus == 1) and '*' or 'null'
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = out
elseif not self:active() then
if ops.banged(x,y) then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = out
end
end
end
ops.F = function(self, x,y)
self.name = 'F'
self.y = y
self.x = x
local b = tonumber(field.cell[y][x + 1])
local a = tonumber(field.cell[y][x - 1])
local val = a == b and '*' or 'null'
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = val
elseif not self:active() then
if ops.banged(x,y) then
field.cell[y+1][x] = val
end
end
end
ops.H = function(self,x,y)
self.name = 'H'
self.y = y
self.x = x
local ports = {{0, 1 , 'output'}}
local a = field.cell[y - 1][x]
if self:active() then
self:spawn(ops.ports[self.name])
field.cell.params[y + 1][x].act = false
if ((field.cell[y + 1][x] == 'H' or field.cell[y + 1][x] == 'h') and field.cell.params[y + 1][x].op) then
field.cell.params[y + 2][x].act = true
end
elseif not self:active() then
if ops.banged(x,y) then
field.cell.params[y + 1][x].act = false
if ((field.cell[y + 1][x] == 'H' or field.cell[y + 1][x] == 'h') and field.cell.params[y + 1][x].op) then
field.cell.params[y + 2][x].act = true
end
else
field.cell.params[y + 1][x].act = true
end
end
end
ops.J = function(self, x,y)
self.name = 'J'
self.y = y
self.x = x
local a = field.cell[y - 1][x]
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y + 1][x] = a
elseif not self:active() then
if ops.banged(x,y) then
field.cell[y + 1][x] = a
end
end
end
ops.I = function (self, x, y, frame)
self.name = 'I'
self.y = y
self.x = x
local a, b
a = ops:input(x - 1, y, 0)
b = ops:input(x + 1, y, 9)
a = tonumber(a) or 0
b = tonumber(b) ~= tonumber(a) and tonumber(b) or tonumber(a) + 1
if b < a then a,b = b,a end
val = util.clamp((frame % math.ceil(b)) + 1,a,b)
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = ops.chars[val]
end
end
ops.W = function(self, x, y)
self.name = 'W'
self.x = x
self.y = y
if self:active() then
ops:move(-1,0)
elseif not self:active() then
if ops.banged(x,y) then
ops:move(-1,0)
end
end
end
ops.E = function (self, x, y)
self.name = 'E'
self.x = x
self.y = y
if self:active() then
ops:move(1,0)
elseif not self:active() then
if ops.banged(x,y) then
ops:move(1,0)
end
end
end
ops.N = function (self, x, y)
self.name = 'N'
self.x = x
self.y = y
if self:active() then
ops:move(0,-1)
elseif not self:active() then
if ops.banged(x,y) then
ops:move(0,-1)
end
end
end
ops.S = function(self, x, y)
self.name = 'S'
self.x = x
self.y = y
if self:active() then
ops:move(0,1)
elseif not self:active() then
if ops.banged(x,y) then
--ops:move(0,1)
end
end
end
ops.O = function (self, x, y)
self.name = 'O'
self.y = y
self.x = x
local a = (tonumber(field.cell[y][x - 2]) == 0 or tonumber(field.cell[y][x - 2]) == nil) and 1 or tonumber(field.cell[y][x - 2]) -- x
local b = tonumber(field.cell[y][x - 1]) or 0 -- y
local offsety = b + y
local offsetx = a + x
if self:active() then
field.cell[y + 1][x] = field.cell[offsety][offsetx]
for i= x - 1, x + 9 do
for l = x - 1, x + 9 do
if (i == y and l == x) then
elseif (i == offsety and l == offsetx) then
field.cell.params[offsety][offsetx].dot_cursor = true
field.cell.params[offsety][offsetx].op = false
else
field.cell.params[i][l].dot_cursor = false
field.cell.params[i][l].op = true
end
end
end
self:spawn(ops.ports[self.name])
end
end
ops.M = function (self, x, y)
self.name = 'M'
self.y = y
self.x = x
local l = tonumber(ops:input(x - 1, y, 1)) or 1 -- only int
local m = tonumber(ops:input(x + 1, y, 1)) or 1-- only int
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = string.sub(l % (m ~= 0 and m or 1), -1)
elseif not self:active() then
end
end
ops.P = function (self, x, y, frame)
self.name = 'P'
self.y = y
self.x = x
local length = tonumber(ops:input(x - 1, y, 0) ) ~= nil and tonumber(ops:input(x - 1, y, 1) ) or 1
local pos = util.clamp(tonumber(ops:input(x - 2, y, 0)) ~= 0 and tonumber(ops:input(x - 2, y, 0)) or 1, 1, length)
local val = field.cell[y][x + 1]
length = util.clamp(length, 1, XSIZE)
if self:active() then
self:spawn(ops.ports[self.name])
for i = 1,length do
field.cell.params[y + 1][(x + i) - 1 ].dot = true
field.cell.params[y + 1][(x + i) - 1 ].op = false
end
-- highliht pos
for l= 1, length do
if ((pos or 1) % (length + 1)) == l then
field.cell.params[y + 1][(x + l) - 1].cursor = true
else
field.cell.params[y + 1][(x + l) - 1].cursor = false
end
end
field.cell[y+1][(x + ((pos or 1) % (length+1))) - 1] = val
end
-- cleanups
for i= length, #ops.chars do
if field.cell.params[y + 1][(x + i)].dot then
field.cell.params[y + 1][(x + i)].dot = false
field.cell.params[y + 1][(x + i) ].op = true
field.cell.params[y + 1][(x + i) ].cursor = false
end
end
end
ops.T = function (self, x, y, frame)
self.name = 'T'
self.y = y
self.x = x
local length = tonumber(ops:input(x - 1, y, 0) ) ~= nil and tonumber(ops:input(x - 1, y, 1) ) or 1
length = util.clamp(length, 1, XSIZE)
local pos = util.clamp(tonumber(ops:input(x - 2, y, 0)) ~= 0 and tonumber(ops:input(x - 2, y, 0)) or 1, 1, length)
local val = field.cell[self.y][self.x + util.clamp(pos,1,length)]
if self:active() then
field.cell.params[y+1][x].lit_out = true
self:spawn(ops.ports[self.name])
for i = 1,length do
field.cell.params[y][(x + i)].dot = true
field.cell.params[y][(x + i)].op = false
end
-- highliht pos
for l= 1, length do
if pos == l then
field.cell.params[y][(x + l)].cursor = true
else
field.cell.params[y][(x + l)].cursor = false
end
end
field.cell[y+1][x] = val or '.'
end
-- cleanups
for i= length+1, #ops.chars do
field.cell.params[y][(x + i)].dot = false
field.cell.params[y][(x + i)].op = true
field.cell.params[y][(x + i)].cursor = false
end
end
ops.K = function (self, x, y, frame)
self.name = 'K'
self.y = y
self.x = x
local length = tonumber(ops:input(x - 1, y, 0) ) ~= nil and tonumber(ops:input(x - 1, y, 0) ) or 0
local offset = 1
length = util.clamp(length,0,XSIZE)
local l_start = x + offset
local l_end = x + length
if self:active() then
self:spawn(ops.ports[self.name])
if length - offset == 0 then
for i=2,length do
field.cell.params[y][x + i].op = true
end
else
for i = 1,length do
local var = ops:input(x+i,y)
field.cell.params[y][(x + i)].dot = true
field.cell.params[y+1][(x + i)].dot_port = false
field.cell.params[y][(x + i)].op = false
field.cell.params[y][(x + i)].act = false
field.cell.params[y+1][(x + i)].lit_out = false
field.cell.params[y][(x + i)].lit = false
if vars[var] ~= nil then
field.cell.params[y+1][(x + i)].op = false
field.cell.params[y + 1][(x + i)].act = false
field.cell.params[y+1][(x + i)].lit_out = false
field.cell.params[y+2][(x + i)].lit_out = false
field.cell.params[y+1][(x + i)].lit = false
field.cell[y+1][(x + i)] = vars[var]
end
end
field.cell.params[y+1][x].dot_port = false
field.cell.params[y+1][length + 1].dot_port = false
end
end
-- cleanups
if length < #ops.chars then
for i= length == 0 and length or length+1, #ops.chars do
field.cell.params[y][util.clamp((x + i),1,XSIZE)].dot = false
field.cell.params[y][util.clamp((x + i),1,XSIZE)].op = true
field.cell.params[y+1][util.clamp((x + i),1,XSIZE)].act = true
end
end
end
ops.L = function (self, x, y, frame)
self.name = 'L'
self.y = y
self.x = x
local length = tonumber(ops:input(x - 1, y, 0) ) ~= nil and tonumber(ops:input(x - 1, y, 0) ) or 0
local rate = (tonumber(ops:input(x - 2, y, 0) ) == nil or tonumber(ops:input(x - 2, y, 0) ) == 0) and 1 or tonumber(ops:input(x - 2, y, 0) )
local offset = 1
length = util.clamp(length,0,XSIZE)
local l_start = x + offset
local l_end = x + length
if self:active() then
self:spawn(ops.ports[self.name])
if length - offset == 0 then
for i=2,length do
field.cell.params[y][x + i].op = true
end
else
for i = 1,length do
field.cell.params[y][(x + i)].dot = true
field.cell.params[y][(x + i)].op = false
field.cell.params[y+1][(x + i)].lit_out = false
field.cell.params[y][(x + i)].lit = false
end
end
end
if frame % rate == 0 and length ~= 0 then
self:shift(offset, length)
end
-- cleanups
if length < #ops.chars then
for i= length == 0 and length or length+1, #ops.chars do
field.cell.params[y][(x + i)].dot = false
field.cell.params[y][(x + i)].op = true
end
end
end
ops.R = function (self, x,y,frame)
self.name = 'R'
self.y = y
self.x = x
local a, b
a = ops:input(x - 1, y, 1)
b = ops:input(x + 1, y, 9)
a = util.clamp(tonumber(a) or 1,0,#ops.chars)
b = util.clamp(tonumber(b) or 9,1,#ops.chars)
if b == 27 and a == 27 then a = math.random(#ops.chars) b = math.random(#ops.chars) end -- rand
if b < a then a,b = b,a end
if self:active() then
self:spawn(ops.ports[self.name])
field.cell[y+1][x] = ops.chars[math.random((a or 1),(b or 9))]
end
end
ops.X = function(self, x,y)
self.name = 'X'