-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave.h
149 lines (135 loc) · 3.85 KB
/
Save.h
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
#ifndef SAVE_H
#define SAVE_H
Length boardFileLength(File *file)
{
assertExpr(file);
Length len = {0};
int c;
while((c = fgetc(file)) != EOF && c != '\n')
len.x++;
printf("x: %i\n", len.x);
rewind(file);
while((c = fgetc(file)) != EOF){
len.y += c == '\n';
}
rewind(file);
printf("y: %i\n", len.y);
for(int y = 0; y < len.y; y++){
for(int x = 0; x < len.x; x++){
if((c = fgetc(file))=='\n'){
fclose(file);
panic("unexpected '\\n'. char pos: %i,%i", x, len.y);
}else if(c == EOF){
fclose(file);
panic("unexpected EOF. char pos: %i,%i", x, len.y);
}
}
if((c = fgetc(file)) != '\n'){
fclose(file);
panic("expected '\\n' at char pos: %i,%i", len.x, y);
}
}
if((c = fgetc(file)) != EOF){
fclose(file);
panic("Expected EOF at pos: %i,%i", len.x, len.y);
}
rewind(file);
printf("Board file len: %i,%i\n", len.x, len.y);
return len;
}
Tile tileUnpack(char c)
{
Tile tile = {0};
tile.isBomb = 1 & (c >> 7);
tile.num = 15 & (c >> 3);
tile.state = 7 & c;
return tile;
}
char tilePack(const Tile tile)
{
return tile.state | (tile.num << 3) | (tile.isBomb << 7);
}
File* saveNewFile(Board *board)
{
for(uint i = 0; i < 1000; i++){
char path[64] = {0};
sprintf(path, "./Saves/%u.swoop", i);
File *file = fopen(path, "r");
if(!file){
printf("Saving game to: \"%s\"\n", path);
File *file = fopen(path, "wb");
assertExpr(file);
board->saveNum = i;
return file;
}
fclose(file);
}
return NULL;
}
File* boardOpen(const uint saveNum, char *mode)
{
char buf[64] = {0};
sprintf(buf, "./Saves/%u.swoop", saveNum);
return fopen(buf, mode);
}
bool boardSave(Board *board, const bool newFile)
{
File *file = NULL;
if(!newFile)
file = boardOpen(board->saveNum, "wb");
if(newFile || !file)
file = saveNewFile(board);
for(int y = 0; y < board->len.y; y++){
for(int x = 0; x < board->len.x; x++)
fputc(tilePack(board->tile[x][y]), file);
if(fputc('\n', file) == EOF){
fclose(file);
return false;
}
}
fclose(file);
return true;
}
Board boardLoad(const uint saveNum)
{
File *file = boardOpen(saveNum, "rb");
assertExprMsg(file, "Could not open board: \"%u\"", saveNum);
Board board = {
.saveNum = saveNum,
.winLen = getWindowLen(),
.len = boardFileLength(file),
.scale = tileScale(board.winLen, board.len),
.off = tileOffset(board.winLen, board.len, board.scale),
.state = BS_PLAY,
.tile = boardTileAlloc(board.len),
};
int c;
for(int y = 0; y < board.len.y; y++){
for(int x = 0; x < board.len.x; x++){
if((c = fgetc(file))=='\n'){
fclose(file);
panic("unexpected '\\n'. char pos: %i,%i", x, y);
printf("Board file len: %i,%i\n", board.len.x, board.len.y);
}else if(c == EOF){
fclose(file);
panic("unexpected EOF. char pos: %i,%i", x, y);
printf("Board file len: %i,%i\n", board.len.x, board.len.y);
}
board.tile[x][y] = tileUnpack(c);
}
if((c = fgetc(file)) != '\n'){
fclose(file);
panic("Expected newline at end of line %i\n", y);
}
}
if((c = fgetc(file)) != EOF){
fclose(file);
panic("Expected EOF at end of line %i\n", board.len.y);
}
board.numBombs = boardNumBombs(&board);
printBoardInfo(&board);
printBoard(&board, true);
printBoard(&board, false);
return board;
}
#endif /* end of include guard: SAVE_H */