~jordanreger/cc

757a3ad6434a3ad31816f1ce7f02a55e060ffe9e — Jordan Reger a month ago bfe6059
feat(xIndustries): initial commit
13 files changed, 358 insertions(+), 176 deletions(-)

M README.md
R automatic_doors/README.md => xDoors/README.md
R automatic_doors/client.lua => xDoors/client.lua
R automatic_doors/receiver.lua => xDoors/receiver.lua
A xIndustries/Counter/count.lua
A xIndustries/Counter/startup.lua
A xIndustries/Inventory/get.lua
A xIndustries/Inventory/startup.lua
A xIndustries/POS/gui.lua
A xIndustries/POS/shop.lua
A xIndustries/POS/startup.lua
D xOS/.x
D xOS/startup.lua
M README.md => README.md +9 -2
@@ 1,8 1,15 @@
# ~jordanreger/cc
This is a collection of my one-off [computercraft](https://tweaked.cc) scripts.

## automatic door system
To read more about the automatic door system, read the [README](https://git.sr.ht/~jordanreger/cc/tree/main/item/automatic_doors/README.md) in the `automatic_doors` directory. This is part of an ongoing effort to create a "smart home" system in Minecraft.
## xDoors
To read more about the xDoors automatic door system, read the [README](https://git.sr.ht/~jordanreger/cc/tree/main/item/automatic_doors/README.md) in the `automatic_doors` directory. This is part of an ongoing effort to create a "smart home" system in Minecraft.

## xIndustries
xIndustries is the store of the future, in Minecraft. All you have to do is walk in, select what you want from a massive screen, drop the correct payment into a hopper, and a turtle will automatically dispense your item above you.

![Outside the store](https://skynetfree.net/_AA4rH1rpmhwkrZc0LoE6z6XYdInRTQFS_g9WDSwRrbXfw)

![Inside the store](https://skynetfree.net/fAGu9SwY_eDtcAClRsUXSnn6HAaSg5saJo2eeDaehKvVZw)

## srht.lua
This script allows you to get other scripts from the [paste tool](https://paste.sr.ht). To download this onto your computer in Minecraft, run the following:

R automatic_doors/README.md => xDoors/README.md +1 -1
@@ 1,4 1,4 @@
# automatic door system
# xDoors
By utilising a GPS cluster, a wireless pocket computer, iron doors, and a receiver computer, you can make a flawless security system that opens the doors to your house just by walking "into" them.

> todo(jordanreger): add photos of setup
\ No newline at end of file

R automatic_doors/client.lua => xDoors/client.lua +0 -0
R automatic_doors/receiver.lua => xDoors/receiver.lua +0 -0
A xIndustries/Counter/count.lua => xIndustries/Counter/count.lua +44 -0
@@ 0,0 1,44 @@
rednet.open("left")

while true do
    local id, price = rednet.receive()
    price_value, price_type = price:match"([^.]*).(.*)"
    price_value = tonumber(price_value)

    local paid = false

    repeat
        while turtle.getItemCount(1) ~= price_value do
            turtle.suck(64)

            if turtle.getItemCount(1) == price_value then
                break
            end
        end
        payment_type = turtle.getItemDetail(1)
        if (price_type == "i") then
            if (payment_type.name == "minecraft:iron_ingot") then
                paid = true
                turtle.transferTo(2)
            else
                turtle.dropUp(64)
            end
        elseif (price_type == "g") then
            if (payment_type.name == "minecraft:gold_ingot") then
                paid = true
                turtle.transferTo(3)
            else
                turtle.dropUp(64)
            end
        elseif (price_type == "d") then
            if (payment_type.name == "minecraft:diamond") then
                paid = true
                turtle.transferTo(4)
            else
                turtle.dropUp(64)
            end
        end
    until paid == true

    rednet.send(21, true)
end

A xIndustries/Counter/startup.lua => xIndustries/Counter/startup.lua +1 -0
@@ 0,0 1,1 @@
shell.run("count.lua")

A xIndustries/Inventory/get.lua => xIndustries/Inventory/get.lua +141 -0
@@ 0,0 1,141 @@
rednet.open("left")

local inventory = {
    ["minecraft:cooked_beef"] = vector.new(677, 95, 724),
    ["minecraft:ender_pearl"] = vector.new(677, 96, 724),
    ["minecraft:blaze_rod"] = vector.new(677, 97, 724)
}

while true do
    local id, dispense = rednet.receive()
    dispense_quantity, dispense_item = dispense:match"([^.]*).(.*)"
    dispense_quantity = tonumber(dispense_quantity)
    local chest_pos = tostring(inventory[dispense_item])
    local c_x, c_y, c_z = chest_pos:match"([^.]*),([^.]*),(.*)"
    local c_x, c_y, c_z = tonumber(c_x), tonumber(c_y), tonumber(c_z)
    local at_chest = false

    repeat
        local t_x, t_y, t_z = gps.locate()
        while t_x ~= c_x do
            turtle.forward()
            local t_x, t_y, t_z = gps.locate()
            if(t_x == c_x) then break end
        end
        while t_y ~= c_y do
            turtle.up()
            local t_x, t_y, t_z = gps.locate()
            if(t_y == c_y) then break end
        end
        if t_z ~= c_z then
            local direction = ""
            if(t_z > c_z) then
                turtle.turnRight()
                direction = "right"
            end
            if(t_z < c_z) then
                turtle.turnLeft()
                direction = "left"
            end
            while t_z ~= c_z do
                turtle.forward()
                local t_x, t_y, t_z = gps.locate()
                if(t_z == c_z) then break end
            end
            if direction == "right" then
                turtle.turnLeft()
            end

            if direction == "left" then
                turtle.turnRight()
            end
        end
        at_chest = true
    until at_chest == true
    
    turtle.suck(dispense_quantity)

    local d_x, d_y, d_z = 681, 95, 720
    local at_drop_off = false

    repeat
        local t_x, t_y, t_z = gps.locate()
        while t_x ~= d_x do
            turtle.back()
            local t_x, t_y, t_z = gps.locate()
            if(t_x == d_x) then break end
        end
        while t_y ~= d_y do
            turtle.down()
            local t_x, t_y, t_z = gps.locate()
            if(t_y == d_y) then break end
        end
        if t_z ~= d_z then
            local direction = ""
            if(t_z > d_z) then
                turtle.turnRight()
                direction = "right"
            end
            if(t_z < d_z) then
                turtle.turnLeft()
                direction = "left"
            end
            while t_z ~= d_z do
                turtle.forward()
                local t_x, t_y, t_z = gps.locate()
                if(t_z == d_z) then break end
            end
            if direction == "right" then
                turtle.turnLeft()
            end

            if direction == "left" then
                turtle.turnRight()
            end
        end
        at_drop_off = true
    until at_drop_off == true

    turtle.dropDown(dispense_quantity)

    local h_x, h_y, h_z = 684, 95, 724
    local at_home = false

    repeat
        local t_x, t_y, t_z = gps.locate()
        while t_x ~= h_x do
            turtle.back()
            local t_x, t_y, t_z = gps.locate()
            if(t_x == h_x) then break end
        end
        while t_y ~= h_y do
            turtle.down()
            local t_x, t_y, t_z = gps.locate()
            if(t_y == h_y) then break end
        end
        if t_z ~= h_z then
            local direction = ""
            if(t_z > h_z) then
                turtle.turnRight()
                direction = "right"
            end
            if(t_z < h_z) then
                turtle.turnLeft()
                direction = "left"
            end
            while t_z ~= h_z do
                turtle.forward()
                local t_x, t_y, t_z = gps.locate()
                if(t_z == h_z) then break end
            end
            if direction == "right" then
                turtle.turnLeft()
            end

            if direction == "left" then
                turtle.turnRight()
            end
        end
        at_home = true
    until at_home == true
end

A xIndustries/Inventory/startup.lua => xIndustries/Inventory/startup.lua +1 -0
@@ 0,0 1,1 @@
shell.run("get.lua")
\ No newline at end of file

A xIndustries/POS/gui.lua => xIndustries/POS/gui.lua +40 -0
@@ 0,0 1,40 @@
local monitor = peripheral.find("monitor")
local width, height = monitor.getSize()

function setup()
    monitor.clear()
    monitor.setTextScale(1)
end

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

function render()
    write_centered("Welcome to xIndustries!", height / 2)

    write_centered("Shop -->", height / 2 + 2)
end

setup()
render()

local redirected = false

repeat
    local e, side, x, y = os.pullEvent("monitor_touch")
    if e then
        if (x >= width / 2 - 4 and x <= width / 2 + 4 and y == height / 2 + 2) then
            redirected = true
            function redirect() shell.run("shop.lua") end
            function exit_shell() shell.exit() end
            parallel.waitForAll(redirect, exit_shell)
        end
    end
until redirected == true

A xIndustries/POS/shop.lua => xIndustries/POS/shop.lua +120 -0
@@ 0,0 1,120 @@
rednet.open("top")

local monitor = peripheral.find("monitor")
local width, height = monitor.getSize()

function setup()
    monitor.clear()
end

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

function render()
    write_centered("xIndustries", 1)

    -- Steak
    monitor.setCursorPos(2, 3)
    monitor.write("4 Steak - 1i")

    -- Ender Pearls
    monitor.setCursorPos(2, 5)
    monitor.write("Ender Pearl - 1d")

    -- Blaze Rods
    monitor.setCursorPos(2, 7)
    monitor.write("Blaze Rods - 1d")

    write_centered("<-- back", height - 1)
end

setup()
render()

local redirected = false

local item, quantity = "", 0

repeat
    local e, side, x, y = os.pullEvent("monitor_touch")
    if e then
        -- Steak
        if (x >= 2 and x <= 14 and y == 3) then
            redirected = true
            rednet.send(22, "1.i")
            item = "minecraft:cooked_beef"
            quantity = 4
        end

        -- Ender Pearl
        if (x >= 2 and x <= 18 and y == 5) then
            redirected = true
            rednet.send(22, "1.d")
            item = "minecraft:ender_pearl"
            quantity = 1
        end

        -- Blaze Rods
        if (x >= 2 and x <= 17 and y == 7) then
            redirected = true
            rednet.send(22, "1.d")
            item = "minecraft:blaze_rod"
            quantity = 1
        end

        -- return to home
        if (x >= width / 2 - 4 and x <= width / 2 + 4 and y == height - 1) then
            redirected = true
            function redirect() shell.run("gui.lua") end
            function exit_shell() shell.exit() end
            parallel.waitForAll(redirect, exit_shell)
        end
    end
until redirected == true

function payment_render()
    monitor.clear()

    write_centered("xIndustries", 1)

    write_centered("Please drop payment below!", height / 2)
    write_centered("(incorrect payment types in chest)", height - 1)
end

payment_render()

local id, paid = rednet.receive(60)

if not id then
    function redirect() shell.run("gui.lua") end
    function exit_shell() shell.exit() end
    parallel.waitForAll(redirect, exit_shell)
else 
    local dispense = (quantity .. "." .. item)

    function paid_render()
        monitor.clear()

        write_centered("xIndustries", 1)

        write_centered("Paid! Dispensing above.", height / 2)
    end

    if(paid) then
        rednet.send(23, dispense)
        paid_render()
    end

    sleep(5)

    function redirect() shell.run("gui.lua") end
    function exit_shell() shell.exit() end
    parallel.waitForAll(redirect, exit_shell)
end

A xIndustries/POS/startup.lua => xIndustries/POS/startup.lua +1 -0
@@ 0,0 1,1 @@
shell.run("gui.lua")

D xOS/.x => xOS/.x +0 -154
@@ 1,154 0,0 @@
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)
\ No newline at end of file

D xOS/startup.lua => xOS/startup.lua +0 -19
@@ 1,19 0,0 @@
rednet.open("back")

local width, height = term.getSize()

term.clear()
term.setCursorPos(width / 2 - 1, height / 2 - 2)
print("x  x")
term.setCursorPos(width / 2 - 1, height / 2 - 1)
print("  x ")
term.setCursorPos(width / 2 - 1, height / 2)
print(" x  ")
term.setCursorPos(width / 2 - 1, height / 2 + 1)
print("x  x")
term.setCursorPos(width / 2 - 3, height / 2 + 3)
print("xOS v0.1")
term.setCursorPos(width / 2 - 4, height / 2 + 4)
textutils.slowPrint("xxxxxxxxxx")
sleep(1)
shell.run(".x")
\ No newline at end of file