some optimisations to searchNear*

This commit is contained in:
darkrose 2014-09-24 00:38:24 +10:00
parent 6d355b4aab
commit 2c6f916c4e
1 changed files with 37 additions and 4 deletions

View File

@ -732,16 +732,33 @@ static void getMob_dungeon_master(Settings &properties)
bool ServerEnvironment::searchNear(v3s16 pos, v3s16 radius_min, v3s16 radius_max, std::vector<content_t> c, v3s16 *found) bool ServerEnvironment::searchNear(v3s16 pos, v3s16 radius_min, v3s16 radius_max, std::vector<content_t> c, v3s16 *found)
{ {
v3s16 blockpos = getNodeBlockPos(pos);
MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
if (block == NULL)
return false;
v3s16 relpos = blockpos*MAP_BLOCKSIZE;
v3s16 p;
v3s16 bp;
for(s16 x=-radius_min.X; x<=radius_max.X; x++) { for(s16 x=-radius_min.X; x<=radius_max.X; x++) {
for(s16 y=-radius_min.Y; y<=radius_max.Y; y++) { for(s16 y=-radius_min.Y; y<=radius_max.Y; y++) {
for(s16 z=-radius_min.Z; z<=radius_max.Z; z++) { for(s16 z=-radius_min.Z; z<=radius_max.Z; z++) {
if (!x && !y && !z) if (!x && !y && !z)
continue; continue;
MapNode n_test = m_map->getNodeNoEx(pos+v3s16(x,y,z)); p = pos+v3s16(x,y,z);
MapNode n_test;
bp = getNodeBlockPos(p);
if (p == blockpos) {
n_test = block->getNodeNoCheck(p-relpos);
}else{
MapBlock *b = m_map->getBlockNoCreateNoEx(bp);
if (b)
n_test = b->getNodeNoEx(p-(bp*MAP_BLOCKSIZE));
}
for (std::vector<content_t>::iterator i=c.begin(); i != c.end(); i++) { for (std::vector<content_t>::iterator i=c.begin(); i != c.end(); i++) {
if (n_test.getContent() == *i) { if (n_test.getContent() == *i) {
if (found != NULL) if (found != NULL)
*found = pos+v3s16(x,y,z); *found = p;
return true; return true;
} }
} }
@ -753,12 +770,28 @@ bool ServerEnvironment::searchNear(v3s16 pos, v3s16 radius_min, v3s16 radius_max
bool ServerEnvironment::searchNearInv(v3s16 pos, v3s16 radius_min, v3s16 radius_max, std::vector<content_t> c, v3s16 *found) bool ServerEnvironment::searchNearInv(v3s16 pos, v3s16 radius_min, v3s16 radius_max, std::vector<content_t> c, v3s16 *found)
{ {
v3s16 blockpos = getNodeBlockPos(pos);
MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
if (block == NULL)
return false;
v3s16 relpos = blockpos*MAP_BLOCKSIZE;
v3s16 p;
v3s16 bp;
for(s16 x=radius_min.X; x<=radius_max.X; x++) { for(s16 x=radius_min.X; x<=radius_max.X; x++) {
for(s16 y=radius_min.Y; y<=radius_max.Y; y++) { for(s16 y=radius_min.Y; y<=radius_max.Y; y++) {
for(s16 z=radius_min.Z; z<=radius_max.Z; z++) { for(s16 z=radius_min.Z; z<=radius_max.Z; z++) {
if (!x && !y && !z) if (!x && !y && !z)
continue; continue;
MapNode n_test = m_map->getNodeNoEx(pos+v3s16(x,y,z)); p = pos+v3s16(x,y,z);
MapNode n_test;
bp = getNodeBlockPos(p);
if (p == blockpos) {
n_test = block->getNodeNoCheck(p-relpos);
}else{
MapBlock *b = m_map->getBlockNoCreateNoEx(bp);
if (b)
n_test = b->getNodeNoEx(p-(bp*MAP_BLOCKSIZE));
}
bool s = false; bool s = false;
for (std::vector<content_t>::iterator i=c.begin(); i != c.end(); i++) { for (std::vector<content_t>::iterator i=c.begin(); i != c.end(); i++) {
if (n_test.getContent() == *i) { if (n_test.getContent() == *i) {
@ -768,7 +801,7 @@ bool ServerEnvironment::searchNearInv(v3s16 pos, v3s16 radius_min, v3s16 radius_
} }
if (!s) { if (!s) {
if (found != NULL) if (found != NULL)
*found = pos+v3s16(x,y,z); *found = p;
return true; return true;
} }
} }