- coverted badguy type into an object
[supertux.git] / src / level.cpp
1 //
2 // C Implementation: level
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2003
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <iostream>
17 #include "globals.h"
18 #include "setup.h"
19 #include "screen.h"
20 #include "level.h"
21 #include "physic.h"
22 #include "scene.h"
23 #include "lispreader.h"
24
25 using namespace std;
26
27 texture_type img_bkgd, img_bkgd_tile[2][4], img_solid[4], img_brick[2];
28
29 st_subset::st_subset()
30 {
31   levels = 0;
32 }
33
34 void st_subset::create(const std::string& subset_name)
35 {
36   st_level new_lev;
37   st_subset new_subset;
38   new_subset.name = subset_name;
39   new_subset.title = "Unknown Title";
40   new_subset.description = "No description so far.";
41   new_subset.save();
42   level_default(&new_lev);
43   level_save(&new_lev,subset_name.c_str(),1);
44 }
45
46 void st_subset::parse (lisp_object_t* cursor)
47 {
48   while(!lisp_nil_p(cursor))
49     {
50       lisp_object_t* cur = lisp_car(cursor);
51       char *s;
52
53       if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
54         {
55           printf("Not good");
56         }
57       else
58         {
59           if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
60             {
61               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
62                 {
63                   title = s;
64                 }
65             }
66           else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
67             {
68               if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
69                 {
70                   description = s;
71                 }
72             }
73         }
74       cursor = lisp_cdr (cursor);
75     }
76 }
77
78 void st_subset::load(char *subset)
79 {
80   FILE* fi;
81   char filename[1024];
82   char str[1024];
83   int i;
84   lisp_object_t* root_obj = 0;
85
86   name = subset;
87
88   snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
89   if(!faccessible(filename))
90     snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
91   if(faccessible(filename))
92     {
93       fi = fopen(filename, "r");
94       if (fi == NULL)
95         {
96           perror(filename);
97         }
98       lisp_stream_t stream;
99       lisp_stream_init_file (&stream, fi);
100       root_obj = lisp_read (&stream);
101
102       if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
103         {
104           printf("World: Parse Error in file %s", filename);
105         }
106
107       lisp_object_t* cur = lisp_car(root_obj);
108
109       if (!lisp_symbol_p (cur))
110         {
111           printf("World: Read error in %s",filename);
112         }
113
114       if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
115         {
116           parse(lisp_cdr(root_obj));
117
118         }
119
120       fclose(fi);
121
122       snprintf(str, 1024, "%s.png", filename);
123       if(faccessible(str))
124         {
125           texture_load(&image,str,IGNORE_ALPHA);
126         }
127       else
128         {
129           snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
130           texture_load(&image,filename,IGNORE_ALPHA);
131         }
132     }
133
134   for(i=1; i != -1; ++i)
135     {
136       /* Get the number of levels in this subset */
137       snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
138       if(!faccessible(filename))
139         {
140           snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
141           if(!faccessible(filename))
142             break;
143         }
144     }
145   levels = --i;
146 }
147
148 void st_subset::save()
149 {
150   FILE* fi;
151   string filename;
152
153   /* Save data file: */
154   filename = "/levels/" + name + "/";
155
156   fcreatedir(filename.c_str());
157   filename = string(st_dir) + "/levels/" + name + "/info";
158   if(!fwriteable(filename.c_str()))
159     filename = datadir + "/levels/" + name + "/info";
160   if(fwriteable(filename.c_str()))
161     {
162       fi = fopen(filename.c_str(), "w");
163       if (fi == NULL)
164         {
165           perror(filename.c_str());
166         }
167
168       /* Write header: */
169       fprintf(fi,";SuperTux-Level-Subset\n");
170       fprintf(fi,"(supertux-level-subset\n");
171
172       /* Save title info: */
173       fprintf(fi,"  (title \"%s\")\n", title.c_str());
174
175       /* Save the description: */
176       fprintf(fi,"  (description \"%s\")\n", description.c_str());
177
178       fprintf( fi,")");
179       fclose(fi);
180
181     }
182 }
183
184 void st_subset::free()
185 {
186   title.clear();
187   description.clear();
188   name.clear();
189   texture_free(&image);
190   levels = 0;
191 }
192
193 void level_default(st_level* plevel)
194 {
195   int i,y;
196   plevel->name = "UnNamed";
197   plevel->theme = "antarctica";
198   plevel->song_title = "Mortimers_chipdisko.mod";
199   plevel->bkgd_image = "arctis.png";
200   plevel->width = 21;
201   plevel->time_left = 100;
202   plevel->gravity = 10.;
203   plevel->bkgd_red = 0;
204   plevel->bkgd_green = 0;
205   plevel->bkgd_blue = 0;
206
207   for(i = 0; i < 15; ++i)
208     {
209       plevel->tiles[i] = (unsigned int*) malloc((plevel->width+1)*sizeof(unsigned int));
210       plevel->tiles[i][plevel->width] = (unsigned int) '\0';
211       for(y = 0; y < plevel->width; ++y)
212         plevel->tiles[i][y] = (unsigned int) '.';
213       plevel->tiles[i][plevel->width] = (unsigned int) '\0';
214     }
215 }
216
217 /* Load data for this level: */
218 /* Returns -1, if the loading of the level failed. */
219 int level_load(st_level* plevel, const  char *subset, int level)
220 {
221   char filename[1024];
222
223   /* Load data file: */
224
225   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
226   if(!faccessible(filename))
227     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
228
229   return level_load(plevel, filename);
230 }
231
232 int level_load(st_level* plevel, const char* filename)
233 {
234   int x, y;
235   FILE * fi;
236   lisp_object_t* root_obj = 0;
237   fi = fopen(filename, "r");
238   if (fi == NULL)
239     {
240       perror(filename);
241       return -1;
242     }
243
244   lisp_stream_t stream;
245   lisp_stream_init_file (&stream, fi);
246   root_obj = lisp_read (&stream);
247
248   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
249     {
250       printf("World: Parse Error in file %s", filename);
251     }
252
253   vector<int> vi;
254
255   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
256     {
257       LispReader reader(lisp_cdr(root_obj));
258       
259       reader.read_int("width",  &plevel->width);
260       reader.read_int("time",  &plevel->time_left);
261       reader.read_int("bkgd_red",  &plevel->bkgd_red);
262       reader.read_int("bkgd_green",  &plevel->bkgd_green);
263       reader.read_int("bkgd_blue",  &plevel->bkgd_blue);
264       reader.read_float("gravity",  &plevel->gravity);
265       reader.read_string("name",  &plevel->name);
266       reader.read_string("theme",  &plevel->theme);
267       reader.read_string("music",  &plevel->song_title);
268       reader.read_string("background",  &plevel->bkgd_image);
269       reader.read_int_vector("tilemap",  &vi);
270     }
271     
272     
273   int i;
274   for( i = 0; i < 15; ++i)
275     plevel->tiles[i] = (unsigned int*) calloc((plevel->width +1) , sizeof(unsigned int) );
276
277   i = 0;
278   int j = 0;
279   for(vector<int>::iterator it = vi.begin(); it != vi.end(); ++it, ++i)
280     {
281
282       plevel->tiles[j][i] = (*it);
283       if(i == plevel->width - 1)
284         {
285           i = -1;
286           ++j;
287         }
288     }
289
290   /* Set the global gravity to the latest loaded level's gravity */
291   gravity = plevel->gravity;
292
293   /*  Mark the end position of this level! - Is a bit wrong here thought */
294
295   for (y = 0; y < 15; ++y)
296     {
297       for (x = 0; x < plevel->width; ++x)
298         {
299           if(plevel->tiles[y][x] == '|')
300             {
301               if(x*32 > endpos)
302                 endpos = x*32;
303             }
304         }
305     }
306
307   fclose(fi);
308   return 0;
309 }
310
311 /* Save data for level: */
312
313 void level_save(st_level* plevel,const  char * subset, int level)
314 {
315   FILE * fi;
316   char filename[1024];
317   int y,i;
318   char str[80];
319
320   /* Save data file: */
321   sprintf(str, "/levels/%s/", subset);
322   fcreatedir(str);
323   snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset, level);
324   if(!fwriteable(filename))
325     snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset, level);
326
327   fi = fopen(filename, "w");
328   if (fi == NULL)
329     {
330       perror(filename);
331       st_shutdown();
332       exit(-1);
333     }
334
335
336         /* Write header: */
337       fprintf(fi,";SuperTux-Level\n");
338       fprintf(fi,"(supertux-level\n");
339
340       fprintf(fi,"  (name \"%s\")\n", plevel->name.c_str());
341       fprintf(fi,"  (theme \"%s\")\n", plevel->theme.c_str());
342       fprintf(fi,"  (music \"%s\")\n", plevel->song_title.c_str());
343       fprintf(fi,"  (background \"%s\")\n", plevel->bkgd_image.c_str());
344       fprintf(fi,"  (bkgd_red %d)\n", plevel->bkgd_red);
345       fprintf(fi,"  (bkgd_green %d)\n", plevel->bkgd_green);
346       fprintf(fi,"  (bkgd_blue %d)\n", plevel->bkgd_blue);
347       fprintf(fi,"  (time %d)\n", plevel->time_left);
348       fprintf(fi,"  (width %d)\n", plevel->width);
349       fprintf(fi,"  (gravity %2.1f)\n", plevel->gravity);
350       fprintf(fi,"  (tilemap ");     
351        
352   for(y = 0; y < 15; ++y)
353     {
354     for(i = 0; i < plevel->width; ++i)
355     fprintf(fi," %d ", plevel->tiles[y][i]); 
356     }
357     
358       fprintf( fi,")");    
359       fprintf( fi,")\n");
360
361   fclose(fi);
362 }
363
364
365 /* Unload data for this level: */
366
367 void level_free(st_level* plevel)
368 {
369   int i;
370   for(i=0; i < 15; ++i)
371     free(plevel->tiles[i]);
372
373   plevel->name.clear();
374   plevel->theme.clear();
375   plevel->song_title.clear();
376   plevel->bkgd_image.clear();
377 }
378
379 /* Load graphics: */
380
381 void level_load_gfx(st_level *plevel)
382 {
383   level_load_image(&img_brick[0],plevel->theme,"brick0.png", IGNORE_ALPHA);
384   level_load_image(&img_brick[1],plevel->theme,"brick1.png", IGNORE_ALPHA);
385
386   level_load_image(&img_solid[0],plevel->theme,"solid0.png", USE_ALPHA);
387   level_load_image(&img_solid[1],plevel->theme,"solid1.png", USE_ALPHA);
388   level_load_image(&img_solid[2],plevel->theme,"solid2.png", USE_ALPHA);
389   level_load_image(&img_solid[3],plevel->theme,"solid3.png", USE_ALPHA);
390
391   level_load_image(&img_bkgd_tile[0][0],plevel->theme,"bkgd-00.png", USE_ALPHA);
392   level_load_image(&img_bkgd_tile[0][1],plevel->theme,"bkgd-01.png", USE_ALPHA);
393   level_load_image(&img_bkgd_tile[0][2],plevel->theme,"bkgd-02.png", USE_ALPHA);
394   level_load_image(&img_bkgd_tile[0][3],plevel->theme,"bkgd-03.png", USE_ALPHA);
395
396   level_load_image(&img_bkgd_tile[1][0],plevel->theme,"bkgd-10.png", USE_ALPHA);
397   level_load_image(&img_bkgd_tile[1][1],plevel->theme,"bkgd-11.png", USE_ALPHA);
398   level_load_image(&img_bkgd_tile[1][2],plevel->theme,"bkgd-12.png", USE_ALPHA);
399   level_load_image(&img_bkgd_tile[1][3],plevel->theme,"bkgd-13.png", USE_ALPHA);
400
401   if(!plevel->bkgd_image.empty())
402     {
403       char fname[1024];
404       snprintf(fname, 1024, "%s/background/%s", st_dir, plevel->bkgd_image.c_str());
405       if(!faccessible(fname))
406         snprintf(fname, 1024, "%s/images/background/%s", datadir.c_str(), plevel->bkgd_image.c_str());
407       texture_load(&img_bkgd, fname, IGNORE_ALPHA);
408     }
409   else
410     {
411       /* Quick hack to make sure an image is loaded, when we are freeing it afterwards. */#
412       level_load_image(&img_bkgd, plevel->theme,"solid0.png", IGNORE_ALPHA);
413     }
414 }
415
416 /* Free graphics data for this level: */
417
418 void level_free_gfx(void)
419 {
420   int i;
421
422   for (i = 0; i < 2; i++)
423     {
424       texture_free(&img_brick[i]);
425     }
426   for (i = 0; i < 4; i++)
427     {
428       texture_free(&img_solid[i]);
429       texture_free(&img_bkgd_tile[0][i]);
430       texture_free(&img_bkgd_tile[1][i]);
431     }
432
433   texture_free(&img_bkgd);
434 }
435
436 /* Load a level-specific graphic... */
437
438 void level_load_image(texture_type* ptexture, string theme,const  char * file, int use_alpha)
439 {
440   char fname[1024];
441
442   snprintf(fname, 1024, "%s/themes/%s/%s", st_dir, theme.c_str(), file);
443   if(!faccessible(fname))
444     snprintf(fname, 1024, "%s/images/themes/%s/%s", datadir.c_str(), theme.c_str(), file);
445
446   texture_load(ptexture, fname, use_alpha);
447 }
448
449 /* Edit a piece of the map! */
450
451 void level_change(st_level* plevel, float x, float y, unsigned char c)
452 {
453   int xx, yy;
454
455   yy = ((int)y / 32);
456   xx = ((int)x / 32);
457
458   if (yy >= 0 && yy < 15 && xx >= 0 && xx <= plevel->width)
459     plevel->tiles[yy][xx] = c;
460 }
461
462 /* Free music data for this level: */
463
464 void level_free_song(void)
465 {
466   free_music(level_song);
467   free_music(level_song_fast);
468 }
469
470 /* Load music: */
471
472 void level_load_song(st_level* plevel)
473 {
474
475   char * song_path;
476   char * song_subtitle;
477
478   level_song = load_song(datadir + "/music/" + plevel->song_title);
479
480   song_path = (char *) malloc(sizeof(char) * datadir.length() +
481                               strlen(plevel->song_title.c_str()) + 8 + 5);
482   song_subtitle = strdup(plevel->song_title.c_str());
483   strcpy(strstr(song_subtitle, "."), "\0");
484   sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), song_subtitle, strstr(plevel->song_title.c_str(), "."));
485   level_song_fast = load_song(song_path);
486   free(song_subtitle);
487   free(song_path);
488 }