00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }