Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
291 строка · 9.1 Кб
1
/*
2
* This file is part of the Pandaria 5.4.8 Project. See THANKS 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 "GameObjectModel.h"
19
#include "Log.h"
20
#include "MapTree.h"
21
#include "Timer.h"
22
#include "VMapFactory.h"
23
#include "VMapManager2.h"
24
#include "VMapDefinitions.h"
25
#include "WorldModel.h"
26
#include <utility>
27

28
using G3D::Vector3;
29
using G3D::Ray;
30
using G3D::AABox;
31

32
struct GameobjectModelData
33
{
34
    GameobjectModelData(std::string name_, const AABox& box, bool isWmo_) :
35
        bound(box), name(std::move(name_)), isWmo(isWmo_) { }
36

37
    AABox bound;
38
    std::string name;
39
    bool isWmo;
40
};
41

42
typedef std::unordered_map<uint32, GameobjectModelData> ModelList;
43
ModelList model_list;
44

45
void LoadGameObjectModelList(std::string const& dataPath)
46
{
47
    uint32 oldMSTime = getMSTime();
48

49
    FILE* model_list_file = fopen((dataPath + "vmaps/" + VMAP::GAMEOBJECT_MODELS).c_str(), "rb");
50
    if (!model_list_file)
51
    {
52
        VMAP_ERROR_LOG("misc", "Unable to open '%s' file.", VMAP::GAMEOBJECT_MODELS);
53
        return;
54
    }
55

56
    char magic[8];
57
    if (fread(magic, 1, 8, model_list_file) != 8
58
        || memcmp(magic, VMAP::VMAP_MAGIC, 8) != 0)
59
    {
60
        TC_LOG_ERROR("misc", "File '%s' has wrong header, expected %s.", VMAP::GAMEOBJECT_MODELS, VMAP::VMAP_MAGIC);
61
        fclose(model_list_file);
62
        return;
63
    }
64

65
    uint32 name_length, displayId;
66
    uint8 isWmo;
67
    char buff[500];
68
    while (true)
69
    {
70
        Vector3 v1, v2;
71
        if (fread(&displayId, sizeof(uint32), 1, model_list_file) != 1)
72
            if (feof(model_list_file))  // EOF flag is only set after failed reading attempt
73
                break;
74

75
        if (fread(&isWmo, sizeof(uint8), 1, model_list_file) != 1
76
            || fread(&name_length, sizeof(uint32), 1, model_list_file) != 1
77
            || name_length >= sizeof(buff)
78
            || fread(&buff, sizeof(char), name_length, model_list_file) != name_length
79
            || fread(&v1, sizeof(Vector3), 1, model_list_file) != 1
80
            || fread(&v2, sizeof(Vector3), 1, model_list_file) != 1)
81
        {
82
            VMAP_ERROR_LOG("misc", "File '%s' seems to be corrupted!", VMAP::GAMEOBJECT_MODELS);
83
            break;
84
        }
85

86
        if (v1.isNaN() || v2.isNaN())
87
        {
88
            VMAP_ERROR_LOG("misc", "File '%s' Model '%s' has invalid v1%s v2%s values!", VMAP::GAMEOBJECT_MODELS, std::string(buff, name_length).c_str(), v1.toString().c_str(), v2.toString().c_str());
89
            continue;
90
        }
91

92
        model_list.insert
93
        (
94
            ModelList::value_type(displayId, GameobjectModelData(std::string(buff, name_length), AABox(v1, v2), isWmo))
95
        );
96
    }
97

98
    fclose(model_list_file);
99
    VMAP_INFO_LOG("server.loading", ">> Loaded %u GameObject models in %u ms", uint32(model_list.size()), GetMSTimeDiffToNow(oldMSTime));
100
}
101

102
GameObjectModel::~GameObjectModel()
103
{
104
    if (iModel)
105
        ((VMAP::VMapManager2*)VMAP::VMapFactory::createOrGetVMapManager())->releaseModelInstance(name);
106
}
107

108
bool GameObjectModel::initialize(std::unique_ptr<GameObjectModelOwnerBase> modelOwner, std::string const& dataPath)
109
{
110
    ModelList::const_iterator it = model_list.find(modelOwner->GetDisplayId());
111
    if (it == model_list.end())
112
        return false;
113

114
    G3D::AABox mdl_box(it->second.bound);
115
    // ignore models with no bounds
116
    if (mdl_box == G3D::AABox::zero())
117
    {
118
        VMAP_ERROR_LOG("misc", "GameObject model %s has zero bounds, loading skipped", it->second.name.c_str());
119
        return false;
120
    }
121

122
    iModel = VMAP::VMapFactory::createOrGetVMapManager()->acquireModelInstance(dataPath + "vmaps/", it->second.name);
123

124
    if (!iModel)
125
        return false;
126

127
    name = it->second.name;
128
    iPos = modelOwner->GetPosition();
129
    phasemask = modelOwner->GetPhaseMask();
130
    iScale = modelOwner->GetScale();
131
    iInvScale = 1.f / iScale;
132

133
    G3D::Matrix3 iRotation = G3D::Matrix3::fromEulerAnglesZYX(modelOwner->GetOrientation(), 0, 0);
134
    iInvRot = iRotation.inverse();
135
    // transform bounding box:
136
    mdl_box = AABox(mdl_box.low() * iScale, mdl_box.high() * iScale);
137
    AABox rotated_bounds;
138
    for (int i = 0; i < 8; ++i)
139
        rotated_bounds.merge(iRotation * mdl_box.corner(i));
140

141
    iBound = rotated_bounds + iPos;
142
#ifdef SPAWN_CORNERS
143
    // test:
144
    for (int i = 0; i < 8; ++i)
145
    {
146
        Vector3 pos(iBound.corner(i));
147
        modelOwner->DebugVisualizeCorner(pos);
148
    }
149
#endif
150

151
    owner = std::move(modelOwner);
152
    isWmo = it->second.isWmo;
153
    return true;
154
}
155

156
GameObjectModel* GameObjectModel::Create(std::unique_ptr<GameObjectModelOwnerBase> modelOwner, std::string const& dataPath)
157
{
158
    GameObjectModel* mdl = new GameObjectModel();
159
    if (!mdl->initialize(std::move(modelOwner), dataPath))
160
    {
161
        delete mdl;
162
        return nullptr;
163
    }
164

165
    return mdl;
166
}
167

168
bool GameObjectModel::intersectRay(const G3D::Ray& ray, float& MaxDist, bool StopAtFirstHit, uint32 ph_mask, VMAP::ModelIgnoreFlags ignoreFlags) const
169
{
170
    if (!(phasemask & ph_mask) || !owner->IsSpawned())
171
        return false;
172

173
    float time = ray.intersectionTime(iBound);
174
    if (time == G3D::inf())
175
        return false;
176

177
    // child bounds are defined in object space:
178
    Vector3 p = iInvRot * (ray.origin() - iPos) * iInvScale;
179
    Ray modRay(p, iInvRot * ray.direction());
180
    float distance = MaxDist * iInvScale;
181
    bool hit = iModel->IntersectRay(modRay, distance, StopAtFirstHit, ignoreFlags);
182
    if (hit)
183
    {
184
        distance *= iScale;
185
        MaxDist = distance;
186
    }
187
    return hit;
188
}
189

190
void GameObjectModel::intersectPoint(G3D::Vector3 const& point, VMAP::AreaInfo& info, uint32 ph_mask) const
191
{
192
    if (!(phasemask & ph_mask) || !owner->IsSpawned() || !isMapObject())
193
        return;
194

195
    if (!iBound.contains(point))
196
        return;
197

198
    // child bounds are defined in object space:
199
    Vector3 pModel = iInvRot * (point - iPos) * iInvScale;
200
    Vector3 zDirModel = iInvRot * Vector3(0.f, 0.f, -1.f);
201
    float zDist;
202
    if (iModel->IntersectPoint(pModel, zDirModel, zDist, info))
203
    {
204
        Vector3 modelGround = pModel + zDist * zDirModel;
205
        float world_Z = ((modelGround * iInvRot) * iScale + iPos).z;
206
        if (info.ground_Z < world_Z)
207
            info.ground_Z = world_Z;
208
    }
209
}
210

211
bool GameObjectModel::GetLocationInfo(G3D::Vector3 const& point, VMAP::LocationInfo& info, uint32 ph_mask) const
212
{
213
    if (!(phasemask & ph_mask) || !owner->IsSpawned() || !isMapObject())
214
        return false;
215

216
    if (!iBound.contains(point))
217
        return false;
218

219
    // child bounds are defined in object space:
220
    Vector3 pModel = iInvRot * (point - iPos) * iInvScale;
221
    Vector3 zDirModel = iInvRot * Vector3(0.f, 0.f, -1.f);
222
    float zDist;
223
    if (iModel->GetLocationInfo(pModel, zDirModel, zDist, info))
224
    {
225
        Vector3 modelGround = pModel + zDist * zDirModel;
226
        float world_Z = ((modelGround * iInvRot) * iScale + iPos).z;
227
        if (info.ground_Z < world_Z)
228
        {
229
            info.ground_Z = world_Z;
230
            return true;
231
        }
232
    }
233

234
    return false;
235
}
236

237
bool GameObjectModel::GetLiquidLevel(G3D::Vector3 const& point, VMAP::LocationInfo& info, float& liqHeight) const
238
{
239
    // child bounds are defined in object space:
240
    Vector3 pModel = iInvRot * (point - iPos) * iInvScale;
241
    //Vector3 zDirModel = iInvRot * Vector3(0.f, 0.f, -1.f);
242
    float zDist;
243
    if (info.hitModel->GetLiquidLevel(pModel, zDist))
244
    {
245
        // calculate world height (zDist in model coords):
246
        // assume WMO not tilted (wouldn't make much sense anyway)
247
        liqHeight = zDist * iScale + iPos.z;
248
        return true;
249
    }
250
    return false;
251
}
252

253
bool GameObjectModel::UpdatePosition()
254
{
255
    if (!iModel)
256
        return false;
257

258
    ModelList::const_iterator it = model_list.find(owner->GetDisplayId());
259
    if (it == model_list.end())
260
        return false;
261

262
    G3D::AABox mdl_box(it->second.bound);
263
    // ignore models with no bounds
264
    if (mdl_box == G3D::AABox::zero())
265
    {
266
        VMAP_ERROR_LOG("misc", "GameObject model %s has zero bounds, loading skipped", it->second.name.c_str());
267
        return false;
268
    }
269

270
    iPos = owner->GetPosition();
271

272
    G3D::Matrix3 iRotation = G3D::Matrix3::fromEulerAnglesZYX(owner->GetOrientation(), 0, 0);
273
    iInvRot = iRotation.inverse();
274
    // transform bounding box:
275
    mdl_box = AABox(mdl_box.low() * iScale, mdl_box.high() * iScale);
276
    AABox rotated_bounds;
277
    for (int i = 0; i < 8; ++i)
278
        rotated_bounds.merge(iRotation * mdl_box.corner(i));
279

280
    iBound = rotated_bounds + iPos;
281
#ifdef SPAWN_CORNERS
282
    // test:
283
    for (int i = 0; i < 8; ++i)
284
    {
285
        Vector3 pos(iBound.corner(i));
286
        go.SummonCreature(1, pos.x, pos.y, pos.z, 0, TEMPSUMMON_MANUAL_DESPAWN);
287
    }
288
#endif
289

290
    return true;
291
}
292

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.