nccn12

remote-pc

Feb 5th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. -- SmartMine server software v0.43
  2. -- v0.43
  3. -- - interface now uses cList object for graphical navigation with keyboard.
  4. -- v0.4
  5. -- -
  6.  
  7.  
  8. os.loadAPI('pervar')
  9. os.loadAPI('vector')
  10. os.loadAPI('gui')
  11.  
  12.  
  13. -- Version info
  14. function version()
  15. print('Droid Control v0.43')
  16. end
  17.  
  18.  
  19. -- global vars
  20. botArray = {-1, -1, -1, -1}
  21. botStatus = {'Idle', 'Idle', 'Idle', 'Idle'}
  22. botID = -1
  23. dock = vector.vector()
  24. mode = 0
  25. -- Modes:
  26. -- 0 - dig
  27. -- 1 - place
  28.  
  29.  
  30. -- # NOTE: Fleet IDs and dock location are not permanent! You may wish to remove the exit option from the menu list.
  31. -- GUI items
  32. menulist = {
  33. 'Change droid ID',
  34. 'Change docking coordinates',
  35. 'Assume direct control',
  36. 'Callibrate droid',
  37. 'Mining operations',
  38. 'Exit'
  39. }
  40. cmenu = gui.clist.create(3,11,menulist)
  41.  
  42. -- startup screen
  43. function screen()
  44. term.clear()
  45. term.setCursorPos( 1, 1 )
  46. version()
  47. status()
  48. menu()
  49. end
  50.  
  51. -- display command options
  52. function menu()
  53. cmenu:out()
  54. end
  55.  
  56. function status()
  57. gui.box(1,10,47,8)
  58. gui.box(1,3,30,3)
  59. gui.box(1,5,30,6)
  60. gui.box(30,3,18,3)
  61. gui.box(30,5,18,6)
  62. term.setCursorPos( 3, 4 )
  63. term.write('Status:')
  64. -- Print droid status
  65. di = 1
  66. while di <= 4 do
  67. term.setCursorPos( 4, 5+di )
  68. term.write(di .. '.[')
  69. if botArray[di] == -1 then term.write('N/A]')
  70. else
  71. term.write(botArray[di] .. '] - ' .. botStatus[di])
  72. end
  73. di=di+1
  74. end
  75. -- Print dock location
  76. term.setCursorPos( 32, 4 )
  77. term.write('Dock location:')
  78. term.setCursorPos( 37, 6 )
  79. term.write('X: ')
  80. term.write(dock.x)
  81. term.setCursorPos( 37, 7 )
  82. term.write('Y: ')
  83. term.write(dock.y)
  84. term.setCursorPos( 37, 8 )
  85. term.write('Z: ')
  86. term.write(dock.z)
  87. term.setCursorPos( 32, 9 )
  88. term.write('Facing: ')
  89. term.write(dock.dir)
  90.  
  91. -- Return cursor
  92. term.setCursorPos( 1, 12 )
  93. end
  94.  
  95. -- Select bot to operate
  96. function whichBot()
  97. local botList = {}
  98. local di = 1
  99. while di <= 4 do
  100. botList[di] = di .. '.['
  101. if botArray[di] == -1 then botList[di] = di..'.[N/A]'
  102. else
  103. botList[di] = di..'.['..botArray[di]..'] - '..botStatus[di]
  104. end
  105. di=di+1
  106. end
  107. local botmenu = gui.clist.create(2,6,botList)
  108. gui.bbox(1,5,30,6)
  109. botmenu:out()
  110.  
  111. local result = nil
  112. while true do
  113. event, key = os.pullEvent()
  114. if event == 'key' then
  115. result = botmenu:input(event, key)
  116. if result ~= nil then return result end
  117. end
  118. gui.bbox(1,5,30,6)
  119. botmenu:out()
  120. end
  121. end
  122.  
  123. function targetID()
  124. local bot = whichBot()
  125. gui.bbox(1,10,47,8)
  126. gui.text(3,11,'Enter target droid ID: ')
  127. botArray[bot] = tonumber(read()) -- ask for the new ID of the turtle
  128. end
  129.  
  130. function setDock()
  131. gui.bbox(1,10,47,8)
  132. gui.text(3,11,' Dock X: ')
  133. dock.x = tonumber(read())
  134. gui.box(1,10,47,8)
  135. gui.text(3,12,' Dock Y: ')
  136. dock.y = tonumber(read())
  137. gui.box(1,10,47,8)
  138. gui.text(3,13,' Dock Z: ')
  139. dock.z = tonumber(read())
  140. gui.box(1,10,47,8)
  141. gui.text(3,14,'Dock Facing: ')
  142. dock.dir = tonumber(read())
  143. end
  144.  
  145. function callibrate()
  146. local bot = -1
  147. while true do
  148. bot = whichBot()
  149. if botArray[bot] ~= -1 then break end
  150. end
  151. gui.bbox(1,10,47,8)
  152. gui.text(3,11,'Callibrating...')
  153. rednet.send(botArray[bot],'setx ' .. dock.x)
  154. rednet.send(botArray[bot],'sety ' .. dock.y)
  155. rednet.send(botArray[bot],'setz ' .. dock.z)
  156. rednet.send(botArray[bot],'setd ' .. dock.dir)
  157. gui.text(3,13,'Press spacebar to cancel.')
  158. getReply(bot)
  159. end
  160.  
  161. -- Wait for bot confirmation (can be cancelled locally)
  162. function getReply(bot)
  163. while true do
  164. event, key, text = os.pullEvent()
  165. if event == 'rednet_message' and key == botArray[bot] then
  166. botStatus[bot] = text
  167. break
  168. elseif event == 'char' and key == ' ' then break
  169. end
  170. end
  171. end
  172.  
  173. -- Direct control via keyboard
  174. function controlLoop()
  175. local bot = -1
  176. while true do
  177. bot = whichBot()
  178. if botArray[bot] ~= -1 then break end
  179. end
  180. botID = botArray[bot]
  181. mode = 0 -- set the mode to dig
  182. gui.bbox(1,10,47,8)
  183. gui.text(3,11,'Assuming direct control.')
  184. while true do
  185. event, key = os.pullEvent()
  186. gui.bbox(1,10,47,8)
  187. gui.text(3,11,'Assuming direct control.')
  188. if key == 203 then -- left
  189. rednet.send(botID, 'left')
  190. gui.text(3,12,'Left')
  191. elseif key == 200 then -- up
  192. rednet.send(botID, 'up')
  193. gui.text(3,12,'Forward')
  194. elseif key == 205 then -- right
  195. rednet.send(botID, 'right')
  196. gui.text(3,12,'Right')
  197. elseif key == 'a' then -- if A - ascend
  198. rednet.send(botID, 'asc')
  199. gui.text(3,12,'Up.')
  200. elseif key == 'z' then -- if Z - descend
  201. rednet.send(botID, 'desc')
  202. gui.text(3,12,'Down.')
  203. elseif key == 57 then -- issued special command (spacebar)
  204. if mode == 0 then -- dig mode
  205. rednet.send(botID, 'dig')
  206. gui.text(3,12,'Dig')
  207. elseif mode == 1 then -- place mode
  208. rednet.send(botID, 'place')
  209. gui.text(3,12,'Place')
  210. end
  211. elseif key == 2 then -- set to dig mode (pressed 1)
  212. mode = 0
  213. gui.text(3,12,'Dig mode selected.') --write it
  214. elseif key == 3 then -- set to place mode (pressed 2)
  215. mode = 1
  216. gui.text(3,12,'Place mode selected.')
  217. elseif key == 157 then -- rs signal (right ctrl key)
  218. rednet.send(botID, 'rs')
  219. gui.text(3,12,'Redstone activated.')
  220. elseif key == '0' then -- exit
  221. return
  222. end
  223. gui.text(3,14,'Press 0 to return to menu')
  224. end
  225. end
  226.  
  227. -- Mining operations
  228. function mineConfig()
  229. local bot = -1
  230. while true do
  231. bot = whichBot()
  232. if botArray[bot] ~= -1 then break end
  233. end
  234.  
  235. -- Input target
  236. while true do
  237. gui.bbox(1,10,47,8)
  238.  
  239. gui.text(3,11,'Starting X: ')
  240. local minex = tonumber(read())
  241. gui.box(1,10,47,8)
  242.  
  243. gui.text(3,12,'Starting Y: ')
  244. local miney = tonumber(read())
  245. gui.box(1,10,47,8)
  246.  
  247. gui.text(3,13,'Starting Z: ')
  248. local minez = tonumber(read())
  249. gui.box(1,10,47,8)
  250.  
  251. gui.text(30,11,'Width: ')
  252. local minew = tonumber(read())
  253. gui.box(1,10,47,8)
  254.  
  255. gui.text(29,12,'Length: ')
  256. local minel = tonumber(read())
  257. gui.box(1,10,47,8)
  258.  
  259. gui.text(26,13,'Max depth: ')
  260. local mined = tonumber(read())
  261. gui.box(1,10,47,8)
  262.  
  263. -- Make sure our coordinates are entered correctly
  264. gui.text(3,15,'Confirm coordinates for Droid#'..bot..'? (y/n) ')
  265. if read() == 'y' then
  266. rednet.send(botArray[bot],'minex ' .. minex)
  267. rednet.send(botArray[bot],'miney ' .. miney)
  268. rednet.send(botArray[bot],'minez ' .. minez)
  269. rednet.send(botArray[bot],'minew ' .. minew)
  270. rednet.send(botArray[bot],'minel ' .. minel)
  271. rednet.send(botArray[bot],'mined ' .. mined)
  272.  
  273. gui.box(1,10,47,8)
  274. gui.text(3,16,'Press spacebar to cancel.')
  275. getReply(bot)
  276. break
  277. else
  278. -- Ask for new input
  279. gui.box(1,10,47,8)
  280. gui.text(3,16,'Enter new? (y/n) ')
  281. if read() == 'n' then break end
  282. end
  283. end
  284. end
  285.  
  286. -- main loop
  287. function main()
  288. rednet.open('right')
  289. screen()
  290. while true do
  291. event, key, text = os.pullEvent()
  292. if event == 'key' then
  293. local result = cmenu:input(event, key)
  294. if result == 1 then
  295. -- change target droid ID
  296. targetID()
  297. elseif result == 2 then
  298. -- change docking location
  299. setDock()
  300. elseif result == 3 then
  301. -- assume direct control
  302. controlLoop()
  303. elseif result == 4 then
  304. -- send callibration data
  305. callibrate()
  306. elseif result == 5 then
  307. -- configure mining ops
  308. mineConfig()
  309. elseif result == 6 then break -- exit
  310. end
  311. -- update the status screen
  312. screen()
  313. elseif event == 'rednet_message' then
  314. -- Handle status reports
  315. local di = 1
  316. while di <= 4 do
  317. if key == botArray[di] then botStatus[di] = text end
  318. di=di+1
  319. end
  320. screen()
  321. end
  322. end
  323. shell.run('clear')
  324. rednet.close('right')
  325. end
  326.  
  327. main()
Add Comment
Please, Sign In to add comment