-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
956 lines (809 loc) · 17.3 KB
/
api.lua
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
local flr=math.floor
local lg=love.graphics
local tinsert = table.insert
local tremove = table.remove
local function color(c, disallowShift)
c=flr(c or 0)%16
pico8.color=c
if disallowShift then
setColor(c)
else
setShiftedColor(c)
end
end
local function warning(msg)
updateStatus("WARNING: "..msg)
end
local function _horizontal_line(lines, x0, y, x1)
tinsert(lines, {x0+0.5, y+0.5, x1+1.5, y+0.5})
end
local function _plot4points(lines, cx, cy, x, y)
_horizontal_line(lines, cx-x, cy+y, cx+x)
if y~=0 then
_horizontal_line(lines, cx-x, cy-y, cx+x)
end
end
--------------------------------------------------------------------------------
-- PICO-8 API
local api={}
function api.flip()
flip_screen()
love.timer.sleep(1/pico8.fps)
end
function api.camera(x, y)
if x~=nil then
pico8.camera_x=flr(x)
pico8.camera_y=flr(y)
else
pico8.camera_x=0
pico8.camera_y=0
end
restore_camera()
end
function api.clip(x, y, w, h)
if x and y and w and h then
lg.setScissor(x, y, w, h)
pico8.clip={x, y, w, h}
else
lg.setScissor()
pico8.clip=nil
end
end
function api.cls(c)
c = tonumber(c) or 0
if c == nil then
c = 0
end
pico8.clip=nil
--size of pico 8 screen
lg.setScissor()
--TODO clear the color passed
local color = pico8.palette[c + 1]
--lg.clear(color[1], color[2], color[3], 1)
--pico love uses the background color for clear. This doesn't match love behavior
lg.setBackgroundColor(color[1], color[2], color[3])
lg.clear()
pico8.cursor[1] = 0
pico8.cursor[2] = 0
end
function api.folder()
end
function api.ls()
end
api.dir=api.ls
function api.cd()
end
function api.mkdir()
end
function api.install_demos()
end
function api.install_games()
end
function api.keyconfig()
end
function api.splore()
end
function api.pset(x, y, col)
if col then
color(col)
end
lg.points(flr(x), flr(y))
end
function api.pget(x, y)
x=x-pico8.camera_x
y=y-pico8.camera_y
--TODO: ruh roh :( need imageData support from LovePotion, or huge refactor
--[[
if x>=0 and x<pico8.resolution[1] and y>=0 and y<pico8.resolution[2] then
lg.setCanvas()
local c=pico8.screen:newImageData():getPixel(flr(x), flr(y))*15
lg.setCanvas(pico8.screen)
return c
end
warning(string.format("pget out of screen %d, %d", x, y))
]]
return 0
end
function api.color(c)
color(c)
end
function api.print(str, x, y, col)
if col then
color(col)
end
if x and y then
pico8.cursor[1]=flr(tonumber(x) or 0)
pico8.cursor[2]=flr(tonumber(y) or 0)
end
local str=tostring(str):gsub("[%z\1-\9\11-\31\154-\255]", " "):gsub("[\128-\153]", "\194%1").."\n"
local size=0
for line in str:gmatch("(.-)\n") do
local xAdd = 0
for i = 1, #tostring(line) do
lg.draw(pico8.fontImg, pico8.fontQuads[string.sub(line, i, i)], pico8.cursor[1]+xAdd, pico8.cursor[2]+size)
xAdd = xAdd + 4
end
size=size+6
end
if not x and not y then
if pico8.cursor[2]+size>122 then
lg.setColor(1, 1, 1, 1)
lg.setCanvas(pico8.tmpscr)
lg.draw(pico8.screen)
lg.setCanvas(pico8.screen)
lg.draw(pico8.tmpscr, 0, -size)
lg.setColor(0, 0, 0, 1)
lg.rectangle("fill", 0, pico8.resolution[2]-size, pico8.resolution[1], size)
setColor(pico8.color)
else
pico8.cursor[2]=pico8.cursor[2]+size
end
end
end
api.printh=print
function api.cursor(x, y)
pico8.cursor[1] = x or 0
pico8.cursor[2] = y or 0
end
function api.tonum(val)
return tonumber(val) -- not a direct assignment to prevent usage of the radix argument
end
function api.tostr(val, hex)
local kind=type(val)
if kind == "string" then
return val
elseif kind == "number" then
if hex then
val=val*0x10000
local part1=bit.rshift(bit.band(val, 0xFFFF0000), 16)
local part2=bit.band(val, 0xFFFF)
return string.format("0x%04x.%04x", part1, part2)
else
return tostring(val)
end
elseif kind == "boolean" then
return tostring(val)
else
return "[" .. kind .. "]"
end
end
function api.spr(n, x, y, w, h, flip_x, flip_y)
n=flr(n)
w=w or 1
h=h or 1
local q
if w==1 and h==1 then
q=pico8.quads[n]
if not q then
updateStatus('warning: sprite '..n..' is missing')
return
end
else
local id=string.format("%d-%d-%d", n, w, h)
if pico8.quads[id] then
q=pico8.quads[id]
else
q=lg.newQuad(flr(n%16)*8, flr(n/16)*8, 8*w, 8*h, 128, 128)
pico8.quads[id]=q
end
end
if not q then
updateStatus('missing quad', n)
end
local r, g, b, a = lg.getColor()
lg.setColor(1, 1, 1)
lg.draw(pico8.spritesheet_data, q,
flr(x)+(w*8*(flip_x and 1 or 0)),
flr(y)+(h*8*(flip_y and 1 or 0)),
0, flip_x and-1 or 1, flip_y and-1 or 1)
lg.setColor(r, g, b, a)
end
function api.sspr(sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y)
dw=dw or sw
dh=dh or sh
-- FIXME: cache this quad
local q=lg.newQuad(sx, sy, sw, sh, pico8.spritesheet_data:getDimensions())
local r, g, b, a = lg.getColor()
lg.setColor(1, 1, 1)
lg.draw(pico8.spritesheet_data, q,
flr(dx)+(flip_x and dw or 0),
flr(dy)+(flip_y and dh or 0),
0, dw/sw*(flip_x and-1 or 1), dh/sh*(flip_y and-1 or 1))
setColor(pico8.color)
lg.setColor(r, g, b, a)
end
function api.rect(x0, y0, x1, y1, col)
if col then
color(col)
end
local w, h=flr(x1-x0), flr(y1-y0)
if w==0 or h==0 then
lg.rectangle("fill", flr(x0), flr(y0), w+1, h+1)
else
lg.rectangle("line", flr(x0)+0.5, flr(y0)+0.5, w, h)
end
end
function api.rectfill(x0, y0, x1, y1, col)
if col then
color(col)
end
if x1<x0 then
x0, x1=x1, x0
end
if y1<y0 then
y0, y1=y1, y0
end
lg.rectangle("fill", flr(x0), flr(y0), flr(x1-x0)+1, flr(y1-y0)+1)
end
function api.circ(ox, oy, r, col)
if col then
color(col)
end
ox=flr(ox)+0.5
oy=flr(oy)+0.5
r=flr(r)
local points={}
local x=r
local y=0
local decisionOver2=1-x
while y<=x do
tinsert(points, ox+x)
tinsert(points, oy+y)
tinsert(points, ox+y)
tinsert(points, oy+x)
tinsert(points, ox-x)
tinsert(points, oy+y)
tinsert(points, ox-y)
tinsert(points, oy+x)
tinsert(points, ox-x)
tinsert(points, oy-y)
tinsert(points, ox-y)
tinsert(points, oy-x)
tinsert(points, ox+x)
tinsert(points, oy-y)
tinsert(points, ox+y)
tinsert(points, oy-x)
y=y+1
if decisionOver2<0 then
decisionOver2=decisionOver2+2*y+1
else
x=x-1
decisionOver2=decisionOver2+2*(y-x)+1
end
end
if #points>0 then
lg.points(points)
end
end
function api.circfill(cx, cy, r, col)
if col then
color(col)
end
cx=flr(cx)
cy=flr(cy)
r=flr(r)
local x=r
local y=0
local err=1-r
local lines={}
while y<=x do
_plot4points(lines, cx, cy, x, y)
if err<0 then
err=err+2*y+3
else
if x~=y then
_plot4points(lines, cx, cy, y, x)
end
x=x-1
err=err+2*(y-x)+3
end
y=y+1
end
if #lines>0 then
for i=1, #lines do
lg.line(lines[i])
end
end
end
function api.line(x0, y0, x1, y1, col)
if col then
color(col)
end
if x0~=x0 or y0~=y0 or x1~=x1 or y1~=y1 then
warning("line has NaN value")
return
end
x0=flr(x0)
y0=flr(y0)
x1=flr(x1)
y1=flr(y1)
if x0==x1 or y0==y1 then
-- simple case draw a straight line
lg.rectangle("fill", x0, y0, x1-x0+1, y1-y0+1)
else
--this line is too fat, but it will do for now
--TODO: redraw using points that isn't fat
lg.line(x0+0.5, y0+0.5, x1+0.5, y1+0.5)
-- Final pixel not being reached?
--lg.points(x1+0.5, y1+0.5)
end
end
function api.pal(c0, c1, p)
if c0==nil then
local __palette_modified=false
local __display_modified=false
local __alpha_modified=false
for i=0, 15 do
if pico8.draw_palette[i]~=i then
pico8.draw_palette[i]=i
__palette_modified=true
end
if pico8.display_palette[i]~=pico8.palette[i+1] then
pico8.display_palette[i]=pico8.palette[i+1]
__display_modified=true
end
local alpha=i==0 and 0 or 1
if pico8.pal_transparent[i]~=alpha then
pico8.pal_transparent[i]=alpha
__alpha_modified=true
end
end
if __palette_modified or __alpha_modified then
refreshSpritesheetCanvas()
end
if __display_modified then
--not yet supported
end
elseif p==1 and c1~=nil then
c0=flr(c0)%16
c1=flr(c1)%16
if pico8.draw_palette[c0]~=pico8.palette[c1+1] then
pico8.display_palette[c0]=pico8.palette[c1+1]
--not sure there is a good way to do this one without imageData or a shader :|
end
elseif c1~=nil then
c0=flr(c0)%16
c1=flr(c1)%16
if pico8.draw_palette[c0]~=c1 then
pico8.draw_palette[c0]=c1
end
refreshSpritesheetCanvas()
end
end
function api.palt(c, t)
if c==nil then
for i=0, 15 do
pico8.pal_transparent[i]=i==0 and 0 or 1
end
else
c=flr(c)%16
pico8.pal_transparent[c]=t and 0 or 1
end
refreshSpritesheetCanvas()
end
function api.fillp(p)
-- TODO: oh jeez
end
function api.map(cel_x, cel_y, sx, sy, cel_w, cel_h, bitmask)
cel_x=flr(cel_x or 0)
cel_y=flr(cel_y or 0)
sx=flr(sx or 0)
sy=flr(sy or 0)
cel_w=flr(cel_w or 128)
cel_h=flr(cel_h or 64)
bitmask=flr(bitmask or 0)
for y=0, cel_h-1 do
if cel_y+y<64 and cel_y+y>=0 then
for x=0, cel_w-1 do
if cel_x+x<128 and cel_x+x>=0 then
local v=pico8.map[flr(cel_y+y)][flr(cel_x+x)]
if v~=0 then
if bitmask==0 or bit.band(pico8.spriteflags[v], bitmask)~=0 then
local xPos = sx + (8*x) - pico8.camera_x;
local yPos = sy + (8*y) - pico8.camera_y;
--limit drawing to what is on screen
if xPos > -9 and xPos < 128 and yPos > -9 and yPos < 128 then
local r, g, b, a = lg.getColor()
lg.setColor(1, 1, 1, 1)
lg.draw(pico8.spritesheet_data, pico8.quads[v], sx + (8*x), sy + (8*y))
lg.setColor(r, g, b, a)
end
end
end
end
end
end
end
end
api.mapdraw=api.map
function api.mget(x, y)
x=flr(x or 0)
y=flr(y or 0)
if x>=0 and x<128 and y>=0 and y<64 then
return pico8.map[y][x]
end
return 0
end
function api.mset(x, y, v)
x=flr(x or 0)
y=flr(y or 0)
v=flr(v or 0)%256
if x>=0 and x<128 and y>=0 and y<64 then
pico8.map[y][x]=v
end
end
function api.fget(n, f)
if n==nil then return nil end
if f~=nil then
-- return just that bit as a boolean
if not pico8.spriteflags[flr(n)] then
warning(string.format('fget(%d, %d)', n, f))
return false
end
return bit.band(pico8.spriteflags[flr(n)], bit.lshift(1, flr(f)))~=0
end
return pico8.spriteflags[flr(n)] or 0
end
function api.fset(n, f, v)
-- fset n [f] v
-- f is the flag index 0..7
-- v is boolean
if v==nil then
v, f=f, nil
end
if f then
-- set specific bit to v (true or false)
if v then
pico8.spriteflags[n]=bit.bor(pico8.spriteflags[n], bit.lshift(1, f))
else
pico8.spriteflags[n]=bit.band(pico8.spriteflags[n], bit.bnot(bit.lshift(1, f)))
end
else
-- set bitfield to v (number)
pico8.spriteflags[n]=v
end
end
function api.sget(x, y)
-- return the color from the spritesheet
x=flr(x)
y=flr(y)
if x>=0 and x<128 and y>=0 and y<128 then
local c=pico8.spritesheet_table[x][y]
return c
end
return 0
end
function api.sset(x, y, c)
x=flr(x)
y=flr(y)
c=flr(c or 0)%16
if x>=0 and x<128 and y>=0 and y<128 then
pico8.spritesheet_table[x][y] = c
--TODO: test this
--bust the sprite sheet cache, it is now inaccurate
--for k in pairs (pico8.spritesheet_cache) do
-- pico8.spritesheet_cache[k] = nil
--end
end
end
function api.music(n, fade_len, channel_mask)
end
function api.sfx(n, channel, offset)
end
function api.peek(addr)
return 0
end
function api.poke(addr, val)
end
function api.peek4(addr)
return 0
end
function api.poke4(addr, val)
end
function api.memcpy(dest_addr, source_addr, len)
end
function api.memset(dest_addr, val, len)
end
function api.reload(dest_addr, source_addr, len)
dest_addr = flr(dest_addr or 0)
source_addr = flr(source_addr or 0)
len = flr(len or 0x4300)
--reset everything
if dest_addr == 0 and source_addr == 0 and len == 0x4300 then
for i =0, 127 do
pico8.spritesheet_table[i] = {}
for j = 0, 127 do
pico8.spritesheet_table[i][j] = pico8.cartrom.spritesheet_table[i][j]
end
end
for y=0, 63 do
pico8.map[y]={}
for x=0, 127 do
pico8.map[y][x]=pico8.cartrom.map[y][x]
end
end
for i=0, 255 do
pico8.spriteflags[i]=pico8.cartrom.spriteflags[i]
end
end
end
function api.cstore(dest_addr, source_addr, len)
end
function api.rnd(x)
return math.random()*(x or 1)
end
function api.srand(seed)
math.randomseed(flr(seed*0x10000))
end
api.flr=math.floor
api.ceil=math.ceil
function api.sgn(x)
return x<0 and-1 or 1
end
api.abs=math.abs
function api.min(a, b)
if a==nil or b==nil then
warning('min a or b are nil returning 0')
return 0
end
if a<b then return a end
return b
end
function api.max(a, b)
if a==nil or b==nil then
warning('max a or b are nil returning 0')
return 0
end
if a>b then return a end
return b
end
function api.mid(x, y, z)
return (x<=y)and((y<=z)and y or((x<z)and z or x))or((x<=z)and x or((y<z)and z or y))
end
function api.cos(x)
return math.cos((x or 0)*math.pi*2)
end
function api.sin(x)
return-math.sin((x or 0)*math.pi*2)
end
api.sqrt=math.sqrt
function api.atan2(x, y)
return (0.75 + math.atan2(x,y) / (math.pi * 2)) % 1.0
end
function api.band(x, y)
return bit.band(x*0x10000, y*0x10000)/0x10000
end
function api.bor(x, y)
return bit.bor(x*0x10000, y*0x10000)/0x10000
end
function api.bxor(x, y)
return bit.bxor(x*0x10000, y*0x10000)/0x10000
end
function api.bnot(x)
return bit.bnot(x*0x10000)/0x10000
end
function api.shl(x, y)
return bit.lshift(x*0x10000, y)/0x10000
end
function api.shr(x, y)
return bit.arshift(x*0x10000, y)/0x10000
end
function api.lshr(x, y)
return bit.rshift(x*0x10000, y)/0x10000
end
function api.rotl(x, y)
return bit.rol(x*0x10000, y)/0x10000
end
function api.rotr(x, y)
return bit.ror(x*0x10000, y)/0x10000
end
function api.load(filename)
_load(filename)
end
function api.save()
end
function api.run()
_load()
end
function api.stop()
end
function api.reboot()
end
function api.shutdown()
end
function api.exit()
end
function api.info()
end
function api.export()
end
function api.import()
end
function api.help()
end
function api.time()
return pico8.frames/30
end
api.t=api.time
function api.login()
return nil
end
function api.logout()
return nil
end
function api.bbsreq()
return nil
end
function api.scoresub()
return nil, 0
end
function api.extcmd(x)
end
function api.radio()
return nil, 0
end
function api.btn(i, p)
if i~=nil or p~=nil then
p=p or 0
if p<0 or p>1 then
return false
end
return not not pico8.keypressed[p][i]
else
local bits=0
for i=0, 5 do
bits=bits+(pico8.keypressed[0][i] and 2^i or 0)
bits=bits+(pico8.keypressed[1][i] and 2^(i+8) or 0)
end
return bits
end
end
function api.btnp(i, p)
if i~=nil or p~=nil then
p=p or 0
if p<0 or p>1 then
return false
end
local init=(pico8.fps/2-1)
local v=pico8.keypressed.counter
if pico8.keypressed[p][i] and (v==init or v==1) then
return true
end
return false
else
local init=(pico8.fps/2-1)
local v=pico8.keypressed.counter
if not (v==init or v==1) then
return 0
end
local bits=0
for i=0, 5 do
bits=bits+(pico8.keypressed[0][i] and 2^i or 0)
bits=bits+(pico8.keypressed[1][i] and 2^(i+8) or 0)
end
return bits
end
end
function api.cartdata(id)
end
function api.dget(index)
index=flr(index)
if index<0 or index>63 then
warning('cartdata index out of range')
return
end
return pico8.cartdata[index]
end
function api.dset(index, value)
index=flr(index)
if index<0 or index>63 then
warning('cartdata index out of range')
return
end
pico8.cartdata[index]=value
end
local tfield={[0]="year", "month", "day", "hour", "min", "sec"}
function api.stat(x)
if x == 4 then
return pico8.clipboard
elseif x == 7 then
return pico8.fps
elseif x == 8 then
return pico8.fps
elseif x == 9 then
return love.timer.getFPS()
elseif x >= 16 and x <= 23 then
local ch=pico8.audio_channels[x%4]
if not ch.sfx then
return -1
elseif x < 20 then
return ch.sfx
else
return flr(ch.offset)
end
elseif x == 30 then
return #pico8.kbdbuffer ~= 0
elseif x == 31 then
return (tremove(pico8.kbdbuffer, 1) or "")
elseif x == 32 then
return getMouseX()
elseif x == 33 then
return getMouseY()
elseif x == 34 then
local btns=0
for i=0, 2 do
if love.mouse.isDown(i+1) then
btns=bit.bor(btns, bit.lshift(1, i))
end
end
return btns
elseif x == 36 then
return pico8.mwheel
elseif (x >= 80 and x <= 85) or (x >= 90 and x <= 95) then
local tinfo
if x < 90 then
tinfo = os.date("!*t")
else
tinfo = os.date("*t")
end
return tinfo[tfield[x%10]]
elseif x == 100 then
return nil -- TODO: breadcrumb not supported
end
return 0
end
function api.holdframe()
-- TODO: Implement this
end
function api.menuitem()
end
api.sub=string.sub
api.pairs=pairs
api.type=type
api.assert=assert
api.setmetatable=setmetatable
api.getmetatable=getmetatable
api.cocreate=coroutine.create
api.coresume=coroutine.resume
api.yield=coroutine.yield
api.costatus=coroutine.status
api.trace=debug.traceback
-- The functions below are normally attached to the program code, but are here for simplicity
function api.all(a)
if a==nil or #a==0 then
return function() end
end
local i, li=1
return function()
if (a[i]==li) then i=i+1 end
while(a[i]==nil and i<=#a) do i=i+1 end
li=a[i]
return a[i]
end
end
function api.foreach(a, f)
for v in api.all(a) do
f(v)
end
end
function api.count(a)
local count=0
for i=1, #a do
if a[i]~=nil then count=count+1 end
end
return count
end
function api.add(a, v)
if a==nil then return end
a[#a+1]=v
end
function api.del(a, dv)
if a==nil then return end
for i=1, #a do
if a[i]==dv then
tremove(a, i)
return
end
end
end
return api