Read the first 5 chars, not the all string of LANG.
[supertux.git] / src / level.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 <map>
22 #include <cstdlib>
23 #include <cstdio>
24 #include <cstring>
25 #include <iostream>
26 #include <fstream>
27 #include <stdexcept>
28
29 #include "globals.h"
30 #include "setup.h"
31 #include "camera.h"
32 #include "screen/screen.h"
33 #include "level.h"
34 #include "physic.h"
35 #include "scene.h"
36 #include "sector.h"
37 #include "tile.h"
38 #include "lispreader.h"
39 #include "resources.h"
40 #include "gameobjs.h"
41 #include "lispwriter.h"
42
43 using namespace std;
44
45 Level::Level()
46   : name("noname"), author("mr. x"), time_left(500)
47
48 {
49 }
50
51 void
52 Level::load(const std::string& filename)
53 {
54   LispReader* level = LispReader::load(filename, "supertux-level");
55
56   int version = 1;
57   level->read_int("version", version);
58   if(version == 1) {
59     load_old_format(*level);
60     delete level;
61     return;
62   }
63
64   for(lisp_object_t* cur = level->get_lisp(); !lisp_nil_p(cur);
65       cur = lisp_cdr(cur)) {
66     std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
67     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
68     LispReader reader(lisp_cdr(lisp_car(cur)));
69
70     if(token == "name") {
71       name = lisp_string(data);
72     } else if(token == "author") {
73       author = lisp_string(data);
74     } else if(token == "time") {
75       time_left = lisp_integer(data);
76     } else if(token == "sector") {
77       Sector* sector = new Sector;
78       sector->parse(reader);
79       add_sector(sector);
80     } else {
81       std::cerr << "Unknown token '" << token << "' in level file.\n";
82       continue;
83     }
84   }
85   
86   delete level;
87 }
88
89 void
90 Level::load_old_format(LispReader& reader)
91 {
92   reader.read_string("name", name, true);
93   reader.read_string("author", author);
94   reader.read_int("time", time_left);
95
96   Sector* sector = new Sector;
97   sector->parse_old_format(reader);
98   add_sector(sector);
99 }
100
101 void
102 Level::save(const std::string& filename)
103 {
104  ofstream file(filename.c_str(), ios::out);
105  LispWriter* writer = new LispWriter(file);
106
107  writer->write_comment("Level made using SuperTux's built-in Level Editor");
108
109  writer->start_list("supertux-level");
110
111  int version = 2;
112  writer->write_int("version", version);
113
114  writer->write_string("name", name);
115  writer->write_string("author", author);
116  writer->write_int("time", time_left);
117
118  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
119    {
120    writer->start_list("sector");
121    i->second->write(*writer);
122    writer->end_list("sector");
123    }
124
125  writer->end_list("supertux-level");
126
127  delete writer;
128  file.close();
129 }
130
131 Level::~Level()
132 {
133   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
134     delete i->second;
135 }
136
137 void
138 Level::do_vertical_flip()
139 {
140   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
141     i->second->do_vertical_flip();
142 }
143
144 void
145 Level::add_sector(Sector* sector)
146 {
147   sectors.insert(std::make_pair(sector->get_name(), sector));       
148 }
149
150 Sector*
151 Level::get_sector(const std::string& name)
152 {
153   Sectors::iterator i = sectors.find(name);
154   if(i == sectors.end())
155     return 0;
156
157   return i->second;
158 }
159