CCCPaste

StartupControl

term.setBackgroundColor(colours.black)
term.clear()
term.setCursorPos(1,1)
--
local airshipConfig = textutils.unserialize(fs.open("disk/airshipConfig.txt",'r').readAll())

local airshipName = airshipConfig.airshipName
local airshipChannel = tonumber(airshipConfig.airshipChannel)

--
local keyboard = peripheral.find("tm_keyboard")
keyboard.setFireNativeEvents(true)

local modem = peripheral.wrap('left')
local wiredModem = peripheral.wrap('bottom')
--
local axis = {
    strafe_x = 0,
    strafe_y = 0,
    strafe_z = 0,
    --
    turn_x = 0,
    turn_y = 0,
    turn_z = 0
}
--
local keys = {}
    keys[87] = {name = 'w', axes = {strafe_z = 1}}
    keys[65] = {name = 'a', axes = {turn_y = -1}}
    keys[83] = {name = 's', axes = {strafe_z = -1}}
    keys[68] = {name = 'd', axes = {turn_y = 1}}
    keys[32] = {name = 'space', axes = {strafe_y = 1}}
    keys[341] = {name = 'ctrl', axes = {strafe_y = -1}}
    keys[81] = {name = 'q', axes = {strafe_x = -1}}
    keys[69] = {name = 'e', axes = {strafe_x = 1}}


--#FUNCTIONS
local function calculateInput()
    --
    local temp_axes = {
        strafe_x = {},
        strafe_y = {},
        strafe_z = {},
        --
        turn_x = {},
        turn_y = {},
        turn_z = {}
    }
    local temp_axes2 = {}
    --
    for i,key in pairs(keys) do
        if key.axes then
            for axis_name,key_axis in pairs(key.axes) do
                --print(axis_name,key_axis)
                if key.pressed == 1 then
                    table.insert(temp_axes[tostring(axis_name)],key_axis)
                end
            end
        end
    end
    --
    for i,current_axis in pairs(temp_axes) do
        local calculated_average = 0
        if type(current_axis) == "table" and #current_axis ~= 0 then
            local num = 0
            --
            for i2, axis_num in pairs(current_axis) do
                num = num + axis_num
            end
            --
            calculated_average = num/#current_axis
        end
        --
        temp_axes2[i] = calculated_average
        print(i,calculated_average)
    end
    --
    return temp_axes2
end

local function doInput(changed_key,pressed)
    --print(changed_key)
    if keys[changed_key] then keys[changed_key].pressed = pressed end
    return calculateInput()
end

local function transmitCommand(temp_axes)
    local airshipTransmission = {'airshipTransmission',airshipName,temp_axes}
    --
    wiredModem.transmit(airshipChannel+2,airshipChannel+3,airshipTransmission)
end

--#MAIN--
for i,key in pairs(keys) do
    key.pressed = 0
end
--
while true do
    local function handleEvent(event, key, is_held)
        local temp_axes
        if event == 'key' and is_held == false then
            temp_axes = doInput(key,1)
        elseif event == 'key_up' then
            temp_axes = doInput(key,0)
        end
        --
        return temp_axes
    end
    local temp_axes = handleEvent(os.pullEvent())
    --
    if temp_axes then
        transmitCommand(temp_axes)
    end
end