#include <cpctelera.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "graphics.h"
#include "functions.h"
#include "patronesTiles.h"
#include "maps.h"
#include "render.h"
#include "m_menu.h"
#include "m_morning.h"
#include "m_afternoon.h"
#include "m_evening.h"
#include "m_night.h"

i8 const jumpTable[16] = {-1,0,-1,0,-1,0,-1,0,
							1,0,1,0,1,0,1,0};

void cycle();							
void ende();

void showPressed() {
	u8 bar;

	pressed = 0;
	while (pressedFire()) {
		pressed++;
		bar = pressed/16;
		cpct_drawSolidBox (cpctm_screenPtr (CPCT_VMEM_START, 27, 60), cpct_px2byteM0 (3, 3), 16, 8);
		if (bar)
			cpct_drawSolidBox (cpctm_screenPtr (CPCT_VMEM_START, 27, 60), cpct_px2byteM0 (4, 4), bar, 8);
	}
}

void playBasket (){

	showPressed();
	
	if (pressed > 210) // Margen para que no sea tan ajustado encestar
		pressed = 210;

	entities[1].y = 136;
	entities[1].x = 49;
	entities[1].sprite_f2 = (u8*) &G_ball_f2[0];
	while (pressed && eod) {
		if (pressed % 7 == 0) {
			entities[1].x--;
		}
		if (pressed % 28 == 0) {
			entities[1].y = entities[1].y - 4;
		}
		pressed--;
		cycle();
	}

	while (entities[1].y < 144) {
		entities[1].y = entities[1].y + 4;
		cycle();
	}

	//Si es canasta, aumentar contador para poner límite y dar recompensa*/
	if (entities[1].x == 19 || entities[1].x == 20) {
		updateEnergy(25);
		playFX(9,28);
		baskets++;

		if (baskets >= 5) {
			activities[17]++;
			showMessage(ID_SPECIAL_MSG+14);
		}
	}


	//Poner pelota en posición inicial
	entities[1].y = 144;
	entities[1].x = 48;
	entities[1].sprite_f2 = (u8*) &G_ball_f1[0];
	render();
}

void playGolf (){
	
	showPressed();

	entities[1].y = 128;
	entities[1].x = 44;
	entities[1].sprite_f2 = (u8*) &G_golf_f2[0];
	entities[1].sprite_f3 = (u8*) &G_golf_f3[0];
	while (pressed && eod) {
		if (pressed % 7 == 0)
			entities[1].x--;
		pressed--;
		cycle();
	}

	while (entities[1].y < 136) {
		entities[1].y = entities[1].y + 4;
		cycle();
	}

	//Si entra al hoyo, aumentar contador para poner límite y dar recompensa
	if (entities[1].x == 14) {
		playFX(9,28);
		updateEnergy(70);
		activities[21]++;
		showMessage(ID_SPECIAL_MSG+18);
	}

	//Poner pelota en posición inicial
	entities[1].y = 136;
	entities[1].x = 45;
	entities[1].sprite_f2 = (u8*) &G_golf_f1[0];
	entities[1].sprite_f3 = (u8*) &G_golf_f1[0];
	render();
}

void resetValues() {
	u8 i;
    day = 0;

	// Protagonista
	entities[0].x = 20;
	entities[0].id = 64;
	entities[0].height = 16;
	entities[0].width = 3;
	entities[0].movY = 0;
	entities[0].movX = 0;
	entities[0].data1 = 0;
	entities[0].properties = mask_jump | mask_only_solids;
	entities[0].sprite_f1 = spriteset;
	entities[0].sprite_f2 = spriteset;
	entities[0].sprite_f2 = spriteset;

	frame = 0;
	energy = 128;
	reset_face = 0;
	object = 0;
	hour = 7;
	carScreen = 0;
	baskets = 0;
	spiders = 0;
	last_mov = 0;

	//Resetear Actividades
	for (i = 0; i < ID_SPECIAL_MSG; i++)
		activities[i] = 0; 
}

void resetObjects() {
	u8 i;
	object = 0; //Sin objeto en posesión
	for (i = ID_OBJECTS; i < ID_SPECIAL_MSG; i++) {
		if (activities[i] == 1)
			activities[i] = 0;
	}
}

void newDay() {
	int i;

	day++;
  	clock = 1;
  	hour = 7;
  	telephone = 0;
  	hintText = 0; 
	hintTextTime = 0;
	dropsCollected = 0;
	symphony_idx = 0;
    
    // inicializar actividades diarias
    for (i = ID_DIARY; i < ID_OBJECTS; i++) {
		activities[i] = 0;
	}

	clearScreens();

	*(u8*)&paletteM0[3] = HW_PASTEL_CYAN;
	updatePalette();

	if (day == 1) {
		mydrawStringM1(txt_monday, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	}
	if (day == 2) {
		mydrawStringM1(txt_tuesday, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	}
	if (day == 3) {
		mydrawStringM1(txt_wednesday, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	}
	if (day == 4) {
		mydrawStringM1(txt_thursday, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	}
	if (day == 5) {
		mydrawStringM1(txt_friday, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	}

	pausa(SECOND);
	cpct_akp_musicInit((u8*) m_morning);
	music_playing = 1;
    fade();

	//Posición inicio protagonista en su casa
	screen = 0;
	entities[0].x = 16;
	entities[0].y = 40;
	entities[0].jumping = 0;
	eod = 1;
}

void createRandomStreetElement() {
	u8 i;

	entities[MAX_ENTITIES - 1].y = 164;
	if (cpct_rand() %2)
		entities[MAX_ENTITIES - 1].y = 182;

   	//entities[MAX_ENTITIES - 1].movY = 0;

   	entities[MAX_ENTITIES - 1].x = 1;
   	entities[MAX_ENTITIES - 1].movX = 1;

   	i = cpct_rand() % 5;
   	if (vehicles & 1 << i ) {  //2^i
   		entities[MAX_ENTITIES - 1].x = 66;
   		entities[MAX_ENTITIES - 1].movX = -1;
   	}
   	if (i == 0) {
   		entities[MAX_ENTITIES - 1].id = 250;
   		entities[MAX_ENTITIES - 1].height = 12;
   		entities[MAX_ENTITIES - 1].width = 5;
   		entities[MAX_ENTITIES - 1].sprite_f1 = (u8*) &G_bike[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f2 = (u8*) &G_bike[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f3 = (u8*) &G_bike[0]; 
   	}
   	if (i == 1) {
   		entities[MAX_ENTITIES - 1].id = 251;
   		entities[MAX_ENTITIES - 1].height = 8;
   		entities[MAX_ENTITIES - 1].width = 5;
   		entities[MAX_ENTITIES - 1].sprite_f1 = (u8*) &G_car_outrun[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f2 = (u8*) &G_car_outrun[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f3 = (u8*) &G_car_outrun[0]; 
   		if (extra_char == 1)
   			showMessage(ID_SPECIAL_MSG+24);
   	}
   	if (i == 2) {
   		entities[MAX_ENTITIES - 1].id = 252;
   		entities[MAX_ENTITIES - 1].height = 10;
   		entities[MAX_ENTITIES - 1].width = 5;
   		entities[MAX_ENTITIES - 1].sprite_f1 = (u8*) &G_hyppie_van[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f2 = (u8*) &G_hyppie_van[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f3 = (u8*) &G_hyppie_van[0]; 
   	}
   	if (i == 3) {
   		entities[MAX_ENTITIES - 1].id = 253;
   		entities[MAX_ENTITIES - 1].height = 10;
   		entities[MAX_ENTITIES - 1].width = 5;
   		entities[MAX_ENTITIES - 1].sprite_f1 = (u8*) &G_ambulance[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f2 = (u8*) &G_ambulance[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f3 = (u8*) &G_ambulance[0]; 
   	}
   	if (i == 4) {
   		entities[MAX_ENTITIES - 1].id = 254;
   		entities[MAX_ENTITIES - 1].height = 8;
   		entities[MAX_ENTITIES - 1].width = 5;
   		entities[MAX_ENTITIES - 1].sprite_f1 = (u8*) &G_policecar[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f2 = (u8*) &G_policecar[0]; 
   		entities[MAX_ENTITIES - 1].sprite_f3 = (u8*) &G_policecar[0]; 
   	}

   	
}

void initScreen(i8 swipe) {
	u8 *map, *map_bottom;
	u8 i,j;

	swiping = 1;

	if (swipe == -1)
		swipeScreen (255, 0);

	if (swipe == 1)
		swipeScreen (255, 2);

	if (swipe == -2)
		swipeScreen(0,255);

	if (swipe == 2)
		swipeScreen(2,255);

	for (i = 1; i < MAX_ENTITIES; i++){
		entities[i].id = 0;
		entities[i].x0 = 0;
		entities[i].y0 = 0;
		entities[i].jumping = 0;
		entities[i].falling = 0;
		//Valores que se repiten para muchas entidades
		entities[i].movX = 0; 
		entities[i].movY = 0; 
	  	entities[i].height = 16;
	  	entities[i].width = 3;
	  	entities[i].properties = 0;
	}
	idle = 0;

	cpct_etm_setTileset2x4(tileset);
	map = (u8 *) home_end;
	if (screen == 1) {
		map = (u8 *) park_end;
		if (hour >= 21) { // De noche sale el ladrón que es casi inevitable y roba el objeto que lleves
   			cpct_memcpy (&bufferFrame1[0], &G_thief_f1[0], 96);
   			cpct_memcpy (&bufferFrame2[0], &G_thief_f2[0], 96);
   			entities[1].movX = 1;
   			if (entities[0].x < 36) { //Girar el sprite según posición de inicio del protagonista
   				cpct_hflipSpriteMaskedM0 (3, 16, bufferFrame1);
	   			cpct_hflipSpriteMaskedM0 (3, 16, bufferFrame2);
	   			entities[1].movX = -1;
   			}
   			 
	   		entities[1].id = 8;
			entities[1].x = 36;
			entities[1].y = 136;
		   	entities[1].data1 = 30 + (cpct_rand() % 10);
		   	entities[1].properties = mask_jump | mask_flip | mask_only_solids | mask_half_speed; 
		   	entities[1].sprite_f1 = (u8*) &bufferFrame1[0]; 
		   	entities[1].sprite_f2 = (u8*) &bufferFrame2[0]; 
		   	entities[1].sprite_f3 = (u8*) &bufferFrame1[0]; 
	   	} else {
			entities[1].id = 2;
			entities[1].x = 20;
			entities[1].y = 148;
		   	entities[1].movX = 1;
		   	entities[1].height = 4;
		   	entities[1].width = 4;
		   	entities[1].sprite_f1 = (u8*) &G_worm_f1[0]; 
		   	entities[1].sprite_f2 = (u8*) &G_worm_f2[0]; 
		   	entities[1].sprite_f3 = (u8*) &G_worm_f1[0]; 
		   }

	if (activities[ID_OBJECTS_ONCE+0] == 0) { //Si ya comió el gato, no vuelve a aparecer
		entities[2].id = 243;
		entities[2].x = 8;
		entities[2].y = 68;
	   	entities[2].height = 8;
	   	entities[2].width = 4;
	   	entities[2].sprite_f1 = (u8*) &G_cat_f1[0]; 
	   	entities[2].sprite_f2 = (u8*) &G_cat_f1[0]; 
	   	entities[2].sprite_f3 = (u8*) &G_cat_f1[0]; 

	   	if (object == 3) { //Si tenemos pescado, tiene animación
	   		entities[2].sprite_f2 = (u8*) &G_cat_f2[0]; 	
	   	}
	  }
	}

	if (screen == 2) {
		map = (u8 *) mall_end;
		entities[1].id = 3;
		entities[1].x = 33;
		entities[1].y = 88;
 		entities[1].height = 4;
 		entities[1].data1 = 20;
 		entities[1].properties = mask_only_solids; 
 		entities[1].sprite_f1 = (u8*) &G_fly_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_fly_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_fly_f1[0]; 

 		entities[2].id = 3;
		entities[2].x = 4;
		entities[2].y = 28;
 		entities[2].movX = 1;
 		entities[2].height = 4;
 		entities[2].data1 = 20;
		entities[2].properties = mask_only_solids; 
 		entities[2].sprite_f1 = (u8*) &G_fly_f1[0]; 
 		entities[2].sprite_f2 = (u8*) &G_fly_f2[0]; 
 		entities[2].sprite_f3 = (u8*) &G_fly_f1[0]; 
	}

	if (screen == 3) {
		map = (u8 *) beach_end;

		entities[1].id = 9;
		entities[1].x = 32;
		entities[1].y = 140;
 		entities[1].movX = 1;
 		entities[1].height = 4;
 		entities[1].width = 2;
 		entities[1].sprite_f1 = (u8*) &G_shark_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_shark_f1[0]; 
 		entities[1].sprite_f3 = (u8*) &G_shark_f1[0]; 
	}

	if (screen == 4) {
		map = (u8 *) street_end;

		entities[1].id = 6;
		entities[1].x = 36;
		entities[1].y = 140;
 		entities[1].height = 12;
 		entities[1].data1 = 20 + (cpct_rand() % 10);
 		entities[1].sprite_f1 = (u8*) &G_granny_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_granny_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_granny_f1[0]; 	
	}

	if (screen == 5) {
		map = (u8 *) office_end;
		entities[1].id = 5;
		entities[1].x = 50;
		entities[1].y = 88;
 		entities[1].movX = -1;
 		entities[1].data1 = 60;
 		entities[1].properties = mask_jump |  mask_half_speed; 
 		entities[1].sprite_f1 = (u8*) &G_boss_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_boss_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_boss_f3[0]; 
	}

	if (screen == 6) {
		map = (u8 *) office2_end;
		entities[1].id = 5;
		entities[1].x = 20;
		entities[1].y = 136;
 		entities[1].movX = 1;
 		entities[1].data1 = 60;
 		entities[1].properties = mask_jump | mask_only_solids | mask_half_speed; 
 		entities[1].sprite_f1 = (u8*) &G_boss_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_boss_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_boss_f3[0];

 		entities[2].id = 1;
		entities[2].x = 38;
		entities[2].y = 40;
 		entities[2].data1 = 60;
 		entities[2].sprite_f1 = (u8*) &G_guard_f1[0]; 
 		entities[2].sprite_f2 = (u8*) &G_guard_f1[0]; 
 		entities[2].sprite_f3 = (u8*) &G_guard_f1[0];
	}

	if (screen == 7) {
		map = (u8 *) mountain_end;

		entities[1].id = 4;
		entities[1].x = 10;
		entities[1].y = 60;
 		entities[1].movX = 1;
 		entities[1].height = 4;
 		entities[1].width = 4;
 		entities[1].properties = mask_only_solids; 
 		entities[1].sprite_f1 = (u8*) &G_bird_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_bird_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_bird_f1[0]; 
	}

	if (screen == 8) {
		map = (u8 *) castle_end;

		entities[1].id = 7;
		entities[1].x = 20;
		entities[1].y = 48;
 		entities[1].movX = 1;
 		entities[1].width = 4;
 		entities[1].properties =  mask_only_solids; 
 		entities[1].sprite_f1 = (u8*) &G_ghost_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_ghost_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_ghost_f1[0]; 

 		entities[2].id = 7;
		entities[2].x = 32;
		entities[2].y = 120;
 		entities[2].movY = 1;
 		entities[2].width = 4;
 		entities[2].properties = mask_only_solids; 
 		entities[2].sprite_f1 = (u8*) &G_ghost_f1[0]; 
 		entities[2].sprite_f2 = (u8*) &G_ghost_f2[0]; 
 		entities[2].sprite_f3 = (u8*) &G_ghost_f1[0]; 
	}

	if (screen == 9) {
		map = (u8 *) empty_end;

		entities[1].id = 2;
		entities[1].x = 20;
		entities[1].y = 144;
 		entities[1].height = 8;
 		entities[1].sprite_f1 = (u8*) &G_dog_f2[0]; 
 		entities[1].sprite_f2 = (u8*) &G_dog_f1[0]; 
 		entities[1].sprite_f3 = (u8*) &G_dog_f3[0]; 

 		entities[2].id = 2;
		entities[2].x = 46;
		entities[2].y = 144;
 		entities[2].height = 8;
 		entities[2].sprite_f1 = (u8*) &G_dog_f3[0]; 
 		entities[2].sprite_f2 = (u8*) &G_dog_f1[0]; 
 		entities[2].sprite_f3 = (u8*) &G_dog_f2[0]; 
	}

	if (screen == 10) {
		map = (u8 *) market_end;

		entities[1].id = 4;
		entities[1].x = 10;
		entities[1].y = 72;
 		entities[1].movX = 1;
 		entities[1].height = 4;
 		entities[1].width = 4;
 		entities[1].properties = mask_only_solids; 
 		entities[1].sprite_f1 = (u8*) &G_bird_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_bird_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_bird_f1[0]; 

 		if (hour < 19) {
 			entities[2].id = 245;
			entities[2].x = 49;
			entities[2].y = 132;
	 		entities[2].height = 12;
	 		entities[2].width = 11;
	 		entities[2].sprite_f1 = (u8*) &G_closed_f1[0]; 
	 		entities[2].sprite_f2 = (u8*) &G_closed_f1[0]; 
	 		entities[2].sprite_f3 = (u8*) &G_closed_f1[0];
 		} else {
 			entities[2].id = 245;
			entities[2].x = 50;
			entities[2].y = 132;
	 		entities[2].height = 12;
	 		entities[2].width = 2;
	 		entities[2].sprite_f1 = (u8*) &G_wine_f1[0]; 
	 		entities[2].sprite_f2 = (u8*) &G_wine_f1[0]; 
	 		entities[2].sprite_f3 = (u8*) &G_wine_f1[0];
 		}
	}

	if (screen == 11) {
		map = (u8 *) camping_end;

		entities[1].id = 10;
		entities[1].x = 16;
		entities[1].y = 144;
 		entities[1].width = 2;
 		entities[1].height = 8;
 		entities[1].data1 = 10 + (cpct_rand() % 10);
 		entities[1].sprite_f1 = (u8*) &G_fire_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_fire_f1[0]; 
 		entities[1].sprite_f3 = (u8*) &G_fire_f1[0]; 

 		entities[2].id = 10;
		entities[2].x = 34;
		entities[2].y = 136;
 		entities[2].width = 2;
 		entities[2].height = 16;
 		entities[2].data1 = 10 + (cpct_rand() % 10);
 		entities[2].sprite_f1 = (u8*) &G_fire_f2[0]; 
 		entities[2].sprite_f2 = (u8*) &G_fire_f2[0]; 
 		entities[2].sprite_f3 = (u8*) &G_fire_f2[0]; 

 		entities[3].id = 10;
		entities[3].x = 52;
		entities[3].y = 144;
 		entities[3].width = 2;
 		entities[3].height = 8;
 		entities[3].data1 = 10 + (cpct_rand() % 10);
 		entities[3].sprite_f1 = (u8*) &G_fire_f1[0]; 
 		entities[3].sprite_f2 = (u8*) &G_fire_f1[0]; 
 		entities[3].sprite_f3 = (u8*) &G_fire_f1[0]; 
	}

	if (screen == 12) {
		map = (u8 *) cavern1_end;

		if (!dropsCollected && (day == 3 || day == 5)) {
			entities[1].id = 11;
	 		entities[1].width = 2;
	 		entities[1].height = 8;
	 		entities[1].data1 = 1;
	 		entities[1].sprite_f1 = (u8*) &G_drop_f1[0]; 
	 		entities[1].sprite_f2 = (u8*) &G_drop_f2[0]; 
	 		entities[1].sprite_f3 = (u8*) &G_drop_f1[0];

	 		showMessage(ID_SPECIAL_MSG+11);
	 	}
	}

	if (screen == 13) {
		map = (u8 *) cavern2_end;
	}

	if (screen == 14) {
		map = (u8 *) golf_end;

		entities[1].id = 244;
		entities[1].x = 45;
		entities[1].y = 136;
 		entities[1].width = 1;
 		entities[1].height = 4;
 		entities[1].sprite_f1 = (u8*) &G_golf_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_golf_f1[0]; 
 		entities[1].sprite_f3 = (u8*) &G_golf_f1[0];
	}

	if (screen == 15) {
		if (hour != 19)
			map = (u8 *) train_empty_end;
		else // El tren sólo aparece a las 19 horas
			map = (u8 *) train_end;
		
		entities[1].id = 12;
		entities[1].x = 13;
		entities[1].y = 128;
 		entities[1].height = 12;
 		entities[1].sprite_f1 = (u8*) &G_homeless_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_homeless_f1[0]; 
 		entities[1].sprite_f3 = (u8*) &G_homeless_f1[0];
	}

	if (screen == 16) {
		map = (u8 *) hospital_end;

		if (activities[ID_OBJECTS_ONCE+4] == 0) {
			entities[1].id = 241;
			entities[1].x = 10;
			entities[1].y = 0;
	 		entities[1].width = 4;
	 		entities[1].height = 12;
	 		entities[1].sprite_f1 = (u8*) &G_suicide_f1[0]; 
	 		entities[1].sprite_f2 = (u8*) &G_suicide_f1[0]; 
	 		entities[1].sprite_f3 = (u8*) &G_suicide_f1[0];
		}
	}

	if (screen == 17) {
		map = (u8 *) fountain_end;

		if (hour < 16) {
			entities[1].id = 13;
			entities[1].x = 56;
			entities[1].y = 144;
	 		entities[1].width = 2;
	 		entities[1].height = 8;
	 		entities[1].sprite_f1 = (u8*) &G_bee_f1[0]; 
	 		entities[1].sprite_f2 = (u8*) &G_bee_f1[0]; 
	 		entities[1].sprite_f3 = (u8*) &G_bee_f1[0];
	 	}
	}

	if (screen == 18) {
		map = (u8 *) basket_end;

		if (activities[17] == 0) {
			entities[1].id = 12;
			entities[1].x = 48;
			entities[1].y = 144;
	 		entities[1].width = 2;
	 		entities[1].height = 8;
	 		entities[1].sprite_f1 = (u8*) &G_ball_f1[0]; 
	 		entities[1].sprite_f2 = (u8*) &G_ball_f1[0]; 
	 		entities[1].sprite_f3 = (u8*) &G_ball_f1[0];
	 	}


	 	if (day > 3 && activities[3] == 0) {
	 		entities[2].id = 12;
			entities[2].x = 8;
			entities[2].y = 148;
	 		entities[2].width = 5;
	 		entities[2].height = 4;
	 		entities[2].sprite_f1 = (u8*) &G_garbage_f1[0]; 
	 		entities[2].sprite_f2 = (u8*) &G_garbage_f1[0]; 
	 		entities[2].sprite_f3 = (u8*) &G_garbage_f1[0];
	 	}

	}

	if (screen == 19) {
		map = (u8 *) cottage_end;

		if (activities[19] == 0) {
			hose = 0;
			entities[1].id = 242;
	 		entities[1].height = 12;
	 		entities[1].data1 = 1;
	 		entities[1].sprite_f1 = (u8*) &G_spider_f1[0]; 
	 		entities[1].sprite_f2 = (u8*) &G_spider_f2[0]; 
	 		entities[1].sprite_f3 = (u8*) &G_spider_f1[0];
	 	}

	 	entities[2].id = 244;
	 	entities[2].x = 25;
	 	entities[2].y = 140;
 		entities[2].height = 12;
 		entities[2].width = 4;
 		entities[2].sprite_f1 = (u8*) &G_dog2_f1[0]; 
 		entities[2].sprite_f2 = (u8*) &G_dog2_f1[0]; 
 		entities[2].sprite_f3 = (u8*) &G_dog2_f1[0];
	}

	if (screen == 20) {
		map = (u8 *) harbour_end;
		if (hour < 16) 
			showMessage(ID_SPECIAL_MSG+17);
		else
			showMessage(ID_SPECIAL_MSG+23);
	}

	if (screen == 21) {
		map = (u8 *) well_end;

		entities[1].id = 240;
		entities[1].x = 5;
		entities[1].y = 44;
 		entities[1].movX = 1;
 		entities[1].height = 12;
 		entities[1].width = 6;
 		entities[1].properties = mask_only_solids; 
 		entities[1].sprite_f1 = (u8*) &G_space_f1[0]; 
 		entities[1].sprite_f2 = (u8*) &G_space_f2[0]; 
 		entities[1].sprite_f3 = (u8*) &G_space_f1[0]; 

 		entities[2].id = 240;
		entities[2].x = 11;
		entities[2].y = 44;
 		entities[2].movX = 1;
 		entities[2].height = 12;
 		entities[2].width = 6;
 		entities[2].properties = mask_only_solids; 
 		entities[2].sprite_f1 = (u8*) &G_space2_f1[0]; 
 		entities[2].sprite_f2 = (u8*) &G_space2_f2[0]; 
 		entities[2].sprite_f3 = (u8*) &G_space2_f1[0]; 

 		entities[3].id = 240;
		entities[3].x = 17;
		entities[3].y = 44;
 		entities[3].movX = 1;
 		entities[3].height = 12;
 		entities[3].width = 6;
 		entities[3].properties = mask_only_solids; 
 		entities[3].sprite_f1 = (u8*) &G_space_f2[0]; 
 		entities[3].sprite_f2 = (u8*) &G_space_f1[0]; 
 		entities[3].sprite_f3 = (u8*) &G_space_f2[0]; 

	}
	if (screen == 22) {
		map = (u8 *) encounters_end;
	}
	// Parte de abajo (se repite en todas las pantallas), carretera + texto
	// Dimensión: 40 tiles de ancho, 11 de alto
	map_bottom = (u8 *) bottom_end;
	if (screen == 21 || screen == 12 || screen == 13)
		map_bottom = (u8 *) bottom2_end;
	if (screen == 11 || screen == 14)
		map_bottom = (u8 *) bottom3_end;
	
	cpct_zx7b_decrunch_s((void *) (&current_map[0] + (40*11) - 1),(void *) map_bottom);
	
	if ((u16) map_bottom == (u16) bottom_end) {
		if (screen % 4 == 0) {
			//Aprovechar tiles ya existentes para crear un paso de peatones en las pantallas múltiplo de 4
			//éstas no tendrán tráfico
			j=15+40;
			if (screen == 4)
				j=3+40;
			if (screen == 8 || screen == 12)
				j=27+40;
			for (i = 0; i < 8; i++) {
				current_map[j  +(i*40)] = 246;
				current_map[j+1+(i*40)] = 246;
				//current_map[j+2+(i*40)] = 246;
			}
		}
	}
	cpct_etm_drawTilemap2x4_f ( 40, 11 , cpctm_screenPtr (CPCT_VMEM_START, 0, 156), &current_map[0]);

	// Pantalla de juego, se pinta después para que quede en current_map, pues se usa para redibujarTiles para borrar sprites
	// Dimensión: 34 tiles de ancho, 39 de alto 
	cpct_zx7b_decrunch_s((void *) (&current_map[0]+ (MAP_WIDTH * MAP_HEIGHT) - 1),(void *) map);
	cpct_etm_drawTilemap2x4_f ( MAP_WIDTH, MAP_HEIGHT , (u8*) 0xC000, &current_map[0]);

	renderScoreboard();
	if (swipe == -1)
		swipeScreen (255, 1);

	if (swipe == 1)
		swipeScreen (255, 3);

	if (swipe == -2)
		swipeScreen(1,255);

	if (swipe == 2)
		swipeScreen(3,255);

	textPrinted = 0;	
	swiping = 0;

	//Aprovechar el cambio de pantalla para resincronizar el raster por si acaso
	//No es necesario volver a cambiar la llamada del INT´, es inicializar music_counter con el waitVSYNC
	setInterruptAtVSYNCStart(intHandler); 
}

void keyboard(){
	u8 music_temp;

	entities[0].movY = 0;
	entities[0].movX = 0;


	if (cpct_isKeyPressed(Key_H)) {
		music_temp = music_playing;
		pausaTecladoLibre();
		music_playing = 0;
		cpct_akp_stop ();

		mydrawStringM1(txt_pause, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));

		do {
			if (cpct_isKeyPressed(Key_E)) {
				eod = 0;
				day = 6;
				energy = 0;
			}
		}
		while (!cpct_isKeyPressed(Key_H) && eod) ;

		pausaTecladoLibre();
		if (music_temp)
			music_playing = 1; //Reanudar música
		
		cpct_drawSolidBox (cpctm_screenPtr (CPCT_VMEM_START, 0, 192), 0, 64, 8);
		cpct_drawSolidBox (cpctm_screenPtr (CPCT_VMEM_START,64, 192), 0, 16, 8);
		textPrinted = 0;
			
		
		// ****
		//energy = 220;
		//ende(); 
		//object = 0;	renderItem();
		//screen = 5;
		//initScreen(2); 
	}

	if (!pressedFire())
		note = 0;

	//Si se pulsa segundo botón, usarlo para saltar
	if (cpct_isKeyPressed(Joy0_Fire2))
		using2ndbtn = 1;


	//Para saltos verticales
	if (last_mov == 1) {
		entities[0].sprite_f1 = spriteset + (96*5);
		entities[0].sprite_f2 = spriteset + (96*5);
		entities[0].sprite_f3 = spriteset + (96*5);
	} 
	if (last_mov == -1) {
		entities[0].sprite_f1 = spriteset + (96*8);
		entities[0].sprite_f2 = spriteset + (96*8);
		entities[0].sprite_f3 = spriteset + (96*8);
	} 

	//Animación Idle
	if (!entities[0].jumping && !entities[0].falling) {
		if (idle >= 128) {
			entities[0].sprite_f1 = spriteset + (96*1);
			entities[0].sprite_f2 = spriteset + (96*2);
			entities[0].sprite_f3 = spriteset + (96*1);
		} 
		else {
			entities[0].sprite_f1 = spriteset;
			entities[0].sprite_f2 = spriteset;
			entities[0].sprite_f3 = spriteset;
		}
	}
	idle++;
	last_mov = 0;

	if (pressedLeft()){
		entities[0].data1++; 
		if (entities[0].data1 == 2) { //Ralentizar movimientos laterales para que no sean tan rápidos
			entities[0].movX = -1;
			entities[0].data1 = 0;
		}
		entities[0].sprite_f1 = spriteset + (96*6);
		entities[0].sprite_f2 = spriteset + (96*7);
		entities[0].sprite_f3 = spriteset + (96*8);
		last_mov = -1;
		idle = 0;
	} else if (pressedRight()){
		entities[0].data1++;
		if (entities[0].data1 == 2) {
			entities[0].movX = 1;
			entities[0].data1 = 0;
		}
		entities[0].sprite_f1 = spriteset + (96*3);
		entities[0].sprite_f2 = spriteset + (96*4);
		entities[0].sprite_f3 = spriteset + (96*5);
		last_mov = 1;
		idle = 0;
	}

	if (pressedUp() && !entities[0].jumping && !entities[0].falling)  {
		entities[0].jumping = 1;
		idle = 0;
		playFX(11,59);
	}

	if (entities[0].properties & mask_dizzy) 
		entities[0].movX = entities[0].movX * -1;
}		


void checkActivities() {
	u16 tile;
	u8 new_object;
	u8 bonus;
	u8 i;

	tile = tileCell(entities[0].x,entities[0].y);
	//*((u16*)0xFFFE) = tile;
	// Coger Objetos

	// Realizar Actividades
	possibleActivity = 255;
	new_object = 0;
	
	//Esta actividad ya tiene en cuenta la pantalla en la que esté
	if (object == 1 && tile == 1166 && carScreen == screen) //Arrancar coche
      	possibleActivity = ID_DIARY+0;

    if (object == 9 && tile == 1166 && carScreen == screen) //Lavar coche
      	possibleActivity = ID_OBJECTS_ONCE+7;

	if (screen == 0) { // CASA ROLAND
		if (tile == 1176 && hour < 9)  //(40,136) Tomar un café entre 7..9
      		possibleActivity = 0;
      	if (tile >= 770 && tile <= 772 && hour >= 21) //(48,88) Ver TV  
      		possibleActivity = 1;
      	if (tile >= 766 && tile <= 768 && hour >= 21) //(42,88) Jugar Videojuegos
      		possibleActivity = 2;
      	if (tile >= 348 && tile <= 351 && hour >= 21) //(16,40) Dormir 
      		possibleActivity = ID_DIARY+5;
      	if (tile == 756) {//Bañador
      		possibleActivity = ID_OBJECTS+3;
      		new_object = 4;
      		}
      	if (tile >= 1179 && tile <= 1181 && hour >=19 && hour < 21) //(48,136) Tomar algo de la nevera 
      		possibleActivity = 5;
      	
    	if (tile >= 358 && tile <= 362 && hour >= 19) // Darse un baño 
      		possibleActivity = ID_DIARY+1;
      	if (tile == 353) {//Coger las llaves del coche
      		possibleActivity = ID_OBJECTS+0;
	  		new_object = 1;
	  	}
    }

    if (screen == 1) { // PARQUE
      	if (object == 2 && tile == 1172) //Dar pan a paloma
      		possibleActivity = ID_OBJECTS_ONCE+2;
      	if (object == 3 && entities[0].x < 12  && entities[0].y < 80) //Alimentar gato
      		possibleActivity = ID_OBJECTS_ONCE+0;
      	if (tile == 1180)  //Leer Periódico
      		possibleActivity = 6;
    	}

    if (screen == 2) { // CENTRO COMERCIAL
    	if (tile == 757) {
    		possibleActivity = 4;
    	}
    	if (tile == 1176) {//Comprar pan
      		possibleActivity = ID_OBJECTS+1;
	  		new_object = 2;
	  	}
	  	if (tile >= 360 && tile <= 364 && hour >= 19) //Entrar Cine 
      		possibleActivity = 8;
	  	if (tile >= 767 && tile <= 772 && hour >= 19 && hour < 21) //Gimnasio 
      		possibleActivity = 9;
      	if (tile >= 348 && tile <= 353 && ((hour >= 12 && hour < 16) || hour >=21  )) //Restaurante 
      		possibleActivity = 10;
    	}

    if (screen == 3) { // PLAYA
	  	if (object == 4 && tile == 1171) //Bañarse en la playa
      		possibleActivity = ID_OBJECTS_ONCE+1;
      	if (tile == 1182) { //Beber Cerveza
      		possibleActivity = 16;
	  	}
   	}

    if (screen == 4) { //PARADA BUS
		if (tile == 1174)
			possibleActivity = ID_DIARY+3;
	}

    if (screen == 5) { // OFICINA
      	if (tile == 366) //Ligar con Secretaria
      		possibleActivity = 11;
      	if (telephone && tile == (telephone - 34)) 
      		possibleActivity = 14;
      	if (tile == 1185) { //Coger moneda
      		possibleActivity = ID_OBJECTS+4;
	  		new_object = 5;
	  	}

      	if (hour >= 9 && hour < 17 && clock % 64 == 0) { // Teléfonos
      		if (telephone == 0) {
      			telephone = cpct_rand() % 4;
      			if (telephone == 1) {
      				telephone = 11 * 34 + 9;
      			}
	      		if (telephone == 2) {
	      			telephone = 11 * 34 + 25;
	      		}
	      		if (telephone == 3) {
	      			telephone = 23 * 34 + 13;
	      		}
	      	} else {
	      		telephone = 0;
	      	}
      	}      		
    	}

    if (screen == 6) { // OFICINA BIS
      	if (tile == 344) //Ligar con compañero
      		possibleActivity = 12;
      	if (tile == 362) //Descansar en terraza
      		possibleActivity = 13;
    	}


    if (screen == 7) { // MONTAÑA
    		if (tile == 390)
    			possibleActivity = 15;
    		if (tile == 1164) { //Coger carta
	      		possibleActivity = ID_OBJECTS+5;
		  		new_object = 6;
		  	}
    	}

    if (screen == 8) { // CASTILLO
    		if  (
	    		//El cofre en el primer día está en (48,28), para convertirlo en tile sería y/4 
	    		// y multiplicarlo por 34 (tiles por fila). Se le suma x/2. Ése sería el tile del cofre
	    		// Pero como el que importa es el del prota hay que quitar 68 tiles (2 filas), pues el prota
	    		// tiene una altura de 16 y el cofre de 8. Es decir hay una diferencia de 2 tiles de altura
	    		(day == 1 && tile == 552)  
	    		|| (day == 2 && tile == 702)
	    		|| (day == 4 && tile == 194)
	    		|| (day == 5 && tile == 463)
	    		)
	    		possibleActivity = ID_DIARY+2;
	}

	if (screen == 9) { //METRO
		if (tile == 1171)
			possibleActivity = ID_DIARY+4;
	}

    if (screen == 10) { //MERCADO
    	if (tile == 1159)
    		possibleActivity = 35;
    	if (tile == 1161)
    		possibleActivity = ID_DIARY+3;
    	if (tile == 1168) { //Coger pico
      		possibleActivity = ID_OBJECTS+6;
	  		new_object = 7;
	  	}
	  	if (tile == 1176) { //Coger sandwich
      		possibleActivity = ID_OBJECTS+7;
	  		new_object = 8;
	  	}
	  	if (tile == 1180 && hour >= 19) { //Beber Vino
      		possibleActivity = 24;
	  	}
    }

    if (screen == 11) { //CAMPING
    	if (tile == 1174) { 
    		possibleActivity = ID_OBJECTS+9; //Coger Carne
	  		new_object = 10;
	  	}
    }

	if (screen == 13) {  //CAVERNA 2
		if (tile == 1182)
			possibleActivity = 7;
	}

	if (screen == 14) { //GOLF
		if (tile == 1076) 
			possibleActivity = 21;
		if (tile == 1168) { //Coger pescado
      		possibleActivity = ID_OBJECTS+2;
	  		new_object = 3;
	  	}
	}

	if (screen == 15) { //ESTACION
		if (object == 5 && tile == 1059) 
			possibleActivity = 22;
		if (object == 3 && tile == 1059) 
			possibleActivity = 23;
		if (object == 8 && tile == 1059) 
			possibleActivity = ID_OBJECTS_ONCE+6;
		if (hour == 19 && tile == 1068)
			possibleActivity = ID_DIARY+6;
	}

	if (screen == 16) { //HOSPITAL
		if (tile == 1171)
			possibleActivity = 18;
		if (object == 6 && tile == 75) //Dar carta
  		possibleActivity = ID_OBJECTS_ONCE+4;
	}

	if (screen == 17) { //FUENTE
		if (tile == 1163)
			possibleActivity = ID_DIARY+4;
		if (object == 5 && tile == 1173) //Tirar moneda
  			possibleActivity = ID_OBJECTS_ONCE+3;
	}

	if (screen == 18) { //BALONCESTO
		if (tile == 1180) 
			possibleActivity = 17;

		if (day > 3 && tile == 1160) 
			possibleActivity = 3;
	}

	if (screen == 19) { //CASITA
		if (tile == 1178 && hose == 0) 
			possibleActivity = 19;
		if (tile == 1059) { //Coger cubo
      		possibleActivity = ID_OBJECTS+8;
	  		new_object = 9;
	  	}
	  	if (tile == 1168 && object == 10) 
	  		possibleActivity = ID_OBJECTS_ONCE+8;
	}

	if (screen == 20) { //PUERTO
		if (tile == 1180) 
			possibleActivity = 20;
	}

	if (screen == 21) { //POZO
		if (tile == 1165 && activities[ID_OBJECTS_ONCE+5] == 0) {
			if(object == 7) 
				possibleActivity = ID_OBJECTS_ONCE+5;
			else
				showMessage(ID_SPECIAL_MSG+15);
		}
	}


	if (screen == 22) { //ENCUENTROS
		if (tile >= 1170 && tile <= 1179 && activities[25] == 0) {
			possibleActivity = 25 + (tile - 1170);
			textPrinted = 0;
			renderText();
		}
	}


	////////////////////////////////////////////////////////////////////////////////////////////

	if (possibleActivity != 255 && activities[possibleActivity] != 0) //Ya está la actividad hecha
		possibleActivity = 255;

	//Si ya tenemos el objeto a coger, no se muestra
	if (object != 0 && possibleActivity == ID_OBJECTS + object - 1)
		possibleActivity = 255;

	if (pressedFire() && possibleActivity != 255) {
		if (new_object > 0) { //Coger Objeto
			//Volver a activar el resto de objetos 
			resetObjects();
			object = new_object;
			renderItem();
			bonus = 0;
			playFX(6,48); 
		}
		else { //Actividad con recompensa
			bonus = 1;
		}

		if (possibleActivity == 0) {
			showMessage(ID_SPECIAL_MSG+5);
		}

		if (possibleActivity == 1 || possibleActivity == 8) {
			showMessage(ID_SPECIAL_MSG+6);
		}

		if (possibleActivity == 2) {//Mostrar arco-iris en el cielo
			cpct_drawSprite (&G_rainbow[0],  cpctm_screenPtr (CPCT_VMEM_START, 56, 30),8, 14);			
		}

		if (possibleActivity == 5 || possibleActivity == 10) {
			showMessage(ID_SPECIAL_MSG+7);
		}

		if (possibleActivity == 6) {
			showMessage(ID_SPECIAL_MSG+9);
		}

		if (possibleActivity == 7) {
			showMessage(ID_SPECIAL_MSG+10);
		}

		if (possibleActivity == 14) {//Teléfono, es actividad repetible
			telephone = 0;
			bonus = 0;
			//Esta actividad tiene recompensa propia
			updateEnergy(5);
			playFX(3,59);
		}

		if (possibleActivity == 15) {
			if (hour < 21) {
				bonus = 0;
				cpct_zx7b_decrunch_s((void *) (&current_map[0]+ (MAP_WIDTH * MAP_HEIGHT) - 1),(void *) sky1_end);
				cpct_etm_drawTilemap2x4_f ( MAP_WIDTH, MAP_HEIGHT , (u8*) 0xC000, &current_map[0]);
				possibleActivity = 255;
				//textPrinted = 0;
				showMessage(ID_SPECIAL_MSG+12);
				renderText();
				
			} else {
				cpct_zx7b_decrunch_s((void *) (&current_map[0]+ (MAP_WIDTH * MAP_HEIGHT) - 1),(void *) sky2_end);
				cpct_etm_drawTilemap2x4_f ( MAP_WIDTH, MAP_HEIGHT , (u8*) 0xC000, &current_map[0]);
			}
			pausa(SECOND);
			pausa(SECOND);
			pausaTeclado();
			initScreen(-2);
		}

		if (possibleActivity == 16) {
			entities[0].properties = entities[0].properties | mask_dizzy;
			showMessage(ID_SPECIAL_MSG+13);
		}

		if (possibleActivity == 17) {
			bonus = 0;
			playBasket();
		}

		if (possibleActivity == 18) {
			entities[0].properties = entities[0].properties | mask_dizzy;
			showMessage(ID_SPECIAL_MSG+13);
		}

		if (possibleActivity == 19) {
			bonus = 0;
			playFX(4,59);
			if (!hose)
				hose = HOSE_MAX;
		}

		if (possibleActivity == 21) {
			bonus = 0;
			playGolf();
		}

		if (possibleActivity == 22) {
			bonus = 0;
			activities[22]++;
			showMessage(ID_SPECIAL_MSG+19);
		}

		if (possibleActivity == 23) {
			bonus = 0;
			activities[23]++;
			showMessage(ID_SPECIAL_MSG+20);
		}

		if (possibleActivity == 24) {
			entities[0].properties = entities[0].properties | mask_dizzy;
			showMessage(ID_SPECIAL_MSG+13);
		}

		if (possibleActivity >= 25 && possibleActivity <= 34) {
			bonus = 0;
			if (!note) {
				note = 48; //C4
				if (possibleActivity == 26) 
					note = 50; //D4
				if (possibleActivity == 27)
					note = 52; //E4
				if (possibleActivity == 28)
					note = 53; //F4
				if (possibleActivity == 29)
					note = 55; //G4
				if (possibleActivity == 30)
					note = 57; //A4
				if (possibleActivity == 31)
					note = 59; //B4
				if (possibleActivity == 32)
					note = 60; //C5
				if (possibleActivity == 33)
					note = 62; //D5
				if (possibleActivity == 34)
					note = 64; //E5
				
				//Para que suene en los 3 canales
				cpct_akp_SFXPlay (2, 15, note, 0, 0, AY_CHANNEL_A);
   				cpct_akp_SFXPlay (2, 15, note, 0, 0, AY_CHANNEL_B);
   				cpct_akp_SFXPlay (2, 15, note, 0, 0, AY_CHANNEL_C);

				note = possibleActivity;

				if (symphony[symphony_idx] == note) {
					cpct_setBorder( paletteM0 [note-24]); //Para que no coja el 0 (Negro), pues es el que se usa para mostrar al jugador nota incorrecta
					symphony_idx++;

					if (symphony_idx == 5) {
						for (i = 25; i <= 34; i++) {// Desactivar piano
							activities[i]++;
						}
						showMessage(ID_SPECIAL_MSG+22);
						updateEnergy(50);
						cpct_setBorder(HW_BLACK);
					}
				} else {
					symphony_idx = 0;
					cpct_setBorder( HW_BLACK);
				}
			}
		}

		if (possibleActivity == ID_OBJECTS_ONCE+1) {
			showMessage(ID_SPECIAL_MSG+8);
		}

		if (possibleActivity == ID_OBJECTS_ONCE+5) {
			showMessage(ID_SPECIAL_MSG+16);
		}

		if (possibleActivity == ID_DIARY+0) {//Usar coche
			bonus = 0;
			if (screen == 0) 
				screen = 5;
			else 
				screen = 0;

			carScreen = screen;
			playFX(8,28);
			initScreen(2);
		}

		if (possibleActivity == ID_DIARY+3) {//Usar Bus
			bonus = 0;
			if (screen == 4) {
				screen = 10;
				entities[0].x = 8;
			}
			else {
				screen = 4;
				entities[0].x = 42;
			}
			playFX(8,28);
			initScreen(2);
		}

		if (possibleActivity == ID_DIARY+4) {//Coger el metro
			bonus = 0;
			if (screen == 9) {
				screen = 17;
				entities[0].x = 8;
			}
			else {
				screen = 9;
				entities[0].x = 36;
			}
			playFX(8,28);
			initScreen(2);
		}

		if (possibleActivity == ID_DIARY+5) {
			bonus = 0;
			eod = 0;
		}

		if (possibleActivity == ID_DIARY+6) {//Usar Tren
			bonus = 0;
			screen = 22;
			entities[0].x = 18;
			entities[0].y = 136;
			playFX(8,28);
			initScreen(2);
		}

		//Marcar para que no se pueda repetir actividad
		if (bonus) {
			activities[possibleActivity]++;
			updateEnergy(15);
			playFX(3,59);
		}

		// Actividades cuyo objetos ya no tienen utilidad, no vuelven a posición inicial. Desaparecen
		if (possibleActivity >= ID_OBJECTS_ONCE && possibleActivity < ID_DIARY) { 
			activities[ID_OBJECTS+(object-1)]=2; //El objeto ya no se puede volver a coger (valor 2 para que no vuelta a 0 con resetObjects
			object = 0;
			activities[possibleActivity]++; //La actividad con el objeto ya no se puede repetir
			renderItem();
		}
	}

	
}

void collisions(){
	u8 i, restar;


	for (i = 1; i < MAX_ENTITIES; i++) { //Sólo revisar colisión contra protagonista
		restar = 0;
		if (entities[i].id && entities[0].x + 3 > entities[i].x && entities[0].x < entities[i].x + entities[i].width 
		    && entities[0].y + 16 > entities[i].y && entities[0].y < entities[i].y + entities[i].height) {
			restar = 1;

			if ( entities[i].id == 1 && ((u16) entities[i].sprite_f1 == (u16) G_guard_f2))  // Guardia dormido
				restar = 0;

			if ( (entities[i].id == 1 && ((u16) entities[i].sprite_f1 == (u16) G_guard_f1))  // Guardia despierto
				|| (entities[i].id == 6) ){													//Abuela  
				if (entities[i].x > entities[0].x && entities[0].x > 4) {
					entities[0].x = entities[0].x - 4;
				}
				if (entities[i].x < entities[0].x && entities[0].x < 59) {
					entities[0].x = entities[0].x + 4;
				}
			}
			

			if (entities[i].id == 8 && object!= 0) { //Ladrón quita objeto
				object = 0;
				resetObjects();
				renderItem();
			}
			

			if (entities[i].id == 11) { //Gotas mágicas
				restar = 2;
				entities[i].data1 = 1;
				dropsCollected++;
			}

			if (entities[i].id == 12) { //Pelota baloncesto
				restar = 0;
			}

			if (entities[i].id > 240 ) { //Secundarios (sin máscara) que no afectan al protagonista
				restar = 0;
			}

			if (restar == 1) { //Colisión con secundario enemigo
				updateEnergy (-1);	
				playFX(1,57);
			}

			if (restar == 2) {
				updateEnergy (5);	
				playFX(7,69 + cpct_rand() % 30);
			}
		}
	}
	//Arañas
	if (screen == 19) { // Si está en x=45
		if (entities[1].x >= 42 && entities[1].x <= 45 && hose > HOSE_MAX-20) {
			updateEnergy (20);	
			spiders++;
			hose = HOSE_MAX - 20;
			entities[1].data1 = 1;
		}
	}
}

void nextMove() {
	u8 i,j;

	for (i = 1; i < MAX_ENTITIES; i++) {
		if (entities[i].id) {
			entities[i].data1--;
			if (entities[i].id == 1) { //Dormir / Despertar guardia
				if (entities[i].data1 == 0) {
					if((u16) entities[i].sprite_f1 == (u16) G_guard_f1) {
						entities[i].sprite_f1 = (u8*) &G_guard_f2[0]; 
						entities[i].sprite_f2 = (u8*) &G_guard_f3[0]; 
   					entities[i].sprite_f3 = (u8*) &G_guard_f2[0];
					} else {
						entities[i].sprite_f1 = (u8*) &G_guard_f1[0]; 
						entities[i].sprite_f2 = (u8*) &G_guard_f1[0]; 
   					entities[i].sprite_f3 = (u8*) &G_guard_f1[0];
					}
					entities[i].data1 = 60;
				}
			}
			if (entities[i].id == 4) { //Ataque en picado (Gaviota)
				if (entities[i].x == entities[0].x && entities[i].movY == 0) {
					entities[i].movX = entities[0].movX;
					entities[i].movY = 1;
				}
				//Detectar suelo, subir
				if (entities[i].movY == 1 && entities[i].y == (156 - entities[i].height)) {
					entities[i].movX =  0;
					entities[i].movY = -1;
				}
				//Al subir al tope, volver a mov. lateral
				if (entities[i].y == 72 && entities[i].movY == -1) {
					entities[i].movY = 0;
					entities[i].movX = 1;
				}
			}
			if (entities[i].id == 6) { //abalanzarse horizontal y pararse
				if (entities[i].x == 36 && entities[i].movX != 0) {
					entities[i].movX = 0;
					entities[i].data1 = 20;
				}

				if (entities[i].data1 == 0) {
					if (entities[i].x > entities[0].x)
						entities[i].movX = -1;
					else
						entities[i].movX = 1;
				}
			}
			if (entities[i].id == 3) { //movimiento aleatorio (Moscas)
				if (entities[i].data1 == 0) {
					j = cpct_rand() % 6;
					if (j > 2)
						entities[i].movX = j - 4;
					else
						entities[i].movY = j - 1;

   					entities[i].data1 = 20;
				}
			}
			if (entities[i].id == 5 || entities[i].id == 8 ) { //saltar
				if (entities[i].data1 == 0 && !entities[i].jumping && !entities[i].falling)  {
					entities[i].jumping = 1;
					entities[i].data1 = 60;
					if (entities[i].id == 8) {
						entities[i].data1 = 30 + (cpct_rand() % 10);
					}
				}

			}

			if (entities[i].id == 9) { //Tiburón
				if (entities[i].x == 14) {
					entities[i].movX = 1;
					entities[i].sprite_f1 = (u8*) &G_shark_f1[0]; 
					entities[i].sprite_f2 = (u8*) &G_shark_f1[0]; 
					entities[i].sprite_f3 = (u8*) &G_shark_f1[0]; 
				}
				if (entities[i].x == 48) {
					entities[i].movX = -1;
					entities[i].sprite_f1 = (u8*) &G_shark_f2[0]; 
					entities[i].sprite_f2 = (u8*) &G_shark_f2[0]; 
					entities[i].sprite_f3 = (u8*) &G_shark_f2[0]; 
				}
			}
			if (entities[i].id == 11) { //Gotas Lluvia
				if (entities[i].data1 == 0) {
					j = cpct_rand() % 6;
					if (j == 0) {
						entities[i].x = 24;
						entities[i].y = 84;
					}
					if (j == 1) {
						entities[i].x = 30;
						entities[i].y = 76;
					}
					if (j == 2) {
						entities[i].x = 36;
						entities[i].y = 76;
					}
					if (j == 3) {
						entities[i].x = 42;
						entities[i].y = 68;
					}
					if (j == 4) {
						entities[i].x = 48;
						entities[i].y = 60;
					}
					if (j == 5) {
						entities[i].x = 54;
						entities[i].y = 52;
					}
					entities[i].movY = 1;

					entities[i].data1 = 100; //La condición de fin será el tocar tierra. Se pone un valor muy alto por si acaso
				}
			}
			if (entities[i].id == 13) { //Abejas
				if (entities[i].movX == 0 && entities[i].movY == 1){
					//entities[i].movX = 0;
					entities[i].movY = 0;
				}
				if (entities[i].movX == 0 && entities[i].movY == -1){
					//entities[i].movX = 0;
					entities[i].movY = 1;
				}
				if (entities[i].movX == 1 && entities[i].movY == 0){
					entities[i].movX = 0;
					entities[i].movY = -1;
				}
				if (entities[i].movX == -1 && entities[i].movY == 0){
					entities[i].movX = 1;
					//entities[i].movY = 0;
				}
				if (entities[i].movX == 0 && entities[i].movY == 0){
					entities[i].movX = -1;
					//entities[i].movY = 0;
				}
			}

			if (entities[i].id == 242) { //Arañas
				if (entities[i].data1 == 0) {
					entities[i].x = 33 + cpct_rand() % 22;
					entities[i].y = 80 + (cpct_rand() % 10)*4;
				}
				//movimiento aleatorio
				entities[i].x = entities[i].x + (cpct_rand() % 3) - 1;
				entities[i].y = entities[i].y + ((cpct_rand() % 3) - 1) * 4;
				//Encerrarla en su rectángulo
				if (entities[i].x < 32)
					entities[i].x = 32;
				if (entities[i].x > 54)
					entities[i].x = 54;
				if (entities[i].y < 80)
					entities[i].y = entities[i].y + 4;
				if (entities[i].y > 120)
					entities[i].y = entities[i].y - 4;
			}
			if (entities[i].id == 240) { //Space Invaders
				if (entities[i].x == 4 || entities[i].x == 58){
					if (entities[i].y == 92) {
 						entities[i].x = 5;
						entities[i].y = 44;
						entities[i].movX = 1;
					} else
						entities[i].y = entities[i].y + 8;					
				}
			}
			
		}
	}

	// Crear elementos aleatorios en la carretera. Excepto en algunas pantallas
	j = cpct_rand();
	if ((screen % 4 == 0) || (screen >= 11 && screen <= 14) || screen == 14 || screen == 18 || screen == 21)
		j = 1;

	if (!entities[MAX_ENTITIES-1].id && j == 0)
		createRandomStreetElement();


	if (screen == 20) { //Viento
		i = 12;
		if (hour < 16) {
			i = 4;
		}
		if (entities[0].x > 8 && (cpct_rand() % i == 0)) {
			entities[0].movX = - 2;
		}
	}
}

void movementsAllowed() {
	u8 i,x,y,colision;

	for (i = 0; i < MAX_ENTITIES; i++) {
		if (entities[i].jumping) { 
				entities[i].movY = jumpTable[entities[i].jumping-1];
				if (entities[i].jumping == 16) //fin de salto
					entities[i].jumping = 0;
				else
					entities[i].jumping++;
			}
			
		colision = 0;
		if (entities[i].id) { //Para entidades vivas 
			//GRAVEDAD para protagonista (y secundarios con mismo tamaño). Si no está saltando y no hay nada sólido en todo su figura
			
			entities[i].falling = 0;
			if (entities[i].properties & mask_jump) { //Entidades que pueden saltar
				if (!entities[i].jumping && !typeOfTile(tileValue(entities[i].x,entities[i].y+17))
							  && !typeOfTile(tileValue(entities[i].x+2,entities[i].y+17)) ){
					entities[i].y = entities[i].y + 4;
					entities[i].falling = 1;
				}
			}

			x = entities[i].x;
			y = entities[i].y;

			if (/*i == 0 ||*/ entities[i].movY || entities[i].movX) {

				if (entities[i].movY == -1) {
					y = y - 4;
				}

				if (entities[i].movY == 1) {
					y = y + 4;
				}

				if (entities[i].properties & mask_half_speed && entities[i].data1 % 2)
					;
				else
					x = x + entities[i].movX;

				//Si estamos saltando o cayendo del salto, parar si hay debajo una plataforma
				if ((entities[i].jumping || entities[i].falling) && entities[i].movY == 1 
					&& (typeOfTile(tileValue(x,y + 15)) == 1 || typeOfTile(tileValue(x + 2,y + 15)) == 1 )) {
					entities[i].jumping = 0;
					y = y - 4;
					entities[i].movY = 0;
				}

				// Colisión con área de juego
				if (x == 0 || x == (66 - entities[i].width) || y == 0)
					colision = 1;

				//Si no hay movimiento, se marca una falsa colisión para que no haga las comprobaciones siguientes
				//A excepción de los que se mueven a mitad de velocidad
				if (x == entities[i].x && y == entities[i].y && (!(entities[i].properties & mask_half_speed))) 
					colision = 1;

				if (!colision) {
					//Comprobar que la figura no colisionará con tiles (deben ser múltiplos de 4 en altura)
					//for (j=0; j < entities[i].height; j+=4) {
						colision = maximo (
						  	maximo ( typeOfTile(tileValue(x,y ))                       ,  typeOfTile(tileValue(x + entities[i].width-1,y)) ) ,
						  	maximo ( typeOfTile(tileValue(x,y + entities[i].height-4)) ,  typeOfTile(tileValue(x + entities[i].width-1,y + entities[i].height-4)) )
						  	) ;

						if (colision == 1) { //Chocó contra plataforma y no contra sólido (2)
							if (entities[i].properties & mask_only_solids) //Para protagonista (id==64) o fantasma,moscas,.. no es colisión tocar tipo plataforma
								colision = 0; 
							else
								colision = 1; 
						}
					//}
				}

				if (colision && entities[i].id == 11) { //Entidades que desaparecen al colisionar
						entities[i].data1 = 1;  // Se marca para que se regenere
				}


				if (!colision || entities[i].id >= 250) {
					entities[i].y = y;
					entities[i].x = x;
				} else {
					if (entities[i].jumping) { //cancelar salto
						entities[i].jumping = 0;
						entities[i].movY = 0;
						entities[i].falling = 1;
					} 
					if (i>0) {//rebotar enemigos
						entities[i].movY = entities[i].movY * -1;
						entities[i].movX = entities[i].movX * -1; 
						if (entities[i].properties & mask_flip) {
							cpct_hflipSpriteMaskedM0 (3, 16, bufferFrame1);
	   						cpct_hflipSpriteMaskedM0 (3, 16, bufferFrame2);
						}
					}
				}
			}
		}
	}
}



void nextScreen(){
 
	if (entities[0].x == 1) {
		if (screen == 22)
			screen = 15;
		else
			screen--;

		if (screen == 255)
			screen = 9;
		entities[0].x = 59;
		initScreen(-1);
	}

	if (entities[0].x == 62) {
		if (screen == 22)
			screen = 15;
		else
			screen++;

		if (screen == 10)
			screen = 0;
		entities[0].x = 6;
		initScreen(1);
	}

	if (screen == 19 && entities[0].x <= 15 && entities[0].x >= 12 && entities[0].y == 136) { // Caída en el pozo
		screen = 21;
		entities[0].x = 40;
		entities[0].y = 0;
		initScreen(2);
	}	

	if (screen == 21 && entities[0].x >= 11 && entities[0].x <= 16 && entities[0].y == 4) { // Salida en el pozo
		screen = 19;
		entities[0].x = 10;
		entities[0].y = 124;
		initScreen(-2);
	}
}


void ticClock(){
	//Se empieza a las 7AM

	if (clock % LOOPS_PER_HOUR == 0) { 
		//Cambiar cielo cuando toque 7 / 10 / 16 / 19 / 21 
		hour = 7 + (clock/LOOPS_PER_HOUR);

		if (hour % 2 == 1) {// Sólo quitar energía en horas impares
			updateEnergy(-5);  
			if (hour != 21) //No hacer sonar el FX cuando hay cambio de música
				playFX(5,70);
		}

		if (hour == 10) 
			*(u8*)&paletteM0[3] = HW_BRIGHT_CYAN;

		if (hour == 12)
			cpct_akp_musicInit((u8*) m_afternoon);

		if (hour == 16) {
			*(u8*)&paletteM0[3] = HW_SKY_BLUE;
			cpct_akp_musicInit((u8*) m_evening);
		}

		if (hour == 19) 
			*(u8*)&paletteM0[3] = HW_BLUE;

		if (hour == 21) {
			*(u8*)&paletteM0[3] = HW_BLACK;
			cpct_akp_musicInit((u8*) m_night);
		}

		updatePalette();
		renderHour();

		if (hour == 24) {//Se acabó el día
			music_playing = 0;
			cpct_akp_stop();
			possibleActivity = 255;
			showMessage (ID_SPECIAL_MSG+0);
			renderText();
			updateEnergy(-10);
			pausa(SECOND);
			pausaTeclado();
			eod = 0;
		}

		if (hour == 8 && day == 1)
			showMessage (ID_SPECIAL_MSG+21);
	}

	//Quitar efecto mareado
	if (entities[0].properties & mask_dizzy && !hintTextTime) 
		entities[0].properties = entities[0].properties ^ mask_dizzy;
}

void intro(){
	u8 i;

	if (!firstTime) {
		possibleActivity = 255;
		for (i = 1; i <= 4; i++) {
			//textPrinted = 0;
			showMessage(ID_SPECIAL_MSG+i); 
			renderText();

			pausa(65535);
			fade();
		}
		firstTime = 1;
		textPrinted = 1;
		hintText = 0;
	}

}

void cycle(){
	waitBorder();
	ticClock();
	render();
	clock++;
}

void game(){

	resetValues();
	intro();	

	while (day < 5) {
		
		newDay();
		//Preparar Fase
		initScreen(2);

		while (eod) {
			//waitBorder();  //** ¿necesario? se podría esperar el siguiente en su momento, dejando un ciclo de 12 interrupciones en vez de 6
			collisions();//ver colisiones contra enemigos del frame anterior. Antes de recalcular posiciones
			keyboard(); //Movimiento
			checkActivities(); //Actividades	
			nextMove();
			movementsAllowed(); //Revisar movimientos
			nextScreen();
			renderText();				

			cycle();
		}


	}

	if (day == 5) { // No se ha salido con abort
		ende ();

		if (energy > hiscore)
			hiscore = energy;
	}
}


void ende(){
	u8* face;
	u8* text;
	u8 *map;

	clearScreens();

	cpct_akp_musicInit((u8*) m_menu);
	music_playing = 1;	

	*(u8*)&paletteM0[3] = HW_PASTEL_CYAN;
	updatePalette();
	face = faces;
	text = (u8*) txt_ende1;
	map = (u8 *) ende_normal_end; 
	if (energy >= 200) {
		face = faces + 300;
		text = (u8*) txt_ende2;
		map = (u8 *) ende_good_end; 
	}
	if (energy < 127) {
		face = faces + 150;
		text = (u8*) txt_ende3;
		map = (u8 *) ende_bad_end; 
	}

	cpct_zx7b_decrunch_s((void *) (&current_map[0]+ (26 * 30) - 1),(void *) map);
	cpct_etm_drawTilemap2x4_f ( 26, 30 , cpctm_screenPtr(CPCT_VMEM_START, 27, 20), &current_map[0]);

	cutreZoomModo0 (face, 10, 60, 5, 30, 4);
	mydrawStringM1(&text[0], cpctm_screenPtr(CPCT_VMEM_START, 0, 192));

    pausa(65535);
    pausaTecladoLibre();
	pausaTeclado();
    fade();

    music_playing = 0;
	cpct_akp_stop ();
	mydrawStringM1(txt_finalscore, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
	renderScore(energy);
	pausa(65535);
	if (energy >= 200) {
		mydrawStringM1(txt_ende4, cpctm_screenPtr(CPCT_VMEM_START, 0, 192));
		pausa(65535);
		fade();
	}
}