Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ace::
- WRAM1:DA7F 21 00 DC ld hl, DC00 ;RESET: INITIALIZE
- WRAM1:DA82 AF xor a ;this will set a to 0
- WRAM1:DA83 22 ldi (hl), a ;Reset base index to 0
- WRAM1:DA84 22 ldi (hl), a ;Reset first index to 0
- WRAM1:DA85 22 ldi (hl), a ;Reset second index to 0
- WRAM1:DA86 36 01 ld (hl), 01 ;Reset merge size to 1
- WRAM1:DA88 0E 01 ld c, 01
- .loop ;LOAD ITEMS
- WRAM1:DA8A 21 01 DC ld hl, DC01
- WRAM1:DA8D 7E ld a, (hl) ;get the first index
- WRAM1:DA8E CD FA DA call getAddressItem [$DAFA] ;get the ptr of the first item
- WRAM1:DA91 28 14 jr z, .oob1 [$DAA7] ;if it is out of bounds, check if also the other item it is in .oob1
- WRAM1:DA93 56 ld d, (hl) ;load first item index id into d
- WRAM1:DA94 23 inc hl
- WRAM1:DA95 5E ld e, (hl) ;load first item quantity into e
- WRAM1:DA96 CD 16 DB call getAddress2 [$DB16] ;load second item
- WRAM1:DA99 28 07 jr z, .secondbigger [$DAA2] ;if it is out of bounds, directly paste the first item that cannot be oob (if it was we're not here)
- WRAM1:DA9B 46 ld b, (hl) ;load second item index id into b
- WRAM1:DA9C 23 inc hl
- WRAM1:DA9D 4E ld c, (hl) ;load second item quantity into c
- WRAM1:DA9E 79 ld a, c ;prepeare for comparing the quantity
- WRAM1:DA9F BB cp e ;compare the quantity
- WRAM1:DAA0 38 0D jr c, .firstBigger [$DAAF] ;if first > second go to first bigger AAAAAAAAAAAAAAA
- .secondBigger
- WRAM1:DAA2 2E 01 ld l, 01 ;set first index to incrase
- WRAM1:DAA4 D5 push de ;push first item into stack
- WRAM1:DAA5 18 0B jr .complete [$DAB2]
- .oob1
- WRAM1:DAA7 CD 16 DB call getAddress2 [$DB16] ;load second item for check if it is oob
- WRAM1:DAAA 28 10 jr z, .Next [$DABC] ;if yes, jump to next array
- WRAM1:DAAC 46 ld b, (hl) ;load second item index id into b
- WRAM1:DAAD 23 inc hl
- WRAM1:DAAE 4E ld c, (hl) ;load second item quantity into c
- .firstBigger
- WRAM1:DAAF 2E 02 ld l, 02 ;set second index to incrase
- WRAM1:DAB1 C5 push bc ;push second item into stack
- .complete
- WRAM1:DAB2 26 DC ld h, DC ;get the address of the index of the selected item
- WRAM1:DAB4 34 inc (hl) ;increment this index
- WRAM1:DAB5 21 03 DC ld hl, DC03
- WRAM1:DAB8 4E ld c, (hl) ;reload the merge size into c
- WRAM1:DAB9 C3 8A DA jp .loop [$DA8A] ;jump again to the loop
- .next
- WRAM1:DABC 79 ld a, c ;put the merge size in a
- WRAM1:DABD 87 add a ;douplicate merge size
- WRAM1:DABE 21 1C D3 ld hl, wNumBagItems [$D31C]
- WRAM1:DAC1 46 ld b, (hl) ;put wNumBagItems in b, for easy use in .sizedone
- WRAM1:DAC2 38 11 jr c, .sizeDone [$DAD5] ;if a > FF goto .sizeDone
- WRAM1:DAC4 21 02 DC ld hl, DC02
- WRAM1:DAC7 36 00 ld (hl), 00 ;reset second index
- WRAM1:DAC9 2B dec hl
- WRAM1:DACA 36 00 ld (hl), 00 ;reset first index
- WRAM1:DACC 2B dec hl
- WRAM1:DACD 86 add (hl) ;add base index. At this point: a[base index] = 2(merge size) + base index
- WRAM1:DACE 38 05 jr c, .sizeDone [$DAD5] ;if a > FF goto .sizeDone
- WRAM1:DAD0 77 ld (hl), a ;save the new base index in ram
- WRAM1:DAD1 B8 cp b ;check if the new base index is oob
- WRAM1:DAD2 DA 8A DA jp c, .loop [$DA8A] ;if no, jump again to .loop
- .sizeDone
- WRAM1:DAD5 21 00 DC ld hl, DC00
- WRAM1:DAD8 36 00 ld (hl), 00 ;reset the base index
- WRAM1:DADA 68 ld l, b
- WRAM1:DADB 26 00 ld h, 00 ;put wNumBagItems into hl
- WRAM1:DADD 29 add hl, hl ;douplicate wNumBagItems
- WRAM1:DADE 11 1C D3 ld de, D31C ;'cuz we can't do directly add hl, d31e we save bc, put this no in hl, add bc to hl and then restore bc
- WRAM1:DAE1 19 add hl, de ;add base inventory quantity index, so it point to the last item quantity
- WRAM1:DAE2 78 ld a, b ;put wNumBagItems into a, for the loop
- .pasteToRam
- WRAM1:DAE3 D1 pop de ;get the item from stack
- WRAM1:DAE4 73 ld (hl), e ;write the item quantity in ram
- WRAM1:DAE5 2B dec hl
- WRAM1:DAE6 72 ld (hl), d ;write the item id in ram
- WRAM1:DAE7 2B dec hl
- WRAM1:DAE8 3D dec a
- WRAM1:DAE9 C2 E3 DA jp nz, .pasteToRam [$DAE3]
- WRAM1:DAEC 79 ld a, c
- WRAM1:DAED 87 add a ;douplicate the merge size
- WRAM1:DAEE D8 ret c ;if a > FF return
- WRAM1:DAEF 4F ld c, a ;put the new merge size in c
- WRAM1:DAF0 21 03 DC ld hl, DC03
- WRAM1:DAF3 71 ld (hl), c ;save the new merge size in ram
- WRAM1:DAF4 78 ld a, b ;restore wNumBagItems
- WRAM1:DAF5 B9 cp c ;check if all merge size are done, by testing if it is more all inventory items
- WRAM1:DAF6 D2 8A DA jp nc, .loop [$DA8A] ;if wNumBagItems > merge size, jump to loop
- WRAM1:DAF9 C9 ret ;else return
- getAddressItem:: ;a = index, c = merge size
- WRAM1:DAFA B9 cp c ;check if AI is out of merge array
- WRAM1:DAFB 30 17 jr nc, .oob [$DB14] ;if yes, jump to .oob
- .noCheck
- WRAM1:DAFD 21 00 DC ld hl, DC00
- WRAM1:DB00 86 add (hl) ;index + base index = absolute index (aka AI)
- WRAM1:DB01 38 11 jr c, .oob [$DB14] ;if result is more than FF, it's obviously out of bounds (also 'cuz max inventory slots are FF)
- WRAM1:DB03 21 1C D3 ld hl, wNumBagItems [$D31C]
- WRAM1:DB06 BE cp (hl) ;check if AI is out of our inventory
- WRAM1:DB07 30 0B jr nc, .oob [$DB14] ;if yes, jump to .oob
- WRAM1:DB09 6F ld l, a
- WRAM1:DB0A 26 00 ld h, 00 ;put AI into lower byte of hl
- WRAM1:DB0C 29 add hl, hl ;AI *= 2
- WRAM1:DB0D C5 push bc
- WRAM1:DB0E 01 1D D3 ld bc, D31D
- WRAM1:DB11 09 add hl, bc ;AI += Address of the index id of the first item in inventory
- WRAM1:DB12 C1 pop bc
- WRAM1:DB13 C9 ret ;We are returning: 2*(BASE INDEX + A ) + BASE INVENTORY ADDRESS
- .oob
- WRAM1:DB14 BF cp a
- WRAM1:DB15 C9 ret ;return a zero flag if it is oob
- getAddress2::
- WRAM1:DB16 21 02 DC ld hl, DC02
- WRAM1:DB19 C5 push bc ;save b and c
- WRAM1:DB1A 79 ld a, c ;copy the merge size into a
- WRAM1:DB1B 41 ld b, c ;copy the merge size into b
- WRAM1:DB1C 87 add a ;douplicate the merge size to avoid false oob
- WRAM1:DB1D 38 08 jr c, .oob2 [$DB27] ;if 2a is oob call getaddresItem without checking if a is oob
- WRAM1:DB1F 4F ld c, a ;set the douplicate merge size into c
- WRAM1:DB20 78 ld a, b ;restore the normal merge size into a
- WRAM1:DB21 86 add (hl) ;add the current index (this is the absoulute merge index)
- WRAM1:DB22 CD FA DA call getAddressItem [$DAFA] ;get the ptr of the second item
- WRAM1:DB25 18 05 jr .subEnd [$DB2C]
- .oob2
- WRAM1:DB27 78 ld a, b ;restore the normal merge size into a
- WRAM1:DB28 86 add (hl) ;add the current index (this is the absoulute merge index)
- WRAM1:DB29 CD FD DA call getAddressItem.noCheck [$DAFD]
- .subEnd
- WRAM1:DB2C C1 pop bc ;restore b and c
- WRAM1:DB2D C9 ret
- ;DC00: base index
- ;DC01: first index
- ;DC02: second index
- ;DC03: dimension (aka merge size)
- ;Raw bytes for easy paste in gbc:
- 21 00 dc af 22 22 22 36 01 0e 01 21 01 dc 7e cd fa da 28 14 56 23 5e cd 16 db 28 07 46 23 4e 79 bb 38 0d 2e 01 d5 18 0b cd 16 db 28 10 46 23 4e 2e 02 c5 26 dc 34 21 03 dc 4e c3 8a da 79 87 21 1c d3 46 38 11 21 02 dc 36 00 2b 36 00 2b 86 38 05 77 b8 da 8a da 21 00 dc 36 00 68 26 00 29 11 1c d3 19 78 d1 73 2b 72 2b 3d c2 e3 da 79 87 d8 4f 21 03 dc 71 78 B9 d2 8a da c9 b9 30 17 21 00 dc 86 38 11 21 1c d3 be 30 0b 6f 26 00 29 c5 01 1d d3 09 c1 c9 bf c9 21 02 dc c5 79 41 87 38 08 4f 78 86 cd fa da 18 05 78 86 cd fd da c1 c9
- Ace:: [$DA7F]
- 21 00 dc af 22 22 22 36 01 0e 01
- .loop [$DA8A]
- 21 01 dc 7e cd fa da 28 14 56 23 5e cd 16 db 28 07 46 23 4e 79 bb 38 0d
- .secondBigger [$DAA2]
- 2e 01 d5 18 0b
- .oob1 [$DAA7]
- cd 16 db 28 10 46 23 4e
- .firstBigger [$DAAF]
- 2e 02 c5
- .complete [$DAB2]
- 26 dc 34 21 03 dc 4e c3 8a da
- .next [$DABC]
- 79 87 21 1c d3 46 38 11 21 02 dc 36 00 2b 36 00 2b 86 38 05 77 b8 da 8a da
- .sizeDone [$DAD5]
- 21 00 dc 36 00 68 26 00 29 11 1c d3 19 78
- .pasteToRam [$DAE3]
- d1 73 2b 72 2b 3d c2 e3 da
- [$DAEC]
- 79 87 d8 4f 21 03 dc 71 78 B9 d2 8a da c9
- getAddressItem:: [$DAFA]
- b9 30 17
- .noCheck [$DAFD]
- 21 00 dc 86 38 11 21 1c d3 be 30 0b 6f 26 00 29 c5 01 1d d3 09 c1 c9
- .oob [$DB14]
- bf c9
- getAddress2:: [$DB16]
- 21 02 dc c5 79 41 87 38 08 4f 78 86 cd fa da 18 05
- .oob2 [$DB27]
- 78 86 cd fd da
- .subEnd [$DB2C]
- c1 c9
- ;Used for this video: https://youtu.be/XTprmo04FKY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement