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