TargetGroupsGroupEntry.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/TargetGroupsGroupEntry.h>
00022 #include <landscapemap/HeightMap.h>
00023 #include <common/Defines.h>
00024 #include <common/Vector.h>
00025 
00026 TargetGroupsGroupEntry::TargetGroupsGroupEntry(
00027         const char *name, HeightMap &map) :
00028         TargetGroupsSetEntry(name),
00029         distance_(0)
00030 {
00031         mapWidthMult_ = 4;
00032         mapHeightMult_ = 4;
00033         mapWidth_ = map.getMapWidth() / mapWidthMult_;
00034         mapHeight_ = map.getMapHeight() / mapHeightMult_;
00035 
00036         distance_ = new float[mapWidth_ * mapHeight_];
00037         for (int a=0; a<mapWidth_; a++)
00038         {
00039                 for (int b=0; b<mapHeight_; b++)
00040                 {
00041                         distance_[a + b * mapWidth_] = 255.0f;
00042                 }
00043         }
00044 }
00045 
00046 TargetGroupsGroupEntry::~TargetGroupsGroupEntry()
00047 {
00048         delete [] distance_;
00049 }
00050 
00051 float TargetGroupsGroupEntry::getDistance(int x, int y)
00052 {
00053         x /= mapWidthMult_;
00054         y /= mapHeightMult_;
00055 
00056         if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
00057         {
00058                 return distance_[x + y * mapWidth_];
00059         }
00060         return 255.0f;
00061 }
00062 
00063 void TargetGroupsGroupEntry::addObject(TargetGroup *object, bool thin)
00064 {
00065         TargetGroupsSetEntry::addObject(object, thin);
00066 
00067         static unsigned int objectCount = 0;
00068         if (thin && objectCount++ % 3 != 0) return;
00069 
00070         int x = object->getPosition()[0].asInt();
00071         int y = object->getPosition()[1].asInt();
00072 
00073         x /= mapWidthMult_;
00074         y /= mapHeightMult_;
00075 
00076         if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
00077         {
00078                 Vector posA(x, y, 0);
00079                 for (int a=0; a<mapWidth_; a++)
00080                 {
00081                         for (int b=0; b<mapHeight_; b++)
00082                         {
00083                                 Vector posB(a, b, 0);
00084                                 posB -= posA;
00085                                 float d = posB.Magnitude();
00086                                 distance_[a + b * mapWidth_] = MIN(distance_[a + b * mapWidth_], d);
00087                         }
00088                 }
00089         }
00090 }
00091 
00092 bool TargetGroupsGroupEntry::removeObject(TargetGroup *object)
00093 {
00094         int x = object->getPosition()[0].asInt();
00095         int y = object->getPosition()[1].asInt();
00096 
00097         x /= mapWidthMult_;
00098         y /= mapHeightMult_;
00099         if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
00100         {
00101                 distance_[x + y * mapWidth_] += 2.0f;
00102         }
00103 
00104         return TargetGroupsSetEntry::removeObject(object);
00105 }

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