os.c
author viric@llimona
Fri, 16 Feb 2007 23:53:32 +0100
changeset 27 545f73869d65
parent 26 95fccfcbd04c
child 28 cd27cb410375
permissions -rw-r--r--
Added USR1 status shower.
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@6
    18
viric@6
    19
void ReadMap(struct Map *M, char *FileName)
viric@6
    20
{
viric@6
    21
	FILE *Fitxer;
viric@6
    22
	int i,j;
viric@6
    23
viric@6
    24
	if(!(Fitxer = fopen(FileName, "r")))
viric@6
    25
	{
viric@6
    26
		printf("Error opening %s!", FileName);
viric@6
    27
		exit(1);
viric@6
    28
	}
viric@6
    29
viric@6
    30
	M->SizeX=0;
viric@6
    31
	M->SizeY=0;
viric@6
    32
	while (!feof(Fitxer))
viric@6
    33
	{
viric@6
    34
		fgets(M->Cells[M->SizeY], MAX_X, Fitxer);
viric@6
    35
		M->SizeY++;
viric@6
    36
	}
viric@6
    37
	M->SizeY--;
viric@6
    38
	M->SizeX = strlen(M->Cells[0]) - 1;
viric@6
    39
viric@6
    40
	M->NumPlatforms = 0;
viric@6
    41
	M->NumBoxesInPlatform = 0;
viric@6
    42
	M->NumBoxes = 0;
viric@6
    43
	for (j = 0; j<M->SizeY; j++)
viric@6
    44
		for (i=0; i<M->SizeX; i++)
viric@6
    45
		{
viric@6
    46
			if (M->Cells[j][i] == MAN)
viric@6
    47
			{ 
viric@6
    48
				M->Man.x = i; M->Man.y = j; 
viric@6
    49
				M->Cells[M->Man.y][M->Man.x] = BLANK;
viric@6
    50
			}
viric@6
    51
viric@6
    52
			if (M->Cells[j][i] == PLATFORM)
viric@6
    53
				M->NumPlatforms++;
viric@6
    54
			else if (M->Cells[j][i] == BOXINPLATFORM)
viric@6
    55
			{
viric@6
    56
				M->Cells[j][i] = PLATFORM;
viric@6
    57
viric@6
    58
				M->NumPlatforms++;
viric@6
    59
				M->NumBoxesInPlatform++;
viric@6
    60
viric@6
    61
				M->Box[M->NumBoxes].x = i;
viric@6
    62
				M->Box[M->NumBoxes].y = j;
viric@6
    63
				M->NumBoxes++;
viric@6
    64
			} else if (M->Cells[j][i] == BOX)
viric@6
    65
			{
viric@6
    66
				M->Cells[j][i] = BLANK;
viric@6
    67
viric@6
    68
				M->Box[M->NumBoxes].x = i;
viric@6
    69
				M->Box[M->NumBoxes].y = j;
viric@6
    70
				M->NumBoxes++;
viric@6
    71
			} else if (M->Cells[j][i] == CORNER)
viric@6
    72
			{
viric@6
    73
				M->Cells[j][i] = CORNER;
viric@6
    74
			} else if (M->Cells[j][i] != WALL)
viric@6
    75
			{
viric@6
    76
				if (    (M->Cells[j][i-1] == WALL &&
viric@6
    77
					 M->Cells[j-1][i] == WALL) ||
viric@6
    78
					(M->Cells[j][i-1] == WALL &&
viric@6
    79
					 M->Cells[j+1][i] == WALL) ||
viric@6
    80
					(M->Cells[j][i+1] == WALL &&
viric@6
    81
					 M->Cells[j-1][i] == WALL) ||
viric@6
    82
					(M->Cells[j][i+1] == WALL &&
viric@6
    83
					 M->Cells[j+1][i] == WALL))
viric@6
    84
				M->Cells[j][i] = CORNER;
viric@6
    85
			}
viric@6
    86
				
viric@6
    87
		}
viric@6
    88
viric@6
    89
}
viric@6
    90
viric@6
    91
viric@6
    92
void ShowMap (const struct Map *M)
viric@6
    93
{
viric@6
    94
	struct Map Temp;
viric@6
    95
	int i,j;
viric@6
    96
viric@6
    97
	CopyMap(&Temp, M);
viric@6
    98
viric@6
    99
	Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN;
viric@6
   100
viric@6
   101
	for (i = 0; i < Temp.NumBoxes; i++)
viric@6
   102
	{
viric@6
   103
		if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM)
viric@6
   104
			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM;
viric@6
   105
		else
viric@6
   106
			Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX;
viric@6
   107
	}
viric@6
   108
viric@6
   109
	for (j = 0; j<Temp.SizeY; j++)
viric@6
   110
	{
viric@6
   111
		for (i=0; i<Temp.SizeX; i++)
viric@27
   112
			fprintf(stdout,"%c", Temp.Cells[j][i]);
viric@27
   113
		fprintf(stdout,"\n");
viric@6
   114
	}
viric@6
   115
viric@6
   116
#if 0
viric@8
   117
	/* Print Where the man can move */
viric@6
   118
	for (j = 0; j<Temp.SizeY; j++)
viric@6
   119
	{
viric@6
   120
		for (i=0; i<Temp.SizeX; i++)
viric@6
   121
			printf("%c", Temp.ManMoves[j][i]);
viric@6
   122
		printf("\n");
viric@6
   123
	}
viric@6
   124
#endif
viric@6
   125
viric@27
   126
	fprintf(stdout,"Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
viric@27
   127
	fprintf(stdout,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms,
viric@6
   128
			Temp.NumBoxesInPlatform);
viric@6
   129
viric@6
   130
}
viric@6
   131
viric@10
   132
void PrintMove(const struct BoxMove b)
viric@6
   133
{
viric@27
   134
	fprintf(stdout,"Box: %i, Direction: {%i,%i}\n", b.box, b.dir.x, b.dir.y);
viric@25
   135
}
viric@25
   136
viric@25
   137
void show_percent_and_map()
viric@25
   138
{
viric@27
   139
	fprintf(stdout, "Percent: %2.12f, depth: %i-%i\n", percent_to_show,
viric@25
   140
		min_depth_period, max_depth_period);
viric@25
   141
	if(actual_map != NULL)
viric@25
   142
		ShowMap(actual_map);
viric@27
   143
	fflush(stdout);
viric@25
   144
	min_depth_period = MAX_STEPS;
viric@25
   145
	max_depth_period = 0;
viric@6
   146
}
viric@6
   147
viric@27
   148
static void show_percent_usr1_callback(const int parameter)
viric@27
   149
{
viric@27
   150
	show_percent_and_map();
viric@27
   151
}
viric@27
   152
viric@27
   153
static void show_percent_alarm_callback(const int parameter)
viric@6
   154
{
viric@25
   155
	show_percent_and_map();
viric@6
   156
	alarm(ALARM_SECONDS);
viric@6
   157
}
viric@6
   158
viric@6
   159
static void program_alarm()
viric@6
   160
{
viric@6
   161
        struct sigaction my_action;
viric@6
   162
        int result;
viric@6
   163
viric@27
   164
        my_action.sa_handler = show_percent_alarm_callback;
viric@6
   165
        my_action.sa_flags = 0;
viric@6
   166
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@6
   167
viric@6
   168
        result = sigaction(SIGALRM, &my_action, NULL);
viric@6
   169
        assert(result == 0);
viric@6
   170
viric@6
   171
	alarm(1);
viric@6
   172
}
viric@6
   173
viric@27
   174
static void program_usr1()
viric@27
   175
{
viric@27
   176
        struct sigaction my_action;
viric@27
   177
        int result;
viric@27
   178
viric@27
   179
        my_action.sa_handler = show_percent_usr1_callback;
viric@27
   180
        my_action.sa_flags = 0;
viric@27
   181
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@27
   182
viric@27
   183
        result = sigaction(SIGUSR1, &my_action, NULL);
viric@27
   184
        assert(result == 0);
viric@27
   185
}
viric@27
   186
viric@6
   187
void init_os()
viric@6
   188
{
viric@25
   189
#ifndef DEBUG
viric@27
   190
	program_usr1();
viric@27
   191
	/* program_alarm();*/
viric@25
   192
#endif
viric@6
   193
}
viric@25
   194