bool Inventory::addItem(Item *item, int x, int y) { if (item == NULL) { return false; } if (hasItem(item)) { return true; } if (hasSpaceFor(item, x, y)) { placeItem(item, x, y); return true; } return false; }
static void initialPlacement(struct chrGapList *bounding, struct bed *placed) /* simply place the items from the 'placed' list into the gaps * defined by the 'bounding' list, the locations of the 'placed' * items are what they say they are in their bed structure. */ { struct bed *bedEl; int unplacedCount = 0; for (bedEl = placed; bedEl != NULL; bedEl = bedEl->next) { struct chrGapList *cl; boolean placedOK = FALSE; for (cl = bounding; cl != NULL; cl = cl->next) { struct gap *gl; if (differentWord(cl->chrom, bedEl->chrom)) continue; for (gl = cl->gList; gl != NULL; gl = gl->next) { if ( ((gl->downstream != NULL) && (gl->upstream != NULL)) && ((bedEl->chromEnd <= gl->downstream->chromStart) && (bedEl->chromStart >= gl->upstream->chromEnd)) ) { placeItem(bedEl, gl); placedOK = TRUE; verbose(5,"item: %s:%d-%d, gap: %s:%d-%d\n", bedEl->chrom, bedEl->chromStart, bedEl->chromEnd, bedEl->chrom, gl->upstream->chromEnd, gl->downstream->chromStart); break; } } } if (! placedOK) { verbose(3,"can not place item: %s:%d-%d\n", bedEl->chrom, bedEl->chromStart, bedEl->chromEnd); ++unplacedCount; } } if (unplacedCount) verbose(2,"Could not place %d items\n", unplacedCount); }
bool Inventory::addItem(Item *item) { if (item == NULL) { return false; } if (hasItem(item)) { return true; } int x = 0; int y = 0; if (findSpotFor(item, x, y)) { placeItem(item, x, y); return true; } return false; }
Region::Region(int w, int h, RoomType type) { if (!initgen) { // Initialise probdist - but only once std::random_device rd; gen = std::mt19937(rd()); probdist = std::uniform_real_distribution<double>(0, 1); initgen = true; } width = w; height = h; this->type = type; switch (type) { case RoomType::Room: for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { Point tp = Point(x, y); points[tp] = Background::TiledFloor; if (probdist(gen) < GOLD_PROB) placeItem(tp, ItemType::Gold); if (probdist(gen) < STAFF_PROB) placeItem(tp, ItemType::Staff); if (probdist(gen) < CHEST_PROB) placeItem(tp, ItemType::Chest); } points[Point(x, -1)] = Background::StoneWall; points[Point(x, h)] = Background::StoneWall; } for (int y = -1; y <=h; y++) { points[Point(-1, y)] = Background::StoneWall; points[Point(w, y)] = Background::StoneWall; } // points[Point(5, 3)] = Background::DirtWall; { idist = std::uniform_int_distribution<int>(4, 4 + (w + h) / 2); int maxconnections = idist(gen); numConnections = 0; idist = std::uniform_int_distribution<int>(0, 4); for (uint8_t i = 0; i < 4; i++) { if (addrandomemptyconnection((Direction)(i))) numConnections++; } for (uint8_t i = 4; i < maxconnections; i++) { if (addrandomemptyconnection((Direction)(idist(gen)))) numConnections++; } // printf("%d->%d\n", maxconnections, numConnections); } break; case RoomType::Corridor: for (int x = -1; x <= w; x++) for (int y = -1; y <= h; y++) points[Point(x, y)] = Background::StoneWall; { std::uniform_int_distribution<int> ydist(0, h - 1); int y = ydist(gen); for (int x = 0; x < w; x++) points[Point(x, y)] = Background::TiledFloor; addrandomemptyconnection(Direction::Left, Point(-1, y)); addrandomemptyconnection(Direction::Right, Point(w, y)); } { std::uniform_int_distribution<int> xdist(0, w - 1); int x = xdist(gen); for (int y = 0; y < h; y++) points[Point(x, y)] = Background::TiledFloor; addrandomemptyconnection(Direction::Up, Point(x, -1)); addrandomemptyconnection(Direction::Down, Point(x, h)); } break; case RoomType::Spiral: for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { Point tp = Point(x, y); points[tp] = Background::TiledFloor; // if (probdist(gen) < GOLD_PROB) // placeItem(tp, ItemType::Gold); // if (probdist(gen) < STAFF_PROB) // placeItem(tp, ItemType::Staff); // if (probdist(gen) < CHEST_PROB) // placeItem(tp, ItemType::Chest); } points[Point(x, -1)] = Background::StoneWall; points[Point(x, h)] = Background::StoneWall; } for (int y = -1; y <=h; y++) { points[Point(-1, y)] = Background::StoneWall; points[Point(w, y)] = Background::StoneWall; } if (h >= 3 && w >= 3){ // Draw spiral int layers = (MIN(w, h) - 2) / 4; int layercount = 0; Point currentlyDrawing = {1,0}; Direction drawdir = Direction::Down; auto contLine = [layers, &layercount, w, h, &drawdir, this](Point pt) -> bool { // printf("%d\n", (layercount + 1) * 2); switch (drawdir) { case Direction::Down: return pt.second < h - (layercount + 1) * 2; case Direction::Up: return pt.second >= (layercount + 1) * 2; case Direction::Left: return pt.first >= (layercount + 2) * 2; // Because want to actually spiral case Direction::Right: // printf("%d\n", w - (layercount + 1) * 2); return pt.first < w - (layercount + 1) * 2; default: fprintf(stderr, "Direction weirdness here\n"); return false; } }; auto turnDir = [&layercount, &drawdir]() { switch (drawdir) { case Direction::Down: drawdir = Direction::Right; break; case Direction::Up: drawdir = Direction::Left; break; case Direction::Left: drawdir = Direction::Down; layercount++; break; case Direction::Right: drawdir = Direction::Up; break; default: fprintf(stderr, "Direction weirdness here\n"); break; } }; while (layercount < layers && currentlyDrawing.second >= 0 && currentlyDrawing.first >= 0) { // printf("Printing at %d:%d\n", currentlyDrawing.first, currentlyDrawing.second); points[currentlyDrawing] = Background::StoneWall; if (!contLine(currentlyDrawing)) { turnDir(); // printf("Turning to direction %d; layercount = %d\n", (int)drawdir, layercount); } currentlyDrawing = PAIR_SUM(currentlyDrawing, DISPLACEMENT(drawdir)); } points[Point(1, 3)] = Background::Door; } { addrandomemptyconnection(Direction::Up, {0, -1}); idist = std::uniform_int_distribution<int>(4, 4 + (w + h) / 2); int maxconnections = idist(gen); numConnections = 0; idist = std::uniform_int_distribution<int>(0, 4); for (uint8_t i = 0; i < 4 && i < maxconnections; i++) { if (addrandomemptyconnection((Direction)(i))) numConnections++; } for (uint8_t i = 4; i < maxconnections; i++) { if (addrandomemptyconnection((Direction)(idist(gen)))) numConnections++; } } break; default: fprintf(stderr, "Unimplemented RoomType\n"); break; } }