summaryrefslogtreecommitdiff
path: root/apps/plugins/goban/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/goban/game.c')
-rw-r--r--apps/plugins/goban/game.c236
1 files changed, 236 insertions, 0 deletions
diff --git a/apps/plugins/goban/game.c b/apps/plugins/goban/game.c
new file mode 100644
index 0000000000..9ecf836f5b
--- /dev/null
+++ b/apps/plugins/goban/game.c
@@ -0,0 +1,236 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007-2009 Joshua Simmons <mud at majidejima dot com>
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "game.h"
23#include "types.h"
24#include "board.h"
25#include "goban.h"
26#include "sgf.h"
27#include "sgf_output.h"
28#include "sgf_parse.h"
29#include "sgf_storage.h"
30#include "display.h"
31
32static void pre_game_setup (void);
33
34char save_file[SAVE_FILE_LENGTH];
35bool game_dirty = false;
36bool autosave_dirty = false;
37
38int move_num = 0;
39
40unsigned char current_player = BLACK;
41
42struct header_t header;
43
44void
45set_game_modified (void)
46{
47 game_dirty = true;
48 autosave_dirty = true;
49}
50
51bool
52load_game (const char *filename)
53{
54 rb->memset (&header, 0, sizeof (header));
55
56 if (rb->strlen (filename) + 1 > SAVE_FILE_LENGTH)
57 {
58 DEBUGF ("file name too long\n");
59 return false;
60 }
61
62 if (!rb->file_exists (filename))
63 {
64 DEBUGF ("file doesn't exist!\n");
65 return false;
66 }
67
68 pre_game_setup ();
69
70 rb->strcpy (save_file, filename);
71
72#ifdef HAVE_ADJUSTABLE_CPU_FREQ
73 rb->cpu_boost (true);
74#endif
75
76 rb->splash (0, "Loading...");
77
78 bool parse_failed = false;
79 if (!parse_sgf (save_file))
80 {
81 rb->splash (3 * HZ, "Unable to parse SGF file. Will overwrite.");
82 parse_failed = true;
83 }
84
85 game_dirty = false;
86 autosave_dirty = false;
87
88#ifdef HAVE_ADJUSTABLE_CPU_FREQ
89 rb->cpu_boost (false);
90#endif
91
92 if (header.handicap >= 2)
93 {
94 current_player = WHITE;
95 }
96
97 post_game_setup_sgf ();
98
99 if (!parse_failed)
100 {
101 draw_screen_display();
102 if (rb->strcmp(filename, DEFAULT_SAVE))
103 {
104 metadata_summary();
105 }
106 }
107
108 return true;
109}
110
111
112bool
113save_game (const char *filename)
114{
115 if (rb->strlen (filename) + 1 > SAVE_FILE_LENGTH)
116 {
117 return false;
118 }
119
120 /* We only have to do something if the game is dirty, or we're being
121 asked to save to a different location than we loaded from.
122
123 If the game isn't dirty and we're being asked to save to default,
124 we also don't have to do anything.*/
125 if (!game_dirty &&
126 (rb->strcmp (filename, save_file) == 0 ||
127 rb->strcmp (filename, DEFAULT_SAVE) == 0))
128 {
129 return true;
130 }
131
132#ifdef HAVE_ADJUSTABLE_CPU_FREQ
133 rb->cpu_boost (true);
134#endif
135
136 rb->splash (0, "Saving...");
137
138 if (output_sgf (filename))
139 {
140 /* saving only "cleans" the game if it's not a save to default,
141 * or if our save_file is actually default
142 *
143 * (so autosaves won't prevent legitimate saves to a Save As or
144 * loaded file)
145 */
146 if (rb->strcmp (filename, DEFAULT_SAVE) ||
147 rb->strcmp (save_file, DEFAULT_SAVE) == 0)
148 {
149 game_dirty = false;
150 }
151
152 /* but saving anywhere means that autosave isn't dirty */
153 autosave_dirty = false;
154
155#ifdef HAVE_ADJUSTABLE_CPU_FREQ
156 rb->cpu_boost (false);
157#endif
158 /* The save succeeded. Now, if we saved to an actual file (not to the
159 * DEFAULT_SAVE), then we should delete the DEFAULT_SAVE file because
160 * the changes stored in it are no longer unsaved.
161 */
162 if (rb->strcmp (filename, DEFAULT_SAVE))
163 {
164 rb->remove(DEFAULT_SAVE);
165 }
166
167 return true;
168 }
169 else
170 {
171#ifdef HAVE_ADJUSTABLE_CPU_FREQ
172 rb->cpu_boost (false);
173#endif
174 return false;
175 }
176}
177
178
179static void
180pre_game_setup (void)
181{
182 rb->memset (&header, 0, sizeof (header));
183
184 clear_caches_sgf ();
185 free_tree_sgf ();
186
187 set_size_board (MAX_BOARD_SIZE, MAX_BOARD_SIZE);
188
189 clear_board ();
190
191 game_dirty = true;
192 move_num = 0;
193
194 play_mode = MODE_PLAY;
195
196 rb->strcpy (save_file, DEFAULT_SAVE);
197
198 header_marked = false;
199}
200
201
202bool
203setup_game (int width, int height, int handicap, int komi)
204{
205 pre_game_setup ();
206
207 if (!set_size_board (width, height))
208 {
209 return false;
210 }
211
212 clear_board ();
213
214 /* place handicap */
215 if (handicap >= 2)
216 {
217 current_player = WHITE;
218 }
219 else if (handicap < 0)
220 {
221 return false;
222 }
223 else
224 {
225 current_player = BLACK;
226 }
227
228 header.handicap = handicap;
229 setup_handicap_sgf ();
230
231 header.komi = komi;
232
233 post_game_setup_sgf ();
234
235 return true;
236}