-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.c
More file actions
239 lines (197 loc) · 8.13 KB
/
Copy pathfile.c
File metadata and controls
239 lines (197 loc) · 8.13 KB
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
#include "file.h"
#define SCORES_PATH "../scores.txt"
#define MIN_BOARD_WIDTH 3 //positions
#define MIN_BOARD_HEIGHT 3 //positions
void MoveScore(score_t *scoreDest, score_t *scoreSrc) {
strcpy(scoreDest->name, scoreSrc->name);
scoreDest->score = scoreSrc->score;
}
void NewScore(score_t *scoreDest, const char *name, int score) {
strcpy(scoreDest->name, name);
scoreDest->score = score;
}
score_t *GetScores() {
FILE *file = fopen(SCORES_PATH, "r");
score_t *scores = malloc(sizeof(score_t) * 4); //3 HighScores from file and 1 for new score
for (int i = 0; i < 4; i++)
scores[i].name = malloc(sizeof(char) * 3);
if (file == NULL) {
SDL_LogError(0, "Scores not found!");
return scores;
}
for (int i = 0; i < 3; i++) {
if (feof(file)) NewScore(&scores[i], "___", 0);
else
fscanf(file, "%s %d\n", scores[i].name, &scores[i].score);
}
fclose(file);
NewScore(&scores[3], "NEW", -99); //Prepared for handling of new scores
return scores;
}
void UpdateScores(score_t *scores) {
int score = scores[3].score;
char *name = scores[3].name;
if (score >= scores[0].score) {
MoveScore(&scores[2], &scores[1]);
MoveScore(&scores[1], &scores[0]);
NewScore(&scores[0], name, score);
} else if (score >= scores[1].score) {
MoveScore(&scores[2], &scores[1]);
NewScore(&scores[1], name, score);
} else
NewScore(&scores[2], name, score);
FILE *file = fopen(SCORES_PATH, "w");
if (file == NULL) {
SDL_LogError(0, "Scores not found!");
return;
}
for (int i = 0; i < 3 && scores[i].score != -99; i++)
fprintf(file, "%s %d\n", scores[i].name, scores[i].score);
fclose(file);
NewScore(&scores[3], "", -99); //Cleared after the new score is handled
}
config_t GetConfig(char *path) {
config_t config = {
1280, 720, 1, 3, 1,
0.1, 10000, 1, 10000, 5, 25,
0.3, 3, 4, 0};
FILE *file = fopen(path, "r");
if (file == NULL) {
SDL_LogError(0, "Config not found!");
return config;
}
while (!feof(file)) {
char name[100];
fscanf(file, "%s", name);
if (!strcmp(name, "BOARD_WIDTH"))
fscanf(file, "%d", &config.width);
else if (!strcmp(name, "BOARD_HEIGHT"))
fscanf(file, "%d", &config.height);
else if (!strcmp(name, "START_SPEED"))
fscanf(file, "%d", &config.start_speed);
else if (!strcmp(name, "START_LENGTH"))
fscanf(file, "%d", &config.start_length);
else if (!strcmp(name, "MAX_SPEED"))
fscanf(file, "%d", &config.max_speed);
else if (!strcmp(name, "APPLE_VALUE"))
fscanf(file, "%d", &config.apple_value);
else if (!strcmp(name, "ORANGE_DELAY"))
fscanf(file, "%d", &config.orange_delay);
else if (!strcmp(name, "ORANGE_VALUE"))
fscanf(file, "%d", &config.orange_value);
else if (!strcmp(name, "ORANGE_CHANCE"))
fscanf(file, "%d", &config.orange_chance);
else if (!strcmp(name, "ACCELERATION"))
fscanf(file, "%f", &config.acceleration);
else if (!strcmp(name, "ACCELERATION_INTERVAL"))
fscanf(file, "%d", &config.acceleration_interval);
else if (!strcmp(name, "BONUS_SLOW_DOWN"))
fscanf(file, "%f", &config.bonus_slow_down);
else if (!strcmp(name, "BONUS_SHORTEN"))
fscanf(file, "%d", &config.bonus_shorten);
else if (!strcmp(name, "FRUIT_MODE"))
fscanf(file, "%d", &config.fruit_mode);
else if (!strcmp(name, "PORTAL_COUNT"))
fscanf(file, "%d", &config.portal_count);
}
fclose(file);
config.width = config.width < MIN_BOARD_WIDTH ? MIN_BOARD_WIDTH : config.width;
config.height = config.height < MIN_BOARD_HEIGHT ? MIN_BOARD_HEIGHT : config.height;
config.acceleration = 1 - config.acceleration;
config.bonus_slow_down = 1 + config.bonus_slow_down;
return config;
}
void SaveGame(game_t game) {
FILE *file = fopen("../qsave.txt", "w");
char *text[100];
config_t config = game.config;
sprintf(text, "%d %d\n", config.width, config.height);
fputs(text, file);
sprintf(text, "%d\n", config.max_speed);
fputs(text, file);
sprintf(text, "%f %d\n", config.acceleration, config.acceleration_interval);
fputs(text, file);
sprintf(text, "%d\n", config.apple_value);
fputs(text, file);
sprintf(text, "%d %d %d\n", config.orange_value, config.orange_delay, config.orange_chance);
fputs(text, file);
sprintf(text, "%f %d\n", config.bonus_slow_down, config.bonus_shorten);
fputs(text, file);
sprintf(text, "%d %d\n", config.portal_count, config.fruit_mode);
fputs(text, file);
sprintf(text, "%d %d %d %d\n", game.area.x, game.area.y, game.area.w, game.area.h);
fputs(text, file);
clock_t clock = game.clock;
sprintf(text, "%d %d\n", clock.delta, clock.move);
fputs(text, file);
sprintf(text, "%d %d\n", clock.game, clock.acceleration);
fputs(text, file);
sprintf(text, "%d %d\n", clock.animation, clock.orange);
fputs(text, file);
sprintf(text, "%d\n", game.score);
fputs(text, file);
snake_t snake = game.snake;
sprintf(text, "%d %d %d\n", snake.length, snake.direction, snake.speed);
fputs(text, file);
int amount = 2 + game.config.portal_count * 2 + game.snake.length;
for (int i = 0; i < amount; i++) {
sprintf(text, "%d %d\n", game.objectPos[i].x, game.objectPos[i].y);
fputs(text, file);
}
fclose(file);
}
void LoadGame(game_t *game) {
FILE *file = fopen("../qsave.txt", "r");
if (file == NULL)
return;
if (!game->automatic)
game->state = LOAD;
config_t *config = &game->config;
int oldBoardW = config->width;
int oldBoardH = config->height;
fscanf(file, "%d %d\n", &config->width, &config->height);
fscanf(file, "%d\n", &config->max_speed);
fscanf(file, "%f %d\n", &config->acceleration, &config->acceleration_interval);
fscanf(file, "%d\n", &config->apple_value);
fscanf(file, "%d %d %d\n", &config->orange_value, &config->orange_delay, &config->orange_chance);
fscanf(file, "%f %d\n", &config->bonus_slow_down, &config->bonus_shorten);
fscanf(file, "%d %d\n", &config->portal_count, &config->fruit_mode);
fscanf(file, "%d %d %d %d\n", &game->area.x, &game->area.y, &game->area.w, &game->area.h);
clock_t *clock = &game->clock;
fscanf(file, "%d %d\n", &clock->delta, &clock->move);
fscanf(file, "%d %d\n", &clock->game, &clock->acceleration);
fscanf(file, "%d %d\n", &clock->animation, &clock->orange);
game->clock.notification = 0;
fscanf(file, "%d\n", &game->score);
free(game->objectPos);
int objectCount = 2 + config->portal_count * 2;
game->objectPos = malloc(sizeof(point_t) * (objectCount + config->width * config->height));
for (int i = 0; i < 2 + objectCount; i++) {
game->object[i].pos = &game->objectPos[i];
game->object[i].show = 0;
}
snake_t *snake = &game->snake;
snake->pos = &game->objectPos[2 + config->portal_count * 2];
//Clear snake positions
for (int i = 0; i < config->width * config->height; i++)
snake->pos[i] = (point_t) {NULL_POS, NULL_POS};
fscanf(file, "%d %d %d\n", &snake->length, &snake->direction, &snake->speed);
snake->change_direction = 0;
for (int i = 0; i < objectCount; i++)
fscanf(file, "%d %d\n", &game->objectPos[i].x, &game->objectPos[i].y);
for (int i = 0; i < snake->length; i++)
fscanf(file, "%d %d\n", &snake->pos[i].x, &snake->pos[i].y);
if (game->object[ORANGE].pos->x != -99)
game->object[ORANGE].show = 1;
else
game->object[ORANGE].show = 0;
// if (config->width != oldBoardW) { TODO change window size and board size when loading from different game
// if (config->width > MAX_SMALL_BOARD_WIDTH){
//
// } else if (oldBoardW > MAX_SMALL_BOARD_WIDTH && config->width < MAX_SMALL_BOARD_WIDTH){
// SDL_SetWindowSize(game->window, 700, 700);
// }
// }
// if (config->height != oldBoardH) {}
fclose(file);
}