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