MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / generateTrees

Function generateTrees

src/OpenLoco/src/Map/MapGenerator/MapGenerator.cpp:651–725  ·  view source on GitHub ↗

0x004BDA49

Source from the content-addressed store, hash-verified

649
650 // 0x004BDA49
651 static void generateTrees()
652 {
653 const auto& options = Scenario::getOptions();
654
655 // Place forests
656 for (auto i = 0; i < options.numberOfForests; ++i)
657 {
658 const auto randRadius = ((gPrng1().randNext(255) * std::max(options.maxForestRadius - options.minForestRadius, 0)) / 255 + options.minForestRadius) * kTileSize;
659 const auto randLoc = World::TilePos2(gPrng1().randNext(kMapRows), gPrng1().randNext(kMapColumns));
660 const auto randDensity = (gPrng1().randNext(15) * std::max(options.maxForestDensity - options.minForestDensity, 0)) / 15 + options.minForestDensity;
661 placeTreeCluster(randLoc, randRadius, randDensity, std::nullopt);
662
663 if (TileManager::numFreeElements() < 0x1B000)
664 {
665 break;
666 }
667 }
668
669 // Place a number of random trees
670 for (auto i = 0; i < options.numberRandomTrees; ++i)
671 {
672 const auto randLoc = World::Pos2(gPrng1().randNext(kMapWidth), gPrng1().randNext(kMapHeight));
673 placeRandomTree(randLoc, std::nullopt);
674 }
675
676 // Cull trees that are too high / low
677 uint32_t randMask = gPrng1().randNext();
678 uint32_t i = 0;
679 std::vector<TileElement*> toBeRemoved;
680 for (auto& loc : getWorldRange())
681 {
682 auto tile = TileManager::get(loc);
683 for (auto& el : tile)
684 {
685 auto* elTree = el.as<TreeElement>();
686 if (elTree == nullptr)
687 {
688 continue;
689 }
690
691 if (elTree->baseHeight() / kMicroToSmallZStep <= options.minAltitudeForTrees)
692 {
693 if (elTree->baseHeight() / kMicroToSmallZStep != options.minAltitudeForTrees
694 || (randMask & (1 << i)))
695 {
696 toBeRemoved.push_back(&el);
697 i++;
698 i %= 32;
699 break;
700 }
701 }
702
703 if (elTree->baseHeight() / kMicroToSmallZStep >= options.maxAltitudeForTrees)
704 {
705 if (elTree->baseHeight() / kMicroToSmallZStep != options.maxAltitudeForTrees
706 || (randMask & (1 << i)))
707 {
708 toBeRemoved.push_back(&el);

Callers 1

generateFunction · 0.85

Calls 11

placeTreeClusterFunction · 0.85
numFreeElementsFunction · 0.85
placeRandomTreeFunction · 0.85
getWorldRangeFunction · 0.85
updateTreeSeasonsFunction · 0.85
baseHeightMethod · 0.80
push_backMethod · 0.80
getFunction · 0.50
removeElementFunction · 0.50
randNextMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected