more fixes
[supertux.git] / src / level.cpp
index 4aa7042..5ad193c 100644 (file)
@@ -39,6 +39,7 @@
 #include "resources.h"
 #include "gameobjs.h"
 #include "utils/lispwriter.h"
+#include "tilemap.h"
 
 using namespace std;
 
@@ -49,9 +50,31 @@ Level::Level()
 }
 
 void
+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);
@@ -101,7 +124,11 @@ Level::load_old_format(LispReader& reader)
 void
 Level::save(const std::string& filename)
 {
- ofstream file(filename.c_str(), ios::out);
+ 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);
 
  writer->write_comment("Level made using SuperTux's built-in Level Editor");
@@ -157,3 +184,63 @@ 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 it = sectors.begin(); it != sectors.end(); ++it)
+    for(int x = 0; static_cast<unsigned int>(x) < it->second->solids->get_width(); x++)
+      for(int y = 0; static_cast<unsigned int>(y) < it->second->solids->get_height(); y++)
+        if(it->second->solids->get_tile(x,y)->attributes & Tile::COIN)
+          total_coins++;
+  return total_coins;
+}