restore trunk
[supertux.git] / src / title.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 <math.h>
30 #include <errno.h>
31 #include <unistd.h>
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/drawing_context.hpp"
39 #include "video/surface.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "gui/menu.hpp"
42 #include "timer.hpp"
43 #include "lisp/lisp.hpp"
44 #include "lisp/parser.hpp"
45 #include "level.hpp"
46 #include "world.hpp"
47 #include "game_session.hpp"
48 #include "worldmap/worldmap.hpp"
49 #include "player_status.hpp"
50 #include "tile.hpp"
51 #include "sector.hpp"
52 #include "object/tilemap.hpp"
53 #include "object/camera.hpp"
54 #include "object/player.hpp"
55 #include "resources.hpp"
56 #include "gettext.hpp"
57 #include "textscroller.hpp"
58 #include "fadeout.hpp"
59 #include "file_system.hpp"
60 #include "control/joystickkeyboardcontroller.hpp"
61 #include "control/codecontroller.hpp"
62 #include "main.hpp"
63 #include "log.hpp"
64 #include "options_menu.hpp"
65 #include "console.hpp"
66 #include "random_generator.hpp"
67 #include "addon_manager.hpp"
68
69 enum MainMenuIDs {
70   MNID_STARTGAME,
71   MNID_LEVELS_CONTRIB,
72   MNID_ADDONS,
73   MNID_OPTIONMENU,
74   MNID_LEVELEDITOR,
75   MNID_CREDITS,
76   MNID_QUITMAINMENU
77 };
78
79 void
80 TitleScreen::update_load_game_menu()
81 {
82   load_game_menu.reset(new Menu());
83
84   load_game_menu->add_label(_("Start Game"));
85   load_game_menu->add_hl();
86   for(int i = 1; i <= 5; ++i) {
87     load_game_menu->add_entry(i, get_slotinfo(i));
88   }
89   load_game_menu->add_hl();
90   load_game_menu->add_back(_("Back"));
91 }
92
93 void
94 TitleScreen::free_contrib_menu()
95 {
96   for(std::vector<World*>::iterator i = contrib_worlds.begin();
97       i != contrib_worlds.end(); ++i)
98     delete *i;
99
100   contrib_worlds.clear();
101 }
102
103 void
104 TitleScreen::generate_contrib_menu()
105 {
106   /** Generating contrib levels list by making use of Level Subset  */
107   std::vector<std::string> level_worlds;
108   char** files = PHYSFS_enumerateFiles("levels/");
109   for(const char* const* filename = files; *filename != 0; ++filename) {
110     std::string filepath = std::string("levels/") + *filename;
111     if(PHYSFS_isDirectory(filepath.c_str()))
112       level_worlds.push_back(filepath);
113   }
114   PHYSFS_freeList(files);
115
116   free_contrib_menu();
117   contrib_menu.reset(new Menu());
118
119   contrib_menu->add_label(_("Contrib Levels"));
120   contrib_menu->add_hl();
121
122   int i = 0;
123   for (std::vector<std::string>::iterator it = level_worlds.begin();
124       it != level_worlds.end(); ++it) {
125     try {
126       std::auto_ptr<World> world (new World());
127       world->load(*it + "/info");
128       if(world->hide_from_contribs) {
129         continue;
130       }
131       contrib_menu->add_entry(i++, world->title);
132       contrib_worlds.push_back(world.release());
133     } catch(std::exception& e) {
134 #ifdef DEBUG
135       log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
136 #endif
137     }
138   }
139
140   contrib_menu->add_hl();
141   contrib_menu->add_back(_("Back"));
142 }
143
144 std::string
145 TitleScreen::get_level_name(const std::string& filename)
146 {
147   try {
148     lisp::Parser parser;
149     const lisp::Lisp* root = parser.parse(filename);
150
151     const lisp::Lisp* level = root->get_lisp("supertux-level");
152     if(!level)
153       return "";
154
155     std::string name;
156     level->get("name", name);
157     return name;
158   } catch(std::exception& e) {
159           log_warning << "Problem getting name of '" << filename << "': "
160                   << e.what() << std::endl;
161     return "";
162   }
163 }
164
165 void
166 TitleScreen::check_levels_contrib_menu()
167 {
168   int index = contrib_menu->check();
169   if (index == -1)
170     return;
171
172   current_world = contrib_worlds[index];
173
174   if(!current_world->is_levelset) {
175     update_load_game_menu();
176     Menu::push_current(load_game_menu.get());
177   } else {
178     contrib_world_menu.reset(new Menu());
179
180     contrib_world_menu->add_label(current_world->title);
181     contrib_world_menu->add_hl();
182
183     for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
184     {
185       /** get level's title */
186       std::string filename = current_world->get_level_filename(i);
187       std::string title = get_level_name(filename);
188       contrib_world_menu->add_entry(i, title);
189     }
190
191     contrib_world_menu->add_hl();
192     contrib_world_menu->add_back(_("Back"));
193
194     Menu::push_current(contrib_world_menu.get());
195   }
196 }
197
198 void
199 TitleScreen::check_contrib_world_menu()
200 {
201   int index = contrib_world_menu->check();
202   if (index != -1) {
203     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
204       sound_manager->stop_music();
205       GameSession* session =
206         new GameSession(current_world->get_level_filename(index));
207       main_loop->push_screen(session);
208     }
209   }
210 }
211
212 namespace {
213   bool generate_addons_menu_sorter(const Addon& a1, const Addon& a2)
214   {
215     return a1.title < a2.title;
216   }
217
218   const int ADDON_LIST_START_ID = 10;
219 }
220
221 void
222 TitleScreen::generate_addons_menu()
223 {
224   AddonManager& adm = AddonManager::get_instance();
225
226   // refresh list of installed addons
227   installed_addons = adm.get_installed_addons();
228   
229   // build new Add-on list
230   addons.clear();
231
232   // add installed addons to list
233   addons.insert(addons.end(), installed_addons.begin(), installed_addons.end());
234
235   // add available addons to list
236   addons.insert(addons.end(), available_addons.begin(), available_addons.end());
237
238   // sort list
239   std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
240
241   // remove available addons that are already installed
242   std::vector<Addon>::iterator it2 = addons.begin();
243   while (it2 != addons.end()) {
244     Addon addon = *it2;
245     if (addon.isInstalled) {
246       bool restart = false;
247       for (std::vector<Addon>::iterator it = addons.begin(); it != addons.end(); ++it) {
248         Addon addon2 = *it;
249         if ((addon2.equals(addon)) && (!addon2.isInstalled)) {
250           addons.erase(it);
251           restart = true;
252           break;
253         }
254       }
255       if (restart) {
256         it2 = addons.begin();
257         continue;
258       }
259     }
260     it2++;
261   }
262
263   // (re)generate menu
264   free_addons_menu();
265   addons_menu.reset(new Menu());
266
267   addons_menu->add_label(_("Add-ons"));
268   addons_menu->add_hl();
269   
270 #ifdef HAVE_LIBCURL
271   addons_menu->add_entry(0, std::string(_("Check Online")));
272 #else
273   addons_menu->add_deactive(0, std::string(_("Check Online (disabled)")));
274 #endif
275
276   //addons_menu->add_hl();
277
278   for (unsigned int i = 0; i < addons.size(); i++) {
279     Addon addon = addons[i];
280     std::string text = "";
281     if (addon.kind != "") text += addon.kind + " ";
282     text += std::string("\"") + addon.title + "\"";
283     if (addon.author != "") text += " by \"" + addon.author + "\"";
284     addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.isInstalled);
285   }
286
287   addons_menu->add_hl();
288   addons_menu->add_back(_("Back"));
289 }
290
291 void
292 TitleScreen::check_addons_menu()
293 {
294   int index = addons_menu->check();
295   if (index == -1) return;
296
297   // check if "Check Online" was chosen
298   if (index == 0) {
299     try {
300       available_addons = AddonManager::get_instance().get_available_addons();
301       generate_addons_menu();
302       Menu::set_current(addons_menu.get());
303       addons_menu->set_active_item(index);
304     } 
305     catch (std::runtime_error e) {
306       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
307     }
308     return;
309   }
310
311   // if one of the Addons listed was chosen, take appropriate action
312   if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
313     Addon addon = addons[index - ADDON_LIST_START_ID];
314     if (!addon.isInstalled) {
315       try {
316         addon.install();
317         //generate_addons_menu();
318         //Menu::set_current(addons_menu.get());
319         //addons_menu->set_active_item(index);
320         Menu::set_current(0);
321       } 
322       catch (std::runtime_error e) {
323         log_warning << "Installation of Add-on failed: " << e.what() << std::endl;
324       }
325     } else {
326       try {
327         addon.remove();
328         //generate_addons_menu();
329         //Menu::set_current(addons_menu.get());
330         //addons_menu->set_active_item(index);
331         Menu::set_current(0);
332       } 
333       catch (std::runtime_error e) {
334         log_warning << "Removal of Add-on failed: " << e.what() << std::endl;
335       }
336     }
337   }
338
339 }
340
341 void
342 TitleScreen::free_addons_menu()
343 {
344 }
345
346 void
347 TitleScreen::make_tux_jump()
348 {
349   static bool jumpWasReleased = true;
350   Sector* sector  = titlesession->get_current_sector();
351   Player* tux = sector->player;
352
353   controller->update();
354   controller->press(Controller::RIGHT);
355
356   // Check if we should press the jump button
357   Rect lookahead = tux->get_bbox();
358   lookahead.p2.x += 96;
359   bool pathBlocked = !sector->is_free_of_statics(lookahead);
360   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
361     controller->press(Controller::JUMP);
362     jumpWasReleased = false;
363   } else {
364     jumpWasReleased = true;
365   }
366
367   // Wrap around at the end of the level back to the beginnig
368   if(sector->get_width() - 320 < tux->get_pos().x) {
369     sector->activate("main");
370     sector->camera->reset(tux->get_pos());
371   }
372 }
373
374 TitleScreen::TitleScreen()
375 {
376   controller.reset(new CodeController());
377   titlesession.reset(new GameSession("levels/misc/menu.stl"));
378
379   Player* player = titlesession->get_current_sector()->player;
380   player->set_controller(controller.get());
381   player->set_speedlimit(230); //MAX_WALK_XM
382
383   generate_main_menu();
384 }
385
386 void
387 TitleScreen::generate_main_menu()
388 {
389   main_menu.reset(new Menu());
390   main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
391   main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
392   main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
393   main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
394   main_menu->add_submenu(_("Options"), get_options_menu());
395   main_menu->add_entry(MNID_CREDITS, _("Credits"));
396   main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
397 }
398
399 TitleScreen::~TitleScreen()
400 {
401 }
402
403 void
404 TitleScreen::setup()
405 {
406   player_status->reset();
407
408   Sector* sector = titlesession->get_current_sector();
409   if(Sector::current() != sector) {
410     sector->play_music(LEVEL_MUSIC);
411     sector->activate(sector->player->get_pos());
412   }
413
414   Menu::set_current(main_menu.get());
415 }
416
417 void
418 TitleScreen::leave()
419 {
420   Sector* sector = titlesession->get_current_sector();
421   sector->deactivate();
422   Menu::set_current(NULL);
423 }
424
425 void
426 TitleScreen::draw(DrawingContext& context)
427 {
428   Sector* sector  = titlesession->get_current_sector();
429   sector->draw(context);
430
431   context.draw_text(white_small_text, "SuperTux " PACKAGE_VERSION "\n",
432       Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
433   context.draw_text(white_small_text,
434       _(
435 "Copyright (c) 2007 SuperTux Devel Team\n"
436 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
437 "redistribute it under certain conditions; see the file COPYING for details.\n"
438 ),
439       Vector(5, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
440       ALIGN_LEFT, LAYER_FOREGROUND1);
441 }
442
443 void
444 TitleScreen::update(float elapsed_time)
445 {
446   main_loop->set_speed(0.6f);
447   Sector* sector  = titlesession->get_current_sector();
448   sector->update(elapsed_time);
449
450   make_tux_jump();
451
452   Menu* menu = Menu::current();
453   if(menu) {
454     menu->update();
455
456     if(menu == main_menu.get()) {
457       switch (main_menu->check()) {
458         case MNID_STARTGAME:
459           // Start Game, ie. goto the slots menu
460           if(main_world.get() == NULL) {
461             main_world.reset(new World());
462             main_world->load("levels/world1/info");
463           }
464           current_world = main_world.get();
465           update_load_game_menu();
466           Menu::push_current(load_game_menu.get());
467           break;
468         case MNID_LEVELS_CONTRIB:
469           // Contrib Menu
470           generate_contrib_menu();
471           Menu::push_current(contrib_menu.get());
472           break;
473         case MNID_ADDONS:
474           // Add-ons Menu
475           generate_addons_menu();
476           Menu::push_current(addons_menu.get());
477           break;
478         case MNID_CREDITS:
479           main_loop->push_screen(new TextScroller("credits.txt"),
480                                  new FadeOut(0.5));
481           break;
482         case MNID_QUITMAINMENU:
483           main_loop->quit(new FadeOut(0.25));
484                   sound_manager->stop_music(0.25);
485           break;
486       }
487     } else if(menu == load_game_menu.get()) {
488       /*
489       if(event.key.keysym.sym == SDLK_DELETE) {
490         int slot = menu->get_active_item_id();
491         std::stringstream stream;
492         stream << slot;
493         std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
494
495         if(confirm_dialog(bkg_title, str.c_str())) {
496           str = "save/slot" + stream.str() + ".stsg";
497           log_debug << "Removing: " << str << std::endl;
498           PHYSFS_delete(str.c_str());
499         }
500
501         update_load_save_game_menu(load_game_menu);
502         Menu::set_current(main_menu.get());
503       }*/
504       process_load_game_menu();
505     } else if(menu == contrib_menu.get()) {
506       check_levels_contrib_menu();
507     } else if(menu == addons_menu.get()) {
508       check_addons_menu();
509     } else if (menu == contrib_world_menu.get()) {
510       check_contrib_world_menu();
511     }
512   }
513
514   // reopen menu of user closed it (so that the app doesn't close when user
515   // accidently hit ESC)
516   if(Menu::current() == 0) {
517     generate_main_menu();
518     Menu::set_current(main_menu.get());
519   }
520 }
521
522 std::string
523 TitleScreen::get_slotinfo(int slot)
524 {
525   std::string tmp;
526   std::string title;
527
528   std::string basename = current_world->get_basedir();
529   basename = basename.substr(0, basename.length()-1);
530   std::string worlddirname = FileSystem::basename(basename);
531   std::ostringstream stream;
532   stream << "save/" << worlddirname << "_" << slot << ".stsg";
533   std::string slotfile = stream.str();
534
535   try {
536     lisp::Parser parser;
537     const lisp::Lisp* root = parser.parse(slotfile);
538
539     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
540     if(!savegame)
541       throw std::runtime_error("file is not a supertux-savegame.");
542
543     savegame->get("title", title);
544   } catch(std::exception& ) {
545     std::ostringstream slottitle;
546     slottitle << _("Slot") << " " << slot << " - " << _("Free");
547     return slottitle.str();
548   }
549
550   std::ostringstream slottitle;
551   slottitle << _("Slot") << " " << slot << " - " << title;
552   return slottitle.str();
553 }
554
555 bool
556 TitleScreen::process_load_game_menu()
557 {
558   int slot = load_game_menu->check();
559
560   if(slot == -1)
561     return false;
562
563   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
564     return false;
565
566   std::string basename = current_world->get_basedir();
567   basename = basename.substr(0, basename.length()-1);
568   std::string worlddirname = FileSystem::basename(basename);
569   std::stringstream stream;
570   stream << "save/" << worlddirname << "_" << slot << ".stsg";
571   std::string slotfile = stream.str();
572
573   try {
574     current_world->set_savegame_filename(slotfile);
575     current_world->run();
576   } catch(std::exception& e) {
577     log_fatal << "Couldn't start world: " << e.what() << std::endl;
578   }
579
580   return true;
581 }