RoofMaps.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <landscapemap/RoofMaps.h>
00022 #include <landscapemap/HeightMapLoader.h>
00023 #include <common/ProgressCounter.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <landscapedef/LandscapeDefinitionCache.h>
00026 #include <landscapedef/LandscapeDefn.h>
00027 
00028 RoofMaps::RoofMaps(LandscapeDefinitionCache &defnCache) :
00029         defnCache_(defnCache)
00030 {
00031 }
00032 
00033 RoofMaps::~RoofMaps()
00034 {
00035 }
00036 
00037 void RoofMaps::generateMaps(
00038         ScorchedContext &context,
00039         ProgressCounter *counter)
00040 {
00041         generateRMap(context, counter);
00042 }
00043 
00044 fixed RoofMaps::getRoofHeight(int x, int y)
00045 {
00046         if (defnCache_.getDefn()->roof->getType() != LandscapeDefnType::eRoofCavern)
00047         {
00048                 return fixed::MAX_FIXED;
00049         }
00050 
00051         // Factors should be caclculated from hmap height/width
00052         int const xFactor = defnCache_.getDefn()->getLandscapeWidth() / rmap_.getMapWidth();
00053         int const yFactor = defnCache_.getDefn()->getLandscapeHeight() / rmap_.getMapHeight();
00054 
00055         x /= xFactor;
00056         y /= yFactor;
00057         if (x < 0 || y < 0)
00058         {
00059                 return rmap_.getHeight(0, 0);
00060         }
00061         else if (x > rmap_.getMapWidth() || y > rmap_.getMapHeight())
00062         {
00063                 return rmap_.getHeight(
00064                         rmap_.getMapWidth(), rmap_.getMapHeight());
00065         }
00066 
00067         return rmap_.getHeight(x, y);
00068 }
00069 
00070 fixed RoofMaps::getInterpRoofHeight(fixed x, fixed y)
00071 {
00072         if (defnCache_.getDefn()->roof->getType() != LandscapeDefnType::eRoofCavern)
00073         {
00074                 return fixed::MAX_FIXED;
00075         }
00076 
00077         // Factors should be caclculated from hmap height/width
00078         int const xFactor = defnCache_.getDefn()->getLandscapeWidth() / rmap_.getMapWidth();
00079         int const yFactor = defnCache_.getDefn()->getLandscapeHeight() / rmap_.getMapHeight();
00080 
00081         x /= xFactor;
00082         y /= yFactor;
00083         if (x < 0 || y < 0 || x > rmap_.getMapWidth() || y > rmap_.getMapHeight())
00084         {
00085                 return fixed::MAX_FIXED;
00086         }
00087         return rmap_.getInterpHeight(x, y);
00088 }
00089 
00090 void RoofMaps::generateRMap(
00091         ScorchedContext &context,
00092         ProgressCounter *counter)
00093 {
00094         // calculate roof size and set it
00095         int mapWidth = defnCache_.getDefn()->getLandscapeWidth() / 4;
00096         int mapHeight = defnCache_.getDefn()->getLandscapeHeight() / 4;
00097         rmap_.create(mapWidth, mapHeight);
00098 
00099         // Generate the roof
00100         if (defnCache_.getDefn()->roof->getType() == LandscapeDefnType::eRoofCavern)
00101         {
00102                 LandscapeDefnRoofCavern *cavern = 
00103                         (LandscapeDefnRoofCavern *) defnCache_.getDefn()->roof;
00104 
00105                 bool smooth = false;
00106                 if (!HeightMapLoader::generateTerrain(
00107                         defnCache_.getSeed() + 1,
00108                         cavern->heightmap,
00109                         rmap_,
00110                         smooth,
00111                         counter))
00112                 {
00113                         S3D::dialogExit("Landscape", "Failed to generate roof");
00114                 }
00115                 
00116                 for (int j=0; j<=rmap_.getMapHeight(); j++)
00117                 {
00118                         for (int i=0; i<=rmap_.getMapWidth(); i++)
00119                         {
00120                                 fixed height = rmap_.getHeight(i, j);
00121                                 height = fixed(cavern->height) - height;
00122                                 rmap_.setHeight(i, j, height);
00123                                 rmap_.getNormal(i, j)[2] = -rmap_.getNormal(i, j)[2];
00124                         }
00125                 }
00126         }
00127 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3