TrinityCore
Loading...
Searching...
No Matches
MoveSplineInit.cpp
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#include "MoveSplineInit.h"
19#include "Creature.h"
20#include "MoveSpline.h"
21#include "MovementPackets.h"
22#include "PathGenerator.h"
23#include "Unit.h"
24#include "VehicleDefines.h"
25
26namespace Movement
27{
28 // Transforms coordinates from global to transport offsets
30 {
31 public:
32 TransportPathTransform(Unit const* owner, bool transformForTransport)
33 : _transport(transformForTransport ? owner->GetDirectTransport() : nullptr) { }
34
35 Vector3 operator()(Vector3 input) const
36 {
37 if (_transport)
38 _transport->GetPositionOffsetTo({ input.x, input.y, input.z }).GetPosition(input.x, input.y, input.z);
39
40 return input;
41 }
42
43 float operator()(float input) const
44 {
45 if (_transport)
47
48 return input;
49 }
50
51 private:
53 };
54
56 {
57 if (moveFlags & MOVEMENTFLAG_FLYING)
58 {
59 if (moveFlags & MOVEMENTFLAG_BACKWARD /*&& speed_obj.flight >= speed_obj.flight_back*/)
60 return MOVE_FLIGHT_BACK;
61 else
62 return MOVE_FLIGHT;
63 }
64 else if (moveFlags & MOVEMENTFLAG_SWIMMING)
65 {
66 if (moveFlags & MOVEMENTFLAG_BACKWARD /*&& speed_obj.swim >= speed_obj.swim_back*/)
67 return MOVE_SWIM_BACK;
68 else
69 return MOVE_SWIM;
70 }
71 else if (moveFlags & MOVEMENTFLAG_WALKING)
72 {
73 //if (speed_obj.run > speed_obj.walk)
74 return MOVE_WALK;
75 }
76 else if (moveFlags & MOVEMENTFLAG_BACKWARD /*&& speed_obj.run >= speed_obj.run_back*/)
77 return MOVE_RUN_BACK;
78
79 // Flying creatures use MOVEMENTFLAG_CAN_FLY or MOVEMENTFLAG_DISABLE_GRAVITY
80 // Run speed is their default flight speed.
81 return MOVE_RUN;
82 }
83
85 {
86 MoveSpline& move_spline = *unit->movespline;
87
88 bool transport = !unit->GetTransGUID().IsEmpty();
89 Location real_position;
90 // there is a big chance that current position is unknown if current state is not finalized, need compute it
91 // this also allows calculate spline position and update map position in much greater intervals
92 // Don't compute for transport movement if the unit is in a motion between two transports
93 if (!move_spline.Finalized() && move_spline.onTransport == transport)
94 real_position = move_spline.ComputePosition();
95 else
96 {
97 Position const* pos;
98 if (!transport)
99 pos = unit;
100 else
102
103 real_position.x = pos->GetPositionX();
104 real_position.y = pos->GetPositionY();
105 real_position.z = pos->GetPositionZ();
106 real_position.orientation = unit->GetOrientation();
107 }
108
109 // should i do the things that user should do? - no.
110 if (args.path.empty())
111 return 0;
112
113 // correct first vertex
114 args.path[0] = real_position;
115 args.initialOrientation = real_position.orientation;
116 args.flags.Enter_Cycle = args.flags.Cyclic;
117 move_spline.onTransport = transport;
118
120 if (!args.flags.Backward)
121 moveFlags = (moveFlags & ~MOVEMENTFLAG_BACKWARD) | MOVEMENTFLAG_FORWARD;
122 else
123 moveFlags = (moveFlags & ~MOVEMENTFLAG_FORWARD) | MOVEMENTFLAG_BACKWARD;
124
125 if (moveFlags & MOVEMENTFLAG_ROOT)
126 moveFlags &= ~MOVEMENTFLAG_MASK_MOVING;
127
128 if (!args.HasVelocity)
129 {
130 // If spline is initialized with SetWalk method it only means we need to select
131 // walk move speed for it but not add walk flag to unit
132 uint32 moveFlagsForSpeed = moveFlags;
133 if (args.walk)
134 moveFlagsForSpeed |= MOVEMENTFLAG_WALKING;
135 else
136 moveFlagsForSpeed &= ~MOVEMENTFLAG_WALKING;
137
138 args.velocity = unit->GetSpeed(SelectSpeedType(moveFlagsForSpeed));
139 if (Creature* creature = unit->ToCreature())
140 if (creature->HasSearchedAssistance())
141 args.velocity *= 0.66f;
142 }
143
144 // limit the speed in the same way the client does
145 float speedLimit = [&]()
146 {
147 if (args.flags.UnlimitedSpeed)
148 return std::numeric_limits<float>::max();
149
150 if (args.flags.Falling || args.flags.Catmullrom || args.flags.Flying || args.flags.Parabolic)
151 return 50.0f;
152
153 return std::max(28.0f, unit->GetSpeed(MOVE_RUN) * 4.0f);
154 }();
155
156 args.velocity = std::min(args.velocity, speedLimit);
157
158 if (!args.Validate(unit))
159 return 0;
160
162 move_spline.Initialize(args);
163
165 packet.MoverGUID = unit->GetGUID();
166 packet.Pos = Position(real_position.x, real_position.y, real_position.z, real_position.orientation);
167 packet.InitializeSplineData(move_spline);
168 if (transport)
169 {
172 }
173
174 unit->SendMessageToSet(packet.Write(), true);
175
176 return move_spline.Duration();
177 }
178
180 {
181 MoveSpline& move_spline = *unit->movespline;
182
183 // No need to stop if we are not moving
184 if (move_spline.Finalized())
185 return;
186
187 bool transport = !unit->GetTransGUID().IsEmpty();
188 Location loc;
189 if (move_spline.onTransport == transport)
190 loc = move_spline.ComputePosition();
191 else
192 {
193 Position const* pos;
194 if (!transport)
195 pos = unit;
196 else
198
199 loc.x = pos->GetPositionX();
200 loc.y = pos->GetPositionY();
201 loc.z = pos->GetPositionZ();
203 }
204
205 if (move_spline.isTurning())
207
210 move_spline.onTransport = transport;
211 move_spline.Initialize(args);
212
214 packet.MoverGUID = unit->GetGUID();
215 packet.Pos = Position(loc.x, loc.y, loc.z, loc.orientation);
216 packet.SplineData.StopSplineStyle = 2;
217 packet.SplineData.ID = move_spline.GetId();
221
222 if (transport)
223 {
226 }
227
228 unit->SendMessageToSet(packet.Write(), true);
229 }
230
232 {
234 // Elevators also use MOVEMENTFLAG_ONTRANSPORT but we do not keep track of their position changes
236 // mix existing state into new
238 args.flags.CanSwim = unit->CanSwim();
240 args.flags.FastSteering = true;
242 }
243
245
246 void MoveSplineInit::SetFacing(Vector3 const& spot)
247 {
249 Vector3 finalSpot = transform(spot);
250 args.facing.f.x = finalSpot.x;
251 args.facing.f.y = finalSpot.y;
252 args.facing.f.z = finalSpot.z;
254 }
255
256 void MoveSplineInit::SetFacing(float x, float y, float z)
257 {
258 SetFacing({ x, y, z });
259 }
260
261 void MoveSplineInit::SetFacing(Unit const* target)
262 {
264 args.facing.target = target->GetGUID();
266 }
267
274
275 void MoveSplineInit::MovebyPath(std::span<Vector3 const> path, int32 pointId)
276 {
277 args.path_Idx_offset = pointId;
278 args.path.resize(path.size());
279 std::ranges::transform(path, args.path.begin(), TransportPathTransform(unit, args.TransformForTransport));
280 }
281
282 void MoveSplineInit::MoveTo(float x, float y, float z, bool generatePath, bool forceDestination)
283 {
284 MoveTo(G3D::Vector3(x, y, z), generatePath, forceDestination);
285 }
286
287 void MoveSplineInit::MoveTo(Vector3 const& dest, bool generatePath, bool forceDestination)
288 {
289 if (generatePath)
290 {
291 PathGenerator path(unit);
292 bool result = path.CalculatePath(dest.x, dest.y, dest.z, forceDestination);
293 if (result && !(path.GetPathType() & PATHFIND_NOPATH))
294 {
295 MovebyPath(path.GetPath());
296 return;
297 }
298 }
299
301 args.path.resize(2);
303 args.path[1] = transform(dest);
304 }
305
307 {
308 args.flags.Falling = true;
310 }
311
313 {
314 _init.SetFacing(point.GetPositionX(), point.GetPositionY(), point.GetPositionZ());
315 }
316}
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
@ PATHFIND_NOPATH
UnitMoveType
@ MOVE_FLIGHT
@ MOVE_SWIM
@ MOVE_FLIGHT_BACK
@ MOVE_SWIM_BACK
@ MOVE_RUN
@ MOVE_RUN_BACK
@ MOVE_WALK
MovementFlags
@ MOVEMENTFLAG_FORWARD
@ MOVEMENTFLAG_BACKWARD
@ MOVEMENTFLAG_DISABLE_GRAVITY
@ MOVEMENTFLAG_FLYING
@ MOVEMENTFLAG_FALLING_SLOW
@ MOVEMENTFLAG_CAN_FLY
@ MOVEMENTFLAG_ROOT
@ MOVEMENTFLAG_SWIMMING
@ MOVEMENTFLAG_WALKING
@ UNIT_NPC_FLAG_2_STEERING
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
void MoveTo(Vector3 const &destination, bool generatePath=true, bool forceDestination=false)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
void SetFacing(float angle)
MoveSplineInitArgs args
bool isTurning() const
Definition MoveSpline.h:142
Location ComputePosition() const
uint32 GetId() const
Definition MoveSpline.h:138
bool Finalized() const
Definition MoveSpline.h:139
void Initialize(MoveSplineInitArgs const &)
int32 Duration() const
Definition MoveSpline.h:106
float operator()(float input) const
TransportPathTransform(Unit const *owner, bool transformForTransport)
Vector3 operator()(Vector3 input) const
bool IsEmpty() const
Definition ObjectGuid.h:362
Creature * ToCreature()
Definition Object.h:121
Movement::PointsArray const & GetPath() const
PathType GetPathType() const
bool CalculatePath(float srcX, float srcY, float srcZ, float destX, float destY, float destZ, bool forceDest=false)
virtual Position GetPositionOffsetTo(Position const &endPos) const =0
This method transforms supplied global coordinates into local offsets.
virtual float GetTransportOrientation() const =0
Definition Unit.h:635
float GetSpeed(UnitMoveType mtype) const
Definition Unit.cpp:8932
bool HasNpcFlag2(NPCFlags2 flags) const
Definition Unit.h:1002
bool HasUnitMovementFlag(uint32 f) const
Definition Unit.h:1734
ObjectGuid GetTransGUID() const override
Definition Unit.cpp:12141
std::unique_ptr< Movement::MoveSpline > movespline
Definition Unit.h:1838
virtual bool CanSwim() const
Definition Unit.cpp:12944
bool IsInCombat() const
Definition Unit.h:1058
int8 GetTransSeat() const
Definition Object.h:544
virtual void SendMessageToSet(WorldPacket const *data, bool self) const
Definition Object.cpp:1094
MovementInfo m_movementInfo
Definition Object.h:548
WorldPacket const * Write() override
TaggedPosition< Position::XYZ > Pos
void InitializeSplineData(::Movement::MoveSpline const &moveSpline)
UnitMoveType SelectSpeedType(uint32 moveFlags)
TC_GAME_API UInt32Counter splineIdGen
@ MONSTER_MOVE_FACING_TARGET
@ MONSTER_MOVE_FACING_ANGLE
@ MONSTER_MOVE_FACING_SPOT
void RemoveMovementFlag(uint32 flag)
struct MovementInfo::TransportInfo transport
void SetMovementFlags(uint32 flag)
uint32 GetMovementFlags() const
struct Movement::FacingInfo::@309 f
bool Validate(Unit const *unit)
============================================================================================
void operator()(std::monostate) const
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
static float NormalizeOrientation(float o)
Definition Position.cpp:207
float GetAbsoluteAngle(float x, float y) const
Definition Position.h:136
constexpr float GetOrientation() const
Definition Position.h:90
constexpr float GetPositionZ() const
Definition Position.h:89