Guest User

control

a guest
Apr 27th, 2014
586
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("touchpoint")
  2. mon=peripheral.wrap("left")
  3. t={}
  4. local curPage=1
  5. local numPage=0
  6. wait=0
  7. grindersOn=false
  8. spawnerOn=false
  9.  
  10. function getData()
  11.   rednet.open("top")
  12.   print("Waiting for message...")
  13.   local event, ID, message, dist = os.pullEvent("rednet_message")
  14.   sleep(0.2)
  15.   if ID == 0 then
  16.     print("Message recieved at distance of "..dist..".")
  17.     invTable=textutils.unserialize(message)
  18.   end
  19.   numPage=math.ceil(#invTable/24)
  20. end
  21.  
  22. function sendData(data)
  23.   rednet.open("top")
  24.   setToggle(data)
  25.   if data=="grinders" and grindersOn==false then
  26.     buttonName={"   Grinders  ", "     On      ", label="grinders"}
  27.     setName("grinders")
  28.     grindersOn=true
  29.   elseif data=="grinders" and grindersOn==true then
  30.     buttonName={"   Grinders  ", "     Off     ", label="grinders"}
  31.     setName("grinders")
  32.     grindersOn=false
  33.   elseif data=="spawner" and spawnerOn==false then
  34.     buttonName={"   Spawner   ", "     On      ", label="spawner"}
  35.     setName("spawner")
  36.     spawnerOn=true
  37.   elseif data=="spawner" and spawnerOn==true then
  38.     buttonName={"   Spawner   ", "     Off     ", label="spawner"}
  39.     setName("spawner")
  40.     spawnerOn=false
  41.   elseif data=="clear" then
  42.     sleep(0.2)
  43.     t[curPage]:toggleButton(data)
  44.     current="none"
  45.     setCurrent()
  46.   end
  47.   if data=="swap" then
  48.     if wait==0 then
  49.       wait=1
  50.     else
  51.       wait=0
  52.     end
  53.   elseif data=="remove" then
  54.     if wait==0 then
  55.       wait=2
  56.     else
  57.       wait=0
  58.     end
  59.   else
  60.     wait=0
  61.   end
  62.   print("Sending data: "..data)
  63.   rednet.send(0, data)
  64.   sleep(0.2)
  65. end
  66.  
  67. function previousPage()
  68.   t[curPage]:flash("prev")
  69.   curPage=curPage-1
  70.   t[curPage]:draw()
  71.   t[curPage]:run()
  72. end
  73.  
  74. function nextPage()
  75.   t[curPage]:flash("next")
  76.   curPage=curPage+1
  77.   t[curPage]:draw()
  78.   t[curPage]:run()
  79. end
  80.  
  81. function setName(set)
  82.   for i=1, numPage do
  83.     t[i]:rename(set, buttonName)
  84.   end
  85. end
  86.  
  87. function setCurrent()
  88.   buttonName={"Current: "..current, label="current"}
  89.   for i=1, numPage do
  90.     t[i]:rename("current", buttonName)
  91.   end
  92. end
  93.  
  94. function setToggle(set)
  95.   for i=1, numPage do
  96.     t[i]:toggleButton(set)
  97.   end
  98. end
  99.  
  100. function refresh()
  101.   drawPage=1
  102.   t[drawPage]=touchpoint.new("left")
  103.   rednet.open("top")
  104.   rednet.send(0, "refresh")
  105.   getData()
  106.   column=1
  107.   current="none"
  108.   for i, v in pairs(invTable) do
  109.     xmin=((column-1)*22)+2
  110.     ymin=((i-((drawPage-1)*24))-((column-1)*8))*3
  111.     xmax=xmin+20
  112.     ymax=ymin+1
  113.     t[drawPage]:add(i..": "..v, function() addMob(i, v) end, xmin, ymin, xmax, ymax, colors.blue, colors.lime)
  114.     if i%8==0 then
  115.       column=column+1
  116.     end
  117.     if i%24==0 then
  118.       drawPage=drawPage+1
  119.       t[drawPage]=touchpoint.new("left")
  120.       column=1
  121.     end
  122.   end
  123.   for i=1, numPage do
  124.     t[i]:add("Refresh", function() refresh() end, 69, 24, 81, 25, colors.red, colors.lime)
  125.     buttonName={"    Clear    ", "   Spawner   ", label="clear"}
  126.     t[i]:add(buttonName, function() sendData("clear") end, 69, 3, 80, 4, colors.red, colors.lime)
  127.     if grindersOn==false then
  128.       buttonName={"   Grinders  ", "     Off     ", label="grinders"}
  129.       t[i]:add(buttonName, function() sendData("grinders") end, 69, 6, 80, 7, colors.red, colors.lime)
  130.     else
  131.       buttonName={"   Grinders  ", "     On      ", label="grinders"}
  132.       t[i]:add(buttonName, function() sendData("grinders") end, 69, 6, 80, 7, colors.lime, colors.red)
  133.     end
  134.     if spawnerOn==false then
  135.       buttonName={"   Spawner   ", "     Off     ", label="spawner"}
  136.       t[i]:add(buttonName, function() sendData("spawner") end, 69, 9, 80, 10, colors.red, colors.lime)
  137.     else
  138.       buttonName={"   Spawner   ", "     On      ", label="spawner"}
  139.       t[i]:add(buttonName, function() sendData("spawner") end, 69, 9, 80, 10, colors.lime, colors.red)
  140.     end
  141.     buttonName={"     Swap    ", "     Mobs    ", label="swap"}
  142.     t[i]:add(buttonName, function() sendData("swap") end, 69, 12, 80, 13, colors.red, colors.lime)
  143.     buttonName={"    Remove   ", "     Mob     ", label="remove"}
  144.     t[i]:add(buttonName, function() sendData("remove") end, 69, 15, 80, 16, colors.red, colors.lime)
  145.     if i<numPage then
  146.       buttonName={"     Next    ", "     Page    ", label="next"}
  147.       t[i]:add(buttonName, function() nextPage() end, 69, 18, 80, 19, colors.blue, colors.lime)
  148.     end
  149.     if i>1 then
  150.       buttonName={"   Previous  ", "     Page    ", label="prev"}
  151.       t[i]:add(buttonName, function() previousPage() end, 69, 21, 80, 22, colors.blue, colors.lime)
  152.     end
  153.     buttonName={"Current: "..current, label="current"}
  154.     t[i]:add(buttonName, nil, 1, 1, 40, 1, colors.black, colors.black)
  155.     t[i]:add("Mob Spawner Control", nil, 42, 1, 63, 1, colors.black, colors.black)
  156.     t[i]:add("Page "..i, nil, 70, 1, 82, 1, colors.black, colors.black)
  157.   end
  158.   t[curPage]:draw()
  159.   t[curPage]:run()
  160. end
  161.  
  162. function addMob(num, name)
  163.   rednet.send(0, num)
  164.   print("Selected num "..num..", type "..name)
  165.   if wait==0 or wait>2 then
  166.     t[curPage]:flash(num..": "..name)
  167.     current=name
  168.     setCurrent()
  169.     wait=0
  170.   elseif wait==1 then
  171.     t[curPage]:toggleButton(num..": "..name)
  172.     wait=2
  173.   elseif wait==2 then
  174.     t[curPage]:toggleButton(num..": "..name)
  175.     wait=0
  176.     local event, ID, message, dist = os.pullEvent("rednet_message")
  177.       if message=="done" then
  178.         print("done")
  179.       else
  180.         print("failed")
  181.       end
  182.       sleep(0.2)
  183.       refresh()
  184.   end
  185. end
  186.  
  187. while true do
  188.   term.clear()
  189.   term.setCursorPos(1,1)
  190.   refresh()
  191. end
RAW Paste Data