Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Per play spawn category caps
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Jan 18, 2022
1 parent 04f7d89 commit 05875a1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.mineinabyss.mobzy.ecs.components

import com.mineinabyss.geary.minecraft.access.toGeary
import com.mineinabyss.idofront.nms.aliases.NMSCreatureType
import com.mineinabyss.idofront.nms.aliases.toNMS
import com.mineinabyss.idofront.nms.entity.creatureType
import com.mineinabyss.idofront.typealiases.BukkitEntity
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -18,3 +22,6 @@ enum class MobCategory {

fun NMSCreatureType.toMobCategory(): MobCategory =
MobCategory.valueOf(this.name)

val BukkitEntity.mobCategory
get() = toGeary().get(MobCategory::class) ?: toNMS().entityType.creatureType.toMobCategory()
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mineinabyss.geary.ecs.accessors.building.get
import com.mineinabyss.geary.ecs.api.autoscan.AutoScan
import com.mineinabyss.geary.ecs.api.autoscan.Handler
import com.mineinabyss.geary.ecs.api.systems.GearyListener
import com.mineinabyss.idofront.nms.aliases.NMSEntityType
import com.mineinabyss.mobzy.spawning.SpawnType
import com.mineinabyss.mobzy.spawning.vertical.SpawnInfo
import kotlinx.serialization.SerialName
Expand Down Expand Up @@ -33,5 +34,5 @@ class CapFull : GearyListener() {

@Handler
fun TargetScope.check(event: EventScope): Boolean =
(event.spawnInfo.localMobs[spawnType.prefab.toEntity()?.get()]?.get() ?: 0) < conf.max
(event.spawnInfo.localTypes[spawnType.prefab.toEntity()?.get<NMSEntityType<*>>()] ?: 0) < conf.max
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.mobzy.spawning.conditions.components

import com.mineinabyss.geary.ecs.accessors.EventScope
import com.mineinabyss.geary.ecs.accessors.TargetScope
import com.mineinabyss.geary.ecs.accessors.building.get
import com.mineinabyss.geary.ecs.api.autoscan.AutoScan
Expand All @@ -8,6 +9,7 @@ import com.mineinabyss.geary.ecs.api.systems.GearyListener
import com.mineinabyss.mobzy.ecs.components.MobCategory
import com.mineinabyss.mobzy.spawning.MobCountManager
import com.mineinabyss.mobzy.spawning.SpawnType
import com.mineinabyss.mobzy.spawning.vertical.SpawnInfo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -19,14 +21,17 @@ class IgnoreSpawnCategoryCap()
class SpawnCategoryCapCondition : GearyListener() {
val TargetScope.spawnType by get<SpawnType>()

val EventScope.spawnInfo by get<SpawnInfo>()

init {
target.not { has<IgnoreSpawnCategoryCap>() }
}

@Handler
fun TargetScope.check(): Boolean {
return MobCountManager.isCategoryAllowed(
spawnType.prefab.toEntity()?.get<MobCategory>() ?: return false
)
fun TargetScope.check(event: EventScope): Boolean {
val category = spawnType.prefab.toEntity()?.get<MobCategory>() ?: return false
return MobCountManager.isCategoryAllowed(category) &&
(event.spawnInfo.localCategories[category] ?: 0) <
(MobCountManager.categoryCounts[category]?.get() ?: 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package com.mineinabyss.mobzy.spawning.vertical
import com.mineinabyss.geary.ecs.accessors.TargetScope
import com.mineinabyss.geary.ecs.accessors.building.get
import com.mineinabyss.geary.ecs.query.Query
import com.mineinabyss.geary.ecs.query.invoke
import com.mineinabyss.idofront.location.down
import com.mineinabyss.idofront.location.up
import com.mineinabyss.idofront.nms.aliases.NMSEntityType
import com.mineinabyss.idofront.nms.aliases.toNMS
import com.mineinabyss.idofront.typealiases.BukkitEntity
import com.mineinabyss.mobzy.ecs.components.MobCategory
import com.mineinabyss.mobzy.ecs.components.mobCategory
import com.mineinabyss.mobzy.spawning.SpawnPosition
import com.mineinabyss.mobzy.spawning.components.SubChunkBlockComposition
import org.bukkit.ChunkSnapshot
import org.bukkit.Location
import org.bukkit.entity.Entity
import java.util.concurrent.atomic.AtomicInteger
import kotlin.random.Random

//TODO name could be confused with SpawnRegion
Expand All @@ -27,7 +29,7 @@ import kotlin.random.Random
class SpawnInfo(
val bottom: Location,
val top: Location,
searchRadius: Double = 200.0,
searchRadius: Double = 400.0,
chunkSnapshot: ChunkSnapshot? = null,
) {
val chunkSnapshot: ChunkSnapshot by lazy { chunkSnapshot ?: bottom.chunk.chunkSnapshot }
Expand All @@ -41,15 +43,15 @@ class SpawnInfo(
private val searchRadiusSquared = searchRadius * searchRadius

//TODO more efficiently finding all ECS entities nearby
val localMobs: Map<NMSEntityType<*>, AtomicInteger> by lazy {
NearbyQuery.run {
val localMobs: List<BukkitEntity> by lazy {
NearbyQuery {
map { it.bukkit }.filter {
it.location.world == bottom.world && it.location.distanceSquared(
bottom
) < searchRadiusSquared
it.location.world == bottom.world && it.location.distanceSquared(bottom) < searchRadiusSquared
}
}.categorizeMobs()
}
}
val localTypes: Map<NMSEntityType<*>, Int> by lazy { localMobs.categorizeMobs() }
val localCategories: Map<MobCategory, Int> by lazy { localMobs.groupingBy { it.mobCategory }.eachCount() }

//adding one since if the blocks are on the same block, they still have a gap of 1 from top to bottom
val gap: Int = top.blockY - bottom.blockY + 1
Expand Down Expand Up @@ -91,10 +93,5 @@ class SpawnInfo(
}
}

fun Collection<Entity>.categorizeMobs(): Map<NMSEntityType<*>, AtomicInteger> {
val map = mutableMapOf<NMSEntityType<*>, AtomicInteger>()
forEach { entity ->
map.getOrPut(entity.toNMS().entityType) { AtomicInteger() }.incrementAndGet()
}
return map
}
fun Collection<Entity>.categorizeMobs(): Map<NMSEntityType<*>, Int> =
groupingBy { it.toNMS().entityType }.eachCount()
2 changes: 1 addition & 1 deletion src/main/java/com/mineinabyss/mobzy/MobzyCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MobzyCommands : IdofrontCommandExecutor(), TabCompleter {
if (categories.size > 1)
sender.info(
categories.entries
.sortedByDescending { it.value.get() }
.sortedByDescending { it.value }
.joinToString("\n") { (type, amount) -> "&7${type.typeName}&r: $amount".color() }
)
}
Expand Down

0 comments on commit 05875a1

Please sign in to comment.