fix wrong score recording.
[kugel-rb.git] / apps / plugins / goban / game.c
blob1971a5b15c6481bc1f3bbd5d24acb17b5c543236
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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"
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
37 been autosaved yet */
39 int move_num = 0;
41 unsigned char current_player = BLACK;
43 struct header_t header; /* game metadata header info */
45 void
46 set_game_modified (void)
48 game_dirty = true;
49 autosave_dirty = true;
52 bool
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");
60 return false;
63 if (!rb->file_exists (filename))
65 DEBUGF ("file doesn't exist!\n");
66 return false;
69 pre_game_setup ();
71 rb->strcpy (save_file, filename);
73 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
74 rb->cpu_boost (true);
75 #endif
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.");
83 parse_failed = true;
86 game_dirty = false;
87 autosave_dirty = false;
89 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
90 rb->cpu_boost (false);
91 #endif
93 if (header.handicap >= 2)
95 current_player = WHITE;
98 post_game_setup_sgf ();
100 if (!parse_failed)
102 draw_screen_display();
103 if (rb->strcmp(filename, DEFAULT_SAVE))
105 metadata_summary();
109 return true;
113 bool
114 save_game (const char *filename)
116 if (rb->strlen (filename) + 1 > SAVE_FILE_LENGTH)
118 return false;
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.*/
126 if (!game_dirty &&
127 (rb->strcmp (filename, save_file) == 0 ||
128 rb->strcmp (filename, DEFAULT_SAVE) == 0))
130 return true;
133 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
134 rb->cpu_boost (true);
135 #endif
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
145 * loaded file)
147 if (rb->strcmp (filename, DEFAULT_SAVE) ||
148 rb->strcmp (save_file, DEFAULT_SAVE) == 0)
150 game_dirty = false;
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);
158 #endif
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);
168 return true;
170 else
172 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
173 rb->cpu_boost (false);
174 #endif
175 return false;
180 static void
181 pre_game_setup (void)
183 rb->memset (&header, 0, sizeof (header));
185 clear_caches_sgf ();
186 free_tree_sgf ();
188 set_size_board (MAX_BOARD_SIZE, MAX_BOARD_SIZE);
190 clear_board ();
192 game_dirty = true;
193 move_num = 0;
195 play_mode = MODE_PLAY;
197 rb->strcpy (save_file, DEFAULT_SAVE);
199 header_marked = false;
203 bool
204 setup_game (int width, int height, int handicap, int komi)
206 pre_game_setup ();
208 if (!set_size_board (width, height))
210 return false;
213 clear_board ();
215 /* place handicap */
216 if (handicap >= 2)
218 current_player = WHITE;
220 else if (handicap < 0)
222 return false;
224 else
226 current_player = BLACK;
229 header.handicap = handicap;
230 setup_handicap_sgf ();
232 header.komi = komi;
234 post_game_setup_sgf ();
236 return true;