1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007-2009 Joshua Simmons <mud at majidejima dot com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
27 #include "sgf_output.h"
28 #include "sgf_parse.h"
29 #include "sgf_storage.h"
32 static void pre_game_setup (void);
34 char save_file
[SAVE_FILE_LENGTH
];
35 bool game_dirty
= false; /* flag for unsaved changes */
36 bool autosave_dirty
= false; /* flag for unsaved changes which haven't even
41 unsigned char current_player
= BLACK
;
43 struct header_t header
; /* game metadata header info */
46 set_game_modified (void)
49 autosave_dirty
= true;
53 load_game (const char *filename
)
55 rb
->memset (&header
, 0, sizeof (header
));
57 if (rb
->strlen (filename
) + 1 > SAVE_FILE_LENGTH
)
59 DEBUGF ("file name too long\n");
63 if (!rb
->file_exists (filename
))
65 DEBUGF ("file doesn't exist!\n");
71 rb
->strcpy (save_file
, filename
);
73 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
77 rb
->splash (0, "Loading...");
79 bool parse_failed
= false;
80 if (!parse_sgf (save_file
))
82 rb
->splash (3 * HZ
, "Unable to parse SGF file. Will overwrite.");
87 autosave_dirty
= false;
89 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
90 rb
->cpu_boost (false);
93 if (header
.handicap
>= 2)
95 current_player
= WHITE
;
98 post_game_setup_sgf ();
102 draw_screen_display();
103 if (rb
->strcmp(filename
, DEFAULT_SAVE
))
114 save_game (const char *filename
)
116 if (rb
->strlen (filename
) + 1 > SAVE_FILE_LENGTH
)
121 /* We only have to do something if the game is dirty, or we're being
122 asked to save to a different location than we loaded from.
124 If the game isn't dirty and we're being asked to save to default,
125 we also don't have to do anything.*/
127 (rb
->strcmp (filename
, save_file
) == 0 ||
128 rb
->strcmp (filename
, DEFAULT_SAVE
) == 0))
133 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
134 rb
->cpu_boost (true);
137 rb
->splash (0, "Saving...");
139 if (output_sgf (filename
))
141 /* saving only "cleans" the game if it's not a save to default,
142 * or if our save_file is actually default
144 * (so autosaves won't prevent legitimate saves to a Save As or
147 if (rb
->strcmp (filename
, DEFAULT_SAVE
) ||
148 rb
->strcmp (save_file
, DEFAULT_SAVE
) == 0)
153 /* but saving anywhere means that autosave isn't dirty */
154 autosave_dirty
= false;
156 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
157 rb
->cpu_boost (false);
159 /* The save succeeded. Now, if we saved to an actual file (not to the
160 * DEFAULT_SAVE), then we should delete the DEFAULT_SAVE file because
161 * the changes stored in it are no longer unsaved.
163 if (rb
->strcmp (filename
, DEFAULT_SAVE
))
165 rb
->remove(DEFAULT_SAVE
);
172 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
173 rb
->cpu_boost (false);
181 pre_game_setup (void)
183 rb
->memset (&header
, 0, sizeof (header
));
188 set_size_board (MAX_BOARD_SIZE
, MAX_BOARD_SIZE
);
195 play_mode
= MODE_PLAY
;
197 rb
->strcpy (save_file
, DEFAULT_SAVE
);
199 header_marked
= false;
204 setup_game (int width
, int height
, int handicap
, int komi
)
208 if (!set_size_board (width
, height
))
218 current_player
= WHITE
;
220 else if (handicap
< 0)
226 current_player
= BLACK
;
229 header
.handicap
= handicap
;
230 setup_handicap_sgf ();
234 post_game_setup_sgf ();