forked from nuive/pokemon-access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gba.lua
2522 lines (2303 loc) · 63.5 KB
/
gba.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
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
ROM_TITLE_ADDRESS = 0x80000A0
ROM_GAMECODE_START = 12
REQUIRED_FILES = {"chars.lua", "memory.lua"}
codemap = {
["BPR"] = "firered",
["BPG"] = "leafgreen",
-- ["BPE"] = "emerald",
}
CHAR_NAME_END = 0xFF
DOWN = 1
UP = 2
LEFT = 3
RIGHT = 4
MAPNAME_PATTERN = "\x00\x23\x00\x24\x00\x24\x00\x25"
DEFAULT_CAMERA_X = -8
DEFAULT_CAMERA_Y = -8
connection_point = {
[0] = "unknown",
[1] = "south",
[2] = "north",
[3] = "west",
[4] = "east",
[5] = "dive",
[6] = "emerge"
}
SPECIAL_FLAGS_START = 0x4000
VARS_START = 0x4000
SPECIAL_VARS_START = 0x8000
PRIMARY_METATILES = 0x280
TOTAL_METATILES = 0x400
BADGES_START = 0x820
BADGE_CUT = 2
BADGE_SURF = 5
BADGE_WATERFALL = 0
TEXTBOX_BORDER = {
"\x00\x13\x00\x14\x00\x15\x00\x16\x00\x17",
"\x08\x13\x08\x14\x08\x15\x08\x16\x08\x17",
"\x02\x00\x02\x01\x02\x02\x02\x03\x02\x04",
"\x0A\x00\x0A\x01\x0A\x02\x0A\x03\x0A\x04",
"\x00\x01\x00\x02\x00\x02\x00\x03",
"\x00\x08\x00\x09\x00\x09\x00\x0A",
"\x00\x02\x00\x03\x00\x03\x00\x04",
"\x00\x08\x00\x09\x00\x09\x00\x0A",
"\x00\x30\x00\x31\x00\x32\x00\x33\x00\x34",
"\x00\x36\x00\x37\x00\x38\x00\x39\x00\x3a",
"\x00\x4f\x00\x50\x00\x50\x00\x51",
"\x00\x55\x00\x56\x00\x56\x00\x57",
"\x00\x58\x00\x59\x00\x59\x00\x5A",
"\x00\x5E\x00\x5F\x00\x5F\x00\x60",
"\x00\x64\x00\x65\x00\x65\x00\x66",
"\x00\x6a\x00\x6b\x00\x6b\x00\x6c",
"\x00\x81\x00\x82\x00\x82\x00\x83",
"\x00\x87\x00\x88\x00\x88\x00\x89",
"\x03\xA3\x03\xA4\x03\xA4\x03\xA5",
"\x03\xA9\x03\xAA\x03\xAA\x03\xAB",
"\x03\xC0\x03\xC1\x03\xC1\x03\xC2",
"\x03\xC6\x03\xC7\x03\xC7\x03\xC8",
}
KEYBOARD_KEYS = {
[0] = "keyboard_change",
[1] = "keyboard_backspace",
[2] = "keyboard_ok"
}
IO_BG_X = 0x4000010
IO_BG_Y = 0x4000012
SCREEN_PIXELS = 38400
SCREEN_WIDTH = 240
SCREEN_HEIGHT = 160
BG_PIXELS = 262144
BG_WIDTH = 512
BG_HEIGHT = 512
MAX_WINDOWS = 32
MAX_SPRITES = 64
SPRITE_DIMS = {
[0] = {
width = 8,
height = 8
},
[1] = {
width = 16,
height = 8
},
[2] = {
width = 8,
height = 16
},
[4] = {
width = 16,
height = 16
},
[5] = {
width = 32,
height = 8
},
[6] = {
width = 8,
height = 32
},
[8] = {
width = 32,
height = 32
},
[9] = {
width = 32,
height = 16
},
[10] = {
width = 16,
height = 32
},
[12] = {
width = 64,
height = 64
},
[13] = {
width = 64,
height = 32
},
[14] = {
width = 32,
height = 64
}
}
SPRITE_TILES = 1024
DOUBLE_COORDS = {
[0] = {71, 72},
[1] = {15},
[2] = {96, 97},
[3] = {40}
}
window_screen = {}
bg_screen = {}
last_scroll_x = {}
last_scroll_y = {}
text_window = {}
sprite_tile = {}
function free_memory()
local address = memory.getregister("r0")
for i=0, 3 do
if address == get_bg_tilemap(i) then
clear_bg_screen(i)
clear_window_screen(i)
return
end
end
end
function copy_window_to_vram()
if memory.getregister("r1") > 1 then
copy_window_to_screen(get_window(memory.getregister("r0")))
end
end
function fill_window_pixel_rect()
local id = memory.getregister("r0")
if not text_window[id] then
return
end
local window = get_window_header(id)
local left = memory.getregister("r2")
local top = memory.getregister("r3")
local width = memory.readword(memory.getregister("r13"))
local height = memory.readword(memory.getregister("r13")+4)
for i = top * window.width, (top + (height -1)) * window.width, window.width do
for j = left, (left + (width -1)) do
text_window[window.id][i+j] = 0xFF
end
end
end
function remove_window()
local window = memory.getregister("r0")
text_window[window] = clear_window(get_window(window), 0xFE)
copy_window_to_screen(get_window(window))
end
function blit_bitmap_rect_to_window()
if ignore_bitmap then
ignore_bitmap = false
return
end
local id = memory.getregister("r0")
if not text_window[id] then
return
end
local window = get_window_header(id)
local left = memory.readdword(memory.getregister("r13")+8)
local top = memory.readdword(memory.getregister("r13")+12)
local width = memory.readword(memory.getregister("r13")+16)
local height = memory.readdword(memory.getregister("r13")+20)
for i = top * window.width, (top + (height -1)) * window.width, window.width do
for j = left, (left + (width -1)) do
text_window[window.id][i+j] = 0xFF
end
end
end
function blit_move_info_icon()
local window = get_window(memory.getregister("r0"))
if not window.data then
return
end
local type = memory.getregister("r1")
local x = memory.getregister("r2")
local y = memory.getregister("r3") +8
window.data[y *window.width +x] = bit.bor(type, 0xFB00)
ignore_bitmap = true
end
function scroll_window()
local id = memory.getregister("r0")
if not text_window[id] then
return
end
local window = get_window_header(id)
local direction = memory.getregister("r1")
local distance = memory.getregister("r2")
local size = window.width * window.height
if direction == 0 then
for i = 0, size - 1 do
local new_i = i + distance * window.width
if new_i < size then
text_window[window.id][i] = text_window[window.id][new_i]
else
text_window[window.id][i] = 0xFF
end
end
elseif direction == 1 then
for i = size - 1, 0, -1 do
local new_i = i - distance * window.width
if new_i < 0 then
text_window[window.id][i] = 0xFF
else
text_window[window.id][i] = text_window[window.id][new_i]
end
end
end
end
function put_window_tilemap()
copy_window_to_screen(get_window(memory.getregister("r0")))
end
function get_window_tilelines(window)
if not window.data then
return {}
end
local tile_lines = {}
for i = 0, (window.width * window.height)-1, window.width do
local tile_line = ""
for j = 0, window.width-1 do
local char = window.data[i+j]
if char ~= nil then
if char > 0xFF then
tile_line = tile_line .. string.char(bit.rshift(char, 8)) .. string.char(char % 0x100)
else
tile_line = tile_line .. string.char(char)
end
else
tile_line = tile_line .. string.char(0xFF)
end
end
table.insert(tile_lines, tile_line)
end
return tile_lines
end
function get_window_tilelines_from_screen(window)
if not window.data then
return {}
end
local screen_tilelines = get_window_screen().tile_lines
local tile_lines = {}
for i = window.top +1, window.top +window.height do
table.insert(tile_lines, screen_tilelines[i]:sub(window.left +1, window.left +window.width))
end
return tile_lines
end
function get_last_window()
for i = 31, 0, -1 do
if memory.readbyte(RAM_WINDOWS +12 *i) ~= 0xFF then
return i
end
end
return nil
end
function window_is_empty(window)
if window.data == nil then
return true
end
for i = 0, (window.width * window.height)-1 do
if window.data[i] ~= 0xFE and window.data[i] ~= 0xFF then
return false
end
end
return true
end
function fill_window_pixel_buffer()
local id = memory.getregister("r0")
text_window[id] = clear_window(get_window(id))
end
function get_window_header(id, pixels)
if pixels == nil then
pixels = true
end
local window = {}
window.id = id
window.bg = memory.readbyte(RAM_WINDOWS+id*12)
window.left = memory.readbyte(RAM_WINDOWS+id*12+1)
window.top = memory.readbyte(RAM_WINDOWS+id*12+2)
window.width = memory.readbyte(RAM_WINDOWS+id*12+3)
window.height = memory.readbyte(RAM_WINDOWS+id*12+4)
window.data_address = memory.readdword(RAM_WINDOWS+id*12+8)
if pixels then
window.left = window.left * 8
window.top = window.top * 8
window.width = window.width * 8
window.height = window.height * 8
end
return window
end
function get_window(id, pixels)
window = get_window_header(id, pixels)
window.data = text_window[id]
return window
end
function copy_window_to_screen(window)
if not window.data then
return
end
local screen_width, screen_height = get_bg_screen_size(window.bg)
local left = bit.band(window.left, screen_width *8 -1)
local top = bit.band(window.top, screen_height *8 -1)
for i = 0, (window.height - 1) do
for j = 0, (window.width - 1) do
window_screen[window.bg][((top + i)*BG_WIDTH) + (left + j)] = window.data[i*window.width+j]
end
end
for i = 0, ((window.height /8) -1) do
for j = 0, ((window.width /8) *2 -1) do
bg_screen[window.bg][(((top /8)+ i)*BG_WIDTH/4) + ((left/8)*2 + j)] = 0x00
end
end
end
function create_window(bg, left, top, width, height, fill)
if not fill then
fill = 0xFE
end
local window = {}
for i = 0, (width * height) -1 do
window[i] = fill
end
return {
bg=bg,
left=left,
top=top,
width=width,
height=height,
data_address=0,
data=window}
end
function clear_window(window, value)
if not value then
value = 0xFF
end
window.data = {}
for i = 0, (window.width * window.height) -1 do
window.data[i] = value
end
return window.data
end
function clear_window_from_screen(window)
local screen_width, screen_height = get_bg_screen_size(window.bg)
local left = bit.band(window.left, screen_width *8 -1)
local top = bit.band(window.top, screen_height *8 -1)
for i = 0, (window.height - 1) do
for j = 0, (window.width - 1) do
window_screen[window.bg][((top + i)*BG_WIDTH) + (left + j)] = 0xFE
end
end
end
function get_screen_starting_position(bg, pixels)
if pixels == nil then
pixels = true
end
local screen_width, screen_height = get_bg_screen_size(bg)
local left = bit.band(memory.readword(IO_BG_X +4 *bg), screen_width *8 -1)
local top = bit.band(memory.readword(IO_BG_Y +4 *bg), screen_height *8 -1)
if not pixels then
left = math.floor(left /4)
top = math.floor(top /8)
end
return left, top
end
function is_scrolling()
local scrolling = false
for i = 0, 3 do
local scroll_x, scroll_y = get_screen_starting_position(i)
if scroll_x ~= last_scroll_x[i] or scroll_y ~= last_scroll_y[i] then
if not unread_text
and last_scroll_x[i] ~= nil and last_scroll_y[i] ~= nil
and (math.abs(scroll_x - last_scroll_x[i]) >= 240
or math.abs(scroll_y -last_scroll_y[i]) >= 160) then
want_read = true
end
scrolling = true
end
last_scroll_x[i] = scroll_x
last_scroll_y[i] = scroll_y
end
return scrolling
end
function get_hidden_bg()
if keyboard_showing() then
return memory.readbyte(memory.readdword(RAM_NAMING_DATA_POINTER) + 0x1E21) + 1
end
return nil
end
function get_bg_screen_size(id)
local size = bit.rshift(bit.band(memory.readbyte(RAM_BGS +4 *id), 0xC), 2)
return 32 *(1 +bit.band(size, 1)), 32 *(1 +bit.rshift(size, 1))
end
function get_bg_priority(id)
return bit.rshift(bit.band(memory.readbyte(RAM_BGS +4 *id), 0x30), 4)
end
function get_bg_tilemap(id)
return memory.readdword(RAM_BG_TILEMAPS +16 *id)
end
function sort_bgs()
local bgs = {0, 1, 2, 3}
for i = 1, 4 do
local priority = get_bg_priority(bgs[i])
for j = i -1, 1, -1 do
local new_priority = get_bg_priority(bgs[j])
if new_priority > priority then
local tmp = bgs[j +1]
bgs[j +1] = bgs[j]
bgs[j] = tmp
end
end
end
return bgs
end
function fill_bg_tilemap_buffer_rect()
local bg = memory.getregister("r0")
if bg > 3 then
return
end
local screen_width, screen_height = get_bg_screen_size(bg)
local tile = memory.getregister("r1")
local left = bit.band(memory.getregister("r2"), screen_width -1) * 2
local top = bit.band(memory.getregister("r3"), screen_height -1)
local width = memory.readword(memory.getregister("r13")) * 2
local height = memory.readword(memory.getregister("r13")+4)
local bg_width = BG_WIDTH / 4
for i = top * bg_width, (top + (height -1)) * bg_width, bg_width do
for j = left, (left + (width -1)), 2 do
bg_screen[bg][i+j] = bit.rshift(tile, 8)
bg_screen[bg][i+j+1] = bit.band(tile, 0xFF)
end
end
left = left * 4
top = top * 8
width = width * 4
height = height * 8
local window_fill
if tile == 0 then
window_fill = 0xFE
else
window_fill = 0xff
end
for i = top * BG_WIDTH, (top + (height -1)) * BG_WIDTH, BG_WIDTH do
for j = left, (left + (width -1)) do
window_screen[bg][i+j] = window_fill
end
end
end
function handle_battle_window()
local flags = memory.readdword(memory.getregister("r13"))
if bit.band(flags, 1) == 0 then
return
end
local bg
if bit.band(flags, 80) ~= 0 then
bg = 1
else
bg = 0
end
local screen_width, screen_height = get_bg_screen_size(bg)
local start_x = bit.band(memory.getregister("r0"), screen_width -1) * 2
local start_y = bit.band(memory.getregister("r1"), screen_height -1)
local end_x = bit.band(memory.getregister("r2"), screen_width -1) * 2
local end_y = bit.band(memory.getregister("r3"), screen_height -1)
local bg_width = BG_WIDTH / 4
for i = start_y * bg_width, end_y * bg_width, bg_width do
for j = start_x, end_x +1 do
bg_screen[bg][i+j] = 0
end
end
start_x = start_x * 4
start_y = start_y * 8
end_x = (end_x+1) *4 -1
end_y = (end_y +1) *8 -1
for i = start_y * BG_WIDTH, end_y * BG_WIDTH, BG_WIDTH do
for j = start_x, end_x do
window_screen[bg][i+j] = 0xFE
end
end
end
function check_window_present(template, first_window, windows_to_check)
if not first_window then
first_window = 0
end
if not windows_to_check then
windows_to_check = 32
end
local current_windows = RAM_WINDOWS + 12 * first_window
local i = 0
while i < windows_to_check and memory.readbyte(template) ~= 0xFF do
local req_window = memory.readbyterange(template, 8)
local window = memory.readbyterange(current_windows, 8)
if not compare(window, req_window) then
return false
end
template = template + 8
current_windows = current_windows + 12
i = i + 1
end
return true
end
function questlog_clean()
local id = memory.getregister("r0")
if not text_window[id] then
return
end
for i = 0, 0x2D00 do
text_window[id][i] = 0xFF
end
end
function sprite_tile_num(id)
return bit.band(memory.readword(RAM_SPRITES + (0x44*id) +4), 0x3FF)
end
function sprite_flags(id)
return memory.readword(RAM_SPRITES + (0x44*id) +0x3E)
end
function sprite_in_use(id)
return bit.band(sprite_flags(id), 1) ~= 0
end
function sprite_invisible(id)
return bit.band(sprite_flags(id), 4) ~= 0
end
function sprite_coords(id)
return bit.band(memory.readword(RAM_SPRITES + (0x44*id) +2), 0x1FF), memory.readbyte(RAM_SPRITES + (0x44*id))
end
function sprite_dimensions(id)
local index = bit.rshift(memory.readbyte(RAM_SPRITES + (0x44*id) +3), 6) *4 +bit.rshift(memory.readbyte(RAM_SPRITES + (0x44*id) +1), 6)
return SPRITE_DIMS[index].width, SPRITE_DIMS[index].height
end
function sprite_priority(id)
return bit.rshift(bit.band(memory.readbyte(RAM_SPRITES + (0x44*id) +5), 0xC), 2)
end
function build_sprite_data(id, sprite)
local left = sprite_tile_num(id)
local width = SPRITE_TILES * 8
local width_tiles = sprite.width /8
for i = 0, sprite.height -1 do
for j = 0, sprite.width -1 do
sprite.data[i * sprite.width + j] = sprite_tile[(i % 8)*width + (left + math.floor(i /8) *width_tiles) *8 +j]
end
end
end
function copy_sprite_to_screen(sprite, screen)
if not sprite.data then
return
end
for i = 0, (sprite.height - 1) do
for j = 0, (sprite.width - 1) do
if sprite.data[i*sprite.width+j] ~= 0xfe then
screen[((sprite.top + i)*SCREEN_WIDTH) + (sprite.left + j)] = sprite.data[i*sprite.width+j]
end
end
end
end
function animate_sprites(last_sprite, bg_priority, screen)
for i = last_sprite, 0, -1 do
local current = memory.readbyte(RAM_SPRITE_ORDER +i)
if sprite_priority(current) >= bg_priority then
if sprite_in_use(current) and not sprite_invisible(current) then
sprite = create_window(nil, nil, nil, sprite_dimensions(current))
build_sprite_data(current, sprite)
sprite.left, sprite.top = sprite_coords(current)
copy_sprite_to_screen(sprite, screen)
end
else
return i
end
end
return -1
end
function draw_text_to_tile_buffer()
local window = get_last_window()
if window == nil then
return
end
window = get_window(window)
if window.data == nil then
return
end
tmp_sheet = create_window(4, 0, 0, 384, 8)
local size = memory.getregister("r0")
local left = 0
for time = size, 1, -1 do
for i = 0, window.height -1 do
for j = 0, 31 do
tmp_sheet.data[(i % 8)*tmp_sheet.width + math.floor(i /8) *32 + left *2 +j] = window.data[i*window.width +left +j]
end
end
left = left +32
end
end
function load_sprite_sheet()
local dest_data = bit.band(memory.getregister("r1"), 0xFFFF)
local size = memory.getregister("r2") *2
local left = dest_data /4
local width = SPRITE_TILES * 8
local copy_width = size /8
local copy_height = 8
local sheet
if tmp_sheet then
sheet = tmp_sheet
tmp_sheet = nil
else
sheet = create_window(nil, 0, 0, copy_width, copy_height)
end
for i = 0, copy_height -1 do
for j = 0, copy_width -1 do
sprite_tile[i*width + left + j] = sheet.data[i *copy_width +j]
end
end
end
function copy_window_to_sprite_tiles(window, source_data, dest_data, line_start, sprite_width, copy_width, copy_height)
if window.data == nil then
return
end
source_data = source_data - window.data_address
dest_data = bit.band(dest_data, 0xFFFF)
local window_left = (source_data /4) %window.width
local window_top = math.floor((source_data /4) /window.width)
local left = dest_data /4
local width = SPRITE_TILES * 8
for i = line_start, copy_height -1 do
for j = 0, copy_width -1 do
sprite_tile[(i % 8)*width + left + math.floor(i /8) *sprite_width + j] = window.data[(window_top + i)*window.width+(window_left + j)]
end
end
end
function add_healthbox_text()
local window = get_last_window()
if window == nil then
return
end
copy_window_to_sprite_tiles(get_window(window), memory.getregister("r1"), memory.getregister("r0"), 5, 64, memory.getregister("r2") *8, 16)
end
function safari_add_healthbox_text()
local window = get_last_window()
if window == nil then
return
end
copy_window_to_sprite_tiles(get_window(window), memory.getregister("r1"), memory.getregister("r0"), 0, 64, memory.getregister("r2") *8, 16)
end
function show_status_in_healthbox()
local window = create_window(nil, 0, 0, 32, 8, 0xFF)
local status = memory.getregister("r4")
local fill = 0xFF
if bit.band(status, 0x07) ~= 0 then
fill = 0xFC03
elseif bit.band(status, 0x88) ~= 0 then
fill = 0xFC01
elseif bit.band(status, 0x10) ~= 0 then
fill = 0xFC05
elseif bit.band(status, 0x20) ~= 0 then
fill = 0xFC04
elseif bit.band(status, 0x40) ~= 0 then
fill = 0xFC02
end
local line = window.width *4
window.data[line] = fill
local dest_data = (sprite_tile_num(memory.getregister("r9")) +memory.getregister("r8")) *32
copy_window_to_sprite_tiles(window, 0, dest_data, 0, 32, 24, 8)
end
function show_status_in_summary()
tmp_sheet = create_window(nil, 0, 0, 256, 8, 0xFF)
local line = tmp_sheet.width *4
for i = 1, 7 do
tmp_sheet.data[line +32 *(i -1) +4] = 0xFC00 +i
end
end
function print_box_data_to_sprite()
local window = get_last_window()
if window == nil then
return
end
copy_window_to_sprite_tiles(get_window(window), memory.getregister("r0"), memory.getregister("r1"), 0, 64, memory.getregister("r2") /2, 16)
end
function clear_window_screen(id)
window_screen[id] = {}
for i = 0, BG_PIXELS-1 do
window_screen[id][i] = 0xFE
end
end
function clear_bg_screen(id)
bg_screen[id] = {}
for i = 0, (BG_PIXELS /32)-1 do
bg_screen[id][i] = 0x00
end
end
function clear_sprite_tiles()
sprite_tiles = {}
for i = 0, (SPRITE_TILES *64) -1 do
sprite_tile[i] = 0xFE
end
end
function clear_all_window_screens()
clear_textbox()
for i = 0, 3 do
clear_window_screen(i)
end
end
function clear_all_bg_screens()
for i = 0, 3 do
clear_bg_screen(i)
end
end
function clear_screen_areas()
clear_all_bg_screens()
clear_all_window_screens()
end
function clear_windows_and_screen_areas()
clear_screen_areas()
text_window = {}
end
function clear_vram()
clear_all_bg_screens()
clear_all_window_screens()
clear_sprite_tiles()
end
function clear_all_graphics()
clear_windows_and_screen_areas()
clear_sprite_tiles()
end
function get_bg_screen()
local screen = {}
for i = 0, (SCREEN_PIXELS /32)-1 do
screen[i] = 0x00
end
local bgs = sort_bgs()
for i = 4, 1, -1 do
local current = bgs[i]
local hidden_bg = get_hidden_bg()
if not hidden_bg or hidden_bg ~= current then
local left, top = get_screen_starting_position(current, false)
for y = 0, (SCREEN_HEIGHT /8) -1 do
for x = 0, (SCREEN_WIDTH /4) -1 do
local value = bg_screen[current][(top +y) *BG_WIDTH /4 +left +x]
if value and value ~= 0x00 then
screen[y *SCREEN_WIDTH /4 +x] = value
end
end
end
end
end
local tile_lines = {}
for i = 0, (SCREEN_PIXELS /32) -1, SCREEN_WIDTH / 4 do
local tile_line = ""
local j = 0
while j < SCREEN_WIDTH/4 do
tile_line = tile_line .. string.char(screen[i+j])
j = j + 1
end
table.insert(tile_lines, tile_line)
end -- i
return tile_lines
end
function get_window_screen()
local screen = {}
for i = 0, SCREEN_PIXELS-1 do
screen[i] = 0xFF
end
local bgs = sort_bgs()
local last_sprite = MAX_SPRITES -1
for i = 4, 1, -1 do
local current = bgs[i]
local hidden_bg = get_hidden_bg()
if not hidden_bg or hidden_bg ~= current then
local left, top = get_screen_starting_position(current)
for y = 0, SCREEN_HEIGHT -1 do
for x = 0, SCREEN_WIDTH -1 do
local value = window_screen[current][(top +y) *BG_WIDTH +left +x]
if value and value ~= 0xFE then
screen[y *SCREEN_WIDTH +x] = value
end
end
end
end
last_sprite = animate_sprites(last_sprite, get_bg_priority(current), screen)
end
local lines = {}
local tile_lines = {}
for i = 0, SCREEN_PIXELS-1, SCREEN_WIDTH do
local line = ""
local tile_line = ""
local empty_pixels = 0
local j = 0
while j < SCREEN_WIDTH do
local char = screen[i+j]
if char ~= 0xFF then
empty_pixels = 0
if char > 0xFF then
tile_line = tile_line .. string.char(bit.rshift(char, 8)) .. string.char(char % 0x100)
else
tile_line = tile_line .. string.char(char)
end
line = line .. translate(char)
else
tile_line = tile_line .. string.char(char)
empty_pixels = empty_pixels + 1
if empty_pixels == 8 then
line = line .. " "
empty_pixels = 0
end
end
j = j + 1
end
table.insert(lines, line)
table.insert(tile_lines, tile_line)
end -- i
return {lines=lines, tile_lines=tile_lines, get_textbox=get_textbox}
end
function translate_tileline(tileline)
local l = ""
local empty_pixels = 0
local i = 1
while i <= #tileline do
local char = tileline:sub(i, i):byte()
if char ~= 0xFE and char ~= 0xFF then
if char > 0xF7 then
i = i + 1
char = char * 0x100 + tileline:sub(i, i):byte()
end
empty_pixels = 0
l = l .. translate(char)
else
empty_pixels = empty_pixels + 1
if empty_pixels == 8 then
l = l .. " "
empty_pixels = 0
end
end
i = i + 1
end
return trim(l)
end
function read_window(window, from_screen)
local tilelines
if from_screen then
tilelines = get_window_tilelines_from_screen(window)
else
tilelines = get_window_tilelines(window)
end
for _, tileline in pairs(tilelines) do
local line = translate_tileline(tileline)
if line ~= "" then
tolk.output(line)
end
end
end
function read_menu_item(grid)
local window = memory.getregister("r0")
if not text_window[window] or window >= MAX_WINDOWS then
return
end
local left = memory.getregister("r3")
local position = memory.readword(memory.getregister("r13")) +9
local tiles = get_window_tilelines(get_window(window))
if tiles[position] then
local item = ""
if grid then
local width = memory.readbyte(RAM_MENU + 7)
item = translate_tileline(tiles[position]:sub(left, left + width - 1))
else
item = translate_tileline(tiles[position]:sub(left))
end
tolk.output(item)
end
end
function read_mainmenu_item()
local position = memory.getregister("r2") / 0x100
local lines = get_window_screen().lines
tolk.output(lines[position + 19])
end
function read_list_menu_item()
local window = memory.getregister("r0")
if not text_window[window] then
return
end
local left = memory.getregister("r2")
local top = memory.getregister("r3") +9
local tiles = get_window_tilelines(get_window(window))
if tiles[top] then
tolk.output(translate_tileline(tiles[top]:sub(left)))
end
end