Advertisement
R167

ComputerCraft: Better Spawner Controller

Mar 11th, 2013
3,525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.70 KB | None | 0 0
  1. --Requires advanced monitors and computers!!!
  2.  
  3. local rsSide = "back"
  4. local monitorSide = "right"
  5.  
  6. function testForMonitor(_side)
  7.   if peripheral.getType(_side) then
  8.     if peripheral.getType(_side) == "monitor" then
  9.       return true
  10.     else return false end
  11.   else return false end
  12. end
  13.  
  14. Args = { ... }
  15. if #Args < 1 then
  16.   print("Usage: "..fs.getName(shell.getRunningProgram()).." <monitor side> [cable side]")
  17.   return
  18. elseif #Args > 1 then
  19.   monitorSide = Args[1]
  20.   rsSide = Args[2]
  21.   if not testForMonitor(monitorSide) then
  22.     print("There is no valid monitor on that side.")
  23.     return
  24.   end
  25. elseif #Args == 1 then
  26.   monitorSide = Args[1]
  27.   if not testForMonitor(monitorSide) then
  28.     print("There is no valid monitor on that side.")
  29.     return
  30.   end
  31. else
  32.   print("Error!!!")
  33.   return
  34. end
  35.  
  36. function toggle(n)
  37.   if not n then
  38.     return
  39.   end
  40.   local state = button[n].state
  41.   if state then
  42.     state = false
  43.   else
  44.     state = true
  45.   end
  46.   button[n].state = state
  47.   return state
  48. end
  49.  
  50. function sayState(n)
  51.   if not n then
  52.     return
  53.   end
  54.   x = button[n].x - 1
  55.   y = button[n].y
  56.   term.setCursorPos(x,y)
  57.   if button[n].state then
  58.     term.setTextColor(colors.lime)
  59.     write("on ")
  60.     term.setTextColor(colors.white)
  61.   else
  62.     term.setTextColor(colors.red)
  63.     write("off")
  64.     term.setTextColor(colors.white)
  65.   end
  66. end
  67.  
  68. function getButton(xPos,yPos)
  69.   for i=1,12 do
  70.     bxPos = button[i].x
  71.     byPos = button[i].y
  72.     xMax = bxPos + 2
  73.     xMin = bxPos - 2
  74.     yMax = byPos + 1
  75.     yMin = byPos - 1
  76.     if xPos >= xMin and xPos <= xMax and yPos >= yMin and yPos <= yMax then
  77.       return i
  78.     end
  79.   end
  80. end
  81.  
  82. function mPrint(w)
  83.   write(w)
  84.   x,y=term.getCursorPos()
  85.   term.setCursorPos(1, y+1)
  86. end
  87.  
  88. function allTheSame()
  89.   local state = button[1].state
  90.   for i = 2,10 do
  91.     if state == button[i].state then
  92.     else return false
  93.     end
  94.   end
  95.   return true
  96. end
  97.  
  98. function stateWriter()
  99.   mPrint("  _____     _____     _____     _____")
  100.   write("  ")
  101.   for i = 1,4 do
  102.     write("|")
  103.     term.setTextColor(colors.red)
  104.     write("off")
  105.     term.setTextColor(colors.white)
  106.     if i<4 then
  107.       write("|     ")
  108.     else
  109.       mPrint("|")
  110.     end
  111.   end
  112.   mPrint("  ~~~~~     ~~~~~     ~~~~~     ~~~~~")
  113. end
  114.  
  115. function mobTypeWrite(_line,_time)
  116.   term.setCursorPos(1,_line)
  117.   write("  ")
  118.   local additive = (_time - 1) * 4
  119.   local currentx = 3
  120.   for i = 1,4 do
  121.     local buttonNumber = additive + i
  122.     write(button[buttonNumber].mob)
  123.     if i == 4 then
  124.      
  125.     else
  126.       currentx = currentx + 10
  127.       term.setCursorPos(currentx,_line)
  128.     end
  129.   end
  130.   term.setCursorPos(1, _line + 1)
  131. end
  132.    
  133.  
  134. function startText()
  135.   term.setCursorPos(1,1)
  136.   mPrint("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  137.   mPrint(" Welcome to spawn select! Control mob")
  138.   mPrint(" spawn choice by right click on screen")
  139.   mPrint(" ")
  140.   mobTypeWrite(5,1)
  141.   stateWriter()
  142.   mPrint(" ")
  143.   mobTypeWrite(10,2)
  144.   stateWriter()
  145.   mPrint(" ")
  146.   mobTypeWrite(15,3)
  147.   stateWriter()
  148.   mPrint("_______________________________________")
  149. end
  150.  
  151. function bundleState()
  152.   running = 0
  153.   for light = 1, 10 do
  154.     if button[light].state == true then
  155.       running = running + button[light].color
  156.     end
  157.   end
  158.   return running
  159. end
  160.  
  161. function refresh()
  162.   redstone.setBundledOutput(rsSide, bundleState())
  163. end
  164.  
  165. display = peripheral.wrap(monitorSide)
  166. term.redirect(display)
  167. term.clear()
  168. term.setCursorPos(1,1)
  169.  
  170. --This is the table that controls everything. Don't touch unless you know what you are doing.
  171.  
  172. button = {
  173.   [1] = {x = 5; y = 7; state = false; color = colors.white; mob = "Skele"},
  174.   [2] = {x = 15; y = 7; state = false; color = colors.orange; mob = "Wither"},
  175.   [3] = {x = 25; y = 7; state = false; color = colors.magenta; mob = "Ender"},
  176.   [4] = {x = 35; y = 7; state = false; color = colors.lightBlue; mob = "Zombie"},
  177.  
  178.   [5] = {x = 5; y = 12; state = false; color = colors.yellow; mob = "Creep"},
  179.   [6] = {x = 15; y = 12; state = false; color = colors.lime; mob = "Witch"},
  180.   [7] = {x = 25; y = 12; state = false; color = colors.pink; mob = "Pigman"},
  181.   [8] = {x = 35; y = 12; state = false; color = colors.gray; mob = "Cow"},
  182.  
  183.   [9] = {x = 5; y = 17; state = false; color = colors.lightGray; mob = "Blaze"},
  184.   [10] = {x = 15; y = 17; state = false; color = colors.cyan; mob = "Slime"},
  185.   [11] = {x = 25; y = 17; state = false; color = colors.purple; mob = "Toggle"},
  186.   [12] = {x = 35; y = 17; state = false; color = colors.blue; mob = "Master"}
  187.  
  188. }
  189.  
  190. local resume = true
  191. startText()
  192. for i = 1,10 do
  193.   sayState(i)
  194. end
  195. term.setCursorPos(button[12].x-1,button[12].y)
  196. term.setTextColor(colors.red)
  197. write("off")
  198. term.setTextColor(colors.white)
  199. term.setCursorPos(button[11].x-1,button[11].y)
  200. term.setTextColor(colors.cyan)
  201. write("TGL")
  202. term.setTextColor(colors.white)
  203. refresh()
  204. while resume == true do
  205.   local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  206.   local selectedButton = getButton(xPos,yPos)
  207.   if selectedButton == 11 then
  208.     for i = 1,10 do
  209.       toggle(i)
  210.     end
  211.   elseif selectedButton == 12 then
  212.     toggle(12)
  213.     for i=1,10 do
  214.       button[i].state = button[12].state
  215.     end
  216.     sayState(12)
  217.     allSame=true
  218.   else
  219.     term.setCursorPos(button[12].x - 1, button[12].y)
  220.     term.setTextColor(colors.lightGray)
  221.     write("---")
  222.     term.setTextColor(colors.white)
  223.     toggle(selectedButton)
  224.   end
  225.   for i=1,10 do
  226.     sayState(i)
  227.   end
  228.   if allTheSame() then
  229.     button[12].state = button[1].state
  230.     sayState(12)
  231.   else
  232.     term.setCursorPos(button[12].x - 1, button[12].y)
  233.     term.setTextColor(colors.lightGray)
  234.     write("---")
  235.     term.setTextColor(colors.white)
  236.   end
  237.   refresh()
  238. end
  239.  
  240. -- optional, more color appropriate colors
  241. --[==[ button = {
  242.   [1] = {x = 5; y = 7; state = false; color = colors.lightGray; mob = "Skele"},
  243.   [2] = {x = 15; y = 7; state = false; color = colors.gray; mob = "Wither"},
  244.   [3] = {x = 25; y = 7; state = false; color = colors.purple; mob = "Ender"},
  245.   [4] = {x = 35; y = 7; state = false; color = colors.cyan; mob = "Zombie"},
  246.  
  247.   [5] = {x = 5; y = 12; state = false; color = colors.green; mob = "Creep"},
  248.   [6] = {x = 15; y = 12; state = false; color = colors.magenta; mob = "Witch"},
  249.   [7] = {x = 25; y = 12; state = false; color = colors.pink; mob = "Pigman"},
  250.   [8] = {x = 35; y = 12; state = false; color = colors.brown; mob = "Cow"},
  251.  
  252.   [9] = {x = 5; y = 17; state = false; color = colors.yellow; mob = "Blaze"},
  253.   [10] = {x = 15; y = 17; state = false; color = colors.lime; mob = "Slime"},
  254.   [11] = {x = 25; y = 17; state = false; color = colors.purple; mob = "Toggle"},
  255.   [12] = {x = 35; y = 17; state = false; color = colors.blue; mob = "Master"}
  256.  
  257. } ]==]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement