author | viric@llimona |
Sat, 17 Feb 2007 15:14:30 +0100 | |
changeset 28 | cd27cb410375 |
parent 27 | 545f73869d65 |
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 |
|
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 | 26 |
|
27 |
void ReadMap(struct Map *M, char *FileName) |
|
28 |
{ |
|
29 |
FILE *Fitxer; |
|
30 |
int i,j; |
|
31 |
||
32 |
if(!(Fitxer = fopen(FileName, "r"))) |
|
33 |
{ |
|
34 |
printf("Error opening %s!", FileName); |
|
35 |
exit(1); |
|
36 |
} |
|
37 |
||
38 |
M->SizeX=0; |
|
39 |
M->SizeY=0; |
|
40 |
while (!feof(Fitxer)) |
|
41 |
{ |
|
42 |
fgets(M->Cells[M->SizeY], MAX_X, Fitxer); |
|
43 |
M->SizeY++; |
|
44 |
} |
|
45 |
M->SizeY--; |
|
46 |
M->SizeX = strlen(M->Cells[0]) - 1; |
|
47 |
||
48 |
M->NumPlatforms = 0; |
|
49 |
M->NumBoxesInPlatform = 0; |
|
50 |
M->NumBoxes = 0; |
|
51 |
for (j = 0; j<M->SizeY; j++) |
|
52 |
for (i=0; i<M->SizeX; i++) |
|
53 |
{ |
|
54 |
if (M->Cells[j][i] == MAN) |
|
55 |
{ |
|
56 |
M->Man.x = i; M->Man.y = j; |
|
57 |
M->Cells[M->Man.y][M->Man.x] = BLANK; |
|
58 |
} |
|
59 |
||
60 |
if (M->Cells[j][i] == PLATFORM) |
|
61 |
M->NumPlatforms++; |
|
62 |
else if (M->Cells[j][i] == BOXINPLATFORM) |
|
63 |
{ |
|
64 |
M->Cells[j][i] = PLATFORM; |
|
65 |
||
66 |
M->NumPlatforms++; |
|
67 |
M->NumBoxesInPlatform++; |
|
68 |
||
69 |
M->Box[M->NumBoxes].x = i; |
|
70 |
M->Box[M->NumBoxes].y = j; |
|
71 |
M->NumBoxes++; |
|
72 |
} else if (M->Cells[j][i] == BOX) |
|
73 |
{ |
|
74 |
M->Cells[j][i] = BLANK; |
|
75 |
||
76 |
M->Box[M->NumBoxes].x = i; |
|
77 |
M->Box[M->NumBoxes].y = j; |
|
78 |
M->NumBoxes++; |
|
79 |
} else if (M->Cells[j][i] == CORNER) |
|
80 |
{ |
|
81 |
M->Cells[j][i] = CORNER; |
|
82 |
} else if (M->Cells[j][i] != WALL) |
|
83 |
{ |
|
84 |
if ( (M->Cells[j][i-1] == WALL && |
|
85 |
M->Cells[j-1][i] == WALL) || |
|
86 |
(M->Cells[j][i-1] == WALL && |
|
87 |
M->Cells[j+1][i] == WALL) || |
|
88 |
(M->Cells[j][i+1] == WALL && |
|
89 |
M->Cells[j-1][i] == WALL) || |
|
90 |
(M->Cells[j][i+1] == WALL && |
|
91 |
M->Cells[j+1][i] == WALL)) |
|
92 |
M->Cells[j][i] = CORNER; |
|
93 |
} |
|
94 |
||
95 |
} |
|
96 |
||
97 |
} |
|
98 |
||
99 |
||
100 |
void ShowMap (const struct Map *M) |
|
101 |
{ |
|
102 |
struct Map Temp; |
|
103 |
int i,j; |
|
104 |
||
105 |
CopyMap(&Temp, M); |
|
106 |
||
107 |
Temp.Cells[Temp.Man.y][Temp.Man.x] = MAN; |
|
108 |
||
109 |
for (i = 0; i < Temp.NumBoxes; i++) |
|
110 |
{ |
|
111 |
if (Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] == PLATFORM) |
|
112 |
Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] =BOXINPLATFORM; |
|
113 |
else |
|
114 |
Temp.Cells[Temp.Box[i].y][Temp.Box[i].x] = BOX; |
|
115 |
} |
|
116 |
||
117 |
for (j = 0; j<Temp.SizeY; j++) |
|
118 |
{ |
|
119 |
for (i=0; i<Temp.SizeX; i++) |
|
27 | 120 |
fprintf(stdout,"%c", Temp.Cells[j][i]); |
121 |
fprintf(stdout,"\n"); |
|
6 | 122 |
} |
123 |
||
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 | 126 |
for (j = 0; j<Temp.SizeY; j++) |
127 |
{ |
|
128 |
for (i=0; i<Temp.SizeX; i++) |
|
129 |
printf("%c", Temp.ManMoves[j][i]); |
|
130 |
printf("\n"); |
|
131 |
} |
|
132 |
#endif |
|
133 |
||
27 | 134 |
fprintf(stdout,"Man is at (%i,%i)\n", Temp.Man.x, Temp.Man.y); |
135 |
fprintf(stdout,"Platforms: %i, BoxesInPlatform: %i\n", Temp.NumPlatforms, |
|
6 | 136 |
Temp.NumBoxesInPlatform); |
137 |
||
138 |
} |
|
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 | 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 | 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 | 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 | 191 |
} |
192 |
||
27 | 193 |
static void show_percent_usr1_callback(const int parameter) |
194 |
{ |
|
195 |
show_percent_and_map(); |
|
196 |
} |
|
197 |
||
198 |
static void show_percent_alarm_callback(const int parameter) |
|
6 | 199 |
{ |
25
b37843173279
Porting from sokoban/caixes the show_map changes.
viric@llimona
parents:
10
diff
changeset
|
200 |
show_percent_and_map(); |
6 | 201 |
alarm(ALARM_SECONDS); |
202 |
} |
|
203 |
||
204 |
static void program_alarm() |
|
205 |
{ |
|
206 |
struct sigaction my_action; |
|
207 |
int result; |
|
208 |
||
27 | 209 |
my_action.sa_handler = show_percent_alarm_callback; |
6 | 210 |
my_action.sa_flags = 0; |
211 |
memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask)); |
|
212 |
||
213 |
result = sigaction(SIGALRM, &my_action, NULL); |
|
214 |
assert(result == 0); |
|
215 |
||
216 |
alarm(1); |
|
217 |
} |
|
218 |
||
27 | 219 |
static void program_usr1() |
220 |
{ |
|
221 |
struct sigaction my_action; |
|
222 |
int result; |
|
223 |
||
224 |
my_action.sa_handler = show_percent_usr1_callback; |
|
225 |
my_action.sa_flags = 0; |
|
226 |
memset(&my_action.sa_mask, 0, sizeof(my_action.sa_mask)); |
|
227 |
||
228 |
result = sigaction(SIGUSR1, &my_action, NULL); |
|
229 |
assert(result == 0); |
|
230 |
} |
|
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 | 286 |
void init_os() |
287 |
{ |
|
25
b37843173279
Porting from sokoban/caixes the show_map changes.
viric@llimona
parents:
10
diff
changeset
|
288 |
#ifndef DEBUG |
27 | 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 | 291 |
/* program_alarm();*/ |
25
b37843173279
Porting from sokoban/caixes the show_map changes.
viric@llimona
parents:
10
diff
changeset
|
292 |
#endif |
6 | 293 |
} |
25
b37843173279
Porting from sokoban/caixes the show_map changes.
viric@llimona
parents:
10
diff
changeset
|
294 |