// MTE MK1 (la Churrera) v5.0
// Copyleft 2010-2014, 2020 by the Mojon Twins

// COSAS DE IMPRIMIR 

void print_time(){
	_x = TIMER_X; _y = TIMER_Y; _t = timer_t; print_number2 ();
	timer_old = timer_t;
}

void print_16bits () {
		// unsigned char d,c,m;
		// m = (var)/1000; // Millares
		// c = (var/100)%10; // centenas
		// d = var%100;	// Decenas y unidades
		_x += 3;
		_t = (unsigned char)(score%100);
		print_number2 ();
		_x -= 2;
		_t = (unsigned char)((score/100)%10);
		print_number2 ();
		_x --;
		_t = (unsigned char)(score/1000);
		print_number2 ();
	}

void print_score(){
	_x = 1; _y = 1; print_16bits ();
	old_score = score;
}


// FUNCIONES PARA VER LOS HUECOS EN PASILLOS.
	unsigned char hueco_h_izq (void) {
		cx1 = cx2 = (_en_x - 1) >> 4;
		cy1 = (_en_y + 15)>> 4; 
		cy2 = (_en_y) >> 4;
		cm_two_points ();
		return (at1 == 0 && at2 == 0);
	}
	
	unsigned char hueco_h_der (void) {
		cx1 = cx2 = (_en_x + 16) >> 4;
		cy1 = (_en_y + 15)>> 4; 
		cy2 = (_en_y) >> 4;
		cm_two_points ();
		return (at1 == 0 && at2 == 0);
	}

	unsigned char hueco_v_arr (void) {
		cx1 = _en_x >> 4;
		cx2 = (_en_x + 15) >> 4;
		cy1 = cy2 = (_en_y -1) >> 4; 
		cm_two_points ();
		return (at1 == 0 && at2 == 0);
	}	

	unsigned char hueco_v_aba (void) {
		cx1 = _en_x >> 4;
		cx2 = (_en_x + 15) >> 4;
		cy1 = cy2 = (_en_y + 16) >> 4;
		cm_two_points ();
		return (at1 == 0 && at2 == 0);
	}

// MOVER ENEMIGOS
void mover_enemigo (void){
	
	#asm
		ld a, (_en_estado) // Si estado es diferente a 0 no se mueve
		or a
		jp nz, _paralizado
		
		ld a, 0
		ld  (_aux1), a

		ld 	a, 1
		ld  (_active), a

		ld  a, (__en_mx)
		ld  c, a
		ld  a, (__en_x)
		add a, c
		ld  (__en_x), a
		
		ld  a, (__en_my)
		ld  c, a
		ld  a, (__en_y)
		add a, c
		ld  (__en_y), a
		
		
	#endasm
	
}

void comprueba_pos_otros_enemigos(void) {
				if (_en_x == malotes [2].x && _en_y == malotes [2].y) return 1;
				if (_en_x == malotes [0].x && _en_y == malotes [0].y) return 1;
			}

// CRUCE DE CAMINOS (IA MOMIAS)
void elegir_camino(void){
	
		// if (_en_my){
		
		#asm
			ld a, (__en_my)
			or a
			jp z, _y_way_done
			
			// ld de, (_gpx)
			// ld d,0
			// ld hl, (__en_x)
			// ld h,0
			// sbc hl,de // Es como sub para 16 bits
			// ld (_diferencia),hl
		#endasm
		
		if (_en_y&16 == 0) return 0;
			if (enit == 1) {
				if (comprueba_pos_otros_enemigos()) return 0;
			}
			
			diferencia = _en_x - gpx;
				
				if (diferencia > 0){ 
					if (hueco_h_izq()){
						#asm
							ld a, 255
							ld (__en_mx), a
							ld a, 0
							ld (__en_my), a
							ld a, 1
							ld (_aux1), a
						#endasm
						// _en_mx = -1; 
						// _en_my = 0;
						// aux1 = 1;
					}
				}else if (diferencia < 0){ 
					if (hueco_h_der()){
						#asm
							ld a, 1
							ld (__en_mx), a
							ld (_aux1), a
							ld a, 0
							ld (__en_my), a
						#endasm
						// _en_mx = 1; 
						// _en_my = 0;
						// aux1 = 1;
					}
				}
		// }else{
		#asm
			jp _x_way_done
			
			._y_way_done
			
			// ld de, (_gpy)
			// ld d,0
			// ld hl, (__en_y)
			// ld h,0
			// sbc hl,de
			// ld (_diferencia),hl
		#endasm
			if (_en_x&16 == 0) return 0;
			if (enit == 1) {
				if (comprueba_pos_otros_enemigos()) return 0;
			}
			diferencia = _en_y - gpy;
				if (diferencia > 0){ 
					if (hueco_v_arr()){
						#asm
							ld a, 255
							ld (__en_my), a
							ld a, 0
							ld (__en_mx), a
							ld a, 1
							ld (_aux1), a
						#endasm
						// _en_my = -1; 
						// _en_mx = 0;
						// aux1 = 1;
					}
				}else if (diferencia < 0){ 
					if (hueco_v_aba()){
						// _en_my = 1; 
						// _en_mx = 0;
						// aux1 = 1;
						#asm
							ld a, 1
							ld (__en_my), a
							ld (_aux1), a
							ld a, 0
							ld (__en_mx), a
						#endasm
					}
				}	
		// }	
		#asm
			._x_way_done
			
		#endasm
}


// COMPROBAR TILES SOLIDOS O LIMITES DE ENEMIGOS
void comprobar_limites (void){

	#asm
			ld a, (__en_x)
			ld  c, a
			ld  a, (__en_x1)
			cp  c
			jr  z, _enems_lm_change_axis_x
			ld  a, (__en_x2)
			cp  c

		#ifdef WALLS_STOP_ENEMIES
			jr  z, _enems_lm_change_axis_x
			
			call _mons_col_sc_x
			xor a
			or l

			jr  z, _enems_lm_change_axis_x_done
		#else
			jr  nz, _enems_lm_change_axis_x_done
		#endif

		._enems_lm_change_axis_x
			ld  a, (__en_mx)
			neg
			ld  (__en_mx), a
			ld a, 1
			ld (_aux1), a

		._enems_lm_change_axis_x_done

			ld a, (__en_y)
			ld  c, a
			ld  a, (__en_y1)
			cp  c
			jr  z, _enems_lm_change_axis_y
			ld  a, (__en_y2)
			cp  c

		#ifdef WALLS_STOP_ENEMIES
			jr  z, _enems_lm_change_axis_y
			
			call _mons_col_sc_y
			xor a
			or l

			jr  z, _enems_lm_change_axis_y_done
		#else
			jr  nz, _enems_lm_change_axis_y_done
		#endif
			
		._enems_lm_change_axis_y
			ld  a, (__en_my)
			neg
			ld  (__en_my), a
			ld a, 1
			ld (_aux1), a

		._enems_lm_change_axis_y_done

	#endasm
}	
	
// DETECCION TILES QUE PISAN LOS ENEMIGOS
	void pisa_tile (void) {
		cx1 = (_en_x + 3) >> 4;
		cx2 = (_en_x + 12) >> 4;
		cy1 = (_en_y + 12)>> 4; 
		cy2 = (_en_y + 3) >> 4;
		cm_two_points ();
	}	

// ANIMACIONES ENEMIGOS (ELEGIR FRAMES)
void animar_enemigo (void){
	// ANIMACIONES MOMIAS
	// if (_en_mx > 0) aux1 = 18;
	// if (_en_mx < 0) aux1 = 16;
	// if (_en_my > 0) aux1 = 22;
	// if (_en_my < 0) aux1 = 20;

	#asm	
		ld a, (__en_mx)
		or a
		jr z, _mx_done

		ld  a, (__en_mx)
		bit 7, a
		jr  nz, _othermx 	; < 0
		ld a, 18
		jr _animation_done
		
		._othermx
		ld a, 16
		jr _animation_done
		
		._mx_done
		
		ld  a, (__en_my)
		bit 7, a
		jr  nz, _othermy 	; < 0
		ld a, 22
		jr _animation_done
		
		._othermy
		ld a, 20
		
		._animation_done
		ld (_aux2), a
		
		
		._paralizado

		
	#endasm	
	
	// if (rdt == 2) aux2 += 9;
	// if (rdt == 3) aux2 += 17;
}


// COMPROBAR SI PISA TILE DE HOYO O SE ENTIERRA
void comprobar_tile_pisado (void){

	pisa_tile();	
	
	if (en_estado == 0){
			// CAE EN HOYO SI ESTA EN ESTADO NORMAL
			if (at1 == 1 && at2 == 1) {
				en_an_state [enit] = GENERAL_DYING;
				en_an_count [enit] = 128;
				en_an_next_frame [enit] = sprite_17_a;
				
				// Ajustar coordenadas enemigo
				_en_x += 7;
				_en_y += 7;
				_en_x &= 0xf0; 
				_en_y &= 0xf0;
				
				// Pintar
				_y = _en_y >> 4; 
				_x = _en_x >> 4;
				// _t = 2; _n = 2; update_tile ();
				#asm
					ld a, 2
					ld (__t), a
					ld (__n), a
					call _update_tile
				#endasm
				score += 25;
				// METER FX
				canal_fx = 1; nota_fx = 53;
				AY_PLAY_SOUND (11); 
			}

	}else{ 
		// Animacion definitiva en hoyo (ojillos) despues de n frames
			// if (at1 == 2) {
				// if (en_an_count [enit] == 123)
					// en_an_next_frame [enit] = sprite_17_a;
			// }
			
		// ES ENTERRADO
		if (at1 == 5) {
			score += 200;
			enemigos_matados ++; // *** PEOR EN ASM
			_en_x = x_respawn << 4;
			_en_y = (y_respawn << 4)+1;
			_en_my = 1;
			_en_mx = 0;
			en_an_count [enit] = 96;
			// METER FX
			canal_fx = 1; nota_fx = 60;
			AY_PLAY_SOUND (6); 
			en_an_state [enit] = GENERAL_DYING;
			en_an_count [enit] = 128;
			en_an_next_frame [enit] = sprite_17_a;
		}
	}	
	
}
	
// PINTAR LA LLAVE
unsigned char pintar_llave (void) {
	if (attr (x_llave, y_llave)){
		x_llave = 1;
		while(1){
			if (attr (x_llave, y_llave) == 0) break;
			x_llave++;
		}
	}
	
	// _x = x_llave;
	// _y = y_llave;
	// _t = 4; _n = 4; 
	// update_tile ();
	#asm
		ld a, 4
		ld (__t), a
		ld (__n), a
		ld a, (_x_llave)
		ld (__x), a
		ld a, (_y_llave)
		ld (__y), a
		call _update_tile
	#endasm
}

unsigned char pintar_borrar_flecha (void) {
	// _n = 8; 
	// _x = x_respawn;
	// _y = y_respawn;
	// update_tile ();
	#asm
		ld a, 8
		ld (__n), a
		ld a, (_x_respawn)
		ld (__x), a
		ld a, (_y_respawn)
		ld (__y), a
		call _update_tile
	#endasm
}
// STAGE CLEAR
void zone_clear (void) {
	_x = 10; _y = 11; _gp_gen = spacer; print_str ();
	_y = 13; print_str ();
	_y = 12; _gp_gen = " WELL DONE! "; print_str ();
	cpc_UpdateNow (0);
	// METER MUSICA
	AY_PLAY_MUSIC (6);
	espera_activa (120);
	// STOP MUSICA	
	while (timer_t){
		timer_t--;
		score += 10;
		print_score ();
		print_time();
		cpc_UpdateNow (0);
		// METER FX
		canal_fx = 0; nota_fx = 79;
		AY_PLAY_SOUND (5); 
		espera_activa (3);
	}
	
	if(!bonus_time) musica_contador++;
	if (musica_contador == 2) { // Repetir la cancion 2 veces
		musica_contador = 0;
		musica_id++;
		if (musica_id > 1) musica_id = 0;
	}
}

// VIDAS
void print_lives(void){
	
	for (gpit = 0; gpit < p_life; gpit++){
		_x = (8 + gpit); _y = 1;
		if (gpit > 4) {
			_gp_gen = "+"; print_str (); break;
		}
		_gp_gen = "\" "; print_str ();
	}
	// sp_UpdateNow ();
}

// PINTA O BORRA POWER UP
void print_powerup (void){
	// _x = power_x; _y = power_y;
	_n = behs[_t]; 
	// update_tile ();	
	#asm
		ld a, (_power_x)
		ld (__x), a
		ld a, (_power_y)
		ld (__y), a
		call _update_tile
	#endasm
}
// PINTA SUELO
void print_suelo (void){
	// _t = 1; _n = 0; update_tile ();
	#asm
		ld a, 1
		ld (__t), a
		ld a, 0
		ld (__n), a
		call _update_tile
	#endasm
}

// Reset cavando
void reset_cavando (void){
	#asm
		ld a, 0
		ld (_cavando), a
		ld (_frames_cavando), a
	#endasm
}




void copy_keys (void){
	// Copy keys to extern 
	#asm
		._copy_keys_to_extern
			ld  hl, (__gp_gen)
			ld  de, cpc_KeysData + 12
			ld  bc, 24
			ldir
	#endasm
}



/// INTRO

void intro(void){
	AY_STOP_SOUND ();
	blackout ();

	AY_PLAY_MUSIC (2);// MUsica intro
	_x = 9; _y = 9; _gp_gen = "UTRERA, 1938"; print_str ();
	cpc_UpdateNow (0);
	espera_activa(280);

	unpack ((unsigned int) (s_intro1), BASE_SUPERBUFF);
	cpc_ShowTileMap (1);
	_x = 0; _y = 17; _gp_gen = "AFTER HARD WORKING, JOHNNY PALAS"; print_str ();
	_y = 18; _gp_gen = "MADE A GREAT DISCOVERY, A SECRET"; print_str ();
	_y = 19; _gp_gen = "ENTRANCE TO THE LHOLHA FLOWERS"; print_str ();
	_y = 20; _gp_gen = "PHARAOH CHAMBER..."; print_str ();
	cpc_UpdateNow (0);
	espera_activa(570);

	unpack ((unsigned int) (s_intro2), BASE_SUPERBUFF);
	cpc_ShowTileMap (1);
	_y = 17; _gp_gen = "ACCORDING TO THE LEGEND, IF YOU"; print_str ();
	_y = 18; _gp_gen = "ENTER THE PYRAMID, YOU WILL HAVE"; print_str ();
	_y = 19; _gp_gen = "TO GET ALL THE GEMS OR YOU WILL"; print_str ();
	_y = 20; _gp_gen = "SUCCUMB TO THE CURSE..."; print_str ();
	cpc_UpdateNow (0);
	espera_activa(650);

	vueltas_intro = 3;
	AY_PLAY_MUSIC (3); // MUsica menu
}

void fin(void){
	blackout ();
	unpack ((unsigned int) (s_ending), BASE_SUPERBUFF);
	cpc_ShowTileMap (1);
	_x = 0; 
	_y = 17; _gp_gen = "YOU FINALLY MADE IT, JOHNNY!!"; print_str ();
	_y = 18; _gp_gen = "LUCKILY YOU ARE ALIVE TO SEE THE"; print_str ();
	_y = 19; _gp_gen = "SECRET LHOLHA FLOWERS CHAMBER..."; print_str ();
	cpc_UpdateNow (0);
	AY_PLAY_MUSIC (8); // MUsica ending
	espera_activa(600);

	unpack ((unsigned int) (s_ending), BASE_SUPERBUFF);
	cpc_ShowTileMap (1);
	_y = 17; _gp_gen = "LET'S SEE WHAT WONDERS HAVE BEEN"; print_str ();
	_y = 18; _gp_gen = "HIDDEN FOR CENTURIES..."; print_str ();
	_y = 19; _gp_gen = "ALL THANKS TO OUR HERO..."; print_str ();
	cpc_UpdateNow (0);
	espera_activa(600);
	blackout ();
	_x = 4;
	_y = 17; _gp_gen = "JONNHY, IT IS TOO DARK..."; print_str ();
	cpc_UpdateNow (0);
	espera_activa(90);
	_y = 18; _gp_gen = "LIGHT YOUR TORCH!"; print_str ();
	cpc_UpdateNow (0);
	espera_activa(200);
	unpack ((unsigned int) (s_ending2), BASE_SUPERBUFF);
	cpc_ShowTileMap (1);
	AY_PLAY_MUSIC (4);// MUsica game over
	espera_activa(1000);
	AY_STOP_SOUND ();
	blackout ();
	_x = 11; _y = 12; _gp_gen = "THE END"; print_str ();
	cpc_UpdateNow (0);
	
	espera_activa (300);
	AY_PLAY_MUSIC (3); // Música menu
}


void show_records(void){
	clear_sprites();
	blackout ();

	_x = 9; _y = 0; _gp_gen = "BEST DIGGERS"; print_str ();
	cpc_UpdateNow (0);
	
	// PINTAR TABLA DE RECORDS
	for (enit = 0; enit < 10; enit ++) {
		espera_activa(15);
		score = records_score[enit];
		_x = 8; _y = 3 + enit; print_16bits ();
		_x = 15; _gp_gen = record_names[enit]; print_str ();
		cpc_UpdateNow (0);
	}
	score = 0;
	_x = 9; _y = 22; _gp_gen = "ESC TO MENU"; print_str ();
	
	// PINTAMOS EL DECORADO
	
	for (enit = 0; enit < 15; enit ++) {
		_x = enit; _y = 6; _t = 8;
		update_tile ();	
		_x = enit; _y = 7; _t = 23+(_x&1);
		update_tile ();	
		_x = enit; _y = 8; _t = 1;
		if (_x == 7)  _t = 3;
		update_tile ();	
	}
	cpc_UpdateNow (0);
	
	/// DEMO ///
	//Jonnhy init
	sp_sw [0].cy = 140;
	p_x = 2;
	p_vx = 1;
	_en_x = 58;
	 _en_mx = 0;
	cavando = 10;
	sp_sw [1].invfunc = sm_invfunc [0];
	sp_sw [1].updfunc = sm_updfunc [0];
	sp_sw [1].cy = 140;
	aux3 = 0;
	while (!cpc_TestKey (KEY_ESC)) {
		
		// JOnnhy
		
		p_x += p_vx;
		_y = 8; _x = 7;
		if(p_x == 26 && p_vx >= 0) {
			p_vx = 0; 
			aux3 = 8;
			cavando--;
			_y = 8;
			if (cavando == 5) {
				 _t = 33;
				update_tile ();	
				_en_mx = 1;
			}
			if (cavando == 0) {
				 _t = 32;
				update_tile ();	
				p_vx = 1; aux3 = 0;
			}
		} 
		if(p_x == 30) {
			 _t = 1;
			update_tile ();	
			p_vx = -1; aux3 = 2;
		} 
		if(p_x == 2) break;
		
		aux2 = !aux2;
		sp_sw [SP_PLAYER].cx = p_x;
		sp_sw [SP_PLAYER].sp0 = (int) (sm_sprptr [aux3+aux2]);
		
		// MOmia
		if (_en_mx) {
			sp_sw [1].cx = _en_x;
			sp_sw [1].sp0 = (int) (sm_sprptr [16+aux2]);
			_en_x--;
		}
		
		
		
		
		cpc_UpdateNow (1);
		while (frames_counter < 25){
					#asm
						halt
					#endasm
				}
		frames_counter = 0;
	}		
	clear_sprites();
	
}

void update_records(void){
	for (enit = 0; enit < 10; enit ++) {
		
		if (hiscore > records_score[enit]){ // Si detecto algún record superado
			aux2 = enit; // guardo la posicion del record superado y actualizo de ahi hacia abajo
			for (enit = 9; enit > aux2; enit --) { // copio el resto una posicion más abajo
				records_score[enit] = records_score[enit - 1];
				record_names[enit] = record_names[enit - 1];
			}
			records_score[aux2] = hiscore;
			record_names[aux2] = record_names[10];
			break;
		}
	}

}

void control_vars (){
	if(!picado){
		press_up = cpc_TestKey (KEY_UP);
		press_down = cpc_TestKey (KEY_DOWN);
		press_left = cpc_TestKey (KEY_LEFT);
		press_right = cpc_TestKey (KEY_RIGHT);
	}else{
		press_up = cpc_TestKey (KEY_LEFT);
		press_down = cpc_TestKey (KEY_RIGHT);
		press_left = cpc_TestKey (KEY_DOWN);
		press_right = cpc_TestKey (KEY_UP);
	}
}

