added asking for and uploading tapes to score server (only once)
[rocksndiamonds.git] / src / libgame / setup.h
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // setup.h
10 // ============================================================================
11
12 #ifndef SETUP_H
13 #define SETUP_H
14
15 #include "system.h"
16 #include "hash.h"
17
18
19 // values for setup file handling
20 #define TYPE_BOOLEAN                    (1 << 0)
21 #define TYPE_SWITCH                     (1 << 1)
22 #define TYPE_SWITCH3                    (1 << 2)
23 #define TYPE_YES_NO                     (1 << 3)
24 #define TYPE_YES_NO_AUTO                (1 << 4)
25 #define TYPE_ECS_AGA                    (1 << 5)
26 #define TYPE_KEY                        (1 << 6)
27 #define TYPE_KEY_X11                    (1 << 7)
28 #define TYPE_INTEGER                    (1 << 8)
29 #define TYPE_STRING                     (1 << 9)
30 #define TYPE_PLAYER                     (1 << 10)
31 #define TYPE_ELEMENT                    (1 << 11)
32 #define TYPE_GRAPHIC                    (1 << 12)
33
34 // additional values for setup screen
35 #define TYPE_ENTER_SCREEN               (1 << 13)
36 #define TYPE_LEAVE_SCREEN               (1 << 14)
37 #define TYPE_ENTER_MENU                 (1 << 15)
38 #define TYPE_LEAVE_MENU                 (1 << 16)
39 #define TYPE_ENTER_LIST                 (1 << 17)
40 #define TYPE_LEAVE_LIST                 (1 << 18)
41 #define TYPE_TEXT_INPUT                 (1 << 19)
42 #define TYPE_EMPTY                      (1 << 20)
43 #define TYPE_SKIPPABLE                  (1 << 21)
44 #define TYPE_KEYTEXT                    (1 << 22)
45 #define TYPE_HEADLINE                   (1 << 23)
46
47 #define TYPE_GHOSTED                    (1 << 24)
48 #define TYPE_QUERY                      (1 << 25)
49
50 // additional values for internal purposes
51 #define TYPE_BITFIELD                   (1 << 26)
52 #define TYPE_CONTENT                    (1 << 27)
53 #define TYPE_ELEMENT_LIST               (1 << 28)
54 #define TYPE_CONTENT_LIST               (1 << 29)
55
56 // derived values for setup file handling
57 #define TYPE_BOOLEAN_STYLE              (TYPE_BOOLEAN | \
58                                          TYPE_SWITCH  | \
59                                          TYPE_YES_NO  | \
60                                          TYPE_ECS_AGA )
61
62 // derived values for setup screen
63 #define TYPE_VALUE                      (TYPE_BOOLEAN_STYLE     | \
64                                          TYPE_SWITCH3           | \
65                                          TYPE_YES_NO_AUTO       | \
66                                          TYPE_KEY               | \
67                                          TYPE_KEY_X11           | \
68                                          TYPE_INTEGER           | \
69                                          TYPE_STRING            | \
70                                          TYPE_PLAYER            | \
71                                          TYPE_ELEMENT           | \
72                                          TYPE_GRAPHIC)
73
74 #define TYPE_SKIP_ENTRY                 (TYPE_EMPTY             | \
75                                          TYPE_SKIPPABLE         | \
76                                          TYPE_KEY               | \
77                                          TYPE_STRING            | \
78                                          TYPE_HEADLINE          | \
79                                          TYPE_GHOSTED)
80
81 #define TYPE_ENTER                      (TYPE_ENTER_SCREEN      | \
82                                          TYPE_ENTER_MENU        | \
83                                          TYPE_ENTER_LIST)
84
85 #define TYPE_LEAVE                      (TYPE_LEAVE_SCREEN      | \
86                                          TYPE_LEAVE_MENU        | \
87                                          TYPE_LEAVE_LIST)
88
89 #define TYPE_ENTER_OR_LEAVE             (TYPE_ENTER | TYPE_LEAVE)
90
91 struct TokenInfo
92 {
93   int type;
94   void *value;
95   char *text;
96 };
97
98 // some definitions for list and hash handling
99 typedef struct SetupFileList SetupFileList;
100 typedef struct hashtable     SetupFileHash;
101
102 #define BEGIN_HASH_ITERATION(hash, itr)                         \
103   if (hash != NULL && hashtable_count(hash) > 0)                \
104   {                                                             \
105     struct hashtable_itr *itr = hashtable_iterator(hash);       \
106     do {                                                        \
107
108 #define HASH_ITERATION_TOKEN(itr)       ((char *)hashtable_iterator_key(itr))
109 #define HASH_ITERATION_VALUE(itr)       ((char *)hashtable_iterator_value(itr))
110
111 #define END_HASH_ITERATION(hash, itr)                           \
112     } while (hashtable_iterator_advance(itr));                  \
113     free(itr);                                                  \
114   }                                                             \
115
116
117 // sort priorities of special tree entries
118 #define LEVELCLASS_TOP                  0
119 #define LEVELCLASS_PARENT               1
120 #define LEVELCLASS_LAST_PLAYED_LEVEL    2
121
122 // sort priorities of level series (also used as level series classes)
123 #define LEVELCLASS_TUTORIAL_START       10
124 #define LEVELCLASS_TUTORIAL_END         99
125 #define LEVELCLASS_CLASSICS_START       100
126 #define LEVELCLASS_CLASSICS_END         199
127 #define LEVELCLASS_CONTRIB_START        200
128 #define LEVELCLASS_CONTRIB_END          299
129 #define LEVELCLASS_PRIVATE_START        300
130 #define LEVELCLASS_PRIVATE_END          399
131 #define LEVELCLASS_BD_START             400
132 #define LEVELCLASS_BD_END               499
133 #define LEVELCLASS_EM_START             500
134 #define LEVELCLASS_EM_END               599
135 #define LEVELCLASS_SP_START             600
136 #define LEVELCLASS_SP_END               699
137 #define LEVELCLASS_DX_START             700
138 #define LEVELCLASS_DX_END               799
139 #define LEVELCLASS_SB_START             800
140 #define LEVELCLASS_SB_END               899
141
142 #define LEVELCLASS_PREDEFINED_START     LEVELCLASS_TUTORIAL_START
143 #define LEVELCLASS_PREDEFINED_END       LEVELCLASS_SB_END
144
145 #define LEVELCLASS_TUTORIAL             LEVELCLASS_TUTORIAL_START
146 #define LEVELCLASS_CLASSICS             LEVELCLASS_CLASSICS_START
147 #define LEVELCLASS_CONTRIB              LEVELCLASS_CONTRIB_START
148 #define LEVELCLASS_PRIVATE              LEVELCLASS_PRIVATE_START
149 #define LEVELCLASS_BD                   LEVELCLASS_BD_START
150 #define LEVELCLASS_EM                   LEVELCLASS_EM_START
151 #define LEVELCLASS_SP                   LEVELCLASS_SP_START
152 #define LEVELCLASS_DX                   LEVELCLASS_DX_START
153 #define LEVELCLASS_SB                   LEVELCLASS_SB_START
154
155 #define LEVELCLASS_UNDEFINED            999
156
157 #define IS_LEVELCLASS_TUTORIAL(p)                                       \
158         ((p)->sort_priority >= LEVELCLASS_TUTORIAL_START &&             \
159          (p)->sort_priority <= LEVELCLASS_TUTORIAL_END)
160 #define IS_LEVELCLASS_CLASSICS(p)                                       \
161         ((p)->sort_priority >= LEVELCLASS_CLASSICS_START &&             \
162          (p)->sort_priority <= LEVELCLASS_CLASSICS_END)
163 #define IS_LEVELCLASS_CONTRIB(p)                                        \
164         ((p)->sort_priority >= LEVELCLASS_CONTRIB_START &&              \
165          (p)->sort_priority <= LEVELCLASS_CONTRIB_END)
166 #define IS_LEVELCLASS_PRIVATE(p)                                        \
167         ((p)->sort_priority >= LEVELCLASS_PRIVATE_START &&              \
168          (p)->sort_priority <= LEVELCLASS_PRIVATE_END)
169 #define IS_LEVELCLASS_BD(p)                                             \
170         ((p)->sort_priority >= LEVELCLASS_BD_START &&                   \
171          (p)->sort_priority <= LEVELCLASS_BD_END)
172 #define IS_LEVELCLASS_EM(p)                                             \
173         ((p)->sort_priority >= LEVELCLASS_EM_START &&                   \
174          (p)->sort_priority <= LEVELCLASS_EM_END)
175 #define IS_LEVELCLASS_SP(p)                                             \
176         ((p)->sort_priority >= LEVELCLASS_SP_START &&                   \
177          (p)->sort_priority <= LEVELCLASS_SP_END)
178 #define IS_LEVELCLASS_DX(p)                                             \
179         ((p)->sort_priority >= LEVELCLASS_DX_START &&                   \
180          (p)->sort_priority <= LEVELCLASS_DX_END)
181 #define IS_LEVELCLASS_SB(p)                                             \
182         ((p)->sort_priority >= LEVELCLASS_SB_START &&                   \
183          (p)->sort_priority <= LEVELCLASS_SB_END)
184 #define IS_LEVELCLASS_UNDEFINED(p)                                      \
185         ((p)->sort_priority < LEVELCLASS_PREDEFINED_START ||            \
186          (p)->sort_priority > LEVELCLASS_PREDEFINED_END)
187
188 #define LEVELCLASS(n)   (IS_LEVELCLASS_TUTORIAL(n) ? LEVELCLASS_TUTORIAL : \
189                          IS_LEVELCLASS_CLASSICS(n) ? LEVELCLASS_CLASSICS : \
190                          IS_LEVELCLASS_CONTRIB(n) ? LEVELCLASS_CONTRIB :   \
191                          IS_LEVELCLASS_PRIVATE(n) ? LEVELCLASS_PRIVATE :   \
192                          IS_LEVELCLASS_BD(n) ? LEVELCLASS_BD :             \
193                          IS_LEVELCLASS_EM(n) ? LEVELCLASS_EM :             \
194                          IS_LEVELCLASS_SP(n) ? LEVELCLASS_SP :             \
195                          IS_LEVELCLASS_DX(n) ? LEVELCLASS_DX :             \
196                          IS_LEVELCLASS_SB(n) ? LEVELCLASS_SB :             \
197                          LEVELCLASS_UNDEFINED)
198
199 // sort priorities of artwork
200 #define ARTWORKCLASS_CLASSICS_START     100
201 #define ARTWORKCLASS_CLASSICS_END       199
202 #define ARTWORKCLASS_CONTRIB_START      200
203 #define ARTWORKCLASS_CONTRIB_END        299
204 #define ARTWORKCLASS_PRIVATE_START      300
205 #define ARTWORKCLASS_PRIVATE_END        399
206 #define ARTWORKCLASS_LEVEL_START        400
207 #define ARTWORKCLASS_LEVEL_END          499
208
209 #define ARTWORKCLASS_CLASSICS           ARTWORKCLASS_CLASSICS_START
210 #define ARTWORKCLASS_CONTRIB            ARTWORKCLASS_CONTRIB_START
211 #define ARTWORKCLASS_PRIVATE            ARTWORKCLASS_PRIVATE_START
212 #define ARTWORKCLASS_LEVEL              ARTWORKCLASS_LEVEL_START
213
214 #define ARTWORKCLASS_UNDEFINED          999
215
216 #define IS_ARTWORKCLASS_CLASSICS(p)                                     \
217         ((p)->sort_priority >= ARTWORKCLASS_CLASSICS_START &&           \
218          (p)->sort_priority <= ARTWORKCLASS_CLASSICS_END)
219 #define IS_ARTWORKCLASS_CONTRIB(p)                                      \
220         ((p)->sort_priority >= ARTWORKCLASS_CONTRIB_START &&            \
221          (p)->sort_priority <= ARTWORKCLASS_CONTRIB_END)
222 #define IS_ARTWORKCLASS_PRIVATE(p)                                      \
223         ((p)->sort_priority >= ARTWORKCLASS_PRIVATE_START &&            \
224          (p)->sort_priority <= ARTWORKCLASS_PRIVATE_END)
225 #define IS_ARTWORKCLASS_LEVEL(p)                                        \
226         ((p)->sort_priority >= ARTWORKCLASS_LEVEL_START &&              \
227          (p)->sort_priority <= ARTWORKCLASS_LEVEL_END)
228
229 #define ARTWORKCLASS(n) (IS_ARTWORKCLASS_CLASSICS(n) ? ARTWORKCLASS_CLASSICS :\
230                          IS_ARTWORKCLASS_CONTRIB(n) ? ARTWORKCLASS_CONTRIB :  \
231                          IS_ARTWORKCLASS_PRIVATE(n) ? ARTWORKCLASS_PRIVATE :  \
232                          IS_ARTWORKCLASS_LEVEL(n) ? ARTWORKCLASS_LEVEL :      \
233                          ARTWORKCLASS_UNDEFINED)
234
235 #define TREE_SORTING_DIR(ti)                                            \
236         (((ti)->parent_link                                ? 0 :        \
237           (ti)->in_user_dir                                ? 4 * 200 :  \
238           (ti)->sort_priority >= LEVELCLASS_CLASSICS_START ? 3 * 200 +  \
239           (ti)->sort_priority % 100 :                                   \
240           (ti)->sort_priority >= LEVELCLASS_TUTORIAL_START ? 2 * 200 +  \
241           (ti)->sort_priority % 100 :                                   \
242           1 * 200) +                                                    \
243          ((ti)->level_group ? 0 : 100))
244
245 #define TREE_COLOR_DIR(ti, active)                                      \
246         ((active)                       ? FC_YELLOW :                   \
247          TREE_SORTING(ti) / 200 == 4    ? FC_GREEN :                    \
248          TREE_SORTING(ti) / 200 == 2    ? FC_BLUE :                     \
249          FC_RED)
250
251 #define TREE_SORTING(ti)                                                \
252         (TREE_TYPE_IS_DIR((ti)->type) ? TREE_SORTING_DIR(ti) :          \
253          (ti)->sort_priority)
254
255 #define TREE_COLOR(ti, active)                                          \
256         (TREE_TYPE_IS_DIR((ti)->type) ? TREE_COLOR_DIR(ti, active) :    \
257          (ti)->color)
258
259
260 char *getUserGraphicsDir(void);
261 char *getUserSoundsDir(void);
262 char *getUserMusicDir(void);
263 char *setLevelArtworkDir(TreeInfo *);
264 char *getProgramMainDataPath(char *, char *);
265 char *getProgramConfigFilename(char *);
266 char *getTapeFilename(int);
267 char *getTemporaryTapeFilename(void);
268 char *getDefaultSolutionTapeFilename(int);
269 char *getSokobanSolutionTapeFilename(int);
270 char *getSolutionTapeFilename(int);
271 char *getScoreFilename(int);
272 char *getScoreCacheFilename(int);
273 char *getScoreTapeBasename(char *);
274 char *getScoreTapeFilename(char *, int);
275 char *getSetupFilename(void);
276 char *getDefaultSetupFilename(void);
277 char *getEditorSetupFilename(void);
278 char *getHelpAnimFilename(void);
279 char *getHelpTextFilename(void);
280 char *getLevelSetInfoFilename(void);
281 char *getLevelSetTitleMessageFilename(int, boolean);
282 char *getImageFilename(char *);
283 char *getCustomImageFilename(char *);
284 char *getCustomSoundFilename(char *);
285 char *getCustomMusicFilename(char *);
286 char *getCustomArtworkFilename(char *, int);
287 char *getCustomArtworkConfigFilename(int);
288 char *getCustomArtworkLevelConfigFilename(int);
289 char *getCustomMusicDirectory(void);
290
291 void InitTapeDirectory(char *);
292 void InitScoreDirectory(char *);
293 void InitScoreCacheDirectory(char *);
294 void InitScoreTapeDirectory(char *, int);
295 void InitUserLevelDirectory(char *);
296 void InitNetworkLevelDirectory(char *);
297 void InitLevelSetupDirectory(char *);
298
299 TreeInfo *newTreeInfo(void);
300 TreeInfo *newTreeInfo_setDefaults(int);
301 void pushTreeInfo(TreeInfo **, TreeInfo *);
302 void removeTreeInfo(TreeInfo **);
303 int numTreeInfo(TreeInfo *);
304 boolean validLevelSeries(TreeInfo *);
305 TreeInfo *getValidLevelSeries(TreeInfo *, TreeInfo *);
306 TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *);
307 TreeInfo *getNextValidTreeInfoEntry(TreeInfo *);
308 TreeInfo *getTreeInfoFirstGroupEntry(TreeInfo *);
309 int numTreeInfoInGroup(TreeInfo *);
310 int getPosFromTreeInfo(TreeInfo *);
311 TreeInfo *getTreeInfoFromPos(TreeInfo *, int);
312 TreeInfo *getTreeInfoFromIdentifier(TreeInfo *, char *);
313 int dumpTreeInfo(TreeInfo *, int);
314 void sortTreeInfoBySortFunction(TreeInfo **,
315                                 int (*compare_function)(const void *,
316                                                         const void *));
317 void sortTreeInfo(TreeInfo **);
318 void freeTreeInfo(TreeInfo *);
319
320 char *getHomeDir(void);
321 char *getPersonalDataDir(void);
322 char *getMainUserGameDataDir(void);
323 char *getUserGameDataDir(void);
324 char *getSetupDir(void);
325 char *getLevelDirFromTreeInfo(TreeInfo *);
326 char *getUserLevelDir(char *);
327 char *getNetworkLevelDir(char *);
328 char *getCurrentLevelDir(void);
329 char *getNewUserLevelSubdir(void);
330 char *getTapeDir(char *);
331
332 void createDirectory(char *, char *, int);
333 void InitMainUserDataDirectory(void);
334 void InitUserDataDirectory(void);
335 void SetFilePermissions(char *, int);
336
337 char *getCookie(char *);
338 void fprintFileHeader(FILE *, char *);
339 int getFileVersionFromCookieString(const char *);
340 boolean checkCookieString(const char *, const char *);
341
342 char *getFormattedSetupEntry(char *, char *);
343
344 boolean getTokenValueFromSetupLine(char *, char **, char **);
345
346 SetupFileList *newSetupFileList(char *, char *);
347 void freeSetupFileList(SetupFileList *);
348 char *getListEntry(SetupFileList *, char *);
349 SetupFileList *setListEntry(SetupFileList *, char *, char *);
350 SetupFileList *addListEntry(SetupFileList *, char *, char *);
351 SetupFileList *loadSetupFileList(char *);
352
353 SetupFileHash *newSetupFileHash(void);
354 void freeSetupFileHash(SetupFileHash *);
355 char *getHashEntry(SetupFileHash *, char *);
356 void setHashEntry(SetupFileHash *, char *, char *);
357 char *removeHashEntry(SetupFileHash *, char *);
358 SetupFileHash *loadSetupFileHash(char *);
359 void setSetupInfo(struct TokenInfo *, int, char *);
360 char *getSetupValue(int, void *);
361 char *getSetupLine(struct TokenInfo *, char *, int);
362
363 unsigned int get_hash_from_key(void *);
364
365 int GetZipFileTreeType(char *);
366 char *ExtractZipFileIntoDirectory(char *, char *, int);
367
368 boolean AdjustGraphicsForEMC(void);
369 boolean AdjustSoundsForEMC(void);
370
371 void SetCurrentArtwork(int);
372 void ChangeCurrentArtworkIfNeeded(int);
373
374 void LoadLevelInfo(void);
375 void LoadArtworkInfo(void);
376 void LoadLevelArtworkInfo(void);
377
378 char *getArtworkIdentifierForUserLevelSet(int);
379 TreeInfo *getArtworkTreeInfoForUserLevelSet(int);
380 boolean checkIfCustomArtworkExistsForCurrentLevelSet(void);
381 void AddUserLevelSetToLevelInfo(char *);
382 void AddTreeSetToTreeInfo(TreeInfo *, char *, char *, int);
383 boolean UpdateUserLevelSet(char *, char *, char *, int);
384 boolean CreateUserLevelSet(char *, char *, char *, int, boolean);
385
386 void UpdateLastPlayedLevels_TreeInfo(void);
387 void StoreLastPlayedLevels(TreeInfo *);
388 void RestoreLastPlayedLevels(TreeInfo **);
389
390 void LoadLevelSetup_LastSeries(void);
391 void SaveLevelSetup_LastSeries(void);
392 void SaveLevelSetup_LastSeries_Deactivate(void);
393 void LoadLevelSetup_SeriesInfo(void);
394 void SaveLevelSetup_SeriesInfo(void);
395
396 int LevelStats_getPlayed(int);
397 int LevelStats_getSolved(int);
398 void LevelStats_setPlayed(int, int);
399 void LevelStats_setSolved(int, int);
400 void LevelStats_incPlayed(int);
401 void LevelStats_incSolved(int);
402
403 void LoadUserSetup(void);
404 void SaveUserSetup(void);
405
406 #endif // MISC_H