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