;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
;;  Copyright (C) 2021 Daniel José García Evans, Iván Alejandro Ontano Herrera,
;;  Sergio Izquierdo Pintado (PyroClastic Software)
;;
;;  Special Thanks to Ennio Fernando Velásquez Berlingeri (PyroBombastic)
;;
;;  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/>.

;;
;;ARTIFICIAL INTELLIGENCE SYSTEM
;;

.include "../globals.h.s"

sys_ia_update::
    ld hl, #sys_ia_update_entity
    ld b, #(e_cmps_alive | e_cmps_enemy)   ;;comprueba que este vivo y sea un enemigo
    call man_entity_for_all_matching

    ret 


sys_ia_update_entity:
    ld a, e_x(ix)   ;;si tocan la izquierda del mapa
    ld b, a         ;;guardo la posicion
    and a
    jr nz, not_turn_right  
        ld e_velX(ix), #1
    not_turn_right:

    ld a, b         ;;si tocan la derecha del mapa
    add e_w(ix)
    sub #80
    jr c, not_turn_left
        ld e_velX(ix), #-1
    not_turn_left:

    ld a, e_velX(ix)
    and a
    jp m, turn_sprite_left
        ;;turn_sprite_right
        ld a, e_cmps(ix)
        and #e_cmps_gravity
        jr nz, goblin_sprite_right
            ld a, e_y(ix)
            cp #168
            jr z, ant_sprite_right
                ld a, e_y(ix)
                ;;cp #56
                cp #100 ;;si esta muy alto es el camaleon
                ;;jr z, chameleon_sprite_right
                jp m, chameleon_sprite_right
                    ;;mosquito_sprite_right
                    ld a, e_cmps(ix)
                    and #e_cmps_animation
                    jr z, mosquito_first_right
                        ;;mosquito_second_right
                        ld e_sprite(ix), #<_enemies_char_03
                        ld e_sprite + 1(ix), #>_enemies_char_03
                        ret
                    mosquito_first_right:
                    ld e_sprite(ix), #<_enemies_char_02
                    ld e_sprite + 1(ix), #>_enemies_char_02
                    ret

                chameleon_sprite_right:
                    ld e_sprite(ix), #<_enemies_char_00
                    ld e_sprite + 1(ix), #>_enemies_char_00
                    ret

            ant_sprite_right:
                ld a, e_cmps(ix)
                and #e_cmps_animation
                jr z, ant_first_right
                    ;;ant_second_right
                    ld e_sprite(ix), #<_enemies_char_07
                    ld e_sprite + 1(ix), #>_enemies_char_07
                    ret
                ant_first_right:
                ld e_sprite(ix), #<_enemies_char_06
                ld e_sprite + 1(ix), #>_enemies_char_06
                ret

        goblin_sprite_right:
            ld e_sprite(ix), #<_main_char_14
            ld e_sprite + 1(ix), #>_main_char_14
            jr goblin_ia

    turn_sprite_left:
        ld a, e_cmps(ix)
        and #e_cmps_gravity
        jr nz, goblin_sprite_left
            ld a, e_y(ix)
            cp #168
            jr z, ant_sprite_left
                ld a, e_y(ix)
                ;;cp #56
                cp #100
                ;;jr z, chameleon_sprite_left
                jp m, chameleon_sprite_left
                    ;;mosquito_sprite_left
                    ld a, e_cmps(ix)
                    and #e_cmps_animation
                    jr z, mosquito_first_left
                        ;;mosquito_second_left
                        ld e_sprite(ix), #<_enemies_char_05
                        ld e_sprite + 1(ix), #>_enemies_char_05
                        ret
                    mosquito_first_left:
                    ld e_sprite(ix), #<_enemies_char_04
                    ld e_sprite + 1(ix), #>_enemies_char_04
                    ret

                chameleon_sprite_left:
                    ld e_sprite(ix), #<_enemies_char_01
                    ld e_sprite + 1(ix), #>_enemies_char_01
                    ret

            ant_sprite_left:
                ld a, e_cmps(ix)
                and #e_cmps_animation
                jr z, ant_first_left
                    ;;ant_second_left
                    ld e_sprite(ix), #<_enemies_char_09
                    ld e_sprite + 1(ix), #>_enemies_char_09
                    ret
                ant_first_left:
                ld e_sprite(ix), #<_enemies_char_08
                ld e_sprite + 1(ix), #>_enemies_char_08
                ret

        goblin_sprite_left:
            ld e_sprite(ix), #<_main_char_15
            ld e_sprite + 1(ix), #>_main_char_15
            ;;jr goblin_ia

    goblin_ia:

    ld a, e_clock(ix)
    dec a
    ret nz  ;;cuando el reloj esta a 0 puede saltar, este se reinicia y decrementa de verdad en el physicss
        ld a, e_velY(ix)
        and a
        ret nz  ;;si esta saltando no se hace nada    si el timer es lo suficientemente grande esto no es necesario
            ld e_velY(ix), #-11
    ret