Skip to content

Commit

Permalink
support for splitting sections into multiple regions of length-y (#86)
Browse files Browse the repository at this point in the history
* support for splitting sections into multiple regions of length-y

* Updates

* remove volume add length

* remove length func

Co-authored-by: Scyu_ <[email protected]>
  • Loading branch information
Boy0000 and DevScyu authored Nov 2, 2021
1 parent 2a10c18 commit 8be98e1
Show file tree
Hide file tree
Showing 46 changed files with 61 additions and 60 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ group=com.derongan.minecraft
version=0.3
kotlinVersion=1.5.31
serverVersion=1.17.1-R0.1-SNAPSHOT
idofrontVersion=0.7.35
idofrontConventions=1.5.31-35
idofrontVersion=0.7.38
idofrontConventions=1.5.31-38
18 changes: 0 additions & 18 deletions src/main/java/com/derongan/minecraft/deeperworld/world/Point.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.mineinabyss.idofront.commands.execution.ExperimentalCommandDSL
import com.mineinabyss.idofront.plugin.registerEvents
import com.mineinabyss.idofront.plugin.registerService
import com.mineinabyss.idofront.slimjar.IdofrontSlimjar
import com.mineinabyss.idofront.slimjar.LibraryLoaderInjector
import com.okkero.skedule.schedule
import org.bukkit.Material
import org.bukkit.plugin.java.JavaPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.derongan.minecraft.deeperworld.config

import com.mineinabyss.idofront.time.TimeSpan
import com.mineinabyss.idofront.time.TimeSpanSerializer
import com.mineinabyss.idofront.time.ticks
import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.derongan.minecraft.deeperworld.config

import com.mineinabyss.idofront.serialization.WorldSerializer
import com.mineinabyss.idofront.time.TimeSpan
import com.mineinabyss.idofront.time.TimeSpanSerializer
import com.mineinabyss.idofront.time.seconds
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface WorldManager {
* @param world The world
* @return The section or null if the location is not within a section.
*/
fun getSectionFor(x: Int, z: Int, world: World): Section?
fun getSectionFor(x: Int, y: Int, z: Int, world: World): Section?

/**
* Gets the section associated with the provided key
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/derongan/minecraft/deeperworld/world/Point.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.derongan.minecraft.deeperworld.world

/**
* Represents a single X/Y/Z cube in a minecraft world.
*/
data class CubePoint(val x: Int, val y: Int, val z: Int) {
operator fun plus(other: CubePoint) = CubePoint(x + other.x, y + other.y, z + other.z)

operator fun minus(other: CubePoint) = CubePoint(x - other.x, y - other.y, z - other.z)

operator fun div(o: Float) = CubePoint((x / o).toInt(), (y / o).toInt(), (z / o).toInt())

operator fun div(o: Int) = CubePoint(x / o, y / o, z / o)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,48 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.math.max
import kotlin.math.min

/**
* Represents a region of the world. Contains all Y values.
* Represents a region of the world.
*/
@Serializable(with = RegionSerializer::class)
class Region(val a: Point, val b: Point) {
constructor(ax: Int, az: Int, bx: Int, bz: Int) : this(Point(ax, az), Point(bx, bz))
class Region(val a: CubePoint, val b: CubePoint) {
constructor(ax: Int, ay: Int, az: Int, bx: Int, by: Int, bz: Int) : this(
CubePoint(ax, ay, az),
CubePoint(bx, by, bz)
)

val center: Point get() = a.plus(b).div(2)
val center: CubePoint get() = a.plus(b).div(2)

fun contains(x: Int, z: Int): Boolean = x in min(a.x, b.x)..max(a.x, b.x) && z in min(a.z, b.z)..max(a.z, b.z)
fun contains(x: Int, y: Int, z: Int): Boolean =
x in min(a.x, b.x)..max(a.x, b.x) &&
y in min(a.y, b.y)..max(a.y, b.y) &&
z in min(a.z, b.z)..max(a.z, b.z)

operator fun contains(p: Point) = contains(p.x, p.z)
operator fun contains(p: CubePoint) = contains(p.x, p.y, p.z)
}

object RegionSerializer : KSerializer<Region> {
private val serializer = ListSerializer(Int.serializer())
override val descriptor: SerialDescriptor = serializer.descriptor

override fun serialize(encoder: Encoder, value: Region) {
val (x, y) = value.a
val (x2, y2) = value.b
encoder.encodeSerializableValue(serializer, listOf(x, y, x2, y2))
val (x, y, z) = value.a
val (x2, y2, z2) = value.b
encoder.encodeSerializableValue(serializer, listOf(x, y, z, x2, y2, z2))
}

override fun deserialize(decoder: Decoder): Region {
val (x, y, x2, y2) = decoder.decodeSerializableValue(serializer)
return Region(x, y, x2, y2)
val (x, y, z, x2, y2, z2) = decoder.decodeSerializableValue(serializer)
return Region(x, y, z, x2, y2, z2)
}
}
}

private operator fun <E> List<E>.component6(): E = this[5]


Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class WorldManagerImpl(config: FileConfiguration) : WorldManager {
override fun unregisterSection(key: SectionKey) = TODO()

override fun getSectionFor(location: Location): Section? {
return getSectionFor(location.blockX, location.blockZ, location.world!!)
return getSectionFor(location.blockX, location.blockY, location.blockZ, location.world!!)
}

override fun getSectionFor(x: Int, z: Int, world: World): Section? = //TODO consider performance
sectionMap.values.firstOrNull { it.world == world && it.region.contains(x, z) }
override fun getSectionFor(x: Int, y: Int, z: Int, world: World): Section? = //TODO consider performance
sectionMap.values.firstOrNull { it.world == world && it.region.contains(x, y, z) }

override fun getSectionFor(key: SectionKey) = sectionMap[key]
override fun getSectionFor(key: String) = sectionMap[CustomSectionKey(key)]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.derongan.minecraft.deeperworld.world

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class RegionTestU {
@Test
fun contains() {
val region = Region(-1, 0, -1, 10, 10, 10)
assertTrue(region.contains(-1, 0, -1))
assertTrue(region.contains(10, 0, 10))
assertTrue(region.contains(8, 2, 5))
assertFalse(region.contains(-2, 5, 5))
assertFalse(region.contains(11, -3, 5))
assertFalse(region.contains(5, 5, -2))
assertFalse(region.contains(5, -7, 11))
}
}

0 comments on commit 8be98e1

Please sign in to comment.