AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
M2Stores.cpp File Reference
#include "M2Stores.h"
#include "Containers.h"
#include "DBCStores.h"
#include "Log.h"
#include "M2Structure.h"
#include "World.h"
#include <boost/filesystem/path.hpp>
#include <fstream>

Go to the source code of this file.

Typedefs

typedef std::vector< FlyByCameraFlyByCameraCollection
 

Functions

G3D::Vector3 TranslateLocation (G3D::Vector4 const *DBCPosition, G3D::Vector3 const *basePosition, G3D::Vector3 const *splineVector)
 
bool readCamera (M2Camera const *cam, uint32 buffSize, M2Header const *header, CinematicCameraEntry const *dbcentry)
 
void LoadM2Cameras (std::string const &dataPath)
 
std::vector< FlyByCamera > const * GetFlyByCameras (uint32 cinematicCameraId)
 

Variables

std::unordered_map< uint32, FlyByCameraCollectionsFlyByCameraStore
 

Typedef Documentation

◆ FlyByCameraCollection

typedef std::vector<FlyByCamera> FlyByCameraCollection

Function Documentation

◆ GetFlyByCameras()

std::vector< FlyByCamera > const * GetFlyByCameras ( uint32  cinematicCameraId)
255{
256 return Acore::Containers::MapGetValuePtr(sFlyByCameraStore, cinematicCameraId);
257}
std::unordered_map< uint32, FlyByCameraCollection > sFlyByCameraStore
Definition: M2Stores.cpp:28
auto MapGetValuePtr(M &map, typename M::key_type const &key) -> decltype(AddressOrSelf(map.find(key) ->second))
Definition: Containers.h:206

References Acore::Containers::MapGetValuePtr(), and sFlyByCameraStore.

Referenced by CinematicMgr::BeginCinematic(), and debug_commandscript::HandleDebugPlayCinematicCommand().

◆ LoadM2Cameras()

void LoadM2Cameras ( std::string const &  dataPath)
174{
175 sFlyByCameraStore.clear();
176 LOG_INFO("server.loading", ">> Loading Cinematic Camera files");
177
178 uint32 oldMSTime = getMSTime();
179 for (CinematicCameraEntry const* dbcentry : sCinematicCameraStore)
180 {
181 std::string filenameWork = dataPath;
182 filenameWork.append(dbcentry->Model);
183
184 // Replace slashes (always to forward slash, because boost!)
185 std::replace(filenameWork.begin(), filenameWork.end(), '\\', '/');
186
187 boost::filesystem::path filename = filenameWork;
188
189 // Convert to native format
190 filename.make_preferred();
191
192 // Replace mdx to .m2
193 filename.replace_extension("m2");
194
195 std::ifstream m2file(filename.string().c_str(), std::ios::in | std::ios::binary);
196 if (!m2file.is_open())
197 continue;
198
199 // Get file size
200 m2file.seekg(0, std::ios::end);
201 std::streamoff fileSize = m2file.tellg();
202
203 // Reject if not at least the size of the header
204 if (static_cast<uint32>(fileSize) < sizeof(M2Header))
205 {
206 LOG_ERROR("server.loading", "Camera file {} is damaged. File is smaller than header size", filename.string());
207 m2file.close();
208 continue;
209 }
210
211 // Read 4 bytes (signature)
212 m2file.seekg(0, std::ios::beg);
213 char fileCheck[5];
214 m2file.read(fileCheck, 4);
215 fileCheck[4] = 0;
216
217 // Check file has correct magic (MD20)
218 if (strcmp(fileCheck, "MD20"))
219 {
220 LOG_ERROR("server.loading", "Camera file {} is damaged. File identifier not found", filename.string());
221 m2file.close();
222 continue;
223 }
224
225 // Now we have a good file, read it all into a vector of char's, then close the file.
226 std::vector<char> buffer(fileSize);
227 m2file.seekg(0, std::ios::beg);
228 if (!m2file.read(buffer.data(), fileSize))
229 {
230 m2file.close();
231 continue;
232 }
233 m2file.close();
234
235 // Read header
236 M2Header const* header = reinterpret_cast<M2Header const*>(buffer.data());
237
238 if (header->ofsCameras + sizeof(M2Camera) > static_cast<uint32>(fileSize))
239 {
240 LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
241 continue;
242 }
243
244 // Get camera(s) - Main header, then dump them.
245 M2Camera const* cam = reinterpret_cast<M2Camera const*>(buffer.data() + header->ofsCameras);
246 if (!readCamera(cam, fileSize, header, dbcentry))
247 LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
248 }
249
250 LOG_INFO("server.loading", ">> Loaded {} Cinematic Waypoint Sets in {} ms", (uint32)sFlyByCameraStore.size(), GetMSTimeDiffToNow(oldMSTime));
251 LOG_INFO("server.loading", " ");
252}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
std::uint32_t uint32
Definition: Define.h:107
DBCStorage< CinematicCameraEntry > sCinematicCameraStore(CinematicCameraEntryfmt)
bool readCamera(M2Camera const *cam, uint32 buffSize, M2Header const *header, CinematicCameraEntry const *dbcentry)
Definition: M2Stores.cpp:50
Definition: M2Structure.h:35
uint32 ofsCameras
Definition: M2Structure.h:95
Definition: M2Structure.h:120
Definition: DBCStructure.h:703

References getMSTime(), GetMSTimeDiffToNow(), LOG_ERROR, LOG_INFO, M2Header::ofsCameras, readCamera(), sCinematicCameraStore, and sFlyByCameraStore.

Referenced by World::SetInitialWorldSettings().

◆ readCamera()

bool readCamera ( M2Camera const *  cam,
uint32  buffSize,
M2Header const *  header,
CinematicCameraEntry const *  dbcentry 
)
51{
52 char const* buffer = reinterpret_cast<char const*>(header);
53
55 FlyByCameraCollection targetcam;
56
57 G3D::Vector4 DBCData;
58 DBCData.x = dbcentry->Origin.X;
59 DBCData.y = dbcentry->Origin.Y;
60 DBCData.z = dbcentry->Origin.Z;
61 DBCData.w = dbcentry->OriginFacing;
62
63 // Read target locations, only so that we can calculate orientation
64 for (uint32 k = 0; k < cam->target_positions.timestamps.number; ++k)
65 {
66 // Extract Target positions
67 if (cam->target_positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
68 return false;
69 M2Array const* targTsArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.timestamps.offset_elements);
70 if (targTsArray->offset_elements + sizeof(uint32) > buffSize || cam->target_positions.values.offset_elements + sizeof(M2Array) > buffSize)
71 return false;
72 uint32 const* targTimestamps = reinterpret_cast<uint32 const*>(buffer + targTsArray->offset_elements);
73 M2Array const* targArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.values.offset_elements);
74
75 if (targArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
76 return false;
77 M2SplineKey<G3D::Vector3> const* targPositions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + targArray->offset_elements);
78
79 // Read the data for this set
80 uint32 currPos = targArray->offset_elements;
81 for (uint32 i = 0; i < targTsArray->number; ++i)
82 {
83 if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
84 return false;
85 // Translate co-ordinates
86 G3D::Vector3 newPos = TranslateLocation(&DBCData, &cam->target_position_base, &targPositions->p0);
87
88 // Add to vector
89 FlyByCamera thisCam;
90 thisCam.timeStamp = targTimestamps[i];
91 thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z, 0.0f);
92 targetcam.push_back(thisCam);
93 targPositions++;
94 currPos += sizeof(M2SplineKey<G3D::Vector3>);
95 }
96 }
97
98 // Read camera positions and timestamps (translating first position of 3 only, we don't need to translate the whole spline)
99 for (uint32 k = 0; k < cam->positions.timestamps.number; ++k)
100 {
101 // Extract Camera positions for this set
102 if (cam->positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
103 return false;
104 M2Array const* posTsArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.timestamps.offset_elements);
105 if (posTsArray->offset_elements + sizeof(uint32) > buffSize || cam->positions.values.offset_elements + sizeof(M2Array) > buffSize)
106 return false;
107 uint32 const* posTimestamps = reinterpret_cast<uint32 const*>(buffer + posTsArray->offset_elements);
108 M2Array const* posArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.values.offset_elements);
109 if (posArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
110 return false;
111 M2SplineKey<G3D::Vector3> const* positions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + posArray->offset_elements);
112
113 // Read the data for this set
114 uint32 currPos = posArray->offset_elements;
115 for (uint32 i = 0; i < posTsArray->number; ++i)
116 {
117 if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
118 return false;
119 // Translate co-ordinates
120 G3D::Vector3 newPos = TranslateLocation(&DBCData, &cam->position_base, &positions->p0);
121
122 // Add to vector
123 FlyByCamera thisCam;
124 thisCam.timeStamp = posTimestamps[i];
125 thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z);
126
127 if (targetcam.size() > 0)
128 {
129 // Find the target camera before and after this camera
130 FlyByCamera lastTarget;
131 FlyByCamera nextTarget;
132
133 // Pre-load first item
134 lastTarget = targetcam[0];
135 nextTarget = targetcam[0];
136 for (uint32 j = 0; j < targetcam.size(); ++j)
137 {
138 nextTarget = targetcam[j];
139 if (targetcam[j].timeStamp > posTimestamps[i])
140 break;
141
142 lastTarget = targetcam[j];
143 }
144
145 float x, y, z;
146 lastTarget.locations.GetPosition(x, y, z);
147
148 // Now, the timestamps for target cam and position can be different. So, if they differ we interpolate
149 if (lastTarget.timeStamp != posTimestamps[i])
150 {
151 uint32 timeDiffTarget = nextTarget.timeStamp - lastTarget.timeStamp;
152 uint32 timeDiffThis = posTimestamps[i] - lastTarget.timeStamp;
153 float xDiff = nextTarget.locations.GetPositionX() - lastTarget.locations.GetPositionX();
154 float yDiff = nextTarget.locations.GetPositionY() - lastTarget.locations.GetPositionY();
155 x = lastTarget.locations.GetPositionX() + (xDiff * (float(timeDiffThis) / float(timeDiffTarget)));
156 y = lastTarget.locations.GetPositionY() + (yDiff * (float(timeDiffThis) / float(timeDiffTarget)));
157 }
158 float xDiff = x - thisCam.locations.GetPositionX();
159 float yDiff = y - thisCam.locations.GetPositionY();
160 thisCam.locations.SetOrientation(std::atan2(yDiff, xDiff));
161 }
162
163 cameras.push_back(thisCam);
164 positions++;
165 currPos += sizeof(M2SplineKey<G3D::Vector3>);
166 }
167 }
168
169 sFlyByCameraStore[dbcentry->ID] = cameras;
170 return true;
171}
const Position positions[MAX_SUMMONS]
Definition: boss_lurker_below.cpp:57
std::vector< FlyByCamera > FlyByCameraCollection
Definition: M2Stores.cpp:27
G3D::Vector3 TranslateLocation(G3D::Vector4 const *DBCPosition, G3D::Vector3 const *basePosition, G3D::Vector3 const *splineVector)
Definition: M2Stores.cpp:31
Definition: M2Stores.h:26
uint32 timeStamp
Definition: M2Stores.h:27
Position locations
Definition: M2Stores.h:28
Definition: M2Structure.h:28
T p0
Definition: M2Structure.h:29
Definition: M2Structure.h:107
uint32 offset_elements
Definition: M2Structure.h:109
uint32_t number
Definition: M2Structure.h:108
void SetOrientation(float orientation)
Definition: Position.h:111
float GetPositionX() const
Definition: Position.h:116
void GetPosition(float &x, float &y) const
Definition: Position.h:121
float GetPositionY() const
Definition: Position.h:117
void Relocate(float x, float y)
Definition: Position.h:72

References Position::GetPosition(), Position::GetPositionX(), Position::GetPositionY(), CinematicCameraEntry::ID, FlyByCamera::locations, M2Array::number, M2Array::offset_elements, CinematicCameraEntry::Origin, CinematicCameraEntry::OriginFacing, M2SplineKey< T >::p0, M2Camera::position_base, M2Camera::positions, positions, Position::Relocate(), Position::SetOrientation(), sFlyByCameraStore, M2Camera::target_position_base, M2Camera::target_positions, FlyByCamera::timeStamp, M2Track::timestamps, TranslateLocation(), M2Track::values, DBCPosition3D::X, DBCPosition3D::Y, and DBCPosition3D::Z.

Referenced by LoadM2Cameras().

◆ TranslateLocation()

G3D::Vector3 TranslateLocation ( G3D::Vector4 const *  DBCPosition,
G3D::Vector3 const *  basePosition,
G3D::Vector3 const *  splineVector 
)
32{
33 G3D::Vector3 work;
34 float x = basePosition->x + splineVector->x;
35 float y = basePosition->y + splineVector->y;
36 float z = basePosition->z + splineVector->z;
37 float const distance = std::sqrt((x * x) + (y * y));
38 float angle = std::atan2(x, y) - DBCPosition->w;
39
40 if (angle < 0)
41 angle += 2 * float(M_PI);
42
43 work.x = DBCPosition->x + (distance * sin(angle));
44 work.y = DBCPosition->y + (distance * cos(angle));
45 work.z = DBCPosition->z + z;
46 return work;
47}

Referenced by readCamera().

Variable Documentation

◆ sFlyByCameraStore

std::unordered_map<uint32, FlyByCameraCollection> sFlyByCameraStore