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