- added load game support for the worldmap
authorIngo Ruhnke <grumbel@gmx.de>
Sat, 17 Apr 2004 00:49:54 +0000 (00:49 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Sat, 17 Apr 2004 00:49:54 +0000 (00:49 +0000)
SVN-Revision: 537

src/gameloop.cpp
src/setup.cpp
src/setup.h
src/supertux.cpp
src/title.cpp
src/worldmap.cpp
src/worldmap.h

index aa972cd..cb91373 100644 (file)
@@ -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;
 }
 
+
index 62781e3..4cfa59f 100644 (file)
@@ -11,6 +11,7 @@
 */
 
 #include <assert.h>
+#include <stdio.h>
 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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;
     }
index 0f32be6..1c09579 100644 (file)
@@ -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*/
index 7f0e624..028d482 100644 (file)
@@ -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)
     {
index 01dacb0..a7cc2f2 100644 (file)
@@ -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
index f6098b5..4f9934f 100644 (file)
@@ -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 */
index a96c4ff..b23923e 100644 (file)
@@ -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: */