;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of Viking Hangover
;;  Copyright (C) 2021 Ahmed Begga / Érika Moreno / Adriana Morejón (@ComputosGame)
;;
;;  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/>.
;;-------------------------------------------------------------------------------

.include "generator.h.s"

.include "man/entity.h.s"
.include "man/game.h.s"

.include "ai.h.s"
.include "colisions.h.s"
.include "render.h.s"
.include "animations.h.s"



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  VARIABLES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

minwait2genBottle:
   .db #ahmed


minwait2genShot:
    .db #init_max2wait

wait2gen:
    .db #init_max2wait

max_nshots:
    .db #min_nshots

n_shots:
    .db #min_nshots

speedUp:
   .db #SpeedUp1


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  TEMPLATES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;_spr_floorAhmed:
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;        .db #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0,#0xF0, #0xF0, #0xF0,  #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0, #0xF0
;;
;;_spr_bonus:
;;   .db #0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00
;;   .db #0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00
;;   .db #0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00
;;   .db #0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00,#0x00
initPlayer: 
   .db #e_cmp_render | #e_cmp_input | #e_cmp_mov | #e_cmp_colide  | #e_cmp_anime      
   .db #40
   .db #138
   .db #0
   .db #0                                         
   .db #4
   .db #16                                                    
   .dw #0                                              
   .dw #sys_colisions_player 
   .dw #sys_colisions_player                   
   .dw #_spr_viking
   .dw #0xc000                                       
   .db #0
   .db #0                                                      
   .dw #animation_viking                                               
   .db #0
   .db #0                                                           
   .dw #0                                                     
   .db #0                                                      
   .db #4
   .db #16      

initShot1: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #10
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_shot                                                       
   .dw #sys_colisions_shot
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_vaso_rojo
   .dw #0xC000                                                
   .db #0x40
   .db #type_shot | #type_static                              
   .dw #animation_shot1                                                      
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #1            ;; se mueve a la izquierda   

initShot2: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #0
   .db #42
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_shot                                                       
   .dw #sys_colisions_shot
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_vaso_morado
   .dw #0xC000                                                
   .db #0x40
   .db #type_shot | #type_static                              
   .dw #animation_shot2                                                     
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #0            ;;  se mueve a la derecha

initShot3:
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #74
   .db #0
   .db #0                                                              
   .db #5
   .db #16                                                                         
   .dw #sys_ai_shot                                                       
   .dw #sys_colisions_shot
   .dw #sys_colisions_sub_enemy                                    
   .dw #_spr_vaso_amarillo
   .dw #0xc000                                                      
   .db #0x10
   .db #type_shot | #type_static                                      
   .dw #animation_shot3                                                      
   .db #5
   .db #0                                                                             
   .dw #0                                                                          
   .db #0                                                                           
   .db #5
   .db #16  
   .db #1             ;; se mueve a la izquierda                                                                   

initShot4:
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #0
   .db #106
   .db #0
   .db #0                                                              
   .db #5
   .db #16                                                                         
   .dw #sys_ai_shot                                                       
   .dw #sys_colisions_shot
   .dw #sys_colisions_sub_enemy                                    
   .dw #_spr_vaso_azul
   .dw #0xc000                                                        
   .db #0x25
   .db #type_shot | #type_static                                      
   .dw #animation_shot4                                                      
   .db #5
   .db #0                                                                            
   .dw #0                                                                          
   .db #0                                                                           
   .db #5
   .db #16  
   .db #0                                                                        

initShot5:
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #138
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_shot                                                       
   .dw #sys_colisions_shot
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_vaso_verde
   .dw #0xc000                                                
   .db #0x40
   .db #type_shot | #type_static                              
   .dw #animation_shot5                                                      
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16  
   .db #1

initBottle1: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #10
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_bottle                                                       
   .dw #sys_colisions_bottle
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_botella
   .dw #0xc000                                                
   .db #0x40
   .db #type_bottle | #type_static                              
   .dw #animation_bottle                                      
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16  
   .db #1

initBottle2: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #0
   .db #42
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_bottle                                                       
   .dw #sys_colisions_bottle
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_botella
   .dw #0xc000                                                
   .db #0x40
   .db #type_bottle | #type_static                              
   .dw #animation_bottle                                       
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #0 

initBottle3: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #74
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_bottle                                                       
   .dw #sys_colisions_bottle
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_botella
   .dw #0xc000                                                
   .db #0x40
   .db #type_bottle | #type_static                              
   .dw #animation_bottle                                       
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #1 

initBottle4: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #0
   .db #106
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_bottle                                                       
   .dw #sys_colisions_bottle
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_botella
   .dw #0xc000                                                
   .db #0x40
   .db #type_bottle | #type_static                              
   .dw #animation_bottle                                       
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #0 

initBottle5: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_mov | #e_cmp_colision | #e_cmp_anime
   .db #74
   .db #138
   .db #0
   .db #0                                                          
   .db #5
   .db #16                                                                  
   .dw #sys_ai_bottle                                                       
   .dw #sys_colisions_bottle
   .dw #sys_colisions_sub_enemy                          
   .dw #_spr_botella
   .dw #0xc000                                                
   .db #0x40
   .db #type_bottle | #type_static                              
   .dw #animation_bottle                                       
   .db #5
   .db #0                                                                  
   .dw #0                                                                       
   .db #0                                                                      
   .db #5
   .db #16
   .db #1
 
initExp1: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_anime           
   .db #0
   .db #0
   .db #0
   .db #0                                            
   .db #4
   .db #16                                                    
   .dw #sys_ai_explosion                               
   .dw #0
   .dw #0                                                         
   .dw #_spr_brigui
   .dw #0xc000                                        
   .db #0
   .db #type_static                                     
   .dw #animation_explosion_shot                             
   .db #2
   .db #0                                                           
   .dw #0                                                     
   .db #10                                                     
   .db #4
   .db #16                                                          

initExpBottle: 
   .db #e_cmp_render | #e_cmp_ia | #e_cmp_anime                         
   .db #0
   .db #0
   .db #0
   .db #0                                    
   .db #4
   .db #16                                            
   .dw #sys_ai_explosion                       
   .dw #0
   .db #0                                                 
   .dw #_spr_brigui_agua
   .dw #0xc000                                
   .db #0
   .db #type_static                             
   .dw #animation_explosion_botella                     
   .db #2
   .db #0                                                       
   .dw #0                                             
   .db #10                                             
   .db #4
   .db #13 


initGrass1: 
   .db #e_cmp_render        
   .db #0
   .db #58
   .db #0
   .db #0                    
   .db #40
   .db #11                           
   .dw #0
   .dw #0
   .dw #0                             
   .dw #_spr_grass
   .dw #0xc000          
   .db #0
   .db #type_static            
   .db #5
   .db #0                             
initGrass2: 
   .db #e_cmp_render                      
   .db #0
   .db #122
   .db #0
   .db #0                   
   .db #40
   .db #11                           
   .dw #0                                 
   .dw #0
   .dw #0                             
   .dw #_spr_grass
   .dw #0xc000          
   .db #0
   .db #type_static            
   .db #5
   .db #0                             
initGrass3: 
   .db #e_cmp_render        
   .db #0
   .db #154
   .db #0
   .db #0                   
   .db #40
   .db #11                           
   .dw #0                                 
   .dw #0
   .dw #0                             
   .dw #_spr_grass
   .dw #0xc000          
   .db #0
   .db #type_static            
   .db #5
   .db #0                             
initGrass4: 
   .db #e_cmp_render        
   .db #0
   .db #90
   .db #0
   .db #0                    
   .db #40
   .db #11                           
   .dw #0                                 
   .dw #0
   .dw #0                             
   .dw #_spr_grass
   .dw #0xc000          
   .db #0
   .db #type_static            
   .db #5
   .db #0       
initGrass5: 
   .db #e_cmp_render                    
   .db #0
   .db #26
   .db #0
   .db #0                  
   .db #40
   .db #11                         
   .dw #0                               
   .dw #0
   .dw #0                           
   .dw #_spr_grass
   .dw #0xc000        
   .db #0
   .db #type_static          
   .db #5
   .db #0   
    
initVida:
   .db #e_cmp_render             ;; 
   .db #60                       ;;x
   .db #176                      ;;y
   .db #0                        ;; vx
   .db #0                        ;;vy
   .db #17                       ;;w
   .db #16                       ;;h
   .dw #0                        ;;ai
   .dw #0                        ;;colision
   .dw #0                        ;;physics
   .dw #_spr_vida_3              ;;sprite
   .dw #0xc000                   ;;prebos
initAranya:
   .db #e_cmp_render             ;; 
   .db #30                       ;;x
   .db #176                      ;;y
   .db #0                        ;; vx
   .db #0                        ;;vy
   .db #16                       ;;w
   .db #16                       ;;h
   .dw #0                        ;;ai
   .dw #0                        ;;colision
   .dw #0                        ;;physics
   .dw #_spr_aranya              ;;sprite
   .dw #0xc000                   ;;prebos

initBottle:
   .db #e_cmp_render             ;; 
   .db #0                        ;;x
   .db #176                      ;;y
   .db #0                        ;;vx
   .db #0                        ;;vy
   .db #16                       ;;w
   .db #16                       ;;h
   .dw #0                        ;;ai
   .dw #0                        ;;colision
   .dw #0                        ;;physics
   .dw #_spr_botella_winner_0    ;;sprite
   .dw #0xc000                   ;;prebos
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Modifies every entity with mov flag, updating vx ;;
;;                                                       ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_generator_init_game::
   call genera_mapa
   
   ld hl, #initPlayer
   call sysGeneratorTmpl

   ld ix, #initVida
   call sys_render_draw_once
   ld ix, #initAranya
   call sys_render_draw_once
   ld ix, #initBottle
   call sys_render_draw_once


ret
genera_mapa:
   ld hl, #initGrass1
   call sysGeneratorTmpl

   ld hl, #initGrass2
   call sysGeneratorTmpl

   ld hl, #initGrass3
   call sysGeneratorTmpl

   ld hl, #initGrass4
   call sysGeneratorTmpl

   ld hl, #initGrass5
   call sysGeneratorTmpl

   ld ix, #initGrass1
   ld a, #40
   ld e_x(ix), a
   ld hl, #initGrass1 
   call sysGeneratorTmpl

   ld ix, #initGrass2
   ld a, #40
   ld e_x(ix), a
   ld hl, #initGrass2 
   call sysGeneratorTmpl

   ld ix, #initGrass3
   ld a, #40
   ld e_x(ix), a
   ld hl, #initGrass3 
   call sysGeneratorTmpl

   ld ix, #initGrass4
   ld a, #40
   ld e_x(ix), a
   ld hl, #initGrass4 
   call sysGeneratorTmpl

   ld ix, #initGrass5
   ld a, #40
   ld e_x(ix), a
   ld hl, #initGrass5 
   call sysGeneratorTmpl

   ret

sys_generator_ray::

   ld ix, #initVida
   ld e_y(ix), b
   ld e_x(ix), a
   ld hl, #initVida 
   call sysGeneratorTmpl

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;       ;;
;;                                                       ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

sys_generator_update::
   ;;Carga el tiempo para generar
   ld a, (#wait2gen)
   ;; lo decrementa
   dec a
   ld (#wait2gen), a
   ret nz          ;; si es cero se sale automaticamente   
   ;;cargamos en a el tiempo minimo para generar
   ld a, (#minwait2genShot)
   ld (#wait2gen), a;; Actualizamos el tiempo para generar
   ld a, (#n_shots) ;; cargamos en a el numero de enemigos que queremos cargar
   or a  
   jp nz, _not_reset_shot           

   ld a, (#max_nshots) ;;
   ld c, #maxShots ;;
   cp c                    
   jp z, _timer ;; Si hay 50 enemigos esto salta al timer para esperar a que se mueran

   ld b, #Addn_shots;; Cargamos el numero de enemigos a añadir
   add a, b ;;A el numero real de enemigos en directo
   ld b, #SpeedUp2 ;;
   cp b ;; 40 - 15?? Que en caso de que sea a 50, lo que pasa es que la resta da 0
   ;; Y se va aqui a la mierda
   jp z, _speed2;;Cuando te pasas de enemigos, van lentos??
   ;; Y sino metemos en b el SpeedUp1
   ld b, #SpeedUp1 ;; Y si son 30 enemigos o mas nos vamos a la mierda otra vez
   cp b
   jp z, _speed1
   ;;Y si no a esperar
   jp _timer

_speed2:
   push af  ;; a = tiene el numero total de entidades en directo
   ;;Actualizamos valores

   ld a, e_movement_ia(ix) ;; pone en a si es 0 (derecha) o 1 (izquierda) dependiendo del movimiento que queramos
   jp z, sys_move_right
   call sys_move_left
sys_move_right:
   ld a, (#ai_speed_right)
   dec a
   ld (ai_speed_right), a
   jp _goToTimer
sys_move_left:
   ld a, (#ai_speed_left)
   dec a
   ld (ai_speed_left), a
_goToTimer:
   pop af
   jp _timer
_speed1:
   push af
   ;;Aqui mas de lo mismo
   ld a, (#ai_counter)
   dec a
   ld (ai_counter), a
   pop af
   
_timer:
   ;;Actualizamos los valores de antes
   ld (n_shots), a
   ld (max_nshots), a
   ;;Cargamos en a y c ->c =  15 - 20
   ;;de primeras siempre va a dar falso por lo que va donde pone "salta aquí gente"
   ld a, (#minwait2genShot)
   ld c, #Minwait2gen
   cp c
   ;;a partir de la 5a iteración saltará
   jp z, skipToEnemies ;; SaltamMinwait2genos aqui si en algun momento
   ;;Salta aqui gente
   ld b, #Subwait2gen
   sub a, b
skipToEnemies:
   ld (minwait2genShot), a
   ld (wait2gen), a
_not_reset_shot:
   ld a, (#n_shots)
   dec a
   ld (#n_shots), a
   call sys_generator_shot_random
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;       Creates all the tmpl                            ;;
;;                                                       ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sysGeneratorTmpl:
   push hl
   call man_entity_create  
   ex de, hl
   pop hl
   ld bc, #entity_size
   call cpct_memcpy_asm
ret


sys_generator_Bottle:
   ;;Genera una variable aleatoria
   call cpct_getRandom_mxor_u8_asm ;; esto lo devuelve en l
   ;;Carga en a un 3
   ;; nosotros tenemos 6 enemigos así que poner 6 guapis
   ld a, #6
   and l;;l tiene lo obtenido en getRandom, hace el and de 3 con l
   ;; si ha devuelto todo 0 genera ese enemigo, sino salta a enemigo2, sino al enemigo3
   ;; 001 -> Enemigo1
   ;; 010 -> Enemigo2
   ;; 011 -> Enemigo3
   ;; 100 -> Enemigo4
   ;; 101 -> Enemigo5
   ;; 110 -> Enemigo6
   ;; 0000 0011 siempre fijo
   ;; 0000 0100
   ;;------------
   ;; 0000 0000
   ;;Y aqui le hace un and
   or a

   jp nz, generateBottle2
   ld hl, #initBottle1

   jp sysGeneratorBottleCall

generateBottle2:

   dec a
   
   jp nz, generateBottle3

   ld hl, #initBottle2
   jp sysGeneratorBottleCall

generateBottle3:
   dec a
   jp nz, generateBottle4

   ld hl, #initBottle3
   jp sysGeneratorBottleCall

generateBottle4:
   dec a
   jp nz, generateBottle5

   ld hl, #initBottle4
   jp sysGeneratorBottleCall

generateBottle5:
   ld hl, #initBottle5
   
sysGeneratorBottleCall:
   call sysGeneratorTmpl

   ld a, #30
   ld (#minwait2genBottle),a
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;    Enemy
;; Parameters:
;;    hl -> Entity template
;; Return:
;;    -
;; Description:
;;    Creates an entity from a template
;;
sys_generator_shot_random:
   ld a, (#minwait2genBottle)
   dec a
   ld (#minwait2genBottle),a
   and a
   jp z, sys_generator_Bottle
   
   ;;Genera una variable aleatoria
   call cpct_getRandom_mxor_u8_asm ;; esto lo devuelve en l
   ;;Carga en a un 3
   ;; nosotros tenemos 6 enemigos así que poner 6 guapis
   ld a, #7
   and l;;l tiene lo obtenido en getRandom, hace el and de 3 con l
   ;; si ha devuelto todo 0 genera ese enemigo, sino salta a enemigo2, sino al enemigo3
   ;; 001 -> Enemigo1
   ;; 010 -> Enemigo2
   ;; 011 -> Enemigo3
   ;; 100 -> Enemigo4
   ;; 101 -> Enemigo5
   ;; 110 -> Enemigo6
   ;; 0000 0011 siempre fijo
   ;; 0000 0100
   ;;------------
   ;; 0000 0000
   ;;Y aqui le hace un and
   or a

   jp nz, genShot2

   ld hl, #initShot1

   jp sys_generator_shot

genShot2:

   dec a
   
   jp nz, genShot3

   ld hl, #initShot2
   jp sys_generator_shot

genShot3:
   dec a
   jp nz, genShot4

   ld hl, #initShot3
   jp sys_generator_shot

genShot4:
   dec a
   jp nz, genShot5

   ld hl, #initShot4
   jp sys_generator_shot

genShot5:
   ld hl, #initShot5
   halt

sys_generator_shot:
   call sysGeneratorTmpl
   
ret

sys_generator_expShot::
   ld iy, #initExp1
   ld a, e_x(ix)
   ld e_x(iy), a
   ld a, e_y(ix)
   ld e_y(iy), a
   ld hl, #initExp1
   call sysGeneratorTmpl
ret

sys_generator_expBottle::
   ld iy, #initExpBottle
   ld a, e_x(ix)
   ld e_x(iy), a
   ld a, e_y(ix)
   ld e_y(iy), a
   ld hl, #initExpBottle
   call sysGeneratorTmpl

ret
