Skip to content

Commit 0c90ab4

Browse files
osjcSmallJoker
authored andcommittedApr 7, 2019
Optimize random turns in dungeongen (#8129)
It turns out there is no need to return the new value and preserve the old one in random_turn, the procedure can be made to modify the value in-place. This saves quite a bunch of parameter and return value copying.
1 parent 25f231a commit 0c90ab4

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed
 

‎src/mapgen/dungeongen.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ void DungeonGen::makeCorridor(v3s16 doorplace, v3s16 doordir,
494494
if (partcount >= partlength) {
495495
partcount = 0;
496496

497-
dir = random_turn(random, dir);
497+
random_turn(random, dir);
498498

499499
partlength = random.range(1, length);
500500

@@ -655,20 +655,19 @@ v3s16 turn_xz(v3s16 olddir, int t)
655655
}
656656

657657

658-
v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
658+
void random_turn(PseudoRandom &random, v3s16 &dir)
659659
{
660660
int turn = random.range(0, 2);
661-
v3s16 dir;
662-
if (turn == 0)
663-
// Go straight
664-
dir = olddir;
665-
else if (turn == 1)
661+
if (turn == 0) {
662+
// Go straight: nothing to do
663+
return;
664+
} else if (turn == 1) {
666665
// Turn right
667-
dir = turn_xz(olddir, 0);
668-
else
666+
dir = turn_xz(dir, 0);
667+
} else {
669668
// Turn left
670-
dir = turn_xz(olddir, 1);
671-
return dir;
669+
dir = turn_xz(dir, 1);
670+
}
672671
}
673672

674673

‎src/mapgen/dungeongen.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class NodeDefManager;
3434

3535
v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs);
3636
v3s16 turn_xz(v3s16 olddir, int t);
37-
v3s16 random_turn(PseudoRandom &random, v3s16 olddir);
37+
void random_turn(PseudoRandom &random, v3s16 &dir);
3838
int dir_to_facedir(v3s16 d);
3939

4040

0 commit comments

Comments
 (0)
Please sign in to comment.