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 "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 "options_menu.hpp"
63 #include "scripting/squirrel_error.hpp"
64 #include "scripting/squirrel_util.hpp"
65 #include "worldmap/level.hpp"
66 #include "worldmap/special_tile.hpp"
67 #include "worldmap/tux.hpp"
68 #include "worldmap/sprite_change.hpp"
70 namespace WorldMapNS {
72 enum WorldMapMenuIDs {
77 WorldMap* WorldMap::current_ = NULL;
79 Direction reverse_dir(Direction direction)
98 direction_to_string(Direction direction)
116 string_to_direction(const std::string& directory)
118 if (directory == "west")
120 else if (directory == "east")
122 else if (directory == "north")
124 else if (directory == "south")
126 else if (directory == "none")
129 log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
134 //---------------------------------------------------------------------------
136 WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint)
137 : tux(0), ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint), in_level(false)
139 tile_manager.reset(new TileManager());
140 //"images/worldmap.strf");
146 music = "music/salcon.ogg";
150 worldmap_menu.reset(new Menu());
151 worldmap_menu->add_label(_("Pause"));
152 worldmap_menu->add_hl();
153 worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue"));
154 worldmap_menu->add_submenu(_("Options"), get_options_menu());
155 worldmap_menu->add_hl();
156 worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
158 // create a new squirrel table for the worldmap
159 using namespace Scripting;
161 sq_collectgarbage(global_vm);
162 sq_newtable(global_vm);
163 sq_pushroottable(global_vm);
164 if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
165 throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
167 sq_resetobject(&worldmap_table);
168 if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
169 throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
171 sq_addref(global_vm, &worldmap_table);
172 sq_pop(global_vm, 1);
174 sound_manager->preload("sounds/warp.wav");
176 // load worldmap objects
180 WorldMap::~WorldMap()
182 using namespace Scripting;
184 for(GameObjects::iterator i = game_objects.begin();
185 i != game_objects.end(); ++i) {
186 GameObject* object = *i;
187 try_unexpose(object);
191 for(SpawnPoints::iterator i = spawn_points.begin();
192 i != spawn_points.end(); ++i) {
196 for(ScriptList::iterator i = scripts.begin();
197 i != scripts.end(); ++i) {
198 HSQOBJECT& object = *i;
199 sq_release(global_vm, &object);
201 sq_release(global_vm, &worldmap_table);
203 sq_collectgarbage(global_vm);
210 WorldMap::add_object(GameObject* object)
212 TileMap* tilemap = dynamic_cast<TileMap*> (object);
213 if(tilemap != 0 && tilemap->is_solid()) {
214 solid_tilemaps.push_back(tilemap);
219 game_objects.push_back(object);
223 WorldMap::try_expose(GameObject* object)
225 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
226 if(interface != NULL) {
227 HSQUIRRELVM vm = Scripting::global_vm;
228 sq_pushobject(vm, worldmap_table);
229 interface->expose(vm, -1);
235 WorldMap::try_unexpose(GameObject* object)
237 ScriptInterface* interface = dynamic_cast<ScriptInterface*> (object);
238 if(interface != NULL) {
239 HSQUIRRELVM vm = Scripting::global_vm;
240 SQInteger oldtop = sq_gettop(vm);
241 sq_pushobject(vm, worldmap_table);
243 interface->unexpose(vm, -1);
244 } catch(std::exception& e) {
245 log_warning << "Couldn't unregister object: " << e.what() << std::endl;
247 sq_settop(vm, oldtop);
252 WorldMap::move_to_spawnpoint(const std::string& spawnpoint)
254 for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
256 if(sp->name == spawnpoint) {
258 tux->set_tile_pos(p);
259 tux->set_direction(sp->auto_dir);
263 log_warning << "Spawnpoint '" << spawnpoint << "' not found." << std::endl;
264 if (spawnpoint != "main") {
265 move_to_spawnpoint("main");
270 WorldMap::change(const std::string& filename, const std::string& force_spawnpoint)
272 main_loop->exit_screen();
273 main_loop->push_screen(new WorldMap(filename, force_spawnpoint));
277 WorldMap::load(const std::string& filename)
279 map_filename = filename;
280 levels_path = FileSystem::dirname(map_filename);
284 const lisp::Lisp* root = parser.parse(map_filename);
286 const lisp::Lisp* lisp = root->get_lisp("supertux-level");
288 throw std::runtime_error("file isn't a supertux-level file.");
290 lisp->get("name", name);
292 const lisp::Lisp* sector = lisp->get_lisp("sector");
294 throw std::runtime_error("No sector sepcified in worldmap file.");
296 lisp::ListIterator iter(sector);
298 if(iter.item() == "tilemap") {
300 TileMap::loading_worldmap = true;
302 add_object(new TileMap(*(iter.lisp())));
303 } else if(iter.item() == "background") {
304 add_object(new Background(*(iter.lisp())));
305 } else if(iter.item() == "music") {
306 iter.value()->get(music);
307 } else if(iter.item() == "init-script") {
308 iter.value()->get(init_script);
309 } else if(iter.item() == "worldmap-spawnpoint") {
310 SpawnPoint* sp = new SpawnPoint(iter.lisp());
311 spawn_points.push_back(sp);
312 } else if(iter.item() == "level") {
313 LevelTile* level = new LevelTile(levels_path, iter.lisp());
314 levels.push_back(level);
316 } else if(iter.item() == "special-tile") {
317 SpecialTile* special_tile = new SpecialTile(iter.lisp());
318 special_tiles.push_back(special_tile);
319 add_object(special_tile);
320 } else if(iter.item() == "sprite-change") {
321 SpriteChange* sprite_change = new SpriteChange(iter.lisp());
322 sprite_changes.push_back(sprite_change);
323 add_object(sprite_change);
324 } else if(iter.item() == "teleporter") {
325 Teleporter* teleporter = new Teleporter(iter.lisp());
326 teleporters.push_back(teleporter);
327 add_object(teleporter);
328 } else if(iter.item() == "ambient-light") {
329 std::vector<float> vColor;
330 sector->get_vector( "ambient-light", vColor );
331 if(vColor.size() < 3) {
332 log_warning << "(ambient-light) requires a color as argument" << std::endl;
334 ambient_light = Color( vColor );
336 } else if(iter.item() == "name") {
339 log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
342 if(solid_tilemaps.size() == 0)
343 throw std::runtime_error("No solid tilemap specified");
345 move_to_spawnpoint("main");
347 } catch(std::exception& e) {
348 std::stringstream msg;
349 msg << "Problem when parsing worldmap '" << map_filename << "': " <<
351 throw std::runtime_error(msg.str());
356 WorldMap::get_level_title(LevelTile& level)
358 /** get special_tile's title */
359 level.title = "<no title>";
363 const lisp::Lisp* root = parser.parse(levels_path + level.get_name());
365 const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
369 level_lisp->get("name", level.title);
370 } catch(std::exception& e) {
371 log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
376 void WorldMap::calculate_total_stats()
379 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
380 LevelTile* level = *i;
382 total_stats += level->statistics;
388 WorldMap::on_escape_press()
390 // Show or hide the menu
391 if(!Menu::current()) {
392 Menu::set_current(worldmap_menu.get());
393 tux->set_direction(D_NONE); // stop tux movement when menu is called
395 Menu::set_current(NULL);
400 WorldMap::get_next_tile(Vector pos, Direction direction)
422 WorldMap::path_ok(Direction direction, const Vector& old_pos, Vector* new_pos)
424 *new_pos = get_next_tile(old_pos, direction);
426 if (!(new_pos->x >= 0 && new_pos->x < get_width()
427 && new_pos->y >= 0 && new_pos->y < get_height()))
428 { // New position is outsite the tilemap
432 { // Check if the tile allows us to go to new_pos
433 int old_tile_data = tile_data_at(old_pos);
434 int new_tile_data = tile_data_at(*new_pos);
438 return (old_tile_data & Tile::WORLDMAP_WEST
439 && new_tile_data & Tile::WORLDMAP_EAST);
442 return (old_tile_data & Tile::WORLDMAP_EAST
443 && new_tile_data & Tile::WORLDMAP_WEST);
446 return (old_tile_data & Tile::WORLDMAP_NORTH
447 && new_tile_data & Tile::WORLDMAP_SOUTH);
450 return (old_tile_data & Tile::WORLDMAP_SOUTH
451 && new_tile_data & Tile::WORLDMAP_NORTH);
454 assert(!"path_ok() can't walk if direction is NONE");
461 WorldMap::finished_level(Level* gamelevel)
463 // TODO use Level* parameter here?
464 LevelTile* level = at_level();
466 bool old_level_state = level->solved;
467 level->solved = true;
468 level->sprite->set_action("solved");
470 // deal with statistics
471 level->statistics.merge(gamelevel->stats);
472 calculate_total_stats();
476 if (old_level_state != level->solved) {
477 // Try to detect the next direction to which we should walk
478 // FIXME: Mostly a hack
479 Direction dir = D_NONE;
481 int dirdata = available_directions_at(tux->get_tile_pos());
482 // first, test for crossroads
483 if (dirdata == Tile::WORLDMAP_CNSE ||
484 dirdata == Tile::WORLDMAP_CNSW ||
485 dirdata == Tile::WORLDMAP_CNEW ||
486 dirdata == Tile::WORLDMAP_CSEW ||
487 dirdata == Tile::WORLDMAP_CNSEW)
489 else if (dirdata & Tile::WORLDMAP_NORTH
490 && tux->back_direction != D_NORTH)
492 else if (dirdata & Tile::WORLDMAP_SOUTH
493 && tux->back_direction != D_SOUTH)
495 else if (dirdata & Tile::WORLDMAP_EAST
496 && tux->back_direction != D_EAST)
498 else if (dirdata & Tile::WORLDMAP_WEST
499 && tux->back_direction != D_WEST)
503 tux->set_direction(dir);
507 if (level->extro_script != "") {
509 std::istringstream in(level->extro_script);
510 run_script(in, "worldmap:extro_script");
511 } catch(std::exception& e) {
512 log_fatal << "Couldn't run level-extro-script: " << e.what() << std::endl;
518 WorldMap::update(float delta)
521 Menu* menu = Menu::current();
525 if(menu == worldmap_menu.get()) {
526 switch (worldmap_menu->check())
528 case MNID_RETURNWORLDMAP: // Return to game
529 Menu::set_current(0);
531 case MNID_QUITWORLDMAP: // Quit Worldmap
532 main_loop->exit_screen();
540 // update GameObjects
541 for(size_t i = 0; i < game_objects.size(); ++i) {
542 GameObject* object = game_objects[i];
543 object->update(delta);
546 // remove old GameObjects
547 for(GameObjects::iterator i = game_objects.begin();
548 i != game_objects.end(); ) {
549 GameObject* object = *i;
550 if(!object->is_valid()) {
551 try_unexpose(object);
553 i = game_objects.erase(i);
559 /* update solid_tilemaps list */
560 //FIXME: this could be more efficient
561 solid_tilemaps.clear();
562 for(std::vector<GameObject*>::iterator i = game_objects.begin();
563 i != game_objects.end(); ++i)
565 TileMap* tm = dynamic_cast<TileMap*>(*i);
567 if (tm->is_solid()) solid_tilemaps.push_back(tm);
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;
575 if (camera_offset.x < 0)
577 if (camera_offset.y < 0)
580 if (camera_offset.x > (int)get_width()*32 - SCREEN_WIDTH)
581 camera_offset.x = (int)get_width()*32 - SCREEN_WIDTH;
582 if (camera_offset.y > (int)get_height()*32 - SCREEN_HEIGHT)
583 camera_offset.y = (int)get_height()*32 - SCREEN_HEIGHT;
585 if (int(get_width()*32) < SCREEN_WIDTH)
586 camera_offset.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
587 if (int(get_height()*32) < SCREEN_HEIGHT)
588 camera_offset.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
591 bool enter_level = false;
592 if(main_controller->pressed(Controller::ACTION)
593 || main_controller->pressed(Controller::JUMP)
594 || main_controller->pressed(Controller::MENU_SELECT)) {
595 /* some people define UP and JUMP on the same key... */
596 if(!main_controller->pressed(Controller::UP))
599 if(main_controller->pressed(Controller::PAUSE_MENU))
602 // check for teleporters
603 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
604 if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
606 if (teleporter->worldmap != "") {
607 change(teleporter->worldmap, teleporter->spawnpoint);
609 // TODO: an animation, camera scrolling or a fading would be a nice touch
610 sound_manager->play("sounds/warp.wav");
611 tux->back_direction = D_NONE;
612 move_to_spawnpoint(teleporter->spawnpoint);
616 // check for auto-play levels
617 LevelTile* level = at_level();
618 if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
622 if (enter_level && !tux->is_moving())
624 /* Check level action */
625 LevelTile* level = at_level();
627 //Respawn if player on a tile with no level and nowhere to go.
628 int tile_data = tile_data_at(tux->get_tile_pos());
629 if(!( tile_data & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
630 log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
631 move_to_spawnpoint("main");
634 log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
638 if (level->pos == tux->get_tile_pos()) {
640 Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
641 level->pos.y*32 + 16 - camera_offset.y);
642 std::string levelfile = levels_path + level->get_name();
644 // update state and savegame
647 main_loop->push_screen(new GameSession(levelfile, &level->statistics),
648 new ShrinkFade(shrinkpos, 0.5));
650 } catch(std::exception& e) {
651 log_fatal << "Couldn't load level: " << e.what() << std::endl;
657 // tux->set_direction(input_direction);
663 WorldMap::tile_data_at(Vector p)
667 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
668 TileMap* tilemap = *i;
669 const Tile* tile = tilemap->get_tile((int)p.x, (int)p.y);
670 int dirdata = tile->getData();
678 WorldMap::available_directions_at(Vector p)
680 return tile_data_at(p) & Tile::WORLDMAP_DIR_MASK;
686 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
687 LevelTile* level = *i;
688 if (level->pos == tux->get_tile_pos())
696 WorldMap::at_special_tile()
698 for(SpecialTiles::iterator i = special_tiles.begin();
699 i != special_tiles.end(); ++i) {
700 SpecialTile* special_tile = *i;
701 if (special_tile->pos == tux->get_tile_pos())
709 WorldMap::at_sprite_change(const Vector& pos)
711 for(SpriteChanges::iterator i = sprite_changes.begin();
712 i != sprite_changes.end(); ++i) {
713 SpriteChange* sprite_change = *i;
714 if(sprite_change->pos == pos)
715 return sprite_change;
722 WorldMap::at_teleporter(const Vector& pos)
724 for(std::vector<Teleporter*>::iterator i = teleporters.begin(); i != teleporters.end(); ++i) {
725 Teleporter* teleporter = *i;
726 if(teleporter->pos == pos) return teleporter;
733 WorldMap::draw(DrawingContext& context)
735 if (int(get_width()*32) < SCREEN_WIDTH || int(get_height()*32) < SCREEN_HEIGHT)
736 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
737 Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
739 context.set_ambient_color( ambient_light );
740 context.push_transform();
741 context.set_translation(camera_offset);
743 for(GameObjects::iterator i = game_objects.begin();
744 i != game_objects.end(); ++i) {
745 GameObject* object = *i;
746 object->draw(context);
750 // FIXME: make this a runtime switch similar to draw_collrects/show_collrects?
751 // draw visual indication of possible walk directions
752 static int flipme = 0;
754 for (int x = 0; x < get_width(); x++) {
755 for (int y = 0; y < get_height(); y++) {
756 int data = tile_data_at(Vector(x,y));
760 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);
761 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);
762 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);
763 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);
764 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);
765 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);
770 draw_status(context);
771 context.pop_transform();
775 WorldMap::draw_status(DrawingContext& context)
777 context.push_transform();
778 context.set_translation(Vector(0, 0));
780 player_status->draw(context);
782 if (!tux->is_moving()) {
783 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
784 LevelTile* level = *i;
786 if (level->pos == tux->get_tile_pos()) {
787 if(level->title == "")
788 get_level_title(*level);
790 context.draw_text(white_text, level->title,
791 Vector(SCREEN_WIDTH/2,
792 SCREEN_HEIGHT - white_text->get_height() - 30),
793 ALIGN_CENTER, LAYER_FOREGROUND1);
795 // if level is solved, draw level picture behind stats
798 if (const Surface* picture = level->get_picture()) {
799 Vector pos = Vector(SCREEN_WIDTH - picture->get_width(), SCREEN_HEIGHT - picture->get_height());
800 context.push_transform();
801 context.set_alpha(0.5);
802 context.draw_surface(picture, pos, LAYER_FOREGROUND1-1);
803 context.pop_transform();
808 level->statistics.draw_worldmap_info(context);
813 for(SpecialTiles::iterator i = special_tiles.begin();
814 i != special_tiles.end(); ++i) {
815 SpecialTile* special_tile = *i;
817 if (special_tile->pos == tux->get_tile_pos()) {
818 /* Display an in-map message in the map, if any as been selected */
819 if(!special_tile->map_message.empty() && !special_tile->passive_message)
820 context.draw_text(gold_text, special_tile->map_message,
821 Vector(SCREEN_WIDTH/2,
822 SCREEN_HEIGHT - white_text->get_height() - 60),
823 ALIGN_CENTER, LAYER_FOREGROUND1);
828 // display teleporter messages
829 Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
830 if (teleporter && (teleporter->message != "")) {
831 Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 30);
832 context.draw_text(white_text, teleporter->message, pos, ALIGN_CENTER, LAYER_FOREGROUND1);
837 /* Display a passive message in the map, if needed */
838 if(passive_message_timer.started())
839 context.draw_text(gold_text, passive_message,
840 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
841 ALIGN_CENTER, LAYER_FOREGROUND1);
843 context.pop_transform();
849 sound_manager->play_music(music);
850 Menu::set_current(NULL);
855 // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
856 if (force_spawnpoint != "") {
857 move_to_spawnpoint(force_spawnpoint);
858 force_spawnpoint = "";
863 // register worldmap_table as worldmap in scripting
864 using namespace Scripting;
866 sq_pushroottable(global_vm);
867 sq_pushstring(global_vm, "worldmap", -1);
868 sq_pushobject(global_vm, worldmap_table);
869 if(SQ_FAILED(sq_createslot(global_vm, -3)))
870 throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
871 sq_pop(global_vm, 1);
873 if(init_script != "") {
874 std::istringstream in(init_script);
875 run_script(in, "WorldMap::init");
882 using namespace Scripting;
884 // save state of world and player
887 // remove worldmap_table from roottable
888 sq_pushroottable(global_vm);
889 sq_pushstring(global_vm, "worldmap", -1);
890 if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
891 throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
892 sq_pop(global_vm, 1);
896 WorldMap::save_state()
898 using namespace Scripting;
900 HSQUIRRELVM vm = global_vm;
901 int oldtop = sq_gettop(vm);
905 sq_pushroottable(vm);
906 sq_pushstring(vm, "state", -1);
907 if(SQ_FAILED(sq_get(vm, -2)))
908 throw Scripting::SquirrelError(vm, "Couldn't get state table");
910 // get or create worlds table
911 sq_pushstring(vm, "worlds", -1);
912 if(SQ_FAILED(sq_get(vm, -2))) {
913 sq_pushstring(vm, "worlds", -1);
915 if(SQ_FAILED(sq_createslot(vm, -3)))
916 throw Scripting::SquirrelError(vm, "Couldn't create state.worlds");
918 sq_pushstring(vm, "worlds", -1);
919 if(SQ_FAILED(sq_get(vm, -2)))
920 throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
923 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
924 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
927 // construct new table for this worldmap
928 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
932 sq_pushstring(vm, "tux", -1);
935 store_float(vm, "x", tux->get_tile_pos().x);
936 store_float(vm, "y", tux->get_tile_pos().y);
937 store_string(vm, "back", direction_to_string(tux->back_direction));
939 sq_createslot(vm, -3);
942 sq_pushstring(vm, "levels", -1);
945 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
946 LevelTile* level = *i;
948 sq_pushstring(vm, level->get_name().c_str(), -1);
951 store_bool(vm, "solved", level->solved);
952 level->statistics.serialize_to_squirrel(vm);
954 sq_createslot(vm, -3);
957 sq_createslot(vm, -3);
959 // overall statistics...
960 total_stats.serialize_to_squirrel(vm);
962 // push world into worlds table
963 sq_createslot(vm, -3);
964 } catch(std::exception& ) {
965 sq_settop(vm, oldtop);
968 sq_settop(vm, oldtop);
970 if(World::current() != NULL)
971 World::current()->save_state();
975 WorldMap::load_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");
990 sq_pushstring(vm, "worlds", -1);
991 if(SQ_FAILED(sq_get(vm, -2)))
992 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds");
994 // get table for our world
995 sq_pushstring(vm, map_filename.c_str(), map_filename.length());
996 if(SQ_FAILED(sq_get(vm, -2)))
997 throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
1000 sq_pushstring(vm, "tux", -1);
1001 if(SQ_FAILED(sq_get(vm, -2)))
1002 throw Scripting::SquirrelError(vm, "Couldn't get tux");
1005 p.x = read_float(vm, "x");
1006 p.y = read_float(vm, "y");
1007 std::string back_str = read_string(vm, "back");
1008 tux->back_direction = string_to_direction(back_str);
1009 tux->set_tile_pos(p);
1014 sq_pushstring(vm, "levels", -1);
1015 if(SQ_FAILED(sq_get(vm, -2)))
1016 throw Scripting::SquirrelError(vm, "Couldn't get levels");
1018 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1019 LevelTile* level = *i;
1020 sq_pushstring(vm, level->get_name().c_str(), -1);
1021 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
1022 level->solved = read_bool(vm, "solved");
1023 level->sprite->set_action(level->solved ? "solved" : "default");
1024 level->statistics.unserialize_from_squirrel(vm);
1029 // leave state table
1032 // load overall statistics
1033 total_stats.unserialize_from_squirrel(vm);
1035 } catch(std::exception& e) {
1036 log_debug << "Not loading worldmap state: " << e.what() << std::endl;
1038 sq_settop(vm, oldtop);
1044 WorldMap::level_count()
1046 return levels.size();
1050 WorldMap::solved_level_count()
1053 for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
1054 LevelTile* level = *i;
1064 WorldMap::run_script(std::istream& in, const std::string& sourcename)
1066 using namespace Scripting;
1068 // garbage collect thread list
1069 for(ScriptList::iterator i = scripts.begin();
1070 i != scripts.end(); ) {
1071 HSQOBJECT& object = *i;
1072 HSQUIRRELVM vm = object_to_vm(object);
1074 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
1075 sq_release(global_vm, &object);
1076 i = scripts.erase(i);
1083 HSQOBJECT object = create_thread(global_vm);
1084 scripts.push_back(object);
1086 HSQUIRRELVM vm = object_to_vm(object);
1088 // set worldmap_table as roottable for the thread
1089 sq_pushobject(vm, worldmap_table);
1090 sq_setroottable(vm);
1092 compile_and_run(vm, in, sourcename);
1098 WorldMap::get_width() const
1101 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1102 TileMap* solids = *i;
1103 if (solids->get_width() > width) width = solids->get_width();
1109 WorldMap::get_height() const
1112 for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
1113 TileMap* solids = *i;
1114 if (solids->get_height() > height) height = solids->get_height();
1119 } // namespace WorldMapNS