From 539fdd92fe0ce46fe6e6e73841c63d05347953de Mon Sep 17 00:00:00 2001 From: templeofshadow Date: Mon, 29 Jun 2026 19:46:03 -0500 Subject: [PATCH] hopefully fix some issues regarding thruster initialization --- main.lua | 157 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 114 insertions(+), 43 deletions(-) diff --git a/main.lua b/main.lua index 89e1a5f..020e140 100644 --- a/main.lua +++ b/main.lua @@ -249,51 +249,53 @@ function PropellerInit() end for pi, pv in ipairs(propellers) do - for ti, tv in ipairs(transmissions) do - if pv.getSubnetworkAnchorId() == tv.getSelfId() then - if Thrusters[peripheral.getName(pv)] == nil then - Thrusters[peripheral.getName(pv)] = { - type = "rotator", - thruster = pv, - transmission = peripheral.wrap(tv), - --[[ - NOTE: - affectVectors will have values depending on how the thruster's - thrust vector interacts with the vessel's attitude - angular: - possible directions for angular.yaw: "port"|"star" - possible directions for angular.roll: "port"|"star" - possible directions for angular.pitch: "up"|"down" - lateral: - possible directions for lateral.x: "port"|"star" - possible directions for lateral.y: "up"|"down" - possible directions for lateral.z: "fore"|"aft" - ]] - affectVectors = { - angular = { - yaw = nil, - pitch = nil, - roll = nil + if Thrusters[peripheral.getName(pv)] == nil then + for ti, tv in ipairs(transmissions) do + if pv.getSubnetworkAnchorId() == tv.getSelfId() then + if Thrusters[peripheral.getName(pv)] == nil then + Thrusters[peripheral.getName(pv)] = { + type = "rotator", + thruster = pv, + transmission = peripheral.wrap(tv), + --[[ + NOTE: + affectVectors will have values depending on how the thruster's + thrust vector interacts with the vessel's attitude + angular: + possible directions for angular.yaw: "port"|"star" + possible directions for angular.roll: "port"|"star" + possible directions for angular.pitch: "up"|"down" + lateral: + possible directions for lateral.x: "port"|"star" + possible directions for lateral.y: "up"|"down" + possible directions for lateral.z: "fore"|"aft" + ]] + affectVectors = { + angular = { + yaw = nil, + pitch = nil, + roll = nil + }, + lateral = { + x = nil, + y = nil, + z = nil + } }, - lateral = { - x = nil, - y = nil, - z = nil - } - }, - power = (tv.getSignal())/15 - } - end - if Thrusters[peripheral.getName(pv)] ~= nil then - if Thrusters[peripheral.getName(pv)].type == nil then - Thrusters[peripheral.getName(pv)].type = "rotator" + power = (tv.getSignal())/15 + } end - Thrusters[peripheral.getName(pv)].thruster = pv - Thrusters[peripheral.getName(pv)].transmission = peripheral.wrap(tv) - if Thrusters[peripheral.getName(pv)].affectVectors == nil then - Thrusters[peripheral.getName(pv)].affectVectors = {} + if Thrusters[peripheral.getName(pv)] ~= nil then + if Thrusters[peripheral.getName(pv)].type == nil then + Thrusters[peripheral.getName(pv)].type = "rotator" + end + Thrusters[peripheral.getName(pv)].thruster = pv + Thrusters[peripheral.getName(pv)].transmission = peripheral.wrap(tv) + if Thrusters[peripheral.getName(pv)].affectVectors == nil then + Thrusters[peripheral.getName(pv)].affectVectors = {} + end + Thrusters[peripheral.getName(pv)].power = (tv.getSignal())/15 end - Thrusters[peripheral.getName(pv)].power = (tv.getSignal())/15 end end end @@ -390,6 +392,66 @@ local function checkIfThrusterIsIndexed() return unindexedThrusters end +-- this is for populating nil values from thruster config files +local function populateThrusterValues() + for _, thruster in pairs(Thrusters) do + if thruster.name == nil and thruster.thruster == nil then -- as long as we have one of thruster or name, we can fetch other data + print("ERROR: Thruster "..peripheral.getName(thruster.thruster).." has no name or thruster object. Removing entry in thruster list") + Thrusters[_] = nil + return + end + if thruster.name == nil and thruster.thruster ~= nil then + thruster.name = peripheral.getName(thruster.thruster) + end + + if thruster.thruster == nil and thruster.name ~= nil then + thruster.thruster = peripheral.wrap(thruster.name) + end + if thruster.type == nil then + for _, tt in ipairs(ThrusterTypes) do + if peripheral.getType(thruster.thruster) == tt then + thruster.type = "thruster" + break + end + end + + for _, pt in ipairs(PropellerTypes) do + if peripheral.getType(thruster.thruster) == pt then + thruster.type = "rotator" + break + end + end + end + if thruster.type == "rotator" and thruster.transmission == nil then + local subnetworkId = peripheral.wrap(thruster.name).getSubnetworkAnchorId() + for _, transmissionType in ipairs(TransmissionTypes) do + local transmissions = peripheral.find(transmissionType) + for _, transmission in ipairs(transmissions) do + if transmission.getSelfId() == subnetworkId then + thruster.transmission = peripheral.getName(transmission) + end + end + end + end + if thruster.power == nil then + if thruster.type == "thruster" then + thruster.power = thruster.thruster.getPower() + elseif thruster.type == "rotator" then + if thruster.transmission ~= nil then + local transmissionType = peripheral.getType(thruster.transmission) + if transmissionType == "analog_transmission" then + thruster.power = (thruster.transmission.getSignal())/15 + elseif transmissionType == "Create_RotationSpeedController" then + thruster.power = (thruster.transmission.getTargetSpeed())/256 + end + else + print("ERROR: Thruster "..peripheral.getName(thruster.thruster).." has no transmission.") + end + end + end + end +end + local function partiallyUpdateThrusters(thrusterList) local thrusterTypes = { "thruster", @@ -855,7 +917,10 @@ function Init() end end - -- If we're here, the config loaded successfully and the initialization is done. + -- If we're here, the config loaded successfully. + + populateThrusterValues() + Update() end -- Display functions for monitors @@ -956,6 +1021,12 @@ function collectSensorData() end function WriteConfigFiles() + -- since the thruster object will change every time, set each thruster's thruster object to nil before writing to the config file, so that the config file only contains the thruster names and not the thruster objects + for _, thruster in pairs(Thrusters) do + thruster.thruster = nil + thruster.transmission = nil + end + print("Writing to thruster config file.") local thrusterConfigFile = fs.open(Config.thrusterConfigPath, "w+") thrusterConfigFile.write(tableToString(Thrusters))