~jordanreger/cc

ref: bfe6059601b2a01c9560b244f2f101d08d933797 cc/xOS/.x -rw-r--r-- 4.1 KiB
bfe60596Jordan Reger feat: "multi-threading" and better functions 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
local width, height = term.getSize()
local background_color = colors.lightGray
local window = window.create(term.current(), 1, 1, width, height)
local page = "home"

function setup()
    window.setBackgroundColor(background_color)
    if (background_color ~= colors.black and background_color ~= colors.gray) then
        window.setTextColor(colors.black)
    else
        window.setTextColor(colors.white)
    end
    window.clear()
end

function render()
    if page == "home" then
        render_home()
    elseif page == "xOrder" then
        render_xOrder()
    elseif page == "xGPS" then
        render_xGPS()
    end
end

function writeCentered(str, y)
    if (math.fmod(string.len(str), 2) ~= 1) then
        window.setCursorPos(width / 2 - ((string.len(str) - 2) / 2), y)
        window.write(str)
    else
        window.setCursorPos(width / 2 - ((string.len(str) - 1) / 2), y)
        window.write(str)
    end
end

function render_home()
    window.clear()

    -- time
    local time = textutils.formatTime(os.time())

    if (string.len(time) < 8) then
        time = "0" .. time
    end

    writeCentered(time, 1)

    -- menu
    writeCentered("xOrder", height / 2)
    writeCentered("xGPS", height / 2 + 1)
    writeCentered("Shutdown", height / 2 + 2)
end

function render_xOrder()
    window.clear()
    writeCentered("Coming soon!", height / 2)
    writeCentered("<-- back", height / 2 + 2)
end

function render_xGPS()
    -- local x, y, z = gps.locate()
    -- x, y, z = math.floor(x), math.floor(y), math.floor(z)
    -- writeCentered(x .. " " .. y .. " " .. z, height / 2)

    window.clear()
    writeCentered("Coming soon!", height / 2)
    writeCentered("<-- back", height / 2 + 2)
end

-- background tasks

function automatic_doors()
    local receiver_id = 17

    rednet.send(receiver_id, "gps")
    local r_id, r_pos = rednet.receive()

    local r_x, r_y, r_z = r_pos[1], r_pos[2], r_pos[3]
    local receiver = vector.new(r_x, r_y, r_z)

    local x, y, z = gps.locate()
    local client = vector.new(x, y, z)

    function update_pos()
        x, y, z = gps.locate()
        client = vector.new(x, y, z)
    end

    while true do
        local displacement = receiver - client
        if ((displacement.x > 0) and (displacement.x < 2)) and ((displacement.y < 2) and (displacement.y > 0)) and ((displacement.z > -1) and (displacement.z < 1)) then
            rednet.send(receiver_id, "open")
        end
        
        if((displacement.x < 0) and (displacement.x > -2)) and ((displacement.y < 2) and (displacement.y > 0)) and ((displacement.z > -1) and (displacement.z < 1)) then
            rednet.send(receiver_id, "open")
        end
        update_pos()
    end
end

-- function location()
--     while true do
--         local x, y, z = gps.locate()
--         x, y, z = math.floor(x), math.floor(y), math.floor(z)
--         rednet.send(21, x .. " " .. y .. " " .. z)
--         sleep(30)
--     end
-- end

-- prerender tasks

function on_click()
    while true do
        local e, id, x, y = os.pullEvent("mouse_click")
        if e then
            if page == "home" then
                -- get clicks on *centered* text
                if (x >= width / 2 - 2 and x <= width / 2 + 2 and y == height / 2) then
                    page = "xOrder"
                elseif (x >= width / 2 - 1 and x <= width / 2 + 1 and y == height / 2 + 1) then
                    page = "xGPS"
                elseif (x >= width / 2 - 3 and x <= width / 2 + 4 and y == height / 2 + 2) then
                    os.shutdown()
                else
                    return
                end
            end
            
            if page == "xOrder" then
                if (x >= width / 2 - 3 and x <= width / 2 + 3 and y == height / 2 + 2) then
                    page = "home"
                end
            end

            if page == "xGPS" then
                if (x >= width / 2 - 3 and x <= width / 2 + 3 and y == height / 2 + 2) then
                    page = "home"
                end
            end
        end
    end
end

function main()
    render()

    sleep(0.25)
    main()
end

setup()

parallel.waitForAll(main, on_click, automatic_doors)