Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- storefront
- -- By Unionhawk
- -- Base code for a record store that uses an interactive sorter and advanced monitor
- -- Uses Direwolf20's Button API http://pastebin.com/S8x0K3ui
- -- pastebin get S8x0K3ui button
- -- Designed for advanced monitor size 3x3, input as an ender chest.
- -- Update the peripheral.wraps as appropriate (don't forget to modify the button API as well)
- os.loadAPI("button")
- sorter = peripheral.wrap("top")
- mon = peripheral.wrap("back")
- price = 10
- currency = 264 -- Item ID for Diamond
- cursorPos = 1
- -- Direction:
- -- 0=down, 1=up, 2=-Z, 3=+Z, 4=-X, 5=+X
- inputDirection = 2
- stockDirection = 1
- paymentDirection = 4
- function checkInput() -- Checks the input chest and returns the number of specified items
- inChest = sorter.list(inputDirection)
- for id,count in pairs(inChest) do
- if id==currency then
- return count
- end
- end
- return 0
- end
- function checkStock() -- Checks the stock chest and returns a table of id,count, adds all other records and sets their count to zero
- stock = {}
- stockChest = sorter.list(stockDirection)
- for id,count in pairs(stockChest) do
- stock[id] = count
- end
- for i = 2256, 2267 do
- if stock[i] == nil then
- stock[i]=0
- end
- end
- return stock
- end
- function inStock(item) -- Returns true if the specified item ID is in the chest. Returns false otherwise.
- if (checkStock())[item] < 1 then
- return false
- else
- return true
- end
- end
- function trade(item) -- Checks for payment, checks for stock, then makes the trade.
- if checkInput() < price then
- mon.setCursorPos(1,3)
- mon.write("Insufficient funds")
- return false
- end
- if not inStock(item) then
- mon.setCursorPos(1,3)
- mon.write("Out of stock")
- return false
- end
- sorter.extract(inputDirection, currency, paymentDirection, price)
- sorter.extract(stockDirection, item, inputDirection, 1)
- return true
- end
- function purchase() -- Function that the button "Purchase" executes - it uses the cursor position to determine which record to pick
- if not trade(2255 + cursorPos) then
- button.flash("Purchase")
- button.flash("Purchase")
- button.flash("Purchase")
- sleep(1)
- end
- end
- function cursor() -- Writes the cursor onto the screen.
- mon.setCursorPos(1,cursorPos+4)
- mon.write(">")
- end
- function cursorUp() -- Moves the cursor up. If the cursor is at the top, moves cursor to the bottom
- mon.setCursorPos(1,cursorPos+4)
- mon.write(" ")
- if cursorPos==1 then
- cursorPos = 12
- else
- cursorPos = cursorPos - 1
- end
- end
- function cursorDown() -- Moves cursor down. If cursor is at the bottom, moves cursor to the top
- mon.setCursorPos(1,cursorPos+4)
- mon.write(" ")
- if cursorPos == 12 then
- cursorPos = 1
- else
- cursorPos = cursorPos + 1
- end
- end
- function fillTable() -- Setting up the buttons
- button.setTable("^", cursorUp, true, 26, 27, 5, 8)
- button.setTable("v", cursorDown, true, 26, 27, 13, 16)
- button.setTable("Purchase", purchase, true, 10, 19, 18, 19)
- end
- function display() -- Function to update display all at once
- dia = checkInput()
- stock = checkStock()
- mon.clear()
- term.redirect(mon)
- term.setCursorPos(1,1)
- print("Diamonds in: ", dia)
- print("All records ", price, " diamonds.")
- print(" ")
- print("Select a record: ")
- print(" C418 - 13: ", stock[2256])
- print(" C418 - cat: ", stock[2257])
- print(" C418 - blocks: ", stock[2258])
- print(" C418 - chirp: ", stock[2259])
- print(" C418 - far: ", stock[2260])
- print(" C418 - mall: ", stock[2261])
- print(" C418 - mellohi: ", stock[2262])
- print(" C418 - stal: ", stock[2263])
- print(" C418 - strad: ", stock[2264])
- print(" C418 - ward: ", stock[2265])
- print(" C418 - 11: ", stock[2266])
- print(" C418 - wait: ", stock[2267])
- term.restore()
- cursor()
- button.screen()
- end
- -- "Main" module
- fillTable()
- while true do -- pullEvent loop - updates the display, waits for a monitor touch, performs the appropriate action (if any), then updates the display.
- display()
- event, side, x, y = os.pullEvent("monitor_touch")
- button.checkxy(x,y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement