1 // SuperTux - A Jump'n Run
2 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
3 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "worldmap/worldmap.hpp"
31 #include "audio/sound_manager.hpp"
32 #include "control/joystickkeyboardcontroller.hpp"
33 #include "gui/menu.hpp"
34 #include "gui/menu_manager.hpp"
35 #include "gui/mousecursor.hpp"
36 #include "lisp/lisp.hpp"
37 #include "lisp/list_iterator.hpp"
38 #include "lisp/parser.hpp"
39 #include "object/background.hpp"
40 #include "object/tilemap.hpp"
41 #include "physfs/ifile_stream.hpp"
42 #include "scripting/squirrel_error.hpp"
43 #include "scripting/squirrel_util.hpp"
44 #include "sprite/sprite.hpp"
45 #include "sprite/sprite_manager.hpp"
46 #include "supertux/game_session.hpp"
47 #include "supertux/globals.hpp"
48 #include "supertux/mainloop.hpp"
49 #include "supertux/menu/menu_storage.hpp"
50 #include "supertux/menu/options_menu.hpp"
51 #include "supertux/menu/worldmap_menu.hpp"
52 #include "supertux/player_status.hpp"
53 #include "supertux/resources.hpp"
54 #include "supertux/sector.hpp"
55 #include "supertux/shrinkfade.hpp"
56 #include "supertux/spawn_point.hpp"
57 #include "supertux/textscroller.hpp"
58 #include "supertux/tile_manager.hpp"
59 #include "supertux/tile_set.hpp"
60 #include "supertux/world.hpp"
61 #include "util/file_system.hpp"
62 #include "util/gettext.hpp"
63 #include "util/log.hpp"
64 #include "util/reader.hpp"
65 #include "video/drawing_context.hpp"
66 #include "video/surface.hpp"
67 #include "worldmap/level.hpp"
68 #include "worldmap/special_tile.hpp"
69 #include "worldmap/sprite_change.hpp"
70 #include "worldmap/tux.hpp"
71 #include "worldmap/worldmap.hpp"
73 static const float CAMERA_PAN_SPEED = 5.0;
75 namespace WorldMapNS {
77 WorldMap* WorldMap::current_ = NULL;
79 Direction reverse_dir(Direction direction)
98 direction_to_string(Direction direction)
116 string_to_direction(const std::string& directory)
118 if (directory == "west")
120 else if (directory == "east")
122 else if (directory == "north")
124 else if (directory == "south")
126 else if (directory == "none")
129 log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
134 //---------------------------------------------------------------------------
136 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint) :
147 passive_message_timer(),
159 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ),
160 force_spawnpoint(force_spawnpoint),
169 music = "music/salcon.ogg";
173 worldmap_menu.reset(new WorldmapMenu());
175 // create a new squirrel table for the worldmap
176 using namespace Scripting;
178 sq_collectgarbage(global_vm);
179 sq_newtable(global_vm);
180 sq_pushroottable(global_vm);
181 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
182 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
184 sq_resetobject(&worldmap_table);
185 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
186 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
188 sq_addref(global_vm, &worldmap_table);
189 sq_pop(global_vm, 1);
191 sound_manager->preload("sounds/warp.wav");
193 // load worldmap objects
197 WorldMap::~WorldMap()
199 using namespace Scripting;
204 for(GameObjects::iterator i = game_objects.begin();
205 i != game_objects.end(); ++i) {
206 GameObject* object = *i;
207 try_unexpose(object);
211 for(SpawnPoints::iterator i = spawn_points.begin();
212 i != spawn_points.end(); ++i) {
216 for(ScriptList::iterator i = scripts.begin();
217 i != scripts.end(); ++i) {
218 HSQOBJECT& object = *i;
219 sq_release(global_vm, &object);
221 sq_release(global_vm, &worldmap_table);
223 sq_collectgarbage(global_vm);
230 WorldMap::add_object(GameObject* object)
232 TileMap* tilemap = dynamic_cast<TileMap*> (object);
233 if(tilemap != 0 && tilemap->is_solid()) {
234 solid_tilemaps.push_back(tilemap);
239 game_objects.push_back(object);
243 WorldMap::try_expose(GameObject* object)
245 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
246 if(interface != NULL) {
247 HSQUIRRELVM vm = Scripting::global_vm;
248 sq_pushobject(vm, worldmap_table);
249 interface->expose(vm, -1);
255 WorldMap::try_unexpose(GameObject* object)
257 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
258 if(interface != NULL) {
259 HSQUIRRELVM vm = Scripting::global_vm;
260 SQInteger oldtop = sq_gettop(vm);
261 sq_pushobject(vm, worldmap_table);
263 interface->unexpose(vm, -1);
264 } catch(std::exception& e) {
265 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
267 sq_settop(vm, oldtop);
272 WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
274 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
276 if(sp->name == spawnpoint) {
278 tux->set_tile_pos(p);
279 tux->set_direction(sp->auto_dir);
282 pan_pos = get_camera_pos_for_tux();
283 clamp_camera_position(pan_pos);
288 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
289 if (spawnpoint != "main") {
290 move_to_spawnpoint("main");
295 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
297 g_main_loop->exit_screen();
298 g_main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
302 WorldMap::load(const std::string& filename)
304 map_filename = filename;
305 levels_path = FileSystem::dirname(map_filename);
309 const lisp::Lisp* root = parser.parse(map_filename);
311 const lisp::Lisp* level = root->get_lisp("supertux-level");
313 throw std::runtime_error("file isn't a supertux-level file.");
315 level->get("name", name);
317 const lisp::Lisp* sector = level->get_lisp("sector");
319 throw std::runtime_error("No sector specified in worldmap file.");
321 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
322 if(tilesets_lisp != NULL) {
323 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
326 std::string tileset_name;
327 if(level->get("tileset", tileset_name)) {
328 if(tileset != NULL) {
329 log_warning << "multiple tilesets specified in level" << std::endl;
331 tileset = tile_manager->get_tileset(tileset_name);
334 /* load default tileset */
335 if(tileset == NULL) {
336 tileset = tile_manager->get_tileset("images/worldmap.strf");
338 current_tileset = tileset;
340 lisp::ListIterator iter(sector);
342 if(iter.item() == "tilemap") {
343 add_object(new TileMap(*(iter.lisp())));
344 } else if(iter.item() == "background") {
345 add_object(new Background(*(iter.lisp())));
346 } else if(iter.item() == "music") {
347 iter.value()->get(music);
348 } else if(iter.item() == "init-script") {
349 iter.value()->get(init_script);
350 } else if(iter.item() == "worldmap-spawnpoint") {
351 SpawnPoint* sp = new SpawnPoint(*iter.lisp());
352 spawn_points.push_back(sp);
353 } else if(iter.item() == "level") {
354 LevelTile* level = new LevelTile(levels_path, *iter.lisp());
355 levels.push_back(level);
357 } else if(iter.item() == "special-tile") {
358 SpecialTile* special_tile = new SpecialTile(*iter.lisp());
359 special_tiles.push_back(special_tile);
360 add_object(special_tile);
361 } else if(iter.item() == "sprite-change") {
362 SpriteChange* sprite_change = new SpriteChange(*iter.lisp());
363 sprite_changes.push_back(sprite_change);
364 add_object(sprite_change);
365 } else if(iter.item() == "teleporter") {
366 Teleporter* teleporter = new Teleporter(*iter.lisp());
367 teleporters.push_back(teleporter);
368 add_object(teleporter);
369 } else if(iter.item() == "ambient-light") {
370 std::vector<float> vColor;
371 sector->get( "ambient-light", vColor );
372 if(vColor.size() < 3) {
373 log_warning << "(ambient-light) requires a color as argument" << std::endl;
375 ambient_light = Color( vColor );
377 } else if(iter.item() == "name") {
380 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
383 current_tileset = NULL;
385 if(solid_tilemaps.size() == 0)
386 throw std::runtime_error("No solid tilemap specified");
388 move_to_spawnpoint("main");
390 } catch(std::exception& e) {
391 std::stringstream msg;
392 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
394 throw std::runtime_error(msg.str());
399 WorldMap::get_level_title(LevelTile& level)
401 /** get special_tile's title */
402 level.title = "<no title>";
406 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
408 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
412 level_lisp->get("name", level.title);
413 } catch(std::exception& e) {
414 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
419 void WorldMap::calculate_total_stats()
422 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
423 LevelTile* level = *i;
425 total_stats += level->statistics;
431 WorldMap::on_escape_press()
433 // Show or hide the menu
434 if(!MenuManager::current()) {
435 MenuManager::set_current(worldmap_menu.get());
436 tux->set_direction(D_NONE); // stop tux movement when menu is called
438 MenuManager::set_current(NULL);
443 WorldMap::get_next_tile(Vector pos, Direction direction)
465 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
467 *new_pos = get_next_tile(old_pos, direction);
469 if (!(new_pos->x >= 0 && new_pos->x < get_width()
470 && new_pos->y >= 0 && new_pos->y < get_height()))
471 { // New position is outsite the tilemap
475 { // Check if the tile allows us to go to new_pos
476 int old_tile_data = tile_data_at(old_pos);
477 int new_tile_data = tile_data_at(*new_pos);
481 return (old_tile_data & Tile::WORLDMAP_WEST
482 && new_tile_data & Tile::WORLDMAP_EAST);
485 return (old_tile_data & Tile::WORLDMAP_EAST
486 && new_tile_data & Tile::WORLDMAP_WEST);
489 return (old_tile_data & Tile::WORLDMAP_NORTH
490 && new_tile_data & Tile::WORLDMAP_SOUTH);
493 return (old_tile_data & Tile::WORLDMAP_SOUTH
494 && new_tile_data & Tile::WORLDMAP_NORTH);
497 assert(!"path_ok() can't walk if direction is NONE");
504 WorldMap::finished_level(Level* gamelevel)
506 // TODO use Level* parameter here?
507 LevelTile* level = at_level();
509 bool old_level_state = level->solved;
510 level->solved = true;
511 level->sprite->set_action("solved");
513 // deal with statistics
514 level->statistics.merge(gamelevel->stats);
515 calculate_total_stats();
519 if (old_level_state != level->solved) {
520 // Try to detect the next direction to which we should walk
521 // FIXME: Mostly a hack
522 Direction dir = D_NONE;
524 int dirdata = available_directions_at(tux->get_tile_pos());
525 // first, test for crossroads
526 if (dirdata == Tile::WORLDMAP_CNSE ||
527 dirdata == Tile::WORLDMAP_CNSW ||
528 dirdata == Tile::WORLDMAP_CNEW ||
529 dirdata == Tile::WORLDMAP_CSEW ||
530 dirdata == Tile::WORLDMAP_CNSEW)
532 else if (dirdata & Tile::WORLDMAP_NORTH
533 && tux->back_direction != D_NORTH)
535 else if (dirdata & Tile::WORLDMAP_SOUTH
536 && tux->back_direction != D_SOUTH)
538 else if (dirdata & Tile::WORLDMAP_EAST
539 && tux->back_direction != D_EAST)
541 else if (dirdata & Tile::WORLDMAP_WEST
542 && tux->back_direction != D_WEST)
546 tux->set_direction(dir);
550 if (level->extro_script != "") {
552 std::istringstream in(level->extro_script);
553 run_script(in, "worldmap:extro_script");
554 } catch(std::exception& e) {
555 log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
561 WorldMap::get_camera_pos_for_tux() {
562 Vector camera_offset;
563 Vector tux_pos = tux->get_pos();
564 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
565 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
566 return camera_offset;
570 WorldMap::clamp_camera_position(Vector& c) {
576 if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
577 c.x = (int)get_width()*32 - SCREEN_WIDTH;
578 if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
579 c.y = (int)get_height()*32 - SCREEN_HEIGHT;
581 if (int(get_width()*32) < SCREEN_WIDTH)
582 c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
583 if (int(get_height()*32) < SCREEN_HEIGHT)
584 c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
588 WorldMap::update(float delta)
591 Menu* menu = MenuManager::current();
593 if(menu == worldmap_menu.get()) {
594 switch (worldmap_menu->check())
596 case MNID_RETURNWORLDMAP: // Return to game
597 MenuManager::set_current(0);
599 case MNID_QUITWORLDMAP: // Quit Worldmap
600 g_main_loop->exit_screen();
608 // update GameObjects
609 for(size_t i = 0; i < game_objects.size(); ++i) {
610 GameObject* object = game_objects[i];
611 if(!panning || object != tux) {
612 object->update(delta);
616 // remove old GameObjects
617 for(GameObjects::iterator i = game_objects.begin();
618 i != game_objects.end(); ) {
619 GameObject* object = *i;
620 if(!object->is_valid()) {
621 try_unexpose(object);
623 i = game_objects.erase(i);
629 /* update solid_tilemaps list */
630 //FIXME: this could be more efficient
631 solid_tilemaps.clear();
632 for(std::vector<GameObject*>::iterator i = game_objects.begin();
633 i != game_objects.end(); ++i)
635 TileMap* tm = dynamic_cast<TileMap*>(*i);
637 if (tm->is_solid()) solid_tilemaps.push_back(tm);
640 Vector requested_pos;
644 camera_offset = get_camera_pos_for_tux();
646 Vector delta = pan_pos - camera_offset;
647 float mag = delta.norm();
648 if(mag > CAMERA_PAN_SPEED) {
649 delta *= CAMERA_PAN_SPEED/mag;
651 camera_offset += delta;
652 if(camera_offset == pan_pos) {
657 requested_pos = camera_offset;
658 clamp_camera_position(camera_offset);
661 if(requested_pos.x != camera_offset.x) {
662 pan_pos.x = camera_offset.x;
664 if(requested_pos.y != camera_offset.y) {
665 pan_pos.y = camera_offset.y;
670 bool enter_level = false;
671 if(g_main_controller->pressed(Controller::ACTION)
672 || g_main_controller->pressed(Controller::JUMP)
673 || g_main_controller->pressed(Controller::MENU_SELECT)) {
674 /* some people define UP and JUMP on the same key... */
675 if(!g_main_controller->pressed(Controller::UP))
678 if(g_main_controller->pressed(Controller::PAUSE_MENU))
681 // check for teleporters
682 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
683 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
685 if (teleporter->worldmap != "") {
686 change(teleporter->worldmap, teleporter->spawnpoint);
688 // TODO: an animation, camera scrolling or a fading would be a nice touch
689 sound_manager->play("sounds/warp.wav");
690 tux->back_direction = D_NONE;
691 move_to_spawnpoint(teleporter->spawnpoint, true);
695 // check for auto-play levels
696 LevelTile* level = at_level();
697 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
701 if (enter_level && !tux->is_moving())
703 /* Check level action */
704 LevelTile* level = at_level();
706 //Respawn if player on a tile with no level and nowhere to go.
707 int tile_data = tile_data_at(tux->get_tile_pos());
708 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
709 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
710 move_to_spawnpoint("main");
713 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
717 if (level->pos == tux->get_tile_pos()) {
719 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
720 level->pos.y*32 + 8 - camera_offset.y);
721 std::string levelfile = levels_path + level->get_name();
723 // update state and savegame
726 g_main_loop->push_screen(new GameSession(levelfile, &level->statistics),
727 new ShrinkFade(shrinkpos, 1.0f));
729 } catch(std::exception& e) {
730 log_fatal << "Couldn't load level: " << e.what() << std::endl;
736 // tux->set_direction(input_direction);
742 WorldMap::tile_data_at(Vector p)
746 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
747 TileMap* tilemap = *i;
748 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
749 int dirdata = tile->getData();
757 WorldMap::available_directions_at(Vector p)
759 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
765 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
766 LevelTile* level = *i;
767 if (level->pos == tux->get_tile_pos())
775 WorldMap::at_special_tile()
777 for(SpecialTiles::iterator i = special_tiles.begin();
778 i != special_tiles.end(); ++i) {
779 SpecialTile* special_tile = *i;
780 if (special_tile->pos == tux->get_tile_pos())
788 WorldMap::at_sprite_change(const Vector& pos)
790 for(SpriteChanges::iterator i = sprite_changes.begin();
791 i != sprite_changes.end(); ++i) {
792 SpriteChange* sprite_change = *i;
793 if(sprite_change->pos == pos)
794 return sprite_change;
801 WorldMap::at_teleporter(const Vector& pos)
803 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
804 Teleporter* teleporter = *i;
805 if(teleporter->pos == pos) return teleporter;
812 WorldMap::draw(DrawingContext& context)
814 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
815 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
816 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
818 context.set_ambient_color( ambient_light );
819 context.push_transform();
820 context.set_translation(camera_offset);
822 for(GameObjects::iterator i = game_objects.begin();
823 i != game_objects.end(); ++i) {
824 GameObject* object = *i;
825 if(!panning || object != tux) {
826 object->draw(context);
831 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
832 // draw visual indication of possible walk directions
833 static int flipme = 0;
835 for (int x = 0; x < get_width(); x++) {
836 for (int y = 0; y < get_height(); y++) {
837 int data = tile_data_at(Vector(x,y));
841 if (data & Tile::WORLDMAP_NORTH) context.draw_filled_rect(Rect(px + 16-W, py , px + 16+W, py + 16-W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
842 if (data & Tile::WORLDMAP_SOUTH) context.draw_filled_rect(Rect(px + 16-W, py + 16+W, px + 16+W, py + 32 ), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
843 if (data & Tile::WORLDMAP_EAST) context.draw_filled_rect(Rect(px + 16+W, py + 16-W, px + 32 , py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
844 if (data & Tile::WORLDMAP_WEST) context.draw_filled_rect(Rect(px , py + 16-W, px + 16-W, py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
845 if (data & Tile::WORLDMAP_DIR_MASK) context.draw_filled_rect(Rect(px + 16-W, py + 16-W, px + 16+W, py + 16+W), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
846 if (data & Tile::WORLDMAP_STOP) context.draw_filled_rect(Rect(px + 4 , py + 4 , px + 28 , py + 28 ), Color(0.2f, 0.2f, 0.2f, 0.7f), LAYER_FOREGROUND1 + 1000);
851 draw_status(context);
852 context.pop_transform();
856 WorldMap::draw_status(DrawingContext& context)
858 context.push_transform();
859 context.set_translation(Vector(0, 0));
861 player_status->draw(context);
863 if (!tux->is_moving()) {
864 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
865 LevelTile* level = *i;
867 if (level->pos == tux->get_tile_pos()) {
868 if(level->title == "")
869 get_level_title(*level);
871 context.draw_text(Resources::normal_font, level->title,
872 Vector(SCREEN_WIDTH/2,
873 SCREEN_HEIGHT - Resources::normal_font->get_height() - 30),
874 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
876 // if level is solved, draw level picture behind stats
879 if (const Surface* picture = level->get_picture()) {
880 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
881 context.push_transform();
882 context.set_alpha(0.5);
883 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
884 context.pop_transform();
889 level->statistics.draw_worldmap_info(context);
894 for(SpecialTiles::iterator i = special_tiles.begin();
895 i != special_tiles.end(); ++i) {
896 SpecialTile* special_tile = *i;
898 if (special_tile->pos == tux->get_tile_pos()) {
899 /* Display an in-map message in the map, if any as been selected */
900 if(!special_tile->map_message.empty() && !special_tile->passive_message)
901 context.draw_text(Resources::normal_font, special_tile->map_message,
902 Vector(SCREEN_WIDTH/2,
903 SCREEN_HEIGHT - Resources::normal_font->get_height() - 60),
904 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
909 // display teleporter messages
910 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
911 if (teleporter && (teleporter->message != "")) {
912 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - Resources::normal_font->get_height() - 30);
913 context.draw_text(Resources::normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
918 /* Display a passive message in the map, if needed */
919 if(passive_message_timer.started())
920 context.draw_text(Resources::normal_font, passive_message,
921 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - Resources::normal_font->get_height() - 60),
922 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
924 context.pop_transform();
930 sound_manager->play_music(music);
931 MenuManager::set_current(NULL);
936 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
937 if (force_spawnpoint != "") {
938 move_to_spawnpoint(force_spawnpoint);
939 force_spawnpoint = "";
944 // register worldmap_table as worldmap in scripting
945 using namespace Scripting;
947 sq_pushroottable(global_vm);
948 sq_pushstring(global_vm, "worldmap", -1);
949 sq_pushobject(global_vm, worldmap_table);
950 if(SQ_FAILED(sq_createslot(global_vm, -3)))
951 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
952 sq_pop(global_vm, 1);
954 //Run default.nut just before init script
956 IFileStream in(levels_path + "/default.nut");
957 run_script(in, "WorldMap::default.nut");
958 } catch(std::exception& ) {
959 // doesn't exist or erroneous; do nothing
962 if(init_script != "") {
963 std::istringstream in(init_script);
964 run_script(in, "WorldMap::init");
971 using namespace Scripting;
973 // save state of world and player
976 // remove worldmap_table from roottable
977 sq_pushroottable(global_vm);
978 sq_pushstring(global_vm, "worldmap", -1);
979 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
980 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
981 sq_pop(global_vm, 1);
985 WorldMap::save_state()
987 using namespace Scripting;
989 HSQUIRRELVM vm = global_vm;
990 int oldtop = sq_gettop(vm);
994 sq_pushroottable(vm);
995 sq_pushstring(vm, "state", -1);
996 if(SQ_FAILED(sq_get(vm, -2)))
997 throw Scripting::SquirrelError(vm, "Couldn't get state table");
999 // get or create worlds table
1000 sq_pushstring(vm, "worlds", -1);
1001 if(SQ_FAILED(sq_get(vm, -2))) {
1002 sq_pushstring(vm, "worlds", -1);
1004 if(SQ_FAILED(sq_createslot(vm, -3)))
1005 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
1007 sq_pushstring(vm, "worlds", -1);
1008 if(SQ_FAILED(sq_get(vm, -2)))
1009 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1012 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1013 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1016 // construct new table for this worldmap
1017 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1021 sq_pushstring(vm, "tux", -1);
1024 store_float(vm, "x", tux->get_tile_pos().x);
1025 store_float(vm, "y", tux->get_tile_pos().y);
1026 store_string(vm, "back", direction_to_string(tux->back_direction));
1028 sq_createslot(vm, -3);
1031 sq_pushstring(vm, "levels", -1);
1034 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1035 LevelTile* level = *i;
1037 sq_pushstring(vm, level->get_name().c_str(), -1);
1040 store_bool(vm, "solved", level->solved);
1041 level->statistics.serialize_to_squirrel(vm);
1043 sq_createslot(vm, -3);
1046 sq_createslot(vm, -3);
1048 // overall statistics...
1049 total_stats.serialize_to_squirrel(vm);
1051 // push world into worlds table
1052 sq_createslot(vm, -3);
1053 } catch(std::exception& ) {
1054 sq_settop(vm, oldtop);
1057 sq_settop(vm, oldtop);
1059 if(World::current() != NULL)
1060 World::current()->save_state();
1064 WorldMap::load_state()
1066 using namespace Scripting;
1068 HSQUIRRELVM vm = global_vm;
1069 int oldtop = sq_gettop(vm);
1073 sq_pushroottable(vm);
1074 sq_pushstring(vm, "state", -1);
1075 if(SQ_FAILED(sq_get(vm, -2)))
1076 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1079 sq_pushstring(vm, "worlds", -1);
1080 if(SQ_FAILED(sq_get(vm, -2)))
1081 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1083 // get table for our world
1084 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1085 if(SQ_FAILED(sq_get(vm, -2)))
1086 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1089 sq_pushstring(vm, "tux", -1);
1090 if(SQ_FAILED(sq_get(vm, -2)))
1091 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1094 p.x = read_float(vm, "x");
1095 p.y = read_float(vm, "y");
1096 std::string back_str = read_string(vm, "back");
1097 tux->back_direction = string_to_direction(back_str);
1098 tux->set_tile_pos(p);
1103 sq_pushstring(vm, "levels", -1);
1104 if(SQ_FAILED(sq_get(vm, -2)))
1105 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1107 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1108 LevelTile* level = *i;
1109 sq_pushstring(vm, level->get_name().c_str(), -1);
1110 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1111 level->solved = read_bool(vm, "solved");
1112 level->sprite->set_action(level->solved ? "solved" : "default");
1113 level->statistics.unserialize_from_squirrel(vm);
1118 // leave state table
1121 // load overall statistics
1122 total_stats.unserialize_from_squirrel(vm);
1124 } catch(std::exception& e) {
1125 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1127 sq_settop(vm, oldtop);
1133 WorldMap::level_count()
1135 return levels.size();
1139 WorldMap::solved_level_count()
1142 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1143 LevelTile* level = *i;
1153 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1155 using namespace Scripting;
1157 // garbage collect thread list
1158 for(ScriptList::iterator i = scripts.begin();
1159 i != scripts.end(); ) {
1160 HSQOBJECT& object = *i;
1161 HSQUIRRELVM vm = object_to_vm(object);
1163 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1164 sq_release(global_vm, &object);
1165 i = scripts.erase(i);
1172 HSQOBJECT object = create_thread(global_vm);
1173 scripts.push_back(object);
1175 HSQUIRRELVM vm = object_to_vm(object);
1177 // set worldmap_table as roottable for the thread
1178 sq_pushobject(vm, worldmap_table);
1179 sq_setroottable(vm);
1181 compile_and_run(vm, in, sourcename);
1187 WorldMap::get_width() const
1190 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1191 TileMap* solids = *i;
1192 if (solids->get_width() > width) width = solids->get_width();
1198 WorldMap::get_height() const
1201 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1202 TileMap* solids = *i;
1203 if (solids->get_height() > height) height = solids->get_height();
1208 } // namespace WorldMapNS