world visibility graph: remove dynamic allocation

This commit is contained in:
Moritz Zwerger 2025-02-08 19:39:36 +01:00
parent 128ced029d
commit d2dfe2b7f6
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2024 Moritz Zwerger * Copyright (C) 2020-2025 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -251,39 +251,42 @@ class WorldVisibilityGraph(
val section = chunk.sections.getOrNull(sectionIndex)?.blocks val section = chunk.sections.getOrNull(sectionIndex)?.blocks
val nextPosition = Vec2i() val positionX = chunkPosition.x
val positionY = chunkPosition.y
if (directionX <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.WEST) != true) && chunkPosition.x > chunkMin.x) { if (directionX <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.WEST) != true) && chunkPosition.x > chunkMin.x) {
nextPosition.x = chunkPosition.x - 1; nextPosition.y = chunkPosition.y // + WEST chunkPosition.x = positionX - 1; chunkPosition.y = positionY // + WEST
val nextChunk = chunk.neighbours[ChunkNeighbours.WEST] val nextChunk = chunk.neighbours[ChunkNeighbours.WEST]
if (nextChunk != null) { if (nextChunk != null) {
val nextVisibilities = getVisibility(nextPosition) ?: return val nextVisibilities = getVisibility(chunkPosition) ?: return
if (!nextVisibilities[visibilitySectionIndex]) { if (!nextVisibilities[visibilitySectionIndex]) {
nextVisibilities[visibilitySectionIndex] = true nextVisibilities[visibilitySectionIndex] = true
checkSection(nextPosition, sectionIndex, nextChunk, nextVisibilities, Directions.WEST, -1, directionY, directionZ, false) checkSection(chunkPosition, sectionIndex, nextChunk, nextVisibilities, Directions.WEST, -1, directionY, directionZ, false)
} }
} }
} }
if (directionX >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.EAST) != true) && chunkPosition.x < chunkMax.x) { if (directionX >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.EAST) != true) && chunkPosition.x < chunkMax.x) {
nextPosition.x = chunkPosition.x + 1; nextPosition.y = chunkPosition.y // + EAST chunkPosition.x = positionX + 1; chunkPosition.y = positionY // + EAST
val nextChunk = chunk.neighbours[ChunkNeighbours.EAST] val nextChunk = chunk.neighbours[ChunkNeighbours.EAST]
if (nextChunk != null) { if (nextChunk != null) {
val nextVisibilities = getVisibility(nextPosition) ?: return val nextVisibilities = getVisibility(chunkPosition) ?: return
if (!nextVisibilities[visibilitySectionIndex]) { if (!nextVisibilities[visibilitySectionIndex]) {
nextVisibilities[visibilitySectionIndex] = true nextVisibilities[visibilitySectionIndex] = true
checkSection(nextPosition, sectionIndex, nextChunk, nextVisibilities, Directions.EAST, 1, directionY, directionZ, false) checkSection(chunkPosition, sectionIndex, nextChunk, nextVisibilities, Directions.EAST, 1, directionY, directionZ, false)
} }
} }
} }
if (sectionIndex > 0 && directionY <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.DOWN) != true)) { if (sectionIndex > 0 && directionY <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.DOWN) != true)) {
chunkPosition.x = positionX; chunkPosition.y = positionY
if (!visibilities[visibilitySectionIndex - 1]) { if (!visibilities[visibilitySectionIndex - 1]) {
visibilities[visibilitySectionIndex - 1] = true visibilities[visibilitySectionIndex - 1] = true
checkSection(chunkPosition, sectionIndex - 1, chunk, visibilities, Directions.DOWN, directionX, -1, directionZ, false) checkSection(chunkPosition, sectionIndex - 1, chunk, visibilities, Directions.DOWN, directionX, -1, directionZ, false)
} }
} }
if (sectionIndex < maxIndex && directionY >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.UP) != true)) { if (sectionIndex < maxIndex && directionY >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.UP) != true)) {
chunkPosition.x = positionX; chunkPosition.y = positionY
if (!visibilities[visibilitySectionIndex + 1]) { if (!visibilities[visibilitySectionIndex + 1]) {
visibilities[visibilitySectionIndex + 1] = true visibilities[visibilitySectionIndex + 1] = true
checkSection(chunkPosition, sectionIndex + 1, chunk, visibilities, Directions.UP, directionX, 1, directionZ, false) checkSection(chunkPosition, sectionIndex + 1, chunk, visibilities, Directions.UP, directionX, 1, directionZ, false)
@ -291,25 +294,25 @@ class WorldVisibilityGraph(
} }
if (directionZ <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.NORTH) != true) && chunkPosition.y > chunkMin.y) { if (directionZ <= 0 && (section?.occlusion?.isOccluded(inverted, Directions.NORTH) != true) && chunkPosition.y > chunkMin.y) {
nextPosition.x = chunkPosition.x; nextPosition.y = chunkPosition.y - 1 // + NORTH chunkPosition.x = positionX; chunkPosition.y = positionY - 1 // + NORTH
val nextChunk = chunk.neighbours[ChunkNeighbours.NORTH] val nextChunk = chunk.neighbours[ChunkNeighbours.NORTH]
if (nextChunk != null) { if (nextChunk != null) {
val nextVisibilities = getVisibility(nextPosition) ?: return val nextVisibilities = getVisibility(chunkPosition) ?: return
if (!nextVisibilities[visibilitySectionIndex]) { if (!nextVisibilities[visibilitySectionIndex]) {
nextVisibilities[visibilitySectionIndex] = true nextVisibilities[visibilitySectionIndex] = true
checkSection(nextPosition, sectionIndex, nextChunk, nextVisibilities, Directions.NORTH, directionX, directionY, -1, false) checkSection(chunkPosition, sectionIndex, nextChunk, nextVisibilities, Directions.NORTH, directionX, directionY, -1, false)
} }
} }
} }
if (directionZ >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.SOUTH) != true) && chunkPosition.y < chunkMax.y) { if (directionZ >= 0 && (section?.occlusion?.isOccluded(inverted, Directions.SOUTH) != true) && chunkPosition.y < chunkMax.y) {
nextPosition.x = chunkPosition.x; nextPosition.y = chunkPosition.y + 1 // + SOUTH chunkPosition.x = positionX; chunkPosition.y = positionY + 1 // + SOUTH
val nextChunk = chunk.neighbours[ChunkNeighbours.SOUTH] val nextChunk = chunk.neighbours[ChunkNeighbours.SOUTH]
if (nextChunk != null) { if (nextChunk != null) {
val nextVisibilities = getVisibility(nextPosition) ?: return val nextVisibilities = getVisibility(chunkPosition) ?: return
if (!nextVisibilities[visibilitySectionIndex]) { if (!nextVisibilities[visibilitySectionIndex]) {
nextVisibilities[visibilitySectionIndex] = true nextVisibilities[visibilitySectionIndex] = true
checkSection(nextPosition, sectionIndex, nextChunk, nextVisibilities, Directions.SOUTH, directionX, directionY, 1, false) checkSection(chunkPosition, sectionIndex, nextChunk, nextVisibilities, Directions.SOUTH, directionX, directionY, 1, false)
} }
} }
} }