Advertisement
FakoTheGreat

startup

Nov 6th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. -------------------------------------------------
  2. -- This script is designed to handle a touch
  3. -- screen controlled mystcraft portal, and
  4. -- requires the Touchpoint API to run (pFHeia96),
  5. -- saved as "touchpoint" on the computer.
  6. --
  7. -- Most important variables to adjust are listed
  8. -- first, and the script assumes that you have
  9. -- two chests (one for storage, one for removal),
  10. -- as well as this computer and a book receptacle
  11. -- adjacent to the storage chest. You also need
  12. -- an advanced monitor connected to the computer.
  13. -------------------------------------------------
  14. os.loadAPI("touchpoint") --Do not adjust.
  15.  
  16. --Use either the side of the computer touching the
  17. --object, or the modem connecting them.
  18. local monitorLocation = "back"
  19. local chestLocation = "iron_4"
  20.  
  21. --Use compass direction from the storage chest to
  22. --the listed object (or up/down).
  23. local directionToPortal = "up"
  24. local directionToEject = "down"
  25.  
  26. --Primary customization options are below.
  27. local textSize = 1
  28. local minimumNameLength = 4
  29. local buttonPadding = 1 --Min 1, Space before/after
  30. local centerPadding = 0 --Min 0, Space before/after
  31. local idleButtonColor = colors.blue
  32. local activeButtonColor = colors.cyan
  33. local portalDuration = 5 --In Seconds
  34. local showEject = true
  35. local showPageNum = true
  36. local showTotalPages = true
  37. local pageSeparator = " of "
  38.  
  39. --Secondary customization options are below.
  40. local drawBorders = false
  41. local borderSymbol1 = "-"
  42. local borderSymbol2 = "+"
  43. local pageBack = "Prev Page"
  44. local pageFore = "Next Page"
  45. local ejectButton = "Eject"
  46. local resetButton = "Refresh"
  47.  
  48. --Used internally. Do not adjust.
  49. local mon = peripheral.wrap(monitorLocation)
  50. local touch = touchpoint.new(monitorLocation)
  51. local chest = peripheral.wrap(chestLocation)
  52. local monX = 0
  53. local monY = 0
  54. local monMidL = 0
  55. local monMidR = 0
  56. local maxNameLength = 0
  57. local problem = false
  58. local iBC = idleButtonColor
  59. local aBC = activeButtonColor
  60. local names = {}
  61. local curPage = 1
  62. local lastPage = 1
  63. local namesPerPage = 1
  64. local pages = {}
  65. local book = {}
  66. local eviction = false
  67. local pLabel
  68. local event, p1
  69.  
  70. function centerText(cString)
  71. local sSize = string.len(cString)
  72. offset = math.floor((monX - sSize) / 2) + 1
  73. return offset
  74. end
  75.  
  76. function populateNames()
  77. names = {}
  78.  
  79. local invSize = chest.getInventorySize()
  80. local stackInfo = chest.getAllStacks()
  81. for a=1,invSize + 1,1 do
  82. if stackInfo[a] ~= nil then
  83. book = chest.getStackInSlot(a)
  84. names[#names + 1] = {name = book["myst_book"]["destination"],slot = a}
  85. print(#names..": "..names[#names]["name"]..", "..names[#names]["slot"])
  86. end
  87. end
  88. end
  89.  
  90. function returnDir(myDir)
  91. if myDir == "north" then
  92. return "south"
  93. elseif myDir == "south" then
  94. return "north"
  95. elseif myDir == "east" then
  96. return "west"
  97. elseif myDir == "west" then
  98. return "east"
  99. elseif myDir == "down" then
  100. return "up"
  101. elseif myDir == "up" then
  102. return "down"
  103. else
  104. problem = true
  105. return "oops..."
  106. end
  107. end
  108.  
  109. function prepareScreen()
  110. mon.clear()
  111. mon.setTextScale(textSize)
  112. monX, monY = mon.getSize()
  113.  
  114. if (monY < 5) or (monX < (minimumNameLength * 2) + 4) then
  115. mon.setBackgroundColor(colors.red)
  116. mon.setTextColor(colors.black)
  117. mon.setCursorPos(1,1)
  118. mon.write("!!!")
  119. print("Screen too small with current resolution, please adjust.")
  120. problem = true
  121. else
  122. if math.floor(monX/2) == math.ceil(monX/2) then --Even
  123. monMidR = monX/2 + 1
  124. monMidL = monMidR - 1
  125. maxNameLength = (monX - 4)/2 - (centerPadding + buttonPadding) * 2
  126. else --Odd
  127. monMidR = math.floor(monX/2) + 1
  128. monMidL = monMidR
  129. maxNameLength = (monX - 3)/2 - (centerPadding + buttonPadding) * 2
  130. end
  131. namesPerPage = math.ceil((monY - 4) / 2) * 2
  132. end
  133. end
  134.  
  135. function checkDirections()
  136. if directionToEject == "oops..." then
  137. print("Direction to Eject isn't a valid forge direction.")
  138. print("Please use north, south, east, west, up or down.")
  139. end
  140.  
  141. if directionToPortal == "oops..." then
  142. print("Direction to Portal isn't a valid forge direction.")
  143. print("Please use north, south, east, west, up or down.")
  144. end
  145. end
  146.  
  147. function initialPull()
  148. chest.pullItemIntoSlot(directionToPortal,1,1,1)
  149. end
  150.  
  151. function determineLength(myString)
  152. local theLength = 0
  153. if string.len(myString) > maxNameLength then
  154. theLength = maxNameLength + buttonPadding * 2
  155. else
  156. theLength = string.len(myString) + buttonPadding * 2
  157. end
  158.  
  159. return theLength
  160. end
  161.  
  162. function drawRuler(vert)
  163. local toggle = true
  164. for a=1,monX,1 do
  165. mon.setCursorPos(a,vert)
  166. if toggle then
  167. mon.write(borderSymbol1)
  168. toggle = false
  169. else
  170. mon.write(borderSymbol2)
  171. toggle = true
  172. end
  173. end
  174. end
  175.  
  176. function drawLabels()
  177. mon.setBackgroundColor(colors.black)
  178.  
  179. if showPageNum then
  180. if showTotalPages then
  181. pLabel = curPage..pageSeparator..#pages
  182. else
  183. pLabel = curPage
  184. end
  185.  
  186. mon.setCursorPos(centerText(pLabel),1)
  187. mon.write(pLabel)
  188. end
  189.  
  190. if drawBorders then
  191. drawRuler(2)
  192. drawRuler(monY - 1)
  193. end
  194. end
  195.  
  196. function prevPage()
  197. lastPage = curPage
  198.  
  199. if eviction then
  200. evictBook()
  201. end
  202.  
  203. pages[lastPage]:toggleButton(pageBack)
  204. drawLabels()
  205. os.sleep(0.125)
  206.  
  207. curPage = curPage - 1
  208. if curPage < 1 then
  209. curPage = #pages
  210. end
  211. pages[lastPage]:toggleButton(pageBack)
  212. drawLabels()
  213. end
  214.  
  215. function nextPage()
  216. lastPage = curPage
  217.  
  218. if eviction then
  219. evictBook()
  220. end
  221.  
  222. pages[lastPage]:toggleButton(pageFore)
  223. drawLabels()
  224. os.sleep(0.125)
  225.  
  226. curPage = curPage + 1
  227. if curPage > #pages then
  228. curPage = 1
  229. end
  230. pages[lastPage]:toggleButton(pageFore)
  231. pages[curPage]:draw()
  232. drawLabels()
  233. end
  234.  
  235. function doAction()
  236. pages[curPage]:toggleButton(p1)
  237. drawLabels()
  238. for k,v in ipairs(names) do
  239. if v["name"] == p1 then
  240. curSlot = v["slot"]
  241. break
  242. end
  243. end
  244.  
  245. if eviction then
  246. chest.pushItemIntoSlot(directionToEject,curSlot,1,1)
  247. os.sleep(0.125)
  248. refresh()
  249. eviction = false
  250. else
  251. chest.pushItemIntoSlot(directionToPortal,curSlot,1,1)
  252. os.sleep(portalDuration)
  253. chest.pullItemIntoSlot(directionToPortal,1,1,curSlot)
  254. pages[curPage]:toggleButton(p1)
  255. drawLabels()
  256. end
  257. end
  258.  
  259.  
  260. function refresh()
  261. print("Collecting Names.")
  262. populateNames()
  263.  
  264. pages = {}
  265.  
  266. local myPage = 1
  267.  
  268. if #names > 0 then
  269. while (myPage - 1) * namesPerPage < #names do
  270. print("Populating page "..myPage..".")
  271. buildPage(myPage)
  272. myPage = myPage + 1
  273. end
  274. else
  275. buildPage(1)
  276. end
  277.  
  278. if curPage > #pages then
  279. curPage = #pages
  280. end
  281.  
  282. pages[curPage]:draw()
  283. drawLabels()
  284. end
  285.  
  286. function rebuildPages()
  287. pages[curPage]:toggleButton(resetButton)
  288. drawLabels()
  289. refresh()
  290. end
  291.  
  292. function evictBook()
  293. pages[curPage]:toggleButton(ejectButton)
  294. if eviction then
  295. eviction = false
  296. else
  297. eviction = true
  298. end
  299. drawLabels()
  300. end
  301.  
  302. function buildPage(pN)
  303. pages[pN] = touchpoint.new(monitorLocation)
  304. pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, iBC, aBC)
  305. pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, iBC, aBC)
  306.  
  307. if #names > 0 then
  308. local curIndex = (pN - 1) * namesPerPage + 1
  309. for y = 3, monY - 2, 2 do
  310. if curIndex < #names + 1 then
  311. pages[pN]:add(names[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, iBC, aBC)
  312. else
  313. break
  314. end
  315. curIndex = curIndex + 1
  316. if curIndex < #names + 1 then
  317. pages[pN]:add(names[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, iBC, aBC)
  318. else
  319. break
  320. end
  321. curIndex = curIndex + 1
  322. end
  323. end
  324.  
  325. pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, iBC, aBC)
  326. if showEject then
  327. pages[pN]:add(ejectButton, evictBook, monX - determineLength(ejectButton) + 1, monY, monX, monY, iBC, aBC)
  328. end
  329. end
  330.  
  331. prepareScreen()
  332. checkDirections()
  333.  
  334. if not problem then
  335. initialPull()
  336. refresh()
  337. print("Starting main control loop.")
  338. while true do
  339. pages[curPage]:draw()
  340. drawLabels()
  341. event, p1 = pages[curPage]:handleEvents(os.pullEvent())
  342. if event == "button_click" then
  343. pages[curPage].buttonList[p1].func()
  344. end
  345. end
  346. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement