-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
364 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 84 additions & 29 deletions
113
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/api/rockets/RocketInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,101 @@ | ||
package io.github.addoncommunity.galactifun.api.rockets | ||
|
||
import io.github.addoncommunity.galactifun.Constants | ||
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas | ||
import io.github.addoncommunity.galactifun.impl.items.FuelTank | ||
import io.github.addoncommunity.galactifun.impl.items.RocketEngine | ||
import io.github.addoncommunity.galactifun.impl.managers.PlanetManager | ||
import io.github.addoncommunity.galactifun.units.Acceleration | ||
import io.github.addoncommunity.galactifun.units.Force | ||
import io.github.addoncommunity.galactifun.units.Mass | ||
import io.github.addoncommunity.galactifun.units.times | ||
import io.github.addoncommunity.galactifun.util.items.mass | ||
import io.github.addoncommunity.galactifun.units.* | ||
import io.github.addoncommunity.galactifun.util.general.mergeMaps | ||
import io.github.addoncommunity.galactifun.util.items.wetMass | ||
import io.github.addoncommunity.galactifun.util.processSlimefunBlocks | ||
import io.github.thebusybiscuit.slimefun4.libraries.dough.blocks.BlockPosition | ||
import kotlin.math.ln | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
data class RocketInfo( | ||
class RocketInfo( | ||
val commandComputer: BlockPosition, | ||
val blocks: Set<BlockPosition>, | ||
val engines: List<Pair<RocketEngine, Set<BlockPosition>>> | ||
engineData: Map<Pair<RocketEngine, BlockPosition>, Set<BlockPosition>> | ||
) { | ||
val thrust: Force | ||
get() = engines.fold(Force.ZERO) { acc, engine -> acc + engine.first.thrust } | ||
val mass: Mass | ||
get() = blocks.fold(Mass.ZERO) { acc, block -> acc + block.block.mass } | ||
|
||
fun twr(gravity: Acceleration): Double { | ||
if (gravity == Acceleration.ZERO) return Double.POSITIVE_INFINITY | ||
return thrust / (mass * gravity) | ||
} | ||
val engines = engineData.keys.map { it.first } | ||
|
||
val thrust = engines.sumBy { it.thrust } | ||
val wetMass = blocks.sumBy { it.block.wetMass } | ||
|
||
val stages: List<Stage> | ||
|
||
init { | ||
stages = if (engineData.isEmpty()) { | ||
emptyList() | ||
} else { | ||
val sortedEngines = engineData.toList().sortedBy { it.first.second.y } | ||
val stageList = mutableListOf<Stage>() | ||
|
||
val fuel: List<Pair<List<RocketEngine>, Map<Gas, Double>>> | ||
get() { | ||
TODO() | ||
for ((engine, fuelBlocks) in sortedEngines) { | ||
val engines = stageList.flatMap { it.engines }.map { it.second } | ||
if (stageList.any { engine.second in engines }) continue | ||
val stage = sortedEngines.filter { it.second == fuelBlocks }.map { it.first } | ||
stageList.add(Stage(stage, fuelBlocks)) | ||
} | ||
|
||
stageList | ||
} | ||
} | ||
|
||
val info: String | ||
get() = buildString { | ||
val planet = PlanetManager.getByWorld(commandComputer.world) ?: error("Planet not found") | ||
appendLine("Fuel:") | ||
for ((gas, amount) in fuel) { | ||
append(" ".repeat(4)) | ||
append(gas) | ||
//appendLine(": %.2f l, %.2f kg".format(amount, amount * gas.liquidDensity)) | ||
val dryMass = wetMass - stages.sumBy { it.fuelMass } | ||
|
||
val info = buildString { | ||
val planet = PlanetManager.getByWorld(commandComputer.world) ?: error("Planet not found") | ||
appendLine("Stages:") | ||
var stageNum = 1 | ||
for (stage in stages) { | ||
appendLine(" Stage ${stageNum++}: ") | ||
appendLine(" Fuel:") | ||
for ((gas, volume) in stage.fuel) { | ||
appendLine(" $gas: %.2f liters, %.2s".format(volume.liters, volume * gas.liquidDensity)) | ||
} | ||
appendLine(" Engines:") | ||
var engineNum = 1 | ||
for (engine in stage.engines) { | ||
appendLine(" Engine ${engineNum++}: %.2f kilonewtons".format(engine.first.thrust.kilonewtons)) | ||
} | ||
appendLine("Thrust: %.2f kN".format(thrust.kilonewtons)) | ||
appendLine("Mass: %.2f kg".format(mass.kilograms)) | ||
appendLine("TWR: %.2f".format(twr(planet.gravity))) | ||
appendLine(" Delta-V: %.2f m/s".format(stage.deltaV.metersPerSecond)) | ||
} | ||
appendLine("Thrust: %.2f kilonewtons".format(thrust.kilonewtons)) | ||
appendLine("Wet mass: %.2s".format(wetMass)) | ||
appendLine("Dry mass: %.2s".format(dryMass)) | ||
appendLine("TWR: %.2f".format(twr(planet.gravity))) | ||
appendLine("Delta-V: %.2f m/s".format(deltaV(engines, wetMass, dryMass).metersPerSecond)) | ||
} | ||
|
||
fun twr(gravity: Acceleration): Double { | ||
if (gravity == Acceleration.ZERO) return Double.POSITIVE_INFINITY | ||
return thrust / (wetMass * gravity) | ||
} | ||
|
||
inner class Stage( | ||
val engines: List<Pair<RocketEngine, BlockPosition>>, | ||
val fuelBlocks: Set<BlockPosition> | ||
) { | ||
val fuel: Map<Gas, Volume> = fuelBlocks.processSlimefunBlocks<FuelTank, _>(FuelTank::getFuelLevel) | ||
.fold(mapOf()) { acc, fuel -> acc.mergeMaps(fuel, Volume::plus) } | ||
|
||
val fuelMass = fuel.toList().sumBy { (gas, volume) -> gas.liquidDensity * volume } | ||
|
||
val deltaV = deltaV( | ||
engines.map { it.first }, | ||
wetMass, | ||
wetMass - fuelMass | ||
) | ||
} | ||
} | ||
|
||
private fun deltaV(engines: List<RocketEngine>, wetMass: Mass, dryMass: Mass): Velocity { | ||
val ispNeum = engines.sumBy { it.thrust }.newtons | ||
val ispDenom = engines.sumOf { it.thrust.newtons / it.specificImpulse.doubleSeconds } | ||
val isp = (ispNeum / ispDenom).seconds | ||
|
||
return Constants.EARTH_GRAVITY * isp * ln(wetMass / dryMass) | ||
} |
10 changes: 10 additions & 0 deletions
10
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/blocks/CustomMass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.addoncommunity.galactifun.blocks | ||
|
||
import io.github.addoncommunity.galactifun.units.Mass | ||
import org.bukkit.block.Block | ||
|
||
interface CustomMass { | ||
fun getMass(block: Block): Mass | ||
|
||
fun getWetMass(block: Block): Mass = getMass(block) | ||
} |
27 changes: 27 additions & 0 deletions
27
plugin/src/main/kotlin/io/github/addoncommunity/galactifun/blocks/MassedBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.addoncommunity.galactifun.blocks | ||
|
||
import io.github.addoncommunity.galactifun.units.Mass | ||
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem | ||
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack | ||
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType | ||
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler | ||
import org.bukkit.block.Block | ||
import org.bukkit.inventory.ItemStack | ||
|
||
open class BasicMassedBlock( | ||
itemGroup: ItemGroup, | ||
item: SlimefunItemStack, | ||
recipeType: RecipeType, | ||
recipe: Array<out ItemStack?>, | ||
private val mass: Mass | ||
) : SlimefunItem(itemGroup, item, recipeType, recipe), CustomMass { | ||
|
||
init { | ||
addItemHandler(BlockUseHandler { | ||
it.cancel() | ||
}) | ||
} | ||
|
||
override fun getMass(block: Block): Mass = mass | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.