os.c
changeset 28 cd27cb410375
parent 27 545f73869d65
equal deleted inserted replaced
27:545f73869d65 28:cd27cb410375
    13 int max_depth = 0;
    13 int max_depth = 0;
    14 int min_depth_period = 0;
    14 int min_depth_period = 0;
    15 int max_depth_period = 0;
    15 int max_depth_period = 0;
    16 struct Map * actual_map = NULL;
    16 struct Map * actual_map = NULL;
    17 
    17 
       
    18 /* The algorithm data */
       
    19 extern struct Map *all_maps;
       
    20 extern struct BoxMove *all_movements; /* DEPTH movements of MAX_MOVES */
       
    21 extern int *all_mov_tries; /* The actual step in movements for every depth */
       
    22 extern int *all_mov_max; /* Maximum of movements per all_movement element */
       
    23 extern float *percent;
       
    24 extern float *percent_part;
       
    25 extern int depth;
    18 
    26 
    19 void ReadMap(struct Map *M, char *FileName)
    27 void ReadMap(struct Map *M, char *FileName)
    20 {
    28 {
    21 	FILE *Fitxer;
    29 	FILE *Fitxer;
    22 	int i,j;
    30 	int i,j;
   127 	fprintf(stdout,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
   135 	fprintf(stdout,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
   128 			Temp.NumBoxesInPlatform);
   136 			Temp.NumBoxesInPlatform);
   129 
   137 
   130 }
   138 }
   131 
   139 
   132 void PrintMove(const struct BoxMove b)
   140 void PrintMoves(const struct BoxMove *b, const int *steps, const int depth)
   133 {
   141 {
   134 	fprintf(stdout,"Box: %i, Direction: {%i,%i}\n", b.box, b.dir.x, b.dir.y);
   142 	int i;
       
   143 	int offset;
       
   144 	char *dir;
       
   145 
       
   146 	/* The first isn't a movement. Movements are stored from b[1] and on */
       
   147 	for (i=0; i < depth; ++i)
       
   148 	{
       
   149 		/* the steps are incremented after try. So to find the movement,
       
   150 		 * we should substract 1. */
       
   151 		offset = i*MAX_MOVES + steps[i] -1;
       
   152 		if (b[offset].dir.x == 0 && 
       
   153 			b[offset].dir.y == 1)
       
   154 			dir = "down";
       
   155 		else if (b[offset].dir.x == 0 && 
       
   156 			b[offset].dir.y == -1)
       
   157 			dir = "up";
       
   158 		else if (b[offset].dir.x == -1 && 
       
   159 			b[offset].dir.y == 0)
       
   160 			dir = "left";
       
   161 		else if (b[offset].dir.x == 1 && 
       
   162 			b[offset].dir.y == 0)
       
   163 			dir = "right";
       
   164 		else
       
   165 			dir = "unknown";
       
   166 
       
   167 		fprintf(stdout,"Box: %i, Direction: %s\n",
       
   168 				b[offset].box, dir);
       
   169 	}
       
   170 }
       
   171 
       
   172 void show_tries(const int d)
       
   173 {
       
   174 	int i;
       
   175 
       
   176 	for(i=0; i<=d; ++i)
       
   177 		printf("%i/%i,", all_mov_tries[i], all_mov_max[i]);
       
   178 	putchar('\n');
   135 }
   179 }
   136 
   180 
   137 void show_percent_and_map()
   181 void show_percent_and_map()
   138 {
   182 {
   139 	fprintf(stdout, "Percent: %2.12f, depth: %i-%i\n", percent_to_show,
   183 	fprintf(stdout, "Percent: %2.12f, depth: %i-%i\n", percent_to_show,
   140 		min_depth_period, max_depth_period);
   184 		min_depth_period, max_depth_period);
   141 	if(actual_map != NULL)
   185 	if(actual_map != NULL)
   142 		ShowMap(actual_map);
   186 		ShowMap(actual_map);
       
   187 	show_tries(min_depth_period);
   143 	fflush(stdout);
   188 	fflush(stdout);
   144 	min_depth_period = MAX_STEPS;
   189 	min_depth_period = MAX_STEPS;
   145 	max_depth_period = 0;
   190 	max_depth_period = 0;
   146 }
   191 }
   147 
   192 
   182 
   227 
   183         result = sigaction(SIGUSR1, &my_action, NULL);
   228         result = sigaction(SIGUSR1, &my_action, NULL);
   184         assert(result == 0);
   229         assert(result == 0);
   185 }
   230 }
   186 
   231 
       
   232 void save_state()
       
   233 {
       
   234 	FILE *f;
       
   235 
       
   236 	f = fopen("state.raw", "w");
       
   237 	fwrite(all_maps, sizeof(*all_maps), MAX_STEPS+1, f);
       
   238 	fwrite(all_movements, sizeof(*all_movements), MAX_MOVES*(MAX_STEPS+1), f);
       
   239 	fwrite(all_mov_tries, sizeof(*all_mov_tries), MAX_STEPS+1, f);
       
   240 	fwrite(all_mov_max, sizeof(*all_mov_max), MAX_STEPS+1, f);
       
   241 	fwrite(percent, sizeof(*percent), MAX_STEPS+1, f);
       
   242 	fwrite(percent_part, sizeof(*percent_part), MAX_STEPS+1, f);
       
   243 	fwrite(&depth, sizeof(depth), 1, f);
       
   244 	fclose(f);
       
   245 }
       
   246 
       
   247 int load_state()
       
   248 {
       
   249 	FILE *f;
       
   250 
       
   251 	f = fopen("state.raw", "r");
       
   252 	if (f == NULL)
       
   253 		return 0;
       
   254 	fread(all_maps, sizeof(*all_maps), MAX_STEPS+1, f);
       
   255 	fread(all_movements, sizeof(*all_movements), MAX_MOVES*(MAX_STEPS+1), f);
       
   256 	fread(all_mov_tries, sizeof(*all_mov_tries), MAX_STEPS+1, f);
       
   257 	fread(all_mov_max, sizeof(*all_mov_max), MAX_STEPS+1, f);
       
   258 	fread(percent, sizeof(*percent), MAX_STEPS+1, f);
       
   259 	fread(percent_part, sizeof(*percent_part), MAX_STEPS+1, f);
       
   260 	fread(&depth, sizeof(depth), 1, f);
       
   261 	fclose(f);
       
   262 	return 1;
       
   263 }
       
   264 
       
   265 static void save_state_callback(const int parameter)
       
   266 {
       
   267 	save_state();
       
   268 	exit(0);
       
   269 }
       
   270 
       
   271 static void program_term_int()
       
   272 {
       
   273         struct sigaction my_action;
       
   274         int result;
       
   275 
       
   276         my_action.sa_handler = save_state_callback;
       
   277         my_action.sa_flags = 0;
       
   278         memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
       
   279 
       
   280         result = sigaction(SIGTERM, &my_action, NULL);
       
   281         assert(result == 0);
       
   282         result = sigaction(SIGINT, &my_action, NULL);
       
   283         assert(result == 0);
       
   284 }
       
   285 
   187 void init_os()
   286 void init_os()
   188 {
   287 {
   189 #ifndef DEBUG
   288 #ifndef DEBUG
   190 	program_usr1();
   289 	program_usr1();
       
   290 	program_term_int();
   191 	/* program_alarm();*/
   291 	/* program_alarm();*/
   192 #endif
   292 #endif
   193 }
   293 }
   194 
   294