| 653 | } |
| 654 | |
| 655 | void WorldStorage::loadSectorToLevel(Sector const& sector, SectorLoadLevel targetLoadLevel) { |
| 656 | if (!m_tileArray->sectorValid(sector)) |
| 657 | return; |
| 658 | |
| 659 | auto entityFactory = Root::singleton().entityFactory(); |
| 660 | |
| 661 | auto& metadata = m_sectorMetadata[sector]; |
| 662 | if (metadata.loadLevel >= targetLoadLevel) |
| 663 | return; |
| 664 | |
| 665 | metadata.timeToLive = randomizedSectorTTL(); |
| 666 | |
| 667 | for (uint8_t i = (uint8_t)metadata.loadLevel + 1; i <= (uint8_t)targetLoadLevel; ++i) { |
| 668 | SectorLoadLevel currentLoad = (SectorLoadLevel)i; |
| 669 | SectorLoadLevel stepDownLoad = (SectorLoadLevel)(i - 1); |
| 670 | |
| 671 | if (stepDownLoad != SectorLoadLevel::None) { |
| 672 | for (auto adjacentSector : adjacentSectors(sector)) |
| 673 | loadSectorToLevel(adjacentSector, stepDownLoad); |
| 674 | } |
| 675 | |
| 676 | if (currentLoad == SectorLoadLevel::Tiles) { |
| 677 | if (auto res = m_db.find(tileSectorKey(sector))) { |
| 678 | TileSectorStore sectorStore = readTileSector(*res); |
| 679 | |
| 680 | m_tileArray->loadSector(sector, std::move(sectorStore.tiles)); |
| 681 | |
| 682 | metadata.generationLevel = sectorStore.generationLevel; |
| 683 | } else { |
| 684 | if (!m_tileArray->sectorLoaded(sector)) |
| 685 | m_tileArray->loadDefaultSector(sector); |
| 686 | } |
| 687 | |
| 688 | metadata.loadLevel = currentLoad; |
| 689 | m_generatorFacade->sectorLoadLevelChanged(this, sector, currentLoad); |
| 690 | |
| 691 | } else if (currentLoad == SectorLoadLevel::Entities) { |
| 692 | List<EntityPtr> addedEntities; |
| 693 | if (auto res = m_db.find(entitySectorKey(sector))) { |
| 694 | EntitySectorStore sectorStore = readEntitySector(*res); |
| 695 | for (auto const& entityStore : sectorStore) { |
| 696 | try { |
| 697 | addedEntities.append(entityFactory->loadVersionedEntity(entityStore)); |
| 698 | } catch (std::exception const& e) { |
| 699 | Logger::warn("Failed to deserialize entity: {}", outputException(e, true)); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | UniqueIndexStore readUniques; |
| 705 | for (auto const& entity : addedEntities) { |
| 706 | m_generatorFacade->initEntity(this, m_entityMap->reserveEntityId(), entity); |
| 707 | m_entityMap->addEntity(entity); |
| 708 | if (auto uniqueId = entity->uniqueId()) |
| 709 | readUniques.add(*uniqueId, {sector, entity->position()}); |
| 710 | } |
| 711 | |
| 712 | // Update the stored unique ids on load, in case a desync has happened |
nothing calls this directly
no test coverage detected