t_pos *read_file(char *file) { int size, x, y, ry, p, inctile; char line[MAX_LINE_SIZE]; t_pos *ret = (t_pos *) malloc(sizeof(t_pos)); t_hex *hex = &ret->hex; t_tiles *tiles = &ret->tiles; t_done *done = &ret->done; FILE *input; tiles_init(tiles); input = fopen(file, "rb"); if (input == NULL) { free(ret); return NULL; } fgets(line, MAX_LINE_SIZE, input); sscanf(line, "%d", &size); hex_init(hex, size); done_init(done, hex->count); for (y = 0; y < size; ++y) { if (fgets(line, MAX_LINE_SIZE, input) == NULL) { printf("ERROR READING LINE %d!\n", y); free(ret); return NULL; } p = size - 1 - y; for (x = 0; x < size + y; ++x) { char *tile = line + p; p += 2; inctile = 0; if (tile[1] == '.') { inctile = 7; } else { inctile = tile[1] - '0'; } if (tile[0] == '+') { printf("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile, x, y, get_by_pos(hex, make_point(x, y))->id); add_done(done, get_by_pos(hex, make_point(x, y))->id, inctile); } else { (*tiles)[inctile] += 1; } } } for (y = 1; y < size; ++y) { ry = size - 1 + y; if (fgets(line, MAX_LINE_SIZE, input) == NULL) { printf("ERROR READING LINE %d!\n", ry); free(ret); return NULL; } p = y; for (x = y; x < 2 * size - 1; ++x) { char *tile = line + p; p += 2; inctile = 0; if (tile[1] == '.') { inctile = 7; } else { inctile = tile[1] - '0'; } if (tile[0] == '+') { printf("Adding locked tile: %d at pos %d, %d, id=%d\n", inctile, x, ry, get_by_pos(hex, make_point(x, ry))->id); add_done(done, get_by_pos(hex, make_point(x, ry))->id, inctile); } else { (*tiles)[inctile] += 1; } } } fclose(input); pos_init(ret); link_nodes(hex); return ret; }
void map_init() { tiles_init(); map_set_map(); map_move_reset(); }