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