- moved levelsubset into an own file
[supertux.git] / src / level_subset.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include "setup.h"
22 #include "level.h"
23 #include "globals.h"
24 #include "screen/surface.h"
25 #include "level_subset.h"
26
27 LevelSubset::LevelSubset()
28     : image(0), levels(0)
29 {
30 }
31
32 LevelSubset::~LevelSubset()
33 {
34   delete image;
35 }
36
37 void LevelSubset::create(const std::string& subset_name)
38 {
39   Level new_lev;
40   LevelSubset new_subset;
41   new_subset.name = subset_name;
42   new_subset.title = "Unknown Title";
43   new_subset.description = "No description so far.";
44   new_subset.save();
45   //new_lev.save(subset_name, 1, 0);
46 }
47
48 void LevelSubset::parse (lisp_object_t* cursor)
49 {
50   while(!lisp_nil_p(cursor))
51     {
52       lisp_object_t* cur = lisp_car(cursor);
53       char *s;
54
55       if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
56         {
57           printf("Not good");
58         }
59       else
60         {
61           if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
62             {
63               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
64                 {
65                   title = s;
66                 }
67             }
68           else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
69             {
70               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
71                 {
72                   description = s;
73                 }
74             }
75         }
76       cursor = lisp_cdr (cursor);
77     }
78 }
79
80 void LevelSubset::load(const char* subset)
81 {
82   FILE* fi;
83   char filename[1024];
84   char str[1024];
85   int i;
86   lisp_object_t* root_obj = 0;
87
88   name = subset;
89
90   snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
91   if(!faccessible(filename))
92     snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
93   if(faccessible(filename))
94     {
95       fi = fopen(filename, "r");
96       if (fi == NULL)
97         {
98           perror(filename);
99         }
100       lisp_stream_t stream;
101       lisp_stream_init_file (&stream, fi);
102       root_obj = lisp_read (&stream);
103
104       if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
105         {
106           printf("World: Parse Error in file %s", filename);
107         }
108
109       lisp_object_t* cur = lisp_car(root_obj);
110
111       if (!lisp_symbol_p (cur))
112         {
113           printf("World: Read error in %s",filename);
114         }
115
116       if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
117         {
118           parse(lisp_cdr(root_obj));
119
120         }
121
122       lisp_free(root_obj);
123       fclose(fi);
124
125       snprintf(str, 1024, "%s.png", filename);
126       if(faccessible(str))
127         {
128           delete image;
129           image = new Surface(str,IGNORE_ALPHA);
130         }
131       else
132         {
133           snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
134           delete image;
135           image = new Surface(filename,IGNORE_ALPHA);
136         }
137     }
138
139   for(i=1; i != -1; ++i)
140     {
141       /* Get the number of levels in this subset */
142       snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
143       if(!faccessible(filename))
144         {
145           snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
146           if(!faccessible(filename))
147             break;
148         }
149     }
150   levels = --i;
151 }
152
153 void
154 LevelSubset::save()
155 {
156   FILE* fi;
157   std::string filename;
158
159   /* Save data file: */
160   filename = "/levels/" + name + "/";
161
162   fcreatedir(filename.c_str());
163   filename = std::string(st_dir) + "/levels/" + name + "/info";
164   if(!fwriteable(filename.c_str()))
165     filename = datadir + "/levels/" + name + "/info";
166   if(fwriteable(filename.c_str()))
167     {
168       fi = fopen(filename.c_str(), "w");
169       if (fi == NULL)
170         {
171           perror(filename.c_str());
172         }
173
174       /* Write header: */
175       fprintf(fi,";SuperTux-Level-Subset\n");
176       fprintf(fi,"(supertux-level-subset\n");
177
178       /* Save title info: */
179       fprintf(fi,"  (title \"%s\")\n", title.c_str());
180
181       /* Save the description: */
182       fprintf(fi,"  (description \"%s\")\n", description.c_str());
183
184       fprintf( fi,")");
185       fclose(fi);
186     }
187 }
188
189 std::string
190 LevelSubset::get_level_filename(unsigned int num)
191 {
192   char filename[1024];
193                                                                                 
194   // Load data file:
195   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir,
196       name.c_str(), num);
197   if(!faccessible(filename))
198     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
199         name.c_str(), num);
200
201   return std::string(filename);
202 }
203
204 /* EOF */
205