-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathones-extra.lua
More file actions
2293 lines (1977 loc) · 39.8 KB
/
ones-extra.lua
File metadata and controls
2293 lines (1977 loc) · 39.8 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
-- ones extra screens
-- by QUALIA
--------------
-- game logic
--------------
gw, gh = 4, 4
function not3(v)
return v == 1 or v == 2
end
function can_merge(a,b)
if a == 0 or b == 0 then
return false
elseif not3(a) and not3(b) then
return a != b
else
return a == b
end
end
function merge(a,b)
if can_merge(a,b) then
if not3(a) then
return 3
else return a+1
end
else return a
end
end
function slide_pieces_left(old_row)
local new_row = deepcopy(old_row)
local move_mask = {false}
for i=2,#old_row do
local val = old_row[i]
new_row[i] = 0
move_mask[i] = val ~= 0
local left_val = new_row[i-1]
if left_val == 0 then
new_row[i-1] = val
elseif can_merge(left_val,val) then
new_row[i-1] = merge(left_val, val)
else
move_mask[i] = false
new_row[i] = val
end
end
return new_row, move_mask
end
function next_pieces(state)
np, nb = bucket(state.piece_bucket)
if #nb == 0 then
nb = new_bucket(state)
end
return np, nb
end
function bonus_piece(max_val)
pieces={}
for i=max(max_val-5,4),
max_val-3 do
add(pieces,i)
end
return pieces
end
function piece_from_np(np,max_val)
if np == 4 and max_val > 6 then
return bonus_piece(max_val)
end
return min(np,3)
end
function move_outcome(move, g)
local ng = {}
local move_mask = {}
if is_vertical(move) then
g = transpose(g)
end
if move % 2 == 1 then
g = lmap(reverse, g)
end
local candidate_rows={}
for j=1,#g do
local row, row_move_mask =
slide_pieces_left(g[j])
add(move_mask, row_move_mask)
add(ng,row)
if any(row_move_mask) then
add(candidate_rows,row)
end
end
if #candidate_rows ~= 0 then
choose(candidate_rows)[#candidate_rows[1]]
= 0xf -- sentinel for new piece
end
if move % 2 == 1 then
ng = lmap(reverse, ng)
move_mask = lmap(reverse, move_mask)
end
if is_vertical(move) then
ng = transpose(ng)
move_mask = transpose(move_mask)
end
return ng, move_mask
end
function is_vertical(move)
return move >= 2
end
function allowed_moves(grid)
-- 0: left
-- 1: right
-- 2: up
-- 3: down
local moves={}
for move=0,3 do
local ng, mask =
move_outcome(move, grid)
if any(lmap(any, mask)) then
moves[move] = {
next_grid=ng,
move_mask=mask
}
end
end
return moves
end
function next_state(move, state)
local ng = mapgrid(
function(i,j,v)
if v==0xf then
if state.next_pieces then
return choose(state.next_pieces)
end
-- special case for tut & title
return 0
end
return v
end,
state.moves[move].next_grid
)
local max_val = maximum(ng)
local np, nb = next_pieces(state)
return {grid=ng,
next_pieces=
piece_from_np(
np,
max_val),
piece_bucket=nb,
moves=allowed_moves(ng),
max_val=max_val}
end
function make_grid(w,h)
local grid = {}
for j=1,h do
grid[j] = {}
for i=1,w do
grid[j][i] = 0
end
end
return grid
end
function max_ever_val()
local mv = 0
for b in all(data.boards) do
mv = max(mv, b.max_val)
end
return mv
end
function init_board(w,h,b)
local grid = make_grid(w,h)
local n = 9
if settings.boost then
grid[4][1] = max(3, max_ever_val() - 3)
n -= 1
end
for i=1,n do
local x,y
repeat
x = rndint(w)
y = rndint(h)
until grid[y][x] == 0
grid[y][x], b = bucket(b)
end
add(b,v)
return grid, b
end
function new_bucket(state)
local b = base_bucket()
if rndint(3) < 2 and state.max_val > 3 then
add(b,4) -- 4 = sentinel for bonus piece
end
return b
end
function score_tile(v)
return v>2 and bigint_new(3)^(v-2) or 0
end
function calculate_score(grid)
score = 0
foreach(joinlists(grid), function(x)
score+=score_tile(x) end)
return score
end
-->8
-- game states
function base_bucket()
return {1,1,1,1,2,2,2,2,3,3,3,3}
end
data = {}
state = nil
function make_state(grid, np, b)
local mv = maximum(grid)
return {
grid=grid,
piece_bucket=b,
next_pieces=piece_from_np(np,mv),
moves=allowed_moves(grid),
max_val=mv,
finished=false
}
end
transition = {}
function transition.init(target, source, dx, dy)
btnsfx()
target.init()
transition.target = target
transition.source = source
transition.dx, transition.dy = dx, dy
transition.x, transition.y = 0, 0
transition.anim = animate(0.5,ease.o.quad,
0,120,function(t)
transition.x = t*dx
transition.y = t*dy
end)
mode = transition
end
function transition.update()
cls(scheme.bg)
coresume(transition.anim)
if coalive(transition.anim) then
camera(transition.x, transition.y)
transition.source.draw()
camera(transition.x - 120 * transition.dx,
transition.y - 120 * transition.dy)
transition.target.draw()
else
camera()
transition.target.draw()
mode = transition.target
end
end
function new_state()
local b, grid, np
b = base_bucket()
grid, b = init_board(gw,gh,b)
np, b = bucket(b)
return make_state(grid, np, b)
end
play = {}
function play.init()
mode = play
play.drawn = false
play.move = nil
-- disable btnp repeating
poke(0x5f5c, 255)
end
function play.draw()
draw_next_pieces(state.next_pieces)
if play.move then
draw_grid(state.grid,nil,
state.moves[play.move.btn].move_mask,
play.move.btn)
else
draw_grid(state.grid)
end
draw_top_buttons("MENU","▤", btn(4),
"STATS","∧", btn(5))
end
function play.update()
load("ones.p8")
end
btn_state = {}
function _update()
for i=0,5 do
if btn_state[i] != btn(i) then
mode.drawn=false
btn_state[i] = btn(i)
end
end
mode.update()
if not mode.drawn and mode != transition then
cls(scheme.bg)
mode.draw()
mode.drawn = true
end
end
function _init()
store_init()
local loaded = load_data()
data.last_name = loaded.last_name
data.boards = loaded.boards
settings = loaded.settings
apply_settings()
cls(scheme.bg)
local grid, np, b = load_game()
if grid then
state = make_state(grid, np, b)
play.init()
mode.draw()
transition.init(stats, mode, 1, 0)
else
tutorial.init()
mode.draw()
end
end
-->8
-- tutorial
tutorial = {}
function tohexdigit(n)
assert(0 <= n and n < 16)
if n < 10 then
return tostr(n)
end
return sub("abcdef", n-9,n-9)
end
function txtcol(s)
return "\f" .. tohexdigit(scheme.txt) .. s
end
function tutorial.init()
mode = tutorial
tutorial.drawn = false
play.drawn = false
play.move = nil
-- disable btnp repeating
poke(0x5f5c, 255)
tutorial.main_text = "\f8o\fcn" .. txtcol("es!")
tutorial.bottom_text = "ANY ⬅️⬇️⬆️➡️ TO BEGIN"
tutorial.print_main_text = function(s,y,c)print(s,55,y,c)end
tutorial.extra_draw = nil
tutorial.seq = cocreate(tutorial_sequence)
coresume(tutorial.seq)
end
function tutorial.update()
local valid_moves = keys(state.moves)
if #valid_moves == 0 then
coresume(tutorial.seq, btnp(0) or btnp(1) or btnp(2) or btnp(3))
return
end
if tutorial.extra_draw then
mode.drawn = false
end
for move in all(valid_moves) do
if btnp(move)
and (not mode.move
or move != mode.move.btn)
then
mode.move = {btn=move, t=t()}
break
elseif mode.move
and mode.move.btn == move
then
if btnp(mode.move.btn) then
state =
next_state(move,state)
slidesfx()
coresume(tutorial.seq, mode.move.btn)
mode.move = nil
break
elseif btn(mode.move.btn) then
mode.move.t = t()
elseif t() - mode.move.t > 0.75 then
mode.move = nil
mode.drawn = false
end
end
end
end
function tutorial.draw()
if mode.move then
draw_grid(state.grid,nil,
state.moves[mode.move.btn].move_mask,
mode.move.btn)
else
draw_grid(state.grid)
end
if type(tutorial.extra_draw) == "function" then
tutorial.extra_draw()
elseif type(tutorial.extra_draw) == "thread" then
coresume(tutorial.extra_draw)
end
if tutorial.main_text then
tutorial.print_main_text(tutorial.main_text,title_y,scheme.txt)
end
if tutorial.bottom_text then
printc(tutorial.bottom_text,64,112,scheme.txt)
end
end
function right_align_main(s,y,c)
printr(s,107,y,c)
end
function center_align_main(s,y,c)
printc(s,64,y,c)
end
function tutorial_sequence()
grid = make_grid(4,4)
state = {
grid=grid,
piece_bucket={},
next_pieces=nil,
moves={},
max_val=0,
finished=false
}
yield()
-- wait for arrow key
while not yield() do end
local red, blue = "\f8red", "\fcblue"
local nx, ny = 0, 0
function track_move(btn)
local dx, dy = move2dirs(btn)
nx += dx
ny += dy
end
state.grid[2][2] = 1
state.grid[3][3] = 2
state.moves=allowed_moves(state.grid)
tutorial.main_text = "meet ".. blue .. txtcol(" & ") .. red
tutorial.print_main_text = right_align_main
tutorial.bottom_text = "DOUBLE ⬅️⬇️⬆️➡️\nTO MOVE THEM"
-- wait for move
track_move(yield())
tutorial.main_text = "⬅️⬇️⬆️➡️;\nmove everybody"
tutorial.bottom_text = nil
track_move(yield())
tutorial.main_text = "rearrange numbers by\npushing 'em into walls"
tutorial.bottom_text = "⬅️⬇️⬆️➡️; MOVE EVERYBODY"
tutorial.extra_draw = draw_walls
while abs(nx) < 2 and abs(ny) < 2 do
state.next_pieces = nil
track_move(yield())
end
tutorial.main_text = (
"use the walls to add\n\fcblue"
.. txtcol(" & ")
.. red
.. txtcol(" together"))
tutorial.bottom_text = nil
-- wait for move
while maximum(state.grid) != 3 do
state.next_pieces = nil
yield()
end
tutorial.extra_draw = nil
tutorial.main_text = "auspicious!"
tutorial.bottom_text = "MOVE ANYWHERE"
state.next_pieces = nil
yield()
tutorial.main_text = "new numbers appear \nwhen you move cards!"
state.next_pieces = 3
yield()
tutorial.print_main_text = center_align_main
tutorial.main_text = "1 + 1 = 2"
tutorial.bottom_text = nil
while maximum(state.grid) != 4 do
state.next_pieces = nil
yield()
end
tutorial.print_main_text = right_align_main
tutorial.main_text = "you're getting it!"
state.next_pieces = 3
yield()
tutorial.print_main_text = center_align_main
tutorial.main_text = "numbers 1 and higher\nwill only add together\n\f8if they are twins."
for i=1,3 do
state.next_pieces = nil
yield()
end
state.next_pieces = 3
yield()
tutorial.bottom_text = "CREATE A 3 TO CONTINUE"
state.next_pieces = 3
yield()
local bad_state = false
while maximum(state.grid) != 5 do
local g = state.grid
state.next_pieces = nil
if not bad_state then
for j=1,4 do
for i=1,2 do
if (g[j][i] == 4 and g[j][i+1] == 3 and g[j][i+2] == 4) or
(g[i][j] == 4 and g[i+1][j] == 3 and g[i+2][j] == 4) then
bad_state = true
state.next_pieces = 4
end
end
end
end
yield()
end
tutorial.print_main_text = right_align_main
tutorial.main_text = "fantastic!"
tutorial.bottom_text = "MOVE ANYWHERE TO CONTINUE"
state.next_pieces = 1
yield()
tutorial.bottom_text = nil
tutorial.print_main_text = center_align_main
tutorial.main_text = (
blue .. txtcol(" WILL only ADD WITH ") .. red)
for i=1,3 do
state.next_pieces = 1
yield()
end
state.next_pieces = 2
yield()
tutorial.main_text = (
red .. txtcol(" WILL only ADD WITH ") .. blue)
for i=1,4 do
state.next_pieces = 2
yield()
end
state.next_pieces = 1
yield()
tutorial.print_main_text = center_align_main
tutorial.main_text = "make a 4!"
while maximum(state.grid) != 6 do
local occupied = 0
mapgrid(function(i,j,v) if (v != 0) occupied += 1 end, state.grid)
if occupied > 8 then
state.next_pieces = nil
end
yield()
end
tutorial.print_main_text = right_align_main
tutorial.main_text = "tHIS IS \f8o\fcn" .. txtcol("es!")
tutorial.bottom_text = "MOVE TO KEEP GROWING!"
yield()
tutorial.print_main_text = center_align_main
tutorial.main_text = "make the highest\nnumber you can!"
tutorial.bottom_text = "THAT'S THE GOAL OF THE GAME"
yield()
tutorial.main_text = nil
tutorial.bottom_text = "LOOK UP TO SEE WHAT'S NEXT"
tutorial.extra_draw = animate(
0.5,
ease.o.quart,
-40,0,
function(y)
camera(0,-y)
draw_next_pieces(state.next_pieces)
camera(0,0)
end, "stick")
for i=1,4 do
yield()
end
tutorial.bottom_text = "IT'S OVER WHEN\nTHE BOARD FILLS UP"
yield()
tutorial.bottom_text = "HOLD A DIRECTION\nTO SEE THE FUTURE "
yield()
save_game()
load("ones.p8")
end
function draw_walls()
rect(29,39,99,109,8)
rect(28,38,100,110,8)
print("w\na\nl\nl",22,70,8)
print("w\na\nl\nl",104,50,8)
end
-->8
-- stats screen
stats = {}
function stats.init()
mode = stats
local n_scores = store_load_byte(152)
stats.scores =
lmap(function(x) return x >>> 5 end,
filter(function(x) return x != 0 end,
store_load_bytes(153, 102)))
-- because this is a ring, we need
-- to put the scores before the pivot
-- at the end of the list
for i=1,n_scores do
add(stats.scores, deli(stats.scores, 1))
end
stats.current_score = poorlog10(calculate_score(state.grid))
add(stats.scores,stats.current_score)
stats.hicards = store_load_bytes(140,12)
local sum = 0
for v in all(stats.hicards) do
sum += v
end
stats.hicard_frame = #stats.scores + 1
stats.num_frames = stats.hicard_frame + sum + 1
stats.frame = 0
stats.subframe = 0
end
function stats.update()
if btnp(4) or btnp(5) then
transition.init(play, stats, -1, 0)
end
if stats.frame < stats.num_frames then
stats.subframe += 1
if stats.subframe % 2 == 0 then
stats.frame += 1
stats.drawn = false
end
end
end
function stats.draw()
local gui_y = 34
local xs, ys = {}, {}
for i=1,min(stats.frame, #stats.scores) do
add(xs, i)
add(ys, stats.scores[i])
end
print("SCORE OVER TIME", 23, gui_y, scheme.stats_subtitle)
gui_y += 8
local min_v, max_v = minmax(stats.scores)
local ax = make_ax(
34,gui_y,70,20,
1, max(#stats.scores, 2),
min_v, max_v
)
if stats.frame >= 1 then
color(scheme.red)
plot(xs,ys,ax,pset)
-- Add current score to grid
if stats.frame >= #stats.scores then
local x, y = axpoint(ax,#stats.scores,stats.current_score)
pset(x,y,scheme.stats_current)
end
end
-- Draw axes
ax.px -= 4
ax.w += 12
ax.h += 2
color(scheme.txt)
draw_ax(ax,1,min_v,3)
color(scheme.txt)
printr(loglabel(ax.y1), ax.px-2, ax.py)
printr(loglabel((ax.y1-ax.y0) / 2), ax.px-2, ax.py + ax.h \ 2)
printr(loglabel(ax.y0), ax.px-2, ax.py + ax.h)
gui_y+=30
local hist_frame = stats.frame - stats.hicard_frame
local mv = nil
if stats.frame >= stats.num_frames then
mv = state.max_val-2
end
print("HIGH CARD DISTRIBUTION", 23, gui_y, scheme.stats_subtitle)
gui_y+=8
draw_high_card_dist(
gui_y, subhist(stats.hicards, hist_frame), mv)
draw_top_buttons("GAME","⬅️")
end
function loglabel(x)
local num_zeros = flr(x)
local fractional_part = x-num_zeros
local digit = 9
for i, v in pairs(log10_lookup) do
if fractional_part < v then
digit = i - 1
break
end
end
local s = tostr(digit)
if num_zeros == 1 then
s ..= "0"
elseif num_zeros == 2 then
s ..= "00"
elseif num_zeros == 3 then
s ..= "k"
elseif num_zeros == 4 then
s ..= "0k"
elseif num_zeros == 5 then
s ..= "00k"
elseif num_zeros == 6 then
s ..= "m"
end
return s
end
function subhist(hist, max_n)
local sum = 0
local new_hist = {}
for k,n in pairs(hist) do
new_hist[k] = max(0, min(n, max_n - sum))
sum += n
end
return new_hist
end
function draw_high_card_dist(y, hist, current_mv)
hist = shallowcopy(hist)
if current_mv then
hist[current_mv] += 1
end
local present = {}
for v,n in pairs(hist) do
if n != 0 then
add(present, v)
end
end
function draw_bars(start_index, x, w)
local vals, vals_c = {}, {}
for i=start_index,min(start_index+5,#present) do
local v = present[i]
if v == current_mv then
add(vals,0)
add(vals_c,hist[v])
else
add(vals,hist[v])
add(vals_c,0)
end
end
local bar_height = 6
local spacing = 2
local min_w = 10
local ax = make_ax(x+min_w,y,w-min_w)
local _,max_x = minmax(hist)
max_x = max(max_x, 5)
color(scheme.red)
hbar(vals,ax,bar_height,spacing,max_x)
color(scheme.stats_current)
hbar(vals_c,ax,bar_height,spacing, max_x)
for i=start_index,min(start_index+5,#present) do
local bar_y = y + (bar_height+spacing) * (i - start_index)
local is_current = present[i] == current_mv
local txt_col = (
is_current
and scheme.stats_current_txt
or scheme.stats_txt)
local n = hist[present[i]]
rectfill(x, bar_y, x+min_w, bar_y+bar_height,
is_current
and scheme.stats_current
or scheme.red)
print(tostr(present[i]), 1+x, 1+bar_y, txt_col)
smallnum(n,
x+min_w + n\ax.dx - 3 - 4*(#tostr(n)-1),
3+bar_y,txt_col)
end
end
if #present <= 6 then
draw_bars(1, 23, 82)
else
draw_bars(1, 23, 40)
draw_bars(7, 73, 40)
end
end
function smallnum(n,x,y,c)
x = x or @0x5f26
y = y or @0x5f27
c = c or @0x5f25
n = tostr(n)
pal(7,c)
palt(0,1)
for i=1,#n do
spr(tonum(sub(n,i,i)),x + (i-1) * 4, y)
end
pal()
end
-->8
-- plotting
-- plots
function round(x)
return (x-flr(x)<0.5 and
flr or ceil)(x)
end
function lerp(t,x0,x1)
return x0 *(1-t) + (x1-x0)*t
end
function make_ax(posx,posy,
width,height,x0,x1,y0,y1)
local ax= {
px=posx,py=posy,
w=width,h=height,
x0=x0,x1=x1,y0=y0,y1=y1
}
if x0 and x1 then
ax.dx = (x1-x0)/width
end
if y0 and y1 then
ax.dy = (y1-y0)/height
end
return ax
end
function axpoint(ax,x,y)
return ax.px +
round((x-ax.x0)/ax.dx),
ax.py + ax.h -
round((y-ax.y0)/ax.dy)
end
function linfun(fn,x0,x1,n)
local xs,ys = {},{}
for x=x0,x1,(x1-x0)/n do
add(xs,x)
add(ys,fn(x))
end
return xs, ys
end
function plotfn(fn,ax,addpt,x0,x1)
ax.x0 = ax.x0 or x0
ax.x1 = ax.x1 or x1
if not ax.dx then
ax.dx = (ax.x1-ax.x0)/width
end
local xs,ys=linfun(fn,ax.x0,ax.x1,ax.w)
plot(xs,ys,ax,addpt)
end
function minmax(xs)
local x0, x1
for i=1,#xs do
x0=min(x0,xs[i])
x1=max(x1,xs[i])
end
return x0, x1
end
function plot(xs,ys,ax,addpt)
addpt = addpt or line
if not (ax.x0 and ax.x1) then
local x0, x1 = minmax(xs)
ax.x0 = ax.x0 or x0
ax.x1 = ax.x1 or x1
ax.dx = (ax.x1-ax.x0)/ax.w
end
if not (ax.y0 and ax.y1) then
local y0, y1 = minmax(ys)
ax.y0 = ax.y0 or y0
ax.y1 = ax.y1 or y1
ax.dy = (ax.y1-ax.y0)/ax.h
end
if addpt==line then
line()
end
for i=1,#xs do
local x,y = axpoint(ax,xs[i],ys[i])
addpt(x,y)
end
end
function draw_ax(ax,x,y,dotted)
dotted = (dotted==true and 2) or dotted or 0
x = x or 0
y = y or 0
x,y = axpoint(ax,x,y)
local x0,y0 = axpoint(ax,ax.x0,ax.y0)
local x1,y1 = axpoint(ax,ax.x1,ax.y1)
if dotted > 1 then
for xi=ax.px,ax.px+ax.w,dotted do
pset(xi,y)
end
for yi=ax.py,ax.py+ax.h,dotted do
pset(x,yi)