;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program 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 Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "patrol.h.s"
.include "cpctelera.h.s"

;;Global includes
.include "global/global.h.s"

;;Managers
.include "man/entity.h.s"

;;Macros
.include "macros/math.h.s"

;;====================================================================
;; PATROLS
;;====================================================================
patrol_steps = 11
patrol_01::
    .db 30
    .db 65
    .db 00
    .db 50
    .db 10
    .db 00
    .db 60
    .db 20
    .db 00
    .db 40
    .db 10
    .db patrol_invalid_move_x
    .dw patrol_01

patrol_02::
    .db 30, 128
    .db 65, 108
    .db 10, 98
    .db 50, 78
    .db 10, 68
    .db 10, 38
    .db 60, 108
    .db 20, 98
    .db 30, 108
    .db 40, 118
    .db 10, 128
    .db patrol_invalid_move_x
    .dw patrol_02
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - 
;; Objetive: Set a random patrol for the walker AI for each game
;; Modifies: AF, BC, DE, HL, IX
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_patrol_init_walker::

    ld b, #patrol_steps
    ld hl, #patrol_01
    set_patrol_loop_walker:
    push bc
    push hl

    call cpct_getRandom_xsp40_u8_asm
    and #0x3C
    add #5
    pop hl
    ld (hl), a
    inc hl

    pop bc
    dec b
    jr nz, set_patrol_loop_walker
    
    push ix ;; IX contains the enemy memory direction, we save it

    ld ix, #patrol_01
    ld 2(ix), #patrol_jump_move
    ld 4(ix), #38
    ld 5(ix), #patrol_jump_move
    ld 8(ix), #patrol_jump_move

    pop ix ;; Rescue the enemy memory direction

    ld hl, #patrol_01
    ld e_ai_patrol_step_h(iy),h
    ld e_ai_patrol_step_l(iy),l

    ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: Should contain the ghost memory direction
;; Objetive: Set a random patrol for the AI for each game
;; Modifies: AF, BC, DE, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_patrol_init_ghost::

    ld b, #patrol_steps
    ld hl, #patrol_02
    set_patrol_loop_ghost_random:
    push bc
    push hl

    call cpct_getRandom_xsp40_u8_asm
    and #0x46
    add #5
    ;;Save the X coordinate
    pop hl
    ld (hl), a
    inc hl

    push hl
    call cpct_getRandom_xsp40_u8_asm
    and #0x64 ;;TODO: Si el fantasma vuela muy alto, cambiar este rango
    add #74

    ;;Save the Y coordinate
    pop hl
    ld (hl), a
    inc hl

    pop bc
    dec b
    jr nz, set_patrol_loop_ghost_random

    push ix ;; IX contains the enemy memory direction, we save it

    ld ix, #patrol_02
    ld (ix), #50
    ld 5(ix), #patrol_ground_y
    ld 11(ix), #patrol_ground_y
    ld 17(ix), #patrol_ground_y
    ld 21(ix), #patrol_ground_y

    pop ix ;; Rescue the enemy memory direction

    ld hl, #patrol_02
    ld e_ai_patrol_step_h(iy),h
    ld e_ai_patrol_step_l(iy),l

    ret