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 "shrinkfade.hpp"
37 #include "video/surface.hpp"
38 #include "video/drawing_context.hpp"
39 #include "sprite/sprite.hpp"
40 #include "sprite/sprite_manager.hpp"
41 #include "audio/sound_manager.hpp"
42 #include "lisp/parser.hpp"
43 #include "lisp/lisp.hpp"
44 #include "lisp/list_iterator.hpp"
45 #include "lisp/writer.hpp"
46 #include "game_session.hpp"
48 #include "worldmap.hpp"
49 #include "resources.hpp"
52 #include "player_status.hpp"
53 #include "textscroller.hpp"
55 #include "spawn_point.hpp"
56 #include "file_system.hpp"
57 #include "physfs/physfs_stream.hpp"
58 #include "tile_manager.hpp"
59 #include "tile_set.hpp"
60 #include "gui/menu.hpp"
61 #include "gui/mousecursor.hpp"
62 #include "control/joystickkeyboardcontroller.hpp"
63 #include "object/background.hpp"
64 #include "object/tilemap.hpp"
65 #include "options_menu.hpp"
66 #include "scripting/squirrel_error.hpp"
67 #include "scripting/squirrel_util.hpp"
68 #include "worldmap/level.hpp"
69 #include "worldmap/special_tile.hpp"
70 #include "worldmap/tux.hpp"
71 #include "worldmap/sprite_change.hpp"
73 static const float CAMERA_PAN_SPEED = 5.0;
75 namespace WorldMapNS {
77 enum WorldMapMenuIDs {
82 WorldMap* WorldMap::current_ = NULL;
84 Direction reverse_dir(Direction direction)
103 direction_to_string(Direction direction)
121 string_to_direction(const std::string& directory)
123 if (directory == "west")
125 else if (directory == "east")
127 else if (directory == "north")
129 else if (directory == "south")
131 else if (directory == "none")
134 log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
139 //---------------------------------------------------------------------------
141 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint)
142 : tux(0), tileset(NULL), free_tileset(false),
143 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint),
144 in_level(false), panning(false)
150 music = "music/salcon.ogg";
154 worldmap_menu.reset(new Menu());
155 worldmap_menu->add_label(_("Pause"));
156 worldmap_menu->add_hl();
157 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
158 worldmap_menu->add_submenu(_("Options"), get_options_menu());
159 worldmap_menu->add_hl();
160 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
162 // create a new squirrel table for the worldmap
163 using namespace Scripting;
165 sq_collectgarbage(global_vm);
166 sq_newtable(global_vm);
167 sq_pushroottable(global_vm);
168 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
169 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
171 sq_resetobject(&worldmap_table);
172 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
173 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
175 sq_addref(global_vm, &worldmap_table);
176 sq_pop(global_vm, 1);
178 sound_manager->preload("sounds/warp.wav");
180 // load worldmap objects
184 WorldMap::~WorldMap()
186 using namespace Scripting;
191 for(GameObjects::iterator i = game_objects.begin();
192 i != game_objects.end(); ++i) {
193 GameObject* object = *i;
194 try_unexpose(object);
198 for(SpawnPoints::iterator i = spawn_points.begin();
199 i != spawn_points.end(); ++i) {
203 for(ScriptList::iterator i = scripts.begin();
204 i != scripts.end(); ++i) {
205 HSQOBJECT& object = *i;
206 sq_release(global_vm, &object);
208 sq_release(global_vm, &worldmap_table);
210 sq_collectgarbage(global_vm);
217 WorldMap::add_object(GameObject* object)
219 TileMap* tilemap = dynamic_cast<TileMap*> (object);
220 if(tilemap != 0 && tilemap->is_solid()) {
221 solid_tilemaps.push_back(tilemap);
226 game_objects.push_back(object);
230 WorldMap::try_expose(GameObject* object)
232 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
233 if(interface != NULL) {
234 HSQUIRRELVM vm = Scripting::global_vm;
235 sq_pushobject(vm, worldmap_table);
236 interface->expose(vm, -1);
242 WorldMap::try_unexpose(GameObject* object)
244 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
245 if(interface != NULL) {
246 HSQUIRRELVM vm = Scripting::global_vm;
247 SQInteger oldtop = sq_gettop(vm);
248 sq_pushobject(vm, worldmap_table);
250 interface->unexpose(vm, -1);
251 } catch(std::exception& e) {
252 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
254 sq_settop(vm, oldtop);
259 WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
261 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
263 if(sp->name == spawnpoint) {
265 tux->set_tile_pos(p);
266 tux->set_direction(sp->auto_dir);
269 pan_pos = get_camera_pos_for_tux();
270 clamp_camera_position(pan_pos);
275 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
276 if (spawnpoint != "main") {
277 move_to_spawnpoint("main");
282 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
284 main_loop->exit_screen();
285 main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
289 WorldMap::load(const std::string& filename)
291 map_filename = filename;
292 levels_path = FileSystem::dirname(map_filename);
296 const lisp::Lisp* root = parser.parse(map_filename);
298 const lisp::Lisp* level = root->get_lisp("supertux-level");
300 throw std::runtime_error("file isn't a supertux-level file.");
302 level->get("name", name);
304 const lisp::Lisp* sector = level->get_lisp("sector");
306 throw std::runtime_error("No sector specified in worldmap file.");
308 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
309 if(tilesets_lisp != NULL) {
310 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
313 std::string tileset_name;
314 if(level->get("tileset", tileset_name)) {
315 if(tileset != NULL) {
316 log_warning << "multiple tilesets specified in level" << std::endl;
318 tileset = tile_manager->get_tileset(tileset_name);
321 /* load default tileset */
322 if(tileset == NULL) {
323 tileset = tile_manager->get_tileset("images/worldmap.strf");
325 current_tileset = tileset;
327 lisp::ListIterator iter(sector);
329 if(iter.item() == "tilemap") {
330 add_object(new TileMap(*(iter.lisp())));
331 } else if(iter.item() == "background") {
332 add_object(new Background(*(iter.lisp())));
333 } else if(iter.item() == "music") {
334 iter.value()->get(music);
335 } else if(iter.item() == "init-script") {
336 iter.value()->get(init_script);
337 } else if(iter.item() == "worldmap-spawnpoint") {
338 SpawnPoint* sp = new SpawnPoint(iter.lisp());
339 spawn_points.push_back(sp);
340 } else if(iter.item() == "level") {
341 LevelTile* level = new LevelTile(levels_path, iter.lisp());
342 levels.push_back(level);
344 } else if(iter.item() == "special-tile") {
345 SpecialTile* special_tile = new SpecialTile(iter.lisp());
346 special_tiles.push_back(special_tile);
347 add_object(special_tile);
348 } else if(iter.item() == "sprite-change") {
349 SpriteChange* sprite_change = new SpriteChange(iter.lisp());
350 sprite_changes.push_back(sprite_change);
351 add_object(sprite_change);
352 } else if(iter.item() == "teleporter") {
353 Teleporter* teleporter = new Teleporter(iter.lisp());
354 teleporters.push_back(teleporter);
355 add_object(teleporter);
356 } else if(iter.item() == "ambient-light") {
357 std::vector<float> vColor;
358 sector->get_vector( "ambient-light", vColor );
359 if(vColor.size() < 3) {
360 log_warning << "(ambient-light) requires a color as argument" << std::endl;
362 ambient_light = Color( vColor );
364 } else if(iter.item() == "name") {
367 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
370 current_tileset = NULL;
372 if(solid_tilemaps.size() == 0)
373 throw std::runtime_error("No solid tilemap specified");
375 move_to_spawnpoint("main");
377 } catch(std::exception& e) {
378 std::stringstream msg;
379 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
381 throw std::runtime_error(msg.str());
386 WorldMap::get_level_title(LevelTile& level)
388 /** get special_tile's title */
389 level.title = "<no title>";
393 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
395 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
399 level_lisp->get("name", level.title);
400 } catch(std::exception& e) {
401 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
406 void WorldMap::calculate_total_stats()
409 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
410 LevelTile* level = *i;
412 total_stats += level->statistics;
418 WorldMap::on_escape_press()
420 // Show or hide the menu
421 if(!Menu::current()) {
422 Menu::set_current(worldmap_menu.get());
423 tux->set_direction(D_NONE); // stop tux movement when menu is called
425 Menu::set_current(NULL);
430 WorldMap::get_next_tile(Vector pos, Direction direction)
452 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
454 *new_pos = get_next_tile(old_pos, direction);
456 if (!(new_pos->x >= 0 && new_pos->x < get_width()
457 && new_pos->y >= 0 && new_pos->y < get_height()))
458 { // New position is outsite the tilemap
462 { // Check if the tile allows us to go to new_pos
463 int old_tile_data = tile_data_at(old_pos);
464 int new_tile_data = tile_data_at(*new_pos);
468 return (old_tile_data & Tile::WORLDMAP_WEST
469 && new_tile_data & Tile::WORLDMAP_EAST);
472 return (old_tile_data & Tile::WORLDMAP_EAST
473 && new_tile_data & Tile::WORLDMAP_WEST);
476 return (old_tile_data & Tile::WORLDMAP_NORTH
477 && new_tile_data & Tile::WORLDMAP_SOUTH);
480 return (old_tile_data & Tile::WORLDMAP_SOUTH
481 && new_tile_data & Tile::WORLDMAP_NORTH);
484 assert(!"path_ok() can't walk if direction is NONE");
491 WorldMap::finished_level(Level* gamelevel)
493 // TODO use Level* parameter here?
494 LevelTile* level = at_level();
496 bool old_level_state = level->solved;
497 level->solved = true;
498 level->sprite->set_action("solved");
500 // deal with statistics
501 level->statistics.merge(gamelevel->stats);
502 calculate_total_stats();
506 if (old_level_state != level->solved) {
507 // Try to detect the next direction to which we should walk
508 // FIXME: Mostly a hack
509 Direction dir = D_NONE;
511 int dirdata = available_directions_at(tux->get_tile_pos());
512 // first, test for crossroads
513 if (dirdata == Tile::WORLDMAP_CNSE ||
514 dirdata == Tile::WORLDMAP_CNSW ||
515 dirdata == Tile::WORLDMAP_CNEW ||
516 dirdata == Tile::WORLDMAP_CSEW ||
517 dirdata == Tile::WORLDMAP_CNSEW)
519 else if (dirdata & Tile::WORLDMAP_NORTH
520 && tux->back_direction != D_NORTH)
522 else if (dirdata & Tile::WORLDMAP_SOUTH
523 && tux->back_direction != D_SOUTH)
525 else if (dirdata & Tile::WORLDMAP_EAST
526 && tux->back_direction != D_EAST)
528 else if (dirdata & Tile::WORLDMAP_WEST
529 && tux->back_direction != D_WEST)
533 tux->set_direction(dir);
537 if (level->extro_script != "") {
539 std::istringstream in(level->extro_script);
540 run_script(in, "worldmap:extro_script");
541 } catch(std::exception& e) {
542 log_fatal << "Couldn't run level-extro-script: " << e.what() << std::endl;
548 WorldMap::get_camera_pos_for_tux() {
549 Vector camera_offset;
550 Vector tux_pos = tux->get_pos();
551 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
552 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
553 return camera_offset;
557 WorldMap::clamp_camera_position(Vector& c) {
563 if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
564 c.x = (int)get_width()*32 - SCREEN_WIDTH;
565 if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
566 c.y = (int)get_height()*32 - SCREEN_HEIGHT;
568 if (int(get_width()*32) < SCREEN_WIDTH)
569 c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
570 if (int(get_height()*32) < SCREEN_HEIGHT)
571 c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
575 WorldMap::update(float delta)
578 Menu* menu = Menu::current();
582 if(menu == worldmap_menu.get()) {
583 switch (worldmap_menu->check())
585 case MNID_RETURNWORLDMAP: // Return to game
586 Menu::set_current(0);
588 case MNID_QUITWORLDMAP: // Quit Worldmap
589 main_loop->exit_screen();
597 // update GameObjects
598 for(size_t i = 0; i < game_objects.size(); ++i) {
599 GameObject* object = game_objects[i];
600 if(!panning || object != tux) {
601 object->update(delta);
605 // remove old GameObjects
606 for(GameObjects::iterator i = game_objects.begin();
607 i != game_objects.end(); ) {
608 GameObject* object = *i;
609 if(!object->is_valid()) {
610 try_unexpose(object);
612 i = game_objects.erase(i);
618 /* update solid_tilemaps list */
619 //FIXME: this could be more efficient
620 solid_tilemaps.clear();
621 for(std::vector<GameObject*>::iterator i = game_objects.begin();
622 i != game_objects.end(); ++i)
624 TileMap* tm = dynamic_cast<TileMap*>(*i);
626 if (tm->is_solid()) solid_tilemaps.push_back(tm);
629 Vector requested_pos;
633 camera_offset = get_camera_pos_for_tux();
635 Vector delta = pan_pos - camera_offset;
636 float mag = delta.norm();
637 if(mag > CAMERA_PAN_SPEED) {
638 delta *= CAMERA_PAN_SPEED/mag;
640 camera_offset += delta;
641 if(camera_offset == pan_pos) {
646 requested_pos = camera_offset;
647 clamp_camera_position(camera_offset);
650 if(requested_pos.x != camera_offset.x) {
651 pan_pos.x = camera_offset.x;
653 if(requested_pos.y != camera_offset.y) {
654 pan_pos.y = camera_offset.y;
659 bool enter_level = false;
660 if(main_controller->pressed(Controller::ACTION)
661 || main_controller->pressed(Controller::JUMP)
662 || main_controller->pressed(Controller::MENU_SELECT)) {
663 /* some people define UP and JUMP on the same key... */
664 if(!main_controller->pressed(Controller::UP))
667 if(main_controller->pressed(Controller::PAUSE_MENU))
670 // check for teleporters
671 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
672 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
674 if (teleporter->worldmap != "") {
675 change(teleporter->worldmap, teleporter->spawnpoint);
677 // TODO: an animation, camera scrolling or a fading would be a nice touch
678 sound_manager->play("sounds/warp.wav");
679 tux->back_direction = D_NONE;
680 move_to_spawnpoint(teleporter->spawnpoint, true);
684 // check for auto-play levels
685 LevelTile* level = at_level();
686 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
690 if (enter_level && !tux->is_moving())
692 /* Check level action */
693 LevelTile* level = at_level();
695 //Respawn if player on a tile with no level and nowhere to go.
696 int tile_data = tile_data_at(tux->get_tile_pos());
697 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
698 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
699 move_to_spawnpoint("main");
702 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
706 if (level->pos == tux->get_tile_pos()) {
708 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
709 level->pos.y*32 + 8 - camera_offset.y);
710 std::string levelfile = levels_path + level->get_name();
712 // update state and savegame
715 main_loop->push_screen(new GameSession(levelfile, &level->statistics),
716 new ShrinkFade(shrinkpos, 1.0f));
718 } catch(std::exception& e) {
719 log_fatal << "Couldn't load level: " << e.what() << std::endl;
725 // tux->set_direction(input_direction);
731 WorldMap::tile_data_at(Vector p)
735 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
736 TileMap* tilemap = *i;
737 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
738 int dirdata = tile->getData();
746 WorldMap::available_directions_at(Vector p)
748 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
754 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
755 LevelTile* level = *i;
756 if (level->pos == tux->get_tile_pos())
764 WorldMap::at_special_tile()
766 for(SpecialTiles::iterator i = special_tiles.begin();
767 i != special_tiles.end(); ++i) {
768 SpecialTile* special_tile = *i;
769 if (special_tile->pos == tux->get_tile_pos())
777 WorldMap::at_sprite_change(const Vector& pos)
779 for(SpriteChanges::iterator i = sprite_changes.begin();
780 i != sprite_changes.end(); ++i) {
781 SpriteChange* sprite_change = *i;
782 if(sprite_change->pos == pos)
783 return sprite_change;
790 WorldMap::at_teleporter(const Vector& pos)
792 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
793 Teleporter* teleporter = *i;
794 if(teleporter->pos == pos) return teleporter;
801 WorldMap::draw(DrawingContext& context)
803 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
804 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
805 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
807 context.set_ambient_color( ambient_light );
808 context.push_transform();
809 context.set_translation(camera_offset);
811 for(GameObjects::iterator i = game_objects.begin();
812 i != game_objects.end(); ++i) {
813 GameObject* object = *i;
814 if(!panning || object != tux) {
815 object->draw(context);
820 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
821 // draw visual indication of possible walk directions
822 static int flipme = 0;
824 for (int x = 0; x < get_width(); x++) {
825 for (int y = 0; y < get_height(); y++) {
826 int data = tile_data_at(Vector(x,y));
830 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);
831 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);
832 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);
833 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);
834 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);
835 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);
840 draw_status(context);
841 context.pop_transform();
845 WorldMap::draw_status(DrawingContext& context)
847 context.push_transform();
848 context.set_translation(Vector(0, 0));
850 player_status->draw(context);
852 if (!tux->is_moving()) {
853 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
854 LevelTile* level = *i;
856 if (level->pos == tux->get_tile_pos()) {
857 if(level->title == "")
858 get_level_title(*level);
860 context.draw_text(normal_font, level->title,
861 Vector(SCREEN_WIDTH/2,
862 SCREEN_HEIGHT - normal_font->get_height() - 30),
863 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
865 // if level is solved, draw level picture behind stats
868 if (const Surface* picture = level->get_picture()) {
869 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
870 context.push_transform();
871 context.set_alpha(0.5);
872 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
873 context.pop_transform();
878 level->statistics.draw_worldmap_info(context);
883 for(SpecialTiles::iterator i = special_tiles.begin();
884 i != special_tiles.end(); ++i) {
885 SpecialTile* special_tile = *i;
887 if (special_tile->pos == tux->get_tile_pos()) {
888 /* Display an in-map message in the map, if any as been selected */
889 if(!special_tile->map_message.empty() && !special_tile->passive_message)
890 context.draw_text(normal_font, special_tile->map_message,
891 Vector(SCREEN_WIDTH/2,
892 SCREEN_HEIGHT - normal_font->get_height() - 60),
893 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
898 // display teleporter messages
899 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
900 if (teleporter && (teleporter->message != "")) {
901 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 30);
902 context.draw_text(normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
907 /* Display a passive message in the map, if needed */
908 if(passive_message_timer.started())
909 context.draw_text(normal_font, passive_message,
910 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 60),
911 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
913 context.pop_transform();
919 sound_manager->play_music(music);
920 Menu::set_current(NULL);
925 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
926 if (force_spawnpoint != "") {
927 move_to_spawnpoint(force_spawnpoint);
928 force_spawnpoint = "";
933 // register worldmap_table as worldmap in scripting
934 using namespace Scripting;
936 sq_pushroottable(global_vm);
937 sq_pushstring(global_vm, "worldmap", -1);
938 sq_pushobject(global_vm, worldmap_table);
939 if(SQ_FAILED(sq_createslot(global_vm, -3)))
940 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
941 sq_pop(global_vm, 1);
943 //Run default.nut just before init script
945 IFileStream in(levels_path + "/default.nut");
946 run_script(in, "WorldMap::default.nut");
947 } catch(std::exception& ) {
948 // doesn't exist or erroneous; do nothing
952 if(init_script != "") {
953 std::istringstream in(init_script);
954 run_script(in, "WorldMap::init");
961 using namespace Scripting;
963 // save state of world and player
966 // remove worldmap_table from roottable
967 sq_pushroottable(global_vm);
968 sq_pushstring(global_vm, "worldmap", -1);
969 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
970 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
971 sq_pop(global_vm, 1);
975 WorldMap::save_state()
977 using namespace Scripting;
979 HSQUIRRELVM vm = global_vm;
980 int oldtop = sq_gettop(vm);
984 sq_pushroottable(vm);
985 sq_pushstring(vm, "state", -1);
986 if(SQ_FAILED(sq_get(vm, -2)))
987 throw Scripting::SquirrelError(vm, "Couldn't get state table");
989 // get or create worlds table
990 sq_pushstring(vm, "worlds", -1);
991 if(SQ_FAILED(sq_get(vm, -2))) {
992 sq_pushstring(vm, "worlds", -1);
994 if(SQ_FAILED(sq_createslot(vm, -3)))
995 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
997 sq_pushstring(vm, "worlds", -1);
998 if(SQ_FAILED(sq_get(vm, -2)))
999 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1002 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1003 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1006 // construct new table for this worldmap
1007 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1011 sq_pushstring(vm, "tux", -1);
1014 store_float(vm, "x", tux->get_tile_pos().x);
1015 store_float(vm, "y", tux->get_tile_pos().y);
1016 store_string(vm, "back", direction_to_string(tux->back_direction));
1018 sq_createslot(vm, -3);
1021 sq_pushstring(vm, "levels", -1);
1024 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1025 LevelTile* level = *i;
1027 sq_pushstring(vm, level->get_name().c_str(), -1);
1030 store_bool(vm, "solved", level->solved);
1031 level->statistics.serialize_to_squirrel(vm);
1033 sq_createslot(vm, -3);
1036 sq_createslot(vm, -3);
1038 // overall statistics...
1039 total_stats.serialize_to_squirrel(vm);
1041 // push world into worlds table
1042 sq_createslot(vm, -3);
1043 } catch(std::exception& ) {
1044 sq_settop(vm, oldtop);
1047 sq_settop(vm, oldtop);
1049 if(World::current() != NULL)
1050 World::current()->save_state();
1054 WorldMap::load_state()
1056 using namespace Scripting;
1058 HSQUIRRELVM vm = global_vm;
1059 int oldtop = sq_gettop(vm);
1063 sq_pushroottable(vm);
1064 sq_pushstring(vm, "state", -1);
1065 if(SQ_FAILED(sq_get(vm, -2)))
1066 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1069 sq_pushstring(vm, "worlds", -1);
1070 if(SQ_FAILED(sq_get(vm, -2)))
1071 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1073 // get table for our world
1074 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1075 if(SQ_FAILED(sq_get(vm, -2)))
1076 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1079 sq_pushstring(vm, "tux", -1);
1080 if(SQ_FAILED(sq_get(vm, -2)))
1081 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1084 p.x = read_float(vm, "x");
1085 p.y = read_float(vm, "y");
1086 std::string back_str = read_string(vm, "back");
1087 tux->back_direction = string_to_direction(back_str);
1088 tux->set_tile_pos(p);
1093 sq_pushstring(vm, "levels", -1);
1094 if(SQ_FAILED(sq_get(vm, -2)))
1095 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1097 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1098 LevelTile* level = *i;
1099 sq_pushstring(vm, level->get_name().c_str(), -1);
1100 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1101 level->solved = read_bool(vm, "solved");
1102 level->sprite->set_action(level->solved ? "solved" : "default");
1103 level->statistics.unserialize_from_squirrel(vm);
1108 // leave state table
1111 // load overall statistics
1112 total_stats.unserialize_from_squirrel(vm);
1114 } catch(std::exception& e) {
1115 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1117 sq_settop(vm, oldtop);
1123 WorldMap::level_count()
1125 return levels.size();
1129 WorldMap::solved_level_count()
1132 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1133 LevelTile* level = *i;
1143 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1145 using namespace Scripting;
1147 // garbage collect thread list
1148 for(ScriptList::iterator i = scripts.begin();
1149 i != scripts.end(); ) {
1150 HSQOBJECT& object = *i;
1151 HSQUIRRELVM vm = object_to_vm(object);
1153 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1154 sq_release(global_vm, &object);
1155 i = scripts.erase(i);
1162 HSQOBJECT object = create_thread(global_vm);
1163 scripts.push_back(object);
1165 HSQUIRRELVM vm = object_to_vm(object);
1167 // set worldmap_table as roottable for the thread
1168 sq_pushobject(vm, worldmap_table);
1169 sq_setroottable(vm);
1171 compile_and_run(vm, in, sourcename);
1177 WorldMap::get_width() const
1180 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1181 TileMap* solids = *i;
1182 if (solids->get_width() > width) width = solids->get_width();
1188 WorldMap::get_height() const
1191 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1192 TileMap* solids = *i;
1193 if (solids->get_height() > height) height = solids->get_height();
1198 } // namespace WorldMapNS