function DrawBackground(w,h)
    term.clear();
    paintutils.drawFilledBox(1,1,w,h,colors.white);
    paintutils.drawFilledBox(1,1,w,1,colors.blue);
    term.setBackgroundColor(colors.blue);
    term.setTextColor(colors.orange);
    local info = "CAInnon"
    term.setCursorPos(w/2-string.len(info)/2,1);
    term.write(info);
    term.setTextColor(colors.white);
    info = "ID: "..tostring(thisID);
    term.setCursorPos(1,1);
    term.write(info);
    term.setBackgroundColor(colors.white);
    term.setTextColor(colors.black);
end

function playerAiming()
    while true do
        os.pullEvent("redstone");
    if playerAim == 1 then
        PL = coord.getPlayersAll(100)
        for key,val in pairs(PL) do
            if val.name == Owner then
                pointRange = 25;
                pointX = val.x + pointRange*val.viewVector.x;
                pointY = val.y + pointRange*val.viewVector.y;
                pointZ = val.z + pointRange*val.viewVector.z;
                while pointRange<range do
                    if coord.isBlockAir(pointX,pointY,pointZ)==false then
                        EXEMODE = 3;
                        cmd = {EXEMODE,pointX,pointZ,pointY};
                        for i = 1, #computerID do
                            rednet.send(computerID[i],cmd)
                        end
                        break
                    else
                        pointRange =pointRange + aimingScale;
                        pointX = pointX + aimingScale*val.viewVector.x;
                        pointY = pointY + aimingScale*val.viewVector.y;
                        pointZ = pointZ + aimingScale*val.viewVector.z;
                    end
                end
                if pointRange>range then
                    EXEMODE = 3;
                    cmd = {EXEMODE,pointX,pointZ,pointY};
                    for i = 1, #computerID do
                        rednet.send(computerID[i],cmd)
                    end
                end
            end
        end
        sleep(3);
    else
        sleep(5);
    end
end
end

function Panel()
    while true do
    local cmd = {}
    local w,h = term.getSize()
    DrawBackground(w,h);
    local info = "Status: "..switch;
    term.setCursorPos(w/2-string.len(info)/2,2);
    term.write(info);
    info = "Location Mode"
    term.setCursorPos(w/2-string.len(info)/2,3);
    term.setBackgroundColor(colors.red);
    term.write(info);
    term.setBackgroundColor(colors.white);
    info = "Horizontal Dev:"..string.format("%.1f",horfix);
    term.setCursorPos(w/2-string.len(info)/2,4);
    term.write(info);
    term.setCursorPos(10,4);
    term.write("-----");
    term.setCursorPos(35,4);
    term.write("+++++");
    info = "Select Owner"
    term.setCursorPos(w/2-string.len(info)/2,6);
    term.setBackgroundColor(colors.blue);
    term.write(info);
    info = "Change cannon computer ID"
    term.setCursorPos(w/2-string.len(info)/2,7);
    term.setBackgroundColor(colors.green);
    term.write(info);
    term.setBackgroundColor(colors.white);
    local evn,but,cursx,cursy = os.pullEvent("mouse_click");
    if cursy == 2 then
        if EXEMODE ~= 0 then
            EXEMODE = 0;
            switch ="off";
            playerAim = 0;
            sleep(0.2);
            DrawBackground(w,h)
            info = "System Paused";
            term.setCursorPos(w/2-string.len(info)/2,3);
            term.write(info);
            sleep(0.4);
            cmd = {EXEMODE};
            for i = 1, #computerID do
                rednet.send(computerID[i],cmd)
            end
        else
            DrawBackground(w,h)
            info = "System Already Paused";
            term.setCursorPos(w/2-string.len(info)/2,3);
            term.write(info);
            sleep(0.4);
        end
    elseif cursy == 3 then
        DrawBackground(w,h);
        info = "Type the location";
        term.setCursorPos(w/2-string.len(info)/2,3);
        term.write(info);
        info = "Player Aiming Mode";
        term.setCursorPos(w/2-string.len(info)/2,5);
        term.write(info);
        local evn,but,cursxx,cursyy = os.pullEvent("mouse_click");
        if cursyy == 3 then
        DrawBackground(w,h);
        info = "Enter the target x axis";
        term.setCursorPos(w/2-string.len(info)/2,3);
        term.write(info);
        term.setCursorPos(w/2-3,4);
        xtgt0 = tonumber(read());
        info = "Enter the target z axis";
        term.setCursorPos(w/2-string.len(info)/2,5);
        term.write(info);
        term.setCursorPos(w/2-3,6);
        ztgt0 = tonumber(read())
        info = "Enter the target y axis";
        term.setCursorPos(w/2-string.len(info)/2,7);
        term.write(info);
        term.setCursorPos(w/2-3,8);
        ytgt0 = tonumber(read());
        if type(xtgt0)=="number" and type(ztgt0)=="number" and type(ytgt0)=="number" then
            xtgt1 = xtgt0;
            ztgt1 = ztgt0;
            ytgt1 = ytgt0;
            EXEMODE = 3;
            switch = "Location"
            info = "Aiming to that shit";
            term.setCursorPos(w/2-string.len(info)/2,9);
            term.write(info);
            cmd = {EXEMODE,xtgt1,ztgt1,ytgt1};
            for i = 1, #computerID do
                rednet.send(computerID[i],cmd)
            end
        else
            info = "Invalid Values";
            term.setCursorPos(w/2-string.len(info)/2,9);
            term.write(info);
        end
    elseif cursyy == 5 then
        if Owner ~=nil then
        EXEMODE = 3;
        playerAim = 1;
        switch = "Aming from "..Owner;
        info = "Player Aiming Start";
        term.setCursorPos(w/2-string.len(info)/2,7);
        term.write(info);
        sleep(1)
        else
            info = "Owner is not assigned ya you FAILURE";
            term.setCursorPos(w/2-string.len(info)/2,7);
            term.write(info);
            sleep(1)
        end
    end
    elseif cursy == 4 then
        if cursx <=15 then
            horfix = horfix - 0.2
        elseif cursx>=35 then
            horfix = horfix + 0.2;
        elseif cursx >20 and cursx < 30 then
            horfix = 0;
        end
        if horfix >20 then
            horfix = 20
        elseif horfix<-20 then
            horfix = -20;
        end
        cmd = {5,horfix};
        for i = 1, #computerID do
            rednet.send(computerID[i],cmd);
        end
    elseif cursy == 6 then
        DrawBackground(w,h);
        players = {}
        E = coord.getEntities(50);
        playernum = 0;
        for key, val in pairs(E) do
            if val.type == "minecraft:player" then
                playernum = playernum + 1;
                players[playernum] = val.name;
                term.setCursorPos(w/2-string.len(players[playernum])/2,playernum+1);
                term.write(players[playernum]);
            end
        end
        local evn,but,cursxxx,cursyyy = os.pullEvent("mouse_click");
        if cursyyy>1 then
            if players[cursyyy-1]~=nil then
                Owner = players[cursyyy-1];
                DrawBackground(w,h)
                info = "Set Owner to "..Owner;
                term.setCursorPos(w/2-string.len(info)/2,5);
                term.write(info);
                sleep(0.7);
            end
        end
    elseif cursy == 7 then
        DrawBackground(w,h)
        info = "Current computers are"
        term.setCursorPos(w/2-string.len(info)/2,3);
        term.write(info);
        term.setBackgroundColor(colors.orange);
        for i = 1,#computerID do
            term.setCursorPos(w/2,3+i);
            term.write(computerID[i]);
        end
        term.setBackgroundColor(colors.white);
        info = "Back"
        term.setCursorPos(w/2-string.len(info)/2,10);
        term.write(info);
        local evn,but,cursxxx,cursyyy = os.pullEvent("mouse_click");
        if cursyyy >= 4 and cursyyy<=7 then
            DrawBackground(w,h)
            info = "Enter new computer number"
            term.setCursorPos(w/2-string.len(info)/2,3);
            term.write(info);
            term.setCursorPos(w/2-2,4)
            newComputerNum = tonumber(read())
            if type(newComputerNum) == "number" then
                computerID[cursyyy-3] = newComputerNum
                info = "Computer Updated As"
                term.setCursorPos(w/2-string.len(info)/2,5);
                term.write(info);
                turretComputers = io.open("turretComputers.lua", "w");
                term.setBackgroundColor(colors.orange);
                for i = 1,#computerID do
                    term.setCursorPos(w/2,5+i);
                    term.write(computerID[i]);
                    turretComputers:write(tostring(computerID[i]).."\n");
                end
                term.setBackgroundColor(colors.white);
                turretComputers:close();
            else
                info = "Invalid value"
                term.setCursorPos(w/2-string.len(info)/2,5);
                term.write(info);
            end
            info = "Press to continue"
            term.setCursorPos(w/2-string.len(info)/2,12);
            term.write(info);
            local evn,but,cursxxx,cursyyy = os.pullEvent("mouse_click");
        end
    end
    sleep(0.1);
end
end

function AddItem()
    TerminalBackground();
    info = "Type in the name of Item"
    term.setCursorPos(W/2-string.len(info)/2,4);
    term.write(info);
    term.setCursorPos(W/2-1,5)
    itemadd0 = read();
    info = "Type in the amount"
    term.setCursorPos(W/2-string.len(info)/2,6);
    term.write(info);
    term.setCursorPos(W/2-1,7)
    amount0 = tonumber(read());
    info = "Type in the price of single"
    term.setCursorPos(W/2-string.len(info)/2,8);
    term.write(info);
    term.setCursorPos(W/2-1,9)
    price0 = tonumber(read());
    if type(price0)=="number" and amount0 ~=nil and amount0>0 then
        N = N + 1;
        Item[N] = itemadd0;
        Num[N] = amount0;
        Price[N] = price0;
        info = "Successfully Added"
        term.setCursorPos(W/2-string.len(info)/2,10);
        term.write(info);
        sleep(0.5);
    else
        info = "Error Processing"
        term.setCursorPos(W/2-string.len(info)/2,10);
        term.write(info);
        sleep(1);
    end
end

turretComputers = io.open("turretComputers.lua","r");
if turretComputers == nil then
    turretComputers = io.open("turretComputers.lua", "w");
    turretComputers:write("-1\n");
    turretComputers:write("-1\n");
    turretComputers:write("-1\n");
    turretComputers:write("-1\n");
    turretComputers:write("-1\n");
    turretComputers:write("-1");
    turretComputers:close();
end

thisID = os.computerID()
computerID = {}
computerIdNum = 0;
for line in io.lines("turretComputers.lua") do
    line = tonumber(line);
    computerIdNum = computerIdNum +1;
    if type(line) == "number" then
        table.insert(computerID, line);
    end
    if computerIdNum >= 4 then
        break
    end
end

mod = peripheral.find("modem");
if mod == nil then
    os.shutdown();
end
mod.open(386);
rednet.open(peripheral.getName(mod));
c = peripheral.find("chatBox");

MODE = "flat";
EXEMODE = 0;
switch = "off";

horfix = 0;
verfix = 0;
range = 500;
aimingScale = 2;
playerAim = 0;

Owner = nil;

parallel.waitForAll(Panel,playerAiming);