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) :
149 passive_message_timer(),
161 ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ),
162 force_spawnpoint(force_spawnpoint),
171 music = "music/salcon.ogg";
175 worldmap_menu.reset(new Menu());
176 worldmap_menu->add_label(_("Pause"));
177 worldmap_menu->add_hl();
178 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
179 worldmap_menu->add_submenu(_("Options"), get_options_menu());
180 worldmap_menu->add_hl();
181 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
183 // create a new squirrel table for the worldmap
184 using namespace Scripting;
186 sq_collectgarbage(global_vm);
187 sq_newtable(global_vm);
188 sq_pushroottable(global_vm);
189 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
190 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
192 sq_resetobject(&worldmap_table);
193 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
194 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
196 sq_addref(global_vm, &worldmap_table);
197 sq_pop(global_vm, 1);
199 sound_manager->preload("sounds/warp.wav");
201 // load worldmap objects
205 WorldMap::~WorldMap()
207 using namespace Scripting;
212 for(GameObjects::iterator i = game_objects.begin();
213 i != game_objects.end(); ++i) {
214 GameObject* object = *i;
215 try_unexpose(object);
219 for(SpawnPoints::iterator i = spawn_points.begin();
220 i != spawn_points.end(); ++i) {
224 for(ScriptList::iterator i = scripts.begin();
225 i != scripts.end(); ++i) {
226 HSQOBJECT& object = *i;
227 sq_release(global_vm, &object);
229 sq_release(global_vm, &worldmap_table);
231 sq_collectgarbage(global_vm);
238 WorldMap::add_object(GameObject* object)
240 TileMap* tilemap = dynamic_cast<TileMap*> (object);
241 if(tilemap != 0 && tilemap->is_solid()) {
242 solid_tilemaps.push_back(tilemap);
247 game_objects.push_back(object);
251 WorldMap::try_expose(GameObject* object)
253 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
254 if(interface != NULL) {
255 HSQUIRRELVM vm = Scripting::global_vm;
256 sq_pushobject(vm, worldmap_table);
257 interface->expose(vm, -1);
263 WorldMap::try_unexpose(GameObject* object)
265 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
266 if(interface != NULL) {
267 HSQUIRRELVM vm = Scripting::global_vm;
268 SQInteger oldtop = sq_gettop(vm);
269 sq_pushobject(vm, worldmap_table);
271 interface->unexpose(vm, -1);
272 } catch(std::exception& e) {
273 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
275 sq_settop(vm, oldtop);
280 WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
282 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
284 if(sp->name == spawnpoint) {
286 tux->set_tile_pos(p);
287 tux->set_direction(sp->auto_dir);
290 pan_pos = get_camera_pos_for_tux();
291 clamp_camera_position(pan_pos);
296 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
297 if (spawnpoint != "main") {
298 move_to_spawnpoint("main");
303 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
305 g_main_loop->exit_screen();
306 g_main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
310 WorldMap::load(const std::string& filename)
312 map_filename = filename;
313 levels_path = FileSystem::dirname(map_filename);
317 const lisp::Lisp* root = parser.parse(map_filename);
319 const lisp::Lisp* level = root->get_lisp("supertux-level");
321 throw std::runtime_error("file isn't a supertux-level file.");
323 level->get("name", name);
325 const lisp::Lisp* sector = level->get_lisp("sector");
327 throw std::runtime_error("No sector specified in worldmap file.");
329 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
330 if(tilesets_lisp != NULL) {
331 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
334 std::string tileset_name;
335 if(level->get("tileset", tileset_name)) {
336 if(tileset != NULL) {
337 log_warning << "multiple tilesets specified in level" << std::endl;
339 tileset = tile_manager->get_tileset(tileset_name);
342 /* load default tileset */
343 if(tileset == NULL) {
344 tileset = tile_manager->get_tileset("images/worldmap.strf");
346 current_tileset = tileset;
348 lisp::ListIterator iter(sector);
350 if(iter.item() == "tilemap") {
351 add_object(new TileMap(*(iter.lisp())));
352 } else if(iter.item() == "background") {
353 add_object(new Background(*(iter.lisp())));
354 } else if(iter.item() == "music") {
355 iter.value()->get(music);
356 } else if(iter.item() == "init-script") {
357 iter.value()->get(init_script);
358 } else if(iter.item() == "worldmap-spawnpoint") {
359 SpawnPoint* sp = new SpawnPoint(iter.lisp());
360 spawn_points.push_back(sp);
361 } else if(iter.item() == "level") {
362 LevelTile* level = new LevelTile(levels_path, iter.lisp());
363 levels.push_back(level);
365 } else if(iter.item() == "special-tile") {
366 SpecialTile* special_tile = new SpecialTile(iter.lisp());
367 special_tiles.push_back(special_tile);
368 add_object(special_tile);
369 } else if(iter.item() == "sprite-change") {
370 SpriteChange* sprite_change = new SpriteChange(iter.lisp());
371 sprite_changes.push_back(sprite_change);
372 add_object(sprite_change);
373 } else if(iter.item() == "teleporter") {
374 Teleporter* teleporter = new Teleporter(iter.lisp());
375 teleporters.push_back(teleporter);
376 add_object(teleporter);
377 } else if(iter.item() == "ambient-light") {
378 std::vector<float> vColor;
379 sector->get( "ambient-light", vColor );
380 if(vColor.size() < 3) {
381 log_warning << "(ambient-light) requires a color as argument" << std::endl;
383 ambient_light = Color( vColor );
385 } else if(iter.item() == "name") {
388 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
391 current_tileset = NULL;
393 if(solid_tilemaps.size() == 0)
394 throw std::runtime_error("No solid tilemap specified");
396 move_to_spawnpoint("main");
398 } catch(std::exception& e) {
399 std::stringstream msg;
400 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
402 throw std::runtime_error(msg.str());
407 WorldMap::get_level_title(LevelTile& level)
409 /** get special_tile's title */
410 level.title = "<no title>";
414 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
416 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
420 level_lisp->get("name", level.title);
421 } catch(std::exception& e) {
422 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
427 void WorldMap::calculate_total_stats()
430 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
431 LevelTile* level = *i;
433 total_stats += level->statistics;
439 WorldMap::on_escape_press()
441 // Show or hide the menu
442 if(!Menu::current()) {
443 Menu::set_current(worldmap_menu.get());
444 tux->set_direction(D_NONE); // stop tux movement when menu is called
446 Menu::set_current(NULL);
451 WorldMap::get_next_tile(Vector pos, Direction direction)
473 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
475 *new_pos = get_next_tile(old_pos, direction);
477 if (!(new_pos->x >= 0 && new_pos->x < get_width()
478 && new_pos->y >= 0 && new_pos->y < get_height()))
479 { // New position is outsite the tilemap
483 { // Check if the tile allows us to go to new_pos
484 int old_tile_data = tile_data_at(old_pos);
485 int new_tile_data = tile_data_at(*new_pos);
489 return (old_tile_data & Tile::WORLDMAP_WEST
490 && new_tile_data & Tile::WORLDMAP_EAST);
493 return (old_tile_data & Tile::WORLDMAP_EAST
494 && new_tile_data & Tile::WORLDMAP_WEST);
497 return (old_tile_data & Tile::WORLDMAP_NORTH
498 && new_tile_data & Tile::WORLDMAP_SOUTH);
501 return (old_tile_data & Tile::WORLDMAP_SOUTH
502 && new_tile_data & Tile::WORLDMAP_NORTH);
505 assert(!"path_ok() can't walk if direction is NONE");
512 WorldMap::finished_level(Level* gamelevel)
514 // TODO use Level* parameter here?
515 LevelTile* level = at_level();
517 bool old_level_state = level->solved;
518 level->solved = true;
519 level->sprite->set_action("solved");
521 // deal with statistics
522 level->statistics.merge(gamelevel->stats);
523 calculate_total_stats();
527 if (old_level_state != level->solved) {
528 // Try to detect the next direction to which we should walk
529 // FIXME: Mostly a hack
530 Direction dir = D_NONE;
532 int dirdata = available_directions_at(tux->get_tile_pos());
533 // first, test for crossroads
534 if (dirdata == Tile::WORLDMAP_CNSE ||
535 dirdata == Tile::WORLDMAP_CNSW ||
536 dirdata == Tile::WORLDMAP_CNEW ||
537 dirdata == Tile::WORLDMAP_CSEW ||
538 dirdata == Tile::WORLDMAP_CNSEW)
540 else if (dirdata & Tile::WORLDMAP_NORTH
541 && tux->back_direction != D_NORTH)
543 else if (dirdata & Tile::WORLDMAP_SOUTH
544 && tux->back_direction != D_SOUTH)
546 else if (dirdata & Tile::WORLDMAP_EAST
547 && tux->back_direction != D_EAST)
549 else if (dirdata & Tile::WORLDMAP_WEST
550 && tux->back_direction != D_WEST)
554 tux->set_direction(dir);
558 if (level->extro_script != "") {
560 std::istringstream in(level->extro_script);
561 run_script(in, "worldmap:extro_script");
562 } catch(std::exception& e) {
563 log_warning << "Couldn't run level-extro-script: " << e.what() << std::endl;
569 WorldMap::get_camera_pos_for_tux() {
570 Vector camera_offset;
571 Vector tux_pos = tux->get_pos();
572 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
573 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
574 return camera_offset;
578 WorldMap::clamp_camera_position(Vector& c) {
584 if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
585 c.x = (int)get_width()*32 - SCREEN_WIDTH;
586 if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
587 c.y = (int)get_height()*32 - SCREEN_HEIGHT;
589 if (int(get_width()*32) < SCREEN_WIDTH)
590 c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
591 if (int(get_height()*32) < SCREEN_HEIGHT)
592 c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
596 WorldMap::update(float delta)
599 Menu* menu = Menu::current();
601 if(menu == worldmap_menu.get()) {
602 switch (worldmap_menu->check())
604 case MNID_RETURNWORLDMAP: // Return to game
605 Menu::set_current(0);
607 case MNID_QUITWORLDMAP: // Quit Worldmap
608 g_main_loop->exit_screen();
616 // update GameObjects
617 for(size_t i = 0; i < game_objects.size(); ++i) {
618 GameObject* object = game_objects[i];
619 if(!panning || object != tux) {
620 object->update(delta);
624 // remove old GameObjects
625 for(GameObjects::iterator i = game_objects.begin();
626 i != game_objects.end(); ) {
627 GameObject* object = *i;
628 if(!object->is_valid()) {
629 try_unexpose(object);
631 i = game_objects.erase(i);
637 /* update solid_tilemaps list */
638 //FIXME: this could be more efficient
639 solid_tilemaps.clear();
640 for(std::vector<GameObject*>::iterator i = game_objects.begin();
641 i != game_objects.end(); ++i)
643 TileMap* tm = dynamic_cast<TileMap*>(*i);
645 if (tm->is_solid()) solid_tilemaps.push_back(tm);
648 Vector requested_pos;
652 camera_offset = get_camera_pos_for_tux();
654 Vector delta = pan_pos - camera_offset;
655 float mag = delta.norm();
656 if(mag > CAMERA_PAN_SPEED) {
657 delta *= CAMERA_PAN_SPEED/mag;
659 camera_offset += delta;
660 if(camera_offset == pan_pos) {
665 requested_pos = camera_offset;
666 clamp_camera_position(camera_offset);
669 if(requested_pos.x != camera_offset.x) {
670 pan_pos.x = camera_offset.x;
672 if(requested_pos.y != camera_offset.y) {
673 pan_pos.y = camera_offset.y;
678 bool enter_level = false;
679 if(g_main_controller->pressed(Controller::ACTION)
680 || g_main_controller->pressed(Controller::JUMP)
681 || g_main_controller->pressed(Controller::MENU_SELECT)) {
682 /* some people define UP and JUMP on the same key... */
683 if(!g_main_controller->pressed(Controller::UP))
686 if(g_main_controller->pressed(Controller::PAUSE_MENU))
689 // check for teleporters
690 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
691 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
693 if (teleporter->worldmap != "") {
694 change(teleporter->worldmap, teleporter->spawnpoint);
696 // TODO: an animation, camera scrolling or a fading would be a nice touch
697 sound_manager->play("sounds/warp.wav");
698 tux->back_direction = D_NONE;
699 move_to_spawnpoint(teleporter->spawnpoint, true);
703 // check for auto-play levels
704 LevelTile* level = at_level();
705 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
709 if (enter_level && !tux->is_moving())
711 /* Check level action */
712 LevelTile* level = at_level();
714 //Respawn if player on a tile with no level and nowhere to go.
715 int tile_data = tile_data_at(tux->get_tile_pos());
716 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
717 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
718 move_to_spawnpoint("main");
721 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
725 if (level->pos == tux->get_tile_pos()) {
727 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
728 level->pos.y*32 + 8 - camera_offset.y);
729 std::string levelfile = levels_path + level->get_name();
731 // update state and savegame
734 g_main_loop->push_screen(new GameSession(levelfile, &level->statistics),
735 new ShrinkFade(shrinkpos, 1.0f));
737 } catch(std::exception& e) {
738 log_fatal << "Couldn't load level: " << e.what() << std::endl;
744 // tux->set_direction(input_direction);
750 WorldMap::tile_data_at(Vector p)
754 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
755 TileMap* tilemap = *i;
756 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
757 int dirdata = tile->getData();
765 WorldMap::available_directions_at(Vector p)
767 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
773 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
774 LevelTile* level = *i;
775 if (level->pos == tux->get_tile_pos())
783 WorldMap::at_special_tile()
785 for(SpecialTiles::iterator i = special_tiles.begin();
786 i != special_tiles.end(); ++i) {
787 SpecialTile* special_tile = *i;
788 if (special_tile->pos == tux->get_tile_pos())
796 WorldMap::at_sprite_change(const Vector& pos)
798 for(SpriteChanges::iterator i = sprite_changes.begin();
799 i != sprite_changes.end(); ++i) {
800 SpriteChange* sprite_change = *i;
801 if(sprite_change->pos == pos)
802 return sprite_change;
809 WorldMap::at_teleporter(const Vector& pos)
811 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
812 Teleporter* teleporter = *i;
813 if(teleporter->pos == pos) return teleporter;
820 WorldMap::draw(DrawingContext& context)
822 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
823 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
824 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
826 context.set_ambient_color( ambient_light );
827 context.push_transform();
828 context.set_translation(camera_offset);
830 for(GameObjects::iterator i = game_objects.begin();
831 i != game_objects.end(); ++i) {
832 GameObject* object = *i;
833 if(!panning || object != tux) {
834 object->draw(context);
839 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
840 // draw visual indication of possible walk directions
841 static int flipme = 0;
843 for (int x = 0; x < get_width(); x++) {
844 for (int y = 0; y < get_height(); y++) {
845 int data = tile_data_at(Vector(x,y));
849 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);
850 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);
851 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);
852 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);
853 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);
854 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);
859 draw_status(context);
860 context.pop_transform();
864 WorldMap::draw_status(DrawingContext& context)
866 context.push_transform();
867 context.set_translation(Vector(0, 0));
869 player_status->draw(context);
871 if (!tux->is_moving()) {
872 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
873 LevelTile* level = *i;
875 if (level->pos == tux->get_tile_pos()) {
876 if(level->title == "")
877 get_level_title(*level);
879 context.draw_text(normal_font, level->title,
880 Vector(SCREEN_WIDTH/2,
881 SCREEN_HEIGHT - normal_font->get_height() - 30),
882 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::level_title_color);
884 // if level is solved, draw level picture behind stats
887 if (const Surface* picture = level->get_picture()) {
888 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
889 context.push_transform();
890 context.set_alpha(0.5);
891 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
892 context.pop_transform();
897 level->statistics.draw_worldmap_info(context);
902 for(SpecialTiles::iterator i = special_tiles.begin();
903 i != special_tiles.end(); ++i) {
904 SpecialTile* special_tile = *i;
906 if (special_tile->pos == tux->get_tile_pos()) {
907 /* Display an in-map message in the map, if any as been selected */
908 if(!special_tile->map_message.empty() && !special_tile->passive_message)
909 context.draw_text(normal_font, special_tile->map_message,
910 Vector(SCREEN_WIDTH/2,
911 SCREEN_HEIGHT - normal_font->get_height() - 60),
912 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
917 // display teleporter messages
918 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
919 if (teleporter && (teleporter->message != "")) {
920 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 30);
921 context.draw_text(normal_font, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::teleporter_message_color);
926 /* Display a passive message in the map, if needed */
927 if(passive_message_timer.started())
928 context.draw_text(normal_font, passive_message,
929 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - normal_font->get_height() - 60),
930 ALIGN_CENTER, LAYER_FOREGROUND1, WorldMap::message_color);
932 context.pop_transform();
938 sound_manager->play_music(music);
939 Menu::set_current(NULL);
944 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
945 if (force_spawnpoint != "") {
946 move_to_spawnpoint(force_spawnpoint);
947 force_spawnpoint = "";
952 // register worldmap_table as worldmap in scripting
953 using namespace Scripting;
955 sq_pushroottable(global_vm);
956 sq_pushstring(global_vm, "worldmap", -1);
957 sq_pushobject(global_vm, worldmap_table);
958 if(SQ_FAILED(sq_createslot(global_vm, -3)))
959 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
960 sq_pop(global_vm, 1);
962 //Run default.nut just before init script
964 IFileStream in(levels_path + "/default.nut");
965 run_script(in, "WorldMap::default.nut");
966 } catch(std::exception& ) {
967 // doesn't exist or erroneous; do nothing
970 if(init_script != "") {
971 std::istringstream in(init_script);
972 run_script(in, "WorldMap::init");
979 using namespace Scripting;
981 // save state of world and player
984 // remove worldmap_table from roottable
985 sq_pushroottable(global_vm);
986 sq_pushstring(global_vm, "worldmap", -1);
987 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
988 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
989 sq_pop(global_vm, 1);
993 WorldMap::save_state()
995 using namespace Scripting;
997 HSQUIRRELVM vm = global_vm;
998 int oldtop = sq_gettop(vm);
1002 sq_pushroottable(vm);
1003 sq_pushstring(vm, "state", -1);
1004 if(SQ_FAILED(sq_get(vm, -2)))
1005 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1007 // get or create worlds table
1008 sq_pushstring(vm, "worlds", -1);
1009 if(SQ_FAILED(sq_get(vm, -2))) {
1010 sq_pushstring(vm, "worlds", -1);
1012 if(SQ_FAILED(sq_createslot(vm, -3)))
1013 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
1015 sq_pushstring(vm, "worlds", -1);
1016 if(SQ_FAILED(sq_get(vm, -2)))
1017 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1020 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1021 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1024 // construct new table for this worldmap
1025 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1029 sq_pushstring(vm, "tux", -1);
1032 store_float(vm, "x", tux->get_tile_pos().x);
1033 store_float(vm, "y", tux->get_tile_pos().y);
1034 store_string(vm, "back", direction_to_string(tux->back_direction));
1036 sq_createslot(vm, -3);
1039 sq_pushstring(vm, "levels", -1);
1042 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1043 LevelTile* level = *i;
1045 sq_pushstring(vm, level->get_name().c_str(), -1);
1048 store_bool(vm, "solved", level->solved);
1049 level->statistics.serialize_to_squirrel(vm);
1051 sq_createslot(vm, -3);
1054 sq_createslot(vm, -3);
1056 // overall statistics...
1057 total_stats.serialize_to_squirrel(vm);
1059 // push world into worlds table
1060 sq_createslot(vm, -3);
1061 } catch(std::exception& ) {
1062 sq_settop(vm, oldtop);
1065 sq_settop(vm, oldtop);
1067 if(World::current() != NULL)
1068 World::current()->save_state();
1072 WorldMap::load_state()
1074 using namespace Scripting;
1076 HSQUIRRELVM vm = global_vm;
1077 int oldtop = sq_gettop(vm);
1081 sq_pushroottable(vm);
1082 sq_pushstring(vm, "state", -1);
1083 if(SQ_FAILED(sq_get(vm, -2)))
1084 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1087 sq_pushstring(vm, "worlds", -1);
1088 if(SQ_FAILED(sq_get(vm, -2)))
1089 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1091 // get table for our world
1092 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1093 if(SQ_FAILED(sq_get(vm, -2)))
1094 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1097 sq_pushstring(vm, "tux", -1);
1098 if(SQ_FAILED(sq_get(vm, -2)))
1099 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1102 p.x = read_float(vm, "x");
1103 p.y = read_float(vm, "y");
1104 std::string back_str = read_string(vm, "back");
1105 tux->back_direction = string_to_direction(back_str);
1106 tux->set_tile_pos(p);
1111 sq_pushstring(vm, "levels", -1);
1112 if(SQ_FAILED(sq_get(vm, -2)))
1113 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1115 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1116 LevelTile* level = *i;
1117 sq_pushstring(vm, level->get_name().c_str(), -1);
1118 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1119 level->solved = read_bool(vm, "solved");
1120 level->sprite->set_action(level->solved ? "solved" : "default");
1121 level->statistics.unserialize_from_squirrel(vm);
1126 // leave state table
1129 // load overall statistics
1130 total_stats.unserialize_from_squirrel(vm);
1132 } catch(std::exception& e) {
1133 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1135 sq_settop(vm, oldtop);
1141 WorldMap::level_count()
1143 return levels.size();
1147 WorldMap::solved_level_count()
1150 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1151 LevelTile* level = *i;
1161 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1163 using namespace Scripting;
1165 // garbage collect thread list
1166 for(ScriptList::iterator i = scripts.begin();
1167 i != scripts.end(); ) {
1168 HSQOBJECT& object = *i;
1169 HSQUIRRELVM vm = object_to_vm(object);
1171 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1172 sq_release(global_vm, &object);
1173 i = scripts.erase(i);
1180 HSQOBJECT object = create_thread(global_vm);
1181 scripts.push_back(object);
1183 HSQUIRRELVM vm = object_to_vm(object);
1185 // set worldmap_table as roottable for the thread
1186 sq_pushobject(vm, worldmap_table);
1187 sq_setroottable(vm);
1189 compile_and_run(vm, in, sourcename);
1195 WorldMap::get_width() const
1198 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1199 TileMap* solids = *i;
1200 if (solids->get_width() > width) width = solids->get_width();
1206 WorldMap::get_height() const
1209 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1210 TileMap* solids = *i;
1211 if (solids->get_height() > height) height = solids->get_height();
1216 } // namespace WorldMapNS