Assume map compatibility by default if client's map version is 1
[neverball] / ball / st_save.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <string.h>
16 #include <ctype.h>
17
18 #include "gui.h"
19 #include "util.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "demo.h"
23 #include "progress.h"
24 #include "text.h"
25
26 #include "game_common.h"
27
28 #include "st_shared.h"
29 #include "st_save.h"
30
31 extern struct state st_save;
32 extern struct state st_clobber;
33
34 static char filename[MAXSTR];
35
36 /*---------------------------------------------------------------------------*/
37
38 static struct state *ok_state;
39 static struct state *cancel_state;
40
41 int goto_save(struct state *ok, struct state *cancel)
42 {
43     char fmt[MAXSTR] = "";
44     const char *name;
45
46     config_get_s(CONFIG_REPLAY_NAME, fmt, sizeof (fmt) - 1);
47     name = demo_format_name(fmt, set_id(curr_set()), level_name(curr_level()));
48
49     strncpy(filename, name, sizeof (filename) - 1);
50
51     ok_state     = ok;
52     cancel_state = cancel;
53
54     return goto_state(&st_save);
55 }
56
57 /*---------------------------------------------------------------------------*/
58
59 static int file_id;
60
61 #define SAVE_SAVE   -1
62 #define SAVE_CANCEL -2
63
64 static int save_action(int i)
65 {
66     char *n;
67
68     audio_play(AUD_MENU, 1.0f);
69
70     switch (i)
71     {
72     case SAVE_SAVE:
73         n = text_to_locale(filename);
74
75         if (strlen(n) == 0)
76             return 1;
77
78         if (demo_exists(n))
79             return goto_state(&st_clobber);
80         else
81         {
82             demo_rename(n);
83             return goto_state(ok_state);
84         }
85
86     case SAVE_CANCEL:
87         return goto_state(cancel_state);
88
89     case GUI_CL:
90         gui_keyboard_lock();
91         break;
92
93     case GUI_BS:
94         if (text_del_char(filename))
95             gui_set_label(file_id, filename);
96         break;
97
98     default:
99         if (text_add_char(i, filename, sizeof (filename) - 1,
100                                        sizeof (filename) - 1))
101             gui_set_label(file_id, filename);
102     }
103     return 1;
104 }
105
106 static int enter_id;
107
108 static int save_enter(void)
109 {
110     int id, jd;
111
112     if ((id = gui_vstack(0)))
113     {
114         gui_label(id, _("Replay Name"), GUI_MED, GUI_ALL, 0, 0);
115         gui_space(id);
116
117         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
118
119         gui_space(id);
120         gui_keyboard(id);
121         gui_space(id);
122
123         if ((jd = gui_harray(id)))
124         {
125             enter_id = gui_start(jd, _("Save"), GUI_SML, SAVE_SAVE, 0);
126             gui_space(jd);
127             gui_state(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
128         }
129
130         gui_layout(id, 0, 0);
131     }
132
133     SDL_EnableUNICODE(1);
134
135     return id;
136 }
137
138 static void save_leave(int id)
139 {
140     SDL_EnableUNICODE(0);
141     gui_delete(id);
142 }
143
144 static int save_keybd(int c, int d)
145 {
146     if (d)
147     {
148         gui_focus(enter_id);
149
150         if (c == '\b' || c == 0x7F)
151             return save_action(GUI_BS);
152         if (c >= ' ')
153             return save_action(c);
154     }
155     return 1;
156 }
157
158 static int save_buttn(int b, int d)
159 {
160     if (d)
161     {
162         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
163         {
164             int c = gui_token(gui_click());
165
166             if (c >= 0 && !GUI_ISMSK(c))
167                 return save_action(gui_keyboard_char(c));
168             else
169                 return save_action(c);
170         }
171         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
172             return save_action(SAVE_CANCEL);
173     }
174     return 1;
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 static int clobber_action(int i)
180 {
181     audio_play(AUD_MENU, 1.0f);
182
183     if (i == SAVE_SAVE)
184     {
185         demo_rename(text_to_locale(filename));
186         return goto_state(ok_state);
187     }
188     return goto_state(&st_save);
189 }
190
191 static int clobber_enter(void)
192 {
193     int id, jd, kd;
194
195     if ((id = gui_vstack(0)))
196     {
197         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
198
199         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
200
201         if ((jd = gui_harray(id)))
202         {
203             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
204             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
205         }
206
207         gui_pulse(kd, 1.2f);
208         gui_layout(id, 0, 0);
209     }
210
211     return id;
212 }
213
214 static int clobber_buttn(int b, int d)
215 {
216     if (d)
217     {
218         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
219             return clobber_action(gui_token(gui_click()));
220         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
221             return clobber_action(SAVE_CANCEL);
222     }
223     return 1;
224 }
225
226 /*---------------------------------------------------------------------------*/
227
228 struct state st_save = {
229     save_enter,
230     save_leave,
231     shared_paint,
232     shared_timer,
233     shared_point,
234     shared_stick,
235     shared_angle,
236     shared_click,
237     save_keybd,
238     save_buttn,
239     1, 0
240 };
241
242 struct state st_clobber = {
243     clobber_enter,
244     shared_leave,
245     shared_paint,
246     shared_timer,
247     shared_point,
248     shared_stick,
249     shared_angle,
250     shared_click,
251     NULL,
252     clobber_buttn,
253     1, 0
254 };