Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --by djm1999, with assistance from rahph and galaxtone
- -- Get Peripherals
- dropper = peripheral.wrap("top")
- chest = peripheral.wrap("bottom")
- mon = peripheral.wrap("right")
- --Define Variables
- credits = 0 --credits at start
- local refund = 0 -- refund flag dont edit
- local headLn = 2 -- monitor setup
- local headLn2 = 1
- local footLn = 12
- local currency = "codecoins:oneCoin"
- --Item Listings
- local items = {
- {" ","",0},
- {"VendTst","",0},
- {"Cobbl","10",2},
- {"Dirt ","02",3},
- {"Sand ","15",4},
- {"Cock ","64",5},
- {"What ","00",6},
- {"shall","00",7},
- {"I ","00",8},
- {"put ","00",9},
- {"here ","00",10},
- {"Loading","",1},
- }
- local formatting = {
- h = {t = "0000000",b = "3333333"},
- f = {t = "fffffff",b = "8888888"},
- e = {t = "00000ff",b = "bbbbb11"},
- o = {t = "00000ff",b = "9999944"},
- err = {t = "0000000",b = "eeeeeee"},
- eOk = {t = "0000055",b = "bbbbb55"},
- oOk = {t = "0000055",b = "9999955"},
- eOut = {t = "00000ee",b = "bbbbbee"},
- oOut = {t = "00000ee",b = "99999ee"},
- }
- --input handling for items
- function check()
- local compar = credits
- local total = 0
- if refund == 0 then
- for i = 1,9 do
- local item = dropper.getItemMeta(i)
- if item then
- if item.name == currency then
- if compar + item.count > 64 then
- return 0
- elseif refund == 0 then
- total = total + dropper.pushItems("down",i,64,1)
- chest.pullItems("up", 1)
- end
- else
- rs.setOutput("top",true)
- sleep(0.08)
- rs.setOutput("top",false)
- end
- end
- end
- else
- repeat
- rs.setOutput("top",true)
- sleep(0.5)
- rs.setOutput("top",false)
- sleep(0.5)
- credits = credits - 1
- until credits == 0
- refund = 0
- update()
- end
- return total
- end
- --check money and vend
- function dispense(y)
- if y == headLn or y == headLn2 then
- return
- elseif y == footLn then
- if credits > 0 then
- local slot = items[y][3]
- print("refund")
- chest.pushItems("up",slot,credits,2)
- dropper.pullItems("down",2)
- refund = 1
- end
- else
- if tonumber(items[y][2]) <= credits then
- local slot = items[y][3]
- local iname = chest.getItemMeta(slot)
- if not iname then
- print("Failed: noStock")
- update("stockFail",y)
- return
- end
- if iname.name == currency then
- print("Attempt to vend credit")
- update("stockFail",y)
- else
- chest.pushItems("up",slot,1,2)
- dropper.pullItems("down",2)
- print ("Slot: "..slot)
- credits = credits - tonumber(items[y][2])
- print("Credits: "..credits)
- update("ok",y)
- end
- else
- print("Failed: Money")
- update("credFail")
- end
- end
- end
- --Draw initial interface
- function drawUI()
- term.clear()
- term.setCursorPos(1,1)
- print("VendSystem 1.5")
- print("Press Q to exit")
- for i =1,#items do
- local entry = items[i]
- mon.setCursorPos(1,i)
- if i == headLn or i == headLn2 then -- header line
- mon.blit(entry[1]..entry[2],formatting.h.t,formatting.h.b)
- elseif i == footLn then -- footer line
- mon.blit(entry[1]..entry[2],formatting.f.t,formatting.f.b)
- elseif i % 2 == 0 then
- mon.blit(entry[1]..entry[2],formatting.e.t,formatting.e.b)
- else
- mon.blit(entry[1]..entry[2],formatting.o.t,formatting.o.b)
- end
- end
- end
- --update interface
- function update(...)
- if arg[1] == "credFail" then
- mon.setCursorPos(1,footLn)
- mon.blit("Lo Cred",
- formatting.err.t,formatting.err.b)
- sleep(1)
- elseif arg[1] == "stockFail" then
- mon.setCursorPos(1,footLn)
- mon.blit("No Stoc",
- formatting.err.t, formatting.err.b)
- mon.setCursorPos(1, arg[2])
- if arg[2] % 2 == 0 then
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.eOut.t, formatting.eOut.b)
- else
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.oOut.t, formatting.oOut.b)
- end
- elseif arg[1] == "ok" then
- print("OK")
- mon.setCursorPos(1, arg[2])
- if arg[2] % 2 == 0 then
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.eOk.t,formatting.eOk.b)
- sleep(1)
- mon.setCursorPos(1, arg[2])
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.e.t,formatting.e.b)
- else
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.oOk.t,formatting.oOk.b)
- sleep(1)
- mon.setCursorPos(1, arg[2])
- mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.o.t,formatting.o.b)
- end
- end
- mon.setCursorPos(1,footLn)
- mon.blit(
- "Cred "..string.format("%02d",tostring(credits)),
- formatting.f.t, formatting.f.b)
- end
- drawUI()
- update()
- -- Events
- local timer, r
- while true do
- timer = os.startTimer(1)
- e = {os.pullEventRaw()}
- os.cancelTimer(timer)
- if e[1] == "char" then
- if e[2] == "q" then -- Hit q to quit.
- break
- end
- elseif e[1] == "timer" then
- r = check()
- if r > 0 then
- print(tostring(r).." items moved.")
- credits = credits + r
- update()
- print(tostring(credits).." credits")
- end
- elseif e[1] == "monitor_touch" then
- print("Monitor touched: "..tostring(e[3]..", "..e[4]))
- dispense(e[4])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement