Advertisement
DustinRosebery

RCmaster_1.1.7

Oct 16th, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. ------------------------------------------------------------
  2. -- Name: RCMaster
  3. -- version: 1.1.7
  4. -- Author: Dustin Rosebery
  5. -- Description: Program that provides touchscreen turtle
  6. -- operation including movement, inventory,
  7. -- placing, and mining.
  8. -- setup: 4x4 advanced monitors with an advanced computer
  9. -- on the bottom of the monitors with a wireless
  10. -- modem on the right side. Also requires an
  11. -- advanced turtle with RCreceive installed
  12. -- and a wireless modem on the left side.
  13. ------------------------------------------------------------
  14.  
  15. m = peripheral.wrap("top")
  16. modem = peripheral.wrap("right")
  17. m.clear()
  18. buttons = {}
  19. currentSlot = 1
  20. currentOutput = "temp"
  21.  
  22. --------------------------------------------------
  23. -- button creation
  24. --------------------------------------------------
  25.  
  26. -- Directional button background color
  27. dirColor = colors.red
  28. -- Inventory button background color
  29. invBackgroundColor = colors.white
  30. -- Inventory button text color
  31. invTextColor = colors.black
  32. ---------------------------------
  33. --Directional buttons
  34. ---------------------------------
  35.  
  36. -- fbut (forward)
  37. fxMin = 23
  38. fxSize = 7
  39. fxMax = fxMin + fxSize - 1
  40.  
  41. fyMin = 6
  42. fySize = 4
  43. fyMax = fyMin + fySize - 1
  44.  
  45. fbut = window.create(m,fxMin,fyMin,fxSize,fySize)
  46. fbut.setBackgroundColor(dirColor)
  47. fbut.restoreCursor()
  48. fbut.redraw()
  49. fbut.setCursorPos(1,1)
  50. fbut.write(" ")
  51. fbut.setCursorPos(1,2)
  52. fbut.write("forward")
  53. fbut.setCursorPos(1,3)
  54. fbut.write(" ")
  55.  
  56. -- bbut (backwards)
  57. bxMin = 23
  58. bxSize = 7
  59. bxMax = bxMin + bxSize - 1
  60.  
  61. byMin = 12
  62. bySize = 3
  63. byMax = byMin + bySize - 1
  64.  
  65. bbut = window.create(m,bxMin,byMin,bxSize,bySize)
  66. bbut.setBackgroundColor(dirColor)
  67. bbut.redraw()
  68. bbut.restoreCursor()
  69. bbut.setCursorPos(1,1)
  70. bbut.write(" ")
  71. bbut.setCursorPos(1,2)
  72. bbut.write(" back ")
  73. bbut.setCursorPos(1,3)
  74. bbut.write(" ")
  75. bbut.redraw()
  76.  
  77. -- lbut (left)
  78. lxMin = 19
  79. lxSize = 7
  80. lxMax = lxMin + lxSize - 1
  81.  
  82. lyMin = 9
  83. lySize = 3
  84. lyMax = lyMin + lySize - 1
  85.  
  86. lbut = window.create(m,lxMin,lyMin,lxSize,lySize)
  87. lbut.setBackgroundColor(dirColor)
  88. lbut.redraw()
  89. lbut.setCursorPos(1,1)
  90. lbut.write(" ")
  91. lbut.setCursorPos(1,2)
  92. lbut.write(" left ")
  93. lbut.setCursorPos(1, 3)
  94. lbut.write(" ")
  95.  
  96. -- rbut (right)
  97. rxMin = 27
  98. rxSize = 7
  99. rxMax = rxMin + rxSize - 1
  100.  
  101. ryMin = 9
  102. rySize = 3
  103. ryMax = ryMin + rySize - 1
  104.  
  105. rbut = window.create(m,rxMin,ryMin,rxSize,rySize)
  106. rbut.setBackgroundColor(dirColor)
  107. rbut.redraw()
  108. rbut.setCursorPos(1,1)
  109. rbut.write(" ")
  110. rbut.setCursorPos(1,2)
  111. rbut.write(" right ")
  112. rbut.setCursorPos(1,3)
  113. rbut.write(" ")
  114.  
  115. -- ubut (up)
  116. uxMin = 36
  117. uxSize = 2
  118. uxMax = uxMin + uxSize - 1
  119.  
  120. uyMin = 7
  121. uySize = 3
  122. uyMax = uyMin + uySize - 1
  123.  
  124. ubut = window.create(m,uxMin,uyMin,uxSize,uySize)
  125. ubut.setBackgroundColor(dirColor)
  126. ubut.restoreCursor()
  127. for i = 1, uySize do
  128. ubut.setCursorPos(1,i)
  129. ubut.write(" ")
  130. end
  131. m.setCursorPos(36,6)
  132. m.write("up")
  133.  
  134. -- dbut (down)
  135. dxMin = 36
  136. dxSize = 2
  137. dxMax = dxMin + dxSize - 1
  138.  
  139. dyMin = 11
  140. dySize = 3
  141. dyMax = dyMin + dySize - 1
  142.  
  143. dbut = window.create(m,dxMin,dyMin,dxSize,dySize)
  144. dbut.setBackgroundColor(dirColor)
  145. dbut.restoreCursor()
  146. for i = 1, dySize do
  147. dbut.setCursorPos(1,i)
  148. dbut.write(" ")
  149. end
  150. m.setCursorPos(35,14)
  151. m.write("down")
  152.  
  153. ----------------------------
  154. --Action Buttons
  155. ----------------------------
  156.  
  157. -- place
  158. pxMin = 21
  159. pxSize = 5
  160. pxMax = pxMin + pxSize - 1
  161.  
  162. pyMin = 2
  163. pySize = 1
  164. pyMax = pyMin + pySize - 1
  165.  
  166. pbut = window.create(m,pxMin,pyMin,pxSize,pySize)
  167. pbut.setBackgroundColor(dirColor)
  168. pbut.write("place")
  169.  
  170. -- dig
  171. digxMin = pxMax + 2
  172. digxSize = 5
  173. digxMax = digxMin + digxSize - 1
  174.  
  175. digyMin = 2
  176. digySize = 1
  177. digyMax = digyMin + digySize - 1
  178.  
  179. digbut = window.create(m,digxMin,digyMin,digxSize,digySize)
  180. digbut.setBackgroundColor(dirColor)
  181. digbut.write(" dig ")
  182.  
  183.  
  184. ----------------------------
  185. --Inventory Buttons
  186. ----------------------------
  187.  
  188. invButtons = {}
  189.  
  190. ixMin = 1 -- x_min
  191. ixSize = 17 -- x_width
  192. ixMax = ixMin + ixSize - 1 -- x_max
  193. iySize = 1 -- y_height
  194.  
  195. -- sets initial inventory button parameters and inserts into global list
  196. function initInventoryButton(button)
  197. button.setBackgroundColor(invBackgroundColor)
  198. button.restoreCursor()
  199. button.setTextColor(invTextColor)
  200. button.setCursorPos(1,1)
  201. for i = 1, ixSize do
  202. button.write(" ")
  203. end
  204. table.insert(invButtons, button)
  205. end
  206.  
  207. -- invLabel
  208. invLabelyMin = 1
  209. invLabel = window.create(m,ixMin,invLabelyMin,ixSize,iySize)
  210. invLabel.setTextColor(colors.white)
  211. invLabel.restoreCursor()
  212. invLabel.write("Inventory:")
  213.  
  214. -- i1
  215. i1yMin = 2
  216. i1 = window.create(m,ixMin,i1yMin,ixSize,iySize)
  217. initInventoryButton(i1)
  218. -- i2
  219. i2yMin = 3
  220. i2 = window.create(m,ixMin,i2yMin,ixSize,iySize)
  221. initInventoryButton(i2)
  222. -- i3
  223. i3yMin = 4
  224. i3 = window.create(m,ixMin,i3yMin,ixSize,iySize)
  225. initInventoryButton(i3)
  226. -- i4
  227. i4yMin = 5
  228. i4 = window.create(m,ixMin,i4yMin,ixSize,iySize)
  229. initInventoryButton(i4)
  230. -- i5
  231. i5yMin = 6
  232. i5 = window.create(m,ixMin,i5yMin,ixSize,iySize)
  233. initInventoryButton(i5)
  234. -- i6
  235. i6yMin = 7
  236. i6 = window.create(m,ixMin,i6yMin,ixSize,iySize)
  237. initInventoryButton(i6)
  238. -- i7
  239. i7yMin = 8
  240. i7 = window.create(m,ixMin,i7yMin,ixSize,iySize)
  241. initInventoryButton(i7)
  242. -- i8
  243. i8yMin = 9
  244. i8 = window.create(m,ixMin,i8yMin,ixSize,iySize)
  245. initInventoryButton(i8)
  246. -- i9
  247. i9yMin = 10
  248. i9 = window.create(m,ixMin,i9yMin,ixSize,iySize)
  249. initInventoryButton(i9)
  250. -- i10
  251. i10yMin = 11
  252. i10 = window.create(m,ixMin,i10yMin,ixSize,iySize)
  253. initInventoryButton(i10)
  254. -- i11
  255. i11yMin = 12
  256. i11 = window.create(m,ixMin,i11yMin,ixSize,iySize)
  257. initInventoryButton(i11)
  258. -- i12
  259. i12yMin = 13
  260. i12 = window.create(m,ixMin,i12yMin,ixSize,iySize)
  261. initInventoryButton(i12)
  262. -- i13
  263. i13yMin = 14
  264. i13 = window.create(m,ixMin,i13yMin,ixSize,iySize)
  265. initInventoryButton(i13)
  266. -- i14
  267. i14yMin = 15
  268. i14 = window.create(m,ixMin,i14yMin,ixSize,iySize)
  269. initInventoryButton(i14)
  270. -- i15
  271. i15yMin = 16
  272. i15 = window.create(m,ixMin,i15yMin,ixSize,iySize)
  273. initInventoryButton(i15)
  274. -- i16
  275. i16yMin = 17
  276. i16 = window.create(m,ixMin,i16yMin,ixSize,iySize)
  277. initInventoryButton(i16)
  278. -- reset
  279. iryMin = 19
  280. iReset = window.create(m,ixMin,iryMin,ixSize,iySize)
  281. iReset.setBackgroundColor(colors.red)
  282. iReset.setTextColor(colors.white)
  283. iReset.restoreCursor()
  284. iReset.write("update")
  285.  
  286.  
  287. -- cleans item names by removing modID:
  288. function split(name)
  289. start, stop = string.find( name, ":" )
  290. result = string.sub( name, start+1, -1 )
  291. return result
  292. end
  293.  
  294.  
  295. -- initial inventory button display
  296. function initInventory()
  297. for i = 1, 16 do
  298. rednet.broadcast(i)
  299. senderID, message, protocol = rednet.receive()
  300. if message == "empty" then
  301. invButtons[i].restoreCursor()
  302. invButtons[i].setCursorPos(1,1)
  303. invButtons[i].clear()
  304. invButtons[i].write("--")
  305. else
  306. name = split(message)
  307. sID, amount, proto = rednet.receive()
  308. output = amount .. " " .. name
  309. invButtons[i].restoreCursor()
  310. invButtons[i].setCursorPos(1,1)
  311. invButtons[i].clear()
  312. invButtons[i].write(output)
  313. end
  314. end
  315. rednet.broadcast(currentSlot)
  316. updateInventory(currentSlot)
  317. touchEvent()
  318. end
  319.  
  320.  
  321. -- updates inventory on inventory button click
  322. function updateInventory(index)
  323. senderID, message, protocol = rednet.receive()
  324. invButtons[index].clear()
  325. invButtons[index].redraw()
  326. invButtons[index].restoreCursor()
  327. invButtons[index].setCursorPos(1,1)
  328. if( index == currentSlot ) then
  329. write("same slot")
  330. else
  331. invButtons[currentSlot].setBackgroundColor(invBackgroundColor)
  332. invButtons[currentSlot].restoreCursor()
  333. invButtons[currentSlot].clear()
  334. invButtons[currentSlot].setCursorPos(1,1)
  335. invButtons[currentSlot].write(currentOutput)
  336. invButtons[currentSlot].redraw()
  337. currentSlot = index
  338. end
  339. invButtons[index].setBackgroundColor(colors.lightGray)
  340. if message == "empty" then
  341. output = ("--")
  342. else
  343. name = split(message)
  344. sID, amount, proto = rednet.receive()
  345. output = amount .. " " .. name
  346. end
  347. invButtons[index].write(output)
  348. currentOutput = output
  349. touchEvent()
  350. end
  351.  
  352.  
  353. ----------------------------------------------------------------------
  354. -- Input listener
  355. ----------------------------------------------------------------------
  356.  
  357. function touchEvent()
  358.  
  359. event, side, xpos, ypos = os.pullEvent("monitor_touch")
  360. write(xpos .. ", " .. ypos.."\n") -- show touch location
  361.  
  362. -- directional control
  363. if ( xpos >= fxMin and xpos <= fxMax and
  364. ypos >= fyMin and ypos <= fyMax )then
  365. rednet.broadcast("forward")
  366.  
  367. elseif ( xpos >= bxMin and xpos <= bxMax and
  368. ypos >= byMin and ypos <= byMax )then
  369. rednet.broadcast("back")
  370.  
  371. elseif ( xpos >= lxMin and xpos <= lxMax and
  372. ypos >= lyMin and ypos <= lyMax )then
  373. rednet.broadcast("left")
  374.  
  375. elseif ( xpos >= rxMin and xpos <= rxMax and
  376. ypos >= ryMin and ypos <= ryMax )then
  377. rednet.broadcast("right")
  378.  
  379. elseif ( xpos >= uxMin and xpos <= uxMax and
  380. ypos >= uyMin and ypos <= uyMax )then
  381. rednet.broadcast("up")
  382.  
  383. elseif ( xpos >= dxMin and xpos <= dxMax and
  384. ypos >= dyMin and ypos <= dyMax )then
  385. rednet.broadcast("down")
  386. -- action control
  387. elseif( xpos >= pxMin and xpos <= pxMax and
  388. ypos >= pyMin and ypos <= pyMax )then
  389. rednet.broadcast("place")
  390. os.sleep(1)
  391. rednet.broadcast(currentSlot)
  392. updateInventory(currentSlot)
  393.  
  394. elseif( xpos >= digxMin and xpos <= digxMax and
  395. ypos >= digyMin and ypos <= digyMax )then
  396. rednet.broadcast("dig")
  397.  
  398.  
  399. -- inventory control
  400. elseif ( xpos >= ixMin and xpos <= ixMax )then
  401.  
  402. if( ypos >= i1yMin and ypos <= i16yMin + iySize )then
  403.  
  404. ypos = ypos - 1 -- accounts for offset
  405. rednet.broadcast(ypos)
  406. updateInventory(ypos)
  407.  
  408. elseif( ypos == iryMin ) then
  409.  
  410. initInventory()
  411. end
  412. end
  413. end
  414.  
  415. rednet.open("right")
  416. initInventory()
  417. while true do
  418. touchEvent()
  419. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement