Goodbye gettext, Welcome TinyGetText
[supertux.git] / src / level.cpp
index dbd3b30..f317b22 100644 (file)
@@ -18,6 +18,8 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
 
+#include <config.h>
+
 #include <map>
 #include <cstdlib>
 #include <cstdio>
 #include <fstream>
 #include <stdexcept>
 
-#include "globals.h"
-#include "setup.h"
+#include "app/globals.h"
+#include "app/setup.h"
 #include "camera.h"
-#include "screen/screen.h"
+#include "video/screen.h"
 #include "level.h"
-#include "physic.h"
+#include "math/physic.h"
 #include "scene.h"
 #include "sector.h"
 #include "tile.h"
-#include "lispreader.h"
+#include "utils/lispreader.h"
 #include "resources.h"
 #include "gameobjs.h"
-#include "lispwriter.h"
+#include "utils/lispwriter.h"
+#include "tilemap.h"
 
 using namespace std;
 
-LevelSubset::LevelSubset()
-    : image(0), levels(0)
-{
-}
-
-LevelSubset::~LevelSubset()
-{
-  delete image;
-}
-
-void LevelSubset::create(const std::string& subset_name)
-{
-  Level new_lev;
-  LevelSubset new_subset;
-  new_subset.name = subset_name;
-  new_subset.title = "Unknown Title";
-  new_subset.description = "No description so far.";
-  new_subset.save();
-  //new_lev.save(subset_name, 1, 0);
-}
-
-void LevelSubset::parse (lisp_object_t* cursor)
-{
-  while(!lisp_nil_p(cursor))
-    {
-      lisp_object_t* cur = lisp_car(cursor);
-      char *s;
-
-      if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
-        {
-          printf("Not good");
-        }
-      else
-        {
-          if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0)
-            {
-              if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
-                {
-                  title = s;
-                }
-            }
-          else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0)
-            {
-              if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL)
-                {
-                  description = s;
-                }
-            }
-        }
-      cursor = lisp_cdr (cursor);
-    }
-}
-
-void LevelSubset::load(const char* subset)
+Level::Level()
+  : name("noname"), author("mr. x"), timelimit(500),
+    end_sequence_type(NONE_ENDSEQ_ANIM) 
 {
-  FILE* fi;
-  char filename[1024];
-  char str[1024];
-  int i;
-  lisp_object_t* root_obj = 0;
-
-  name = subset;
-
-  snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset);
-  if(!faccessible(filename))
-    snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset);
-  if(faccessible(filename))
-    {
-      fi = fopen(filename, "r");
-      if (fi == NULL)
-        {
-          perror(filename);
-        }
-      lisp_stream_t stream;
-      lisp_stream_init_file (&stream, fi);
-      root_obj = lisp_read (&stream);
-
-      if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
-        {
-          printf("World: Parse Error in file %s", filename);
-        }
-
-      lisp_object_t* cur = lisp_car(root_obj);
-
-      if (!lisp_symbol_p (cur))
-        {
-          printf("World: Read error in %s",filename);
-        }
-
-      if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0)
-        {
-          parse(lisp_cdr(root_obj));
-
-        }
-
-      lisp_free(root_obj);
-      fclose(fi);
-
-      snprintf(str, 1024, "%s.png", filename);
-      if(faccessible(str))
-        {
-          delete image;
-          image = new Surface(str,IGNORE_ALPHA);
-        }
-      else
-        {
-          snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str());
-          delete image;
-          image = new Surface(filename,IGNORE_ALPHA);
-        }
-    }
-
-  for(i=1; i != -1; ++i)
-    {
-      /* Get the number of levels in this subset */
-      snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i);
-      if(!faccessible(filename))
-        {
-          snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i);
-          if(!faccessible(filename))
-            break;
-        }
-    }
-  levels = --i;
 }
 
 void
-LevelSubset::save()
-{
-  FILE* fi;
-  string filename;
-
-  /* Save data file: */
-  filename = "/levels/" + name + "/";
-
-  fcreatedir(filename.c_str());
-  filename = string(st_dir) + "/levels/" + name + "/info";
-  if(!fwriteable(filename.c_str()))
-    filename = datadir + "/levels/" + name + "/info";
-  if(fwriteable(filename.c_str()))
-    {
-      fi = fopen(filename.c_str(), "w");
-      if (fi == NULL)
-        {
-          perror(filename.c_str());
-        }
-
-      /* Write header: */
-      fprintf(fi,";SuperTux-Level-Subset\n");
-      fprintf(fi,"(supertux-level-subset\n");
-
-      /* Save title info: */
-      fprintf(fi,"  (title \"%s\")\n", title.c_str());
-
-      /* Save the description: */
-      fprintf(fi,"  (description \"%s\")\n", description.c_str());
-
-      fprintf( fi,")");
-      fclose(fi);
-    }
-}
-
-std::string
-LevelSubset::get_level_filename(unsigned int num)
-{
-  char filename[1024];
-                                                                                
-  // Load data file:
-  snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir,
-      name.c_str(), num);
-  if(!faccessible(filename))
-    snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(),
-        name.c_str(), num);
-
-  return std::string(filename);
-}
-
-//---------------------------------------------------------------------------
-
-Level::Level()
-  : name("noname"), author("mr. x"), time_left(500)
+Level::create(const std::string& filename)
 {
+  Level level;
+  const size_t width = 25;
+  const size_t height = 19;
+  level.add_sector(Sector::create("main", width, height));
+  level.save(filename);
 }
 
 void
 Level::load(const std::string& filename)
 {
-  LispReader* level = LispReader::load(filename, "supertux-level");
+  std::string filepath;
+  filepath = st_dir + "/levels/" + filename;
+  if (access(filepath.c_str(), R_OK) != 0)
+  {
+    filepath = datadir + "/levels/" + filename;
+    if (access(filepath.c_str(), R_OK) != 0)
+    {
+      std::cerr << "Error: Level: couldn't find level: " << filename << std::endl;
+      return;
+    }
+  }
+  
+  LispReader* level = LispReader::load(filepath, "supertux-level");
 
   int version = 1;
   level->read_int("version", version);
   if(version == 1) {
     load_old_format(*level);
+    delete level;
     return;
   }
 
@@ -244,16 +92,28 @@ Level::load(const std::string& filename)
     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
     LispReader reader(lisp_cdr(lisp_car(cur)));
 
-    if(token == "name") {
+    if(token == "version") {
+      if(lisp_integer(data) > 2) {
+        std::cerr << "Warning: level format newer than application.\n";
+      }
+    } else if(token == "name") {
       name = lisp_string(data);
     } else if(token == "author") {
       author = lisp_string(data);
     } else if(token == "time") {
-      time_left = lisp_integer(data);
+      timelimit = lisp_integer(data);
     } else if(token == "sector") {
       Sector* sector = new Sector;
       sector->parse(reader);
       add_sector(sector);
+    } else if(token == "end-sequence-animation") {
+      std::string endsequencename = lisp_string(data);
+      if(endsequencename == "fireworks") {
+        end_sequence_type = FIREWORKS_ENDSEQ_ANIM;
+      } else {
+        std::cout << "Unknown endsequence type: '" << endsequencename <<
+          "'.\n";
+      }
     } else {
       std::cerr << "Unknown token '" << token << "' in level file.\n";
       continue;
@@ -266,9 +126,9 @@ Level::load(const std::string& filename)
 void
 Level::load_old_format(LispReader& reader)
 {
-  reader.read_string("name", name);
+  reader.read_string("name", name, true);
   reader.read_string("author", author);
-  reader.read_int("time", time_left);
+  reader.read_int("time", timelimit);
 
   Sector* sector = new Sector;
   sector->parse_old_format(reader);
@@ -278,40 +138,36 @@ Level::load_old_format(LispReader& reader)
 void
 Level::save(const std::string& filename)
 {
-#if 0
-  LispReader* level = LispReader::load(filename, "supertux-level");
+ std::string filepath = "levels/" + filename;
+ int last_slash = filepath.find_last_of('/');
+ FileSystem::fcreatedir(filepath.substr(0,last_slash).c_str());
+ filepath = st_dir + "/" + filepath;
+ ofstream file(filepath.c_str(), ios::out);
+ LispWriter* writer = new LispWriter(file);
 
-  int version = 1;
-  level->read_int("version", version);
-  if(version == 1) {
-    load_old_format(*level);
-    return;
-  }
+ writer->write_comment("Level made using SuperTux's built-in Level Editor");
 
-  for(lisp_object_t* cur = level->get_lisp(); !lisp_nil_p(cur);
-      cur = lisp_cdr(cur)) {
-    std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
-    lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
-    LispReader reader(lisp_cdr(lisp_car(cur)));
+ writer->start_list("supertux-level");
 
-    if(token == "name") {
-      name = lisp_string(data);
-    } else if(token == "author") {
-      author = lisp_string(data);
-    } else if(token == "time") {
-      time_left = lisp_integer(data);
-    } else if(token == "sector") {
-      Sector* sector = new Sector;
-      sector->parse(reader);
-      add_sector(sector);
-    } else {
-      std::cerr << "Unknown token '" << token << "' in level file.\n";
-      continue;
-    }
-  }
-  
-  delete level;
-#endif
+ int version = 2;
+ writer->write_int("version", version);
+
+ writer->write_string("name", name);
+ writer->write_string("author", author);
+ writer->write_int("time", timelimit);
+ writer->write_string("end-sequence-animation",
+     end_sequence_type == FIREWORKS_ENDSEQ_ANIM ? "fireworks" : "none");
+
+ for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
+   writer->start_list("sector");
+   i->second->write(*writer);
+   writer->end_list("sector");
+ }
+
+ writer->end_list("supertux-level");
+
+ delete writer;
+ file.close();
 }
 
 Level::~Level()
@@ -321,6 +177,13 @@ Level::~Level()
 }
 
 void
+Level::do_vertical_flip()
+{
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+    i->second->do_vertical_flip();
+}
+
+void
 Level::add_sector(Sector* sector)
 {
   sectors.insert(std::make_pair(sector->get_name(), sector));       
@@ -336,3 +199,75 @@ Level::get_sector(const std::string& name)
   return i->second;
 }
 
+Sector*
+Level::get_next_sector(const Sector* sector)
+{
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+    {
+    if(i->second == sector)
+      {
+      i++;
+      if(i == sectors.end())
+        return NULL;
+      return i->second;
+      }
+    }
+  std::cerr << "Warning: Sector not found on level\n";
+  return NULL;
+}
+
+Sector*
+Level::get_previous_sector(const Sector* sector)
+{
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+    {
+    if(i->second == sector)
+      {
+      if(i == sectors.begin())
+        return NULL;
+      i--;
+      return i->second;
+      }
+    }
+  std::cerr << "Warning: Sector not found on level\n";
+  return NULL;
+}
+
+int
+Level::get_total_sectors()
+{
+return sectors.size();
+}
+
+int
+Level::get_total_badguys()
+{
+  int total_badguys = 0;
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+    total_badguys += i->second->get_total_badguys();
+  return total_badguys;
+}
+
+int
+Level::get_total_coins()
+{
+  int total_coins = 0;
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
+    TileMap* solids = i->second->solids;
+    if(!solids) {
+      std::cerr << "Sector '" << i->first << "' contains no solids!?!\n";
+      continue;
+    }
+    for(size_t x = 0; x < solids->get_width(); ++x)
+      for(size_t y = 0; y < solids->get_height(); ++y) {
+        const Tile* tile = solids->get_tile(x, y);
+        if(tile == 0) {
+          std::cerr << "Invalid tile in sector '" << i->first << "'.\n";
+          continue;
+        }
+        if(tile->attributes & Tile::COIN)
+          total_coins++;
+      }
+  }
+  return total_coins;
+}