Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.setBackgroundColor(colours.black)
- term.clear()
- term.setCursorPos(1,1)
- --
- local propConfig = textutils.unserialize(fs.open("propConfig.txt",'r').readAll())
- local propList = propConfig.props
- local relayList = propConfig.relays
- local rotorList = {}
- local airshipConfig = textutils.unserialize(fs.open("airshipConfig.txt",'r').readAll())
- local airshipName = airshipConfig.airshipName
- local airshipChannel = tonumber(airshipConfig.airshipChannel)
- --
- local modem = peripheral.wrap('back')
- local all_sides = {"front","back","top","bottom","left","right"}
- local function adjustSpeedController(prop,multiplier)
- --print(peripheral.getType(prop.rotor))
- --print(prop.rotorName,multiplier)
- if prop.type == 'Create_RotationSpeedController' then
- prop.rotor.setTargetSpeed(prop.speed*multiplier)
- elseif prop.type == 'electric_motor' then
- prop.rotor.setSpeed(prop.speed*multiplier)
- end
- end
- local function adjustRelay(relay,command_axes)
- for i2,axis in pairs(relay.axes) do
- if command_axes[i2] > 0 then
- if axis.pos then
- relay.relay.setOutput(axis.pos,true)
- end
- if axis.neg then
- relay.relay.setOutput(axis.neg,false)
- end
- elseif command_axes[i2] < 0 then
- if axis.neg then
- relay.relay.setOutput(axis.neg,true)
- end
- if axis.pos then
- relay.relay.setOutput(axis.pos,false)
- end
- else
- relay.relay.setOutput(axis.pos,false)
- relay.relay.setOutput(axis.neg,false)
- end
- end
- end
- local function handleMovementCommand(command_axes)
- --#props
- for i,prop in pairs(propList) do
- local num = 0
- local total = 0
- local calculated_average = 0
- --
- for i2,axis in pairs(prop.axes) do
- local relative_axis = command_axes[i2]
- if relative_axis ~= 0 and prop.axes[i2] ~= 0 then
- --print(i2,prop.axes[i2])
- total = total + 1
- num = num + relative_axis*axis
- end
- end
- if total ~= 0 then
- --print(total)
- calculated_average = (num/total)
- end
- local ok, err = pcall(adjustSpeedController,prop,calculated_average)
- end
- --#relays
- for i,relay in pairs(relayList) do
- adjustRelay(relay,command_axes)
- end
- end
- local function handleTransmission(event, modemSide, senderChannel, replyChannel, transmission, senderDistance)
- if event == 'modem_message' and transmission[1] and transmission[1] == 'airshipTransmission' and senderDistance < 100 then
- if transmission[2] == airshipName then
- --print('Received Airship Command Transmission:',airshipName)
- --
- local command_axes = transmission[3]
- handleMovementCommand(command_axes)
- end
- end
- end
- --
- for i,prop in pairs(propList) do
- --print('Got Rotor:',prop.peripheralName)
- prop.rotor = peripheral.wrap(prop.peripheralName)
- prop.type = peripheral.getType(prop.rotor)
- adjustSpeedController(prop,0)
- end
- for i,relay in pairs(relayList) do
- --print('Got Relay:',relay.peripheralName)
- relay.relay = peripheral.wrap(relay.peripheralName)
- for i,side in pairs(all_sides) do
- relay.relay.setOutput(side, false)
- end
- end
- modem.open(airshipChannel)
- while true do
- local airshipTransmission = handleTransmission(os.pullEvent())
- end
Advertisement
Add Comment
Please, Sign In to add comment