os.c
author viric@llimona
Sun, 07 May 2006 13:34:25 +0200
changeset 25 b37843173279
parent 10 9148590f009d
child 26 95fccfcbd04c
permissions -rw-r--r--
Porting from sokoban/caixes the show_map changes.
Corrects possible sigsegv because of actual_map.
Better separation of sigalarm display / not-signal display.
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@6
   112
			fprintf(stderr,"%c", Temp.Cells[j][i]);
viric@6
   113
		fprintf(stderr,"\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@25
   126
	fprintf(stderr,"Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y);
viric@25
   127
	fprintf(stderr,"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@25
   134
	fprintf(stderr,"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@25
   139
	fprintf(stderr, "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@25
   143
	fflush(stderr);
viric@25
   144
	min_depth_period = MAX_STEPS;
viric@25
   145
	max_depth_period = 0;
viric@6
   146
}
viric@6
   147
viric@10
   148
static void show_percent_callback(const int parameter)
viric@6
   149
{
viric@25
   150
	show_percent_and_map();
viric@6
   151
	alarm(ALARM_SECONDS);
viric@6
   152
}
viric@6
   153
viric@6
   154
static void program_alarm()
viric@6
   155
{
viric@6
   156
        struct sigaction my_action;
viric@6
   157
        int result;
viric@6
   158
viric@6
   159
        my_action.sa_handler = show_percent_callback;
viric@6
   160
        my_action.sa_flags = 0;
viric@6
   161
        memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask));
viric@6
   162
viric@6
   163
        result = sigaction(SIGALRM, &my_action, NULL);
viric@6
   164
        assert(result == 0);
viric@6
   165
viric@6
   166
	alarm(1);
viric@6
   167
}
viric@6
   168
viric@6
   169
void init_os()
viric@6
   170
{
viric@25
   171
#ifndef DEBUG
viric@6
   172
	program_alarm();
viric@25
   173
#endif
viric@6
   174
}
viric@25
   175