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/mousecursor.hpp"
35 #include "lisp/lisp.hpp"
36 #include "lisp/list_iterator.hpp"
37 #include "lisp/parser.hpp"
38 #include "object/background.hpp"
39 #include "object/tilemap.hpp"
40 #include "physfs/ifile_stream.hpp"
41 #include "scripting/squirrel_error.hpp"
42 #include "scripting/squirrel_util.hpp"
43 #include "sprite/sprite.hpp"
44 #include "sprite/sprite_manager.hpp"
45 #include "supertux/game_session.hpp"
46 #include "supertux/globals.hpp"
47 #include "supertux/mainloop.hpp"
48 #include "supertux/menu/menu_manager.hpp"
49 #include "supertux/menu/options_menu.hpp"
50 #include "supertux/player_status.hpp"
51 #include "supertux/resources.hpp"
52 #include "supertux/sector.hpp"
53 #include "supertux/shrinkfade.hpp"
54 #include "supertux/spawn_point.hpp"
55 #include "supertux/textscroller.hpp"
56 #include "supertux/tile_manager.hpp"
57 #include "supertux/tile_set.hpp"
58 #include "supertux/world.hpp"
59 #include "util/file_system.hpp"
60 #include "util/gettext.hpp"
61 #include "util/log.hpp"
62 #include "util/reader.hpp"
63 #include "video/drawing_context.hpp"
64 #include "video/surface.hpp"
65 #include "worldmap/level.hpp"
66 #include "worldmap/special_tile.hpp"
67 #include "worldmap/sprite_change.hpp"
68 #include "worldmap/tux.hpp"
69 #include "worldmap/worldmap.hpp"
71 static const float CAMERA_PAN_SPEED = 5.0;
73 namespace WorldMapNS {
75 enum WorldMapMenuIDs {
80 WorldMap* WorldMap::current_ = NULL;
82 Direction reverse_dir(Direction direction)
101 direction_to_string(Direction direction)
119 string_to_direction(const std::string& directory)
121 if (directory == "west")
123 else if (directory == "east")
125 else if (directory == "north")
127 else if (directory == "south")
129 else if (directory == "none")
132 log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
137 //---------------------------------------------------------------------------
139 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint) :
150 passive_message_timer(),
162 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ),
163 force_spawnpoint(force_spawnpoint),
172 music = "music/salcon.ogg";
176 worldmap_menu.reset(new Menu());
177 worldmap_menu->add_label(_("Pause"));
178 worldmap_menu->add_hl();
179 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
180 worldmap_menu->add_submenu(_("Options"), MenuManager::get_options_menu());
181 worldmap_menu->add_hl();
182 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
184 // create a new squirrel table for the worldmap
185 using namespace Scripting;
187 sq_collectgarbage(global_vm);
188 sq_newtable(global_vm);
189 sq_pushroottable(global_vm);
190 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
191 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
193 sq_resetobject(&worldmap_table);
194 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
195 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
197 sq_addref(global_vm, &worldmap_table);
198 sq_pop(global_vm, 1);
200 sound_manager->preload("sounds/warp.wav");
202 // load worldmap objects
206 WorldMap::~WorldMap()
208 using namespace Scripting;
213 for(GameObjects::iterator i = game_objects.begin();
214 i != game_objects.end(); ++i) {
215 GameObject* object = *i;
216 try_unexpose(object);
220 for(SpawnPoints::iterator i = spawn_points.begin();
221 i != spawn_points.end(); ++i) {
225 for(ScriptList::iterator i = scripts.begin();
226 i != scripts.end(); ++i) {
227 HSQOBJECT& object = *i;
228 sq_release(global_vm, &object);
230 sq_release(global_vm, &worldmap_table);
232 sq_collectgarbage(global_vm);
239 WorldMap::add_object(GameObject* object)
241 TileMap* tilemap = dynamic_cast<TileMap*> (object);
242 if(tilemap != 0 && tilemap->is_solid()) {
243 solid_tilemaps.push_back(tilemap);
248 game_objects.push_back(object);
252 WorldMap::try_expose(GameObject* object)
254 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
255 if(interface != NULL) {
256 HSQUIRRELVM vm = Scripting::global_vm;
257 sq_pushobject(vm, worldmap_table);
258 interface->expose(vm, -1);
264 WorldMap::try_unexpose(GameObject* object)
266 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
267 if(interface != NULL) {
268 HSQUIRRELVM vm = Scripting::global_vm;
269 SQInteger oldtop = sq_gettop(vm);
270 sq_pushobject(vm, worldmap_table);
272 interface->unexpose(vm, -1);
273 } catch(std::exception& e) {
274 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
276 sq_settop(vm, oldtop);
281 WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
283 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
285 if(sp->name == spawnpoint) {
287 tux->set_tile_pos(p);
288 tux->set_direction(sp->auto_dir);
291 pan_pos = get_camera_pos_for_tux();
292 clamp_camera_position(pan_pos);
297 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
298 if (spawnpoint != "main") {
299 move_to_spawnpoint("main");
304 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
306 g_main_loop->exit_screen();
307 g_main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
311 WorldMap::load(const std::string& filename)
313 map_filename = filename;
314 levels_path = FileSystem::dirname(map_filename);
318 const lisp::Lisp* root = parser.parse(map_filename);
320 const lisp::Lisp* level = root->get_lisp("supertux-level");
322 throw std::runtime_error("file isn't a supertux-level file.");
324 level->get("name", name);
326 const lisp::Lisp* sector = level->get_lisp("sector");
328 throw std::runtime_error("No sector specified in worldmap file.");
330 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
331 if(tilesets_lisp != NULL) {
332 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
335 std::string tileset_name;
336 if(level->get("tileset", tileset_name)) {
337 if(tileset != NULL) {
338 log_warning << "multiple tilesets specified in level" << std::endl;
340 tileset = tile_manager->get_tileset(tileset_name);
343 /* load default tileset */
344 if(tileset == NULL) {
345 tileset = tile_manager->get_tileset("images/worldmap.strf");
347 current_tileset = tileset;
349 lisp::ListIterator iter(sector);
351 if(iter.item() == "tilemap") {
352 add_object(new TileMap(*(iter.lisp())));
353 } else if(iter.item() == "background") {
354 add_object(new Background(*(iter.lisp())));
355 } else if(iter.item() == "music") {
356 iter.value()->get(music);
357 } else if(iter.item() == "init-script") {
358 iter.value()->get(init_script);
359 } else if(iter.item() == "worldmap-spawnpoint") {
360 SpawnPoint* sp = new SpawnPoint(*iter.lisp());
361 spawn_points.push_back(sp);
362 } else if(iter.item() == "level") {
363 LevelTile* level = new LevelTile(levels_path, *iter.lisp());
364 levels.push_back(level);
366 } else if(iter.item() == "special-tile") {
367 SpecialTile* special_tile = new SpecialTile(*iter.lisp());
368 special_tiles.push_back(special_tile);
369 add_object(special_tile);
370 } else if(iter.item() == "sprite-change") {
371 SpriteChange* sprite_change = new SpriteChange(*iter.lisp());
372 sprite_changes.push_back(sprite_change);
373 add_object(sprite_change);
374 } else if(iter.item() == "teleporter") {
375 Teleporter* teleporter = new Teleporter(*iter.lisp());
376 teleporters.push_back(teleporter);
377 add_object(teleporter);
378 } else if(iter.item() == "ambient-light") {
379 std::vector<float> vColor;
380 sector->get( "ambient-light", vColor );
381 if(vColor.size() < 3) {
382 log_warning << "(ambient-light) requires a color as argument" << std::endl;
384 ambient_light = Color( vColor );
386 } else if(iter.item() == "name") {
389 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
392 current_tileset = NULL;
394 if(solid_tilemaps.size() == 0)
395 throw std::runtime_error("No solid tilemap specified");
397 move_to_spawnpoint("main");
399 } catch(std::exception& e) {
400 std::stringstream msg;
401 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
403 throw std::runtime_error(msg.str());
408 WorldMap::get_level_title(LevelTile& level)
410 /** get special_tile's title */
411 level.title = "<no title>";
415 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
417 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
421 level_lisp->get("name", level.title);
422 } catch(std::exception& e) {
423 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
428 void WorldMap::calculate_total_stats()
431 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
432 LevelTile* level = *i;
434 total_stats += level->statistics;
440 WorldMap::on_escape_press()
442 // Show or hide the menu
443 if(!Menu::current()) {
444 Menu::set_current(worldmap_menu.get());
445 tux->set_direction(D_NONE); // stop tux movement when menu is called
447 Menu::set_current(NULL);
452 WorldMap::get_next_tile(Vector pos, Direction direction)
474 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
476 *new_pos = get_next_tile(old_pos, direction);
478 if (!(new_pos->x >= 0 && new_pos->x < get_width()
479 && new_pos->y >= 0 && new_pos->y < get_height()))
480 { // New position is outsite the tilemap
484 { // Check if the tile allows us to go to new_pos
485 int old_tile_data = tile_data_at(old_pos);
486 int new_tile_data = tile_data_at(*new_pos);
490 return (old_tile_data & Tile::WORLDMAP_WEST
491 && new_tile_data & Tile::WORLDMAP_EAST);
494 return (old_tile_data & Tile::WORLDMAP_EAST
495 && new_tile_data & Tile::WORLDMAP_WEST);
498 return (old_tile_data & Tile::WORLDMAP_NORTH
499 && new_tile_data & Tile::WORLDMAP_SOUTH);
502 return (old_tile_data & Tile::WORLDMAP_SOUTH
503 && new_tile_data & Tile::WORLDMAP_NORTH);
506 assert(!"path_ok() can't walk if direction is NONE");
513 WorldMap::finished_level(Level* gamelevel)
515 // TODO use Level* parameter here?
516 LevelTile* level = at_level();
518 bool old_level_state = level->solved;
519 level->solved = true;
520 level->sprite->set_action("solved");
522 // deal with statistics
523 level->statistics.merge(gamelevel->stats);
524 calculate_total_stats();
528 if (old_level_state != level->solved) {
529 // Try to detect the next direction to which we should walk
530 // FIXME: Mostly a hack
531 Direction dir = D_NONE;
533 int dirdata = available_directions_at(tux->get_tile_pos());
534 // first, test for crossroads
535 if (dirdata == Tile::WORLDMAP_CNSE ||
536 dirdata == Tile::WORLDMAP_CNSW ||
537 dirdata == Tile::WORLDMAP_CNEW ||
538 dirdata == Tile::WORLDMAP_CSEW ||
539 dirdata == Tile::WORLDMAP_CNSEW)
541 else if (dirdata & Tile::WORLDMAP_NORTH
542 && tux->back_direction != D_NORTH)
544 else if (dirdata & Tile::WORLDMAP_SOUTH
545 && tux->back_direction != D_SOUTH)
547 else if (dirdata & Tile::WORLDMAP_EAST
548 && tux->back_direction != D_EAST)
550 else if (dirdata & Tile::WORLDMAP_WEST
551 && tux->back_direction != D_WEST)
555 tux->set_direction(dir);
559 if (level->extro_script != "") {
561 std::istringstream in(level->extro_script);
562 run_script(in, "worldmap:extro_script");
563 } catch(std::exception& e) {
564 log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
570 WorldMap::get_camera_pos_for_tux() {
571 Vector camera_offset;
572 Vector tux_pos = tux->get_pos();
573 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
574 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
575 return camera_offset;
579 WorldMap::clamp_camera_position(Vector& c) {
585 if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
586 c.x = (int)get_width()*32 - SCREEN_WIDTH;
587 if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
588 c.y = (int)get_height()*32 - SCREEN_HEIGHT;
590 if (int(get_width()*32) < SCREEN_WIDTH)
591 c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
592 if (int(get_height()*32) < SCREEN_HEIGHT)
593 c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
597 WorldMap::update(float delta)
600 Menu* menu = Menu::current();
602 if(menu == worldmap_menu.get()) {
603 switch (worldmap_menu->check())
605 case MNID_RETURNWORLDMAP: // Return to game
606 Menu::set_current(0);
608 case MNID_QUITWORLDMAP: // Quit Worldmap
609 g_main_loop->exit_screen();
617 // update GameObjects
618 for(size_t i = 0; i < game_objects.size(); ++i) {
619 GameObject* object = game_objects[i];
620 if(!panning || object != tux) {
621 object->update(delta);
625 // remove old GameObjects
626 for(GameObjects::iterator i = game_objects.begin();
627 i != game_objects.end(); ) {
628 GameObject* object = *i;
629 if(!object->is_valid()) {
630 try_unexpose(object);
632 i = game_objects.erase(i);
638 /* update solid_tilemaps list */
639 //FIXME: this could be more efficient
640 solid_tilemaps.clear();
641 for(std::vector<GameObject*>::iterator i = game_objects.begin();
642 i != game_objects.end(); ++i)
644 TileMap* tm = dynamic_cast<TileMap*>(*i);
646 if (tm->is_solid()) solid_tilemaps.push_back(tm);
649 Vector requested_pos;
653 camera_offset = get_camera_pos_for_tux();
655 Vector delta = pan_pos - camera_offset;
656 float mag = delta.norm();
657 if(mag > CAMERA_PAN_SPEED) {
658 delta *= CAMERA_PAN_SPEED/mag;
660 camera_offset += delta;
661 if(camera_offset == pan_pos) {
666 requested_pos = camera_offset;
667 clamp_camera_position(camera_offset);
670 if(requested_pos.x != camera_offset.x) {
671 pan_pos.x = camera_offset.x;
673 if(requested_pos.y != camera_offset.y) {
674 pan_pos.y = camera_offset.y;
679 bool enter_level = false;
680 if(g_main_controller->pressed(Controller::ACTION)
681 || g_main_controller->pressed(Controller::JUMP)
682 || g_main_controller->pressed(Controller::MENU_SELECT)) {
683 /* some people define UP and JUMP on the same key... */
684 if(!g_main_controller->pressed(Controller::UP))
687 if(g_main_controller->pressed(Controller::PAUSE_MENU))
690 // check for teleporters
691 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
692 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
694 if (teleporter->worldmap != "") {
695 change(teleporter->worldmap, teleporter->spawnpoint);
697 // TODO: an animation, camera scrolling or a fading would be a nice touch
698 sound_manager->play("sounds/warp.wav");
699 tux->back_direction = D_NONE;
700 move_to_spawnpoint(teleporter->spawnpoint, true);
704 // check for auto-play levels
705 LevelTile* level = at_level();
706 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
710 if (enter_level && !tux->is_moving())
712 /* Check level action */
713 LevelTile* level = at_level();
715 //Respawn if player on a tile with no level and nowhere to go.
716 int tile_data = tile_data_at(tux->get_tile_pos());
717 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
718 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
719 move_to_spawnpoint("main");
722 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
726 if (level->pos == tux->get_tile_pos()) {
728 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
729 level->pos.y*32 + 8 - camera_offset.y);
730 std::string levelfile = levels_path + level->get_name();
732 // update state and savegame
735 g_main_loop->push_screen(new GameSession(levelfile, &level->statistics),
736 new ShrinkFade(shrinkpos, 1.0f));
738 } catch(std::exception& e) {
739 log_fatal << "Couldn't load level: " << e.what() << std::endl;
745 // tux->set_direction(input_direction);
751 WorldMap::tile_data_at(Vector p)
755 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
756 TileMap* tilemap = *i;
757 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
758 int dirdata = tile->getData();
766 WorldMap::available_directions_at(Vector p)
768 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
774 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
775 LevelTile* level = *i;
776 if (level->pos == tux->get_tile_pos())
784 WorldMap::at_special_tile()
786 for(SpecialTiles::iterator i = special_tiles.begin();
787 i != special_tiles.end(); ++i) {
788 SpecialTile* special_tile = *i;
789 if (special_tile->pos == tux->get_tile_pos())
797 WorldMap::at_sprite_change(const Vector& pos)
799 for(SpriteChanges::iterator i = sprite_changes.begin();
800 i != sprite_changes.end(); ++i) {
801 SpriteChange* sprite_change = *i;
802 if(sprite_change->pos == pos)
803 return sprite_change;
810 WorldMap::at_teleporter(const Vector& pos)
812 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
813 Teleporter* teleporter = *i;
814 if(teleporter->pos == pos) return teleporter;
821 WorldMap::draw(DrawingContext& context)
823 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
824 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
825 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
827 context.set_ambient_color( ambient_light );
828 context.push_transform();
829 context.set_translation(camera_offset);
831 for(GameObjects::iterator i = game_objects.begin();
832 i != game_objects.end(); ++i) {
833 GameObject* object = *i;
834 if(!panning || object != tux) {
835 object->draw(context);
840 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
841 // draw visual indication of possible walk directions
842 static int flipme = 0;
844 for (int x = 0; x < get_width(); x++) {
845 for (int y = 0; y < get_height(); y++) {
846 int data = tile_data_at(Vector(x,y));
850 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);
851 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);
852 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);
853 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);
854 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);
855 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);
860 draw_status(context);
861 context.pop_transform();
865 WorldMap::draw_status(DrawingContext& context)
867 context.push_transform();
868 context.set_translation(Vector(0, 0));
870 player_status->draw(context);
872 if (!tux->is_moving()) {
873 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
874 LevelTile* level = *i;
876 if (level->pos == tux->get_tile_pos()) {
877 if(level->title == "")
878 get_level_title(*level);
880 context.draw_text(normal_font, level->title,
881 Vector(SCREEN_WIDTH/2,
882 SCREEN_HEIGHT - normal_font->get_height() - 30),
883 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
885 // if level is solved, draw level picture behind stats
888 if (const Surface* picture = level->get_picture()) {
889 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
890 context.push_transform();
891 context.set_alpha(0.5);
892 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
893 context.pop_transform();
898 level->statistics.draw_worldmap_info(context);
903 for(SpecialTiles::iterator i = special_tiles.begin();
904 i != special_tiles.end(); ++i) {
905 SpecialTile* special_tile = *i;
907 if (special_tile->pos == tux->get_tile_pos()) {
908 /* Display an in-map message in the map, if any as been selected */
909 if(!special_tile->map_message.empty() && !special_tile->passive_message)
910 context.draw_text(normal_font, special_tile->map_message,
911 Vector(SCREEN_WIDTH/2,
912 SCREEN_HEIGHT - normal_font->get_height() - 60),
913 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
918 // display teleporter messages
919 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
920 if (teleporter && (teleporter->message != "")) {
921 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 30);
922 context.draw_text(normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
927 /* Display a passive message in the map, if needed */
928 if(passive_message_timer.started())
929 context.draw_text(normal_font, passive_message,
930 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 60),
931 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
933 context.pop_transform();
939 sound_manager->play_music(music);
940 Menu::set_current(NULL);
945 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
946 if (force_spawnpoint != "") {
947 move_to_spawnpoint(force_spawnpoint);
948 force_spawnpoint = "";
953 // register worldmap_table as worldmap in scripting
954 using namespace Scripting;
956 sq_pushroottable(global_vm);
957 sq_pushstring(global_vm, "worldmap", -1);
958 sq_pushobject(global_vm, worldmap_table);
959 if(SQ_FAILED(sq_createslot(global_vm, -3)))
960 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
961 sq_pop(global_vm, 1);
963 //Run default.nut just before init script
965 IFileStream in(levels_path + "/default.nut");
966 run_script(in, "WorldMap::default.nut");
967 } catch(std::exception& ) {
968 // doesn't exist or erroneous; do nothing
971 if(init_script != "") {
972 std::istringstream in(init_script);
973 run_script(in, "WorldMap::init");
980 using namespace Scripting;
982 // save state of world and player
985 // remove worldmap_table from roottable
986 sq_pushroottable(global_vm);
987 sq_pushstring(global_vm, "worldmap", -1);
988 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
989 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
990 sq_pop(global_vm, 1);
994 WorldMap::save_state()
996 using namespace Scripting;
998 HSQUIRRELVM vm = global_vm;
999 int oldtop = sq_gettop(vm);
1003 sq_pushroottable(vm);
1004 sq_pushstring(vm, "state", -1);
1005 if(SQ_FAILED(sq_get(vm, -2)))
1006 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1008 // get or create worlds table
1009 sq_pushstring(vm, "worlds", -1);
1010 if(SQ_FAILED(sq_get(vm, -2))) {
1011 sq_pushstring(vm, "worlds", -1);
1013 if(SQ_FAILED(sq_createslot(vm, -3)))
1014 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
1016 sq_pushstring(vm, "worlds", -1);
1017 if(SQ_FAILED(sq_get(vm, -2)))
1018 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1021 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1022 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1025 // construct new table for this worldmap
1026 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1030 sq_pushstring(vm, "tux", -1);
1033 store_float(vm, "x", tux->get_tile_pos().x);
1034 store_float(vm, "y", tux->get_tile_pos().y);
1035 store_string(vm, "back", direction_to_string(tux->back_direction));
1037 sq_createslot(vm, -3);
1040 sq_pushstring(vm, "levels", -1);
1043 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1044 LevelTile* level = *i;
1046 sq_pushstring(vm, level->get_name().c_str(), -1);
1049 store_bool(vm, "solved", level->solved);
1050 level->statistics.serialize_to_squirrel(vm);
1052 sq_createslot(vm, -3);
1055 sq_createslot(vm, -3);
1057 // overall statistics...
1058 total_stats.serialize_to_squirrel(vm);
1060 // push world into worlds table
1061 sq_createslot(vm, -3);
1062 } catch(std::exception& ) {
1063 sq_settop(vm, oldtop);
1066 sq_settop(vm, oldtop);
1068 if(World::current() != NULL)
1069 World::current()->save_state();
1073 WorldMap::load_state()
1075 using namespace Scripting;
1077 HSQUIRRELVM vm = global_vm;
1078 int oldtop = sq_gettop(vm);
1082 sq_pushroottable(vm);
1083 sq_pushstring(vm, "state", -1);
1084 if(SQ_FAILED(sq_get(vm, -2)))
1085 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1088 sq_pushstring(vm, "worlds", -1);
1089 if(SQ_FAILED(sq_get(vm, -2)))
1090 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1092 // get table for our world
1093 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1094 if(SQ_FAILED(sq_get(vm, -2)))
1095 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1098 sq_pushstring(vm, "tux", -1);
1099 if(SQ_FAILED(sq_get(vm, -2)))
1100 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1103 p.x = read_float(vm, "x");
1104 p.y = read_float(vm, "y");
1105 std::string back_str = read_string(vm, "back");
1106 tux->back_direction = string_to_direction(back_str);
1107 tux->set_tile_pos(p);
1112 sq_pushstring(vm, "levels", -1);
1113 if(SQ_FAILED(sq_get(vm, -2)))
1114 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1116 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1117 LevelTile* level = *i;
1118 sq_pushstring(vm, level->get_name().c_str(), -1);
1119 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1120 level->solved = read_bool(vm, "solved");
1121 level->sprite->set_action(level->solved ? "solved" : "default");
1122 level->statistics.unserialize_from_squirrel(vm);
1127 // leave state table
1130 // load overall statistics
1131 total_stats.unserialize_from_squirrel(vm);
1133 } catch(std::exception& e) {
1134 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1136 sq_settop(vm, oldtop);
1142 WorldMap::level_count()
1144 return levels.size();
1148 WorldMap::solved_level_count()
1151 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1152 LevelTile* level = *i;
1162 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1164 using namespace Scripting;
1166 // garbage collect thread list
1167 for(ScriptList::iterator i = scripts.begin();
1168 i != scripts.end(); ) {
1169 HSQOBJECT& object = *i;
1170 HSQUIRRELVM vm = object_to_vm(object);
1172 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1173 sq_release(global_vm, &object);
1174 i = scripts.erase(i);
1181 HSQOBJECT object = create_thread(global_vm);
1182 scripts.push_back(object);
1184 HSQUIRRELVM vm = object_to_vm(object);
1186 // set worldmap_table as roottable for the thread
1187 sq_pushobject(vm, worldmap_table);
1188 sq_setroottable(vm);
1190 compile_and_run(vm, in, sourcename);
1196 WorldMap::get_width() const
1199 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1200 TileMap* solids = *i;
1201 if (solids->get_width() > width) width = solids->get_width();
1207 WorldMap::get_height() const
1210 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1211 TileMap* solids = *i;
1212 if (solids->get_height() > height) height = solids->get_height();
1217 } // namespace WorldMapNS