aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Entities/Object/ObjectPosSelector.h
blob: 42bb2dd07945f6f3f156ecf26cd0bb46dd8b6570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _OBJECT_POS_SELECTOR_H
#define _OBJECT_POS_SELECTOR_H

#include "Common.h"
#include <map>
#include <cmath>

enum UsedPosType { USED_POS_PLUS, USED_POS_MINUS };

inline UsedPosType operator~(UsedPosType uptype)
{
    return uptype == USED_POS_PLUS ? USED_POS_MINUS : USED_POS_PLUS;
}

struct TC_GAME_API ObjectPosSelector
{
    struct UsedPos
    {
        UsedPos(float sign_, float size_, float dist_) : sign(sign_), size(size_), dist(dist_) { }

        float sign;

        float size;                                         // size of point
        float dist;                                         // dist to central point (including central point size)
    };

    typedef std::multimap<float, UsedPos> UsedPosList;       // abs(angle)->Node

    ObjectPosSelector(float x, float y, float size, float dist);

    void AddUsedPos(float size, float angle, float dist);
    void InitializeAngle();

    bool FirstAngle(float& angle);
    bool NextAngle(float& angle);
    bool NextUsedAngle(float& angle);

    bool NextPosibleAngle(float& angle);

    bool CheckAngle(UsedPosList::value_type const& nextUsedPos, float sign, float angle ) const
    {
        float angle_step2  = GetAngle(nextUsedPos.second);

        float next_angle = nextUsedPos.first;
        if (nextUsedPos.second.sign * sign < 0)                       // last node from diff. list (-pi+alpha)
            next_angle = 2 * float(M_PI) - next_angle;   // move to positive

        return std::fabs(angle) + angle_step2 <= next_angle;
    }

    bool CheckOriginal() const
    {
        return (m_UsedPosLists[USED_POS_PLUS].empty() || CheckAngle(*m_UsedPosLists[USED_POS_PLUS].begin(), 1.0f, 0)) &&
            (m_UsedPosLists[USED_POS_MINUS].empty() || CheckAngle(*m_UsedPosLists[USED_POS_MINUS].begin(), -1.0f, 0));
    }

    bool IsNonBalanced() const { return m_UsedPosLists[USED_POS_PLUS].empty() != m_UsedPosLists[USED_POS_MINUS].empty(); }

    bool NextAngleFor(UsedPosList::value_type const& usedPos, float sign, UsedPosType uptype, float &angle)
    {
        float angle_step  = GetAngle(usedPos.second);

        // next possible angle
        angle  = usedPos.first * usedPos.second.sign + angle_step * sign;

        UsedPosList::value_type const* nextNode = nextUsedPos(uptype);
        if (nextNode)
        {
            // if next node permit use selected angle, then do it
            if (!CheckAngle(*nextNode, sign, angle))
            {
                m_smallStepOk[uptype] = false;
                return false;
            }
        }

        // possible more points
        m_smallStepOk[uptype] = true;
        m_smallStepAngle[uptype] = angle;
        m_smallStepNextUsedPos[uptype] = nextNode;

        return true;
    }

    bool NextSmallStepAngle(float sign, UsedPosType uptype, float &angle)
    {
        // next possible angle
        angle  = m_smallStepAngle[uptype] + m_anglestep * sign;

        if (std::fabs(angle) > float(M_PI))
        {
            m_smallStepOk[uptype] = false;
            return false;
        }

        if (m_smallStepNextUsedPos[uptype])
        {
            if (std::fabs(angle) >= m_smallStepNextUsedPos[uptype]->first)
            {
                m_smallStepOk[uptype] = false;
                return false;
            }

            // if next node permit use selected angle, then do it
            if (!CheckAngle(*m_smallStepNextUsedPos[uptype], sign, angle))
            {
                m_smallStepOk[uptype] = false;
                return false;
            }
        }

        // possible more points
        m_smallStepAngle[uptype] = angle;
        return true;
    }

    // next used post for m_nextUsedPos[uptype]
    UsedPosList::value_type const* nextUsedPos(UsedPosType uptype);

    // angle from used pos to next possible free pos
    float GetAngle(UsedPos const& usedPos) const { return std::acos(m_dist/(usedPos.dist+usedPos.size+m_size)); }

    float m_center_x;
    float m_center_y;
    float m_size;                                           // size of object in center
    float m_dist;                                           // distance for searching pos (including central object size)
    float m_anglestep;

    UsedPosList m_UsedPosLists[2];
    UsedPosList::const_iterator m_nextUsedPos[2];

    // field for small step from first after next used pos until next pos
    float m_smallStepAngle[2];
    bool  m_smallStepOk[2];
    UsedPosList::value_type const* m_smallStepNextUsedPos[2];
};
#endif