forked from mangoszero/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectModel.cpp
More file actions
216 lines (179 loc) · 6.32 KB
/
GameObjectModel.cpp
File metadata and controls
216 lines (179 loc) · 6.32 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* MaNGOS is a full featured server for World of Warcraft, supporting
* the following clients: 1.12.x, 2.4.3, 3.3.5a, 4.3.4a and 5.4.8
*
* Copyright (C) 2005-2019 MaNGOS project <https://getmangos.eu>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* World of Warcraft, and all World of Warcraft or Warcraft art, images,
* and lore are copyrighted by Blizzard Entertainment, Inc.
*/
#include "VMapFactory.h"
#include "VMapManager2.h"
#include "VMapDefinitions.h"
#include "WorldModel.h"
#include "GameObject.h"
#include "World.h"
#include "GameObjectModel.h"
#include "DBCStores.h"
struct GameobjectModelData
{
GameobjectModelData(const std::string& name_, const G3D::AABox& box) :
name(name_), bound(box) {}
std::string name;
G3D::AABox bound;
};
typedef UNORDERED_MAP<uint32, GameobjectModelData> ModelList;
ModelList model_list;
void LoadGameObjectModelList()
{
FILE* model_list_file = fopen((sWorld.GetDataPath() + "vmaps/" + VMAP::GAMEOBJECT_MODELS).c_str(), "rb");
if (!model_list_file)
{ return; }
uint32 name_length, displayId;
char buff[500];
while (!feof(model_list_file))
{
if (fread(&displayId, sizeof(uint32), 1, model_list_file) <= 0)
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
if (fread(&name_length, sizeof(uint32), 1, model_list_file) <= 0)
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
if (name_length >= sizeof(buff))
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
if (fread(&buff, sizeof(char), name_length, model_list_file) <= 0)
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
G3D::Vector3 v1, v2;
if (fread(&v1, sizeof(G3D::Vector3), 1, model_list_file) <= 0)
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
if (fread(&v2, sizeof(G3D::Vector3), 1, model_list_file) <= 0)
{
sLog.outDebug("File %s seems to be corrupted", VMAP::GAMEOBJECT_MODELS);
break;
}
model_list.insert(ModelList::value_type(displayId, GameobjectModelData(std::string(buff, name_length), AABox(v1, v2))));
}
fclose(model_list_file);
}
GameObjectModel::~GameObjectModel()
{
if (iModel)
{ ((VMAP::VMapManager2*)VMAP::VMapFactory::createOrGetVMapManager())->releaseModelInstance(iName); }
}
bool GameObjectModel::initialize(const GameObject* const pGo, const GameObjectDisplayInfoEntry* const pDisplayInfo)
{
ModelList::const_iterator it = model_list.find(pDisplayInfo->Displayid);
if (it == model_list.end())
{ return false; }
iModelBound = it->second.bound;
// ignore models with no bounds
if (iModelBound == G3D::AABox::zero())
{
sLog.outDebug("Model %s has zero bounds, loading skipped", it->second.name.c_str());
return false;
}
iModel = ((VMAP::VMapManager2*)VMAP::VMapFactory::createOrGetVMapManager())->acquireModelInstance(sWorld.GetDataPath() + "vmaps/", it->second.name);
if (!iModel)
{ return false; }
iOwner = pGo;
iName = it->second.name;
iPos = Vector3(pGo->GetPositionX(), pGo->GetPositionY(), pGo->GetPositionZ());
iScale = pGo->GetObjectScale();
iInvScale = 1.f / iScale;
G3D::Quat q;
pGo->GetQuaternion(q);
UpdateRotation(q);
return true;
}
void GameObjectModel::UpdateRotation(G3D::Quat const& q)
{
iQuat = q;
G3D::AABox mdl_box(iModelBound);
// transform bounding box:
mdl_box = AABox(mdl_box.low() * iScale, mdl_box.high() * iScale);
G3D::AABox rotated_bounds;
for (int i = 0; i < 8; ++i)
{ rotated_bounds.merge((iQuat * G3D::Quat(mdl_box.corner(i)) * iQuat.conj()).imag()); }
iBound = rotated_bounds + iPos;
}
GameObjectModel* GameObjectModel::Create(const GameObject* const pGo)
{
const GameObjectDisplayInfoEntry* info = sGameObjectDisplayInfoStore.LookupEntry(pGo->GetDisplayId());
if (!info)
{ return NULL; }
GameObjectModel* mdl = new GameObjectModel();
if (!mdl->initialize(pGo, info))
{
delete mdl;
return NULL;
}
return mdl;
}
bool GameObjectModel::IntersectRay(const G3D::Ray& ray, float& MaxDist, bool StopAtFirstHit) const
{
if (!isCollidable)
{ return false; }
float time = ray.intersectionTime(iBound);
if (time == G3D::inf())
{ return false; }
// child bounds are defined in object space:
Vector3 p = (iQuat.conj() * G3D::Quat((ray.origin() - iPos) * iInvScale) * iQuat).imag();
Ray modRay(p, (iQuat.conj() * G3D::Quat(ray.direction()) * iQuat).imag());
float distance = MaxDist * iInvScale;
bool hit = iModel->IntersectRay(modRay, distance, StopAtFirstHit);
if (hit)
{
distance *= iScale;
MaxDist = distance;
}
return hit;
}
bool GameObjectModel::GetIntersectPoint(const G3D::Vector3& srcPoint, G3D::Vector3& dstPoint, bool absolute) const
{
G3D::Vector3 p;
if (absolute)
p = (iQuat.conj() * G3D::Quat((srcPoint - iPos) * iInvScale) * iQuat).imag();
else
p = srcPoint;
float dist;
bool hit = iModel->GetContactPoint(p, G3D::Vector3(0.0f, 0.0f, -1.0f), dist);
if (hit)
{
dstPoint = p;
dstPoint.z -= dist;
}
return hit;
}
void GameObjectModel::GetLocalCoords(const G3D::Vector3& worldCoords, G3D::Vector3& localCoords)
{
}
void GameObjectModel::GetWorldCoords(const G3D::Vector3& localCoords, G3D::Vector3& worldCoords)
{
}