hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[supertux.git] / src / game_session.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 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; either version 2
11 //  of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 // 
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 #include <config.h>
22
23 #include <fstream>
24 #include <sstream>
25 #include <cassert>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cmath>
29 #include <cstring>
30 #include <cerrno>
31 #include <unistd.h>
32 #include <ctime>
33 #include <stdexcept>
34
35 #include <SDL.h>
36
37 #include "game_session.hpp"
38 #include "msg.hpp"
39 #include "worldmap.hpp"
40 #include "mainloop.hpp"
41 #include "video/screen.hpp"
42 #include "audio/sound_manager.hpp"
43 #include "gui/menu.hpp"
44 #include "sector.hpp"
45 #include "level.hpp"
46 #include "tile.hpp"
47 #include "player_status.hpp"
48 #include "object/particlesystem.hpp"
49 #include "object/background.hpp"
50 #include "object/gradient.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "object/level_time.hpp"
55 #include "lisp/lisp.hpp"
56 #include "lisp/parser.hpp"
57 #include "resources.hpp"
58 #include "worldmap.hpp"
59 #include "misc.hpp"
60 #include "statistics.hpp"
61 #include "timer.hpp"
62 #include "object/fireworks.hpp"
63 #include "textscroller.hpp"
64 #include "control/codecontroller.hpp"
65 #include "control/joystickkeyboardcontroller.hpp"
66 #include "main.hpp"
67 #include "file_system.hpp"
68 #include "gameconfig.hpp"
69 #include "gettext.hpp"
70 #include "console.hpp"
71 #include "flip_level_transformer.hpp"
72
73 // the engine will be run with a logical framerate of 64fps.
74 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
75 // binary fraction...
76 static const float LOGICAL_FPS = 64.0;
77
78 namespace {
79   const char* consoleCommands[] = {
80           "foo",
81           "whereami",
82           "camera",
83           "grease",
84           "invincible",
85           "mortal",
86           "shrink", 
87           "kill",
88           "gotoend",
89           "flip",
90           "finish",
91           "restart",
92           "quit"
93   };
94 }
95
96 using namespace WorldMapNS;
97
98 GameSession* GameSession::current_ = 0;
99
100 GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode,
101     Statistics* statistics)
102   : level(0), currentsector(0), mode(mode),
103     end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
104     levelfile(levelfile_), best_level_statistics(statistics),
105     capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
106 {
107   current_ = this;
108   currentsector = 0;
109   
110   game_pause = false;
111   fps_fps = 0;
112
113   for (uint16_t i=0; i < sizeof(::consoleCommands)/sizeof(typeof(consoleCommands[0])); i++) {
114     Console::registerCommand(consoleCommands[i], this);
115   }
116
117   statistics_backdrop = new Surface("images/engine/menu/score-backdrop.png");
118
119   restart_level(true);
120 }
121
122 void
123 GameSession::restart_level(bool fromBeginning)
124 {
125   game_pause   = false;
126   end_sequence = NO_ENDSEQUENCE;
127
128   main_controller->reset();
129
130   delete level;
131   currentsector = 0;
132
133   level = new Level;
134   level->load(levelfile);
135
136   global_stats.reset();
137   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
138   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
139   
140   // get time
141   int time = 0;
142   for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
143   {
144     Sector* sec = *i;
145
146     for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
147         j != sec->gameobjects.end(); ++j)
148     {
149       GameObject* obj = *j;
150       
151       LevelTime* lt = dynamic_cast<LevelTime*> (obj);
152       if(lt)
153         time += int(lt->get_level_time());
154     }
155   }
156   global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time);
157
158   if (fromBeginning) reset_sector="";
159   if(reset_sector != "") {
160     currentsector = level->get_sector(reset_sector);
161     if(!currentsector) {
162       std::stringstream msg;
163       msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
164       throw std::runtime_error(msg.str());
165     }
166     currentsector->activate(reset_pos);
167   } else {
168     currentsector = level->get_sector("main");
169     if(!currentsector)
170       throw std::runtime_error("Couldn't find main sector");
171     currentsector->activate("main");
172   }
173   
174   if(mode == ST_GL_PLAY || mode == ST_GL_LOAD_LEVEL_FILE)
175     levelintro();
176
177   currentsector->play_music(LEVEL_MUSIC);
178
179   if(capture_file != "")
180     record_demo(capture_file);
181 }
182
183 GameSession::~GameSession()
184 {
185   delete capture_demo_stream;
186   delete playback_demo_stream;
187   delete demo_controller;
188
189   delete end_sequence_controller;
190   delete level;
191
192   delete statistics_backdrop;
193
194   current_ = NULL;
195 }
196
197 void
198 GameSession::record_demo(const std::string& filename)
199 {
200   delete capture_demo_stream;
201   
202   capture_demo_stream = new std::ofstream(filename.c_str()); 
203   if(!capture_demo_stream->good()) {
204     std::stringstream msg;
205     msg << "Couldn't open demo file '" << filename << "' for writing.";
206     throw std::runtime_error(msg.str());
207   }
208   capture_file = filename;
209 }
210
211 void
212 GameSession::play_demo(const std::string& filename)
213 {
214   delete playback_demo_stream;
215   delete demo_controller;
216   
217   playback_demo_stream = new std::ifstream(filename.c_str());
218   if(!playback_demo_stream->good()) {
219     std::stringstream msg;
220     msg << "Couldn't open demo file '" << filename << "' for reading.";
221     throw std::runtime_error(msg.str());
222   }
223
224   Player& tux = *currentsector->player;
225   demo_controller = new CodeController();
226   tux.set_controller(demo_controller);
227 }
228
229 void
230 GameSession::levelintro()
231 {
232   char str[60];
233
234   sound_manager->stop_music();
235
236   DrawingContext context;
237   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
238       i != currentsector->gameobjects.end(); ++i) {
239     Background* background = dynamic_cast<Background*> (*i);
240     if(background) {
241       background->draw(context);
242     }
243     Gradient* gradient = dynamic_cast<Gradient*> (*i);
244     if(gradient) {
245       gradient->draw(context);
246     }
247   }
248
249 //  context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
250 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
251   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
252       LAYER_FOREGROUND1);
253
254   sprintf(str, "Coins: %d", player_status->coins);
255   context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
256       CENTER_ALLIGN, LAYER_FOREGROUND1);
257
258   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
259     //TODO make author check case/blank-insensitive
260     context.draw_text(white_small_text,
261       std::string(_("contributed by ")) + level->get_author(), 
262       Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
263
264
265   if(best_level_statistics != NULL)
266     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
267
268   wait_for_event(1.0, 3.0);
269 }
270
271 void
272 GameSession::on_escape_press()
273 {
274   if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
275     return;   // don't let the player open the menu, when he is dying
276   
277   if(mode == ST_GL_TEST) {
278     main_loop->exit_screen();
279   } else if (!Menu::current()) {
280     Menu::set_current(game_menu);
281     game_menu->set_active_item(MNID_CONTINUE);
282     game_pause = true;
283   } else {
284     game_pause = false;
285   }
286 }
287
288 void
289 GameSession::process_events()
290 {
291   Player& tux = *currentsector->player;
292
293   // end of pause mode?
294   if(!Menu::current() && game_pause) {
295     game_pause = false;
296   }
297
298   if (end_sequence != NO_ENDSEQUENCE) {
299     if(end_sequence_controller == 0) {
300       end_sequence_controller = new CodeController();
301       tux.set_controller(end_sequence_controller);
302     }
303
304     end_sequence_controller->press(Controller::RIGHT);
305     
306     if (int(last_x_pos) == int(tux.get_pos().x))
307       end_sequence_controller->press(Controller::JUMP);    
308     last_x_pos = tux.get_pos().x;
309   }
310
311   // playback a demo?
312   if(playback_demo_stream != 0) {
313     demo_controller->update();
314     char left = false;
315     char right = false;
316     char up = false;
317     char down = false;
318     char jump = false;
319     char action = false;
320     playback_demo_stream->get(left);
321     playback_demo_stream->get(right);
322     playback_demo_stream->get(up);
323     playback_demo_stream->get(down);
324     playback_demo_stream->get(jump);
325     playback_demo_stream->get(action);
326     demo_controller->press(Controller::LEFT, left);
327     demo_controller->press(Controller::RIGHT, right);
328     demo_controller->press(Controller::UP, up);
329     demo_controller->press(Controller::DOWN, down);
330     demo_controller->press(Controller::JUMP, jump);
331     demo_controller->press(Controller::ACTION, action);
332   }
333
334   // save input for demo?
335   if(capture_demo_stream != 0) {
336     capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
337     capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
338     capture_demo_stream ->put(main_controller->hold(Controller::UP));
339     capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
340     capture_demo_stream ->put(main_controller->hold(Controller::JUMP));   
341     capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
342   }
343 }
344
345 bool
346 GameSession::consoleCommand(std::string command, std::vector<std::string>)
347 {
348   if (command == "foo") {
349     msg_info << "bar" << std::endl;
350     return true;
351   }
352
353   if (currentsector == 0) return false;
354   Player& tux = *currentsector->player;
355   
356   // Cheating words (the goal of this is really for debugging,
357   // but could be used for some cheating, nothing wrong with that)
358   if (command == "grease") {
359     tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
360     return true;
361   }
362   if (command == "invincible") {
363     // be invincle for the rest of the level
364     tux.invincible_timer.start(10000);
365     return true;
366   }
367   if (command == "mortal") {
368     // give up invincibility
369     tux.invincible_timer.stop();
370     return true;
371   }
372   if (command == "shrink") {
373     // remove powerups
374     tux.kill(tux.SHRINK);
375     return true;
376   }
377   if (command == "kill") {
378     tux.kill(tux.KILL);
379     return true;
380   }
381   if (command == "restart") {
382     restart_level(true);
383     return true;
384   }
385   if (command == "whereami") {
386     msg_info << "You are at x " << tux.get_pos().x << ", y " << tux.get_pos().y << std::endl;
387     return true;
388   }
389   if (command == "gotoend") {
390     // goes to the end of the level
391     tux.move(Vector(
392           (currentsector->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
393     currentsector->camera->reset(
394         Vector(tux.get_pos().x, tux.get_pos().y));
395     return true;
396   }
397   if (command == "flip") {
398     FlipLevelTransformer flip_transformer;
399     flip_transformer.transform(GameSession::current()->get_current_level());
400     return true;
401   }
402   if (command == "finish") {
403     finish(true);
404     return true;
405   }
406   if (command == "camera") {
407     msg_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
408     return true;
409   }
410   if (command == "quit") {
411     main_loop->quit();
412     return true;
413   }
414
415   return false;
416 }
417
418 void
419 GameSession::check_end_conditions()
420 {
421   Player* tux = currentsector->player;
422
423   /* End of level? */
424   if(end_sequence && endsequence_timer.check()) {
425     finish(true);
426     return;
427   } else if (!end_sequence && tux->is_dead()) {
428     if (player_status->coins < 0) { 
429       // No more coins: restart level from beginning
430       player_status->coins = 0;
431       restart_level(true);
432     } else { 
433       // Still has coins: restart level from last reset point
434       restart_level(false);
435     }
436     
437     return;
438   }
439 }
440
441 void 
442 GameSession::draw(DrawingContext& context)
443 {
444   currentsector->draw(context);
445   drawstatus(context);
446
447   if(game_pause)
448     draw_pause(context);
449 }
450
451 void
452 GameSession::draw_pause(DrawingContext& context)
453 {
454   context.draw_filled_rect(
455       Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
456       Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
457 }
458   
459 void
460 GameSession::process_menu()
461 {
462   Menu* menu = Menu::current();
463   if(menu) {
464     menu->update();
465
466     if(menu == game_menu) {
467       switch (game_menu->check()) {
468         case MNID_CONTINUE:
469           Menu::set_current(0);
470           break;
471         case MNID_ABORTLEVEL:
472           Menu::set_current(0);
473           main_loop->exit_screen();
474           break;
475       }
476     }
477   }
478 }
479
480 void
481 GameSession::setup()
482 {
483   Menu::set_current(NULL);
484   current_ = this;
485
486   // Eat unneeded events
487   SDL_Event event;
488   while(SDL_PollEvent(&event))
489   {}
490 }
491
492 void
493 GameSession::update(float elapsed_time)
494 {
495   process_events();
496   process_menu();
497
498   check_end_conditions();
499
500   // handle controller
501   if(main_controller->pressed(Controller::PAUSE_MENU))
502     on_escape_press();
503   
504   // respawning in new sector?
505   if(newsector != "" && newspawnpoint != "") {
506     Sector* sector = level->get_sector(newsector);
507     if(sector == 0) {
508       msg_warning << "Sector '" << newsector << "' not found" << std::endl;
509     }
510     sector->activate(newspawnpoint);
511     sector->play_music(LEVEL_MUSIC);
512     currentsector = sector;
513     newsector = "";
514     newspawnpoint = "";
515   }
516
517   // Update the world state and all objects in the world
518   if(!game_pause) {
519     // Update the world
520     if (end_sequence == ENDSEQUENCE_RUNNING) {
521       currentsector->update(elapsed_time/2);
522     } else if(end_sequence == NO_ENDSEQUENCE) {
523       if(!currentsector->player->growing_timer.started())
524         currentsector->update(elapsed_time);
525     } 
526   }
527
528   // update sounds
529   sound_manager->set_listener_position(currentsector->player->get_pos());
530
531   /* Handle music: */
532   if (currentsector->player->invincible_timer.started() && 
533       currentsector->player->invincible_timer.get_timeleft() 
534       > TUX_INVINCIBLE_TIME_WARNING && !end_sequence) {
535     currentsector->play_music(HERRING_MUSIC);
536   } else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence) {
537     currentsector->play_music(LEVEL_MUSIC);
538   }
539 }
540
541 #if 0
542 GameSession::ExitStatus
543 GameSession::run()
544 {
545   Menu::set_current(0);
546   current_ = this;
547   
548   int fps_cnt = 0;
549
550   // Eat unneeded events
551   SDL_Event event;
552   while(SDL_PollEvent(&event))
553   {}
554
555   draw();
556
557   Uint32 fps_ticks = SDL_GetTicks();
558   Uint32 fps_nextframe_ticks = SDL_GetTicks();
559   Uint32 ticks;
560   bool skipdraw = false;
561
562   while (exit_status == ES_NONE) {
563     // we run in a logical framerate so elapsed time is a constant
564     // This will make the game run determistic and not different on different
565     // machines
566     static const float elapsed_time = 1.0 / LOGICAL_FPS;
567     // old code... float elapsed_time = float(ticks - lastticks) / 1000.;
568     if(!game_pause)
569       game_time += elapsed_time;
570
571     // regulate fps
572     ticks = SDL_GetTicks();
573     if(ticks > fps_nextframe_ticks) {
574       if(skipdraw == true) {
575         // already skipped last frame? we have to slow down the game then...
576         skipdraw = false;
577         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
578       } else {
579         // don't draw all frames when we're getting too slow
580         skipdraw = true;
581       }
582     } else {
583       skipdraw = false;
584       while(fps_nextframe_ticks > ticks) {
585         /* just wait */
586         // If we really have to wait long, then do an imprecise SDL_Delay()
587         Uint32 diff = fps_nextframe_ticks - ticks;
588         if(diff > 15) {
589           SDL_Delay(diff - 10);
590         } 
591         ticks = SDL_GetTicks();
592       }
593     }
594     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
595
596     process_events();
597     process_menu();
598
599     // Update the world state and all objects in the world
600     if(!game_pause)
601     {
602       // Update the world
603       check_end_conditions();
604       if (end_sequence == ENDSEQUENCE_RUNNING)
605         update(elapsed_time/2);
606       else if(end_sequence == NO_ENDSEQUENCE)
607         update(elapsed_time);
608     }
609
610     if(!skipdraw)
611       draw();
612
613     // update sounds
614     sound_manager->update();
615
616     /* Time stops in pause mode */
617     if(game_pause || Menu::current())
618     {
619       continue;
620     }
621
622     /* Handle music: */
623     if (currentsector->player->invincible_timer.started() && 
624             currentsector->player->invincible_timer.get_timeleft() 
625             > TUX_INVINCIBLE_TIME_WARNING && !end_sequence)
626     {
627       currentsector->play_music(HERRING_MUSIC);
628     }
629     /* or just normal music? */
630     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
631     {
632       currentsector->play_music(LEVEL_MUSIC);
633     }
634
635     /* Calculate frames per second */
636     if(config->show_fps)
637     {
638       ++fps_cnt;
639       
640       if(SDL_GetTicks() - fps_ticks >= 500)
641       {
642         fps_fps = (float) fps_cnt / .5;
643         fps_cnt = 0;
644         fps_ticks = SDL_GetTicks();
645       }
646     }
647   }
648  
649   // just in case
650   currentsector = 0;
651   main_controller->reset();
652   return exit_status;
653 }
654 #endif
655
656 void
657 GameSession::finish(bool win)
658 {
659   if(win) {
660     if(WorldMap::current())
661       WorldMap::current()->finished_level(levelfile);
662   }
663   
664   main_loop->exit_screen();
665 }
666
667 void
668 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
669 {
670   newsector = sector;
671   newspawnpoint = spawnpoint;
672 }
673
674 void
675 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
676 {
677   reset_sector = sector;
678   reset_pos = pos;
679 }
680
681 std::string
682 GameSession::get_working_directory()
683 {
684   return FileSystem::dirname(levelfile);
685 }
686
687 void
688 GameSession::display_info_box(const std::string& text)
689 {
690   InfoBox* box = new InfoBox(text);
691
692   bool running = true;
693   DrawingContext context;
694   
695   while(running)  {
696
697     // TODO make a screen out of this, another mainloop is ugly
698     main_controller->update();
699     SDL_Event event;
700     while (SDL_PollEvent(&event)) {
701       main_controller->process_event(event);
702       if(event.type == SDL_QUIT)
703         main_loop->quit();
704     }
705
706     if(main_controller->pressed(Controller::JUMP)
707         || main_controller->pressed(Controller::ACTION)
708         || main_controller->pressed(Controller::PAUSE_MENU)
709         || main_controller->pressed(Controller::MENU_SELECT))
710       running = false;
711     else if(main_controller->pressed(Controller::DOWN))
712       box->scrolldown();
713     else if(main_controller->pressed(Controller::UP))
714       box->scrollup();
715     box->draw(context);
716     draw(context);
717     context.do_drawing();
718     sound_manager->update();
719   }
720
721   delete box;
722 }
723
724 void
725 GameSession::start_sequence(const std::string& sequencename)
726 {
727   if(sequencename == "endsequence" || sequencename == "fireworks") {
728     if(end_sequence)
729       return;
730
731     end_sequence = ENDSEQUENCE_RUNNING;
732     endsequence_timer.start(7.3);
733     last_x_pos = -1;
734     sound_manager->play_music("music/leveldone.ogg", false);
735     currentsector->player->invincible_timer.start(7.3);
736
737     // Stop all clocks.
738     for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
739         i != currentsector->gameobjects.end(); ++i)
740     {
741       GameObject* obj = *i;
742
743       LevelTime* lt = dynamic_cast<LevelTime*> (obj);
744       if(lt)
745         lt->stop();
746     }
747
748     // add time spent to statistics
749     int tottime = 0, remtime = 0;
750     for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
751     {
752       Sector* sec = *i;
753
754       for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
755           j != sec->gameobjects.end(); ++j)
756       {
757         GameObject* obj = *j;
758
759         LevelTime* lt = dynamic_cast<LevelTime*> (obj);
760         if(lt)
761         {
762           tottime += int(lt->get_level_time());
763           remtime += int(lt->get_remaining_time());
764         }
765       }
766     }
767     global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime)));
768
769     if(sequencename == "fireworks") {
770       currentsector->add_object(new Fireworks());
771     }
772   } else if(sequencename == "stoptux") {
773     if(!end_sequence) {
774       msg_warning << "Final target reached without an active end sequence" << std::endl;
775       this->start_sequence("endsequence");
776     }
777     end_sequence =  ENDSEQUENCE_WAITING;
778   } else {
779     msg_warning << "Unknown sequence '" << sequencename << "'" << std::endl;
780   }
781 }
782
783 /* (Status): */
784 void
785 GameSession::drawstatus(DrawingContext& context)
786 {
787   player_status->draw(context);
788
789   if(config->show_fps) {
790     char str[60];
791     snprintf(str, sizeof(str), "%3.1f", fps_fps);
792     const char* fpstext = "FPS";
793     context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
794     context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
795   }
796
797   // draw level stats while end_sequence is running
798   if (end_sequence) {
799     global_stats.draw_endseq_panel(context, best_level_statistics, statistics_backdrop);
800   }
801 }
802