Experia888

StartupMovement

Nov 28th, 2025 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. term.setBackgroundColor(colours.black)
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. --
  5. local propConfig = textutils.unserialize(fs.open("propConfig.txt",'r').readAll())
  6. local propList = propConfig.props
  7. local relayList = propConfig.relays
  8. local rotorList = {}
  9.  
  10. local airshipConfig = textutils.unserialize(fs.open("airshipConfig.txt",'r').readAll())
  11. local airshipName = airshipConfig.airshipName
  12. local airshipChannel = tonumber(airshipConfig.airshipChannel)
  13. --
  14. local modem = peripheral.wrap('back')
  15. local all_sides = {"front","back","top","bottom","left","right"}
  16.  
  17. local function adjustSpeedController(prop,multiplier)
  18.     --print(peripheral.getType(prop.rotor))
  19.     --print(prop.rotorName,multiplier)
  20.     if prop.type == 'Create_RotationSpeedController' then
  21.         prop.rotor.setTargetSpeed(prop.speed*multiplier)
  22.     elseif prop.type == 'electric_motor' then
  23.         prop.rotor.setSpeed(prop.speed*multiplier)
  24.     end
  25. end
  26.  
  27. local function adjustRelay(relay,command_axes)
  28. for i2,axis in pairs(relay.axes) do
  29.             if command_axes[i2] > 0 then
  30.                 if axis.pos then
  31.                     relay.relay.setOutput(axis.pos,true)
  32.                 end
  33.                 if axis.neg then
  34.                     relay.relay.setOutput(axis.neg,false)
  35.                 end
  36.             elseif command_axes[i2] < 0 then
  37.                 if axis.neg then
  38.                     relay.relay.setOutput(axis.neg,true)
  39.                 end
  40.                 if axis.pos then
  41.                     relay.relay.setOutput(axis.pos,false)
  42.                 end
  43.             else
  44.                 relay.relay.setOutput(axis.pos,false)
  45.                 relay.relay.setOutput(axis.neg,false)
  46.             end
  47.         end
  48. end
  49.  
  50. local function handleMovementCommand(command_axes)
  51.     --#props
  52.     for i,prop in pairs(propList) do
  53.         local num = 0
  54.         local total = 0
  55.         local calculated_average = 0
  56.         --
  57.         for i2,axis in pairs(prop.axes) do
  58.             local relative_axis = command_axes[i2]
  59.             if relative_axis ~= 0 and prop.axes[i2] ~= 0 then
  60.                 --print(i2,prop.axes[i2])
  61.                 total = total + 1
  62.                 num = num + relative_axis*axis
  63.             end
  64.         end
  65.         if total ~= 0 then
  66.             --print(total)
  67.            calculated_average = (num/total)
  68.         end
  69.         local ok, err = pcall(adjustSpeedController,prop,calculated_average)
  70.     end
  71.     --#relays
  72.     for i,relay in pairs(relayList) do
  73.         adjustRelay(relay,command_axes)
  74.     end
  75. end
  76.  
  77. local function handleTransmission(event, modemSide, senderChannel, replyChannel, transmission, senderDistance)
  78.     if event == 'modem_message' and transmission[1] and transmission[1] == 'airshipTransmission' and senderDistance < 100 then
  79.         if transmission[2] == airshipName then
  80.             --print('Received Airship Command Transmission:',airshipName)
  81.             --
  82.             local command_axes = transmission[3]
  83.             handleMovementCommand(command_axes)
  84.         end
  85.     end
  86. end
  87. --
  88. for i,prop in pairs(propList) do
  89.     --print('Got Rotor:',prop.peripheralName)
  90.     prop.rotor = peripheral.wrap(prop.peripheralName)
  91.     prop.type = peripheral.getType(prop.rotor)
  92.     adjustSpeedController(prop,0)
  93. end
  94.  
  95. for i,relay in pairs(relayList) do
  96.     --print('Got Relay:',relay.peripheralName)
  97.     relay.relay = peripheral.wrap(relay.peripheralName)
  98.     for i,side in pairs(all_sides) do
  99.         relay.relay.setOutput(side, false)
  100.     end
  101. end
  102.  
  103. modem.open(airshipChannel)
  104. while true do
  105.     local airshipTransmission = handleTransmission(os.pullEvent())
  106. end
Advertisement
Add Comment
Please, Sign In to add comment