From: Ingo Ruhnke Date: Sat, 17 Apr 2004 00:49:54 +0000 (+0000) Subject: - added load game support for the worldmap X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e7978bb4f7e4ca74b29dcad75c72303e78fc323b;p=supertux.git - added load game support for the worldmap SVN-Revision: 537 --- diff --git a/src/gameloop.cpp b/src/gameloop.cpp index aa972cd4b..cb9137399 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -519,10 +519,11 @@ GameSession::run() st_pause_ticks_stop(); break; case 3: - update_load_save_game_menu(save_game_menu, false); + // FIXME: + //update_load_save_game_menu(save_game_menu); break; case 4: - update_load_save_game_menu(load_game_menu, true); + update_load_save_game_menu(load_game_menu); break; case 7: st_pause_ticks_stop(); @@ -737,33 +738,16 @@ GameSession::drawresultscreen(void) std::string slotinfo(int slot) { - FILE* fi; + char tmp[1024]; char slotfile[1024]; - char tmp[200]; - char str[5]; - int slot_level; - sprintf(slotfile,"%s/slot%d.save",st_save_dir,slot); + sprintf(slotfile,"%s/slot%d.stsg",st_save_dir,slot); - fi = fopen(slotfile, "rb"); - - sprintf(tmp,"Slot %d - ",slot); - - if (fi == NULL) - { - strcat(tmp,"Free"); - } + if (access(slotfile, F_OK) == 0) + sprintf(tmp,"Slot %d - Savegame",slot); else - { - fgets(str, 100, fi); - str[strlen(str)-1] = '\0'; - strcat(tmp, str); - strcat(tmp, " / Level:"); - fread(&slot_level,sizeof(int),1,fi); - sprintf(str,"%d",slot_level); - strcat(tmp,str); - fclose(fi); - } + sprintf(tmp,"Slot %d - Free",slot); return tmp; } + diff --git a/src/setup.cpp b/src/setup.cpp index 62781e332..4cfa59f3b 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -11,6 +11,7 @@ */ #include +#include #include #include #include @@ -440,18 +441,14 @@ void st_menu(void) highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0); } -void update_load_save_game_menu(Menu* pmenu, int load) +void update_load_save_game_menu(Menu* pmenu) { for(int i = 2; i < 7; ++i) { // FIXME: Insert a real savegame struct/class here instead of // doing string vodoo - std::string tmp = slotinfo(i-1); - - if(load && tmp.length() == strlen("Slot X - Free")) - pmenu->item[i].kind = MN_ACTION; - else - pmenu->item[i].kind = MN_ACTION; + std::string tmp = slotinfo(i - 1); + pmenu->item[i].kind = MN_ACTION; pmenu->item[i].change_text(tmp.c_str()); } } @@ -462,21 +459,18 @@ bool process_load_game_menu() if(slot != -1) { - // FIXME: Insert a real savegame struct/class here instead of - // doing string vodoo - std::string tmp = slotinfo(slot-1); + WorldMapNS::WorldMap worldmap; + + char slotfile[1024]; + snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot-1); + + worldmap.loadgame(slotfile); + + worldmap.display(); + + show_menu = true; + Menu::set_current(main_menu); - if (tmp.length() == strlen("Slot X - Free")) - { // Slot is free, so start a new game - worldmap_run(); - - show_menu = true; - Menu::set_current(main_menu); - } - else - { - puts("Warning: Loading games isn't supported at the moment"); - } st_pause_ticks_stop(); return true; } diff --git a/src/setup.h b/src/setup.h index 0f32be63e..1c0957934 100644 --- a/src/setup.h +++ b/src/setup.h @@ -39,7 +39,7 @@ void process_options_menu(void); /** Return true if the gameloop() was entered, false otherwise */ bool process_load_game_menu(); -void update_load_save_game_menu(Menu* pmenu, int load); +void update_load_save_game_menu(Menu* pmenu); void parseargs(int argc, char * argv[]); #endif /*SUPERTUX_SETUP_H*/ diff --git a/src/supertux.cpp b/src/supertux.cpp index 7f0e624cb..028d48227 100644 --- a/src/supertux.cpp +++ b/src/supertux.cpp @@ -41,7 +41,8 @@ int main(int argc, char * argv[]) if (launch_worldmap_mode) { - worldmap_run(); + WorldMapNS::WorldMap worldmap; + worldmap.display(); } else if (level_startup_file) { diff --git a/src/title.cpp b/src/title.cpp index 01dacb0b6..a7cc2f2d4 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -408,7 +408,7 @@ bool title(void) #endif case 0: // Start Game, ie. goto the slots menu - update_load_save_game_menu(load_game_menu, true); + update_load_save_game_menu(load_game_menu); break; case 1: // Contrib Menu diff --git a/src/worldmap.cpp b/src/worldmap.cpp index f6098b5ec..4f9934fd0 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -450,7 +450,8 @@ WorldMap::update() play_music(song, 1); show_menu = 0; menu_reset(); - savegame(std::string(st_save_dir) + "/slot1.stsg"); + if (!savegame_file.empty()) + savegame(savegame_file); return; } } @@ -591,14 +592,23 @@ WorldMap::display() void WorldMap::savegame(const std::string& filename) { + std::cout << "savegame: " << filename << std::endl; std::ofstream out(filename.c_str()); + int nb_solved_levels = 0; + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (i->solved) + ++nb_solved_levels; + } + out << "(supertux-savegame\n" << " (version 1)\n" + << " (title \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n" << " (lives " << player_status.lives << ")\n" << " (score " << player_status.score << ")\n" << " (distros " << player_status.distros << ")\n" - << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n" + << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << "))\n" << " (levels\n"; for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) @@ -614,13 +624,60 @@ WorldMap::savegame(const std::string& filename) << " )\n\n;; EOF ;;" << std::endl; } -} // namespace WorldMapNS - -void worldmap_run() +void +WorldMap::loadgame(const std::string& filename) { - WorldMapNS::WorldMap worldmap; + std::cout << "loadgame: " << filename << std::endl; + savegame_file = filename; + + if (access(filename.c_str(), F_OK) == 0) + { + lisp_object_t* cur = lisp_read_from_file(filename); + + if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0) + return; + + cur = lisp_cdr(cur); + LispReader reader(cur); - worldmap.display(); + reader.read_int("lives", &player_status.lives); + reader.read_int("score", &player_status.score); + reader.read_int("distros", &player_status.distros); + + lisp_object_t* tux_cur = 0; + if (reader.read_lisp("tux", &tux_cur)) + { + Point p; + LispReader tux_reader(tux_cur); + tux_reader.read_int("x", &p.x); + tux_reader.read_int("y", &p.y); + + tux->set_tile_pos(p); + } + + lisp_object_t* level_cur = 0; + if (reader.read_lisp("levels", &level_cur)) + { + while(level_cur) + { + std::string name; + bool solved = false; + LispReader level_reader(level_cur); + level_reader.read_string("name", &name); + level_reader.read_bool("solved", &solved); + + for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) + { + if (name == i->name) + i->solved = solved; + } + + level_cur = lisp_cdr(level_cur); + } + } + } } +} // namespace WorldMapNS + /* EOF */ diff --git a/src/worldmap.h b/src/worldmap.h index a96c4ff27..b23923e89 100644 --- a/src/worldmap.h +++ b/src/worldmap.h @@ -144,6 +144,7 @@ private: bool enter_level; Point offset; + std::string savegame_file; void draw_status(); public: @@ -171,12 +172,11 @@ public: bool path_ok(Direction direction, Point pos, Point* new_pos); void savegame(const std::string& filename); + void loadgame(const std::string& filename); }; } // namespace WorldMapNS -void worldmap_run(); - #endif /* Local Variables: */