-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsell.asm
1010 lines (870 loc) · 13.2 KB
/
sell.asm
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
; This file is part of Pwn Adventure Z.
; Pwn Adventure Z is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; Pwn Adventure Z is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with Pwn Adventure Z. If not, see <http://www.gnu.org/licenses/>.
.include "defines.inc"
.code
PROC show_sell_tab
jsr clear_alt_screen
LOAD_ALL_TILES $000, inventory_ui_tiles
; Determine which sell items are available
lda #0
sta arg0
sta valid_shop_count
checkloop:
ldx arg0
lda sell_items, x
jsr find_item
ldx valid_shop_count
sta valid_shop_index, x
cmp #$ff
beq nextitem
hasitem:
lda arg0
ldx valid_shop_count
sta valid_shop_list, x
inc valid_shop_count
nextitem:
ldx arg0
inx
stx arg0
cpx sell_item_count
bne checkloop
lda #0
sta selection
lda #0
sta scroll
jsr render_sell_screen
lda valid_shop_count
beq nosetupselect
jsr select_sell_item
nosetupselect:
lda #30
sta repeat_time
lda valid_shop_count
bne selectloop
jmp emptyloop
selectloop:
jsr update_controller
jsr wait_for_vblank
lda controller
and #JOY_DOWN
bne downpressed
lda controller
and #JOY_UP
bne up
lda controller
and #JOY_A | JOY_B
bne sellpressed
lda controller
and #JOY_LEFT
bne buy
lda controller
and #JOY_RIGHT
bne buyback
lda controller
and #JOY_SELECT | JOY_START
beq nobutton
jmp done
buy:
PLAY_SOUND_EFFECT effect_uimove
jsr fade_out
jmp show_buy_tab
buyback:
PLAY_SOUND_EFFECT effect_uimove
jsr fade_out
jmp show_buyback_tab
nobutton:
lda #30
sta repeat_time
jmp selectloop
downpressed:
jmp down
sellpressed:
jmp sell
up:
lda selection
beq attop
PLAY_SOUND_EFFECT effect_uimove
jsr deselect_inventory_item
dec selection
jsr select_sell_item
attop:
jmp waitfordepress
down:
lda selection
clc
adc #1
cmp valid_shop_count
beq atbottom
PLAY_SOUND_EFFECT effect_uimove
jsr deselect_inventory_item
inc selection
jsr select_sell_item
atbottom:
jmp waitfordepress
sell:
jsr sell_current_item
cmp #0
beq waitfordepresslong
jsr wait_for_vblank
jsr generate_gold_string
LOAD_PTR gold_str
ldx #24
ldy #32 + 26
jsr write_string
jsr prepare_for_rendering
PLAY_SOUND_EFFECT_NO_OVERRIDE effect_sell
lda controller
and #JOY_B
beq waitfordepresslong
ldx #2
jsr wait_for_frame_count
jmp selectloop
shortwaitfordepress:
lda repeat_time
cmp #30
bne nowait
ldx #6
jsr wait_for_frame_count
lda #3
sta repeat_time
jmp selectloop
waitfordepresslong:
jsr update_controller
jsr wait_for_vblank
lda controller
bne waitfordepresslong
jmp selectloop
nowait:
jmp selectloop
waitfordepress:
lda repeat_time
sta arg0
waitfordepressloop:
dec arg0
beq waitfordepresstimeout
jsr update_controller
jsr wait_for_vblank
lda controller
bne waitfordepressloop
jmp selectloop
waitfordepresstimeout:
lda #3
sta repeat_time
jmp selectloop
emptyloop:
jsr update_controller
jsr wait_for_vblank
lda controller
and #JOY_LEFT
bne emptybuy
lda controller
and #JOY_RIGHT
bne emptybuyback
lda controller
and #JOY_SELECT | JOY_START
beq emptyloop
jmp done
emptybuy:
jmp buy
emptybuyback:
jmp buyback
done:
jmp end_inventory_screen
.endproc
PROC sell_current_item
; Get item to be sold
ldx selection
lda valid_shop_list, x
sta arg2
tax
lda sell_items, x
jsr get_item_type
cmp #ITEM_TYPE_GUN
beq sellgun
; Check for item
ldx arg2
lda sell_items, x
jsr find_item
cmp #$ff
beq invalidsell
asl
tay
lda inventory, y
bne validsell
invalidsell:
lda #0
rts
validsell:
; Take away item
sec
sbc #1
sta inventory, y
bne notempty
jmp deleteitem
sellgun:
; Selling a gun will remove the item no matter what the ammo count is
ldx arg2
lda sell_items, x
jsr find_item
cmp #$ff
beq invalidsell
asl
tay
; Out of the item, remove it from inventory
deleteitem:
tya
lsr
sta arg3
deleteloop:
lda arg3
clc
adc #1
cmp inventory_count
beq deletedone
lda arg3
asl
tax
lda inventory + 2, x
sta inventory, x
lda inventory + 3, x
sta inventory + 1, x
inc arg3
jmp deleteloop
deletedone:
dec inventory_count
notempty:
; Give gold for item
ldx arg2
lda sell_price_low, x
clc
adc gold + 3
cmp #10
bcc nocarry3
adc #$f5
nocarry3:
sta gold + 3
lda sell_price_mid, x
adc gold + 2
cmp #10
bcc nocarry2
adc #$f5
nocarry2:
sta gold + 2
lda sell_price_high, x
adc gold + 1
cmp #10
bcc nocarry1
adc #$f5
nocarry1:
sta gold + 1
lda gold
adc #0
sta gold
cmp #10
bcc notmax
lda #9
sta gold
sta gold + 1
sta gold + 2
sta gold + 3
notmax:
ldx arg2
jsr add_buyback_item
; Refresh item indexes as they may have changed
lda #0
sta arg2
refreshloop:
ldx arg2
lda valid_shop_list, x
tax
lda sell_items, x
jsr find_item
ldx arg2
sta valid_shop_index, x
ldx arg2
inx
stx arg2
cpx valid_shop_count
bne refreshloop
lda selection
jsr get_string_with_item_count
; Draw item count
jsr wait_for_vblank
LOAD_PTR scratch
ldx #7
lda selection
sta arg2
asl
clc
adc arg2
adc #32 + 3
tay
jsr write_string
jsr prepare_for_rendering
lda #1
rts
.endproc
PROC add_buyback_item
; Save sold item into buyback list, first move previous entries down
ldy #5
movebuyback:
lda buyback_items - 1, y
sta buyback_items, y
lda buyback_price_low - 1, y
sta buyback_price_low, y
lda buyback_price_mid - 1, y
sta buyback_price_mid, y
lda buyback_price_high - 1, y
sta buyback_price_high, y
dey
bne movebuyback
lda sell_items, x
sta buyback_items
lda sell_price_low, x
sta buyback_price_low
lda sell_price_mid, x
sta buyback_price_mid
lda sell_price_mid, x
sta buyback_price_mid
ldy buyback_count
cpy #6
beq maxbuyback
iny
sty buyback_count
maxbuyback:
rts
.endproc
.segment "FIXED"
PROC render_sell_screen
lda current_bank
pha
lda #^do_render_sell_screen
jsr bankswitch
jsr do_render_sell_screen & $ffff
pla
jsr bankswitch
rts
.endproc
PROC render_sell_items
lda current_bank
pha
lda #^do_render_sell_items
jsr bankswitch
jsr do_render_sell_items & $ffff
pla
jsr bankswitch
rts
.endproc
PROC select_sell_item
lda current_bank
pha
lda #^do_select_sell_item
jsr bankswitch
jsr do_select_sell_item & $ffff
pla
jsr bankswitch
rts
.endproc
.segment "UI"
PROC do_render_sell_screen
; Draw box around sell screen
lda #1
sta arg0
lda #32 + 1
sta arg1
lda #28
sta arg2
lda #32 + 22
sta arg3
jsr draw_large_box
LOAD_PTR sell_str
ldx #4
ldy #32 + 1
jsr write_string
; Set palette for title
lda #1
sta arg0
lda #16 + 0
sta arg1
lda #3
sta arg2
lda #16 + 0
sta arg3
lda #2
sta arg4
jsr set_box_palette
lda #9
sta arg0
lda #16 + 0
sta arg1
lda #13
sta arg2
lda #16 + 0
sta arg3
lda #2
sta arg4
jsr set_box_palette
; Set palette for inside of box
lda #1
sta arg0
lda #16 + 1
sta arg1
lda #13
sta arg2
lda #16 + 10
sta arg3
lda #1
sta arg4
jsr set_box_palette
; Set palette for status area
lda #0
sta arg0
lda #16 + 12
sta arg1
lda #14
sta arg2
lda #16 + 13
sta arg3
lda #1
sta arg4
jsr set_box_palette
jsr render_inventory_status_bar
; Draw help text
LOAD_PTR sell_help_str
ldx #2
ldy #32 + 23
jsr write_string
lda valid_shop_count
bne hasitems
LOAD_PTR no_items_str
ldx #11
ldy #32 + 11
jsr write_string
jmp itemend & $ffff
hasitems:
; Draw initial items
lda valid_shop_count
bne notempty
LOAD_PTR no_items_str
ldx #11
ldy #32 + 11
jsr write_string
jmp itemend & $ffff
notempty:
jsr render_sell_items
jsr select_sell_item
itemend:
LOAD_PTR inventory_palette
jsr fade_in
rts
.endproc
PROC do_render_sell_items
lda #0
sta arg0
itemloop:
lda arg0
cmp valid_shop_count
bne hasitem
jmp itemend & $ffff
hasitem:
lda arg0
beq first
LOAD_PTR inventory_item_second_box_tiles
jmp drawitem & $ffff
first:
LOAD_PTR inventory_item_first_box_tiles
drawitem:
lda rendering_enabled
bne count
; Draw box around item graphic
ldx #2
lda arg0
asl
clc
adc arg0
adc #32 + 2
sta arg1
tay
lda #4
jsr write_tiles
ldx #2
inc arg1
ldy arg1
lda #4
jsr write_tiles
ldx #2
inc arg1
ldy arg1
lda #4
jsr write_tiles
ldx #2
inc arg1
ldy arg1
lda #4
jsr write_tiles
count:
; Load icon for the item
jsr wait_for_vblank_if_rendering
lda arg0
asl
asl
tax
lda arg0
tay
lda valid_shop_list, y
tay
lda sell_items, y
jsr load_item_sprite_tiles
jsr prepare_for_rendering
lda arg0
asl
asl
asl
clc
adc #16
tay
lda arg0
asl
clc
adc arg0
asl
asl
asl
adc #31
sta sprites, y
sta sprites + 4, y
lda arg0
asl
asl
clc
adc #1
sta sprites + 1, y
adc #2
sta sprites + 5, y
lda #1
sta sprites + 2, y
sta sprites + 6, y
lda #32
sta sprites + 3, y
lda #40
sta sprites + 7, y
jsr wait_for_vblank_if_rendering
lda arg0
tax
lda valid_shop_index, x
cmp #$ff
bne nonzerocount
lda #0
jmp getcountstr & $ffff
nonzerocount:
asl
tax
lda inventory, x
getcountstr:
jsr byte_to_padded_str
; Get the item type
lda arg0
tax
lda valid_shop_list, x
tax
lda sell_items, x
jsr get_item_type
sta arg2
cmp #ITEM_TYPE_GUN
bne notgun
lda #' '
sta scratch
sta scratch + 1
lda #'1'
sta scratch + 2
notgun:
lda #$40
sta scratch + 3
lda #0
sta scratch + 4
; Draw item count
LOAD_PTR scratch
ldx #7
lda arg0
asl
clc
adc arg0
adc #32 + 3
tay
jsr write_string
; Draw item name
lda arg0
tax
lda valid_shop_list, x
tax
lda sell_items, x
jsr get_item_name
ldx #12
lda arg0
asl
clc
adc arg0
adc #32 + 3
sta arg1
tay
jsr write_string
; Draw item type
lda arg2
cmp #ITEM_TYPE_GUN
beq weapon
cmp #ITEM_TYPE_MELEE
beq weapon
cmp #ITEM_TYPE_GRENADE
beq weapon
cmp #ITEM_TYPE_CRAFTING
beq crafting
cmp #ITEM_TYPE_OUTFIT
beq wearable
cmp #ITEM_TYPE_HEALTH
beq healing
cmp #ITEM_TYPE_CONSUMABLE
beq usable
cmp #ITEM_TYPE_CAMPFIRE
beq usable
LOAD_PTR inventory_sell_tiles
jmp drawtype & $ffff
weapon:
LOAD_PTR inventory_weapon_tiles
jmp drawtype & $ffff
crafting:
LOAD_PTR inventory_crafting_tiles
jmp drawtype & $ffff
wearable:
LOAD_PTR inventory_wearable_tiles
jmp drawtype & $ffff
healing:
LOAD_PTR inventory_healing_tiles
jmp drawtype & $ffff
usable:
LOAD_PTR inventory_usable_tiles
drawtype:
lda #6
ldx #12
ldy arg1
iny
jsr write_tiles
jsr prepare_for_rendering
jsr wait_for_vblank_if_rendering
; Draw item price
lda arg0
tax
lda valid_shop_list, x
tax
lda sell_price_high, x
beq twodigit
clc
adc #$30
sta scratch + 1
lda sell_price_mid, x
adc #$30
sta scratch + 2
lda sell_price_low, x
adc #$30
sta scratch + 3
lda #'$'
sta scratch
lda #0
sta scratch + 4
jmp renderprice & $ffff
twodigit:
lda sell_price_mid, x
beq onedigit
clc
adc #$30
sta scratch + 2
lda sell_price_low, x
adc #$30
sta scratch + 3
lda #' '
sta scratch
lda #'$'
sta scratch + 1
lda #0
sta scratch + 4
jmp renderprice & $ffff
onedigit:
lda sell_price_low, x
clc
adc #$30
sta scratch + 3
lda #' '
sta scratch
sta scratch + 1
lda #'$'
sta scratch + 2
lda #0
sta scratch + 4
renderprice:
LOAD_PTR scratch
ldx #23
ldy arg1
iny
jsr write_string
jsr prepare_for_rendering
nextitem:
ldx arg0
inx
stx arg0
jmp itemloop & $ffff
itemend:
rts
.endproc
PROC do_select_sell_item
lda selection
and #1
beq even
lda selection
lsr
sta temp
asl
clc
adc temp
adc #16 + 3
sta temp
lda #3
sta arg0
lda temp
sta arg1
lda #13
sta arg2
lda temp
sta arg3
lda #0
sta arg4
jsr wait_for_vblank
jsr set_box_palette
jsr prepare_for_rendering
jmp palettedone & $ffff
even:
lda selection
lsr
sta temp
asl
clc
adc temp
adc #16 + 1
sta temp
lda #3
sta arg0
lda temp
sta arg1
lda #13
sta arg2
lda temp
sta arg3
lda #0
sta arg4
jsr wait_for_vblank
jsr set_box_palette
jsr prepare_for_rendering
lda selection
lsr
sta temp
asl
clc
adc temp
adc #16 + 2
sta temp
lda #3
sta arg0
lda temp
sta arg1
lda #13
sta arg2
lda temp
sta arg3
lda #0
sta arg4
jsr wait_for_vblank
jsr set_box_palette
jsr prepare_for_rendering
jmp palettedone & $ffff
palettedone:
jsr wait_for_vblank
lda selection
tax
lda valid_shop_list, x
tax
lda sell_items, x
jsr get_item_description
ldx #2
ldy #32 + 21
jsr write_string
jsr prepare_for_rendering
lda selection
sta temp
asl
clc
adc temp
asl
asl
asl
adc #24
sta temp
sta sprites
lda #$5c
sta sprites + 1
lda #0
sta sprites + 2
lda #58
sta sprites + 3
lda temp
clc
adc #13
sta sprites + 4
lda #$5c
sta sprites + 5
lda #SPRITE_FLIP_VERT
sta sprites + 6
lda #58
sta sprites + 7
lda temp
sta sprites + 8
lda #$5c
sta sprites + 9
lda #SPRITE_FLIP_HORIZ
sta sprites + 10
lda #222
sta sprites + 11
lda temp
clc
adc #13
sta sprites + 12
lda #$5c
sta sprites + 13
lda #SPRITE_FLIP_HORIZ | SPRITE_FLIP_VERT
sta sprites + 14
lda #222
sta sprites + 15
rts
.endproc
.data
VAR sell_str
.byte "BUY", $3c, $3c, $3b, " SELL ", $3d, $3c, $3c, "BUYBACK", 0
VAR sell_help_str
.byte "A:SELL B:REPEAT ", $23, "/", $25, ":TAB", 0
.segment "TEMP"
VAR sell_items