Advertisement
Guest User

startup

a guest
Aug 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.33 KB | None | 0 0
  1. --by djm1999, with assistance from rahph and galaxtone
  2. -- Get Peripherals
  3. dropper = peripheral.wrap("top")
  4. chest = peripheral.wrap("bottom")
  5. mon = peripheral.wrap("right")
  6.  
  7. --Define Variables
  8. credits = 0 --credits at start
  9. local refund = 0 -- refund flag dont edit
  10. local headLn = 2 -- monitor setup
  11. local headLn2 = 1
  12. local footLn = 12
  13. local currency = "codecoins:oneCoin"
  14.  
  15. --Item Listings
  16. local items = {
  17.   {"       ","",0},
  18.   {"VendTst","",0},
  19.   {"Cobbl","10",2},
  20.   {"Dirt ","02",3},
  21.   {"Sand ","15",4},
  22.   {"Cock ","64",5},
  23.   {"What ","00",6},
  24.   {"shall","00",7},
  25.   {"I    ","00",8},
  26.   {"put  ","00",9},
  27.   {"here ","00",10},
  28.   {"Loading","",1},
  29.   }
  30.  
  31. local formatting = {
  32.     h = {t = "0000000",b = "3333333"},
  33.     f = {t = "fffffff",b = "8888888"},
  34.     e = {t = "00000ff",b = "bbbbb11"},
  35.     o = {t = "00000ff",b = "9999944"},
  36.   err = {t = "0000000",b = "eeeeeee"},
  37.   eOk = {t = "0000055",b = "bbbbb55"},
  38.   oOk = {t = "0000055",b = "9999955"},
  39.  eOut = {t = "00000ee",b = "bbbbbee"},
  40.  oOut = {t = "00000ee",b = "99999ee"},
  41.     }
  42.  
  43. --input handling for items
  44. function check()
  45.   local compar = credits
  46.   local total = 0
  47.   if refund == 0 then
  48.   for i = 1,9 do
  49.     local item = dropper.getItemMeta(i)
  50.     if item then
  51.       if item.name == currency then
  52.          if compar + item.count > 64 then
  53.            return 0
  54.          elseif refund == 0 then
  55.           total = total + dropper.pushItems("down",i,64,1)
  56.           chest.pullItems("up", 1)
  57.          end
  58.       else
  59.          rs.setOutput("top",true)
  60.          sleep(0.08)
  61.          rs.setOutput("top",false)
  62.       end
  63.     end
  64.   end
  65.   else
  66.      repeat
  67.          rs.setOutput("top",true)
  68.          sleep(0.5)
  69.          rs.setOutput("top",false)
  70.          sleep(0.5)
  71.          credits = credits - 1
  72.      until credits == 0
  73.      refund = 0
  74.      update()
  75.   end
  76.   return total
  77. end
  78.  
  79. --check money and vend
  80. function dispense(y)
  81.   if y == headLn or y == headLn2 then
  82.     return
  83.   elseif y == footLn then
  84.       if credits > 0 then
  85.           local slot = items[y][3]
  86.           print("refund")
  87.           chest.pushItems("up",slot,credits,2)
  88.           dropper.pullItems("down",2)
  89.           refund = 1
  90.       end
  91.   else
  92.     if tonumber(items[y][2]) <= credits then
  93.       local slot = items[y][3]
  94.       local iname = chest.getItemMeta(slot)
  95.       if not iname then
  96.         print("Failed: noStock")
  97.         update("stockFail",y)
  98.         return
  99.       end
  100.       if iname.name == currency then
  101.         print("Attempt to vend credit")
  102.         update("stockFail",y)
  103.       else
  104.         chest.pushItems("up",slot,1,2)
  105.         dropper.pullItems("down",2)
  106.         print ("Slot: "..slot)
  107.         credits = credits - tonumber(items[y][2])
  108.         print("Credits: "..credits)
  109.         update("ok",y)
  110.       end
  111.     else
  112.       print("Failed: Money")
  113.       update("credFail")          
  114.     end
  115.   end
  116. end
  117.  
  118. --Draw initial interface
  119. function drawUI()
  120.   term.clear()
  121.   term.setCursorPos(1,1)
  122.   print("VendSystem 1.5")
  123.   print("Press Q to exit")
  124.  
  125.   for i =1,#items do
  126.     local entry = items[i]
  127.     mon.setCursorPos(1,i)
  128.    
  129.     if i == headLn or i == headLn2 then -- header line
  130.         mon.blit(entry[1]..entry[2],formatting.h.t,formatting.h.b)
  131.     elseif i == footLn then -- footer line
  132.         mon.blit(entry[1]..entry[2],formatting.f.t,formatting.f.b)
  133.     elseif i % 2 == 0 then
  134.         mon.blit(entry[1]..entry[2],formatting.e.t,formatting.e.b)
  135.     else
  136.         mon.blit(entry[1]..entry[2],formatting.o.t,formatting.o.b)
  137.     end
  138.    
  139.   end  
  140. end
  141.  
  142. --update interface
  143. function update(...)
  144.    if arg[1] == "credFail" then
  145.      mon.setCursorPos(1,footLn)  
  146.      mon.blit("Lo Cred",
  147.       formatting.err.t,formatting.err.b)
  148.      sleep(1)
  149.    elseif arg[1] == "stockFail" then
  150.      mon.setCursorPos(1,footLn)
  151.      mon.blit("No Stoc",
  152.       formatting.err.t, formatting.err.b)
  153.      mon.setCursorPos(1, arg[2])
  154.      if arg[2] % 2 == 0 then
  155.       mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.eOut.t, formatting.eOut.b)
  156.      else
  157.       mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.oOut.t, formatting.oOut.b)
  158.      end
  159.      elseif arg[1] == "ok" then
  160.     print("OK")
  161.        mon.setCursorPos(1, arg[2])
  162.     if arg[2] % 2 == 0 then
  163.       mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.eOk.t,formatting.eOk.b)
  164.          sleep(1)
  165.       mon.setCursorPos(1, arg[2])
  166.          mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.e.t,formatting.e.b)
  167.     else
  168.       mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.oOk.t,formatting.oOk.b)
  169.          sleep(1)
  170.       mon.setCursorPos(1, arg[2])
  171.          mon.blit(items[arg[2]][1]..items[arg[2]][2],formatting.o.t,formatting.o.b)
  172.     end
  173.   end
  174.   mon.setCursorPos(1,footLn)
  175.   mon.blit(
  176.     "Cred "..string.format("%02d",tostring(credits)),
  177.     formatting.f.t, formatting.f.b)
  178. end
  179.  
  180.  
  181.  
  182. drawUI()
  183. update()
  184.  
  185. -- Events
  186. local timer, r
  187. while true do
  188.   timer = os.startTimer(1)
  189.   e = {os.pullEventRaw()}
  190.   os.cancelTimer(timer)
  191.   if e[1] == "char" then
  192.     if e[2] == "q" then -- Hit q to quit.
  193.       break
  194.     end
  195.   elseif e[1] == "timer" then
  196.     r = check()
  197.     if r > 0 then
  198.       print(tostring(r).." items moved.")
  199.       credits = credits + r
  200.       update()
  201.       print(tostring(credits).." credits")
  202.     end
  203.   elseif e[1] == "monitor_touch" then
  204.     print("Monitor touched: "..tostring(e[3]..", "..e[4]))
  205.     dispense(e[4])
  206.   end
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement