Advertisement
ZodiacMentor

BATTLESNAKE player.s

Jul 22nd, 2022
3,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; whenever calling player.s functions, put the player struct address for the player we are manipulating in the
  2. ; ZP_ADDRESS
  3.  
  4. BODY_MASK_INV = %11110011
  5. BODY_HEAD_GC = %0000
  6. BODY_STRT_GC = %0100
  7. BODY_BEND_GC = %1000
  8. BODY_TAIL_GC = %1100
  9. DIR_MASK_INV = %11111100
  10. DIR_RIGHT_GC = %00
  11. DIR_DOWN_GC = %01
  12. DIR_LEFT_GC = %10
  13. DIR_UP_GC = %11
  14.  
  15. change_worm_tile .macro andmask, ormask
  16.     ldy #0
  17.     lda (ZP_ADDRESS),Y
  18.     and #\andmask
  19.     ora #\ormask
  20.     sta (ZP_ADDRESS),Y
  21.     sta gpuIndex
  22.     jsr gpuSetIndex
  23.     lda gpuIndex
  24.     .endm
  25.  
  26.     .local playfield
  27.     .local playfield_offset
  28.     .local playerAddress
  29.     .global tmpDirection
  30.  
  31. playfield:
  32.     .repeat 768
  33.     .byte 0
  34.     .endrepeat
  35.  
  36. playfield_offset:
  37.     .word $0000
  38.  
  39. playfieldAddress:
  40.     .word $0000
  41.  
  42. playerAddress:
  43.     .word $0000
  44.  
  45. tmpDirection:
  46.     .byte $00
  47.  
  48. ; write the players head to playfield and also draw to screen
  49. writeHead:
  50.     jsr savePlayerAddress
  51.  
  52.     ldy #$0
  53.     lda (ZP_ADDRESS), Y     ; get id
  54.     ; form the correct sprite index
  55.     ; in the form of 00gpbbdd where
  56.     ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
  57.     ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
  58.     ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
  59.     ; in this case, bb is 0 and we'll assume direction is left (0b10)
  60.    ; and we just got the id, OR 0b10 with ID and save to stack
  61.    ora tmpDirection
  62.    sta gpuIndex
  63.  
  64.    ldy #1
  65.    jsr calculateGPUXYandPlayfieldOffset
  66.    jsr calculateAndSavePlayfieldAddress
  67.  
  68.    lda gpuIndex
  69.    ldy #0
  70.    sta (ZP_ADDRESS),Y
  71.    
  72.    jsr gpuSetIndex
  73.  
  74.    jsr loadPlayerAddress
  75.  
  76.    rts
  77.  
  78. calculateGPUXYandPlayfieldOffset:
  79.    jsr loadPlayerAddress
  80.    ; send set index command with proper arguments to GPU
  81.    lda (ZP_ADDRESS), Y     ; get x
  82.    sta gpuX
  83.    sta playfield_offset+1  ; save x to playfield_offset
  84.    iny
  85.    lda (ZP_ADDRESS), Y     ; get y
  86.    sta gpuY
  87.  
  88.    ; y * 32 (or 2^5)
  89.    ldy #32
  90.    jsr ROM_mul8x8
  91.  
  92.    clc
  93.    adc playfield_offset+1
  94.    sta playfield_offset+1
  95.    tya
  96.    sta playfield_offset        ; save 32 (width) times Y added to X in playfield offset
  97.    rts
  98.  
  99. writeTail:
  100.    jsr savePlayerAddress
  101.  
  102.    ldy #$0
  103.    lda (ZP_ADDRESS), Y     ; get id
  104.    ; form the correct sprite index
  105.    ; in the form of 00gpbbdd where
  106.    ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
  107.    ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
  108.    ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
  109.    ; in this case, bb is 0 and we'll assume direction is left (0b10)
  110.     ; and we just got the id, OR 0b10 with ID and save to stack
  111.     ora #%1100
  112.     ora tmpDirection
  113.     sta gpuIndex
  114.  
  115.     ldy #3
  116.     jsr calculateGPUXYandPlayfieldOffset
  117.     jsr calculateAndSavePlayfieldAddress
  118.  
  119.     lda gpuIndex
  120.     ldy #0
  121.     sta (ZP_ADDRESS),Y
  122.    
  123.     jsr gpuSetIndex
  124.  
  125.     jsr loadPlayerAddress
  126.  
  127.     rts
  128.  
  129. lastPlayerDirection:
  130.     .byte 0
  131. tmpPlayerDirection:
  132.     .byte 0
  133. newPlayerDirection:
  134.     .byte 0
  135.  
  136. moveHead:
  137.     jsr savePlayerAddress
  138.     ; save last playerdirection
  139.     ldy #7
  140.     lda (ZP_ADDRESS),Y
  141.     sta lastPlayerDirection
  142.     ; get player data at head location
  143.     ldy #1 ; head location
  144.     jsr calculateGPUXYandPlayfieldOffset
  145.     jsr calculateAndSavePlayfieldAddress
  146.  
  147.     ldy #0
  148.     lda (ZP_ADDRESS),Y
  149.     and #%11
  150.     sta tmpPlayerDirection
  151.  
  152.     cmp lastPlayerDirection
  153.     beq .straight ; if direction not changed, just set a straight
  154.     ; if direction changed, check whether cw or ccw bend
  155.     ; if (new dir - old dir) % 3 == 1, cw.
  156.     ; if cw, add 1 from new dir
  157.     sec
  158.     sbc lastPlayerDirection
  159.     and #%11
  160.     cmp #1
  161.     bne .setBend ; if !1, ccw, no modification
  162.     ; cw, add one from new direction
  163.     lda tmpPlayerDirection
  164.     clc
  165.     adc #1
  166.     and #%11
  167.     sta newPlayerDirection
  168.     ldy #0
  169.     lda (ZP_ADDRESS),Y
  170.     and #DIR_MASK_INV
  171.     ora newPlayerDirection
  172.     sta (ZP_ADDRESS),Y
  173.     .setBend:
  174.     ; set bend
  175.     change_worm_tile BODY_MASK_INV, BODY_BEND_GC
  176.     jmp .saveLastDirection
  177.  
  178.     .straight:
  179.     ; change spriteindex to be a straight part
  180.     change_worm_tile BODY_MASK_INV, BODY_STRT_GC
  181.  
  182.     .saveLastDirection:
  183.     ; get direction at start and save it as last direction
  184.     jsr loadPlayerAddress
  185.     lda tmpPlayerDirection
  186.     and #%11
  187.     ldy #7
  188.     sta (ZP_ADDRESS),Y
  189.    
  190.     ; check which way the direction at start is and act accordingly
  191.     cmp #%00
  192.     beq moveHeadRight
  193.     cmp #%01   ; 1 down
  194.     beq moveHeadDown
  195.     cmp #%10   ; 2 left
  196.     beq moveHeadLeft
  197.    
  198.     ; move head up
  199.     jsr loadPlayerAddress
  200.     ldy #2
  201.     lda (ZP_ADDRESS),Y
  202.     sec
  203.     sbc #1
  204.     cmp #-1
  205.     bne .continue
  206.     lda #23
  207.   .continue:
  208.     sta (ZP_ADDRESS),Y
  209.     lda #%11
  210.     sta tmpDirection
  211.  
  212.     jsr writeHead
  213.  
  214.     rts
  215.  
  216.   moveHeadRight:
  217.     jsr loadPlayerAddress
  218.     ldy #1
  219.     lda (ZP_ADDRESS),Y
  220.     clc
  221.     adc #1
  222.     and #%11111     ; scrap carry, ie. % 32
  223.     sta (ZP_ADDRESS),Y
  224.     lda #%00
  225.     sta tmpDirection
  226.  
  227.     jsr writeHead
  228.  
  229.     rts
  230.  
  231.   moveHeadDown:
  232.     jsr loadPlayerAddress
  233.     ldy #2
  234.     lda (ZP_ADDRESS),Y
  235.     clc
  236.     adc #1
  237.     cmp #24
  238.     bne .continue
  239.     lda #0
  240.   .continue:
  241.     sta (ZP_ADDRESS),Y
  242.     lda #%01
  243.     sta tmpDirection
  244.  
  245.     jsr writeHead
  246.  
  247.     rts
  248.  
  249.   moveHeadLeft:
  250.     jsr loadPlayerAddress
  251.  
  252.     ldy #1
  253.     lda (ZP_ADDRESS),Y ; read X
  254.     sec
  255.     sbc #1
  256.     and #%11111     ; scrap carry, ie. % 32
  257.     sta (ZP_ADDRESS),Y  ; save new X
  258.  
  259.     lda #%10
  260.     sta tmpDirection
  261.  
  262.     jsr writeHead
  263.  
  264.     rts
  265.  
  266. moveTail:
  267.     jsr savePlayerAddress
  268.     ; save last player tail direction
  269.     ldy #7
  270.     lda (ZP_ADDRESS),Y
  271.     sta lastPlayerDirection
  272.  
  273.     ; get player data at tail location
  274.     ldy #3 ; tail location
  275.     jsr calculateGPUXYandPlayfieldOffset
  276.     jsr calculateAndSavePlayfieldAddress
  277.  
  278.     ldy #0
  279.     lda (ZP_ADDRESS),Y
  280.     and #%11
  281.     sta tmpPlayerDirection
  282.  
  283.     ; erase old tail
  284.     lda #$ff
  285.     sta (ZP_ADDRESS), Y
  286.     sta gpuIndex
  287.  
  288.     jsr loadPlayerAddress
  289.  
  290.     ldy #3
  291.     lda (ZP_ADDRESS), Y     ; get tail_x
  292.     sta gpuX
  293.     iny
  294.     lda (ZP_ADDRESS), Y     ; get tail_y
  295.     sta gpuY
  296.    
  297.     jsr gpuSetIndex
  298.  
  299.     lda tmpPlayerDirection
  300.     cmp #0   ; And the direction
  301.     beq moveTailRight
  302.     cmp #01
  303.     beq moveTailDown
  304.     cmp #02
  305.     beq moveTailLeft
  306.    
  307.     ; move tail up
  308.     ldy #4
  309.     lda (ZP_ADDRESS), Y     ; get tail_y
  310.     sec
  311.     sbc #1
  312.     cmp #-1
  313.     bne .continue
  314.     lda #23
  315.     .continue:
  316.     sta (ZP_ADDRESS), Y     ; save new head_x
  317.  
  318.     jmp changeToTail
  319.  
  320.   moveTailRight:
  321.     ldy #3
  322.     lda (ZP_ADDRESS), Y     ; get tail_x
  323.     clc
  324.     adc #1
  325.     and #%11111     ; scrap carry, ie. % 32
  326.     sta (ZP_ADDRESS), Y     ; save new head_x
  327.  
  328.     jmp changeToTail
  329.  
  330.   moveTailDown:
  331.     ldy #4
  332.     lda (ZP_ADDRESS), Y     ; get tail_y
  333.     clc
  334.     adc #1
  335.     cmp #24
  336.     bne .continue
  337.     lda #0
  338.     .continue:
  339.     sta (ZP_ADDRESS), Y     ; save new head_x
  340.  
  341.     jmp changeToTail
  342.  
  343.   moveTailLeft:
  344.     ldy #3
  345.     lda (ZP_ADDRESS), Y     ; get tail_x
  346.     sec
  347.     sbc #1
  348.     and #%11111     ; scrap carry, ie. % 32
  349.     sta (ZP_ADDRESS), Y     ; save new head_x
  350.  
  351. changeToTail:
  352.     ldy #3 ; tail location
  353.     jsr calculateGPUXYandPlayfieldOffset
  354.     jsr calculateAndSavePlayfieldAddress
  355.  
  356.     change_worm_tile BODY_MASK_INV, BODY_TAIL_GC
  357.    
  358.     jsr loadPlayerAddress
  359.     rts
  360.  
  361. loadPlayerAddress:
  362.     lda playerAddress
  363.     sta ZP_ADDRESS
  364.     lda playerAddress+1
  365.     sta ZP_ADDRESS+1
  366.     rts  
  367.  
  368. savePlayerAddress:
  369.     lda ZP_ADDRESS
  370.     sta playerAddress
  371.     lda ZP_ADDRESS+1
  372.     sta playerAddress+1
  373.     rts
  374.  
  375. calculateAndSavePlayfieldAddress:
  376.     clc                         ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
  377.     lda #<playfield
  378.     adc playfield_offset+1
  379.     sta ZP_ADDRESS
  380.     sta playfieldAddress
  381.     lda #>playfield
  382.     adc playfield_offset
  383.     sta ZP_ADDRESS+1    
  384.     sta playfieldAddress+1
  385.     rts
  386.  
  387. loadPlayfieldAddress:
  388.     lda playfieldAddress
  389.     sta ZP_ADDRESS
  390.     lda playfieldAddress+1
  391.     sta ZP_ADDRESS+1
  392.     rts
  393.  
  394. gpuX:
  395.     .byte $00
  396. gpuY:
  397.     .byte $00
  398. gpuIndex:
  399.     .byte $00
  400.  
  401. gpuSetIndex:
  402.     lda #CF_CMD_SET_INDEX
  403.     sta GPU_CMDQ_ADDRESS
  404.     lda gpuX
  405.     sta GPU_CMDQ_ADDRESS
  406.     lda gpuY
  407.     sta GPU_CMDQ_ADDRESS
  408.     lda gpuIndex
  409.     sta GPU_CMDQ_ADDRESS
  410.  
  411.     rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement