From 439cbccfe2095132a45e739a6ff4c89bfa5845bd Mon Sep 17 00:00:00 2001 From: TONY_All Date: Sun, 25 Jun 2023 02:15:01 +0800 Subject: [PATCH] finish ore generate part (Part Of MAXMC-T-4) --- .../cc/maxmc/blastingcrisis/BlastingCrisis.kt | 3 ++- .../maxmc/blastingcrisis/command/DebugCommand.kt | 3 ++- .../kotlin/cc/maxmc/blastingcrisis/game/Game.kt | 13 +++++++++---- .../blastingcrisis/game/GameOreGenerator.kt | 14 +++++++------- .../cc/maxmc/blastingcrisis/game/GameTeam.kt | 2 +- .../kotlin/cc/maxmc/blastingcrisis/misc/Area.kt | 16 ++++++++++++++-- .../kotlin/cc/maxmc/blastingcrisis/misc/Math.kt | 6 +++++- .../cc/maxmc/blastingcrisis/misc/UniArea.kt | 6 ++++++ 8 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 src/main/kotlin/cc/maxmc/blastingcrisis/misc/UniArea.kt diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/BlastingCrisis.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/BlastingCrisis.kt index ea4308e..a274382 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/BlastingCrisis.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/BlastingCrisis.kt @@ -70,7 +70,8 @@ object BlastingCrisis : Plugin() { listOf( teamRed, teamGreen ), - 1 + 1, + "default" ) DebugCommand.game = Game(map) GameManager.currentGame = DebugCommand.game diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/command/DebugCommand.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/command/DebugCommand.kt index 8b05565..b165644 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/command/DebugCommand.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/command/DebugCommand.kt @@ -4,6 +4,7 @@ import cc.maxmc.blastingcrisis.game.Game import cc.maxmc.blastingcrisis.game.GameOreGenerator import cc.maxmc.blastingcrisis.game.GameState import cc.maxmc.blastingcrisis.misc.Area +import cc.maxmc.blastingcrisis.misc.UniArea import cc.maxmc.blastingcrisis.packet.BEntityVillager import kotlinx.coroutines.cancel import org.bukkit.Bukkit @@ -116,7 +117,7 @@ object DebugCommand { println("saved") val config = Configuration.loadFromFile(File(getDataFolder(), "ore-generators/default.yml")) val gen = GameOreGenerator(config) - val job = gen.enable(area) + val job = gen.enable(UniArea(listOf(area))) Bukkit.getScheduler().runTaskLater(BukkitPlugin.getInstance(), { job.cancel("Stop by hand") }, 60 * 20) diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt index f48ed5b..774c2fe 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt @@ -1,7 +1,9 @@ package cc.maxmc.blastingcrisis.game import cc.maxmc.blastingcrisis.map.GameMap +import cc.maxmc.blastingcrisis.misc.Area import cc.maxmc.blastingcrisis.misc.BlockGenManager +import cc.maxmc.blastingcrisis.misc.UniArea import cc.maxmc.blastingcrisis.misc.debug import org.bukkit.Material import org.bukkit.entity.Player @@ -17,7 +19,7 @@ class Game( val timer: GameTimer = GameTimer(this) val players = ArrayList() val placeBreakRule = GamePlaceBreakRule(this) - val generator = BlockGenManager.getGenerator(map.blockGen) + val generator = BlockGenManager.getGenerator(map.blockGen)!! var state: GameState = GameState.WAITING private fun autoJoinTeam() { @@ -51,7 +53,7 @@ class Game( fun start() { debug("game ${map.name} started.") placeBreakRule.loadDefaultRule() - + startOreGen() state = GameState.START timer.startTimer() timer.submitEvent("wall_fall", Duration.ofMinutes(1)) { @@ -85,9 +87,12 @@ class Game( } private fun startOreGen() { - map.teams.forEach { - + val mines = map.teams.fold(ArrayList()) { list, team -> + list += team.mine + list += team.sides + list } + generator.enable(UniArea(mines)) } fun checkEnd() { diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameOreGenerator.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameOreGenerator.kt index c35ab59..eaa8330 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameOreGenerator.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameOreGenerator.kt @@ -1,9 +1,6 @@ package cc.maxmc.blastingcrisis.game -import cc.maxmc.blastingcrisis.misc.Area -import cc.maxmc.blastingcrisis.misc.WeightRandom -import cc.maxmc.blastingcrisis.misc.debug -import cc.maxmc.blastingcrisis.misc.pluginScope +import cc.maxmc.blastingcrisis.misc.* import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -24,13 +21,14 @@ class GameOreGenerator(config: Configuration) { } private val random = WeightRandom(ores.map { it.key to it.value }) - fun enable(area: Area): Job { + fun enable(area: UniArea): Job { return pluginScope.launch { while (true) { - val airPercent = area.getAirPercentage() + val targetArea = area.randomizer.random() + val airPercent = targetArea.getAirPercentage() debug("remain air: $airPercent.") if (airPercent > 0.5) { - generate(area) + generate(targetArea) } delay(rate * 50L) } @@ -54,4 +52,6 @@ class GameOreGenerator(config: Configuration) { return generate(area, times + 1) } } + + } \ No newline at end of file diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameTeam.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameTeam.kt index e46727c..4d1c07d 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameTeam.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GameTeam.kt @@ -24,7 +24,7 @@ class GameTeam(val game: Game, val teamInfo: MapTeam) { val player = it.player ?: throw IllegalStateException("Bukkit API LOL") debug("teleporting $player to battle field") val team = player.team ?: throw IllegalStateException("Player ${player.name} should have a team") - player.teleport(team.teamInfo.mine.randomLocation().toPlayerLocation()) + player.teleport(team.teamInfo.mine.randomLocationRestrict().toPlayerLocation()) }) } diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Area.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Area.kt index f3b52a2..0fda3dd 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Area.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Area.kt @@ -80,7 +80,7 @@ class Area(loc1: Location, loc2: Location) : ConfigurationSerializable { * * @return a random Location that player can stand */ - tailrec fun randomLocation(): Location { + tailrec fun randomLocationRestrict(): Location { val x = random(locMin.blockX, locTop.blockX) val z = random(locMin.blockZ, locTop.blockZ) for (y in locMin.blockY..locTop.blockY) { @@ -88,9 +88,21 @@ class Area(loc1: Location, loc2: Location) : ConfigurationSerializable { val add = loc.clone().add(0.0, 1.0, 0.0) if (loc.block.type == Material.AIR && add.block.type == Material.AIR) return loc } - return randomLocation() + return randomLocationRestrict() } + tailrec fun randomLocationAny(): Location { + val x = random(locMin.blockX, locTop.blockX) + val y = random(locMin.blockY, locTop.blockY) + val z = random(locMin.blockZ, locTop.blockZ) + val loc = Vector(x, y, z).toLocation(locMin.world) + if (loc.block.type == Material.AIR) return loc + return randomLocationAny() + } + + fun volume(): Int = + (locTop.blockX - locMin.blockX) * (locTop.blockY - locMin.blockY) * (locTop.blockZ - locMin.blockZ) + override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Math.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Math.kt index 608d93a..f457c4b 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Math.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/Math.kt @@ -11,7 +11,7 @@ class WeightRandom(list: List>) { init { Preconditions.checkNotNull(list, "list can NOT be null!") for (pair in list) { - Preconditions.checkArgument(pair.second.toDouble() > 0, "非法权重值:pair=$pair") +// Preconditions.checkArgument(pair.second.toDouble() > 0, "非法权重值:pair=$pair") val lastWeight: Double = if (weightMap.size == 0) 0.0 else weightMap.lastKey().toDouble() //统一转为double weightMap[pair.second.toDouble() + lastWeight] = pair.first //权重累加 } @@ -24,6 +24,10 @@ class WeightRandom(list: List>) { } } +fun List>.weightedRandom(): WeightRandom { + return WeightRandom(this) +} + fun Location.toPlayerLocation(): Location = clone().apply { x = blockX + 0.5 y = blockY.toDouble() diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/misc/UniArea.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/UniArea.kt new file mode 100644 index 0000000..051958e --- /dev/null +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/misc/UniArea.kt @@ -0,0 +1,6 @@ +package cc.maxmc.blastingcrisis.misc + + +class UniArea(subArea: List) { + val randomizer = subArea.map { it to it.volume() }.weightedRandom() +} \ No newline at end of file