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/physfs_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/main.hpp"
47 #include "supertux/mainloop.hpp"
48 #include "supertux/options_menu.hpp"
49 #include "supertux/player_status.hpp"
50 #include "supertux/resources.hpp"
51 #include "supertux/sector.hpp"
52 #include "supertux/shrinkfade.hpp"
53 #include "supertux/spawn_point.hpp"
54 #include "supertux/textscroller.hpp"
55 #include "supertux/tile_manager.hpp"
56 #include "supertux/tile_set.hpp"
57 #include "supertux/world.hpp"
58 #include "util/file_system.hpp"
59 #include "util/gettext.hpp"
60 #include "util/log.hpp"
61 #include "util/log.hpp"
62 #include "video/drawing_context.hpp"
63 #include "video/surface.hpp"
64 #include "worldmap/level.hpp"
65 #include "worldmap/special_tile.hpp"
66 #include "worldmap/sprite_change.hpp"
67 #include "worldmap/tux.hpp"
68 #include "worldmap/worldmap.hpp"
70 static const float CAMERA_PAN_SPEED = 5.0;
72 namespace WorldMapNS {
74 enum WorldMapMenuIDs {
79 WorldMap* WorldMap::current_ = NULL;
81 Direction reverse_dir(Direction direction)
100 direction_to_string(Direction direction)
118 string_to_direction(const std::string& directory)
120 if (directory == "west")
122 else if (directory == "east")
124 else if (directory == "north")
126 else if (directory == "south")
128 else if (directory == "none")
131 log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
136 //---------------------------------------------------------------------------
138 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint) :
142 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ),
143 force_spawnpoint(force_spawnpoint),
151 music = "music/salcon.ogg";
155 worldmap_menu.reset(new Menu());
156 worldmap_menu->add_label(_("Pause"));
157 worldmap_menu->add_hl();
158 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
159 worldmap_menu->add_submenu(_("Options"), get_options_menu());
160 worldmap_menu->add_hl();
161 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
163 // create a new squirrel table for the worldmap
164 using namespace Scripting;
166 sq_collectgarbage(global_vm);
167 sq_newtable(global_vm);
168 sq_pushroottable(global_vm);
169 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
170 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
172 sq_resetobject(&worldmap_table);
173 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
174 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
176 sq_addref(global_vm, &worldmap_table);
177 sq_pop(global_vm, 1);
179 sound_manager->preload("sounds/warp.wav");
181 // load worldmap objects
185 WorldMap::~WorldMap()
187 using namespace Scripting;
192 for(GameObjects::iterator i = game_objects.begin();
193 i != game_objects.end(); ++i) {
194 GameObject* object = *i;
195 try_unexpose(object);
199 for(SpawnPoints::iterator i = spawn_points.begin();
200 i != spawn_points.end(); ++i) {
204 for(ScriptList::iterator i = scripts.begin();
205 i != scripts.end(); ++i) {
206 HSQOBJECT& object = *i;
207 sq_release(global_vm, &object);
209 sq_release(global_vm, &worldmap_table);
211 sq_collectgarbage(global_vm);
218 WorldMap::add_object(GameObject* object)
220 TileMap* tilemap = dynamic_cast<TileMap*> (object);
221 if(tilemap != 0 && tilemap->is_solid()) {
222 solid_tilemaps.push_back(tilemap);
227 game_objects.push_back(object);
231 WorldMap::try_expose(GameObject* object)
233 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
234 if(interface != NULL) {
235 HSQUIRRELVM vm = Scripting::global_vm;
236 sq_pushobject(vm, worldmap_table);
237 interface->expose(vm, -1);
243 WorldMap::try_unexpose(GameObject* object)
245 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
246 if(interface != NULL) {
247 HSQUIRRELVM vm = Scripting::global_vm;
248 SQInteger oldtop = sq_gettop(vm);
249 sq_pushobject(vm, worldmap_table);
251 interface->unexpose(vm, -1);
252 } catch(std::exception& e) {
253 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
255 sq_settop(vm, oldtop);
260 WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
262 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
264 if(sp->name == spawnpoint) {
266 tux->set_tile_pos(p);
267 tux->set_direction(sp->auto_dir);
270 pan_pos = get_camera_pos_for_tux();
271 clamp_camera_position(pan_pos);
276 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
277 if (spawnpoint != "main") {
278 move_to_spawnpoint("main");
283 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
285 g_main_loop->exit_screen();
286 g_main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
290 WorldMap::load(const std::string& filename)
292 map_filename = filename;
293 levels_path = FileSystem::dirname(map_filename);
297 const lisp::Lisp* root = parser.parse(map_filename);
299 const lisp::Lisp* level = root->get_lisp("supertux-level");
301 throw std::runtime_error("file isn't a supertux-level file.");
303 level->get("name", name);
305 const lisp::Lisp* sector = level->get_lisp("sector");
307 throw std::runtime_error("No sector specified in worldmap file.");
309 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
310 if(tilesets_lisp != NULL) {
311 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
314 std::string tileset_name;
315 if(level->get("tileset", tileset_name)) {
316 if(tileset != NULL) {
317 log_warning << "multiple tilesets specified in level" << std::endl;
319 tileset = tile_manager->get_tileset(tileset_name);
322 /* load default tileset */
323 if(tileset == NULL) {
324 tileset = tile_manager->get_tileset("images/worldmap.strf");
326 current_tileset = tileset;
328 lisp::ListIterator iter(sector);
330 if(iter.item() == "tilemap") {
331 add_object(new TileMap(*(iter.lisp())));
332 } else if(iter.item() == "background") {
333 add_object(new Background(*(iter.lisp())));
334 } else if(iter.item() == "music") {
335 iter.value()->get(music);
336 } else if(iter.item() == "init-script") {
337 iter.value()->get(init_script);
338 } else if(iter.item() == "worldmap-spawnpoint") {
339 SpawnPoint* sp = new SpawnPoint(iter.lisp());
340 spawn_points.push_back(sp);
341 } else if(iter.item() == "level") {
342 LevelTile* level = new LevelTile(levels_path, iter.lisp());
343 levels.push_back(level);
345 } else if(iter.item() == "special-tile") {
346 SpecialTile* special_tile = new SpecialTile(iter.lisp());
347 special_tiles.push_back(special_tile);
348 add_object(special_tile);
349 } else if(iter.item() == "sprite-change") {
350 SpriteChange* sprite_change = new SpriteChange(iter.lisp());
351 sprite_changes.push_back(sprite_change);
352 add_object(sprite_change);
353 } else if(iter.item() == "teleporter") {
354 Teleporter* teleporter = new Teleporter(iter.lisp());
355 teleporters.push_back(teleporter);
356 add_object(teleporter);
357 } else if(iter.item() == "ambient-light") {
358 std::vector<float> vColor;
359 sector->get( "ambient-light", vColor );
360 if(vColor.size() < 3) {
361 log_warning << "(ambient-light) requires a color as argument" << std::endl;
363 ambient_light = Color( vColor );
365 } else if(iter.item() == "name") {
368 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
371 current_tileset = NULL;
373 if(solid_tilemaps.size() == 0)
374 throw std::runtime_error("No solid tilemap specified");
376 move_to_spawnpoint("main");
378 } catch(std::exception& e) {
379 std::stringstream msg;
380 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
382 throw std::runtime_error(msg.str());
387 WorldMap::get_level_title(LevelTile& level)
389 /** get special_tile's title */
390 level.title = "<no title>";
394 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
396 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
400 level_lisp->get("name", level.title);
401 } catch(std::exception& e) {
402 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
407 void WorldMap::calculate_total_stats()
410 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
411 LevelTile* level = *i;
413 total_stats += level->statistics;
419 WorldMap::on_escape_press()
421 // Show or hide the menu
422 if(!Menu::current()) {
423 Menu::set_current(worldmap_menu.get());
424 tux->set_direction(D_NONE); // stop tux movement when menu is called
426 Menu::set_current(NULL);
431 WorldMap::get_next_tile(Vector pos, Direction direction)
453 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
455 *new_pos = get_next_tile(old_pos, direction);
457 if (!(new_pos->x >= 0 && new_pos->x < get_width()
458 && new_pos->y >= 0 && new_pos->y < get_height()))
459 { // New position is outsite the tilemap
463 { // Check if the tile allows us to go to new_pos
464 int old_tile_data = tile_data_at(old_pos);
465 int new_tile_data = tile_data_at(*new_pos);
469 return (old_tile_data & Tile::WORLDMAP_WEST
470 && new_tile_data & Tile::WORLDMAP_EAST);
473 return (old_tile_data & Tile::WORLDMAP_EAST
474 && new_tile_data & Tile::WORLDMAP_WEST);
477 return (old_tile_data & Tile::WORLDMAP_NORTH
478 && new_tile_data & Tile::WORLDMAP_SOUTH);
481 return (old_tile_data & Tile::WORLDMAP_SOUTH
482 && new_tile_data & Tile::WORLDMAP_NORTH);
485 assert(!"path_ok() can't walk if direction is NONE");
492 WorldMap::finished_level(Level* gamelevel)
494 // TODO use Level* parameter here?
495 LevelTile* level = at_level();
497 bool old_level_state = level->solved;
498 level->solved = true;
499 level->sprite->set_action("solved");
501 // deal with statistics
502 level->statistics.merge(gamelevel->stats);
503 calculate_total_stats();
507 if (old_level_state != level->solved) {
508 // Try to detect the next direction to which we should walk
509 // FIXME: Mostly a hack
510 Direction dir = D_NONE;
512 int dirdata = available_directions_at(tux->get_tile_pos());
513 // first, test for crossroads
514 if (dirdata == Tile::WORLDMAP_CNSE ||
515 dirdata == Tile::WORLDMAP_CNSW ||
516 dirdata == Tile::WORLDMAP_CNEW ||
517 dirdata == Tile::WORLDMAP_CSEW ||
518 dirdata == Tile::WORLDMAP_CNSEW)
520 else if (dirdata & Tile::WORLDMAP_NORTH
521 && tux->back_direction != D_NORTH)
523 else if (dirdata & Tile::WORLDMAP_SOUTH
524 && tux->back_direction != D_SOUTH)
526 else if (dirdata & Tile::WORLDMAP_EAST
527 && tux->back_direction != D_EAST)
529 else if (dirdata & Tile::WORLDMAP_WEST
530 && tux->back_direction != D_WEST)
534 tux->set_direction(dir);
538 if (level->extro_script != "") {
540 std::istringstream in(level->extro_script);
541 run_script(in, "worldmap:extro_script");
542 } catch(std::exception& e) {
543 log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
549 WorldMap::get_camera_pos_for_tux() {
550 Vector camera_offset;
551 Vector tux_pos = tux->get_pos();
552 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
553 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
554 return camera_offset;
558 WorldMap::clamp_camera_position(Vector& c) {
564 if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
565 c.x = (int)get_width()*32 - SCREEN_WIDTH;
566 if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
567 c.y = (int)get_height()*32 - SCREEN_HEIGHT;
569 if (int(get_width()*32) < SCREEN_WIDTH)
570 c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
571 if (int(get_height()*32) < SCREEN_HEIGHT)
572 c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
576 WorldMap::update(float delta)
579 Menu* menu = Menu::current();
581 if(menu == worldmap_menu.get()) {
582 switch (worldmap_menu->check())
584 case MNID_RETURNWORLDMAP: // Return to game
585 Menu::set_current(0);
587 case MNID_QUITWORLDMAP: // Quit Worldmap
588 g_main_loop->exit_screen();
596 // update GameObjects
597 for(size_t i = 0; i < game_objects.size(); ++i) {
598 GameObject* object = game_objects[i];
599 if(!panning || object != tux) {
600 object->update(delta);
604 // remove old GameObjects
605 for(GameObjects::iterator i = game_objects.begin();
606 i != game_objects.end(); ) {
607 GameObject* object = *i;
608 if(!object->is_valid()) {
609 try_unexpose(object);
611 i = game_objects.erase(i);
617 /* update solid_tilemaps list */
618 //FIXME: this could be more efficient
619 solid_tilemaps.clear();
620 for(std::vector<GameObject*>::iterator i = game_objects.begin();
621 i != game_objects.end(); ++i)
623 TileMap* tm = dynamic_cast<TileMap*>(*i);
625 if (tm->is_solid()) solid_tilemaps.push_back(tm);
628 Vector requested_pos;
632 camera_offset = get_camera_pos_for_tux();
634 Vector delta = pan_pos - camera_offset;
635 float mag = delta.norm();
636 if(mag > CAMERA_PAN_SPEED) {
637 delta *= CAMERA_PAN_SPEED/mag;
639 camera_offset += delta;
640 if(camera_offset == pan_pos) {
645 requested_pos = camera_offset;
646 clamp_camera_position(camera_offset);
649 if(requested_pos.x != camera_offset.x) {
650 pan_pos.x = camera_offset.x;
652 if(requested_pos.y != camera_offset.y) {
653 pan_pos.y = camera_offset.y;
658 bool enter_level = false;
659 if(g_main_controller->pressed(Controller::ACTION)
660 || g_main_controller->pressed(Controller::JUMP)
661 || g_main_controller->pressed(Controller::MENU_SELECT)) {
662 /* some people define UP and JUMP on the same key... */
663 if(!g_main_controller->pressed(Controller::UP))
666 if(g_main_controller->pressed(Controller::PAUSE_MENU))
669 // check for teleporters
670 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
671 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
673 if (teleporter->worldmap != "") {
674 change(teleporter->worldmap, teleporter->spawnpoint);
676 // TODO: an animation, camera scrolling or a fading would be a nice touch
677 sound_manager->play("sounds/warp.wav");
678 tux->back_direction = D_NONE;
679 move_to_spawnpoint(teleporter->spawnpoint, true);
683 // check for auto-play levels
684 LevelTile* level = at_level();
685 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
689 if (enter_level && !tux->is_moving())
691 /* Check level action */
692 LevelTile* level = at_level();
694 //Respawn if player on a tile with no level and nowhere to go.
695 int tile_data = tile_data_at(tux->get_tile_pos());
696 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
697 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
698 move_to_spawnpoint("main");
701 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
705 if (level->pos == tux->get_tile_pos()) {
707 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
708 level->pos.y*32 + 8 - camera_offset.y);
709 std::string levelfile = levels_path + level->get_name();
711 // update state and savegame
714 g_main_loop->push_screen(new GameSession(levelfile, &level->statistics),
715 new ShrinkFade(shrinkpos, 1.0f));
717 } catch(std::exception& e) {
718 log_fatal << "Couldn't load level: " << e.what() << std::endl;
724 // tux->set_direction(input_direction);
730 WorldMap::tile_data_at(Vector p)
734 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
735 TileMap* tilemap = *i;
736 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
737 int dirdata = tile->getData();
745 WorldMap::available_directions_at(Vector p)
747 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
753 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
754 LevelTile* level = *i;
755 if (level->pos == tux->get_tile_pos())
763 WorldMap::at_special_tile()
765 for(SpecialTiles::iterator i = special_tiles.begin();
766 i != special_tiles.end(); ++i) {
767 SpecialTile* special_tile = *i;
768 if (special_tile->pos == tux->get_tile_pos())
776 WorldMap::at_sprite_change(const Vector& pos)
778 for(SpriteChanges::iterator i = sprite_changes.begin();
779 i != sprite_changes.end(); ++i) {
780 SpriteChange* sprite_change = *i;
781 if(sprite_change->pos == pos)
782 return sprite_change;
789 WorldMap::at_teleporter(const Vector& pos)
791 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
792 Teleporter* teleporter = *i;
793 if(teleporter->pos == pos) return teleporter;
800 WorldMap::draw(DrawingContext& context)
802 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
803 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
804 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
806 context.set_ambient_color( ambient_light );
807 context.push_transform();
808 context.set_translation(camera_offset);
810 for(GameObjects::iterator i = game_objects.begin();
811 i != game_objects.end(); ++i) {
812 GameObject* object = *i;
813 if(!panning || object != tux) {
814 object->draw(context);
819 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
820 // draw visual indication of possible walk directions
821 static int flipme = 0;
823 for (int x = 0; x < get_width(); x++) {
824 for (int y = 0; y < get_height(); y++) {
825 int data = tile_data_at(Vector(x,y));
829 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);
830 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);
831 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);
832 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);
833 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);
834 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);
839 draw_status(context);
840 context.pop_transform();
844 WorldMap::draw_status(DrawingContext& context)
846 context.push_transform();
847 context.set_translation(Vector(0, 0));
849 player_status->draw(context);
851 if (!tux->is_moving()) {
852 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
853 LevelTile* level = *i;
855 if (level->pos == tux->get_tile_pos()) {
856 if(level->title == "")
857 get_level_title(*level);
859 context.draw_text(normal_font, level->title,
860 Vector(SCREEN_WIDTH/2,
861 SCREEN_HEIGHT - normal_font->get_height() - 30),
862 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
864 // if level is solved, draw level picture behind stats
867 if (const Surface* picture = level->get_picture()) {
868 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
869 context.push_transform();
870 context.set_alpha(0.5);
871 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
872 context.pop_transform();
877 level->statistics.draw_worldmap_info(context);
882 for(SpecialTiles::iterator i = special_tiles.begin();
883 i != special_tiles.end(); ++i) {
884 SpecialTile* special_tile = *i;
886 if (special_tile->pos == tux->get_tile_pos()) {
887 /* Display an in-map message in the map, if any as been selected */
888 if(!special_tile->map_message.empty() && !special_tile->passive_message)
889 context.draw_text(normal_font, special_tile->map_message,
890 Vector(SCREEN_WIDTH/2,
891 SCREEN_HEIGHT - normal_font->get_height() - 60),
892 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
897 // display teleporter messages
898 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
899 if (teleporter && (teleporter->message != "")) {
900 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 30);
901 context.draw_text(normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
906 /* Display a passive message in the map, if needed */
907 if(passive_message_timer.started())
908 context.draw_text(normal_font, passive_message,
909 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 60),
910 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
912 context.pop_transform();
918 sound_manager->play_music(music);
919 Menu::set_current(NULL);
924 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
925 if (force_spawnpoint != "") {
926 move_to_spawnpoint(force_spawnpoint);
927 force_spawnpoint = "";
932 // register worldmap_table as worldmap in scripting
933 using namespace Scripting;
935 sq_pushroottable(global_vm);
936 sq_pushstring(global_vm, "worldmap", -1);
937 sq_pushobject(global_vm, worldmap_table);
938 if(SQ_FAILED(sq_createslot(global_vm, -3)))
939 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
940 sq_pop(global_vm, 1);
942 //Run default.nut just before init script
944 IFileStream in(levels_path + "/default.nut");
945 run_script(in, "WorldMap::default.nut");
946 } catch(std::exception& ) {
947 // doesn't exist or erroneous; do nothing
950 if(init_script != "") {
951 std::istringstream in(init_script);
952 run_script(in, "WorldMap::init");
959 using namespace Scripting;
961 // save state of world and player
964 // remove worldmap_table from roottable
965 sq_pushroottable(global_vm);
966 sq_pushstring(global_vm, "worldmap", -1);
967 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
968 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
969 sq_pop(global_vm, 1);
973 WorldMap::save_state()
975 using namespace Scripting;
977 HSQUIRRELVM vm = global_vm;
978 int oldtop = sq_gettop(vm);
982 sq_pushroottable(vm);
983 sq_pushstring(vm, "state", -1);
984 if(SQ_FAILED(sq_get(vm, -2)))
985 throw Scripting::SquirrelError(vm, "Couldn't get state table");
987 // get or create worlds table
988 sq_pushstring(vm, "worlds", -1);
989 if(SQ_FAILED(sq_get(vm, -2))) {
990 sq_pushstring(vm, "worlds", -1);
992 if(SQ_FAILED(sq_createslot(vm, -3)))
993 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
995 sq_pushstring(vm, "worlds", -1);
996 if(SQ_FAILED(sq_get(vm, -2)))
997 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1000 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1001 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1004 // construct new table for this worldmap
1005 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1009 sq_pushstring(vm, "tux", -1);
1012 store_float(vm, "x", tux->get_tile_pos().x);
1013 store_float(vm, "y", tux->get_tile_pos().y);
1014 store_string(vm, "back", direction_to_string(tux->back_direction));
1016 sq_createslot(vm, -3);
1019 sq_pushstring(vm, "levels", -1);
1022 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1023 LevelTile* level = *i;
1025 sq_pushstring(vm, level->get_name().c_str(), -1);
1028 store_bool(vm, "solved", level->solved);
1029 level->statistics.serialize_to_squirrel(vm);
1031 sq_createslot(vm, -3);
1034 sq_createslot(vm, -3);
1036 // overall statistics...
1037 total_stats.serialize_to_squirrel(vm);
1039 // push world into worlds table
1040 sq_createslot(vm, -3);
1041 } catch(std::exception& ) {
1042 sq_settop(vm, oldtop);
1045 sq_settop(vm, oldtop);
1047 if(World::current() != NULL)
1048 World::current()->save_state();
1052 WorldMap::load_state()
1054 using namespace Scripting;
1056 HSQUIRRELVM vm = global_vm;
1057 int oldtop = sq_gettop(vm);
1061 sq_pushroottable(vm);
1062 sq_pushstring(vm, "state", -1);
1063 if(SQ_FAILED(sq_get(vm, -2)))
1064 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1067 sq_pushstring(vm, "worlds", -1);
1068 if(SQ_FAILED(sq_get(vm, -2)))
1069 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1071 // get table for our world
1072 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1073 if(SQ_FAILED(sq_get(vm, -2)))
1074 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1077 sq_pushstring(vm, "tux", -1);
1078 if(SQ_FAILED(sq_get(vm, -2)))
1079 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1082 p.x = read_float(vm, "x");
1083 p.y = read_float(vm, "y");
1084 std::string back_str = read_string(vm, "back");
1085 tux->back_direction = string_to_direction(back_str);
1086 tux->set_tile_pos(p);
1091 sq_pushstring(vm, "levels", -1);
1092 if(SQ_FAILED(sq_get(vm, -2)))
1093 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1095 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1096 LevelTile* level = *i;
1097 sq_pushstring(vm, level->get_name().c_str(), -1);
1098 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1099 level->solved = read_bool(vm, "solved");
1100 level->sprite->set_action(level->solved ? "solved" : "default");
1101 level->statistics.unserialize_from_squirrel(vm);
1106 // leave state table
1109 // load overall statistics
1110 total_stats.unserialize_from_squirrel(vm);
1112 } catch(std::exception& e) {
1113 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1115 sq_settop(vm, oldtop);
1121 WorldMap::level_count()
1123 return levels.size();
1127 WorldMap::solved_level_count()
1130 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1131 LevelTile* level = *i;
1141 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1143 using namespace Scripting;
1145 // garbage collect thread list
1146 for(ScriptList::iterator i = scripts.begin();
1147 i != scripts.end(); ) {
1148 HSQOBJECT& object = *i;
1149 HSQUIRRELVM vm = object_to_vm(object);
1151 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1152 sq_release(global_vm, &object);
1153 i = scripts.erase(i);
1160 HSQOBJECT object = create_thread(global_vm);
1161 scripts.push_back(object);
1163 HSQUIRRELVM vm = object_to_vm(object);
1165 // set worldmap_table as roottable for the thread
1166 sq_pushobject(vm, worldmap_table);
1167 sq_setroottable(vm);
1169 compile_and_run(vm, in, sourcename);
1175 WorldMap::get_width() const
1178 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1179 TileMap* solids = *i;
1180 if (solids->get_width() > width) width = solids->get_width();
1186 WorldMap::get_height() const
1189 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1190 TileMap* solids = *i;
1191 if (solids->get_height() > height) height = solids->get_height();
1196 } // namespace WorldMapNS