;; INIT OF AI CONTROL.S
.module sys_ai_control

.include "cpctelera.h.s"
.include "./manager/entities.h.s"
.include "./manager/music.h.s"
.include "ui.h.s"
.include "./manager/rounds.h.s"

.globl _sprite_calabaza_woke
.globl cpct_getRandom_lcg_u8_asm

sys_ai_movement_food_enemy::
    ld a, e_food_ctr_destroy_counter(ix)
    dec a
    jr nz, skip_ctr_food
        ld e_food_ctr_destroy_counter(ix), #25
        ld a, e_food_ctr_destroy(ix)
        dec a
        ld e_food_ctr_destroy(ix), a

        cp #0
        jr nz, not_food_expired
            call set_death_anim_food
            ld e_death(iy), #0
        ret    
    skip_ctr_food:
    
    ld a, e_food_ctr_destroy_counter(ix)
    dec a
    ld e_food_ctr_destroy_counter(ix), a

    not_food_expired:
    ld a, e_death(ix)
    cp #0
    jr nz, keep_moving

    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0

    ld a, e_food_final_reder(ix)
    dec a
    ld e_food_final_reder(ix), a
    ret nz

    call sys_ui_food_increase
    call enemy_died
    call man_entity_set4destruction_simple
        push af
        ld l, #4 ; que sonido va a sonar
        ld h, #15 ; volumen 0-15
        ld e, #65 ; nota 0-143
        ld d, #0 ; speed (0 es original, 1 es la mas rapida)
        ld bc, #0xFFFF ;pitch
        ld a, #10 ;canal del medio
        push ix
        call cpct_akp_SFXPlay_asm
        pop ix
        pop af
    ret
    keep_moving:

    ld a, e_food_decision_enemy(ix)
    cp #0
    jr nz, right_left_side

    ld a, e_food_ctr_enemy(ix)
    dec a
    jr nz, no_check_food

    ld a, e_x(ix)
    cp #3
    jp m, movement_horizontal_up_food_enemy
    cp #4
    jp m, movement_horizontal_up_food_enemy;;IZQUIERDA
    
    add a, e_w(ix)
    cp #78    
    jr nc, movement_horizontal_down_food_enemy;;DERECHA
    cp #77    
    jr nc, movement_horizontal_down_food_enemy

    jp no_movement_food_enemy

    no_check_food:

    ld e_food_ctr_enemy(ix), a

    jp no_movement_food_enemy
    right_left_side:

    ld a, e_food_ctr_enemy(ix)
    dec a
    jr nz, no_check_food_2

    ld a, e_y(ix)
    cp #32
    jr c, movement_vertical_r_food_enemy;;ARRIBA
    
    add a, e_h(ix)
    cp #194
    jr nc, movement_vertical_l_food_enemy;;ABAJO
    jp no_movement_food_enemy

    no_check_food_2:

    ld e_food_ctr_enemy(ix), a

    jp no_movement_food_enemy;;FIN

    movement_horizontal_up_food_enemy:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #-3
    ld e_vy+1(ix), #0
    ld e_food_decision_enemy(ix), #1
    ld e_food_ctr_enemy(ix), #10
    jp no_movement_food_enemy

    movement_vertical_r_food_enemy:
    ld e_vx(ix), #1
    ld e_vx+1(ix), #0x80
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    ld e_food_decision_enemy(ix), #0
    ld e_food_ctr_enemy(ix), #10
    jp no_movement_food_enemy

    movement_vertical_l_food_enemy:
    ld e_vx(ix), #-2
    ld e_vx+1(ix), #0x80
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    ld e_food_decision_enemy(ix), #0
    ld e_food_ctr_enemy(ix), #10
    jp no_movement_food_enemy


    movement_horizontal_down_food_enemy:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #3
    ld e_vy+1(ix), #0
    ld e_food_decision_enemy(ix), #1
    ld e_food_ctr_enemy(ix), #10
    no_movement_food_enemy:
ret 



sys_ai_movement_spider:
    ld a, e_spider_ctr(ix)
    cp #110
    jp nc, spider_wait

    ld a, e_spider_move(ix)
    cp #2
    jr z, movement_spider_vertical
        ;;Movimiento en x
        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld a, e_spider_ctr(ix)
        cp #0
        jr nz, dec_spider_ctr
            ld e_spider_ctr(ix), #150 ;;TIempo entre espera y movimineto en Y
            ld e_spider_move(ix), #2
            ld e_spider_move_h(ix), #2
            ld e_vx(ix), #0
            ld e_vx+1(ix), #0
            jp end_of_spider
        dec_spider_ctr:
        dec a
        ld e_spider_ctr(ix), a
        call sys_ai_movement_spider_walls
        ld a, e_spider_move_h(ix)
        cp #1 ;;Si es 0
        jp c, movement_spider_right
        jr z, movement_spider_left
            ld l, #0
            call cpct_getRandom_lcg_u8_asm
            ld a,l
            and #1
            ld e_spider_move_h(ix), a
            jp end_of_spider
        movement_spider_right:
            ld e_vx(ix), #0
            ld e_vx+1(ix), #0x50
            ;;call sys_ai_movement_spider_walls
            ld e_vy(ix), #0

            jp end_of_spider

        movement_spider_left:
            ld e_vx(ix), #-1
            ld e_vx+1(ix), #0xB0
            ;;call sys_ai_movement_spider_walls
            ld e_vy(ix), #0
            jp end_of_spider

    movement_spider_vertical:
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0
        ld a, e_spider_ctr(ix)
        cp #0
        jr nz, dec_spider_ctr_2
            ld e_spider_ctr(ix), #200 ;;TIempo entre espera y movimineto en X
            ld e_spider_move(ix), #1
            ld e_spider_move_h(ix), #2
            ld e_vy(ix), #0
            jp end_of_spider
        dec_spider_ctr_2:
        dec a
        ld e_spider_ctr(ix), a

        ld a, e_spider_move_h(ix)
        cp #1 ;;Si es 0
        jr c, movement_spider_up
        jr z, movement_spider_down
            ld l, #0
            call cpct_getRandom_lcg_u8_asm
            ld a,l
            and #1
            ld e_spider_move_h(ix), a
            jp end_of_spider
        movement_spider_up:
            ld e_vy(ix), #-3
            ld e_vx(ix), #0
            ;;call sys_ai_movement_spider_walls
            ret

        movement_spider_down:
            ld e_vy(ix), #3
            ld e_vx(ix), #0
            ;;call sys_ai_movement_spider_walls
            ret
    spider_wait:
    dec a
    ld e_spider_ctr(ix), a
    end_of_spider:
ret

sys_ai_movement_spider_walls:
    ld a, e_x(ix)
    cp #3
    jp m, movement_horizontal_r_spider

    add a, e_w(ix)
    cp #73    
    jr nc, movement_horizontal_l_spider
    jp no_movement_spider

    movement_horizontal_r_spider:
    ld e_spider_move_h(ix), #0
    jp no_movement_spider

    movement_vertical_down_spider:
    ld e_spider_move_h(ix), #1
    jp no_movement_spider


    movement_vertical_up_spider:
    ld e_spider_move_h(ix), #0
    jp no_movement_spider


    movement_horizontal_l_spider:
    ld e_spider_move_h(ix), #1
    no_movement_spider:
ret

sys_ai_movement_witch:
    ld a, e_witch_ctr(ix)
    cp #100
    jp nc, witch_wait

    ld a, e_witch_move(ix)
    cp #2
    jr z, movement_witch_vertical
        ;;Movimiento en x
        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld a, e_witch_ctr(ix)
        cp #0
        jr nz, dec_witch_ctr
            ld e_witch_ctr(ix), #150 ;;TIempo entre espera y movimineto en Y
            ld e_witch_move(ix), #2
            ld e_witch_move_h(ix), #2
            ld e_vx(ix), #0
            jp end_of_witch
        dec_witch_ctr:
        dec a
        ld e_witch_ctr(ix), a

        call sys_ai_movement_witch_walls
        ld a, e_witch_move_h(ix)
        cp #1 ;;Si es 0
        jp c, movement_witch_right
        jr z, movement_witch_left
            ld l, #0
            call cpct_getRandom_lcg_u8_asm
            ld a,l
            and #1
            ld e_witch_move_h(ix), a
            jp end_of_witch
        movement_witch_right:
            ld e_vx(ix), #1
            ld e_vx+1(ix), #0
            ;;call sys_ai_movement_witch_walls
            ld e_vy(ix), #0
            ld e_vy+1(ix), #0

            jp end_of_witch

        movement_witch_left:
            ld e_vx(ix), #-1
            ld e_vx+1(ix), #0
            ;;call sys_ai_movement_witch_walls
            ld e_vy(ix), #0
            ld e_vy+1(ix), #0
            jp end_of_witch

    movement_witch_vertical:
        ld e_vx(ix), #0
        ld a, e_witch_ctr(ix)
        cp #0
        jr nz, dec_witch_ctr_2
            ld e_witch_ctr(ix), #200 ;;TIempo entre espera y movimineto en X
            ld e_witch_move(ix), #1
            ld e_witch_move_h(ix), #2
            ld e_vy(ix), #0
            jp end_of_witch
        dec_witch_ctr_2:
        dec a
        ld e_witch_ctr(ix), a

        ld a, e_witch_move_h(ix)
        cp #1 ;;Si es 0
        jr c, movement_witch_up
        jr z, movement_witch_down
            ld l, #0
            call cpct_getRandom_lcg_u8_asm
            ld a,l
            and #1
            ld e_witch_move_h(ix), a
            jp end_of_witch
        movement_witch_up:
            ld e_vy(ix), #-1
            ld e_vx(ix), #0
            call sys_ai_movement_witch_walls
            ret

        movement_witch_down:
            ld e_vy(ix), #1
            ld e_vx(ix), #0
            call sys_ai_movement_witch_walls
            ret
    witch_wait:
    dec a
    ld e_witch_ctr(ix), a
    end_of_witch:
ret

sys_ai_movement_witch_walls:
    ld a, e_x(ix)
    cp #4
    jp m, movement_horizontal_r_witch

    add a, e_w(ix)
    cp #73    
    jr nc, movement_horizontal_l_witch

    ld a, e_y(ix)
    cp #38
    jr c, movement_vertical_down_witch
    
    add a, e_h(ix)
    cp #185
    jr nc, movement_vertical_up_witch

    jp no_movement_witch

    movement_horizontal_r_witch:
    ld e_witch_move_h(ix), #0
    jp no_movement_witch

    movement_vertical_down_witch:
    ld e_witch_move_h(ix), #1
    jp no_movement_witch


    movement_vertical_up_witch:
    ld e_witch_move_h(ix), #0
    jp no_movement_witch


    movement_horizontal_l_witch:
    ld e_witch_move_h(ix), #1
    
    no_movement_witch:
ret

sys_ai_movement_calabaza:
    ld a, e_calabaza_ctr(ix)
    cp #50
    jr nc, not_follow_calabaza
        call sys_ia_movement_calabaza

        ld a, e_vx(ix)
        cp #0
        jr nz, skip_calabaza
        ld a, e_vy(ix)
        cp #0
        jr nz, skip_calabaza

        ld a, e_calabaza_ctr(ix)
        dec a
        ld e_calabaza_ctr(ix), a

        jr nz, skip_death_calabaza

        ld e_lifes(ix), #0
        call set_death_anim

        skip_death_calabaza:
    ret
    not_follow_calabaza:
        dec a
        ld e_calabaza_ctr(ix), a
        cp #51
        call z, calculate_calabaza_pos
        cp #100
        jr z, vel_0_calabaza
        
        ret
        vel_0_calabaza:
            ld e_vx(ix), #0
            ld e_vx+1(ix), #0
            ld e_vy(ix), #0
            ld e_vy+1(ix), #0
    skip_calabaza:
ret

calculate_calabaza_pos:
    call man_entity_getFirstEntity_IY
    ld a, e_x(iy)
    ld e_calabaza_x(ix), a
    ld a, e_y(iy)
    ld e_calabaza_y(ix), a
    ;;AQUI TOCA EL CAMBIO DE SPRITE JIJI
    ld hl, #_sprite_calabaza_woke
    ld e_sprite(ix), l
    ld e_sprite+1(ix), h
ret

sys_ia_movement_calabaza:
    move_to_position_calabaza:
    ld a, e_calabaza_x(ix)
    cp e_x(ix)

    jp m, left_ia_calabaza
    jr z, arrives_x_calabaza

    call ai_movement_right_calabaza
    jr next_pos_calabaza

    left_ia_calabaza:
    call ai_movement_left_calabaza
    jr next_pos_calabaza

    arrives_x_calabaza:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0

    next_pos_calabaza:

    ;;POSICION EN Y

    ld a, e_calabaza_y(ix)
    cp e_y(ix)

    jp m, up_ia_calabaza
    jr z, arrives_y_calabaza

    ld a, e_calabaza_y(ix)
    inc a
    cp e_y(ix)

    jp m, up_ia_calabaza
    jr z, arrives_y_calabaza

    ld a, e_calabaza_y(ix)
    dec a
    cp e_y(ix)

    jp m, up_ia_calabaza
    jr z, arrives_y_calabaza

    call ai_movement_down_calabaza
    jr next_pos_Y_calabaza

    up_ia_calabaza:
    call ai_movement_up_calabaza

    jr next_pos_Y_calabaza

    arrives_y_calabaza:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    ret
    next_pos_Y_calabaza:
ret

ai_movement_left_calabaza:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0
ret

ai_movement_right_calabaza:
    ld e_vx(ix), #1
    ld e_vx+1(ix), #0
ret

ai_movement_up_calabaza:
    ld e_vy(ix), #-2
    ld e_vy+1(ix), #0
ret

ai_movement_down_calabaza:
    ld e_vy(ix), #2
    ld e_vy+1(ix), #0
ret

sys_ai_movement_ovni:
    ld a, e_x(ix)
    cp #4
    jp m, movement_horizontal_r

    add e_w(ix)
    cp #75 
    jr nc, movement_horizontal_l 

    ld a, e_y(ix)
    cp #35
    jr c, movement_vertical_down
    
    add e_h(ix)
    cp #190
    jr nc, movement_vertical_up

    jp no_movement_ovni

    movement_horizontal_r:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x80
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    jp no_movement_ovni

    movement_vertical_down:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #1
    ld e_vy+1(ix), #0xC0
    jp no_movement_ovni


    movement_vertical_up:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #-2
    ld e_vy+1(ix), #0x40
    jp no_movement_ovni


    movement_horizontal_l:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0x80
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    no_movement_ovni:
ret

sys_ai_movement_bat:
    ld a, e_x(ix)
    cp #7
    jp m, movement_horizontal_r_bat

    add e_w(ix)
    cp #72    
    jr nc, movement_horizontal_l_bat

    jp check_vertical_bat

    movement_horizontal_r_bat:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x60
    jp check_vertical_bat

    movement_horizontal_l_bat:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0xA0

    check_vertical_bat:

    ld a, e_y(ix)
    cp #35
    jr c, movement_vertical_down_bat

    add e_h(ix)
    cp #185
    jr nc, movement_vertical_up_bat

    jp no_movement_bat

    movement_vertical_down_bat:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0x80
    jp no_movement_bat


    movement_vertical_up_bat:
    ld e_vy(ix), #-1
    ld e_vy+1(ix), #0x80

    no_movement_bat:
ret


sys_ai_control_update::
    call man_entity_getFirstEntity_IY
    ld hl, #sys_ai_control_update_forone
    call man_entity_forall
ret

sys_ai_stand_by:
    ld a, e_foodctr(ix)
    dec a
    jr nz, not_damaged_food
        push af
        ld l, #5 ; que sonido va a sonar
        ld h, #15 ; volumen 0-15
        ld e, #35 ; nota 0-143
        ld d, #0 ; speed (0 es original, 1 es la mas rapida)
        ld bc, #0xFFFF ;pitch
        ld a, #10 ;canal del medio
        push ix
        call cpct_akp_SFXPlay_asm
        cpctm_setBorder_asm HW_RED
        pop ix
        pop af
        call sys_ui_food_damaged
        ld a, #200 ;;Tiempo entre hit y hit 50 = 1 Segundo
        ld e_foodctr(ix), a
        ret
    not_damaged_food:
        ld e_foodctr(ix), a
ret 


sys_ia_movement:
    ;;if (a<b)
    ;;a = e_x(ix)+e_w(ix) < e_x(iy)
    ;;a = e_x(ix)+e_w(ix) - e_x(iy) < 0

    ld a, e_x(iy)
    cp e_x(ix)

    jp c, left_ia
    jr z, arrives_x

    call ai_movement_right_ghost
    jr next_pos

    left_ia:
    call ai_movement_left_ghost
    jr next_pos

    arrives_x:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0

    next_pos:

    ld a, e_y(iy)
    cp e_y(ix)

    jp m, up_ia
    jr z, arrives_y

    call ai_movement_down_ghost
    jr next_pos_Y

    up_ia:
    call ai_movement_up_ghost
    jr next_pos_Y

    arrives_y:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    next_pos_Y:
ret

enemy_arrived: .db 0x00

ai_movement_left_ghost:
    ld a, e_enemlevel(ix)
    cp #1
    jr z, enem0_1_left
    cp #2
    jr z, enem0_2_left

    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0xE5
    jr after_enem_left

    enem0_1_left:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0xE2
    jr after_enem_left

    enem0_2_left:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0xE0

    after_enem_left:
ret

ai_movement_right_ghost:
    ld a, e_enemlevel(ix)
    cp #1
    jr z, enem0_1_right
    cp #2
    jr z, enem0_2_right

    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x1B
    jr after_enem_right

    enem0_1_right:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x1E
    jr after_enem_right

    enem0_2_right:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x20

    after_enem_right:
ret

ai_movement_up_ghost:
    ld a, e_enemlevel(ix)
    cp #1
    jr z, enem0_1_up
    cp #2
    jr z, enem0_2_up

    ld e_vy(ix), #-1
    ld e_vy+1(ix), #0xA0
    jr after_enem_up

    enem0_1_up:
    ld e_vy(ix), #-1
    ld e_vy+1(ix), #0x90
    jr after_enem_up

    enem0_2_up:
    ld e_vy(ix), #-1
    ld e_vy+1(ix), #0x80

    after_enem_up:
ret

ai_movement_down_ghost:
    ld a, e_enemlevel(ix)
    cp #1
    jr z, enem0_1_down
    cp #2
    jr z, enem0_2_down

    ld e_vy(ix), #0
    ld e_vy+1(ix), #0x50
    jr after_enem_down

    enem0_1_down:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0x60
    jr after_enem_down

    enem0_2_down:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0x70

    after_enem_down:
ret 


sys_ai_movement_food:
    ;;chequear que este (por ejemplo, ya lo cambiaremos)
    ;; x entre 35 y 45
    ;; y entre 90 y 10

    ;jr (lo que sea), standby ;;Si el chequeo dio true, se vuelve standby
    ld a, e_x(ix)
    cp #31
    jp m, move_to_food
    cp #45
    jr nc, move_to_food

    ld a, e_y(ix)
    cp #78
    jp m, move_to_food
    cp #128
    jr nc, move_to_food

    jp standby
 
    move_to_food:

    ld a, #36   ;; X Objetivo
    ld (enemy_arrived), a
    cp e_x(ix)  ;; Esto controla al zombi
    jp m, left_ia_food  ;; Esto significa que se mueve hacia la izquierda
    jr z, arrives_x_food

    ld e_vx(ix), #0
    ld e_vx+1(ix), #0x20
    jr next_pos_food

    left_ia_food:
    ld e_vx(ix), #-1
    ld e_vx+1(ix), #0xE0
    jr next_pos_food

    arrives_x_food:
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld a, #1
    ld (enemy_arrived), a

    next_pos_food:

    ld a, #100  ;; Cargas la Y
    cp e_y(ix)

    jp m, up_ia_food
    jr z, arrives_y_food

    ;; Cuando el enemigo va hacia abajo
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0x40
    jr next_pos_Y_food

    up_ia_food:
    ld e_vy(ix), #-1
    ld e_vy+1(ix), #0xC0
    jr next_pos_Y_food

    arrives_y_food:
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    ld a, (enemy_arrived)
    inc a
    ld (enemy_arrived), a
    ;;ld e_ai_st(ix), #e_ai_st_stand_by
    next_pos_Y_food:

    

    ld a, (enemy_arrived)
    cp #2
    jr nz, _skip
    standby:
    ld e_ai_st(ix), #e_ai_st_stand_by
    ld e_vx(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy(ix), #0
    ld e_vy+1(ix), #0
    _skip:
ret


sys_ai_move_to:
    call sys_ia_movement
ret

sys_ai_move_to_food:
    call sys_ai_movement_food
ret


sys_ai_control_update_forone:
    ld a, e_enemy_ctr_retroceso(ix)
    cp #1
    jr nz, no_AI_ent

    ld a, e_ai_st(ix)
    cp #e_ai_st_noAI
    jr z, no_AI_ent

    cp #e_ai_st_move_to
    jr nz, next_movement
        ld a, e_type_enemy_ai(ix)
        cp #e_type_enemy_ovni
        jr nz, not_ovni
            call sys_ai_movement_ovni
            jp no_AI_ent
        
        not_ovni:
        cp #e_type_enemy_bat
        jr nz, not_bat
            call sys_ai_movement_bat
            jp no_AI_ent
        not_bat:
        cp #e_type_enemy_calabaza
        jr nz, not_calabaza
            call sys_ai_movement_calabaza
            jp no_AI_ent
        not_calabaza:
        cp #e_type_enemy_spider
        jr nz, not_spider
            call sys_ai_movement_spider
            jp no_AI_ent
        not_spider:
        cp #e_type_enemy_witch
        jr nz, not_witch
            call sys_ai_movement_witch
            jp no_AI_ent
        not_witch:
        cp #e_type_enemy_food
        jr nz, not_food
            call sys_ai_movement_food_enemy
            jp no_AI_ent
        not_food:
        call sys_ai_move_to
    ret
    next_movement:
    cp #e_ai_st_move_to_food
    jr nz, next_movement_food
    call sys_ai_move_to_food
    ret
    next_movement_food:
    cp #e_ai_st_stand_by
    jr nz, no_AI_ent
    call sys_ai_stand_by
    no_AI_ent:
ret

