add magic to sol files and allow simple loading without texture and other things...
[neverball] / ball / level.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 <stdio.h>
16 #include <string.h>
17 #include <math.h>
18 #include <errno.h>
19
20 #include "level.h"
21 #include "solid.h"
22
23 /*---------------------------------------------------------------------------*/
24
25 void score_init_hs(struct score *s, int timer, int coins)
26 {
27     int i;
28     strcpy(s->player[0], "Hard");
29     strcpy(s->player[1], "Medium");
30     strcpy(s->player[2], "Easy");
31     strcpy(s->player[3], "");
32     for (i = 0; i < NSCORE+1; i++)
33     {
34         s->timer[i] = timer;
35         s->coins[i] = coins;
36     }
37 }
38
39 /*---------------------------------------------------------------------------*/
40
41 int level_load(const char *filename, struct level *level)
42 /* Load the sol file 'filename' and fill the 'level' structure
43  * return 1 on success, 0 on error
44  * TODO: Currently this function does nothing since metadata are not stored
45  *       in the sol file.  Therefore the sol file in not loaded */
46 {
47     FILE *fp;
48     struct s_file sol;
49     memset(&sol, 0, sizeof(sol));
50
51     /* Try to load the sol file */
52     if (!sol_load_only_file(&sol, filename))
53     {
54         fprintf(stderr, "Error while loading level file '%s': ", filename);
55         if (errno)
56            perror(NULL);
57         else
58            fprintf(stderr, _("Not a valid level file\n"));
59         return 0;
60     }
61
62     sol_free(&sol);
63     
64     /* Set filename */
65     strcpy(level->file, filename);
66     
67     /* Init hs with default values */
68     score_init_hs(&level->time_score, 59999, 0);
69     score_init_hs(&level->goal_score, 59999, 0);
70     score_init_hs(&level->coin_score, 59999, 0);
71     /* Consider that true HS are set latter by the caller */
72
73     /* Raz set info */
74     level->set        = NULL;
75     level->number     = 0;
76     level->numbername = "0";
77     level->is_locked  = 0;
78     level->is_bonus   = 0;
79
80     /* Considers that internal data are set by the caller */
81                             
82     return 1;
83 }
84
85 void level_dump_info(const struct level * level)
86 {
87     printf("filename:        %s\n"
88            "background:      %s\n"
89            "gradiant:        %s\n"
90            "screenshot:      %s\n"
91            "song:            %s\n"
92            "time limit:      %d\n"
93            "goal count:      %d\n",
94            level->file, level->back, level->grad, level->shot, level->song,
95            level->time, level->goal);
96 }
97
98 /*---------------------------------------------------------------------------*/
99
100 const char * mode_to_str(int m)
101 {
102     switch (m)
103     {
104     case MODE_CHALLENGE: return _("Challenge");
105     case MODE_NORMAL:    return _("Normal");
106     case MODE_PRACTICE:  return _("Practice");
107     case MODE_SINGLE:    return _("Single");
108     default:             return "???";
109     }
110 }
111
112 /*---------------------------------------------------------------------------*/
113
114 const char * state_to_str(int m)
115 {
116     switch (m)
117     {
118     case GAME_NONE:    return _("Aborted");
119     case GAME_TIME:    return _("Time-out");
120     case GAME_GOAL:    return _("Success");
121     case GAME_FALL:    return _("Fall-out");
122     default:           return "???";
123     }
124 }
125
126 /*---------------------------------------------------------------------------*/