aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Movement/Spline/MovementUtil.cpp
blob: 71fc20db4857b4f2fe8f5e33abd19c3f5ff15588 (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
/*
 * 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/>.
 */

#include "MoveSplineFlag.h"
#include "MovementTypedefs.h"
#include "SmartEnum.h"
#include <cmath>

namespace Movement
{
    UInt32Counter splineIdGen;

    /// Velocity bounds that makes fall speed limited
    float constexpr terminalVelocity = 60.148003f;
    float constexpr terminalSafefallVelocity = 7.0f;

    float constexpr terminal_length = float(terminalVelocity * terminalVelocity) / (2.0f * gravity);
    float constexpr terminal_safeFall_length = (terminalSafefallVelocity * terminalSafefallVelocity) / (2.0f * gravity);
    float constexpr terminal_fallTime = float(terminalVelocity / gravity); // the time that needed to reach terminalVelocity
    float constexpr terminal_safeFall_fallTime = float(terminalSafefallVelocity / gravity); // the time that needed to reach terminalVelocity with safefall

    float computeFallTime(float path_length, bool isSafeFall)
    {
        if (path_length < 0.0f)
            return 0.0f;

        float time;
        if (isSafeFall)
        {
            if (path_length >= terminal_safeFall_length)
                time = (path_length - terminal_safeFall_length) / terminalSafefallVelocity + terminal_safeFall_fallTime;
            else
                time = std::sqrt(2.0f * path_length / gravity);
        }
        else
        {
            if (path_length >= terminal_length)
                time = (path_length - terminal_length) / terminalVelocity + terminal_fallTime;
            else
                time = std::sqrt(2.0f * path_length / gravity);
        }

        return time;
    }

    float computeFallElevation(float t_passed, bool isSafeFall, float start_velocity /*= 0.0f*/)
    {
        float termVel;
        float result;

        if (isSafeFall)
            termVel = terminalSafefallVelocity;
        else
            termVel = terminalVelocity;

        if (start_velocity > termVel)
            start_velocity = termVel;

        float terminal_time = (isSafeFall ? terminal_safeFall_fallTime : terminal_fallTime) - start_velocity / gravity; // the time that needed to reach terminalVelocity

        if (t_passed > terminal_time)
        {
            result = termVel * (t_passed - terminal_time) +
                start_velocity * terminal_time +
                gravity * terminal_time * terminal_time * 0.5f;
        }
        else
            result = t_passed * (start_velocity + t_passed * gravity * 0.5f);

        return result;
    }

    template<class Flags>
    void PrintFlags(Flags t, std::string& str)
    {
        for (Flags flag : EnumUtils::Iterate<Flags>())
            if ((t & flag) != Flags(0))
                str.append(" ").append(EnumUtils::ToConstant(flag));
    }

    std::string MoveSplineFlag::ToString() const
    {
        std::string str;
        PrintFlags<MoveSplineFlagEnum>(Raw, str);
        return str;
    }

    std::string MovementFlags_ToString(MovementFlags flags)
    {
        std::string str;
        PrintFlags(flags, str);
        return str;
    }

    std::string MovementFlags_ToString(MovementFlags2 flags)
    {
        std::string str;
        PrintFlags(flags, str);
        return str;
    }

    std::string MovementFlags_ToString(MovementFlags3 flags)
    {
        std::string str;
        PrintFlags(flags, str);
        return str;
    }
}