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 "video/surface.hpp"
37 #include "video/screen.hpp"
38 #include "video/drawing_context.hpp"
39 #include "sprite/sprite_manager.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "lisp/parser.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/list_iterator.hpp"
44 #include "lisp/writer.hpp"
45 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "resources.hpp"
52 #include "player_status.hpp"
53 #include "textscroller.hpp"
55 #include "spawn_point.hpp"
56 #include "file_system.hpp"
57 #include "gui/menu.hpp"
58 #include "gui/mousecursor.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "object/background.hpp"
61 #include "object/tilemap.hpp"
62 #include "scripting/squirrel_error.hpp"
63 #include "scripting/wrapper_util.hpp"
65 Menu* worldmap_menu = 0;
67 static const float TUXSPEED = 200;
68 static const float map_message_TIME = 2.8;
70 namespace WorldMapNS {
72 WorldMap* WorldMap::current_ = NULL;
74 Direction reverse_dir(Direction direction)
93 direction_to_string(Direction direction)
111 string_to_direction(const std::string& directory)
113 if (directory == "west")
115 else if (directory == "east")
117 else if (directory == "north")
119 else if (directory == "south")
125 //---------------------------------------------------------------------------
127 Tux::Tux(WorldMap* worldmap_)
128 : worldmap(worldmap_)
130 tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
135 input_direction = D_NONE;
144 Tux::draw(DrawingContext& context)
146 switch (player_status->bonus) {
148 tux_sprite->set_action(moving ? "large-walking" : "large-stop");
151 tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
154 tux_sprite->set_action(moving ? "small-walking" : "small-stop");
157 log_debug << "Bonus type not handled in worldmap." << std::endl;
158 tux_sprite->set_action("large-stop");
162 tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
169 float x = tile_pos.x * 32;
170 float y = tile_pos.y * 32;
198 input_direction = D_NONE;
203 Tux::set_direction(Direction dir)
205 input_direction = dir;
209 Tux::tryStartWalking()
212 if (input_direction == D_NONE) return;
214 WorldMap::Level* level = worldmap->at_level();
216 // We got a new direction, so lets start walking when possible
218 if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
220 tile_pos = next_tile;
222 direction = input_direction;
223 back_direction = reverse_dir(direction);
225 else if (input_direction == back_direction)
228 direction = input_direction;
229 tile_pos = worldmap->get_next_tile(tile_pos, direction);
230 back_direction = reverse_dir(direction);
236 Tux::canWalk(const Tile* tile, Direction dir)
238 return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
239 (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
240 (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
241 (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
245 Tux::tryContinueWalking(float elapsed_time)
250 offset += TUXSPEED * elapsed_time;
252 // Do nothing if we have not yet reached the next tile
253 if (offset <= 32) return;
257 // if this is a special_tile with passive_message, display it
258 WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
259 if(special_tile && special_tile->passive_message)
261 // direction and the apply_action_ are opposites, since they "see"
262 // directions in a different way
263 if((direction == D_NORTH && special_tile->apply_action_south) ||
264 (direction == D_SOUTH && special_tile->apply_action_north) ||
265 (direction == D_WEST && special_tile->apply_action_east) ||
266 (direction == D_EAST && special_tile->apply_action_west))
268 worldmap->passive_message = special_tile->map_message;
269 worldmap->passive_message_timer.start(map_message_TIME);
273 // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
274 if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
276 if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
281 // if user wants to change direction, try changing, else guess the direction in which to walk next
282 const Tile* tile = worldmap->at(tile_pos);
283 if (direction != input_direction)
285 if(canWalk(tile, input_direction))
287 direction = input_direction;
288 back_direction = reverse_dir(direction);
293 Direction dir = D_NONE;
294 if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
295 else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
296 else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
297 else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
301 // Should never be reached if tiledata is good
302 log_warning << "Could not determine where to walk next" << std::endl;
308 input_direction = direction;
309 back_direction = reverse_dir(direction);
312 // Walk automatically to the next tile
313 if(direction != D_NONE)
316 if (worldmap->path_ok(direction, tile_pos, &next_tile))
318 tile_pos = next_tile;
322 log_warning << "Tilemap data is buggy" << std::endl;
329 Tux::updateInputDirection()
331 if(main_controller->hold(Controller::UP)) input_direction = D_NORTH;
332 else if(main_controller->hold(Controller::DOWN)) input_direction = D_SOUTH;
333 else if(main_controller->hold(Controller::LEFT)) input_direction = D_WEST;
334 else if(main_controller->hold(Controller::RIGHT)) input_direction = D_EAST;
339 Tux::update(float elapsed_time)
341 updateInputDirection();
342 if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
345 //---------------------------------------------------------------------------
350 tile_manager = new TileManager("images/worldmap.strf");
355 messagedot = new Surface("images/worldmap/common/messagedot.png");
356 teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
359 music = "music/salcon.ogg";
360 intro_displayed = false;
365 WorldMap::~WorldMap()
371 for(SpawnPoints::iterator i = spawn_points.begin();
372 i != spawn_points.end(); ++i) {
375 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
379 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i) {
386 delete teleporterdot;
390 WorldMap::add_object(GameObject* object)
392 TileMap* tilemap = dynamic_cast<TileMap*> (object);
393 if(tilemap != 0 && tilemap->is_solid()) {
397 game_objects.push_back(object);
401 WorldMap::clear_objects()
403 for(GameObjects::iterator i = game_objects.begin();
404 i != game_objects.end(); ++i)
406 game_objects.clear();
412 // Don't forget to set map_filename before calling this
416 levels_path = FileSystem::dirname(map_filename);
420 std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
422 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
424 throw std::runtime_error("file isn't a supertux-level file.");
426 lisp->get("name", name);
428 const lisp::Lisp* sector = lisp->get_lisp("sector");
430 throw std::runtime_error("No sector sepcified in worldmap file.");
433 lisp::ListIterator iter(sector);
435 if(iter.item() == "tilemap") {
436 add_object(new TileMap(*(iter.lisp()), tile_manager));
437 } else if(iter.item() == "background") {
438 add_object(new Background(*(iter.lisp())));
439 } else if(iter.item() == "music") {
440 iter.value()->get(music);
441 } else if(iter.item() == "intro-script") {
442 iter.value()->get(intro_script);
443 } else if(iter.item() == "worldmap-spawnpoint") {
444 SpawnPoint* sp = new SpawnPoint(iter.lisp());
445 spawn_points.push_back(sp);
446 } else if(iter.item() == "level") {
447 parse_level_tile(iter.lisp());
448 } else if(iter.item() == "special-tile") {
449 parse_special_tile(iter.lisp());
450 } else if(iter.item() == "name") {
453 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
457 throw std::runtime_error("No solid tilemap specified");
459 // search for main spawnpoint
460 for(SpawnPoints::iterator i = spawn_points.begin();
461 i != spawn_points.end(); ++i) {
463 if(sp->name == "main") {
465 tux->set_tile_pos(p);
470 } catch(std::exception& e) {
471 std::stringstream msg;
472 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
474 throw std::runtime_error(msg.str());
479 WorldMap::parse_special_tile(const lisp::Lisp* lisp)
481 SpecialTile special_tile;
483 lisp->get("x", special_tile.pos.x);
484 lisp->get("y", special_tile.pos.y);
487 if (lisp->get("sprite", sprite)) {
488 special_tile.sprite = sprite_manager->create(sprite);
490 special_tile.sprite = 0;
493 lisp->get("map-message", special_tile.map_message);
494 special_tile.passive_message = false;
495 lisp->get("passive-message", special_tile.passive_message);
496 special_tile.teleport_dest = Vector(-1,-1);
497 lisp->get("teleport-to-x", special_tile.teleport_dest.x);
498 lisp->get("teleport-to-y", special_tile.teleport_dest.y);
499 special_tile.invisible = false;
500 lisp->get("invisible-tile", special_tile.invisible);
502 special_tile.apply_action_north = true;
503 special_tile.apply_action_south = true;
504 special_tile.apply_action_east = true;
505 special_tile.apply_action_west = true;
507 std::string apply_direction;
508 lisp->get("apply-to-direction", apply_direction);
509 if(!apply_direction.empty()) {
510 special_tile.apply_action_north = false;
511 special_tile.apply_action_south = false;
512 special_tile.apply_action_east = false;
513 special_tile.apply_action_west = false;
514 if(apply_direction.find("north") != std::string::npos)
515 special_tile.apply_action_north = true;
516 if(apply_direction.find("south") != std::string::npos)
517 special_tile.apply_action_south = true;
518 if(apply_direction.find("east") != std::string::npos)
519 special_tile.apply_action_east = true;
520 if(apply_direction.find("west") != std::string::npos)
521 special_tile.apply_action_west = true;
524 special_tiles.push_back(special_tile);
528 WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
532 level.solved = false;
539 std::string sprite = "images/worldmap/common/leveldot.sprite";
540 level_lisp->get("sprite", sprite);
541 level.sprite = sprite_manager->create(sprite);
543 level_lisp->get("extro-script", level.extro_script);
544 level_lisp->get("next-worldmap", level.next_worldmap);
546 level.quit_worldmap = false;
547 level_lisp->get("quit-worldmap", level.quit_worldmap);
549 level_lisp->get("name", level.name);
551 if (!PHYSFS_exists((levels_path + level.name).c_str()))
553 // Do we want to bail out instead...? We might get messages from modders
554 // who can't make their levels run because they're too dumb to watch
555 // their terminals...
556 log_warning << "level file '" << level.name << "' does not exist and will not be added to the worldmap" << std::endl;
560 level_lisp->get("x", level.pos.x);
561 level_lisp->get("y", level.pos.y);
563 level.auto_path = true;
564 level_lisp->get("auto-path", level.auto_path);
566 level.vertical_flip = false;
567 level_lisp->get("vertical-flip", level.vertical_flip);
569 levels.push_back(level);
573 WorldMap::get_level_title(Level& level)
575 /** get special_tile's title */
576 level.title = "<no title>";
580 std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
582 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
586 level_lisp->get("name", level.title);
587 } catch(std::exception& e) {
588 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
593 void WorldMap::calculate_total_stats()
596 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
600 total_stats += i->statistics;
606 WorldMap::on_escape_press()
608 // Show or hide the menu
609 if(!Menu::current()) {
610 Menu::set_current(worldmap_menu);
611 tux->set_direction(D_NONE); // stop tux movement when menu is called
613 Menu::set_current(0);
618 WorldMap::get_next_tile(Vector pos, Direction direction)
640 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
642 *new_pos = get_next_tile(old_pos, direction);
644 if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
645 && new_pos->y >= 0 && new_pos->y < solids->get_height()))
646 { // New position is outsite the tilemap
650 { // Check if the tile allows us to go to new_pos
654 return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
655 && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
658 return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
659 && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
662 return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
663 && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
666 return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
667 && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
670 assert(!"path_ok() can't work if direction is NONE");
677 WorldMap::finished_level(const std::string& filename)
679 // TODO calculate level from filename?
681 Level* level = at_level();
683 bool old_level_state = level->solved;
684 level->solved = true;
685 level->sprite->set_action("solved");
687 // deal with statistics
688 level->statistics.merge(global_stats);
689 calculate_total_stats();
692 if(World::current() != NULL)
693 World::current()->save_state();
695 if (old_level_state != level->solved && level->auto_path) {
696 // Try to detect the next direction to which we should walk
697 // FIXME: Mostly a hack
698 Direction dir = D_NONE;
700 const Tile* tile = at(tux->get_tile_pos());
702 // first, test for crossroads
703 if (tile->getData() & Tile::WORLDMAP_CNSE || tile->getData() && Tile::WORLDMAP_CNSW
704 || tile->getData() & Tile::WORLDMAP_CNEW || tile->getData() && Tile::WORLDMAP_CSEW
705 || tile->getData() & Tile::WORLDMAP_CNSEW)
707 else if (tile->getData() & Tile::WORLDMAP_NORTH
708 && tux->back_direction != D_NORTH)
710 else if (tile->getData() & Tile::WORLDMAP_SOUTH
711 && tux->back_direction != D_SOUTH)
713 else if (tile->getData() & Tile::WORLDMAP_EAST
714 && tux->back_direction != D_EAST)
716 else if (tile->getData() & Tile::WORLDMAP_WEST
717 && tux->back_direction != D_WEST)
721 tux->set_direction(dir);
725 if (level->extro_script != "") {
728 std::auto_ptr<ScriptInterpreter> interpreter
729 (new ScriptInterpreter(levels_path));
730 std::istringstream in(level->extro_script);
731 interpreter->run_script(in, "level-extro-script");
732 add_object(interpreter.release());
733 } catch(std::exception& e) {
734 log_fatal << "Couldn't run level-extro-script:" << e.what() << std::endl;
739 if (!level->next_worldmap.empty()) {
740 // Load given worldmap
741 loadmap(level->next_worldmap);
744 if (level->quit_worldmap)
745 main_loop->exit_screen();
749 WorldMap::update(float delta)
751 Menu* menu = Menu::current();
755 if(menu == worldmap_menu) {
756 switch (worldmap_menu->check())
758 case MNID_RETURNWORLDMAP: // Return to game
759 Menu::set_current(0);
761 case MNID_QUITWORLDMAP: // Quit Worldmap
762 main_loop->exit_screen();
770 // update GameObjects
771 for(GameObjects::iterator i = game_objects.begin();
772 i != game_objects.end(); ++i) {
773 GameObject* object = *i;
774 object->update(delta);
777 // remove old GameObjects
778 for(GameObjects::iterator i = game_objects.begin();
779 i != game_objects.end(); ) {
780 GameObject* object = *i;
781 if(!object->is_valid()) {
783 i = game_objects.erase(i);
790 Vector tux_pos = tux->get_pos();
791 camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
792 camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
794 if (camera_offset.x < 0)
796 if (camera_offset.y < 0)
799 if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
800 camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
801 if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
802 camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
805 bool enter_level = false;
806 if(main_controller->pressed(Controller::ACTION)
807 || main_controller->pressed(Controller::JUMP)
808 || main_controller->pressed(Controller::MENU_SELECT))
810 if(main_controller->pressed(Controller::PAUSE_MENU))
813 if (enter_level && !tux->is_moving())
815 /* Check special tile action */
816 SpecialTile* special_tile = at_special_tile();
819 if (special_tile->teleport_dest != Vector(-1,-1))
821 // TODO: an animation, camera scrolling or a fading would be a nice touch
822 sound_manager->play("sounds/warp.wav");
823 tux->back_direction = D_NONE;
824 tux->set_tile_pos(special_tile->teleport_dest);
829 /* Check level action */
830 Level* level = at_level();
832 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
836 if (level->pos == tux->get_tile_pos()) {
837 // do a shriking fade to the level
838 shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
839 (level->pos.y*32 + 16 + offset.y)), 500);
842 GameSession *session =
843 new GameSession(levels_path + level->name,
844 ST_GL_LOAD_LEVEL_FILE, &level->statistics);
845 main_loop->push_screen(session);
846 } catch(std::exception& e) {
847 log_fatal << "Couldn't load level: " << e.what() << std::endl;
853 // tux->set_direction(input_direction);
858 WorldMap::at(Vector p)
860 return solids->get_tile((int) p.x, (int) p.y);
866 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
868 if (i->pos == tux->get_tile_pos())
875 WorldMap::SpecialTile*
876 WorldMap::at_special_tile()
878 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
880 if (i->pos == tux->get_tile_pos())
888 WorldMap::draw(DrawingContext& context)
890 context.push_transform();
891 context.set_translation(camera_offset);
893 for(GameObjects::iterator i = game_objects.begin();
894 i != game_objects.end(); ++i) {
895 GameObject* object = *i;
896 object->draw(context);
899 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
901 const Level& level = *i;
902 level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
905 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
911 i->sprite->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
913 else if (i->teleport_dest != Vector(-1, -1))
914 teleporterdot->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
916 else if (!i->map_message.empty() && !i->passive_message)
917 context.draw_surface(messagedot,
918 Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
921 draw_status(context);
922 context.pop_transform();
926 WorldMap::draw_status(DrawingContext& context)
928 context.push_transform();
929 context.set_translation(Vector(0, 0));
931 player_status->draw(context);
933 if (!tux->is_moving())
935 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
937 if (i->pos == tux->get_tile_pos())
942 context.draw_text(white_text, i->title,
943 Vector(SCREEN_WIDTH/2,
944 SCREEN_HEIGHT - white_text->get_height() - 30),
945 CENTER_ALLIGN, LAYER_FOREGROUND1);
947 i->statistics.draw_worldmap_info(context);
951 for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
953 if (i->pos == tux->get_tile_pos())
955 /* Display an in-map message in the map, if any as been selected */
956 if(!i->map_message.empty() && !i->passive_message)
957 context.draw_text(gold_text, i->map_message,
958 Vector(SCREEN_WIDTH/2,
959 SCREEN_HEIGHT - white_text->get_height() - 60),
960 CENTER_ALLIGN, LAYER_FOREGROUND1);
965 /* Display a passive message in the map, if needed */
966 if(passive_message_timer.started())
967 context.draw_text(gold_text, passive_message,
968 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
969 CENTER_ALLIGN, LAYER_FOREGROUND1);
971 context.pop_transform();
977 sound_manager->play_music(music);
978 Menu::set_current(NULL);
984 static void store_float(HSQUIRRELVM vm, const char* name, float val)
986 sq_pushstring(vm, name, -1);
987 sq_pushfloat(vm, val);
988 if(SQ_FAILED(sq_createslot(vm, -3)))
989 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
993 static void store_int(HSQUIRRELVM vm, const char* name, int val)
995 sq_pushstring(vm, name, -1);
996 sq_pushinteger(vm, val);
997 if(SQ_FAILED(sq_createslot(vm, -3)))
998 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
1002 static void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
1004 sq_pushstring(vm, name, -1);
1005 sq_pushstring(vm, val.c_str(), val.length());
1006 if(SQ_FAILED(sq_createslot(vm, -3)))
1007 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
1010 static void store_bool(HSQUIRRELVM vm, const char* name, bool val)
1012 sq_pushstring(vm, name, -1);
1013 sq_pushbool(vm, val ? SQTrue : SQFalse);
1014 if(SQ_FAILED(sq_createslot(vm, -3)))
1015 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
1018 static float read_float(HSQUIRRELVM vm, const char* name)
1020 sq_pushstring(vm, name, -1);
1021 if(SQ_FAILED(sq_get(vm, -2))) {
1022 std::ostringstream msg;
1023 msg << "Couldn't get float value for '" << name << "' from table";
1024 throw Scripting::SquirrelError(vm, msg.str());
1028 if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
1029 std::ostringstream msg;
1030 msg << "Couldn't get float value for '" << name << "' from table";
1031 throw Scripting::SquirrelError(vm, msg.str());
1038 static std::string read_string(HSQUIRRELVM vm, const char* name)
1040 sq_pushstring(vm, name, -1);
1041 if(SQ_FAILED(sq_get(vm, -2))) {
1042 std::ostringstream msg;
1043 msg << "Couldn't get string value for '" << name << "' from table";
1044 throw Scripting::SquirrelError(vm, msg.str());
1048 if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
1049 std::ostringstream msg;
1050 msg << "Couldn't get string value for '" << name << "' from table";
1051 throw Scripting::SquirrelError(vm, msg.str());
1055 return std::string(result);
1058 static bool read_bool(HSQUIRRELVM vm, const char* name)
1060 sq_pushstring(vm, name, -1);
1061 if(SQ_FAILED(sq_get(vm, -2))) {
1062 std::ostringstream msg;
1063 msg << "Couldn't get bool value for '" << name << "' from table";
1064 throw Scripting::SquirrelError(vm, msg.str());
1068 if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
1069 std::ostringstream msg;
1070 msg << "Couldn't get bool value for '" << name << "' from table";
1071 throw Scripting::SquirrelError(vm, msg.str());
1075 return result == SQTrue;
1079 WorldMap::save_state()
1081 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
1082 int oldtop = sq_gettop(vm);
1086 sq_pushroottable(vm);
1087 sq_pushstring(vm, "state", -1);
1088 if(SQ_FAILED(sq_get(vm, -2)))
1089 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1091 // get or create worlds table
1092 sq_pushstring(vm, "worlds", -1);
1093 if(SQ_FAILED(sq_get(vm, -2))) {
1094 sq_pushstring(vm, "worlds", -1);
1096 if(SQ_FAILED(sq_createslot(vm, -3)))
1097 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
1099 sq_pushstring(vm, "worlds", -1);
1100 if(SQ_FAILED(sq_get(vm, -2)))
1101 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
1104 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1105 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
1108 // construct new table for this worldmap
1109 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1113 sq_pushstring(vm, "tux", -1);
1116 store_float(vm, "x", tux->get_tile_pos().x);
1117 store_float(vm, "y", tux->get_tile_pos().y);
1118 store_string(vm, "back", direction_to_string(tux->back_direction));
1120 sq_createslot(vm, -3);
1123 sq_pushstring(vm, "levels", -1);
1126 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1128 sq_pushstring(vm, i->name.c_str(), -1);
1131 store_bool(vm, "solved", true);
1132 // TODO write statistics
1133 // i->statistics.write(writer);
1135 sq_createslot(vm, -3);
1139 sq_createslot(vm, -3);
1141 // push world into worlds table
1142 sq_createslot(vm, -3);
1143 } catch(std::exception& e) {
1144 sq_settop(vm, oldtop);
1147 sq_settop(vm, oldtop);
1151 WorldMap::load_state()
1153 HSQUIRRELVM vm = ScriptManager::instance->get_vm();
1154 int oldtop = sq_gettop(vm);
1158 sq_pushroottable(vm);
1159 sq_pushstring(vm, "state", -1);
1160 if(SQ_FAILED(sq_get(vm, -2)))
1161 throw Scripting::SquirrelError(vm, "Couldn't get state table");
1164 sq_pushstring(vm, "worlds", -1);
1165 if(SQ_FAILED(sq_get(vm, -2)))
1166 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
1168 // get table for our world
1169 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
1170 if(SQ_FAILED(sq_get(vm, -2)))
1171 throw Scripting::SquirrelError(vm, "Couldn't get state.world.mapfilename");
1174 sq_pushstring(vm, "tux", -1);
1175 if(SQ_FAILED(sq_get(vm, -2)))
1176 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1179 p.x = read_float(vm, "x");
1180 p.y = read_float(vm, "y");
1181 std::string back_str = read_string(vm, "back");
1182 tux->back_direction = string_to_direction(back_str);
1183 tux->set_tile_pos(p);
1188 sq_pushstring(vm, "levels", -1);
1189 if(SQ_FAILED(sq_get(vm, -2)))
1190 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1192 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1193 sq_pushstring(vm, i->name.c_str(), -1);
1194 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1195 i->solved = read_bool(vm, "solved");
1196 i->sprite->set_action(i->solved ? "solved" : "default");
1197 // i->statistics.parse(*level);
1203 } catch(std::exception& e) {
1204 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1206 sq_settop(vm, oldtop);
1210 WorldMap::level_count()
1212 return levels.size();
1216 WorldMap::solved_level_count()
1219 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
1228 WorldMap::loadmap(const std::string& filename)
1231 map_filename = filename;
1235 } // namespace WorldMapNS