-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace.cpp
144 lines (139 loc) · 3.33 KB
/
trace.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
#pragma optimize("gtp",on)
#include <string.h>
#include "engine/wrect.h"
#include "engine/cl_dll.h"
#include "common/com_model.h"
#include "engine/cl_entity.h"
#include "trace.h"
#include "misc/defs.h"
#undef DotProduct
//===================================================================================
mnode_t *GetNodeFromPoint(float *point)
{
struct cl_entity_s *ent;
mnode_t *node;
float d;
mplane_t *plane;
ent = gEngfuncs.GetEntityByIndex(0);
if (!ent || !ent->model)
return NULL;
node = ent->model->nodes;
while (node)
{
if (node->contents < 0)
{
if (!node->parent)
return NULL;
return node->parent;
}
plane = node->plane;
if (!plane)
return NULL;
d = DotProd(point, plane->normal) - plane->dist;
if (d > 0)
node = node->children[0];
else
node = node->children[1];
}
return NULL;
}
//===================================================================================
mleaf_t *GetLeafFromPoint(float *point)
{
struct cl_entity_s *ent;
mnode_t *node;
float d;
mplane_t *plane;
ent = gEngfuncs.GetEntityByIndex(0);
if (!ent || !ent->model)
return NULL;
node = ent->model->nodes;
while (node)
{
if (node->contents < 0)
return (mleaf_t *)node;
plane = node->plane;
d = DotProd(point, plane->normal) - plane->dist;
if (d > 0)
node = node->children[0];
else
node = node->children[1];
}
return NULL;
}
//===================================================================================
void TraceThickness(float *start, float *end, float thickness, strace_t *tr)
{
mleaf_t *startleaf, *endleaf, *prevleaf;
int numsteps, count = 0;
float move[3], step[3], position[3];
float stepdist, depth = 0;
memset(tr, 0, sizeof(strace_t));
if ((start[0] < -4095) || (start[0] > 4095) || (start[1] < -4095) || (start[1] > 4095) || (start[2] < -4095) || (start[2] > 4095))
{
tr->hitsky = true;
tr->startsolid = true;
tr->finished = false;
tr->fraction = 0.0f;
return;
}
startleaf = GetLeafFromPoint(start);
endleaf = GetLeafFromPoint(end);
if (startleaf->contents == CONTENTS_SOLID)
{
tr->startsolid = true;
VectorCopy(start, tr->endpos);
tr->finished = false;
if (endleaf->contents == CONTENTS_SOLID)
tr->allsolid = true;
}
else
{
tr->startsolid = false;
tr->allsolid = false;
}
VectorSubtract(end, start, move);
tr->dist = (float)VectorLength(move);
if (tr->startsolid)
return;
if (startleaf == endleaf)
{
tr->finished = true;
tr->fraction = 1.0f;
VectorCopy(end, tr->endpos);
}
if (tr->dist > 1.0f)
numsteps = (int)tr->dist;
else
numsteps = 1;
VectorScale(move, 1.0f / (float)numsteps, step);
stepdist = (float)VectorLength(step);
VectorCopy(start, position);
endleaf = NULL;
for (;numsteps;numsteps--)
{
VectorAdd(position, step, position);
prevleaf = endleaf;
endleaf = GetLeafFromPoint(position);
if (prevleaf != endleaf && endleaf->contents == CONTENTS_SOLID)
count++;
if (count == 1 && endleaf->contents == CONTENTS_SOLID)
depth += stepdist;
if (endleaf->contents == CONTENTS_SKY)
tr->hitsky = true;
if (count > 1 || depth > thickness || tr->hitsky)
{
VectorCopy(position, tr->endpos);
VectorSubtract(position, start, move);
tr->fraction = (float)VectorLength(move) / tr->dist;
tr->finished = false;
return;
}
}
if (numsteps == 0)
{
tr->finished = true;
tr->fraction = 1.0f;
VectorCopy(end, tr->endpos);
}
}