Skip to content

Commit 213857d

Browse files
authored
Merge pull request #24 from TechTastic/1.19.x/update
1.19.x/update
2 parents 4b8c401 + d5f9ff6 commit 213857d

File tree

11 files changed

+604
-69
lines changed

11 files changed

+604
-69
lines changed

changelog.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
Fixes
2-
- Removed the thorns in my side (getRoll, getPitch, getYaw)
3-
- Welcome to Quaternion to Euler conversions (added the following methods)
4-
- getEulerAnglesXYZ
5-
- getEulerAnglesYXZ
6-
- getEulerAnglesZXY
7-
- getEulerAnglesZYX
1+
# Changes
2+
- Removed the `getEulerAngles***` methods in favor of a `quaternion` Lua library in-game
3+
- Added `quaternion` Lua library as an in-game API
4+
- Changed the `physics_tick` event to `physics_ticks` and updated how it works
5+
- `physics_ticks` now saves up previous ticks of data THEN, on game tick, send out the event with the queued physics data
6+
- Updated config documentation
7+
- Added `ship` help page in-game
8+
- Added new methods to both Ship and Extended Ship API that take vectors

common/src/main/kotlin/io/github/techtastic/cc_vs/apis/ExtendedShipAPI.kt

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import dan200.computercraft.core.apis.IAPIEnvironment
77
import io.github.techtastic.cc_vs.PlatformUtils
88
import io.github.techtastic.cc_vs.ship.PhysTickEventHandler
99
import io.github.techtastic.cc_vs.ship.QueuedForcesApplier
10+
import io.github.techtastic.cc_vs.util.CCVSUtils.toVector
1011
import net.minecraft.server.level.ServerLevel
12+
import org.checkerframework.common.reflection.qual.NewInstance
1113
import org.joml.Quaterniond
1214
import org.joml.Quaterniondc
1315
import org.joml.Vector3d
@@ -26,33 +28,69 @@ class ExtendedShipAPI(private val api: IAPIEnvironment, ship: ServerShip, level:
2628
}
2729

2830
@LuaFunction
29-
fun applyInvariantForce(xForce: Double, yForce: Double, zForce: Double) {
30-
this.control.applyInvariantForce(Vector3d(xForce, yForce, zForce))
31+
fun applyInvariantForce(args: IArguments) {
32+
val newForce =
33+
if (args.count() == 1)
34+
args.getTable(0).toVector()
35+
else
36+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2))
37+
this.control.applyInvariantForce(newForce)
3138
}
3239

3340
@LuaFunction
34-
fun applyInvariantTorque(xTorque: Double, yTorque: Double, zTorque: Double) {
35-
this.control.applyInvariantTorque(Vector3d(xTorque, yTorque, zTorque))
41+
fun applyInvariantTorque(args: IArguments) {
42+
val newTorque =
43+
if (args.count() == 1)
44+
args.getTable(0).toVector()
45+
else
46+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2))
47+
this.control.applyInvariantTorque(newTorque)
3648
}
3749

3850
@LuaFunction
39-
fun applyInvariantForceToPos(xForce: Double, yForce: Double, zForce: Double, xPos: Double, yPos: Double, zPos: Double) {
40-
this.control.applyInvariantForceToPos(Vector3d(xForce, yForce, zForce), Vector3d(xPos, yPos, zPos))
51+
fun applyInvariantForceToPos(args: IArguments) {
52+
val (newForce, newPos) =
53+
if (args.count() == 2)
54+
Pair(args.getTable(0).toVector(), args.getTable(1).toVector())
55+
else
56+
Pair(
57+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2)),
58+
Vector3d(args.getDouble(3), args.getDouble(4), args.getDouble(5))
59+
)
60+
this.control.applyInvariantForceToPos(newForce, newPos)
4161
}
4262

4363
@LuaFunction
44-
fun applyRotDependentForce(xForce: Double, yForce: Double, zForce: Double) {
45-
this.control.applyRotDependentForce(Vector3d(xForce, yForce, zForce))
64+
fun applyRotDependentForce(args: IArguments) {
65+
val newForce =
66+
if (args.count() == 1)
67+
args.getTable(0).toVector()
68+
else
69+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2))
70+
this.control.applyRotDependentForce(newForce)
4671
}
4772

4873
@LuaFunction
49-
fun applyRotDependentTorque(xTorque: Double, yTorque: Double, zTorque: Double) {
50-
this.control.applyRotDependentTorque(Vector3d(xTorque, yTorque, zTorque))
74+
fun applyRotDependentTorque(args: IArguments) {
75+
val newTorque =
76+
if (args.count() == 1)
77+
args.getTable(0).toVector()
78+
else
79+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2))
80+
this.control.applyRotDependentTorque(newTorque)
5181
}
5282

5383
@LuaFunction
54-
fun applyRotDependentForceToPos(xForce: Double, yForce: Double, zForce: Double, xPos: Double, yPos: Double, zPos: Double) {
55-
this.control.applyRotDependentForceToPos(Vector3d(xForce, yForce, zForce), Vector3d(xPos, yPos, zPos))
84+
fun applyRotDependentForceToPos(args: IArguments) {
85+
val (newForce, newPos) =
86+
if (args.count() == 2)
87+
Pair(args.getTable(0).toVector(), args.getTable(1).toVector())
88+
else
89+
Pair(
90+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2)),
91+
Vector3d(args.getDouble(3), args.getDouble(4), args.getDouble(5))
92+
)
93+
this.control.applyRotDependentForceToPos(newForce, newPos)
5694
}
5795

5896
@LuaFunction

common/src/main/kotlin/io/github/techtastic/cc_vs/apis/ShipAPI.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package io.github.techtastic.cc_vs.apis
22

3+
import dan200.computercraft.api.lua.IArguments
34
import dan200.computercraft.api.lua.ILuaAPI
5+
import dan200.computercraft.api.lua.LuaException
46
import dan200.computercraft.api.lua.LuaFunction
57
import io.github.techtastic.cc_vs.mixin.ShipObjectWorldAccessor
8+
import io.github.techtastic.cc_vs.util.CCVSUtils
69
import io.github.techtastic.cc_vs.util.CCVSUtils.toLua
10+
import io.github.techtastic.cc_vs.util.CCVSUtils.toVector
711
import net.minecraft.server.level.ServerLevel
12+
import net.minecraft.world.phys.Vec3
813
import org.joml.Vector3d
914
import org.joml.Vector4d
1015
import org.joml.primitives.AABBi
1116
import org.valkyrienskies.core.api.ships.ServerShip
17+
import org.valkyrienskies.core.apigame.constraints.VSAttachmentConstraint
1218
import org.valkyrienskies.core.apigame.constraints.VSConstraintAndId
1319
import org.valkyrienskies.mod.common.shipObjectWorld
20+
import org.valkyrienskies.mod.common.util.toJOML
1421
import kotlin.math.asin
1522
import kotlin.math.atan2
1623

@@ -42,7 +49,7 @@ open class ShipAPI(val ship: ServerShip, val level: ServerLevel) : ILuaAPI {
4249
this.ship.inertiaData.momentOfInertiaTensor.toLua()
4350

4451
@LuaFunction
45-
fun getName(): String = this.ship.slug ?: "no-name"
52+
fun getSlug(): String = this.ship.slug ?: "no-name"
4653

4754
@LuaFunction
4855
fun getOmega(): Map<String, Double> =
@@ -52,22 +59,6 @@ open class ShipAPI(val ship: ServerShip, val level: ServerLevel) : ILuaAPI {
5259
fun getQuaternion(): Map<String, Double> =
5360
this.ship.transform.shipToWorldRotation.toLua()
5461

55-
@LuaFunction
56-
fun getEulerAnglesXYZ() =
57-
this.ship.transform.shipToWorldRotation.getEulerAnglesXYZ(Vector3d()).toLua()
58-
59-
@LuaFunction
60-
fun getEulerAnglesYXZ() =
61-
this.ship.transform.shipToWorldRotation.getEulerAnglesYXZ(Vector3d()).toLua()
62-
63-
@LuaFunction
64-
fun getEulerAnglesZXY() =
65-
this.ship.transform.shipToWorldRotation.getEulerAnglesZXY(Vector3d()).toLua()
66-
67-
@LuaFunction
68-
fun getEulerAnglesZYX() =
69-
this.ship.transform.shipToWorldRotation.getEulerAnglesZYX(Vector3d()).toLua()
70-
7162
@LuaFunction
7263
fun getScale(): Map<String, Double> =
7364
this.ship.transform.shipToWorldScaling.toLua()
@@ -95,14 +86,20 @@ open class ShipAPI(val ship: ServerShip, val level: ServerLevel) : ILuaAPI {
9586
this.ship.transform.positionInWorld.toLua()
9687

9788
@LuaFunction
98-
fun transformPositionToWorld(x: Double, y: Double, z: Double): Map<String, Double> =
99-
this.ship.shipToWorld.transformPosition(Vector3d(x, y, z)).toLua()
89+
fun transformPositionToWorld(args: IArguments): Map<String, Double> {
90+
val pos =
91+
if (args.count() == 1)
92+
Vector3d(args.getTable(0).toVector())
93+
else
94+
Vector3d(args.getDouble(0), args.getDouble(1), args.getDouble(2))
95+
return this.ship.shipToWorld.transformPosition(pos).toLua()
96+
}
10097

10198
@LuaFunction
10299
fun isStatic(): Boolean = this.ship.isStatic
103100

104101
@LuaFunction
105-
fun setName(name: String) {
102+
fun setSlug(name: String) {
106103
this.ship.slug = name
107104
}
108105

common/src/main/kotlin/io/github/techtastic/cc_vs/ship/PhysTickEventHandler.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,38 @@ import io.github.techtastic.cc_vs.PlatformUtils
55
import io.github.techtastic.cc_vs.apis.LuaPhysShip
66
import org.valkyrienskies.core.api.ships.*
77
import org.valkyrienskies.core.impl.game.ships.PhysShipImpl
8+
import java.util.concurrent.ConcurrentLinkedQueue
89

9-
class PhysTickEventHandler: ShipForcesInducer {
10+
class PhysTickEventHandler: ShipForcesInducer, ServerTickListener {
1011
@JsonIgnore
1112
private val computers = mutableListOf<Int>()
13+
private val queuedData = ConcurrentLinkedQueue<LuaPhysShip>()
1214

1315
override fun applyForces(physShip: PhysShip) {
1416
if (!PlatformUtils.exposePhysTick())
1517
return
1618

17-
val physShip = physShip as PhysShipImpl
18-
19-
this.computers.removeIf { PlatformUtils.getComputerByID(it) == null }
20-
21-
this.computers.forEach {
22-
PlatformUtils.getComputerByID(it)?.queueEvent("physics_tick", arrayOf(LuaPhysShip(physShip)))
23-
}
19+
this.queuedData.add(LuaPhysShip(physShip as PhysShipImpl))
2420
}
2521

2622
fun addComputer(id: Int) {
2723
this.computers.add(id)
2824
}
2925

26+
override fun onServerTick() {
27+
this.computers.removeIf { PlatformUtils.getComputerByID(it) == null }
28+
this.computers.forEach {
29+
PlatformUtils.getComputerByID(it)?.queueEvent("physics_ticks", this.queuedData.toTypedArray())
30+
}
31+
this.queuedData.clear()
32+
}
33+
3034
companion object {
3135
fun getOrCreateControl(ship: ServerShip): PhysTickEventHandler {
32-
var control = ship.getAttachment(PhysTickEventHandler::class.java)
36+
var control = ship.getAttachment<PhysTickEventHandler>()
3337
if (control == null) {
3438
control = PhysTickEventHandler()
35-
ship.saveAttachment(PhysTickEventHandler::class.java, control)
39+
ship.saveAttachment<PhysTickEventHandler>(control)
3640
}
3741

3842
return control

common/src/main/kotlin/io/github/techtastic/cc_vs/util/CCVSUtils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.techtastic.cc_vs.util
22

3+
import dan200.computercraft.api.lua.LuaException
34
import dan200.computercraft.shared.computer.core.ComputerFamily
45
import dan200.computercraft.shared.computer.core.ServerComputer
56
import io.github.techtastic.cc_vs.PlatformUtils
@@ -121,4 +122,11 @@ object CCVSUtils {
121122

122123
return constraint
123124
}
125+
126+
fun Map<*, *>.toVector(): Vector3dc {
127+
val posTable = this as? Map<String, Double>
128+
?: throw LuaException("Invalid Argument! Expects either a vector or a table with x, y, and z keys!")
129+
130+
return Vector3d(posTable["x"] ?: 0.0, posTable["y"] ?: 0.0, posTable["z"] ?: 0.0)
131+
}
124132
}

0 commit comments

Comments
 (0)