-
Notifications
You must be signed in to change notification settings - Fork 146
/
CMiniMem.cpp
225 lines (178 loc) · 5.11 KB
/
CMiniMem.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
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
#undef clamp
#include <algorithm>
#include "hud.h"
#include "cl_util.h"
#include "particleman.h"
#include "particleman_internal.h"
#include "CMiniMem.h"
void* CMiniMem::Allocate(std::size_t sizeInBytes, std::size_t alignment)
{
auto particle = reinterpret_cast<CBaseParticle*>(_pool.allocate(sizeInBytes, alignment));
if (nullptr != particle)
{
_particles.push_back(particle);
}
return particle;
}
void CMiniMem::Deallocate(void* memory, std::size_t sizeInBytes, std::size_t alignment)
{
if (!memory)
{
return;
}
_particles.erase(std::find(_particles.begin(), _particles.end(), memory));
_pool.deallocate(memory, sizeInBytes, alignment);
}
void CMiniMem::Shutdown()
{
delete _instance;
_instance = nullptr;
}
CMiniMem* CMiniMem::Instance()
{
if (!_instance)
{
_instance = new CMiniMem();
}
return _instance;
}
void CMiniMem::ProcessAll()
{
const float time = gEngfuncs.GetClientTime();
//Clear list of visible particles.
_visibleParticles = 0;
//Divide the particle list in two: the list of visible particles and the list of invisible particles.
//Remove any particles that have died.
std::size_t invisibleCount = 0;
for (std::size_t i = 0; i < (_particles.size() - invisibleCount);)
{
auto effect = _particles[i];
if (!IsGamePaused())
{
effect->Think(time);
}
if (0 != effect->m_flDieTime && time >= effect->m_flDieTime)
{
effect->Die();
delete effect;
//Don't do this! operator delete removes the effect from the list.
//_particles.erase(_particles.begin() + i);
continue;
}
if (effect->CheckVisibility())
{
auto player = gEngfuncs.GetLocalPlayer();
effect->SetPlayerDistance((player->origin - effect->m_vOrigin).LengthSquared());
++_visibleParticles;
}
else
{
if (i + invisibleCount < _particles.size())
{
//There is an effect we haven't checked yet.
//Put the invisible effect at the end of the list and check the other effect next.
std::swap(_particles[i], _particles[_particles.size() - 1 - invisibleCount]);
++invisibleCount;
continue;
}
//No more unchecked effects in the list.
}
++i;
}
std::sort(_particles.begin(), _particles.begin() + _visibleParticles, [](const auto& lhs, const auto& rhs)
{
//Particles are ordered farthest to nearest so they can be drawn in order.
const float lhsDistance = lhs->GetPlayerDistance();
const float rhsDistance = rhs->GetPlayerDistance();
return lhsDistance > rhsDistance;
});
for (std::size_t i = 0; i < _visibleParticles; ++i)
{
auto effect = _particles[i];
effect->Draw();
}
g_flOldTime = time;
}
int CMiniMem::ApplyForce(Vector vOrigin, Vector vDirection, float flRadius, float flStrength)
{
const float radiusSquared = flRadius * flRadius;
for (auto effect : _particles)
{
if (!effect->m_bAffectedByForce)
{
continue;
}
const float size = effect->m_flSize / 5;
const Vector mins = effect->m_vOrigin - Vector{size, size, size};
const Vector maxs = effect->m_vOrigin + Vector{size, size, size};
//If the force origin lies outside the effect's bounding box, calculate the distance from the box.
float totalDistanceSquared = 0;
for (int i = 0; i < 3; ++i)
{
float boundingValue;
if (vOrigin[i] < mins[i])
{
boundingValue = mins[i];
}
else if (vOrigin[i] > maxs[i])
{
boundingValue = maxs[i];
}
else
{
continue;
}
totalDistanceSquared += (vOrigin[i] - boundingValue) * (vOrigin[i] - boundingValue);
}
//Effect is further away from position than force radius, don't apply force.
if (totalDistanceSquared > radiusSquared)
{
continue;
}
const float strength = std::max(0.f, flStrength - (vOrigin - effect->m_vOrigin).Length() * (flStrength / (0.5f * radiusSquared)));
if (vDirection == g_vecZero)
{
const float acceleration = -(strength / effect->m_flMass);
const Vector direction = (vOrigin - effect->m_vOrigin).Normalize();
const Vector velocity = effect->m_vVelocity.Normalize();
effect->m_vVelocity = acceleration * (direction + velocity);
}
else
{
const float acceleration = strength / effect->m_flMass;
const Vector direction = vDirection.Normalize();
const Vector velocity = effect->m_vVelocity.Normalize();
effect->m_vVelocity = acceleration * (direction + velocity);
}
effect->Force();
}
return 1;
}
void CMiniMem::Reset()
{
_visibleParticles = 0;
for (auto particle : _particles)
{
particle->Die();
delete particle;
}
//Cleared by CBaseParticle::operator delete
//_particles.clear();
//Wipe away previously allocated memory so maps with loads of particles don't eat up memory forever.
_pool.release();
_particles.shrink_to_fit();
}