Advertisement
Guest User

storefront

a guest
Dec 12th, 2013
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. -- storefront
  2. -- By Unionhawk
  3. -- Base code for a record store that uses an interactive sorter and advanced monitor
  4. -- Uses Direwolf20's Button API http://pastebin.com/S8x0K3ui
  5. -- pastebin get S8x0K3ui button
  6. -- Designed for advanced monitor size 3x3, input as an ender chest.
  7. -- Update the peripheral.wraps as appropriate (don't forget to modify the button API as well)
  8.  
  9. os.loadAPI("button")
  10. sorter = peripheral.wrap("top")
  11. mon = peripheral.wrap("back")
  12.  
  13. price = 10
  14. currency = 264 -- Item ID for Diamond
  15. cursorPos = 1
  16.  
  17. -- Direction:
  18. -- 0=down, 1=up, 2=-Z, 3=+Z, 4=-X, 5=+X
  19. inputDirection = 2
  20. stockDirection = 1
  21. paymentDirection = 4
  22.  
  23. function checkInput() -- Checks the input chest and returns the number of specified items
  24.   inChest = sorter.list(inputDirection)
  25.   for id,count in pairs(inChest) do
  26.     if id==currency then
  27.       return count
  28.     end
  29.   end
  30.   return 0
  31. end
  32.  
  33. function checkStock() -- Checks the stock chest and returns a table of id,count, adds all other records and sets their count to zero
  34.   stock = {}
  35.   stockChest = sorter.list(stockDirection)
  36.   for id,count in pairs(stockChest) do
  37.     stock[id] = count
  38.   end
  39.   for i = 2256, 2267 do
  40.     if stock[i] == nil then
  41.       stock[i]=0
  42.     end
  43.   end
  44.   return stock
  45. end
  46.  
  47. function inStock(item) -- Returns true if the specified item ID is in the chest. Returns false otherwise.
  48.   if (checkStock())[item] < 1 then
  49.     return false
  50.   else
  51.     return true
  52.   end
  53. end
  54.  
  55. function trade(item) -- Checks for payment, checks for stock, then makes the trade.
  56.   if checkInput() < price then
  57.     mon.setCursorPos(1,3)
  58.     mon.write("Insufficient funds")
  59.     return false
  60.   end
  61.   if not inStock(item) then
  62.     mon.setCursorPos(1,3)
  63.     mon.write("Out of stock")
  64.     return false
  65.   end
  66.   sorter.extract(inputDirection, currency, paymentDirection, price)
  67.   sorter.extract(stockDirection, item, inputDirection, 1)
  68.   return true
  69. end
  70.  
  71. function purchase() -- Function that the button "Purchase" executes - it uses the cursor position to determine which record to pick
  72.   if not trade(2255 + cursorPos) then
  73.     button.flash("Purchase")
  74.     button.flash("Purchase")
  75.     button.flash("Purchase")
  76.     sleep(1)
  77.   end
  78. end
  79.  
  80. function cursor() -- Writes the cursor onto the screen.
  81.   mon.setCursorPos(1,cursorPos+4)
  82.   mon.write(">")
  83. end
  84.  
  85. function cursorUp() -- Moves the cursor up. If the cursor is at the top, moves cursor to the bottom
  86.   mon.setCursorPos(1,cursorPos+4)
  87.   mon.write(" ")
  88.   if cursorPos==1 then
  89.     cursorPos = 12
  90.   else
  91.     cursorPos = cursorPos - 1
  92.   end
  93. end
  94.  
  95. function cursorDown() -- Moves cursor down. If cursor is at the bottom, moves cursor to the top
  96.   mon.setCursorPos(1,cursorPos+4)
  97.   mon.write(" ")
  98.   if cursorPos == 12 then
  99.     cursorPos = 1
  100.   else
  101.     cursorPos = cursorPos + 1
  102.   end
  103. end
  104.  
  105. function fillTable() -- Setting up the buttons
  106.   button.setTable("^", cursorUp, true, 26, 27, 5, 8)
  107.   button.setTable("v", cursorDown, true, 26, 27, 13, 16)
  108.   button.setTable("Purchase", purchase, true, 10, 19, 18, 19)
  109. end
  110.  
  111. function display() -- Function to update display all at once
  112.   dia = checkInput()
  113.   stock = checkStock()
  114.  
  115.   mon.clear()
  116.   term.redirect(mon)
  117.   term.setCursorPos(1,1)
  118.   print("Diamonds in: ", dia)
  119.   print("All records ", price, " diamonds.")
  120.   print(" ")
  121.   print("Select a record: ")
  122.   print(" C418 - 13: ", stock[2256])
  123.   print(" C418 - cat: ", stock[2257])
  124.   print(" C418 - blocks: ", stock[2258])
  125.   print(" C418 - chirp: ", stock[2259])
  126.   print(" C418 - far: ", stock[2260])
  127.   print(" C418 - mall: ", stock[2261])
  128.   print(" C418 - mellohi: ", stock[2262])
  129.   print(" C418 - stal: ", stock[2263])
  130.   print(" C418 - strad: ", stock[2264])
  131.   print(" C418 - ward: ", stock[2265])
  132.   print(" C418 - 11: ", stock[2266])
  133.   print(" C418 - wait: ", stock[2267])
  134.  
  135.   term.restore()
  136.   cursor()
  137.   button.screen()
  138. end
  139.  
  140.  
  141. -- "Main" module
  142. fillTable()
  143. while true do -- pullEvent loop - updates the display, waits for a monitor touch, performs the appropriate action (if any), then updates the display.
  144.   display()
  145.   event, side, x, y = os.pullEvent("monitor_touch")
  146.   button.checkxy(x,y)
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement