3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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 02111-1307, USA.
31 #include "worldmap.hpp"
33 #include "gettext.hpp"
35 #include "mainloop.hpp"
36 #include "video/surface.hpp"
37 #include "video/screen.hpp"
38 #include "video/drawing_context.hpp"
39 #include "sprite/sprite_manager.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "lisp/parser.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/list_iterator.hpp"
44 #include "lisp/writer.hpp"
45 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "resources.hpp"
52 #include "player_status.hpp"
53 #include "textscroller.hpp"
55 #include "spawn_point.hpp"
56 #include "file_system.hpp"
57 #include "gui/menu.hpp"
58 #include "gui/mousecursor.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "object/background.hpp"
61 #include "object/tilemap.hpp"
62 #include "script_manager.hpp"
63 #include "options_menu.hpp"
64 #include "scripting/squirrel_error.hpp"
65 #include "scripting/wrapper_util.hpp"
66 #include "worldmap/level.hpp"
67 #include "worldmap/special_tile.hpp"
68 #include "worldmap/tux.hpp"
69 #include "worldmap/sprite_change.hpp"
71 namespace WorldMapNS {
73 enum WorldMapMenuIDs {
78 WorldMap* WorldMap::current_ = NULL;
80 Direction reverse_dir(Direction direction)
99 direction_to_string(Direction direction)
117 string_to_direction(const std::string& directory)
119 if (directory == "west")
121 else if (directory == "east")
123 else if (directory == "north")
125 else if (directory == "south")
131 //---------------------------------------------------------------------------
136 tile_manager.reset(new TileManager("images/worldmap.strf"));
142 music = "music/salcon.ogg";
143 intro_displayed = false;
147 worldmap_menu.reset(new Menu());
148 worldmap_menu->add_label(_("Pause"));
149 worldmap_menu->add_hl();
150 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
151 worldmap_menu->add_submenu(_("Options"), get_options_menu());
152 worldmap_menu->add_hl();
153 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
156 WorldMap::~WorldMap()
162 for(SpawnPoints::iterator i = spawn_points.begin();
163 i != spawn_points.end(); ++i) {
169 WorldMap::add_object(GameObject* object)
171 TileMap* tilemap = dynamic_cast<TileMap*> (object);
172 if(tilemap != 0 && tilemap->is_solid()) {
176 game_objects.push_back(object);
180 WorldMap::clear_objects()
182 for(GameObjects::iterator i = game_objects.begin();
183 i != game_objects.end(); ++i)
185 game_objects.clear();
191 // Don't forget to set map_filename before calling this
195 levels_path = FileSystem::dirname(map_filename);
199 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
201 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
203 throw std::runtime_error("file isn't a supertux-level file.");
205 lisp->get("name", name);
207 const lisp::Lisp* sector = lisp->get_lisp("sector");
209 throw std::runtime_error("No sector sepcified in worldmap file.");
212 lisp::ListIterator iter(sector);
214 if(iter.item() == "tilemap") {
215 add_object(new TileMap(*(iter.lisp()), tile_manager.get()));
216 } else if(iter.item() == "background") {
217 add_object(new Background(*(iter.lisp())));
218 } else if(iter.item() == "music") {
219 iter.value()->get(music);
220 } else if(iter.item() == "intro-script") {
221 iter.value()->get(intro_script);
222 } else if(iter.item() == "worldmap-spawnpoint") {
223 SpawnPoint* sp = new SpawnPoint(iter.lisp());
224 spawn_points.push_back(sp);
225 } else if(iter.item() == "level") {
226 Level* level = new Level(levels_path, iter.lisp());
227 levels.push_back(level);
228 game_objects.push_back(level);
229 } else if(iter.item() == "special-tile") {
230 SpecialTile* special_tile = new SpecialTile(iter.lisp());
231 special_tiles.push_back(special_tile);
232 game_objects.push_back(special_tile);
233 } else if(iter.item() == "spritechange") {
234 SpriteChange* sprite_change = new SpriteChange(iter.lisp());
235 sprite_changes.push_back(sprite_change);
236 game_objects.push_back(sprite_change);
237 } else if(iter.item() == "name") {
240 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
244 throw std::runtime_error("No solid tilemap specified");
246 // search for main spawnpoint
247 for(SpawnPoints::iterator i = spawn_points.begin();
248 i != spawn_points.end(); ++i) {
250 if(sp->name == "main") {
252 tux->set_tile_pos(p);
257 } catch(std::exception& e) {
258 std::stringstream msg;
259 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
261 throw std::runtime_error(msg.str());
266 WorldMap::get_level_title(Level& level)
268 /** get special_tile's title */
269 level.title = "<no title>";
273 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
275 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
279 level_lisp->get("name", level.title);
280 } catch(std::exception& e) {
281 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
286 void WorldMap::calculate_total_stats()
289 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
292 total_stats += level->statistics;
298 WorldMap::on_escape_press()
300 // Show or hide the menu
301 if(!Menu::current()) {
302 Menu::set_current(worldmap_menu.get());
303 tux->set_direction(D_NONE); // stop tux movement when menu is called
305 Menu::set_current(NULL);
310 WorldMap::get_next_tile(Vector pos, Direction direction)
332 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
334 *new_pos = get_next_tile(old_pos, direction);
336 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
337 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
338 { // New position is outsite the tilemap
342 { // Check if the tile allows us to go to new_pos
346 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
347 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
350 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
351 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
354 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
355 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
358 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
359 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
362 assert(!"path_ok() can't work if direction is NONE");
369 WorldMap::finished_level(const std::string& filename)
371 // TODO calculate level from filename?
373 Level* level = at_level();
375 bool old_level_state = level->solved;
376 level->solved = true;
377 level->sprite->set_action("solved");
379 // deal with statistics
380 level->statistics.merge(global_stats);
381 calculate_total_stats();
384 if(World::current() != NULL)
385 World::current()->save_state();
387 if (old_level_state != level->solved && level->auto_path) {
388 // Try to detect the next direction to which we should walk
389 // FIXME: Mostly a hack
390 Direction dir = D_NONE;
392 const Tile* tile = at(tux->get_tile_pos());
394 // first, test for crossroads
395 if (tile->getData() & Tile::WORLDMAP_CNSE || tile->getData() && Tile::WORLDMAP_CNSW
396 || tile->getData() & Tile::WORLDMAP_CNEW || tile->getData() && Tile::WORLDMAP_CSEW
397 || tile->getData() & Tile::WORLDMAP_CNSEW)
399 else if (tile->getData() & Tile::WORLDMAP_NORTH
400 && tux->back_direction != D_NORTH)
402 else if (tile->getData() & Tile::WORLDMAP_SOUTH
403 && tux->back_direction != D_SOUTH)
405 else if (tile->getData() & Tile::WORLDMAP_EAST
406 && tux->back_direction != D_EAST)
408 else if (tile->getData() & Tile::WORLDMAP_WEST
409 && tux->back_direction != D_WEST)
413 tux->set_direction(dir);
417 if (level->extro_script != "") {
419 HSQUIRRELVM vm = ScriptManager::instance->create_thread();
421 std::istringstream in(level->extro_script);
422 Scripting::compile_and_run(vm, in, "worldmap,extro_script");
423 } catch(std::exception& e) {
424 log_fatal << "Couldn't run level-extro-script: " << e.what() << std::endl;
430 WorldMap::update(float delta)
432 Menu* menu = Menu::current();
436 if(menu == worldmap_menu.get()) {
437 switch (worldmap_menu->check())
439 case MNID_RETURNWORLDMAP: // Return to game
440 Menu::set_current(0);
442 case MNID_QUITWORLDMAP: // Quit Worldmap
443 main_loop->exit_screen();
451 // update GameObjects
452 for(GameObjects::iterator i = game_objects.begin();
453 i != game_objects.end(); ++i) {
454 GameObject* object = *i;
455 object->update(delta);
458 // remove old GameObjects
459 for(GameObjects::iterator i = game_objects.begin();
460 i != game_objects.end(); ) {
461 GameObject* object = *i;
462 if(!object->is_valid()) {
464 i = game_objects.erase(i);
471 Vector tux_pos = tux->get_pos();
472 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
473 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
475 if (camera_offset.x < 0)
477 if (camera_offset.y < 0)
480 if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
481 camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
482 if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
483 camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
486 bool enter_level = false;
487 if(main_controller->pressed(Controller::ACTION)
488 || main_controller->pressed(Controller::JUMP)
489 || main_controller->pressed(Controller::MENU_SELECT))
491 if(main_controller->pressed(Controller::PAUSE_MENU))
494 if (enter_level && !tux->is_moving())
496 /* Check special tile action */
497 SpecialTile* special_tile = at_special_tile();
500 if (special_tile->teleport_dest != Vector(-1,-1))
502 // TODO: an animation, camera scrolling or a fading would be a nice touch
503 sound_manager->play("sounds/warp.wav");
504 tux->back_direction = D_NONE;
505 tux->set_tile_pos(special_tile->teleport_dest);
510 /* Check level action */
511 Level* level = at_level();
513 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
517 if (level->pos == tux->get_tile_pos()) {
518 // do a shriking fade to the level
519 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
520 (level->pos.y*32 + 16 + offset.y)), 500);
523 GameSession *session =
524 new GameSession(levels_path + level->name,
525 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
526 main_loop->push_screen(session);
527 } catch(std::exception& e) {
528 log_fatal << "Couldn't load level: " << e.what() << std::endl;
534 // tux->set_direction(input_direction);
539 WorldMap::at(Vector p)
541 return solids->get_tile((int) p.x, (int) p.y);
547 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
549 if (level->pos == tux->get_tile_pos())
557 WorldMap::at_special_tile()
559 for(SpecialTiles::iterator i = special_tiles.begin();
560 i != special_tiles.end(); ++i) {
561 SpecialTile* special_tile = *i;
562 if (special_tile->pos == tux->get_tile_pos())
570 WorldMap::at_sprite_change()
576 WorldMap::draw(DrawingContext& context)
578 context.push_transform();
579 context.set_translation(camera_offset);
581 for(GameObjects::iterator i = game_objects.begin();
582 i != game_objects.end(); ++i) {
583 GameObject* object = *i;
584 object->draw(context);
587 draw_status(context);
588 context.pop_transform();
592 WorldMap::draw_status(DrawingContext& context)
594 context.push_transform();
595 context.set_translation(Vector(0, 0));
597 player_status->draw(context);
599 if (!tux->is_moving()) {
600 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
603 if (level->pos == tux->get_tile_pos()) {
604 if(level->title == "")
605 get_level_title(*level);
607 context.draw_text(white_text, level->title,
608 Vector(SCREEN_WIDTH/2,
609 SCREEN_HEIGHT - white_text->get_height() - 30),
610 CENTER_ALLIGN, LAYER_FOREGROUND1);
612 level->statistics.draw_worldmap_info(context);
617 for(SpecialTiles::iterator i = special_tiles.begin();
618 i != special_tiles.end(); ++i) {
619 SpecialTile* special_tile = *i;
621 if (special_tile->pos == tux->get_tile_pos()) {
622 /* Display an in-map message in the map, if any as been selected */
623 if(!special_tile->map_message.empty() && !special_tile->passive_message)
624 context.draw_text(gold_text, special_tile->map_message,
625 Vector(SCREEN_WIDTH/2,
626 SCREEN_HEIGHT - white_text->get_height() - 60),
627 CENTER_ALLIGN, LAYER_FOREGROUND1);
633 /* Display a passive message in the map, if needed */
634 if(passive_message_timer.started())
635 context.draw_text(gold_text, passive_message,
636 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
637 CENTER_ALLIGN, LAYER_FOREGROUND1);
639 context.pop_transform();
645 sound_manager->play_music(music);
646 Menu::set_current(NULL);
652 static void store_float(HSQUIRRELVM vm, const char* name, float val)
654 sq_pushstring(vm, name, -1);
655 sq_pushfloat(vm, val);
656 if(SQ_FAILED(sq_createslot(vm, -3)))
657 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
661 static void store_int(HSQUIRRELVM vm, const char* name, int val)
663 sq_pushstring(vm, name, -1);
664 sq_pushinteger(vm, val);
665 if(SQ_FAILED(sq_createslot(vm, -3)))
666 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
670 static void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
672 sq_pushstring(vm, name, -1);
673 sq_pushstring(vm, val.c_str(), val.length());
674 if(SQ_FAILED(sq_createslot(vm, -3)))
675 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
678 static void store_bool(HSQUIRRELVM vm, const char* name, bool val)
680 sq_pushstring(vm, name, -1);
681 sq_pushbool(vm, val ? SQTrue : SQFalse);
682 if(SQ_FAILED(sq_createslot(vm, -3)))
683 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
686 static float read_float(HSQUIRRELVM vm, const char* name)
688 sq_pushstring(vm, name, -1);
689 if(SQ_FAILED(sq_get(vm, -2))) {
690 std::ostringstream msg;
691 msg << "Couldn't get float value for '" << name << "' from table";
692 throw Scripting::SquirrelError(vm, msg.str());
696 if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
697 std::ostringstream msg;
698 msg << "Couldn't get float value for '" << name << "' from table";
699 throw Scripting::SquirrelError(vm, msg.str());
706 static std::string read_string(HSQUIRRELVM vm, const char* name)
708 sq_pushstring(vm, name, -1);
709 if(SQ_FAILED(sq_get(vm, -2))) {
710 std::ostringstream msg;
711 msg << "Couldn't get string value for '" << name << "' from table";
712 throw Scripting::SquirrelError(vm, msg.str());
716 if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
717 std::ostringstream msg;
718 msg << "Couldn't get string value for '" << name << "' from table";
719 throw Scripting::SquirrelError(vm, msg.str());
723 return std::string(result);
726 static bool read_bool(HSQUIRRELVM vm, const char* name)
728 sq_pushstring(vm, name, -1);
729 if(SQ_FAILED(sq_get(vm, -2))) {
730 std::ostringstream msg;
731 msg << "Couldn't get bool value for '" << name << "' from table";
732 throw Scripting::SquirrelError(vm, msg.str());
736 if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
737 std::ostringstream msg;
738 msg << "Couldn't get bool value for '" << name << "' from table";
739 throw Scripting::SquirrelError(vm, msg.str());
743 return result == SQTrue;
747 WorldMap::save_state()
749 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
750 int oldtop = sq_gettop(vm);
754 sq_pushroottable(vm);
755 sq_pushstring(vm, "state", -1);
756 if(SQ_FAILED(sq_get(vm, -2)))
757 throw Scripting::SquirrelError(vm, "Couldn't get state table");
759 // get or create worlds table
760 sq_pushstring(vm, "worlds", -1);
761 if(SQ_FAILED(sq_get(vm, -2))) {
762 sq_pushstring(vm, "worlds", -1);
764 if(SQ_FAILED(sq_createslot(vm, -3)))
765 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
767 sq_pushstring(vm, "worlds", -1);
768 if(SQ_FAILED(sq_get(vm, -2)))
769 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
772 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
773 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
776 // construct new table for this worldmap
777 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
781 sq_pushstring(vm, "tux", -1);
784 store_float(vm, "x", tux->get_tile_pos().x);
785 store_float(vm, "y", tux->get_tile_pos().y);
786 store_string(vm, "back", direction_to_string(tux->back_direction));
788 sq_createslot(vm, -3);
791 sq_pushstring(vm, "levels", -1);
794 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
798 sq_pushstring(vm, level->name.c_str(), -1);
801 store_bool(vm, "solved", true);
802 // TODO write statistics
803 // i->statistics.write(writer);
805 sq_createslot(vm, -3);
809 sq_createslot(vm, -3);
811 // push world into worlds table
812 sq_createslot(vm, -3);
813 } catch(std::exception& e) {
814 sq_settop(vm, oldtop);
817 sq_settop(vm, oldtop);
821 WorldMap::load_state()
823 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
824 int oldtop = sq_gettop(vm);
828 sq_pushroottable(vm);
829 sq_pushstring(vm, "state", -1);
830 if(SQ_FAILED(sq_get(vm, -2)))
831 throw Scripting::SquirrelError(vm, "Couldn't get state table");
834 sq_pushstring(vm, "worlds", -1);
835 if(SQ_FAILED(sq_get(vm, -2)))
836 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
838 // get table for our world
839 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
840 if(SQ_FAILED(sq_get(vm, -2)))
841 throw Scripting::SquirrelError(vm, "Couldn't get state.world.mapfilename");
844 sq_pushstring(vm, "tux", -1);
845 if(SQ_FAILED(sq_get(vm, -2)))
846 throw Scripting::SquirrelError(vm, "Couldn't get tux");
849 p.x = read_float(vm, "x");
850 p.y = read_float(vm, "y");
851 std::string back_str = read_string(vm, "back");
852 tux->back_direction = string_to_direction(back_str);
853 tux->set_tile_pos(p);
858 sq_pushstring(vm, "levels", -1);
859 if(SQ_FAILED(sq_get(vm, -2)))
860 throw Scripting::SquirrelError(vm, "Couldn't get levels");
862 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
864 sq_pushstring(vm, level->name.c_str(), -1);
865 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
866 level->solved = read_bool(vm, "solved");
867 level->sprite->set_action(level->solved ? "solved" : "default");
868 // i->statistics.parse(*level);
874 } catch(std::exception& e) {
875 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
877 sq_settop(vm, oldtop);
881 WorldMap::level_count()
883 return levels.size();
887 WorldMap::solved_level_count()
890 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
901 WorldMap::loadmap(const std::string& filename)
904 map_filename = filename;
908 } // namespace WorldMapNS