TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ObjectPosSelector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _OBJECT_POS_SELECTOR_H
20 #define _OBJECT_POS_SELECTOR_H
21 
22 #include<Common.h>
23 
24 #include<map>
25 
27 
29 {
30  return uptype==USED_POS_PLUS ? USED_POS_MINUS : USED_POS_PLUS;
31 }
32 
34 {
35  struct UsedPos
36  {
37  UsedPos(float sign_, float size_, float dist_) : sign(sign_), size(size_), dist(dist_) { }
38 
39  float sign;
40 
41  float size; // size of point
42  float dist; // dist to central point (including central point size)
43  };
44 
45  typedef std::multimap<float, UsedPos> UsedPosList; // abs(angle)->Node
46 
47  ObjectPosSelector(float x, float y, float size, float dist);
48 
49  void AddUsedPos(float size, float angle, float dist);
50  void InitializeAngle();
51 
52  bool FirstAngle(float& angle);
53  bool NextAngle(float& angle);
54  bool NextUsedAngle(float& angle);
55 
56  bool NextPosibleAngle(float& angle);
57 
58  bool CheckAngle(UsedPosList::value_type const& nextUsedPos, float sign, float angle ) const
59  {
60  float angle_step2 = GetAngle(nextUsedPos.second);
61 
62  float next_angle = nextUsedPos.first;
63  if (nextUsedPos.second.sign * sign < 0) // last node from diff. list (-pi+alpha)
64  next_angle = 2 * float(M_PI) - next_angle; // move to positive
65 
66  return std::fabs(angle) + angle_step2 <= next_angle;
67  }
68 
69  bool CheckOriginal() const
70  {
71  return (m_UsedPosLists[USED_POS_PLUS].empty() || CheckAngle(*m_UsedPosLists[USED_POS_PLUS].begin(), 1.0f, 0)) &&
72  (m_UsedPosLists[USED_POS_MINUS].empty() || CheckAngle(*m_UsedPosLists[USED_POS_MINUS].begin(), -1.0f, 0));
73  }
74 
75  bool IsNonBalanced() const { return m_UsedPosLists[USED_POS_PLUS].empty() != m_UsedPosLists[USED_POS_MINUS].empty(); }
76 
77  bool NextAngleFor(UsedPosList::value_type const& usedPos, float sign, UsedPosType uptype, float &angle)
78  {
79  float angle_step = GetAngle(usedPos.second);
80 
81  // next possible angle
82  angle = usedPos.first * usedPos.second.sign + angle_step * sign;
83 
84  UsedPosList::value_type const* nextNode = nextUsedPos(uptype);
85  if (nextNode)
86  {
87  // if next node permit use selected angle, then do it
88  if (!CheckAngle(*nextNode, sign, angle))
89  {
90  m_smallStepOk[uptype] = false;
91  return false;
92  }
93  }
94 
95  // possible more points
96  m_smallStepOk[uptype] = true;
97  m_smallStepAngle[uptype] = angle;
98  m_smallStepNextUsedPos[uptype] = nextNode;
99 
100  return true;
101  }
102 
103  bool NextSmallStepAngle(float sign, UsedPosType uptype, float &angle)
104  {
105  // next possible angle
106  angle = m_smallStepAngle[uptype] + m_anglestep * sign;
107 
108  if (std::fabs(angle) > float(M_PI))
109  {
110  m_smallStepOk[uptype] = false;
111  return false;
112  }
113 
114  if (m_smallStepNextUsedPos[uptype])
115  {
116  if (std::fabs(angle) >= m_smallStepNextUsedPos[uptype]->first)
117  {
118  m_smallStepOk[uptype] = false;
119  return false;
120  }
121 
122  // if next node permit use selected angle, then do it
123  if (!CheckAngle(*m_smallStepNextUsedPos[uptype], sign, angle))
124  {
125  m_smallStepOk[uptype] = false;
126  return false;
127  }
128  }
129 
130  // possible more points
131  m_smallStepAngle[uptype] = angle;
132  return true;
133  }
134 
135  // next used post for m_nextUsedPos[uptype]
136  UsedPosList::value_type const* nextUsedPos(UsedPosType uptype);
137 
138  // angle from used pos to next possible free pos
139  float GetAngle(UsedPos const& usedPos) const { return std::acos(m_dist/(usedPos.dist+usedPos.size+m_size)); }
140 
141  float m_center_x;
142  float m_center_y;
143  float m_size; // size of object in center
144  float m_dist; // distance for searching pos (including central object size)
145  float m_anglestep;
146 
147  UsedPosList m_UsedPosLists[2];
148  UsedPosList::const_iterator m_nextUsedPos[2];
149 
150  // field for small step from first after next used pos until next pos
151  float m_smallStepAngle[2];
152  bool m_smallStepOk[2];
153  UsedPosList::value_type const* m_smallStepNextUsedPos[2];
154 };
155 #endif
bool IsNonBalanced() const
Definition: ObjectPosSelector.h:75
Definition: ObjectPosSelector.h:26
#define M_PI
Definition: Common.h:163
Definition: ObjectPosSelector.h:35
UsedPosType operator~(UsedPosType uptype)
Definition: ObjectPosSelector.h:28
bool NextSmallStepAngle(float sign, UsedPosType uptype, float &angle)
Definition: ObjectPosSelector.h:103
bool CheckOriginal() const
Definition: ObjectPosSelector.h:69
float size
Definition: ObjectPosSelector.h:41
float sign
Definition: ObjectPosSelector.h:39
bool CheckAngle(UsedPosList::value_type const &nextUsedPos, float sign, float angle) const
Definition: ObjectPosSelector.h:58
float m_center_x
Definition: ObjectPosSelector.h:141
float m_anglestep
Definition: ObjectPosSelector.h:145
bool NextAngleFor(UsedPosList::value_type const &usedPos, float sign, UsedPosType uptype, float &angle)
Definition: ObjectPosSelector.h:77
float m_dist
Definition: ObjectPosSelector.h:144
float GetAngle(UsedPos const &usedPos) const
Definition: ObjectPosSelector.h:139
float dist
Definition: ObjectPosSelector.h:42
G3D::int16 y
Definition: Vector2int16.h:38
Definition: ObjectPosSelector.h:26
float acos(float fValue)
Definition: g3dmath.h:633
UsedPosType
Definition: ObjectPosSelector.h:26
#define TC_GAME_API
Definition: Define.h:134
float m_center_y
Definition: ObjectPosSelector.h:142
G3D::int16 x
Definition: Vector2int16.h:37
double sign(double fValue)
Definition: g3dmath.h:669
Definition: ObjectPosSelector.h:33
std::multimap< float, UsedPos > UsedPosList
Definition: ObjectPosSelector.h:45
UsedPos(float sign_, float size_, float dist_)
Definition: ObjectPosSelector.h:37
float m_size
Definition: ObjectPosSelector.h:143