Skip to content

Commit 067a10d

Browse files
committed
xrGame/obstacles_query.cpp: cleanup
Thanks @abramcumner
1 parent 4038665 commit 067a10d

File tree

1 file changed

+34
-44
lines changed

1 file changed

+34
-44
lines changed

src/xrGame/obstacles_query.cpp

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
void obstacles_query::set_intersection(const obstacles_query& query)
2222
{
23-
u32 n = m_obstacles.size();
24-
u32 buffer_size = n * sizeof(OBSTACLES::value_type);
23+
// XXX: probably replace alloca
24+
const u32 n = m_obstacles.size();
25+
const u32 buffer_size = n * sizeof(OBSTACLES::value_type);
2526
OBSTACLES::value_type* temp = (OBSTACLES::value_type*)_alloca(buffer_size);
2627
memcpy(temp, &*obstacles().begin(), buffer_size);
2728
m_obstacles.erase(
@@ -36,14 +37,12 @@ void obstacles_query::set_intersection(const obstacles_query& query)
3637

3738
void obstacles_query::merge(const AREA& object_area)
3839
{
39-
u32 area_size = m_area.size();
40-
u32 destination_size = area_size + object_area.size();
41-
u32 buffer_size = destination_size * sizeof(u32);
42-
u32* temp = (u32*)_alloca(buffer_size);
43-
memcpy(temp, &*m_area.begin(), area_size * sizeof(u32));
40+
AREA temp(std::move(m_area));
41+
const u32 area_size = temp.size();
42+
const u32 destination_size = area_size + object_area.size();
4443
m_area.resize(destination_size);
4544
m_area.erase(
46-
std::set_union(temp, temp + area_size, object_area.begin(), object_area.end(), m_area.begin()), m_area.end());
45+
std::set_union(temp.begin(), temp.end(), object_area.begin(), object_area.end(), m_area.begin()), m_area.end());
4746
}
4847

4948
void obstacles_query::compute_area()
@@ -53,23 +52,20 @@ void obstacles_query::compute_area()
5352
m_area.clear();
5453

5554
m_crc = 0;
56-
OBSTACLES::iterator I = m_obstacles.begin();
57-
OBSTACLES::iterator E = m_obstacles.end();
58-
for (; I != E; ++I)
55+
56+
for (auto& it : m_obstacles)
5957
{
60-
ai_obstacle& obstacle = (*I).first->obstacle();
58+
ai_obstacle& obstacle = it.first->obstacle();
6159
merge(obstacle.area());
62-
(*I).second = obstacle.crc();
63-
m_crc ^= (*I).second;
60+
it.second = obstacle.crc();
61+
m_crc ^= it.second;
6462
}
6563
}
6664

6765
void obstacles_query::merge(const obstacles_query& query)
6866
{
69-
OBSTACLES::const_iterator I = query.obstacles().begin();
70-
OBSTACLES::const_iterator E = query.obstacles().end();
71-
for (; I != E; ++I)
72-
add((*I).first);
67+
for (const auto& it : query.obstacles())
68+
add(it.first);
7369
}
7470

7571
bool obstacles_query::merge(const Fvector& position, const float& radius, const obstacles_query& query)
@@ -85,27 +81,25 @@ bool obstacles_query::merge(const Fvector& position, const float& radius, const
8581
return (true);
8682
}
8783

88-
u32 crc_before = crc();
84+
const u32 crc_before = crc();
8985
compute_area();
90-
return (crc() != crc_before);
86+
return crc() != crc_before;
9187
}
9288

9389
bool obstacles_query::objects_changed(const Fvector& position, const float& radius) const
9490
{
95-
OBSTACLES::const_iterator I = obstacles().begin();
96-
OBSTACLES::const_iterator E = obstacles().end();
97-
for (; I != E; ++I)
91+
for (const auto& it : obstacles())
9892
{
99-
if ((*I).first->obstacle().crc() == (*I).second)
93+
if (it.first->obstacle().crc() == it.second)
10094
continue;
10195

102-
if ((*I).first->obstacle().distance_to(position) >= radius)
96+
if (it.first->obstacle().distance_to(position) >= radius)
10397
continue;
10498

105-
return (true);
99+
return true;
106100
}
107101

108-
return (false);
102+
return false;
109103
}
110104

111105
struct too_far_predicate
@@ -121,44 +115,40 @@ struct too_far_predicate
121115

122116
IC bool operator()(const std::pair<const CGameObject*, u32>& object) const
123117
{
124-
typedef obstacles_query::AREA AREA;
125-
const AREA& area = object.first->obstacle().area();
126-
AREA::const_iterator I = area.begin();
127-
AREA::const_iterator E = area.end();
128-
for (; I != E; ++I)
118+
for (const auto& it : object.first->obstacle().area())
129119
{
130-
Fvector vertex_position = ai().level_graph().vertex_position(*I);
120+
Fvector vertex_position = ai().level_graph().vertex_position(it);
131121
const float distance_sqr = vertex_position.distance_to_sqr(m_position);
132122
if (distance_sqr < m_radius_sqr)
133-
return (false);
123+
return false;
134124
}
135125

136-
return (true);
126+
return true;
137127
}
138128
};
139129

140130
bool obstacles_query::remove_objects(const Fvector& position, const float& radius)
141131
{
142132
update_objects(position, radius);
143133

144-
OBSTACLES::iterator I = std::remove_if(m_obstacles.begin(), m_obstacles.end(), too_far_predicate(position, radius));
134+
const auto iterator = std::remove_if(m_obstacles.begin(), m_obstacles.end(), too_far_predicate(position, radius));
145135

146-
if (I == m_obstacles.end())
147-
return (false);
136+
if (iterator == m_obstacles.end())
137+
return false;
148138

149-
m_obstacles.erase(I, m_obstacles.end());
139+
m_obstacles.erase(iterator, m_obstacles.end());
150140

151141
m_actual = false;
152-
u32 crc_before = crc();
142+
const u32 crc_before = crc();
153143
compute_area();
154-
return (crc_before != crc());
144+
return crc_before != crc();
155145
}
156146

157147
void obstacles_query::remove_links(IGameObject* object)
158148
{
159-
OBSTACLES::iterator I = m_obstacles.find(smart_cast<CGameObject*>(object));
160-
if (I == m_obstacles.end())
149+
const auto iterator = m_obstacles.find(smart_cast<CGameObject*>(object));
150+
if (iterator == m_obstacles.end())
161151
return;
162152

163-
m_obstacles.erase(I);
153+
m_obstacles.erase(iterator);
164154
}

0 commit comments

Comments
 (0)