Skip to content

Commit

Permalink
More orbit work
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Feb 23, 2024
1 parent 3438c95 commit f713789
Show file tree
Hide file tree
Showing 30 changed files with 482 additions and 435 deletions.
35 changes: 24 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@ repositories {
maven(url = "https://hub.jeff-media.com/nexus/repository/jeff-media-public/")
}

fun DependencyHandlerScope.libraryAndTest(dependency: Any) {
library(dependency)
testImplementation(dependency)
}

fun DependencyHandlerScope.compileOnlyAndTest(dependency: Any) {
compileOnly(dependency)
testImplementation(dependency)
}

dependencies {
library(kotlin("stdlib"))
library("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
library("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
libraryAndTest("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
libraryAndTest("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")

library(kotlin("scripting-common"))
library(kotlin("scripting-jvm"))
library(kotlin("scripting-jvm-host"))
library(kotlin("script-runtime"))
libraryAndTest(kotlin("scripting-common"))
libraryAndTest(kotlin("scripting-jvm"))
libraryAndTest(kotlin("scripting-jvm-host"))
libraryAndTest(kotlin("script-runtime"))

compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("com.github.Slimefun:Slimefun4:RC-36")
compileOnlyAndTest("com.github.Slimefun:Slimefun4:206a9d6")

implementation("org.bstats:bstats-bukkit:3.0.2")
implementation("co.aikar:acf-paper:0.5.1-SNAPSHOT")
Expand All @@ -34,9 +44,8 @@ dependencies {

testImplementation(kotlin("test"))
testImplementation("io.kotest:kotest-assertions-core:5.8.0")
testImplementation("com.github.seeseemelk:MockBukkit-v1.20:3.9.0")

testImplementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
// Need MockBukkit for unimplemented entities
compileOnlyAndTest("com.github.seeseemelk:MockBukkit-v1.20:3.74.0")
}

group = "io.github.addoncommunity.galactifun"
Expand All @@ -50,8 +59,12 @@ tasks.compileKotlin {
kotlinOptions.javaParameters = true
}

tasks.withType<Test>().configureEach {
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
showStandardStreams = true
}
}

tasks.shadowJar {
Expand Down
33 changes: 21 additions & 12 deletions src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.addoncommunity.galactifun

import co.aikar.commands.PaperCommandManager
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryObject
import io.github.addoncommunity.galactifun.api.objects.PlanetaryObject
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.composition
Expand All @@ -13,14 +13,15 @@ import io.github.addoncommunity.galactifun.scripting.PlanetScript
import io.github.addoncommunity.galactifun.scripting.dsl.*
import io.github.addoncommunity.galactifun.scripting.dsl.gen.*
import io.github.addoncommunity.galactifun.scripting.evalScript
import io.github.addoncommunity.galactifun.util.units.Distance.Companion.au
import io.github.addoncommunity.galactifun.util.units.Distance.Companion.kilometers
import io.github.addoncommunity.galactifun.util.units.Mass.Companion.kilograms
import io.github.addoncommunity.galactifun.util.units.years
import io.github.addoncommunity.galactifun.units.Angle.Companion.degrees
import io.github.addoncommunity.galactifun.units.Distance.Companion.au
import io.github.addoncommunity.galactifun.units.Distance.Companion.kilometers
import io.github.addoncommunity.galactifun.units.Mass.Companion.kilograms
import io.github.seggan.kfun.AbstractAddon
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun
import io.github.thebusybiscuit.slimefun4.libraries.paperlib.PaperLib
import kotlinx.datetime.Instant
import org.bstats.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.Material
Expand All @@ -34,20 +35,24 @@ import kotlin.script.experimental.host.toScriptSource
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

class Galactifun2 : AbstractAddon() {
open class Galactifun2 : AbstractAddon() {

lateinit var manager: PaperCommandManager
private set

var isTest = classLoader.javaClass.packageName.startsWith("be.seeseemelk.mockbukkit")

override fun onLoad() {
Bukkit.spigot().config["world-settings.default.verbose"] = false
if (!isTest) {
Bukkit.spigot().config["world-settings.default.verbose"] = false
}
}

override fun onEnable() {
instance = this

var shouldDisable = false
if (!PaperLib.isPaper()) {
if (!PaperLib.isPaper() && !isTest) {
log(Level.SEVERE, "Galactifun2 only supports Paper and its forks (e.x. Airplane and Purpur)")
log(Level.SEVERE, "Please use Paper or a fork of Paper")
shouldDisable = true
Expand All @@ -73,7 +78,9 @@ class Galactifun2 : AbstractAddon() {
return
}

Metrics(this, 11613)
if (!isTest) {
Metrics(this, 11613)
}

manager = PaperCommandManager(this)
manager.enableUnstableAPI("help")
Expand Down Expand Up @@ -159,10 +166,12 @@ class Galactifun2 : AbstractAddon() {
script.planet {
name = "Mars"
item = Material.RED_CONCRETE
orbiting = BaseUniverse.solarSystem
orbit {
distance = 1.52.au
yearLength = 1.88.years
parent = BaseUniverse.sun
semimajorAxis = 1.524.au
eccentricity = 0.0934
argumentOfPeriapsis = 336.04.degrees
timeOfPeriapsis = Instant.parse("2022-05-21T15:00:00Z")
}
mass = 6.417e23.kilograms
radius = 3389.5.kilometers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.addoncommunity.galactifun.api.objects

import io.github.addoncommunity.galactifun.api.objects.properties.Orbit
import io.github.addoncommunity.galactifun.units.Angle.Companion.degrees
import io.github.addoncommunity.galactifun.units.Distance
import io.github.addoncommunity.galactifun.units.Mass
import io.github.addoncommunity.galactifun.util.Constants
import io.github.addoncommunity.galactifun.util.LazyDouble
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils
import kotlinx.datetime.Instant
import org.bukkit.inventory.ItemStack
import kotlin.math.sqrt

sealed class CelestialObject(name: String, baseItem: ItemStack) {

val name = ChatUtils.removeColorCodes(name)
val id = this.name.lowercase().replace(' ', '_')

val item = CustomItemStack(baseItem, name)

abstract fun distanceTo(other: CelestialObject, time: Instant): Distance



abstract val mass: Mass
abstract val radius: Distance

val gravitationalParameter by LazyDouble { Constants.GRAVITATIONAL_CONSTANT * mass.kilograms }
val escapeVelocity by LazyDouble { sqrt(2 * Constants.GRAVITATIONAL_CONSTANT * mass.kilograms / radius.kilometers) }
val parkingOrbit: Orbit by lazy {
Orbit(
parent = this,
semimajorAxis = radius * 1.1,
eccentricity = 0.0,
argumentOfPeriapsis = 0.0.degrees,
timeOfPeriapsis = Instant.fromEpochMilliseconds(0)
)
}

private val _orbiters = mutableListOf<CelestialObject>()
val orbiters: List<CelestialObject> = _orbiters

fun addOrbiter(orbiter: CelestialObject) {
_orbiters.add(orbiter)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is CelestialObject) return false
return id == other.id
}

override fun hashCode(): Int = id.hashCode()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.addoncommunity.galactifun.api.objects

object MilkyWay {

private val _stars = mutableListOf<Star>()
val stars: List<Star> get() = _stars

fun addStar(star: Star) {
_stars.add(star)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.github.addoncommunity.galactifun.api.objects.planet
package io.github.addoncommunity.galactifun.api.objects

import io.github.addoncommunity.galactifun.api.objects.UniversalObject
import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle
import io.github.addoncommunity.galactifun.api.objects.properties.Orbit
import io.github.addoncommunity.galactifun.api.objects.properties.OrbitPosition
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Atmosphere
import io.github.addoncommunity.galactifun.api.objects.properties.visVivaEquation
import io.github.addoncommunity.galactifun.core.managers.PlanetManager
import io.github.addoncommunity.galactifun.units.Distance
import io.github.addoncommunity.galactifun.units.Distance.Companion.meters
import io.github.addoncommunity.galactifun.units.cos
import io.github.addoncommunity.galactifun.util.Constants
import io.github.addoncommunity.galactifun.util.units.Distance
import io.github.addoncommunity.galactifun.util.units.Distance.Companion.meters
import io.github.seggan.kfun.location.plus
import kotlinx.datetime.Instant
import org.bukkit.Location
Expand All @@ -20,10 +21,16 @@ import kotlin.math.sqrt
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

abstract class PlanetaryObject(name: String, baseItem: ItemStack) : UniversalObject(name, baseItem) {
abstract class PlanetaryObject(name: String, baseItem: ItemStack) : CelestialObject(name, baseItem) {

abstract val dayCycle: DayCycle
abstract val atmosphere: Atmosphere
abstract val orbit: Orbit

val star: Star by lazy {
val parent = orbit.parent
if (parent is Star) parent else (parent as PlanetaryObject).star
}

val orbitPosition: OrbitPosition
get() = PlanetManager.getOrbit(this)
Expand All @@ -32,51 +39,74 @@ abstract class PlanetaryObject(name: String, baseItem: ItemStack) : UniversalObj
return orbitPosition.centerLocation + location
}

override fun distanceTo(other: CelestialObject, time: Instant): Distance {
if (other == this) return 0.0.meters
if (other is Star) {
if (star == other) {
var dist = orbit.radius(time)
if (orbit.parent != other) {
dist += orbit.parent.distanceTo(other, time)
}
return dist
} else {
return star.distanceTo(other, time) + distanceTo(star, time)
}
}

require(other is PlanetaryObject)
if (star == other.star) {
if (orbit.parent == other.orbit.parent) {
val thisDist = orbit.radius(time).meters
val otherDist = other.orbit.radius(time).meters
val cosAngle = cos(orbit.trueAnomaly(time) - other.orbit.trueAnomaly(time))
return sqrt(thisDist * thisDist + otherDist * otherDist - 2 * thisDist * otherDist * cosAngle).meters
}
return orbit.radius(time) + orbit.parent.distanceTo(other, time)
} else {
return other.distanceTo(star, time) + distanceTo(star, time)
}
}

fun getDeltaVForTransferTo(other: PlanetaryObject, time: Instant): Double {
if (this == other) return 0.0
val thisParents = generateSequence(this as UniversalObject) {
if (it.orbitLevel == 0) null else it.orbit.parent
val thisParents = generateSequence(this as CelestialObject) {
if (it is PlanetaryObject) it.orbit.parent else null
}.toList()
if (other in thisParents) {
return other.getDeltaVForTransferTo(this, time)
}
val otherParents = generateSequence(other as UniversalObject) {
if (it.orbitLevel == 0) null else it.orbit.parent
val otherParents = generateSequence(other as CelestialObject) {
if (it is PlanetaryObject) it.orbit.parent else null
}.toList()
if (this in otherParents) {
var height = other.parkingOrbit
var dV = 0.0
for (obj in otherParents) {
if (obj == this) break
dV += abs(obj.escapeVelocity - visViva(obj.gravitationalParameter, height.radius(time), height.semimajorAxis))
height = obj.orbit
dV += abs(
obj.escapeVelocity - visVivaEquation(
obj.gravitationalParameter,
height.radius(time),
height.semimajorAxis
)
)
height = (obj as PlanetaryObject).orbit
}
dV += hohmannTransfer(height, parkingOrbit, time)
return dV
} else {
val closestParent = thisParents.first { it in otherParents }
val thisClosestSibling = thisParents[thisParents.indexOf(closestParent) - 1] as PlanetaryObject
val thisClosestOrbit = thisClosestSibling.orbit
val otherClosestSibling = otherParents[otherParents.indexOf(closestParent) - 1] as PlanetaryObject
val otherClosestOrbit = otherClosestSibling.orbit

TODO()
}
}
}

// These formulas came from http://www.braeunig.us/space/

private fun visViva(mu: Double, r: Distance, a: Distance): Double =
sqrt(mu * (2 / r.meters - 1 / a.meters))

private fun hohmannTransfer(parking: Orbit, target: Orbit, time: Instant): Double {
val parkingR = parking.radius(time)
val targetR = target.radius(time)
val transferA = (parkingR + targetR) / 2
val transferA = (parkingR + targetR) / 2.0
val mu = parking.parent.gravitationalParameter
val firstManeuver = abs(visViva(mu, parkingR, transferA) - visViva(mu, parkingR, parking.semimajorAxis))
val secondManeuver = abs(visViva(mu, targetR, target.semimajorAxis) - visViva(mu, targetR, transferA))
val firstManeuver = abs(visVivaEquation(mu, parkingR, transferA) - visVivaEquation(mu, parkingR, parking.semimajorAxis))
val secondManeuver = abs(visVivaEquation(mu, targetR, target.semimajorAxis) - visVivaEquation(mu, targetR, transferA))
return firstManeuver + secondManeuver
}

Expand Down Expand Up @@ -105,7 +135,7 @@ private fun oneTangentTransferOrbit(
private fun brachistochroneTransfer(
distance: Distance,

): BrachistochroneTransfer {
): BrachistochroneTransfer {
val time = 2 * sqrt(distance.meters / Constants.EARTH_GRAVITY)
val dV = Constants.EARTH_GRAVITY * time
return BrachistochroneTransfer(dV, time.seconds)
Expand Down
Loading

0 comments on commit f713789

Please sign in to comment.