Skip to content

Commit d677f29

Browse files
adridonerzhul
authored andcommittedDec 10, 2017
Use std::vector instead of dynamic C-Array (#6744)
1 parent da298a2 commit d677f29

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed
 

‎src/mapgen/mapgen.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,8 @@ void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
301301

302302

303303
void Mapgen::getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
304-
s16 *floors, s16 *ceilings, u16 *num_floors, u16 *num_ceilings)
304+
std::vector<s16> &floors, std::vector<s16> &ceilings)
305305
{
306-
u16 floor_i = 0;
307-
u16 ceiling_i = 0;
308306
const v3s16 &em = vm->m_area.getExtent();
309307

310308
bool is_walkable = false;
@@ -318,19 +316,14 @@ void Mapgen::getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
318316
is_walkable = ndef->get(mn).walkable;
319317

320318
if (is_walkable && !walkable_above) {
321-
floors[floor_i] = y;
322-
floor_i++;
319+
floors.push_back(y);
323320
} else if (!is_walkable && walkable_above) {
324-
ceilings[ceiling_i] = y + 1;
325-
ceiling_i++;
321+
ceilings.push_back(y + 1);
326322
}
327323

328324
vm->m_area.add_y(em, vi, -1);
329325
walkable_above = is_walkable;
330326
}
331-
332-
*num_floors = floor_i;
333-
*num_ceilings = ceiling_i;
334327
}
335328

336329

‎src/mapgen/mapgen.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Mapgen {
194194
s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax);
195195
void updateHeightmap(v3s16 nmin, v3s16 nmax);
196196
void getSurfaces(v2s16 p2d, s16 ymin, s16 ymax,
197-
s16 *floors, s16 *ceilings, u16 *num_floors, u16 *num_ceilings);
197+
std::vector<s16> &floors, std::vector<s16> &ceilings);
198198

199199
void updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax);
200200

‎src/mapgen/mg_decoration.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include "log.h"
2727
#include "util/numeric.h"
2828
#include <algorithm>
29+
#include <vector>
2930

3031

3132
FlagDesc flagdesc_deco[] = {
@@ -186,18 +187,16 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
186187

187188
// Get all floors and ceilings in node column
188189
u16 size = (nmax.Y - nmin.Y + 1) / 2;
189-
s16 floors[size];
190-
s16 ceilings[size];
191-
u16 num_floors = 0;
192-
u16 num_ceilings = 0;
190+
std::vector<s16> floors;
191+
std::vector<s16> ceilings;
192+
floors.reserve(size);
193+
ceilings.reserve(size);
193194

194-
mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y,
195-
floors, ceilings, &num_floors, &num_ceilings);
195+
mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
196196

197-
if ((flags & DECO_ALL_FLOORS) && num_floors > 0) {
197+
if (flags & DECO_ALL_FLOORS) {
198198
// Floor decorations
199-
for (u16 fi = 0; fi < num_floors; fi++) {
200-
s16 y = floors[fi];
199+
for (const s16 y : floors) {
201200
if (y < y_min || y > y_max)
202201
continue;
203202

@@ -208,10 +207,9 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
208207
}
209208
}
210209

211-
if ((flags & DECO_ALL_CEILINGS) && num_ceilings > 0) {
210+
if (flags & DECO_ALL_CEILINGS) {
212211
// Ceiling decorations
213-
for (u16 ci = 0; ci < num_ceilings; ci++) {
214-
s16 y = ceilings[ci];
212+
for (const s16 y : ceilings) {
215213
if (y < y_min || y > y_max)
216214
continue;
217215

0 commit comments

Comments
 (0)
Please sign in to comment.