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( "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();
580 if(menu == worldmap_menu.get()) {
581 switch (worldmap_menu->check())
583 case MNID_RETURNWORLDMAP: // Return to game
584 Menu::set_current(0);
586 case MNID_QUITWORLDMAP: // Quit Worldmap
587 main_loop->exit_screen();
595 // update GameObjects
596 for(size_t i = 0; i < game_objects.size(); ++i) {
597 GameObject* object = game_objects[i];
598 if(!panning || object != tux) {
599 object->update(delta);
603 // remove old GameObjects
604 for(GameObjects::iterator i = game_objects.begin();
605 i != game_objects.end(); ) {
606 GameObject* object = *i;
607 if(!object->is_valid()) {
608 try_unexpose(object);
610 i = game_objects.erase(i);
616 /* update solid_tilemaps list */
617 //FIXME: this could be more efficient
618 solid_tilemaps.clear();
619 for(std::vector<GameObject*>::iterator i = game_objects.begin();
620 i != game_objects.end(); ++i)
622 TileMap* tm = dynamic_cast<TileMap*>(*i);
624 if (tm->is_solid()) solid_tilemaps.push_back(tm);
627 Vector requested_pos;
631 camera_offset = get_camera_pos_for_tux();
633 Vector delta = pan_pos - camera_offset;
634 float mag = delta.norm();
635 if(mag > CAMERA_PAN_SPEED) {
636 delta *= CAMERA_PAN_SPEED/mag;
638 camera_offset += delta;
639 if(camera_offset == pan_pos) {
644 requested_pos = camera_offset;
645 clamp_camera_position(camera_offset);
648 if(requested_pos.x != camera_offset.x) {
649 pan_pos.x = camera_offset.x;
651 if(requested_pos.y != camera_offset.y) {
652 pan_pos.y = camera_offset.y;
657 bool enter_level = false;
658 if(main_controller->pressed(Controller::ACTION)
659 || main_controller->pressed(Controller::JUMP)
660 || main_controller->pressed(Controller::MENU_SELECT)) {
661 /* some people define UP and JUMP on the same key... */
662 if(!main_controller->pressed(Controller::UP))
665 if(main_controller->pressed(Controller::PAUSE_MENU))
668 // check for teleporters
669 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
670 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
672 if (teleporter->worldmap != "") {
673 change(teleporter->worldmap, teleporter->spawnpoint);
675 // TODO: an animation, camera scrolling or a fading would be a nice touch
676 sound_manager->play("sounds/warp.wav");
677 tux->back_direction = D_NONE;
678 move_to_spawnpoint(teleporter->spawnpoint, true);
682 // check for auto-play levels
683 LevelTile* level = at_level();
684 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
688 if (enter_level && !tux->is_moving())
690 /* Check level action */
691 LevelTile* level = at_level();
693 //Respawn if player on a tile with no level and nowhere to go.
694 int tile_data = tile_data_at(tux->get_tile_pos());
695 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
696 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
697 move_to_spawnpoint("main");
700 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
704 if (level->pos == tux->get_tile_pos()) {
706 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
707 level->pos.y*32 + 8 - camera_offset.y);
708 std::string levelfile = levels_path + level->get_name();
710 // update state and savegame
713 main_loop->push_screen(new GameSession(levelfile, &level->statistics),
714 new ShrinkFade(shrinkpos, 1.0f));
716 } catch(std::exception& e) {
717 log_fatal << "Couldn't load level: " << e.what() << std::endl;
723 // tux->set_direction(input_direction);
729 WorldMap::tile_data_at(Vector p)
733 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
734 TileMap* tilemap = *i;
735 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
736 int dirdata = tile->getData();
744 WorldMap::available_directions_at(Vector p)
746 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
752 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
753 LevelTile* level = *i;
754 if (level->pos == tux->get_tile_pos())
762 WorldMap::at_special_tile()
764 for(SpecialTiles::iterator i = special_tiles.begin();
765 i != special_tiles.end(); ++i) {
766 SpecialTile* special_tile = *i;
767 if (special_tile->pos == tux->get_tile_pos())
775 WorldMap::at_sprite_change(const Vector& pos)
777 for(SpriteChanges::iterator i = sprite_changes.begin();
778 i != sprite_changes.end(); ++i) {
779 SpriteChange* sprite_change = *i;
780 if(sprite_change->pos == pos)
781 return sprite_change;
788 WorldMap::at_teleporter(const Vector& pos)
790 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
791 Teleporter* teleporter = *i;
792 if(teleporter->pos == pos) return teleporter;
799 WorldMap::draw(DrawingContext& context)
801 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
802 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
803 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
805 context.set_ambient_color( ambient_light );
806 context.push_transform();
807 context.set_translation(camera_offset);
809 for(GameObjects::iterator i = game_objects.begin();
810 i != game_objects.end(); ++i) {
811 GameObject* object = *i;
812 if(!panning || object != tux) {
813 object->draw(context);
818 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
819 // draw visual indication of possible walk directions
820 static int flipme = 0;
822 for (int x = 0; x < get_width(); x++) {
823 for (int y = 0; y < get_height(); y++) {
824 int data = tile_data_at(Vector(x,y));
828 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);
829 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);
830 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);
831 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);
832 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);
833 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);
838 draw_status(context);
839 context.pop_transform();
843 WorldMap::draw_status(DrawingContext& context)
845 context.push_transform();
846 context.set_translation(Vector(0, 0));
848 player_status->draw(context);
850 if (!tux->is_moving()) {
851 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
852 LevelTile* level = *i;
854 if (level->pos == tux->get_tile_pos()) {
855 if(level->title == "")
856 get_level_title(*level);
858 context.draw_text(normal_font, level->title,
859 Vector(SCREEN_WIDTH/2,
860 SCREEN_HEIGHT - normal_font->get_height() - 30),
861 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
863 // if level is solved, draw level picture behind stats
866 if (const Surface* picture = level->get_picture()) {
867 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
868 context.push_transform();
869 context.set_alpha(0.5);
870 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
871 context.pop_transform();
876 level->statistics.draw_worldmap_info(context);
881 for(SpecialTiles::iterator i = special_tiles.begin();
882 i != special_tiles.end(); ++i) {
883 SpecialTile* special_tile = *i;
885 if (special_tile->pos == tux->get_tile_pos()) {
886 /* Display an in-map message in the map, if any as been selected */
887 if(!special_tile->map_message.empty() && !special_tile->passive_message)
888 context.draw_text(normal_font, special_tile->map_message,
889 Vector(SCREEN_WIDTH/2,
890 SCREEN_HEIGHT - normal_font->get_height() - 60),
891 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
896 // display teleporter messages
897 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
898 if (teleporter && (teleporter->message != "")) {
899 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 30);
900 context.draw_text(normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
905 /* Display a passive message in the map, if needed */
906 if(passive_message_timer.started())
907 context.draw_text(normal_font, passive_message,
908 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 60),
909 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
911 context.pop_transform();
917 sound_manager->play_music(music);
918 Menu::set_current(NULL);
923 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
924 if (force_spawnpoint != "") {
925 move_to_spawnpoint(force_spawnpoint);
926 force_spawnpoint = "";
931 // register worldmap_table as worldmap in scripting
932 using namespace Scripting;
934 sq_pushroottable(global_vm);
935 sq_pushstring(global_vm, "worldmap", -1);
936 sq_pushobject(global_vm, worldmap_table);
937 if(SQ_FAILED(sq_createslot(global_vm, -3)))
938 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
939 sq_pop(global_vm, 1);
941 //Run default.nut just before init script
943 IFileStream in(levels_path + "/default.nut");
944 run_script(in, "WorldMap::default.nut");
945 } catch(std::exception& ) {
946 // 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