- Yet another try in the endless quest for perfect collision detection.
[supertux.git] / src / title.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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 "video/screen.hpp"
38 #include "video/surface.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "gui/menu.hpp"
41 #include "timer.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/parser.hpp"
44 #include "level.hpp"
45 #include "level_subset.hpp"
46 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "player_status.hpp"
49 #include "tile.hpp"
50 #include "sector.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "resources.hpp"
55 #include "gettext.hpp"
56 #include "misc.hpp"
57 #include "textscroller.hpp"
58 #include "file_system.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "control/codecontroller.hpp"
61 #include "main.hpp"
62 #include "exceptions.hpp"
63 #include "msg.hpp"
64
65 static Surface* bkg_title;
66 static Surface* logo;
67 //static Surface* img_choose_subset;
68
69 static int frame;
70
71 static GameSession* titlesession;
72 static CodeController* controller;
73
74 static std::vector<LevelSubset*> contrib_subsets;
75 static LevelSubset* current_contrib_subset = 0;
76 static int current_subset = -1;
77
78 /* If the demo was stopped - because game started, level
79    editor was excuted, etc - call this when you get back
80    to the title code.
81  */
82 void resume_demo()
83 {
84   player_status->reset();
85   titlesession->get_current_sector()->activate("main");
86   titlesession->set_current();
87
88   //frame_rate.update();
89 }
90
91 void update_load_save_game_menu(Menu* menu)
92 {
93   msg_debug("update loadsavemenu");
94   for(int i = 1; i < 6; ++i) {
95     MenuItem& item = menu->get_item_by_id(i);
96     item.kind = MN_ACTION;
97     item.change_text(slotinfo(i));
98   }
99 }
100
101 void free_contrib_menu()
102 {
103   for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
104       i != contrib_subsets.end(); ++i)
105     delete *i;
106
107   contrib_subsets.clear();
108   contrib_menu->clear();
109   current_contrib_subset = 0;
110   current_subset = -1;
111 }
112
113 void generate_contrib_menu()
114 {
115   /** Generating contrib levels list by making use of Level Subset  */
116   std::vector<std::string> level_subsets; 
117   char** files = PHYSFS_enumerateFiles("levels/");
118   for(const char* const* filename = files; *filename != 0; ++filename) {
119     std::string filepath = std::string("levels/") + *filename;
120     if(PHYSFS_isDirectory(filepath.c_str()))
121       level_subsets.push_back(filepath);
122   }
123   PHYSFS_freeList(files);
124
125   free_contrib_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_subsets.begin();
132       it != level_subsets.end(); ++it) {
133     try {
134       std::auto_ptr<LevelSubset> subset (new LevelSubset());
135       subset->load(*it);
136       if(subset->hide_from_contribs) {
137         continue;
138       }
139       contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
140       contrib_subsets.push_back(subset.release());
141     } catch(std::exception& e) {
142 #ifdef DEBUG
143       msg_warning("Couldn't parse levelset info for '"
144         << *it << "': " << e.what() << "");
145 #endif
146     }
147   }
148
149   contrib_menu->add_hl();
150   contrib_menu->add_back(_("Back"));
151 }
152
153 std::string get_level_name(const std::string& filename)
154 {
155   try {
156     lisp::Parser parser;
157     std::auto_ptr<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     msg_warning("Problem getting name of '" << filename << "'.");
168     return "";
169   }
170 }
171
172 void check_levels_contrib_menu()
173 {
174   int index = contrib_menu->check();
175   if (index == -1)
176     return;
177
178   LevelSubset& subset = * (contrib_subsets[index]);
179   
180   if(subset.has_worldmap) {
181     WorldMapNS::WorldMap worldmap;
182     worldmap.set_map_filename(subset.get_worldmap_filename());
183     sound_manager->stop_music();
184
185     // some fading
186     fadeout(256);
187     DrawingContext context;
188     context.draw_text(white_text, "Loading...",
189         Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
190     context.do_drawing();
191
192     // TODO: slots should be available for contrib maps
193     worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
194     worldmap.display();  // run the map
195
196     Menu::set_current(main_menu);
197     resume_demo();
198   } else if (current_subset != index) {
199     current_subset = index;
200     LevelSubset& subset = * (contrib_subsets[index]);
201
202     current_contrib_subset = &subset;
203
204     contrib_subset_menu->clear();
205
206     contrib_subset_menu->add_label(subset.title);
207     contrib_subset_menu->add_hl();
208
209     for (int i = 0; i < subset.get_num_levels(); ++i)
210     {
211       /** get level's title */
212       std::string filename = subset.get_level_filename(i);
213       std::string title = get_level_name(filename);
214       contrib_subset_menu->add_entry(i, title);
215     }
216
217     contrib_subset_menu->add_hl();
218     contrib_subset_menu->add_back(_("Back"));
219
220     titlesession->get_current_sector()->activate("main");
221     titlesession->set_current();
222   }
223 }
224
225 void check_contrib_subset_menu()
226 {
227   int index = contrib_subset_menu->check();
228   if (index != -1) {
229     if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
230       sound_manager->stop_music();
231       GameSession session(
232           current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
233       session.run();
234       player_status->reset();
235       Menu::set_current(main_menu);
236       resume_demo();
237     }
238   }  
239 }
240
241 void draw_demo(float elapsed_time)
242 {
243   static Timer randomWaitTimer;
244   static Timer jumpPushTimer;
245   static Timer jumpRecoverTimer;
246   static float last_tux_x_pos = -1;
247   static float last_tux_y_pos = -1;
248
249   Sector* sector  = titlesession->get_current_sector();
250   Player* tux = sector->player;
251
252   sector->play_music(LEVEL_MUSIC);
253
254   controller->update();
255   controller->press(Controller::RIGHT);
256
257   // Determine how far we moved since last frame
258   float dx = fabsf(last_tux_x_pos - tux->get_pos().x); 
259   float dy = fabsf(last_tux_y_pos - tux->get_pos().y); 
260  
261   // Calculate space to check for obstacles 
262   Rect lookahead = Rect(tux->get_bbox());
263   lookahead.move(Vector(lookahead.get_width()*2,0));
264   
265   // Check if we should press the jump button
266   bool randomJump = !randomWaitTimer.started();
267   bool mayJump = !jumpRecoverTimer.started();
268   bool notMoving = (dx+dy < 0.1);
269   bool pathBlocked = !sector->is_free_space(lookahead); 
270   if ((notMoving || pathBlocked || randomJump) && mayJump) {
271     float jumpDuration = float(rand() % 200 + 500) / 1000.0;
272     jumpPushTimer.start(jumpDuration);
273     jumpRecoverTimer.start(jumpDuration+0.1);
274     randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
275   }
276
277   // Keep jump button pressed
278   if (jumpPushTimer.started()) controller->press(Controller::JUMP);
279
280   // Remember last position, so we can determine if we moved
281   last_tux_x_pos = tux->get_pos().x;
282   last_tux_y_pos = tux->get_pos().y;
283
284   // Wrap around at the end of the level back to the beginnig
285   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
286     sector->activate("main");
287     sector->camera->reset(tux->get_pos());
288   }
289
290   sector->update(elapsed_time);
291   sector->draw(*titlesession->context);
292 }
293
294 /* --- TITLE SCREEN --- */
295 void title()
296 {
297   //LevelEditor* leveleditor;
298   controller = new CodeController();
299
300   titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
301
302   /* Load images: */
303   bkg_title = new Surface("images/background/arctis.jpg");
304   logo = new Surface("images/engine/menu/logo.png");
305   //img_choose_subset = new Surface("images/status/choose-level-subset.png");
306
307   titlesession->get_current_sector()->activate("main");
308   titlesession->set_current();
309
310   Player* player = titlesession->get_current_sector()->player;
311   player->set_controller(controller);
312
313   /* --- Main title loop: --- */
314   frame = 0;
315
316   Uint32 lastticks = SDL_GetTicks();
317   
318   Menu::set_current(main_menu);
319   DrawingContext& context = *titlesession->context;
320   bool running = true;
321   while (running)
322     {
323       // Calculate the movement-factor
324       Uint32 ticks = SDL_GetTicks();
325       float elapsed_time = float(ticks - lastticks) / 1000.;
326       game_time += elapsed_time;
327       lastticks = ticks;
328       // 40fps is minimum
329       if(elapsed_time > .04)
330         elapsed_time = .04;
331       
332       /* Lower the speed so that Tux doesn't jump too hectically throught
333          the demo. */
334       elapsed_time /= 2;
335
336       SDL_Event event;
337       main_controller->update();
338       while (SDL_PollEvent(&event)) {
339         if (Menu::current()) {
340           Menu::current()->event(event);
341         }
342         main_controller->process_event(event);
343         if (event.type == SDL_QUIT)
344           throw graceful_shutdown();
345       }
346   
347       /* Draw the background: */
348       draw_demo(elapsed_time);
349
350       if (Menu::current() == main_menu)
351         context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
352             LAYER_FOREGROUND1+1);
353
354       context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
355               Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
356       context.draw_text(white_small_text,
357         _(
358 "Copyright (c) 2006 SuperTux Devel Team\n"
359 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
360 "redistribute it under certain conditions; see the file COPYING for details.\n"
361         ),
362         Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
363         LEFT_ALLIGN, LAYER_FOREGROUND1);
364
365       /* Don't draw menu, if quit is true */
366       Menu* menu = Menu::current();
367       if(menu)
368         {
369           menu->draw(context);
370           menu->update();
371           
372           if(menu == main_menu)
373             {
374               switch (main_menu->check())
375                 {
376                 case MNID_STARTGAME:
377                   // Start Game, ie. goto the slots menu
378                   update_load_save_game_menu(load_game_menu);
379                   break;
380                 case MNID_LEVELS_CONTRIB:
381                   // Contrib Menu
382                   generate_contrib_menu();
383                   break;
384                 case MNID_CREDITS:
385                   sound_manager->stop_music();
386                   fadeout(500);
387                   sound_manager->play_music("music/credits.ogg");
388                   display_text_file("credits.txt");
389                   sound_manager->stop_music();
390                   fadeout(500);
391                   Menu::set_current(main_menu);
392                   break;
393                 case MNID_QUITMAINMENU:
394                   running = false;
395                   break;
396                 }
397             }
398           else if(menu == options_menu)
399             {
400               process_options_menu();
401             }
402           else if(menu == load_game_menu)
403             {
404               if(event.key.keysym.sym == SDLK_DELETE)
405                 {
406                 int slot = menu->get_active_item_id();
407                 std::stringstream stream;
408                 stream << slot;
409                 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
410                 
411                 if(confirm_dialog(bkg_title, str.c_str())) {
412                   str = "save/slot" + stream.str() + ".stsg";
413                   msg_debug("Removing: " << str);
414                   PHYSFS_delete(str.c_str());
415                 }
416
417                 update_load_save_game_menu(load_game_menu);
418                 Menu::set_current(main_menu);
419                 resume_demo();
420                 }
421               else if (process_load_game_menu())
422                 {
423                   resume_demo();
424                 }
425             }
426           else if(menu == contrib_menu)
427             {
428               check_levels_contrib_menu();
429             }
430           else if (menu == contrib_subset_menu)
431             {
432               check_contrib_subset_menu();
433             }
434         }
435
436       // reopen menu of user closed it (so that the app doesn't close when user
437       // accidently hit ESC)
438       if(Menu::current() == 0) {
439         Menu::set_current(main_menu);
440       }
441
442       context.do_drawing();
443       sound_manager->update();
444
445       //frame_rate.update();
446
447       /* Pause: */
448       frame++;
449     }
450   /* Free surfaces: */
451
452   free_contrib_menu();
453   delete titlesession;
454   delete bkg_title;
455   delete logo;
456   //delete img_choose_subset;
457 }