Logo Search packages:      
Sourcecode: earth3d version File versions  Download package

MapPart MapTileTreeNode::getNeighbor ( int  direction  ) 

get neighbor on higher or same level

Definition at line 158 of file mapTileTreeNode.cpp.

References getChild(), getChildnr(), getNeighbor(), getSimpleTileCoordinates(), getSimpleTileDirections(), and getThisChildnr().

Referenced by getNeighbor(), and getSelectedNeighbor().

                                                  {
  if (!getParent()) return(MapPart(0, this, Point2DInt(0,0))); // loop the root tile

  /* test which child this tile is */
  Point2DInt ownposition = getSimpleTileCoordinates(getThisChildnr());

  /* if we can reach the neighbor directly using the parent then do so */
  Point2DInt godirection = getSimpleTileDirections(direction);
  Point2DInt newposition = ownposition + godirection;
  if (newposition.x>=0 && newposition.x<=1 &&
      newposition.y>=0 && newposition.y<=1) {
    MapTileTreeNode *result = getParent()->getChild(getChildnr(newposition));
    if (result==NULL) { // child does not exist, use parent and difference level of 1
      return(MapPart(1, getParent(), newposition));
    }
    else { // child does exist, use it with level difference of 0
      return(MapPart(0, getParent()->getChild(getChildnr(newposition)), Point2DInt(0,0)));
    }
  }

  /* otherwise we need to get the right neighbor from the parent tile */
  MapPart parentNeighbor = getParent()->getNeighbor(direction);

  /* from the parentNeighbor we need the child that is in the opposite direction */
  Point2DInt searchposition = ownposition + getSimpleTileDirections((direction + 2) % 4);
  assert(searchposition.x>=0 && searchposition.x<=1 && searchposition.y>=0 && searchposition.y<=1);

//   /* by getting this parent neighbor, we moved two times in that direction
//      and need to correct our search position accordingly */
//   newposition = newposition - godirection*2;

  if (parentNeighbor.node==NULL) { // neighbor does not exist
    return(MapPart(0, NULL, Point2DInt(0,0)));
  }

  if (parentNeighbor.level==0 && !parentNeighbor.node->getSelectedForDrawing()) { // more childs exist
    /* now get this child */
    MapTileTreeNode *result = parentNeighbor.node->getChild(getChildnr(searchposition));
    if (result) { // has a child
      return(MapPart(0, result, Point2DInt(0,0)));
    }
    else { // has no child, use parent
      return(MapPart(1, parentNeighbor.node, searchposition));
    }
  }
  else { // there was already a child missing so the parent was used
    parentNeighbor.offset *= 2;
    parentNeighbor.offset += searchposition;
    return(MapPart(parentNeighbor.level+1, parentNeighbor.node, parentNeighbor.offset));
  }
}


Generated by  Doxygen 1.6.0   Back to index