Skip to content

Commit

Permalink
Accepted Lugia's PR to Fix Roll/Pitch/Yaw
Browse files Browse the repository at this point in the history
Added LuaPhysShip and PhysTickEventHandler
Removed unused import statements
Version Bump
  • Loading branch information
TechTastic committed May 26, 2024
1 parent 165bfbb commit 48b0139
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 62 deletions.
13 changes: 13 additions & 0 deletions common/src/main/kotlin/io/github/techtastic/cc_vs/PlatformUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.techtastic.cc_vs

import dan200.computercraft.shared.computer.core.ServerComputer
import dev.architectury.injectables.annotations.ExpectPlatform

object PlatformUtils {
Expand All @@ -14,4 +15,16 @@ object PlatformUtils {
fun isCommandOnly(): Boolean {
throw AssertionError()
}

@JvmStatic
@ExpectPlatform
fun exposePhysTick(): Boolean {
throw AssertionError()
}

@JvmStatic
@ExpectPlatform
fun getComputerByID(id: Int): ServerComputer? {
throw AssertionError()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import dan200.computercraft.api.lua.IArguments
import dan200.computercraft.api.lua.LuaException
import dan200.computercraft.api.lua.LuaFunction
import dan200.computercraft.core.apis.IAPIEnvironment
import io.github.techtastic.cc_vs.CCVSMod
import io.github.techtastic.cc_vs.PlatformUtils
import io.github.techtastic.cc_vs.ship.PhysTickEventHandler
import io.github.techtastic.cc_vs.ship.QueuedForcesApplier
import net.minecraft.server.level.ServerLevel
import org.apache.logging.log4j.core.pattern.NotANumber
import org.joml.Quaterniond
import org.joml.Quaterniondc
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.LoadedServerShip
import org.valkyrienskies.core.api.ships.ServerShip
import org.valkyrienskies.core.apigame.ShipTeleportData
import org.valkyrienskies.core.impl.game.ShipTeleportDataImpl
import org.valkyrienskies.mod.common.dimensionId
import org.valkyrienskies.mod.common.shipObjectWorld
import org.valkyrienskies.mod.common.util.settings
import org.valkyrienskies.mod.common.vsCore

class ExtendedShipAPI(environment: IAPIEnvironment, ship: ServerShip, val level: ServerLevel) : ShipAPI(environment, ship) {
class ExtendedShipAPI(private val api: IAPIEnvironment, ship: ServerShip, private val level: ServerLevel) : ShipAPI(ship) {
var control: QueuedForcesApplier = QueuedForcesApplier.getOrCreateControl(this.ship)

init {
if (PlatformUtils.exposePhysTick())
PhysTickEventHandler.getOrCreateControl(this.ship).addComputer(api.computerID)
}

@LuaFunction
fun applyInvariantForce(xForce: Double, yForce: Double, zForce: Double) {
this.control.applyInvariantForce(Vector3d(xForce, yForce, zForce))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.techtastic.cc_vs.apis

import dan200.computercraft.api.lua.LuaFunction
import io.github.techtastic.cc_vs.util.CCVSUtils.momentToTable
import io.github.techtastic.cc_vs.util.CCVSUtils.quatToTable
import io.github.techtastic.cc_vs.util.CCVSUtils.vectorToTable
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl

class LuaPhysShip(private val physShip: PhysShipImpl) {

@LuaFunction
fun getBuoyantFactor() = this.physShip.buoyantFactor

@LuaFunction
fun isStatic() = this.physShip.isStatic

@LuaFunction
fun doFluidDrag() = this.physShip.doFluidDrag

@LuaFunction
fun getInertia(): Map<String, Any> {
val inertia = this.physShip.inertia
return mapOf(
Pair("momentOfInertiaTensor", momentToTable(inertia.momentOfInertiaTensor)),
Pair("mass", inertia.shipMass)
)
}

@LuaFunction
fun getPoseVel(): Map<String, Map<String, Double>> {
val poseVel = this.physShip.poseVel
return mapOf(
Pair("vel", vectorToTable(poseVel.vel)),
Pair("omega", vectorToTable(poseVel.omega)),
Pair("pos", vectorToTable(poseVel.pos)),
Pair("rot", quatToTable(poseVel.rot))
)
}

@LuaFunction
fun getForcesInducers(): List<String> {
val inducers = this.physShip.forceInducers

val list = mutableListOf<String>()
inducers.forEach {
list.add(it.toString())
}

return list
}
}
35 changes: 6 additions & 29 deletions common/src/main/kotlin/io/github/techtastic/cc_vs/apis/ShipAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package io.github.techtastic.cc_vs.apis

import dan200.computercraft.api.lua.ILuaAPI
import dan200.computercraft.api.lua.LuaFunction
import dan200.computercraft.core.apis.IAPIEnvironment
import org.joml.Vector3d
import org.joml.Vector3dc
import io.github.techtastic.cc_vs.util.CCVSUtils.momentToTable
import io.github.techtastic.cc_vs.util.CCVSUtils.quatToTable
import io.github.techtastic.cc_vs.util.CCVSUtils.vectorToTable
import org.joml.Vector4d
import org.joml.primitives.AABBi
import org.valkyrienskies.core.api.ships.ServerShip
import kotlin.math.asin
import kotlin.math.atan2

open class ShipAPI(val environment: IAPIEnvironment, val ship: ServerShip) : ILuaAPI {
open class ShipAPI(val ship: ServerShip) : ILuaAPI {
var names: ArrayList<String> = arrayListOf("ship", this.ship.slug ?: "ship")

override fun getNames(): Array<String> = names.toTypedArray()
Expand All @@ -29,16 +29,7 @@ open class ShipAPI(val environment: IAPIEnvironment, val ship: ServerShip) : ILu
fun getMass(): Double = this.ship.inertiaData.mass

@LuaFunction
fun getMomentOfInertiaTensor(): List<List<Double>> {
val tensor: MutableList<List<Double>> = mutableListOf()

for (i in 0..2) {
val row = this.ship.inertiaData.momentOfInertiaTensor.getRow(i, Vector3d())
tensor.add(i, listOf(row.x, row.y, row.z))
}

return tensor
}
fun getMomentOfInertiaTensor(): List<List<Double>> = momentToTable(this.ship.inertiaData.momentOfInertiaTensor)

@LuaFunction
fun getName(): String = this.ship.slug ?: "no-name"
Expand All @@ -47,15 +38,7 @@ open class ShipAPI(val environment: IAPIEnvironment, val ship: ServerShip) : ILu
fun getOmega(): Map<String, Double> = vectorToTable(this.ship.omega)

@LuaFunction
fun getQuaternion(): Map<String, Double> {
val q = this.ship.transform.shipToWorldRotation
return mapOf(
Pair("x", q.x()),
Pair("y", q.y()),
Pair("z", q.z()),
Pair("w", q.w())
)
}
fun getQuaternion(): Map<String, Double> = quatToTable(this.ship.transform.shipToWorldRotation)

@LuaFunction
fun getRoll(): Double {
Expand Down Expand Up @@ -115,10 +98,4 @@ open class ShipAPI(val environment: IAPIEnvironment, val ship: ServerShip) : ILu

return matrix.toList()
}

private fun vectorToTable(vec: Vector3dc): Map<String, Double> = mapOf(
Pair("x", vec.x()),
Pair("y", vec.y()),
Pair("z", vec.z())
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.techtastic.cc_vs.ship

import com.fasterxml.jackson.annotation.JsonIgnore
import io.github.techtastic.cc_vs.PlatformUtils
import io.github.techtastic.cc_vs.apis.LuaPhysShip
import org.valkyrienskies.core.api.ships.*
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl

class PhysTickEventHandler: ShipForcesInducer {
@JsonIgnore
private val computers = mutableListOf<Int>()

override fun applyForces(physShip: PhysShip) {
if (!PlatformUtils.exposePhysTick())
return

val physShip = physShip as PhysShipImpl

this.computers.removeIf { PlatformUtils.getComputerByID(it) == null }

this.computers.forEach {
PlatformUtils.getComputerByID(it)?.queueEvent("physics_tick", arrayOf(LuaPhysShip(physShip)))
}
}

fun addComputer(id: Int) {
this.computers.add(id)
}

companion object {
fun getOrCreateControl(ship: ServerShip): PhysTickEventHandler {
var control = ship.getAttachment(PhysTickEventHandler::class.java)
if (control == null) {
control = PhysTickEventHandler()
ship.saveAttachment(PhysTickEventHandler::class.java, control)
}

return control
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.github.techtastic.cc_vs.ship

import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.LoadedServerShip
import org.valkyrienskies.core.api.ships.PhysShip
import org.valkyrienskies.core.api.ships.ServerShip
import org.valkyrienskies.core.api.ships.ShipForcesInducer
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl
import org.valkyrienskies.core.util.pollUntilEmpty
import java.util.concurrent.ConcurrentLinkedQueue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package io.github.techtastic.cc_vs.util

import dan200.computercraft.shared.computer.core.ComputerFamily
import dan200.computercraft.shared.computer.core.ServerComputer
import io.github.techtastic.cc_vs.CCVSMod
import io.github.techtastic.cc_vs.PlatformUtils
import io.github.techtastic.cc_vs.apis.ExtendedShipAPI
import io.github.techtastic.cc_vs.apis.ShipAPI
import net.minecraft.core.BlockPos
import net.minecraft.server.level.ServerLevel
import org.joml.Matrix3dc
import org.joml.Quaterniondc
import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.ServerShip
import org.valkyrienskies.mod.common.getShipManagingPos

object CCVSUtils {
fun applyShipAPIsToComputer(computer: ServerComputer, level: ServerLevel, ship: ServerShip?) {
Expand All @@ -17,6 +22,32 @@ object CCVSUtils {
if (!PlatformUtils.isCommandOnly() || computer.family == ComputerFamily.COMMAND)
computer.addAPI(ExtendedShipAPI(computer.apiEnvironment, ship, level))
else
computer.addAPI(ShipAPI(computer.apiEnvironment, ship))
computer.addAPI(ShipAPI(ship))
}

fun vectorToTable(vec: Vector3dc): Map<String, Double> = mapOf(
Pair("x", vec.x()),
Pair("y", vec.y()),
Pair("z", vec.z())
)

fun quatToTable(q: Quaterniondc): Map<String, Double> = mapOf(
Pair("x", q.x()),
Pair("y", q.y()),
Pair("z", q.z()),
Pair("w", q.w())
)

fun momentToTable(moment: Matrix3dc): List<List<Double>> {
val tensor: MutableList<List<Double>> = mutableListOf()

for (i in 0..2) {
val row = moment.getRow(i, Vector3d())
tensor.add(i, listOf(row.x, row.y, row.z))
}

return tensor
}

fun getShip(level: ServerLevel, pos: BlockPos) = level.getShipManagingPos(pos)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.techtastic.cc_vs.fabric;

import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.computer.core.ServerComputer;
import io.github.techtastic.cc_vs.fabric.config.CCVSConfig;

public class PlatformUtilsImpl {
Expand All @@ -10,4 +12,12 @@ public static boolean canTeleport() {
public static boolean isCommandOnly() {
return CCVSConfig.COMMAND_ONLY.get();
}

public static boolean exposePhysTick() {
return CCVSConfig.EXPOSE_PHYS_TICK.get();
}

public static ServerComputer getComputerByID(int id) {
return ComputerCraft.serverComputerRegistry.get(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ public class CCVSConfig {

public static ForgeConfigSpec.ConfigValue<Boolean> CAN_TELEPORT;
public static ForgeConfigSpec.ConfigValue<Boolean> COMMAND_ONLY;
public static ForgeConfigSpec.ConfigValue<Boolean> EXPOSE_PHYS_TICK;

static {
BUILDER.push("CC: VS Mod Config");

CAN_TELEPORT = BUILDER.comment("Is ExtendedShipAPI.teleport() enabled?", "Enable at your own Risk!")
.worldRestart().translation("config.cc_vs.can_teleport").define("can_teleport", false);
COMMAND_ONLY = BUILDER.comment("Is ExtendedShipAPI only available for the Command Computer?",
"This API is more powerful than the normal and advanced ShipAPI.").worldRestart()
"This API is more powerful than the normal and advanced ShipAPI.").worldRestart()
.translation("config.cc_vs.command_only").define("command_only", true);
EXPOSE_PHYS_TICK = BUILDER.comment("Expose PhysShipImpl from the physics tick?",
"This will fire an event every physics tick for every computer using ExtendedShipAPI on a Ship. Concurrency issues may occur!").worldRestart()
.translation("config.cc_vs.expose_phys_tick").define("expose_phys_tick", false);

BUILDER.pop();
SPEC = BUILDER.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.techtastic.cc_vs.fabric.mixin;

import dan200.computercraft.shared.computer.blocks.TileComputer;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import io.github.techtastic.cc_vs.apis.ExtendedShipAPI;
import io.github.techtastic.cc_vs.apis.ShipAPI;
import io.github.techtastic.cc_vs.util.CCVSUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -21,7 +18,6 @@ public class MixinTileComputer {
@Inject(
method = "createComputer",
at = @At("RETURN"),
cancellable = true,
remap = false
)
private void cc_vs$addShipAPI(int instanceID, int id, CallbackInfoReturnable<ServerComputer> cir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.techtastic.cc_vs.fabric.mixin;

import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import io.github.techtastic.cc_vs.apis.ExtendedShipAPI;
import io.github.techtastic.cc_vs.apis.ShipAPI;
import io.github.techtastic.cc_vs.util.CCVSUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -21,7 +18,6 @@ public class MixinTileTurtle {
@Inject(
method = "createComputer",
at = @At("RETURN"),
cancellable = true,
remap = false
)
private void cc_vs$addShipAPI(int instanceID, int id, CallbackInfoReturnable<ServerComputer> cir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.techtastic.cc_vs.forge.mixin;

import dan200.computercraft.shared.computer.blocks.TileComputer;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import io.github.techtastic.cc_vs.apis.ExtendedShipAPI;
import io.github.techtastic.cc_vs.apis.ShipAPI;
import io.github.techtastic.cc_vs.util.CCVSUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package io.github.techtastic.cc_vs.forge.mixin;

import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
import io.github.techtastic.cc_vs.apis.ExtendedShipAPI;
import io.github.techtastic.cc_vs.apis.ShipAPI;
import io.github.techtastic.cc_vs.util.CCVSUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand Down
Loading

0 comments on commit 48b0139

Please sign in to comment.