|
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; |
|
16 struct Map * actual_map; |
|
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++) |
|
112 fprintf(stderr,"%c", Temp.Cells[j][i]); |
|
113 fprintf(stderr,"\n"); |
|
114 } |
|
115 |
|
116 #if 0 |
|
117 // Print Where the man can move |
|
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 |
|
126 printf("Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y); |
|
127 printf("Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms, |
|
128 Temp.NumBoxesInPlatform); |
|
129 |
|
130 } |
|
131 |
|
132 void PrintMove(struct BoxMove b) |
|
133 { |
|
134 printf("Box: %i, Direction: {%i,%i}\n", b.box, b.dir.x, b.dir.y); |
|
135 } |
|
136 |
|
137 static void show_percent_callback(int parameter) |
|
138 { |
|
139 fprintf(stderr, "Percent: %2.12f, depth: %i-%i\n", percent_to_show, |
|
140 min_depth_period, max_depth_period); |
|
141 ShowMap(actual_map); |
|
142 fflush(stderr); |
|
143 min_depth_period = MAX_STEPS; |
|
144 max_depth_period = 0; |
|
145 #ifndef DEBUG |
|
146 alarm(ALARM_SECONDS); |
|
147 #endif |
|
148 } |
|
149 |
|
150 static void program_alarm() |
|
151 { |
|
152 struct sigaction my_action; |
|
153 int result; |
|
154 |
|
155 my_action.sa_handler = show_percent_callback; |
|
156 my_action.sa_flags = 0; |
|
157 memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask)); |
|
158 |
|
159 result = sigaction(SIGALRM, &my_action, NULL); |
|
160 assert(result == 0); |
|
161 |
|
162 alarm(1); |
|
163 } |
|
164 |
|
165 void init_os() |
|
166 { |
|
167 program_alarm(); |
|
168 } |