4 // Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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.
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.
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
24 #include "screen/surface.h"
25 #include "level_subset.h"
27 LevelSubset::LevelSubset()
32 LevelSubset::~LevelSubset()
37 void LevelSubset::create(const std::string& subset_name)
40 LevelSubset new_subset;
41 new_subset.name = subset_name;
42 new_subset.title = "Unknown Title";
43 new_subset.description = "No description so far.";
45 //new_lev.save(subset_name, 1, 0);
48 void LevelSubset::parse (lisp_object_t* cursor)
50 while(!lisp_nil_p(cursor))
52 lisp_object_t* cur = lisp_car(cursor);
55 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
61 if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
63 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
68 else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
70 if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
76 cursor = lisp_cdr (cursor);
80 void LevelSubset::load(const char* subset)
86 lisp_object_t* root_obj = 0;
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))
95 fi = fopen(filename, "r");
100 lisp_stream_t stream;
101 lisp_stream_init_file (&stream, fi);
102 root_obj = lisp_read (&stream);
104 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
106 printf("World: Parse Error in file %s", filename);
109 lisp_object_t* cur = lisp_car(root_obj);
111 if (!lisp_symbol_p (cur))
113 printf("World: Read error in %s",filename);
116 if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
118 parse(lisp_cdr(root_obj));
125 snprintf(str, 1024, "%s.png", filename);
129 image = new Surface(str,IGNORE_ALPHA);
133 snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
135 image = new Surface(filename,IGNORE_ALPHA);
139 for(i=1; i != -1; ++i)
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))
145 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
146 if(!faccessible(filename))
157 std::string filename;
159 /* Save data file: */
160 filename = "/levels/" + name + "/";
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()))
168 fi = fopen(filename.c_str(), "w");
171 perror(filename.c_str());
175 fprintf(fi,";SuperTux-Level-Subset\n");
176 fprintf(fi,"(supertux-level-subset\n");
178 /* Save title info: */
179 fprintf(fi," (title \"%s\")\n", title.c_str());
181 /* Save the description: */
182 fprintf(fi," (description \"%s\")\n", description.c_str());
190 LevelSubset::get_level_filename(unsigned int num)
195 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir,
197 if(!faccessible(filename))
198 snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
201 return std::string(filename);