-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibbox.c
335 lines (304 loc) · 7.6 KB
/
libbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
** libbox.c
** Login : <najon@najon-desktop>
** Started on Sun May 30 02:58:36 2010 najon
** $Id$
**
** Author(s):
** - najon <>
**
** Copyright (C) 2010 najon
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <string.h>
#include "libbox.h"
void DEBUG(char *mess)
{
printf("%s\n", mess);
}
static int gmaxx = 0;
static int gmaxy = 0;
static int gminx = 0;
static int gminy = 0;
// ajoute la box dans la hash tab
inline box *addboxtohtab(struct box * pbox, GHashTable *ht){
char* key = malloc(sizeof(char)*16);
memset(key,0, 16);
snprintf(key, 16,"%.4d%.4d",pbox->x, pbox->y);
box *b = getBoxbyXY(pbox->x, pbox->y, ht);
//do add a box twice to the hashmap
if (b)
{
if (b->x != pbox->x ||
b->y != pbox->y)
printf("add to ht FAIL: %d, %d %d, %d\n", b->x, b->y, pbox->x,pbox->y);
printf("reusing: %d, %d -- %d, %d\n", b->x, b->y, pbox->x,pbox->y);
b->state = pbox->state;
return b;
}
g_hash_table_insert(ht,key,pbox);
return pbox;
}
//retourne la box qui a les coordonnée xy NULL si aucun
inline struct box * getBoxbyXY(int x, int y, GHashTable *ht){
char key[16];
struct box * pbox = NULL;
memset(key, 0, 16);
snprintf(key, 16, "%.4d%.4d", x, y);
pbox = g_hash_table_lookup(ht, &key);
if (pbox == NULL)
return NULL;
if ( (pbox->x != x ) | (pbox->y != y)){
printf("ERROR HTAB: %d, %d -- %d, %d\n", x, y, pbox->x,pbox->y);
//DEBUG("ERROR HTAB");
}
return pbox;
}
int box_decode_state(char c)
{
switch(c)
{
case '0':
return ROUTE;
case '1':
return MUR;
case '2':
return PLAYER;
case '3':
return EXIT;
}
return UNKNOWN;
}
char box_get_state(box *pbox)
{
if (!pbox)
return ' ';
if (pbox->state & UNKNOWN)
return 'U';
if (pbox->state & EXIT)
return '#';
if (pbox->state & VISITED)
return '*';
if (pbox->state & ROUTE)
return '.';
if (pbox->state & MUR)
return '1';
return ' ';
}
void box_verify(box *pbox, GHashTable *ht)
{
box *test;
if (!pbox)
return;
if (pbox->up != getBoxbyXY(pbox->x, pbox->y + 1, ht))
printf("fucked up box[UP]: %d, %d\n", pbox->x, pbox->y);
if (pbox->down != getBoxbyXY(pbox->x, pbox->y - 1, ht))
printf("fucked up box[DO]: %d, %d\n", pbox->x, pbox->y);
if (pbox->left != getBoxbyXY(pbox->x - 1, pbox->y, ht))
printf("fucked up box[LE]: %d, %d\n", pbox->x, pbox->y);
if (pbox->right != getBoxbyXY(pbox->x + 1, pbox->y, ht))
printf("fucked up box[RI]: %d, %d\n", pbox->x, pbox->y);
}
void print_map(GHashTable *ht, box *current)
{
int i = 0;
int j = 0;
printf("Map size: x[%d, %d] y[%d, %d]\n", gminx, gmaxx, gminy, gmaxy);
//for (i = gminy; i < gmaxy; ++i)
for (i = gmaxy - 1; i >= gminy; --i)
{
for (j = gminx; j < gmaxx; ++j)
{
box_verify(getBoxbyXY(j, i, ht), ht);
if (current && current->x == j && current->y == i)
printf("X");
else
printf("%c", box_get_state(getBoxbyXY(j, i, ht)));
}
printf("\n");
}
}
int box_is_unknown(box *pbox)
{
if (!pbox)
return 1;
if (pbox->state & UNKNOWN)
return 1;
return 0;
}
struct box * newbox(char state, int x, int y, GHashTable *ht){
//DEBUG("ds new box");
struct box * nbox;
nbox = (box *)malloc(sizeof(box));
//DEBUG("ds new box av assign");
nbox->left = NULL;
//DEBUG("ds new box apres");
nbox->right = NULL;
nbox->up = NULL;
nbox->down = NULL;
nbox->state = state;
nbox->x = x;
nbox->y = y;
if (x > gmaxx)
gmaxx = x;
if (x < gminx)
gminx = x;
if (y > gmaxy)
gmaxy = y;
if (y < gminy)
gminy = y;
//printf("newbox[%d, %d]: %d\n", x, y, state);
nbox = addboxtohtab( nbox, ht );
if (nbox != getBoxbyXY(x,y, ht)){
printf("error htab dans newbox");
exit(1);
}
chainbox( nbox, ht);
//box_print(nbox);
return nbox;
}
//chaine la box avec les cases autours
void chainbox(struct box * pbox, GHashTable *ht){
struct box * ptempbox = NULL;
//si il y a une case a x - 1 on fait le chainage a gauche
ptempbox = getBoxbyXY( (pbox->x - 1 ), pbox->y, ht);
if (ptempbox != NULL){
pbox->left = ptempbox;
ptempbox->right = pbox;
}
ptempbox = getBoxbyXY( (pbox->x + 1 ), pbox->y, ht);
if (ptempbox != NULL){
pbox->right = ptempbox;
ptempbox->left = pbox;
}
ptempbox = getBoxbyXY( pbox->x, ( pbox->y - 1 ), ht);
if (ptempbox != NULL){
pbox->down = ptempbox;
ptempbox->up = pbox;
}
ptempbox = getBoxbyXY( pbox->x, ( pbox->y + 1 ), ht);
if (ptempbox != NULL){
pbox->up = ptempbox;
ptempbox->down = pbox;
}
// printbox(pbox);
}
void box_print(box *pbox){
char buff[15];
memset(buff,0, sizeof(buff));
if (pbox == NULL){
DEBUG("ERREUR : la case courante est NULL");
return;
}
printf("Box[%d, %d] : %s (Up:%s, Down:%s, Left: %s, Right:%s)\n", pbox->x, pbox->y,
givestatebox(pbox , buff),
givestatebox(pbox->up , buff),
givestatebox(pbox->down , buff),
givestatebox(pbox->left , buff),
givestatebox(pbox->right, buff));
}
char *givestatebox(struct box * pbox, char* rep){
int i = 0;
if (pbox == NULL){
strcpy(rep, "NULL");
return rep;
}
if (pbox->state & UNKNOWN)
rep[i++] = 'U';
if (pbox->state & EXIT)
rep[i++] = 'E';
if (pbox->state & ROUTE)
rep[i++] = 'R';
if (pbox->state & VISITED)
rep[i++] = 'V';
if (pbox->state & MUR)
rep[i++] = 'M';
rep[i] = 0;
return rep;
}
int box_is_wall(box *pbox)
{
if (pbox->state & MUR)
return 1;
return 0;
}
int box_is_visited(box *pbox)
{
if (pbox->state & VISITED)
return 1;
return 0;
}
//return true is this is an unexplorated case
int box_is_interesting(box *pbox)
{
if (!pbox)
{
printf("error: box is NULL\n");
//on feinte, on va dire que c'est encore une case inconnu (au pire ca fait pas de mal)
return 1;
}
if (box_is_wall(pbox))
return 0;
if (box_is_visited(pbox))
return 0;
return 1;
}
int box_is_exit(box *pbox)
{
if (pbox->state & EXIT)
return 1;
return 0;
}
// return the number of road starting from this box
int box_count_interesting_road(box *pbox)
{
int ret = 0;
ret += box_is_interesting(pbox->left);
ret += box_is_interesting(pbox->right);
ret += box_is_interesting(pbox->up);
ret += box_is_interesting(pbox->down);
return ret;
}
void box_mark_visited(box *pbox)
{
pbox->state |= VISITED;
}
/* try to find exit first
* if no exit found, try to found a box not explorated yet
*/
box *box_get_interesting_neighboor(box *pbox)
{
if (box_is_exit(pbox->left))
return pbox->left;
if (box_is_exit(pbox->right))
return pbox->right;
if (box_is_exit(pbox->up))
return pbox->up;
if (box_is_exit(pbox->down))
return pbox->down;
if (box_is_interesting(pbox->left))
return pbox->left;
if (box_is_interesting(pbox->right))
return pbox->right;
if (box_is_interesting(pbox->up))
return pbox->up;
if (box_is_interesting(pbox->down))
return pbox->down;
return 0;
}