569ebfd13b8d1d259b6d26721132199581303220
[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 <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/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
68 enum MainMenuIDs {
69   MNID_STARTGAME,
70   MNID_LEVELS_CONTRIB,
71   MNID_OPTIONMENU,
72   MNID_LEVELEDITOR,
73   MNID_CREDITS,
74   MNID_QUITMAINMENU
75 };
76
77 void
78 TitleScreen::update_load_game_menu()
79 {
80   load_game_menu.reset(new Menu());
81
82   load_game_menu->add_label(_("Start Game"));
83   load_game_menu->add_hl();
84   for(int i = 1; i <= 5; ++i) {
85     load_game_menu->add_entry(i, get_slotinfo(i));
86   }
87   load_game_menu->add_hl();
88   load_game_menu->add_back(_("Back"));
89 }
90
91 void
92 TitleScreen::free_contrib_menu()
93 {
94   for(std::vector<World*>::iterator i = contrib_worlds.begin();
95       i != contrib_worlds.end(); ++i)
96     delete *i;
97
98   contrib_worlds.clear();
99 }
100
101 void
102 TitleScreen::generate_contrib_menu()
103 {
104   /** Generating contrib levels list by making use of Level Subset  */
105   std::vector<std::string> level_worlds;
106   char** files = PHYSFS_enumerateFiles("levels/");
107   for(const char* const* filename = files; *filename != 0; ++filename) {
108     std::string filepath = std::string("levels/") + *filename;
109     if(PHYSFS_isDirectory(filepath.c_str()))
110       level_worlds.push_back(filepath);
111   }
112   PHYSFS_freeList(files);
113
114   free_contrib_menu();
115   contrib_menu.reset(new Menu());
116
117   contrib_menu->add_label(_("Contrib Levels"));
118   contrib_menu->add_hl();
119
120   int i = 0;
121   for (std::vector<std::string>::iterator it = level_worlds.begin();
122       it != level_worlds.end(); ++it) {
123     try {
124       std::auto_ptr<World> world (new World());
125       world->load(*it + "/info");
126       if(world->hide_from_contribs) {
127         continue;
128       }
129       contrib_menu->add_entry(i++, world->title);
130       contrib_worlds.push_back(world.release());
131     } catch(std::exception& e) {
132 #ifdef DEBUG
133       log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
134 #endif
135     }
136   }
137
138   contrib_menu->add_hl();
139   contrib_menu->add_back(_("Back"));
140 }
141
142 std::string
143 TitleScreen::get_level_name(const std::string& filename)
144 {
145   try {
146     lisp::Parser parser;
147     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
148
149     const lisp::Lisp* level = root->get_lisp("supertux-level");
150     if(!level)
151       return "";
152
153     std::string name;
154     level->get("name", name);
155     return name;
156   } catch(std::exception& e) {
157           log_warning << "Problem getting name of '" << filename << "': "
158                   << e.what() << std::endl;
159     return "";
160   }
161 }
162
163 void
164 TitleScreen::check_levels_contrib_menu()
165 {
166   int index = contrib_menu->check();
167   if (index == -1)
168     return;
169
170   current_world = contrib_worlds[index];
171
172   if(!current_world->is_levelset) {
173     update_load_game_menu();
174     Menu::push_current(load_game_menu.get());
175   } else {
176     contrib_world_menu.reset(new Menu());
177
178     contrib_world_menu->add_label(current_world->title);
179     contrib_world_menu->add_hl();
180
181     for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
182     {
183       /** get level's title */
184       std::string filename = current_world->get_level_filename(i);
185       std::string title = get_level_name(filename);
186       contrib_world_menu->add_entry(i, title);
187     }
188
189     contrib_world_menu->add_hl();
190     contrib_world_menu->add_back(_("Back"));
191
192     Menu::push_current(contrib_world_menu.get());
193   }
194 }
195
196 void
197 TitleScreen::check_contrib_world_menu()
198 {
199   int index = contrib_world_menu->check();
200   if (index != -1) {
201     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
202       sound_manager->stop_music();
203       GameSession* session =
204         new GameSession(current_world->get_level_filename(index));
205       main_loop->push_screen(session);
206     }
207   }
208 }
209
210 void
211 TitleScreen::make_tux_jump()
212 {
213   static Timer randomWaitTimer;
214   static Timer jumpPushTimer;
215   static float last_tux_x_pos = -1;
216   static float last_tux_y_pos = -1;
217
218   Sector* sector  = titlesession->get_current_sector();
219   Player* tux = sector->player;
220
221   //sector->play_music(LEVEL_MUSIC);
222
223   controller->update();
224   controller->press(Controller::RIGHT);
225
226   // Determine how far we moved since last frame
227   float dx = fabsf(last_tux_x_pos - tux->get_pos().x);
228   float dy = fabsf(last_tux_y_pos - tux->get_pos().y);
229
230   // Calculate space to check for obstacles
231   Rect lookahead = tux->get_bbox();
232   lookahead.move(Vector(96, 0));
233
234   // Check if we should press the jump button
235   bool randomJump = !randomWaitTimer.started();
236   bool notMoving = (fabsf(dx) + fabsf(dy)) < 0.1;
237   bool pathBlocked = !sector->is_free_of_statics(lookahead);
238   if (!controller->released(Controller::JUMP)
239       && (notMoving || pathBlocked || randomJump)) {
240     float jumpDuration;
241     if(pathBlocked)
242       jumpDuration = 0.5;
243     else
244       jumpDuration = systemRandom.randf(0.3, 0.8);
245     jumpPushTimer.start(jumpDuration);
246     randomWaitTimer.start(systemRandom.randf(3.0, 6.0));
247   }
248
249   // Keep jump button pressed
250   if (jumpPushTimer.started())
251     controller->press(Controller::JUMP);
252
253   // Remember last position, so we can determine if we moved
254   last_tux_x_pos = tux->get_pos().x;
255   last_tux_y_pos = tux->get_pos().y;
256
257   // Wrap around at the end of the level back to the beginnig
258   if(sector->get_width() - 320 < tux->get_pos().x) {
259     sector->activate("main");
260     sector->camera->reset(tux->get_pos());
261   }
262 }
263
264 TitleScreen::TitleScreen()
265 {
266   controller.reset(new CodeController());
267   titlesession.reset(new GameSession("levels/misc/menu.stl"));
268
269   Player* player = titlesession->get_current_sector()->player;
270   player->set_controller(controller.get());
271   player->set_speedlimit(230); //MAX_WALK_XM
272
273   main_menu.reset(new Menu());
274   main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
275   main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
276   main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
277   main_menu->add_submenu(_("Options"), get_options_menu());
278   main_menu->add_entry(MNID_CREDITS, _("Credits"));
279   main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
280 }
281
282 TitleScreen::~TitleScreen()
283 {
284 }
285
286 void
287 TitleScreen::setup()
288 {
289   player_status->reset();
290
291   Sector* sector = titlesession->get_current_sector();
292   if(Sector::current() != sector) {
293     sector->play_music(LEVEL_MUSIC);
294     sector->activate(sector->player->get_pos());
295   }
296
297   Menu::set_current(main_menu.get());
298 }
299
300 void
301 TitleScreen::leave()
302 {
303   Sector* sector = titlesession->get_current_sector();
304   sector->deactivate();
305   Menu::set_current(NULL);
306 }
307
308 void
309 TitleScreen::draw(DrawingContext& context)
310 {
311   Sector* sector  = titlesession->get_current_sector();
312   sector->draw(context);
313
314   context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
315       Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
316   context.draw_text(white_small_text,
317       _(
318 "Copyright (c) 2006 SuperTux Devel Team\n"
319 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
320 "redistribute it under certain conditions; see the file COPYING for details.\n"
321 ),
322       Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
323       LEFT_ALLIGN, LAYER_FOREGROUND1);
324 }
325
326 void
327 TitleScreen::update(float elapsed_time)
328 {
329   main_loop->set_speed(0.6f);
330   Sector* sector  = titlesession->get_current_sector();
331   sector->update(elapsed_time);
332
333   make_tux_jump();
334
335   Menu* menu = Menu::current();
336   if(menu) {
337     menu->update();
338
339     if(menu == main_menu.get()) {
340       switch (main_menu->check()) {
341         case MNID_STARTGAME:
342           // Start Game, ie. goto the slots menu
343           if(main_world.get() == NULL) {
344             main_world.reset(new World());
345             main_world->load("levels/world1/info");
346           }
347           current_world = main_world.get();
348           update_load_game_menu();
349           Menu::push_current(load_game_menu.get());
350           break;
351         case MNID_LEVELS_CONTRIB:
352           // Contrib Menu
353           generate_contrib_menu();
354           Menu::push_current(contrib_menu.get());
355           break;
356         case MNID_CREDITS:
357           main_loop->push_screen(new TextScroller("credits.txt"),
358                                  new FadeOut(0.5));
359           break;
360         case MNID_QUITMAINMENU:
361           main_loop->quit(new FadeOut(0.25));
362           break;
363       }
364     } else if(menu == load_game_menu.get()) {
365       /*
366       if(event.key.keysym.sym == SDLK_DELETE) {
367         int slot = menu->get_active_item_id();
368         std::stringstream stream;
369         stream << slot;
370         std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
371
372         if(confirm_dialog(bkg_title, str.c_str())) {
373           str = "save/slot" + stream.str() + ".stsg";
374           log_debug << "Removing: " << str << std::endl;
375           PHYSFS_delete(str.c_str());
376         }
377
378         update_load_save_game_menu(load_game_menu);
379         Menu::set_current(main_menu.get());
380       }*/
381       process_load_game_menu();
382     } else if(menu == contrib_menu.get()) {
383       check_levels_contrib_menu();
384     } else if (menu == contrib_world_menu.get()) {
385       check_contrib_world_menu();
386     }
387   }
388
389   // reopen menu of user closed it (so that the app doesn't close when user
390   // accidently hit ESC)
391   if(Menu::current() == 0) {
392     Menu::set_current(main_menu.get());
393   }
394 }
395
396 std::string
397 TitleScreen::get_slotinfo(int slot)
398 {
399   std::string tmp;
400   std::string title;
401
402   std::string basename = current_world->get_basedir();
403   basename = basename.substr(0, basename.length()-1);
404   std::string worlddirname = FileSystem::basename(basename);
405   std::ostringstream stream;
406   stream << "save/" << worlddirname << "_" << slot << ".stsg";
407   std::string slotfile = stream.str();
408
409   try {
410     lisp::Parser parser;
411     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
412
413     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
414     if(!savegame)
415       throw std::runtime_error("file is not a supertux-savegame.");
416
417     savegame->get("title", title);
418   } catch(std::exception& ) {
419     std::ostringstream slottitle;
420     slottitle << _("Slot") << " " << slot << " - " << _("Free");
421     return slottitle.str();
422   }
423
424   std::ostringstream slottitle;
425   slottitle << _("Slot") << " " << slot << " - " << title;
426   return slottitle.str();
427 }
428
429 bool
430 TitleScreen::process_load_game_menu()
431 {
432   int slot = load_game_menu->check();
433
434   if(slot == -1)
435     return false;
436
437   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
438     return false;
439
440   std::string basename = current_world->get_basedir();
441   basename = basename.substr(0, basename.length()-1);
442   std::string worlddirname = FileSystem::basename(basename);
443   std::stringstream stream;
444   stream << "save/" << worlddirname << "_" << slot << ".stsg";
445   std::string slotfile = stream.str();
446
447   try {
448     current_world->set_savegame_filename(slotfile);
449     current_world->run();
450   } catch(std::exception& e) {
451     log_fatal << "Couldn't start world: " << e.what() << std::endl;
452   }
453
454   return true;
455 }