void QuadTree::add_entities_in_range(Entity * e, float dist, std::vector<Entity*> & res) { draw_p(); if(circle_intersect_rect(e->pos, dist, pos, size)) { if(entity != nullptr) { add_entities(res, e); return; } for(int i = 0; i < 4; ++i) { if(children[i] != nullptr) { children[i]->add_entities_in_range(e, dist, res); } } } }
// Recursive city generator. Accepts 'depth' to determine // the segmentation granularity. void generate(int depth) { // determine the number of segments to cut vertically and horizontally int vRooms = randNext(ROOM_MIN_SEGMENTS, ROOM_MAX_SEGMENTS); int hRooms = randNext(ROOM_MIN_SEGMENTS, ROOM_MAX_SEGMENTS); // determine the size of each segment int vSize = city.getYSize() / vRooms; int hSize = city.getXSize() / hRooms; // often the factorization might not be clean // so determine where the vertical oversized segment should go. int *vSegments = new int[vRooms]; for (int i = 0; i < vRooms; i++) vSegments[i] = vSize; int vIndex = randNext(0, vRooms); vSegments[vIndex] = vSize + city.getYSize() - (vSize * vRooms); // often the factorization might not be clean // so determine where the horizontal oversized segment should go. int *hSegments = new int[hRooms]; for (int i = 0; i < hRooms; i++) hSegments[i] = hSize; int hIndex = randNext(0, hRooms); hSegments[hIndex] = hSize + city.getXSize() - (hSize * hRooms); // Start placing rooms recursively for (int i = 0, top = 0; i < vRooms; i++) { for (int j = 0, left = 0; j < hRooms; j++) { // determine the coordinates of the room int height = vSegments[i], width = hSegments[j]; int actualTop = top, actualLeft = left; // this is to avoid double walling each room // if removed each room will sit inside a previous bound // hence adding a wall for each level of depth if (i != 0) { actualTop = top - 1; height += 1; } if (j != 0) { actualLeft = left - 1; width += 1; } // create room properties, this determines what kind of room, if it has doors // where the doors are, orientation, etc RoomProperties rp(actualTop, actualLeft, height, width); // determine if the room is a boundry for smaller rooms if (rp.isCompound) { // build more rooms inside the boundary generate(rp, depth - 1); } else { // construct the room using the room properties build_room(rp); } left += hSegments[j]; } top += vSegments[i]; } // construct the perimeter wall build_perimeter_wall(); // add RED entities at a random position add_entities(Security::Mask::RED, 2, 4); // add ORANGE entities at a random position add_entities(Security::Mask::ORANGE, 1, 1); // change doors to ground finalize_doors(); // find room locations // TODO: this can be removed( see comment on method) find_rooms(); delete[] vSegments; delete[] hSegments; }