-
Notifications
You must be signed in to change notification settings - Fork 45
/
savestate.c
324 lines (269 loc) · 9.09 KB
/
savestate.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
/***************************************************************
savestate.c
The file contains a savestate system so that the game can be
loaded if it crashes
***************************************************************/
#include <libdragon.h>
#include "core.h"
#include "minigame.h"
#include "results.h"
#include "savestate.h"
/*********************************
Structures
*********************************/
typedef struct {
char header[4];
uint32_t blacklist;
uint8_t crashedflag;
uint8_t aidiff;
uint8_t pointstowin;
uint8_t nextplaystyle;
uint8_t playerconts[MAXPLAYERS];
uint8_t points[MAXPLAYERS];
uint8_t chooser;
uint8_t curgame;
uint8_t checksum;
} GameSave;
/*********************************
Function Prototypes
*********************************/
static bool controller_isleft();
static bool controller_isright();
static bool controller_isa();
/*********************************
Globals
*********************************/
static int global_selection;
static uint8_t global_cansave;
static GameSave global_gamesave;
static rdpq_font_t* global_font;
/*==============================
calc_checksum
Calculate a basic checksum for the save state
This is done by just adding all the bytes together
@return The checksum
==============================*/
static uint8_t calc_checksum()
{
uint8_t checksum = 0;
uint8_t* asarray = (uint8_t*)&global_gamesave;
for (int i=0; i<sizeof(GameSave); i++)
checksum += asarray[i];
return checksum;
}
/*==============================
savestate_test
Test that EEPROM is present to save a game state to
@return Whether EEPROM is present
==============================*/
bool savestate_initialize()
{
global_cansave = 0;
// Test for EEPROM
if (eeprom_present() == EEPROM_NONE)
return false;
global_cansave = 1;
// Read the savestate from EEPROM
eeprom_read_bytes((uint8_t*)(&global_gamesave), 0, sizeof(GameSave));
// If the EEPROM hasn't been initialized before, do so now
if (strncmp(global_gamesave.header, "NBGJ", 4) != 0)
{
memset(&global_gamesave, 0, sizeof(GameSave));
global_gamesave.header[0] = 'N';
global_gamesave.header[1] = 'B';
global_gamesave.header[2] = 'G';
global_gamesave.header[3] = 'J';
global_gamesave.checksum = calc_checksum();
}
// Success
return true;
}
/*==============================
savestate_checkcrashed
Check if the game recently crashed
@return Whether the game recently crashed
==============================*/
bool savestate_checkcrashed()
{
return global_gamesave.crashedflag;
}
/*==============================
savestate_save
Save the current game state to EEPROM
@param Whether to only save the game config as opposed to game state
==============================*/
void savestate_save(bool configonly)
{
if (!global_cansave)
return;
// Grab the game state
if (!configonly)
{
bool playerconts[MAXPLAYERS];
global_gamesave.crashedflag = 1;
core_get_playerconts(playerconts);
for (int i=0; i<MAXPLAYERS; i++)
global_gamesave.playerconts[i] = playerconts[i];
global_gamesave.aidiff = core_get_aidifficulty();
global_gamesave.pointstowin = results_get_points_to_win();
for (int i=0; i<MAXPLAYERS; i++)
global_gamesave.points[i] = results_get_points(PLAYER_1+i);
global_gamesave.nextplaystyle = core_get_nextround();
global_gamesave.chooser = core_get_curchooser();
global_gamesave.curgame = minigame_get_index();
global_gamesave.checksum = calc_checksum();
}
// Save to EEPROM
eeprom_write_bytes((uint8_t*)(&global_gamesave), 0, sizeof(GameSave));
}
/*==============================
savestate_load
Load the game state saved in EEPROM
==============================*/
void savestate_load()
{
bool playerconts[MAXPLAYERS];
if (!global_cansave)
return;
// Recover the game state
for (int i=0; i<MAXPLAYERS; i++)
playerconts[i] = global_gamesave.playerconts[i];
core_set_playercount(playerconts);
core_set_aidifficulty(global_gamesave.aidiff);
results_set_points_to_win(global_gamesave.pointstowin);
results_set_points(PLAYER_1, global_gamesave.points[0]);
results_set_points(PLAYER_2, global_gamesave.points[1]);
results_set_points(PLAYER_3, global_gamesave.points[2]);
results_set_points(PLAYER_4, global_gamesave.points[3]);
core_set_nextround(global_gamesave.nextplaystyle);
core_set_curchooser(global_gamesave.chooser);
minigame_loadnext(global_minigame_list[global_gamesave.curgame].internalname);
}
/*==============================
savestate_clear
Clear the game state saved in EEPROM
==============================*/
void savestate_clear()
{
if (!global_cansave)
return;
global_gamesave.crashedflag = 0;
eeprom_write_bytes((uint8_t*)(&global_gamesave), 0, sizeof(GameSave));
}
void savestate_setblacklist(bool* list)
{
uint32_t bitfield = 0;
for (int i=0; i<global_minigame_count; i++)
bitfield |= (list[i] & 0x01) << i;
global_gamesave.blacklist = bitfield;
}
void savestate_getblacklist(bool* list)
{
for (int i=0; i<global_minigame_count; i++)
list[i] = (global_gamesave.blacklist >> i) & 0x01;
}
/*=============================================================
=============================================================*/
void loadsave_init()
{
display_init(RESOLUTION_320x240, DEPTH_16_BPP, 3, GAMMA_NONE, FILTERS_RESAMPLE);
global_font = rdpq_font_load("rom:/squarewave_l.font64");
rdpq_text_register_font(1, global_font);
rdpq_font_style(global_font, 1, &(rdpq_fontstyle_t){.color = RGBA32(255, 255, 255, 255)});
rdpq_font_style(global_font, 2, &(rdpq_fontstyle_t){.color = RGBA32(148, 145, 8, 255)});
global_selection = 0;
if (!savestate_checkcrashed() && global_cansave)
core_level_changeto(LEVEL_MAINMENU);
}
void loadsave_loop(float deltatime)
{
surface_t* disp;
int maxselection = 2;
if (!savestate_checkcrashed() && global_cansave)
return;
if (!global_cansave)
maxselection = 1;
// Handle controls
if (controller_isleft())
{
global_selection++;
if (global_selection > maxselection-1)
global_selection = 0;
}
else if (controller_isright())
{
global_selection--;
if (global_selection < 0)
global_selection = maxselection-1;
}
else if (controller_isa())
{
if (savestate_checkcrashed() && global_selection == 0)
{
savestate_load();
core_level_changeto(LEVEL_MINIGAME);
}
else
{
if (savestate_checkcrashed())
savestate_clear();
core_level_changeto(LEVEL_MAINMENU);
}
}
// Get a framebuffer
disp = display_get();
rdpq_attach(disp, NULL);
// Black background
rdpq_set_mode_standard();
rdpq_mode_combiner(RDPQ_COMBINER_FLAT);
rdpq_mode_blender(RDPQ_BLENDER_MULTIPLY);
rdpq_set_prim_color(RGBA32(0, 0, 0, 255));
rdpq_fill_rectangle(0, 0, 320, 240);
// Render text
if (savestate_checkcrashed())
{
rdpq_text_print(&(rdpq_textparms_t){.width=320, .align=ALIGN_CENTER, .style_id=1}, 1, 0, 240/2-32, "A crash was detected.\nWould you like to restore the save?");
rdpq_text_print(&(rdpq_textparms_t){.style_id=((global_selection == 0) ? 2 : 1)}, 1, 320/2-64, 240/2+32, "Yes");
rdpq_text_print(&(rdpq_textparms_t){.style_id=((global_selection == 1) ? 2 : 1)}, 1, 320/2+64, 240/2+32, "No");
}
else
{
rdpq_text_print(&(rdpq_textparms_t){.width=320, .align=ALIGN_CENTER, .style_id=1}, 1, 0, 240/2-48, "EEPROM save was not detected.\n\nIf the game crashes, you will\nnot be able to restore it.");
rdpq_text_print(&(rdpq_textparms_t){.width=320, .align=ALIGN_CENTER, .style_id=((global_selection == 0) ? 2 : 1)}, 1, 0, 240/2+64, "Ok");
}
// Done
rdpq_detach_show();
}
void loadsave_cleanup()
{
rdpq_text_unregister_font(1);
rdpq_font_free(global_font);
display_close();
}
static bool controller_isleft()
{
for (int i=0; i<MAXPLAYERS; i++)
{
joypad_inputs_t stick = joypad_get_inputs(i);
if (joypad_get_buttons_pressed(i).c_left || joypad_get_buttons_pressed(i).d_left || (joypad_get_axis_pressed(i, JOYPAD_AXIS_STICK_X) == -1 && stick.stick_x < -20))
return true;
}
return false;
}
static bool controller_isright()
{
for (int i=0; i<MAXPLAYERS; i++)
{
joypad_inputs_t stick = joypad_get_inputs(i);
if (joypad_get_buttons_pressed(i).c_right || joypad_get_buttons_pressed(i).d_right || (joypad_get_axis_pressed(i, JOYPAD_AXIS_STICK_X) == 1 && stick.stick_x > 20))
return true;
}
return false;
}
static bool controller_isa()
{
for (int i=0; i<MAXPLAYERS; i++)
if (joypad_get_buttons_pressed(i).a)
return true;
return false;
}