From fbc4e88f8cee5c9612c1b1905bedbeb2faaef13d Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Fri, 12 Jan 2024 21:20:29 +0100 Subject: [PATCH] remove/refactor Directions::getBlock --- .../minosoft/data/direction/Directions.kt | 59 +------------------ .../minosoft/data/world/chunk/ChunkSection.kt | 55 ++++++++++++++++- .../world/chunk/neighbours/ChunkNeighbours.kt | 3 +- .../chunk/mesher/FluidSectionMesher.kt | 4 +- 4 files changed, 59 insertions(+), 62 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/direction/Directions.kt b/src/main/java/de/bixilon/minosoft/data/direction/Directions.kt index 696335ad9..440f83f0c 100644 --- a/src/main/java/de/bixilon/minosoft/data/direction/Directions.kt +++ b/src/main/java/de/bixilon/minosoft/data/direction/Directions.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -22,10 +22,7 @@ import de.bixilon.kutil.enums.ValuesEnum import de.bixilon.kutil.reflection.ReflectionUtil.forceSet import de.bixilon.kutil.reflection.ReflectionUtil.jvmField import de.bixilon.minosoft.data.Axes -import de.bixilon.minosoft.data.registries.blocks.state.BlockState -import de.bixilon.minosoft.data.world.chunk.ChunkSection import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.get -import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition enum class Directions( val vector: Vec3i, @@ -60,60 +57,6 @@ enum class Directions( return vector[axis] } - @Deprecated("outsource") - fun getBlock(x: Int, y: Int, z: Int, section: ChunkSection, neighbours: Array): BlockState? { - return when (this) { - DOWN -> { - if (y == 0) { - neighbours[Directions.O_DOWN]?.blocks?.let { it[x, ProtocolDefinition.SECTION_MAX_Y, z] } - } else { - section.blocks[x, y - 1, z] - } - } - - UP -> { - if (y == ProtocolDefinition.SECTION_MAX_Y) { - neighbours[Directions.O_UP]?.blocks?.let { it[x, 0, z] } - } else { - section.blocks[x, y + 1, z] - } - } - - NORTH -> { - if (z == 0) { - neighbours[Directions.O_NORTH]?.blocks?.let { it[x, y, ProtocolDefinition.SECTION_MAX_Z] } - } else { - section.blocks[x, y, z - 1] - } - } - - SOUTH -> { - if (z == ProtocolDefinition.SECTION_MAX_Z) { - neighbours[Directions.O_SOUTH]?.blocks?.let { it[x, y, 0] } - } else { - section.blocks[x, y, z + 1] - } - } - - WEST -> { - if (x == 0) { - neighbours[Directions.O_WEST]?.blocks?.let { it[ProtocolDefinition.SECTION_MAX_X, y, z] } - } else { - section.blocks[x - 1, y, z] - } - } - - EAST -> { - if (x == ProtocolDefinition.SECTION_MAX_X) { - neighbours[Directions.O_EAST]?.blocks?.let { it[0, y, z] } - } else { - section.blocks[x + 1, y, z] - } - } - } - } - - companion object : ValuesEnum { const val O_DOWN = 0 // Directions.DOWN.ordinal const val O_UP = 1 // Directions.UP.ordinal diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/ChunkSection.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/ChunkSection.kt index ad581ae71..065b7fb76 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/ChunkSection.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/ChunkSection.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.world.chunk import de.bixilon.kotlinglm.vec2.Vec2i import de.bixilon.kotlinglm.vec3.Vec3i +import de.bixilon.minosoft.data.direction.Directions import de.bixilon.minosoft.data.entities.block.BlockEntity import de.bixilon.minosoft.data.world.chunk.chunk.Chunk import de.bixilon.minosoft.data.world.chunk.light.SectionLight @@ -22,6 +23,7 @@ import de.bixilon.minosoft.data.world.container.biome.BiomeSectionDataProvider import de.bixilon.minosoft.data.world.container.block.BlockSectionDataProvider import de.bixilon.minosoft.gui.rendering.util.VecUtil.of import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection +import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import java.util.* /** @@ -68,6 +70,57 @@ class ChunkSection( blockEntities.clear() } + + fun traceBlock(x: Int, y: Int, z: Int, direction: Directions) = when (direction) { + Directions.DOWN -> { + if (y == 0) { + neighbours?.get(Directions.O_DOWN)?.blocks?.let { it[x, ProtocolDefinition.SECTION_MAX_Y, z] } + } else { + blocks[x, y - 1, z] + } + } + + Directions.UP -> { + if (y == ProtocolDefinition.SECTION_MAX_Y) { + neighbours?.get(Directions.O_UP)?.blocks?.let { it[x, 0, z] } + } else { + blocks[x, y + 1, z] + } + } + + Directions.NORTH -> { + if (z == 0) { + neighbours?.get(Directions.O_NORTH)?.blocks?.let { it[x, y, ProtocolDefinition.SECTION_MAX_Z] } + } else { + blocks[x, y, z - 1] + } + } + + Directions.SOUTH -> { + if (z == ProtocolDefinition.SECTION_MAX_Z) { + neighbours?.get(Directions.O_SOUTH)?.blocks?.let { it[x, y, 0] } + } else { + blocks[x, y, z + 1] + } + } + + Directions.WEST -> { + if (x == 0) { + neighbours?.get(Directions.O_WEST)?.blocks?.let { it[ProtocolDefinition.SECTION_MAX_X, y, z] } + } else { + blocks[x - 1, y, z] + } + } + + Directions.EAST -> { + if (x == ProtocolDefinition.SECTION_MAX_X) { + neighbours?.get(Directions.O_EAST)?.blocks?.let { it[0, y, z] } + } else { + blocks[x + 1, y, z] + } + } + } + companion object { inline val Vec3i.index: Int get() = getIndex(x, y, z) diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt index ceafb4e90..c5377bf4e 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -161,6 +161,7 @@ class ChunkNeighbours(val chunk: Chunk) : Iterable { return trace(chunkOffset)?.get(x, y, z) } + companion object { const val COUNT = 8 const val NORTH = 3 diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/mesher/FluidSectionMesher.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/mesher/FluidSectionMesher.kt index 21b45c3e1..51d0cbfb9 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/mesher/FluidSectionMesher.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/chunk/mesher/FluidSectionMesher.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2023 Moritz Zwerger + * Copyright (C) 2020-2024 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. * @@ -87,7 +87,7 @@ class FluidSectionMesher( val height = fluid.getHeight(state) fun isSideCovered(direction: Directions): Boolean { - val neighbour = direction.getBlock(x, y, z, section, neighbours) ?: return false + val neighbour = section.traceBlock(x, y, z, direction) ?: return false if (fluid.matches(neighbour)) { return true