MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / expandRegionInLayer

Method expandRegionInLayer

source/game/StarWorldLayout.cpp:649–792  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

647WorldLayout::WorldLayer::WorldLayer() : yStart(0) {}
648
649pair<WorldLayout::WorldLayer, List<RectI>> WorldLayout::expandRegionInLayer(WorldLayout::WorldLayer targetLayer, size_t cellIndex, int newWidth) const {
650 struct RegionCell {
651 int lBound;
652 int rBound;
653 WorldRegionPtr region;
654 };
655
656 // auto printRegionCells = [](List<RegionCell> const& cells) {
657 // String output = "";
658 // for (auto cell : cells)
659 // output += strf("[{} {}] ", cell.lBound, cell.rBound);
660 // return output;
661 // };
662
663 // Logger::info("expanding region in layer with cellIndex {} newWidth {}", cellIndex, newWidth);
664
665 List<RectI> regionRects;
666
667 if (targetLayer.cells.size() == 1) {
668 Logger::info("Cannot expand region as it already fills the layer");
669 return {targetLayer, regionRects};
670 }
671
672 // Logger::info("boundaries before expansion are {}", targetLayer.boundaries);
673
674 // TODO: this is a messy way to get the top of the layer, but maybe it's ok
675 int layerTop = (int)m_worldSize[1];
676 for (size_t i = 0; i < m_layers.size(); ++i) {
677 if (m_layers[i].yStart == targetLayer.yStart && m_layers.size() > i + 1) {
678 layerTop = m_layers[i + 1].yStart;
679 break;
680 }
681 }
682
683 // if the region is going to cover the full layer width, this is much simpler
684 if (newWidth == (int)m_worldSize[0]) {
685 targetLayer.cells = {targetLayer.cells[cellIndex]};
686 targetLayer.boundaries = {};
687
688 regionRects.append(RectI{0, targetLayer.yStart, (int)m_worldSize[0], layerTop});
689 } else {
690 auto targetRegion = targetLayer.cells[cellIndex];
691
692 // convert cells and boundaries into something more tractable
693 List<RegionCell> targetCells;
694 List<RegionCell> otherCells;
695
696 int lastBoundary = 0;
697 size_t lastCellIndex = targetLayer.cells.size() - 1;
698 for (size_t i = 0; i <= lastCellIndex; ++i) {
699 int nextBoundary = i == lastCellIndex ? (int)m_worldSize[0] : targetLayer.boundaries[i];
700 if (i == cellIndex ||
701 (i == 0 && cellIndex == lastCellIndex && targetLayer.cells[i] == targetRegion) ||
702 (cellIndex == 0 && i == lastCellIndex && targetLayer.cells[i] == targetRegion))
703
704 targetCells.append(RegionCell{lastBoundary, nextBoundary, targetLayer.cells[i]});
705 else
706 otherCells.append(RegionCell{lastBoundary, nextBoundary, targetLayer.cells[i]});

Callers

nothing calls this directly

Calls 5

appendAllMethod · 0.80
sizeMethod · 0.45
appendMethod · 0.45
sortMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected