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