/
contact.cpp
271 lines (228 loc) · 6.6 KB
/
contact.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/** @file contact.cpp World object => BSP leaf "contact" and contact lists.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_platform.h"
#include "world/contact.h"
#include "world/map.h"
#include "world/p_object.h"
#include "BspLeaf"
#include "ConvexSubspace"
#include <de/Error>
#include <de/memoryzone.h>
using namespace de;
ContactType Contact::type() const
{
return _type;
}
void *Contact::objectPtr() const
{
return _object;
}
Vector3d Contact::objectOrigin() const
{
switch(_type)
{
case ContactLumobj: return objectAs<Lumobj>().origin();
case ContactMobj: return Mobj_Origin(objectAs<mobj_t>());
default:
DENG2_ASSERT(false);
return Vector3d();
}
}
double Contact::objectRadius() const
{
switch(_type)
{
case ContactLumobj: return objectAs<Lumobj>().radius();
case ContactMobj: return Mobj_VisualRadius(objectAs<mobj_t>());
default:
DENG2_ASSERT(false);
return 0;
}
}
AABoxd Contact::objectAABox() const
{
switch(_type)
{
case ContactLumobj: return objectAs<Lumobj>().aaBox();
case ContactMobj: return Mobj_AABox(objectAs<mobj_t>());
default:
DENG2_ASSERT(false);
return AABoxd();
}
}
BspLeaf &Contact::objectBspLeafAtOrigin() const
{
switch(_type)
{
case ContactLumobj: return objectAs<Lumobj>().bspLeafAtOrigin();
case ContactMobj: return Mobj_BspLeafAtOrigin(objectAs<mobj_t>());
default:
throw Error("Contact::objectBspLeafAtOrigin", "Invalid type");
}
}
struct ContactList::Node
{
Node *next; ///< Next in the BSP leaf.
Node *nextUsed; ///< Next used contact.
void *obj;
};
static ContactList::Node *firstNode; ///< First unused list node.
static ContactList::Node *cursor; ///< Current list node.
void ContactList::reset() // static
{
cursor = firstNode;
}
void ContactList::link(Contact *contact)
{
if(!contact) return;
Node *node = newNode(contact->objectPtr());
node->next = _head;
_head = node;
}
ContactList::Node *ContactList::begin() const
{
return _head;
}
ContactList::Node *ContactList::newNode(void *object) // static
{
DENG2_ASSERT(object != 0);
Node *node;
if(!cursor)
{
node = (Node *) Z_Malloc(sizeof(*node), PU_APPSTATIC, 0);
// Link in the global list of used nodes.
node->nextUsed = firstNode;
firstNode = node;
}
else
{
node = cursor;
cursor = cursor->nextUsed;
}
node->obj = object;
node->next = 0;
return node;
}
// Separate contact lists for each BSP leaf and contact type.
static ContactList *subspaceContactLists;
ContactList &R_ContactList(ConvexSubspace &subspace, ContactType type)
{
return subspaceContactLists[subspace.indexInMap() * ContactTypeCount + int( type )];
}
static Contact *contacts;
static Contact *contactFirst, *contactCursor;
static Contact *newContact(void *object, ContactType type)
{
DENG2_ASSERT(object != 0);
Contact *contact;
if(!contactCursor)
{
contact = (Contact *) Z_Malloc(sizeof *contact, PU_APPSTATIC, 0);
// Link in the global list of used contacts.
contact->nextUsed = contactFirst;
contactFirst = contact;
}
else
{
contact = contactCursor;
contactCursor = contactCursor->nextUsed;
}
// Link in the list of in-use contacts.
contact->next = contacts;
contacts = contact;
contact->_object = object;
contact->_type = type;
return contact;
}
void R_InitContactLists(Map &map)
{
// Initialize object => BspLeaf contact lists.
subspaceContactLists = (ContactList *)
Z_Calloc(map.subspaceCount() * ContactTypeCount * sizeof(*subspaceContactLists),
PU_MAPSTATIC, 0);
}
void R_DestroyContactLists()
{
if(subspaceContactLists)
{
Z_Free(subspaceContactLists);
subspaceContactLists = 0;
}
}
void R_ClearContactLists(Map &map)
{
// Start reusing contacts.
contactCursor = contactFirst;
contacts = 0;
// Start reusing nodes from the first one in the list.
ContactList::reset();
if(subspaceContactLists)
{
std::memset(subspaceContactLists, 0,
map.subspaceCount() * ContactTypeCount * sizeof(*subspaceContactLists));
}
}
void R_AddContact(mobj_t &mobj)
{
// BspLeafs with no geometry cannot be contacted (zero world volume).
if(Mobj_BspLeafAtOrigin(mobj).hasSubspace())
{
newContact(&mobj, ContactMobj);
}
}
void R_AddContact(Lumobj &lum)
{
// BspLeafs with no geometry cannot be contacted (zero world volume).
if(lum.bspLeafAtOrigin().hasSubspace())
{
newContact(&lum, ContactLumobj);
}
}
int R_ContactIterator(int (*callback) (Contact &, void *), void *context)
{
// Link contacts into the relevant blockmap.
for(Contact *contact = contacts; contact; contact = contact->next)
{
if(int result = callback(*contact, context))
return result;
}
return false; // Continue iteration.
}
int R_SubspaceMobjContactIterator(ConvexSubspace &subspace,
int (*callback)(mobj_s &, void *), void *context)
{
ContactList &list = R_ContactList(subspace, ContactMobj);
for(ContactList::Node *node = list.begin(); node; node = node->next)
{
if(int result = callback(*static_cast<mobj_t *>(node->obj), context))
return result;
}
return false; // Continue iteration.
}
int R_SubspaceLumobjContactIterator(ConvexSubspace &subspace,
int (*callback)(Lumobj &, void *), void *context)
{
ContactList &list = R_ContactList(subspace, ContactLumobj);
for(ContactList::Node *node = list.begin(); node; node = node->next)
{
if(int result = callback(*static_cast<Lumobj *>(node->obj), context))
return result;
}
return false; // Continue iteration.
}