Advertisement
pferguson2212

SimpleMenu

Apr 11th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. local curx = 0
  2. local cury = 0
  3. local curz = 0
  4.  
  5. local storeroomx = 0
  6. local storeroomy = 0
  7. local storeroomz = 0
  8.  
  9. local dir = 1 -- 1=Forward, 2=Right, 3=Back & 4=Left
  10. local storemanID = nil
  11.  
  12. local running = true
  13.  
  14. chest = {}
  15. chest.__index = chest
  16.  
  17. function chest.create(x, y, z)
  18. local chst = {}
  19. setmetatable(chst, chest)
  20. chst.x = x
  21. chst.y = y
  22. chst.z = z
  23. return chst
  24. end
  25.  
  26. -- Menu to create/print ... do everything really :P/>
  27. local function showMenu( tMenu, sTitle, startXPos, startYPos )
  28. -- This function just prints the menu
  29. local function printMenu( tMenu, sTitle, nSelected, startXPos, startYPos )
  30. term.setCursorPos( 1, 1)
  31. write("Store Room")
  32. term.setCursorPos( 1, 2)
  33. write("Please select option from the menu below")
  34.  
  35. term.setCursorPos( startXPos, startYPos)
  36. write(sTitle)
  37. for index, text in pairs( tMenu ) do
  38. term.setCursorPos( startXPos, startYPos + index )
  39. write( (nSelected == index and '> ' or ' ') .. text )
  40. end
  41. term.setCursorPos( 1, 19)
  42. write("Program by FergieP2212")
  43. if (storemanID ~= nil) then
  44. term.setCursorPos( 40, 19)
  45. write("Storeman:" .. storemanID)
  46. end
  47. end
  48. -- Set default selection to the first one
  49. local selection = 1
  50. -- Inifinite loop until enter is clicked
  51. while true do
  52. printMenu( tMenu, sTitle, selection, startXPos, startYPos )
  53. event, but = os.pullEvent("key")
  54. if but == keys.up then
  55. -- Up
  56. selection = selection - 1
  57. elseif but == keys.down then
  58. -- Down
  59. selection = selection + 1
  60. elseif but == keys.enter then
  61. -- Enter
  62. return tMenu[selection], selection -- returns the text AND the number
  63. end
  64. -- Advanced way to make the selection 1 if you have gone past the amount of items in the menu,
  65. -- or make it the amount of items in the menu/table if you have gone past above the first selection
  66. selection = selection < 1 and #tMenu or selection > #tMenu and 1 or selection
  67. end
  68. end
  69.  
  70. local function showMessage(message)
  71. term.setCursorPos(1, 18)
  72. write(" ")
  73. term.setCursorPos(1, 18)
  74. write(message)
  75. end
  76.  
  77. local function getStoreman()
  78. showMessage("Locating Storeman...")
  79. rednet.broadcast("Storeman?")
  80. local id, message = rednet.receive(5)
  81. if id~=nil then
  82. showMessage("Found storeman " .. id)
  83. storemanID = id
  84. else
  85. showMessage("No storeman available.")
  86. end
  87. end
  88.  
  89. term.clear()
  90.  
  91. print("Starting app")
  92. print("Checking parameters")
  93. local args = { ... }
  94. if #args ~= 3 then
  95. print("Incorrect usage: menu <x> <y> <z>")
  96. error()
  97. end
  98.  
  99. assert(type(args[1]) ~= "number", "Incorrect parameter <x>: invalid format")
  100. storeroomx = args[1]
  101. assert(type(args[2]) ~= "number", "Incorrect parameter <y>: invalid format")
  102. storeroomy = args[2]
  103. assert(type(args[3]) ~= "number", "Incorrect parameter <z>: invalid format")
  104. storeroomz = args[3]
  105.  
  106. print("Opening connection")
  107. rednet.open("left")
  108.  
  109. getStoreman()
  110. if storemanID == nil then
  111. print("No storeman available.")
  112. running = false
  113. exit()
  114. else
  115. local msg = "SetWarehouse"
  116. if storeroomx < 10 then msg = msg .. "0" .. storeroomx else msg = msg .. storeroomx end
  117. if storeroomy < 10 then msg = msg .. "0" .. storeroomy else msg = msg .. storeroomy end
  118. if storeroomz < 10 then msg = msg .. "0" .. storeroomz else msg = msg .. storeroomz end
  119. rednet.broadcast(msg)
  120. end
  121.  
  122. local tMainMenu = {
  123. "Store Item",
  124. "Get Item",
  125. "Do Inventory Check",
  126. "Close"
  127. }
  128.  
  129. print("Showing main menu")
  130.  
  131. while running == true do
  132. term.clear()
  133.  
  134. sOption, nNumb = showMenu( tMainMenu, "Main Menu", 5, 5 ) -- start the function ... 'sOption' and 'nNumb'
  135. term.setCursorPos(1, 1)
  136.  
  137. if nNumb == 1 then
  138. showMessage("Store Item")
  139. rednet.send(storemanID, "GoGet")
  140. elseif nNumb == 2 then
  141. showMessage("Get Item")
  142. if storemanID == nil then getStoreman() end
  143. elseif nNumb == 3 then
  144. showMessage("Do Inventory Check")
  145. else
  146. running = false
  147. term.clear()
  148. showMessage("Goodbye")
  149. end
  150.  
  151. end
  152.  
  153. rednet.close("left")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement