;---------------------------------------------------------------;
;                                                               ;
; Snail Maze                                                    ;
; Maze routines                                                 ;
;                                                               ;
;---------------------------------------------------------------;

;------------------------------------------------
; getTile - get the tile at a specified position
;   input:  E = x
;           L = y
;   output: A = tile #
;           HL => tile
;------------------------------------------------
getTile:
        ld      h,MAZE_WIDTH
        mlt     hl                              ; HL = offset to row
        ld      bc,0
        ld      c,e                             ; BC = x
        add     hl,bc                           ; HL = offset to tile
        ld      bc,maze
        add     hl,bc                           ; HL => tile
        ld      a,(hl)
        ret

;------------------------------------------------
; loadMaze - decompress a maze
;   input:  HL => header + compressed data
;   output: none
;------------------------------------------------
loadMaze:
        ld      de,sx
        ld      bc,5
        ldir                                    ; load header vars
        ld      c,MAZE_HEIGHT                   ; C = # rows to load
        ld      ix,maze                         ; IX => where to load to
lmOuter:
        ld      b,MAZE_WIDTH/4+1                ; B = # bytes per row
lmInner:
        ; data is compressed to 4 tiles per byte (2 bits per tile)
        ld a,(hl) \ rlca \ rlca \ and $03
        ld (ix),a \ inc ix                      ; first tile of byte
        ld a,(hl) \ rrca \ rrca \ rrca \ rrca \ and $03
        ld (ix),a \ inc ix                      ; second tile of byte
        ld a,(hl) \ rrca \ rrca \ and $03
        ld (ix),a \ inc ix                      ; third
        ld a,(hl) \ and $03
        ld (ix),a \ inc ix                      ; fourth
        inc     hl
        djnz    lmInner
        dec ix \ dec ix                         ; on the last byte we only want 2 tiles, so decrement the destination pointer to overwrite redundant data
        dec     c
        jr      nz,lmOuter
        ; direction, time & reset moveCount
        ld      a,3
        ld      (dir),a
        ld      hl,timeMain
        ld      a,(timeAdd)
        add     a,(hl)
        ld      (hl),a
        ld      a,-1
        ld      (moveCount),a
        ret

;------------------------------------------------
; checkGoal - check if snail has reached the goal
;   input:  none
;   output: CA = goal reached
;------------------------------------------------
checkGoal:
        ld      a,(moveCount)
        or      a                               ; is snail still moving?
        ret     nz                              ; if so, can't finish (and carry is clear)
        ld      hl,gx
        ld      a,(sx)
        cp      (hl)                            ; check x
        jr      nz,goalNotReached
        inc     hl                              ; HL => gy
        ld      a,(sy)
        cp      (hl)
        jr      nz,goalNotReached
        scf                                     ; goal reached, set carry
        ret
goalNotReached:
        or      a
        ret

.end