Advertisement
cyber_Ahn

Train Station Control

Dec 30th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 KB | None | 0 0
  1. --- next step colors the tracks
  2.  
  3. --Train Control--
  4. --works with cable server--
  5. -- MONITOR was CHANGED in monitor.cfg--
  6. --optimal monitor size 5*4--
  7.  
  8. --config--
  9. local channel = 31
  10. local backchan = 32
  11. local col = colors.white
  12. modem = peripheral.wrap("back")
  13.  
  14. --var----
  15. local mon = ""
  16. local power = true
  17.  
  18. --set monitor
  19. function set_monitor()
  20. local monitor_number = "monitor_1"
  21. local found = fs.exists("config/monitor.cfg")
  22. if found == true then
  23. file = fs.open("config/monitor.cfg","r")
  24. local fileData = {}
  25. local line = file.readLine()
  26. repeat
  27. table.insert(fileData,line)
  28. line = file.readLine()
  29. until line == nil
  30. file.close()
  31. monitor_number = fileData[1]
  32. end
  33. mon = peripheral.wrap(monitor_number)
  34. end
  35.  
  36. -- draw screen --
  37. function draw_screen()
  38. mon.setBackgroundColor(colors.black)
  39. mon.setTextColor(colors.white)
  40. mon.clear()
  41. mon.setBackgroundColor(colors.black)
  42. mon.setCursorPos(1,1)
  43. mon.setBackgroundColor(colors.blue)
  44. mon.write("Train Control     Channel:"..channel.."                     ")
  45. mon.setCursorPos(50,1)
  46. mon.setBackgroundColor(colors.red)
  47. mon.setTextColor(colors.black)
  48. mon.write("X")
  49. mon.setBackgroundColor(colors.black)
  50. mon.setTextColor(colors.white)
  51. mon.setCursorPos(25,2)
  52. mon.write("Track 11")
  53. mon.setCursorPos(25,4)
  54. mon.write("Track 10")
  55. mon.setCursorPos(25,6)
  56. mon.write("Track 9")
  57. mon.setCursorPos(25,8)
  58. mon.write("Track 8")
  59. mon.setCursorPos(25,10)
  60. mon.write("Track 7")
  61. mon.setCursorPos(25,12)
  62. mon.write("Track 6")
  63. mon.setCursorPos(25,15)
  64. mon.write("Track 5")
  65. mon.setCursorPos(25,17)
  66. mon.write("Track 4")
  67. mon.setCursorPos(25,19)
  68. mon.write("Track 3")
  69. mon.setCursorPos(25,21)
  70. mon.write("Track 2")
  71. mon.setCursorPos(25,23)
  72. mon.write("Track 1")
  73. mon.setCursorPos(1,21)
  74. mon.write("Track 13")
  75. mon.setCursorPos(1,23)
  76. mon.write("Track 14")
  77. --draw button
  78. draw_button(20,4,3)
  79. draw_button(20,6,3)
  80. draw_button(20,8,3)
  81. draw_button(20,10,3)
  82. draw_button(20,12,3)
  83. draw_button(20,19,3)
  84. draw_button(20,21,3)
  85. draw_button(20,23,3)--1
  86. draw_button(10,21,3)--13
  87. draw_button(10,23,3)--14
  88. draw_button(3,15,3) --left ran.
  89. draw_button(44,15,3) --right ran.
  90. draw_button(1,17,3) --left 4to5
  91. draw_button(8,17,3) --left 5to4
  92. draw_button(42,17,3) --right 4to5
  93. draw_button(49,17,3) --right 5to4
  94. --draw track
  95. if rs.testBundledInput("left", colors.red) then
  96. col = colors.green
  97. else
  98. col = colors.white
  99. end
  100. draw_line(8,3,34,col)
  101. if rs.testBundledInput("left", colors.green) then
  102. col = colors.green
  103. else
  104. col = colors.white
  105. end
  106. draw_line(9,5,32,col)
  107. draw_switch(41,6,"|",col)
  108. draw_switch(8,6,"/",col)
  109. if rs.testBundledInput("left", colors.brown) then
  110. col = colors.green
  111. else
  112. col = colors.white
  113. end
  114. draw_line(9,7,32,col)
  115. draw_switch(41,8,"|",col)
  116. draw_switch(8,8,"/",col)
  117. if rs.testBundledInput("left", colors.blue) then
  118. col = colors.green
  119. else
  120. col = colors.white
  121. end
  122. draw_line(9,9,32,col)
  123. draw_switch(41,10,"|",col)
  124. draw_switch(8,10,"/",col)
  125. if rs.testBundledInput("left", colors.purple) then
  126. col = colors.green
  127. else
  128. col = colors.white
  129. end
  130. draw_line(9,11,32,col)
  131. draw_switch(41,12,"|",col)
  132. draw_switch(8,12,"/",col)
  133. if rs.testBundledInput("left", colors.cyan) then
  134. col = colors.green
  135. else
  136. col = colors.white
  137. end
  138. draw_line(9,13,32,col)
  139. draw_switch(41,14,"|",col)
  140. draw_switch(8,14,"/",col)
  141. col = colors.green
  142. draw_line(1,16,50,col)
  143. draw_line(1,18,50,col)
  144. if rs.testBundledInput("left", colors.lightGray) then
  145. col = colors.green
  146. else
  147. col = colors.white
  148. end
  149. draw_line(19,20,28,col)
  150. draw_switch(47,19,"/",col)
  151. draw_switch(18,20,"|",col)
  152. if rs.testBundledInput("left", colors.yellow) then
  153. col = colors.green
  154. else
  155. col = colors.white
  156. end
  157. draw_line(16,22,24,col)
  158. draw_column(16,18,5,col)
  159. draw_column(39,18,5,col)
  160. draw_switch(15,17,"|",col)
  161. draw_switch(40,17,"/",col)
  162. if rs.testBundledInput("left", colors.lime) then
  163. col = colors.green
  164. else
  165. col = colors.white
  166. end
  167. draw_line(14,24,28,col)
  168. draw_switch(13,19,"|",col)
  169. draw_switch(43,19,"/",col)
  170. draw_column(14,20,5,col)
  171. draw_column(42,20,5,col)
  172. if rs.testBundledInput("left", colors.lightBlue) then
  173. col = colors.green
  174. else
  175. col = colors.white
  176. end
  177. draw_line(2,22,7,col)
  178. draw_switch(1,22,"|",col)
  179. if rs.testBundledInput("left", colors.magenta) then
  180. col = colors.green
  181. else
  182. col = colors.white
  183. end
  184. draw_line(2,24,8,col)
  185. draw_switch(1,24,"|",col)
  186. draw_switch(10,19,"/",col)
  187. draw_column(9,20,5,col)
  188. if rs.testBundledInput("left", colors.black) then
  189. col = colors.green
  190. else
  191. col = colors.white
  192. end
  193. draw_column(7,3,12,col)
  194. draw_column(42,3,12,col)
  195. draw_switch(6,15,"/",col)
  196. draw_switch(43,15,"|",col)
  197. col = colors.white
  198. --
  199. draw_switch(4,17,"/",col)
  200. draw_switch(11,17,"|",col)
  201. draw_switch(45,17,"|",col)
  202. draw_switch(48,17,"/",col)
  203. mon.setBackgroundColor(colors.black)
  204. mon.setTextColor(colors.white)
  205. touch()
  206. end
  207.  
  208. -- draw-switch--
  209. function draw_switch(x,y,data,colX)
  210. mon.setTextColor(colX)
  211. mon.setCursorPos(x,y)
  212. mon.write(data)
  213. mon.setTextColor(colors.white)
  214. end
  215.  
  216. --draw-column-
  217. function draw_column(x,y,len,colX)
  218. mon.setBackgroundColor(colors.black)
  219. mon.setTextColor(colX)
  220. len = len + (y - 1)
  221. for yi = y,len do
  222. mon.setCursorPos(x,yi)
  223. mon.write("|")
  224. yi = yi + 1
  225. end
  226. mon.setTextColor(colors.white)
  227. end
  228.  
  229. --draw-line--
  230. function draw_line(x,y,len,colX)
  231. mon.setBackgroundColor(colors.black)
  232. mon.setTextColor(colX)
  233. mon.setCursorPos(x,y)
  234. for xi=1,len do
  235. mon.write("-")
  236. xi = xi + 1
  237. end
  238. mon.setTextColor(colors.white)
  239. end
  240.  
  241. --draw-button--
  242. function draw_button(x,y,len)
  243. mon.setBackgroundColor(colors.gray)
  244. mon.setTextColor(colors.white)
  245. mon.setCursorPos(x,y)
  246. for xi=1,len do
  247. mon.write(" ")
  248. xi = xi + 1
  249. end
  250. mon.setBackgroundColor(colors.black)
  251. end
  252.  
  253. --touch event--
  254. function touch()
  255. event,side,x,y = os.pullEvent()
  256. if event == "monitor_touch" then
  257. if x == 50 and y == 1 then
  258. power = false
  259. end
  260. if x >= 1 and x <= 4 and y == 17 then
  261. mon.setCursorPos(1,26)
  262. mon.write("left 4to5")
  263. modem.transmit(channel,backchan,"white")
  264. sleep(5)
  265. modem.transmit(channel,backchan,"orange")
  266. sleep(2)
  267. end
  268. if x >= 7 and x <= 12 and y == 17 then
  269. mon.setCursorPos(1,26)
  270. mon.write("left 5to4")
  271. modem.transmit(channel,backchan,"gray")
  272. sleep(5)
  273. modem.transmit(channel,backchan,"yellow")
  274. sleep(2)
  275. end
  276. if x >= 41 and x <= 46 and y == 17 then
  277. mon.setCursorPos(1,26)
  278. mon.write("right 4to5")
  279. modem.transmit(channel,backchan,"green-1")
  280. sleep(5)
  281. modem.transmit(channel,backchan,"brown")
  282. sleep(2)
  283. end
  284. if x >= 48 and x <= 50 and y == 17 then
  285. mon.setCursorPos(1,26)
  286. mon.write("right 5to4")
  287. modem.transmit(channel,backchan,"blue")
  288. sleep(5)
  289. modem.transmit(channel,backchan,"cyan")
  290. sleep(2)
  291. end
  292. if x >= 19 and x <= 24 and y == 4 then
  293. mon.setCursorPos(1,26)
  294. mon.write("track 10")
  295. modem.transmit(channel,backchan,"yellow-1")
  296. sleep(5)
  297. modem.transmit(channel,backchan,"purple-1")
  298. sleep(2)
  299. end
  300. if x >= 19 and x <= 24 and y == 6 then
  301. mon.setCursorPos(1,26)
  302. mon.write("track 9")
  303. modem.transmit(channel,backchan,"lightblue-1")
  304. sleep(5)
  305. modem.transmit(channel,backchan,"cyan-1")
  306. sleep(2)
  307. end
  308. if x >= 19 and x <= 24 and y == 8 then
  309. mon.setCursorPos(1,26)
  310. mon.write("track 8")
  311. modem.transmit(channel,backchan,"magenta-1")
  312. sleep(5)
  313. modem.transmit(channel,backchan,"lightgray-1")
  314. sleep(2)
  315. end
  316. if x >= 19 and x <= 24 and y == 10 then
  317. mon.setCursorPos(1,26)
  318. mon.write("track 7")
  319. modem.transmit(channel,backchan,"orange-1")
  320. sleep(5)
  321. modem.transmit(channel,backchan,"gray-1")
  322. sleep(2)
  323. end
  324. if x >= 19 and x <= 24 and y == 12 then
  325. mon.setCursorPos(1,26)
  326. mon.write("track 6")
  327. modem.transmit(channel,backchan,"white-1")
  328. sleep(5)
  329. modem.transmit(channel,backchan,"pink-1")
  330. sleep(2)
  331. end
  332. if x >= 19 and x <= 24 and y == 19 then
  333. mon.setCursorPos(1,26)
  334. mon.write("track 3")
  335. modem.transmit(channel,backchan,"purple")
  336. sleep(2)
  337. end
  338. if x >= 19 and x <= 24 and y == 21 then
  339. mon.setCursorPos(1,26)
  340. mon.write("track 2")
  341. modem.transmit(channel,backchan,"lightgray")
  342. sleep(5)
  343. modem.transmit(channel,backchan,"brown-1")
  344. sleep(2)
  345. end
  346. if x >= 19 and x <= 24 and y == 23 then
  347. mon.setCursorPos(1,26)
  348. mon.write("track 1")
  349. modem.transmit(channel,backchan,"lime")
  350. sleep(5)
  351. modem.transmit(channel,backchan,"green")
  352. sleep(2)
  353. end
  354. if x >= 9 and x <= 14 and y == 21 then
  355. mon.setCursorPos(1,26)
  356. mon.write("track 13")
  357. modem.transmit(channel,backchan,"lightblue")
  358. sleep(2)
  359. end
  360. if x >= 9 and x <= 14 and y == 23 then
  361. mon.setCursorPos(1,26)
  362. mon.write("track 14")
  363. modem.transmit(channel,backchan,"pink")
  364. sleep(2)
  365. end
  366. if x >= 2 and x <= 7 and y == 15 then
  367. mon.setCursorPos(1,26)
  368. mon.write("left")
  369. modem.transmit(channel,backchan,"magenta")
  370. sleep(2)
  371. end
  372. if x >= 43 and x <= 47 and y == 15 then
  373. mon.setCursorPos(1,26)
  374. mon.write("right")
  375. modem.transmit(channel,backchan,"red")
  376. sleep(2)
  377. end
  378. end
  379. if power == true then
  380. draw_screen()
  381. else
  382. modem.transmit(channel,backchan,"shutdown")
  383. shell.run("clear")
  384. end
  385. end
  386.  
  387. --programm--
  388. set_monitor()
  389. draw_screen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement