TrinityCore
ScriptSystem.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 "ScriptSystem.h"
19#include "Creature.h"
20#include "DatabaseEnv.h"
21#include "Log.h"
22#include "ObjectMgr.h"
23#include "ScriptMgr.h"
24#include "SplineChain.h"
25
26SystemMgr::SystemMgr() = default;
27SystemMgr::~SystemMgr() = default;
28
30{
31 static SystemMgr instance;
32 return &instance;
33}
34
36{
37 uint32 oldMSTime = getMSTime();
38
39 m_mSplineChainsMap.clear();
40
41 // 0 1 2 3 4 5
42 QueryResult resultMeta = WorldDatabase.Query("SELECT entry, chainId, splineId, expectedDuration, msUntilNext, velocity FROM script_spline_chain_meta ORDER BY entry asc, chainId asc, splineId asc");
43 // 0 1 2 3 4 5 6
44 QueryResult resultWP = WorldDatabase.Query("SELECT entry, chainId, splineId, wpId, x, y, z FROM script_spline_chain_waypoints ORDER BY entry asc, chainId asc, splineId asc, wpId asc");
45 if (!resultMeta || !resultWP)
46 {
47 TC_LOG_INFO("server.loading", ">> Loaded spline chain data for 0 chains, consisting of 0 splines with 0 waypoints. DB tables `script_spline_chain_meta` and `script_spline_chain_waypoints` are empty.");
48 }
49 else
50 {
51 uint32 chainCount = 0, splineCount = 0, wpCount = 0;
52 do
53 {
54 Field* fieldsMeta = resultMeta->Fetch();
55 uint32 entry = fieldsMeta[0].GetUInt32();
56 uint16 chainId = fieldsMeta[1].GetUInt16();
57 uint8 splineId = fieldsMeta[2].GetUInt8();
58 std::vector<SplineChainLink>& chain = m_mSplineChainsMap[{entry, chainId}];
59
60 if (splineId != chain.size())
61 {
62 TC_LOG_WARN("server.loading", "Creature #{}: Chain {} has orphaned spline {}, skipped.", entry, chainId, splineId);
63 continue;
64 }
65
66 uint32 expectedDuration = fieldsMeta[3].GetUInt32();
67 uint32 msUntilNext = fieldsMeta[4].GetUInt32();
68 float velocity = fieldsMeta[5].GetFloat();
69 chain.emplace_back(expectedDuration, msUntilNext, velocity);
70
71 if (splineId == 0)
72 ++chainCount;
73 ++splineCount;
74 } while (resultMeta->NextRow());
75
76 do
77 {
78 Field* fieldsWP = resultWP->Fetch();
79 uint32 entry = fieldsWP[0].GetUInt32();
80 uint16 chainId = fieldsWP[1].GetUInt16();
81 uint8 splineId = fieldsWP[2].GetUInt8(), wpId = fieldsWP[3].GetUInt8();
82 float posX = fieldsWP[4].GetFloat(), posY = fieldsWP[5].GetFloat(), posZ = fieldsWP[6].GetFloat();
83 auto it = m_mSplineChainsMap.find({entry,chainId});
84 if (it == m_mSplineChainsMap.end())
85 {
86 TC_LOG_WARN("server.loading", "Creature #{} has waypoint data for spline chain {}. No such chain exists - entry skipped.", entry, chainId);
87 continue;
88 }
89 std::vector<SplineChainLink>& chain = it->second;
90 if (splineId >= chain.size())
91 {
92 TC_LOG_WARN("server.loading", "Creature #{} has waypoint data for spline ({},{}). The specified chain does not have a spline with this index - entry skipped.", entry, chainId, splineId);
93 continue;
94 }
95 SplineChainLink& spline = chain[splineId];
96 if (wpId != spline.Points.size())
97 {
98 TC_LOG_WARN("server.loading", "Creature #{} has orphaned waypoint data in spline ({},{}) at index {}. Skipped.", entry, chainId, splineId, wpId);
99 continue;
100 }
101 spline.Points.emplace_back(posX, posY, posZ);
102 ++wpCount;
103 } while (resultWP->NextRow());
104
105 TC_LOG_INFO("server.loading", ">> Loaded spline chain data for {} chains, consisting of {} splines with {} waypoints in {} ms", chainCount, splineCount, wpCount, GetMSTimeDiffToNow(oldMSTime));
106 }
107}
108
109std::vector<SplineChainLink> const* SystemMgr::GetSplineChain(uint32 entry, uint16 chainId) const
110{
111 auto it = m_mSplineChainsMap.find({ entry, chainId });
112 if (it != m_mSplineChainsMap.end())
113 return &it->second;
114 return nullptr;
115}
116
117std::vector<SplineChainLink> const* SystemMgr::GetSplineChain(Creature const* who, uint16 id) const
118{
119 return GetSplineChain(who->GetEntry(), id);
120}
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint8_t uint8
Definition: Define.h:144
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:162
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
Class used to access individual fields of database query result.
Definition: Field.h:90
uint8 GetUInt8() const
Definition: Field.cpp:30
uint16 GetUInt16() const
Definition: Field.cpp:46
float GetFloat() const
Definition: Field.cpp:94
uint32 GetUInt32() const
Definition: Field.cpp:62
uint32 GetEntry() const
Definition: Object.h:161
void LoadScriptSplineChains()
std::unordered_map< ChainKeyType, std::vector< SplineChainLink > > m_mSplineChainsMap
Definition: ScriptSystem.h:50
static SystemMgr * instance()
std::vector< SplineChainLink > const * GetSplineChain(uint32 entry, uint16 chainId) const