GroundMaps.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/GroundMaps.h>
00022 #include <landscapemap/HeightMapLoader.h>
00023 #include <landscapemap/LandscapeMaps.h>
00024 #include <landscapedef/LandscapeInclude.h>
00025 #include <landscapedef/LandscapeTex.h>
00026 #include <landscapedef/LandscapeDefinitions.h>
00027 #include <landscapedef/LandscapeDefinitionCache.h>
00028 #include <target/Target.h>
00029 #include <placement/PlacementTankPosition.h>
00030 #include <movement/TargetMovement.h>
00031 #include <common/Logger.h>
00032 #include <tankai/TankAIAdder.h>
00033 #include <lang/LangResource.h>
00034 #ifndef S3D_SERVER
00035 #include <land/VisibilityPatchGrid.h>
00036 #endif
00037 
00038 GroundMaps::GroundMaps(LandscapeDefinitionCache &defnCache) :
00039         defnCache_(defnCache), 
00040         arenaX_(-1),
00041         arenaY_(-1),
00042         arenaWidth_(-1), 
00043         arenaHeight_(-1)
00044 {
00045 }
00046 
00047 GroundMaps::~GroundMaps()
00048 {
00049 }
00050 
00051 void GroundMaps::generateMaps(
00052         ScorchedContext &context,
00053         ProgressCounter *counter)
00054 {
00055         arenaWidth_ = context.getLandscapeMaps().getDefinitions().getDefn()->getArenaWidth();
00056         arenaHeight_ = context.getLandscapeMaps().getDefinitions().getDefn()->getArenaHeight();
00057         arenaX_ = context.getLandscapeMaps().getDefinitions().getDefn()->getArenaX();
00058         arenaY_ = context.getLandscapeMaps().getDefinitions().getDefn()->getArenaY();
00059 
00060         generateHMap(context, counter);
00061 #ifndef S3D_SERVER
00062         if (!context.getServerMode())
00063         {
00064                 VisibilityPatchGrid::instance()->generate();
00065         }
00066 #endif
00067         generateObjects(context, counter);
00068 
00069         // Place the tanks after the objects and hmap
00070         // This can remove objects, and flatten the hmap
00071         PlacementTankPosition::flattenTankPositions(context);
00072 
00073         // Create movement after targets, so we can mark 
00074         // those targets that are in movement groups
00075         context.getTargetMovement().generate(context); 
00076         nmap_.create(getLandscapeWidth(), getLandscapeHeight());
00077 }
00078 
00079 int GroundMaps::getLandscapeWidth()
00080 {
00081         return map_.getMapWidth();
00082 }
00083 
00084 int GroundMaps::getLandscapeHeight()
00085 {
00086         return map_.getMapHeight();
00087 }
00088 
00089 int GroundMaps::getArenaWidth()
00090 {
00091         return arenaWidth_;
00092 }
00093 
00094 int GroundMaps::getArenaHeight()
00095 {
00096         return arenaHeight_;
00097 }
00098 
00099 int GroundMaps::getArenaX()
00100 {
00101         return arenaX_;
00102 }
00103 
00104 int GroundMaps::getArenaY()
00105 {
00106         return arenaY_;
00107 }
00108 
00109 void GroundMaps::generateHMap(
00110         ScorchedContext &context, 
00111         ProgressCounter *counter)
00112 {
00113         // Initialize the ground and surround maps
00114         map_.create(defnCache_.getDefn()->getLandscapeWidth(), 
00115                 defnCache_.getDefn()->getLandscapeHeight());
00116 
00117         // Generate the landscape
00118         bool levelSurround = false;
00119         if (!HeightMapLoader::generateTerrain(
00120                 defnCache_.getSeed(),
00121                 defnCache_.getDefn()->heightmap,
00122                 map_,
00123                 levelSurround,
00124                 counter))
00125         {
00126                 S3D::dialogExit("Landscape", "Failed to generate landscape");
00127         }
00128 }
00129 
00130 void GroundMaps::generateObject(RandomGenerator &generator, 
00131         LandscapeInclude &place,
00132         ScorchedContext &context,
00133         unsigned int &playerId,
00134         ProgressCounter *counter)
00135 {
00136         if (counter) counter->setNewOp(LANG_RESOURCE("POPULATING_LANDSCAPE", "Populating Landscape"));
00137 
00138         // Generate all the objects using the objects definitions
00139         for (unsigned int i=0; i<place.placements.size(); i++)
00140         {
00141                 PlacementType *type = place.placements[i];
00142                 type->createObjects(context, generator, playerId, counter);
00143         }
00144 }
00145 
00146 void GroundMaps::generateObjects(
00147         ScorchedContext &context,
00148         ProgressCounter *counter)
00149 {
00150         LandscapeTex *tex = defnCache_.getTex();
00151         LandscapeDefn *defn = defnCache_.getDefn();
00152 
00153         // Remove any existing shadows
00154         groups_.getShadows().clear();
00155 
00156         // Remove any existing groups
00157         groups_.clearGroups();
00158 
00159         // Add objects to the landscape (if any)
00160         // Do this now as it adds shadows to the mainmap
00161         unsigned int playerId = TargetID::MIN_TARGET_ID;
00162         {
00163                 // Do this for the definition file
00164                 std::vector<LandscapeInclude *>::iterator itor;
00165                 for (itor = defn->texDefn.includes.begin();
00166                         itor != defn->texDefn.includes.end();
00167                         itor++)
00168                 {
00169                         LandscapeInclude *place = (*itor);
00170                         RandomGenerator objectsGenerator;
00171                         objectsGenerator.seed(defnCache_.getSeed());
00172                         generateObject(objectsGenerator, *place, 
00173                                 context, playerId, counter);
00174                 }
00175         }
00176         {
00177                 // Do this for the texture file
00178                 std::vector<LandscapeInclude *>::iterator itor;
00179                 for (itor = tex->texDefn.includes.begin();
00180                         itor != tex->texDefn.includes.end();
00181                         itor++)
00182                 {
00183                         LandscapeInclude *place = (*itor);
00184                         RandomGenerator objectsGenerator;
00185                         objectsGenerator.seed(defnCache_.getSeed());
00186                         generateObject(objectsGenerator, *place, 
00187                                 context, playerId, counter);
00188                 }
00189         }
00190 
00191         // Deform the main map with respect to the objects
00192         {
00193                 std::list<PlacementShadowDefinition::Entry> &shadows = 
00194                         groups_.getShadows();
00195                 std::list<PlacementShadowDefinition::Entry>::iterator itor;
00196                 for (itor = shadows.begin();
00197                         itor != shadows.end();
00198                         itor++)
00199                 {
00200                         PlacementShadowDefinition::Entry &entry = (*itor);
00201                         entry.definition_->updateLandscapeHeight(
00202                                 context,
00203                                 entry.position_, entry.size_);
00204                 }
00205         }
00206 }
00207 
00208 fixed GroundMaps::getHeight(int w, int h)
00209 {
00210         return map_.getHeight(w, h);
00211 }
00212 
00213 fixed GroundMaps::getInterpHeight(fixed w, fixed h)
00214 {
00215         return map_.getInterpHeight(w, h);
00216 }
00217 
00218 FixedVector &GroundMaps::getNormal(int w, int h)
00219 {
00220         return map_.getNormal(w, h);
00221 }
00222 
00223 void GroundMaps::getInterpNormal(fixed w, fixed h, FixedVector &normal)
00224 {
00225         FixedVector result;
00226         map_.getInterpNormal(w, h, normal);
00227 }
00228 
00229 bool GroundMaps::getIntersect(Line &direction, Vector &intersect)
00230 {
00231         return map_.getIntersect(direction, intersect);
00232 }
00233 

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