- remove pointless leveltime from world1 levels
[supertux.git] / src / title.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <config.h>
22
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <cmath>
32 #include <SDL.h>
33 #include <SDL_image.h>
34 #include <physfs.h>
35
36 #include "title.hpp"
37 #include "mainloop.hpp"
38 #include "video/screen.hpp"
39 #include "video/drawing_context.hpp"
40 #include "video/surface.hpp"
41 #include "audio/sound_manager.hpp"
42 #include "gui/menu.hpp"
43 #include "timer.hpp"
44 #include "lisp/lisp.hpp"
45 #include "lisp/parser.hpp"
46 #include "level.hpp"
47 #include "world.hpp"
48 #include "game_session.hpp"
49 #include "worldmap.hpp"
50 #include "player_status.hpp"
51 #include "tile.hpp"
52 #include "sector.hpp"
53 #include "object/tilemap.hpp"
54 #include "object/camera.hpp"
55 #include "object/player.hpp"
56 #include "resources.hpp"
57 #include "gettext.hpp"
58 #include "misc.hpp"
59 #include "textscroller.hpp"
60 #include "file_system.hpp"
61 #include "control/joystickkeyboardcontroller.hpp"
62 #include "control/codecontroller.hpp"
63 #include "main.hpp"
64 #include "msg.hpp"
65 #include "console.hpp"
66
67 void
68 TitleScreen::update_load_game_menu()
69 {
70   load_game_menu.reset(new Menu());
71
72   load_game_menu->add_label(_("Start Game"));
73   load_game_menu->add_hl();
74   for(int i = 1; i <= 5; ++i) {
75     load_game_menu->add_entry(i, get_slotinfo(i));
76   }
77   load_game_menu->add_hl();
78   load_game_menu->add_back(_("Back"));
79 }
80
81 void
82 TitleScreen::free_contrib_menu()
83 {
84   for(std::vector<World*>::iterator i = contrib_worlds.begin();
85       i != contrib_worlds.end(); ++i)
86     delete *i;
87
88   contrib_worlds.clear();
89 }
90
91 void
92 TitleScreen::generate_contrib_menu()
93 {
94   /** Generating contrib levels list by making use of Level Subset  */
95   std::vector<std::string> level_worlds; 
96   char** files = PHYSFS_enumerateFiles("levels/");
97   for(const char* const* filename = files; *filename != 0; ++filename) {
98     std::string filepath = std::string("levels/") + *filename;
99     if(PHYSFS_isDirectory(filepath.c_str()))
100       level_worlds.push_back(filepath);
101   }
102   PHYSFS_freeList(files);
103
104   free_contrib_menu();
105   contrib_menu.reset(new Menu());
106
107   contrib_menu->add_label(_("Contrib Levels"));
108   contrib_menu->add_hl();
109   
110   int i = 0;
111   for (std::vector<std::string>::iterator it = level_worlds.begin();
112       it != level_worlds.end(); ++it) {
113     try {
114       std::auto_ptr<World> world (new World());
115       world->load(*it + "/info");
116       if(world->hide_from_contribs) {
117         continue;
118       }
119       contrib_menu->add_entry(i++, world->title);
120       contrib_worlds.push_back(world.release());
121     } catch(std::exception& e) {
122 #ifdef DEBUG
123       msg_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
124 #endif
125     }
126   }
127
128   contrib_menu->add_hl();
129   contrib_menu->add_back(_("Back"));
130 }
131
132 std::string
133 TitleScreen::get_level_name(const std::string& filename)
134 {
135   try {
136     lisp::Parser parser;
137     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
138
139     const lisp::Lisp* level = root->get_lisp("supertux-level");
140     if(!level)
141       return "";
142
143     std::string name;
144     level->get("name", name);
145     return name;
146   } catch(std::exception& e) {
147     msg_warning << "Problem getting name of '" << filename << "'." << std::endl;
148     return "";
149   }
150 }
151
152 void
153 TitleScreen::check_levels_contrib_menu()
154 {
155   int index = contrib_menu->check();
156   if (index == -1)
157     return;
158
159   current_world = contrib_worlds[index];
160
161   if(!current_world->is_levelset) {
162     update_load_game_menu();
163     Menu::push_current(load_game_menu.get());
164   } else {
165     contrib_world_menu.reset(new Menu());
166
167     contrib_world_menu->add_label(current_world->title);
168     contrib_world_menu->add_hl();
169
170     for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
171     {
172       /** get level's title */
173       std::string filename = current_world->get_level_filename(i);
174       std::string title = get_level_name(filename);
175       contrib_world_menu->add_entry(i, title);
176     }
177
178     contrib_world_menu->add_hl();
179     contrib_world_menu->add_back(_("Back"));
180
181     Menu::push_current(contrib_world_menu.get());
182   }
183 }
184
185 void
186 TitleScreen::check_contrib_world_menu()
187 {
188   int index = contrib_world_menu->check();
189   if (index != -1) {
190     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
191       sound_manager->stop_music();
192       GameSession* session =
193         new GameSession(
194           current_world->get_level_filename(index), ST_GL_PLAY);
195       main_loop->push_screen(session);
196     }
197   }  
198 }
199
200 void
201 TitleScreen::make_tux_jump()
202 {
203   static Timer randomWaitTimer;
204   static Timer jumpPushTimer;
205   static float last_tux_x_pos = -1;
206   static float last_tux_y_pos = -1;
207
208   Sector* sector  = titlesession->get_current_sector();
209   Player* tux = sector->player;
210
211   //sector->play_music(LEVEL_MUSIC);
212
213   controller->update();
214   controller->press(Controller::RIGHT);
215
216   // Determine how far we moved since last frame
217   float dx = fabsf(last_tux_x_pos - tux->get_pos().x); 
218   float dy = fabsf(last_tux_y_pos - tux->get_pos().y); 
219  
220   // Calculate space to check for obstacles 
221   Rect lookahead = tux->get_bbox();
222   lookahead.move(Vector(96, 0));
223   
224   // Check if we should press the jump button
225   bool randomJump = !randomWaitTimer.started();
226   bool notMoving = (fabsf(dx) + fabsf(dy)) < 0.1;
227   bool pathBlocked = !sector->is_free_space(lookahead); 
228   if (!controller->released(Controller::JUMP)
229       && (notMoving || pathBlocked || randomJump)) {
230     float jumpDuration;
231     if(pathBlocked)
232       jumpDuration = 0.5;
233     else
234       jumpDuration = float(rand() % 500 + 300) / 1000.0;
235     jumpPushTimer.start(jumpDuration);
236     randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
237   }
238
239   // Keep jump button pressed
240   if (jumpPushTimer.started())
241     controller->press(Controller::JUMP);
242
243   // Remember last position, so we can determine if we moved
244   last_tux_x_pos = tux->get_pos().x;
245   last_tux_y_pos = tux->get_pos().y;
246
247   // Wrap around at the end of the level back to the beginnig
248   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
249     sector->activate("main");
250     sector->camera->reset(tux->get_pos());
251   }
252 }
253
254 TitleScreen::TitleScreen()
255 {
256   controller.reset(new CodeController());
257   titlesession.reset(new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME));
258
259   Player* player = titlesession->get_current_sector()->player;
260   player->set_controller(controller.get());
261 }
262
263 TitleScreen::~TitleScreen()
264 {
265 }
266
267 void
268 TitleScreen::setup()
269 {
270   player_status->reset();
271
272   Sector* sector = titlesession->get_current_sector();
273   if(Sector::current() != sector) {
274     sector->play_music(LEVEL_MUSIC);
275     sector->activate(sector->player->get_pos());
276   }
277
278   Menu::set_current(main_menu);
279 }
280
281 void
282 TitleScreen::leave()
283 {
284   Sector* sector = titlesession->get_current_sector();
285   sector->deactivate();
286 }
287
288 void
289 TitleScreen::draw(DrawingContext& context)
290 {
291   Sector* sector  = titlesession->get_current_sector();
292   sector->draw(context);
293  
294   /*
295   if (Menu::current() == main_menu)
296     context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
297             LAYER_FOREGROUND1+1);
298   */
299
300   context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
301       Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
302   context.draw_text(white_small_text,
303       _(
304 "Copyright (c) 2006 SuperTux Devel Team\n"
305 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
306 "redistribute it under certain conditions; see the file COPYING for details.\n"
307 ),
308       Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
309       LEFT_ALLIGN, LAYER_FOREGROUND1);
310 }
311
312 void
313 TitleScreen::update(float elapsed_time)
314 {
315   main_loop->set_speed(0.6);
316   Sector* sector  = titlesession->get_current_sector();
317   sector->update(elapsed_time);
318
319   make_tux_jump();
320   
321   Menu* menu = Menu::current();
322   if(menu) {
323     menu->update();
324           
325     if(menu == main_menu) {
326       switch (main_menu->check()) {
327         case MNID_STARTGAME:
328           // Start Game, ie. goto the slots menu
329           if(main_world.get() == NULL) {
330             main_world.reset(new World());
331             main_world->load("levels/world1/info");
332           }
333           current_world = main_world.get();
334           update_load_game_menu();
335           Menu::push_current(load_game_menu.get());
336           break;
337         case MNID_LEVELS_CONTRIB:
338           // Contrib Menu
339           generate_contrib_menu();
340           Menu::push_current(contrib_menu.get());
341           break;
342         case MNID_CREDITS:
343           fadeout(500);
344           main_loop->push_screen(new TextScroller("credits.txt"));
345           break;
346         case MNID_QUITMAINMENU:
347           main_loop->quit();
348           break;
349       }
350     } else if(menu == load_game_menu.get()) {
351       /*
352       if(event.key.keysym.sym == SDLK_DELETE) {
353         int slot = menu->get_active_item_id();
354         std::stringstream stream;
355         stream << slot;
356         std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
357         
358         if(confirm_dialog(bkg_title, str.c_str())) {
359           str = "save/slot" + stream.str() + ".stsg";
360           msg_debug << "Removing: " << str << std::endl;
361           PHYSFS_delete(str.c_str());
362         }
363
364         update_load_save_game_menu(load_game_menu);
365         Menu::set_current(main_menu);
366       }*/
367       process_load_game_menu();
368     } else if(menu == contrib_menu.get()) {
369       check_levels_contrib_menu();
370     } else if (menu == contrib_world_menu.get()) {
371       check_contrib_world_menu();
372     }
373   }
374
375   // reopen menu of user closed it (so that the app doesn't close when user
376   // accidently hit ESC)
377   if(Menu::current() == 0) {
378     Menu::set_current(main_menu);
379   }
380 }
381
382 std::string
383 TitleScreen::get_slotinfo(int slot)
384 {
385   std::string tmp;
386   std::string title;
387
388   std::string basename = current_world->get_basedir();
389   basename = basename.substr(0, basename.length()-1);
390   std::string worlddirname = FileSystem::basename(basename);
391   std::ostringstream stream;
392   stream << "save/" << worlddirname << "_" << slot << ".stsg";
393   std::string slotfile = stream.str();
394
395   try {
396     lisp::Parser parser;
397     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
398
399     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
400     if(!savegame)
401       throw std::runtime_error("file is not a supertux-savegame.");
402
403     savegame->get("title", title);
404   } catch(std::exception& e) {
405     std::ostringstream slottitle;
406     slottitle << _("Slot") << " " << slot << " - " << _("Free");
407     return slottitle.str();
408   }
409
410   std::ostringstream slottitle;
411   slottitle << _("Slot") << " " << slot << " - " << title;
412   return slottitle.str();
413 }
414
415 bool
416 TitleScreen::process_load_game_menu()
417 {
418   int slot = load_game_menu->check();
419
420   if(slot == -1)
421     return false;
422
423   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
424     return false;
425
426   std::string basename = current_world->get_basedir();
427   basename = basename.substr(0, basename.length()-1);
428   std::string worlddirname = FileSystem::basename(basename);
429   std::stringstream stream;
430   stream << "save/" << worlddirname << "_" << slot << ".stsg";
431   std::string slotfile = stream.str();
432
433   fadeout(256);
434
435   try {
436     current_world->set_savegame_filename(slotfile);
437     current_world->run();
438   } catch(std::exception& e) {
439     msg_fatal << "Couldn't start world: " << e.what() << std::endl;
440   }
441
442   return true;
443 }
444