DrFair

Nuclear Control

Apr 18th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. reader = peripheral.wrap("left")
  2. m = peripheral.wrap("bottom")
  3. w,h = m.getSize()
  4. w = w + 1
  5. h = h + 1
  6. col = { ["white"]=1, ["orange"]=2, ["magenta"]=4, ["lightblue"]=8, ["yellow"]=16, ["lime"]=32, ["pink"]=64, ["gray"]=128, ["lightgray"]=256, ["cyan"]=512, ["purple"]=1024, ["blue"]=2048, ["brown"]=4096, ["green"]=8192, ["red"]=16384, ["black"]=32768 }
  7. id,state,title,data = reader.get(1)
  8. reactorOn = false
  9. shutdown = false
  10.  
  11. function swrite(str,x,y,color)
  12. m.setCursorPos(x,y)
  13. m.setTextColor(col[color])
  14. m.write(str)
  15. end
  16.  
  17. function swritecenter(str,y,color)
  18. m.setCursorPos((w/2-#str/2)+1,y)
  19. m.setTextColor(col[color])
  20. m.write(str)
  21. end
  22.  
  23. function drawbox(str,x1,x2,y1,y2,strcol,color)
  24. m.setCursorPos(x1,y1)
  25. m.setBackgroundColor(col[color])
  26. m.setTextColor(col[strcol])
  27. local bw = x2-x1
  28. local bh = y2-y1
  29. local bstr = " "
  30. while #bstr < bw do
  31. bstr = bstr.." "
  32. end
  33. for i=1,bh do
  34. m.setCursorPos(x1,y1+i-1)
  35. m.write(bstr)
  36. end
  37. m.setCursorPos(x1+bw/2-#str/2,y1+bh/2)
  38. m.write(str)
  39. m.setBackgroundColor(col["black"])
  40. return { [1]=x1, [2]=x2, [3]=y1, [4]=y2 }
  41. end
  42.  
  43. function drawtext(str,x,y,color)
  44. m.setCursorPos(x,y)
  45. m.setTextColor(col[color])
  46. m.write(str)
  47. return { [1]=x, [2]=x+1, [3]=y, [4]=y+#str }
  48. end
  49.  
  50. function press(but)
  51. if event == "monitor_touch" then
  52. if pr2 >= but[1] and pr2 < but[2] and pr3 >= but[3] and pr3 < but[4] then
  53. return true
  54. else
  55. return false
  56. end
  57. end
  58. end
  59.  
  60. function drawMenu()
  61. m.clear()
  62. drawbox("",1,w,1,h,"white","white")
  63. drawbox("",2,w-1,2,h-1,"white","black")
  64. swritecenter("Fair's Reactor Control System",3,"lime")
  65. swritecenter("Uranium left:",5,"white")
  66. swrite("Status:",4,11,"white")
  67. swrite("Heat:",4,13,"white")
  68. swrite("EU Output:",22,11,"white")
  69. swrite("Max heat:",22,13,"white")
  70. swrite("1000",33,13,"red")
  71. start = drawbox("Activate Reactor",4,w-4,16,19,"white","lime")
  72. os.startTimer(0.5)
  73. end
  74.  
  75. function drawTimeLeft()
  76. drawbox("",3,w-3,7,10,"white","white")
  77. drawbox("",4,w-4,8,9,"white","red")
  78. local timeLeft = data["timeLeft"]
  79. if timeLeft > 0 then
  80. drawbox("",4,timeLeft/(10000/36),8,9,"white","lime")
  81. else
  82. drawbox("Out of Uranium",4,w-4,8,9,"black","red")
  83. end
  84. end
  85.  
  86. function drawStatus()
  87. local Output = data["output"]
  88. local On = data["reactorPoweredB"]
  89. local Heat = data["heat"]
  90. if Output > 0 then
  91. swrite(tostring(Output).." ",33,11,"lime")
  92. else
  93. swrite(tostring(Output).." ",33,11,"lime")
  94. end
  95. if On then
  96. swrite("ACTIVE ",12,11,"lime")
  97. else
  98. swrite("STANDBY ",12,11,"red")
  99. end
  100. if Heat > 0 then
  101. swrite(tostring(Heat).." ",12,13,"red")
  102. else
  103. shutdown = false
  104. toggle()
  105. drawbox("",4,w-4,21,22,"red","black")
  106. swrite(tostring(Heat).." ",12,13,"lime")
  107. end
  108. if Heat > 1000 then
  109. shutdown = true
  110. toggle()
  111. drawbox("!! EMERGENCY SHUTDOWN !!",4,w-4,21,22,"red","black")
  112. end
  113. end
  114.  
  115. function toggle()
  116. if reactorOn then
  117. redstone.setOutput("right",true)
  118. start = drawbox("Activate Reactor",4,w-4,1600,1900,"white","lime")
  119. stop = drawbox("Deactivate Reactor",4,w-4,16,19,"white","red")
  120. else
  121. redstone.setOutput("right",false)
  122. start = drawbox("Activate Reactor",4,w-4,16,19,"white","lime")
  123. stop = drawbox("Deactivate Reactor",4,w-4,1600,1900,"white","red")
  124. end
  125. if shutdown then
  126. redstone.setOutput("right",false)
  127. end
  128. end
  129.  
  130. drawMenu()
  131.  
  132. while true do
  133. drawTimeLeft()
  134. drawStatus()
  135. event,pr1,pr2,pr3 = os.pullEvent()
  136. id,state,title,data = reader.get(1)
  137. if press(start) or press(stop) then
  138. if reactorOn then
  139. reactorOn = false
  140. else
  141. reactorOn = true
  142. end
  143. toggle()
  144. elseif event == "timer" then
  145. drawTimeLeft()
  146. drawStatus()
  147. os.startTimer(0.5)
  148. end
  149. end
  150.  
  151. -- data: heat,isSteam,reactorPoweredB,maxHeat,timeLeft,output
Advertisement
Add Comment
Please, Sign In to add comment