git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / landscapemap / TargetGroupsGroupEntry.cpp
blobf1a71d91698042220e6cf2089962cc2a0accc128
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <landscapemap/TargetGroupsGroupEntry.h>
22 #include <landscapemap/HeightMap.h>
23 #include <common/Defines.h>
24 #include <common/Vector.h>
26 TargetGroupsGroupEntry::TargetGroupsGroupEntry(
27 const char *name, HeightMap &map) :
28 TargetGroupsSetEntry(name),
29 distance_(0)
31 mapWidthMult_ = 4;
32 mapHeightMult_ = 4;
33 mapWidth_ = map.getMapWidth() / mapWidthMult_;
34 mapHeight_ = map.getMapHeight() / mapHeightMult_;
36 distance_ = new float[mapWidth_ * mapHeight_];
37 for (int a=0; a<mapWidth_; a++)
39 for (int b=0; b<mapHeight_; b++)
41 distance_[a + b * mapWidth_] = 255.0f;
46 TargetGroupsGroupEntry::~TargetGroupsGroupEntry()
48 delete [] distance_;
51 float TargetGroupsGroupEntry::getDistance(int x, int y)
53 x /= mapWidthMult_;
54 y /= mapHeightMult_;
56 if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
58 return distance_[x + y * mapWidth_];
60 return 255.0f;
63 void TargetGroupsGroupEntry::addObject(TargetGroup *object, bool thin)
65 TargetGroupsSetEntry::addObject(object, thin);
67 static unsigned int objectCount = 0;
68 if (thin && objectCount++ % 3 != 0) return;
70 int x = object->getPosition()[0].asInt();
71 int y = object->getPosition()[1].asInt();
73 x /= mapWidthMult_;
74 y /= mapHeightMult_;
76 if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
78 Vector posA(x, y, 0);
79 for (int a=0; a<mapWidth_; a++)
81 for (int b=0; b<mapHeight_; b++)
83 Vector posB(a, b, 0);
84 posB -= posA;
85 float d = posB.Magnitude();
86 distance_[a + b * mapWidth_] = MIN(distance_[a + b * mapWidth_], d);
92 bool TargetGroupsGroupEntry::removeObject(TargetGroup *object)
94 int x = object->getPosition()[0].asInt();
95 int y = object->getPosition()[1].asInt();
97 x /= mapWidthMult_;
98 y /= mapHeightMult_;
99 if (x >=0 && x < mapWidth_ && y >=0 && y < mapHeight_)
101 distance_[x + y * mapWidth_] += 2.0f;
104 return TargetGroupsSetEntry::removeObject(object);