os.c
author viric@llimona
Sat, 17 Feb 2007 15:14:30 +0100
changeset 28 cd27cb410375
parent 27 545f73869d65
permissions -rw-r--r--
Recursivity changed into a loop. Now the state is loaded on boot, and saved on TERM.
viric@6
     1
#include <stdio.h>
viric@6
     2
#include <string.h>
viric@6
     3
#include <stdlib.h>
viric@6
     4
#include <unistd.h>
viric@6
     5
#include <signal.h>
viric@6
     6
#include <assert.h>
viric@6
     7
#include "general.h"
viric@6
     8
viric@6
     9
/* Things related to the showing of the status */
viric@6
    10
float percent_to_show = 0;
viric@6
    11
int depth_to_show = 0;
viric@6
    12
viric@6
    13
int max_depth = 0;
viric@6
    14
int min_depth_period = 0;
viric@6
    15
int max_depth_period = 0;
viric@25
    16
struct Map * actual_map = NULL;
viric@6
    17
viric@28
    18
/* The algorithm data */
viric@28
    19
extern struct Map *all_maps;
viric@28
    20
extern struct BoxMove *all_movements; /* DEPTH movements of MAX_MOVES */
viric@28
    21
extern int *all_mov_tries; /* The actual step in movements for every depth */
viric@28
    22
extern int *all_mov_max; /* Maximum of movements per all_movement element */
viric@28
    23
extern float *percent;
viric@28
    24
extern float *percent_part;
viric@28
    25
extern int depth;
viric@6
    26
viric@6
    27
void ReadMap(struct Map *M, char *FileName)
viric@6
    28
{
viric@6
    29
	FILE *Fitxer;
viric@6
    30
	int i,j;
viric@6
    31
viric@6
    32
	if(!(Fitxer = fopen(FileName, "r")))
viric@6
    33
	{
viric@6
    34
		printf("Error opening %s!", FileName);
viric@6
    35
		exit(1);
viric@6
    36
	}
viric@6
    37
viric@6
    38
	M->SizeX=0;
viric@6
    39
	M->SizeY=0;
viric@6
    40
	while (!feof(Fitxer))
viric@6
    41
	{
viric@6
    42
		fgets(M->Cells[M->SizeY], MAX_X, Fitxer);
viric@6
    43
		M->SizeY++;
viric@6
    44
	}
viric@6
    45
	M->SizeY--;
viric@6
    46
	M->SizeX = strlen(M->Cells[0]) - 1;
viric@6
    47
viric@6
    48
	M->NumPlatforms = 0;
viric@6
    49
	M->NumBoxesInPlatform = 0;
viric@6
    50
	M->NumBoxes = 0;
viric@6
    51
	for (j = 0; j<M->SizeY; j++)
viric@6
    52
		for (i=0; i<M->SizeX; i++)
viric@6
    53
		{
viric@6
    54
			if (M->Cells[j][i] == MAN)
viric@6
    55
			{ 
viric@6
    56
				M->Man.x = i; M->Man.y = j; 
viric@6
    57
				M->Cells[M->Man.y][M->Man.x] = BLANK;
viric@6
    58
			}
viric@6
    59
viric@6
    60
			if (M->Cells[j][i] == PLATFORM)
viric@6
    61
				M->NumPlatforms++;
viric@6
    62
			else if (M->Cells[j][i] == BOXINPLATFORM)
viric@6
    63
			{
viric@6
    64
				M->Cells[j][i] = PLATFORM;
viric@6
    65
viric@6
    66
				M->NumPlatforms++;
viric@6
    67
				M->NumBoxesInPlatform++;
viric@6
    68
viric@6
    69
				M->Box[M->NumBoxes].x = i;
viric@6
    70
				M->Box[M->NumBoxes].y = j;
viric@6
    71
				M->NumBoxes++;
viric@6
    72
			} else if (M->Cells[j][i] == BOX)
viric@6
    73
			{
viric@6
    74
				M->Cells[j][i] = BLANK;
viric@6
    75
viric@6
    76
				M->Box[M->NumBoxes].x = i;
viric@6
    77
				M->Box[M->NumBoxes].y = j;
viric@6
    78
				M->NumBoxes++;
viric@6
    79
			} else if (M->Cells[j][i] == CORNER)
viric@6
    80
			{
viric@6
    81
				M->Cells[j][i] = CORNER;
viric@6
    82
			} else if (M->Cells[j][i] != WALL)
viric@6
    83
			{
viric@6
    84
				if (    (M->Cells[j][i-1] == WALL &&
viric@6
    85
					 M->Cells[j-1][i] == WALL) ||
viric@6
    86
					(M->Cells[j][i-1] == WALL &&
viric@6
    87
					 M->Cells[j+1][i] == WALL) ||
viric@6
    88
					(M->Cells[j][i+1] == WALL &&
viric@6
    89
					 M->Cells[j-1][i] == WALL) ||
viric@6
    90
					(M->Cells[j][i+1] == WALL &&
viric@6
    91
					 M->Cells[j+1][i] == WALL))
viric@6
    92
				M->Cells[j][i] = CORNER;
viric@6
    93
			}
viric@6
    94
				
viric@6
    95
		}
viric@6
    96
viric@6
    97
}
viric@6
    98
viric@6
    99
viric@6
   100
void ShowMap (const struct Map *M)
viric@6
   101
{
viric@6
   102
	struct Map Temp;
viric@6
   103
	int i,j;
viric@6
   104
viric@6
   105
	CopyMap(&Temp, M);
viric@6
   106
viric@6
   107
	Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN;
viric@6
   108
viric@6
   109
	for (i = 0; i < Temp.NumBoxes; i++)
viric@6
   110
	{
viric@6
   111
		if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM)
viric@6
   112
			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM;
viric@6
   113
		else
viric@6
   114
			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX;
viric@6
   115
	}
viric@6
   116
viric@6
   117
	for (j = 0; j<Temp.SizeY; j++)
viric@6
   118
	{
viric@6
   119
		for (i=0; i<Temp.SizeX; i++)
viric@27
   120
			fprintf(stdout,"%c", Temp.Cells[j][i]);
viric@27
   121
		fprintf(stdout,"\n");
viric@6
   122
	}
viric@6
   123
viric@6
   124
#if 0
viric@8
   125
	/* Print Where the man can move */
viric@6
   126
	for (j = 0; j<Temp.SizeY; j++)
viric@6
   127
	{
viric@6
   128
		for (i=0; i<Temp.SizeX; i++)
viric@6
   129
			printf("%c", Temp.ManMoves[j][i]);
viric@6
   130
		printf("\n");
viric@6
   131
	}
viric@6
   132
#endif
viric@6
   133
viric@27
   134
	fprintf(stdout,"Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
viric@27
   135
	fprintf(stdout,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
viric@6
   136
			Temp.NumBoxesInPlatform);
viric@6
   137
viric@6
   138
}
viric@6
   139
viric@28
   140
void PrintMoves(const struct BoxMove *b, const int *steps, const int depth)
viric@6
   141
{
viric@28
   142
	int i;
viric@28
   143
	int offset;
viric@28
   144
	char *dir;
viric@28
   145
viric@28
   146
	/* The first isn't a movement. Movements are stored from b[1] and on */
viric@28
   147
	for (i=0; i < depth; ++i)
viric@28
   148
	{
viric@28
   149
		/* the steps are incremented after try. So to find the movement,
viric@28
   150
		 * we should substract 1. */
viric@28
   151
		offset = i*MAX_MOVES + steps[i] -1;
viric@28
   152
		if (b[offset].dir.x == 0 && 
viric@28
   153
			b[offset].dir.y == 1)
viric@28
   154
			dir = "down";
viric@28
   155
		else if (b[offset].dir.x == 0 && 
viric@28
   156
			b[offset].dir.y == -1)
viric@28
   157
			dir = "up";
viric@28
   158
		else if (b[offset].dir.x == -1 && 
viric@28
   159
			b[offset].dir.y == 0)
viric@28
   160
			dir = "left";
viric@28
   161
		else if (b[offset].dir.x == 1 && 
viric@28
   162
			b[offset].dir.y == 0)
viric@28
   163
			dir = "right";
viric@28
   164
		else
viric@28
   165
			dir = "unknown";
viric@28
   166
viric@28
   167
		fprintf(stdout,"Box: %i, Direction: %s\n",
viric@28
   168
				b[offset].box, dir);
viric@28
   169
	}
viric@28
   170
}
viric@28
   171
viric@28
   172
void show_tries(const int d)
viric@28
   173
{
viric@28
   174
	int i;
viric@28
   175
viric@28
   176
	for(i=0; i<=d; ++i)
viric@28
   177
		printf("%i/%i,", all_mov_tries[i], all_mov_max[i]);
viric@28
   178
	putchar('\n');
viric@25
   179
}
viric@25
   180
viric@25
   181
void show_percent_and_map()
viric@25
   182
{
viric@27
   183
	fprintf(stdout, "Percent: %2.12f, depth: %i-%i\n", percent_to_show,
viric@25
   184
		min_depth_period, max_depth_period);
viric@25
   185
	if(actual_map != NULL)
viric@25
   186
		ShowMap(actual_map);
viric@28
   187
	show_tries(min_depth_period);
viric@27
   188
	fflush(stdout);
viric@25
   189
	min_depth_period = MAX_STEPS;
viric@25
   190
	max_depth_period = 0;
viric@6
   191
}
viric@6
   192
viric@27
   193
static void show_percent_usr1_callback(const int parameter)
viric@27
   194
{
viric@27
   195
	show_percent_and_map();
viric@27
   196
}
viric@27
   197
viric@27
   198
static void show_percent_alarm_callback(const int parameter)
viric@6
   199
{
viric@25
   200
	show_percent_and_map();
viric@6
   201
	alarm(ALARM_SECONDS);
viric@6
   202
}
viric@6
   203
viric@6
   204
static void program_alarm()
viric@6
   205
{
viric@6
   206
        struct sigaction my_action;
viric@6
   207
        int result;
viric@6
   208
viric@27
   209
        my_action.sa_handler = show_percent_alarm_callback;
viric@6
   210
        my_action.sa_flags = 0;
viric@6
   211
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@6
   212
viric@6
   213
        result = sigaction(SIGALRM, &my_action, NULL);
viric@6
   214
        assert(result == 0);
viric@6
   215
viric@6
   216
	alarm(1);
viric@6
   217
}
viric@6
   218
viric@27
   219
static void program_usr1()
viric@27
   220
{
viric@27
   221
        struct sigaction my_action;
viric@27
   222
        int result;
viric@27
   223
viric@27
   224
        my_action.sa_handler = show_percent_usr1_callback;
viric@27
   225
        my_action.sa_flags = 0;
viric@27
   226
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@27
   227
viric@27
   228
        result = sigaction(SIGUSR1, &my_action, NULL);
viric@27
   229
        assert(result == 0);
viric@27
   230
}
viric@27
   231
viric@28
   232
void save_state()
viric@28
   233
{
viric@28
   234
	FILE *f;
viric@28
   235
viric@28
   236
	f = fopen("state.raw", "w");
viric@28
   237
	fwrite(all_maps, sizeof(*all_maps), MAX_STEPS+1, f);
viric@28
   238
	fwrite(all_movements, sizeof(*all_movements), MAX_MOVES*(MAX_STEPS+1), f);
viric@28
   239
	fwrite(all_mov_tries, sizeof(*all_mov_tries), MAX_STEPS+1, f);
viric@28
   240
	fwrite(all_mov_max, sizeof(*all_mov_max), MAX_STEPS+1, f);
viric@28
   241
	fwrite(percent, sizeof(*percent), MAX_STEPS+1, f);
viric@28
   242
	fwrite(percent_part, sizeof(*percent_part), MAX_STEPS+1, f);
viric@28
   243
	fwrite(&depth, sizeof(depth), 1, f);
viric@28
   244
	fclose(f);
viric@28
   245
}
viric@28
   246
viric@28
   247
int load_state()
viric@28
   248
{
viric@28
   249
	FILE *f;
viric@28
   250
viric@28
   251
	f = fopen("state.raw", "r");
viric@28
   252
	if (f == NULL)
viric@28
   253
		return 0;
viric@28
   254
	fread(all_maps, sizeof(*all_maps), MAX_STEPS+1, f);
viric@28
   255
	fread(all_movements, sizeof(*all_movements), MAX_MOVES*(MAX_STEPS+1), f);
viric@28
   256
	fread(all_mov_tries, sizeof(*all_mov_tries), MAX_STEPS+1, f);
viric@28
   257
	fread(all_mov_max, sizeof(*all_mov_max), MAX_STEPS+1, f);
viric@28
   258
	fread(percent, sizeof(*percent), MAX_STEPS+1, f);
viric@28
   259
	fread(percent_part, sizeof(*percent_part), MAX_STEPS+1, f);
viric@28
   260
	fread(&depth, sizeof(depth), 1, f);
viric@28
   261
	fclose(f);
viric@28
   262
	return 1;
viric@28
   263
}
viric@28
   264
viric@28
   265
static void save_state_callback(const int parameter)
viric@28
   266
{
viric@28
   267
	save_state();
viric@28
   268
	exit(0);
viric@28
   269
}
viric@28
   270
viric@28
   271
static void program_term_int()
viric@28
   272
{
viric@28
   273
        struct sigaction my_action;
viric@28
   274
        int result;
viric@28
   275
viric@28
   276
        my_action.sa_handler = save_state_callback;
viric@28
   277
        my_action.sa_flags = 0;
viric@28
   278
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@28
   279
viric@28
   280
        result = sigaction(SIGTERM, &my_action, NULL);
viric@28
   281
        assert(result == 0);
viric@28
   282
        result = sigaction(SIGINT, &my_action, NULL);
viric@28
   283
        assert(result == 0);
viric@28
   284
}
viric@28
   285
viric@6
   286
void init_os()
viric@6
   287
{
viric@25
   288
#ifndef DEBUG
viric@27
   289
	program_usr1();
viric@28
   290
	program_term_int();
viric@27
   291
	/* program_alarm();*/
viric@25
   292
#endif
viric@6
   293
}
viric@25
   294