BioPrince

spawnControl (computer)

May 22nd, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.unloadAPI("touchpoint")
  2. os.loadAPI("touchpoint")
  3. rednet.open("left") -- change to side wireless modem is on
  4.  
  5.  
  6. local mon = "top" -- side advanced monitor is on
  7.  
  8. local spawnTurtleID = -1 -- do not change set in first line of spawnList
  9. local m = peripheral.wrap(mon)
  10. local pageOne = touchpoint.new(mon)
  11. local pageTwo = touchpoint.new(mon)
  12. local page = 1
  13. -- dont change anything here. just list up to 16 mobs in spawnList
  14. local pageOneSpawns = {}
  15. local pageTwoSpawns = {}
  16.  
  17. local space = 2
  18. local width, height = m.getSize()
  19.  
  20. m.setTextScale(1)
  21. term.clear()
  22. term.setCursorPos(1,1)
  23. m.clear()
  24. m.setCursorPos(1,1)
  25.  
  26. function tableLength(T)
  27.     local count = 0
  28.     if T == nil then
  29.     return 0
  30.     else
  31.         for _ in pairs(T) do
  32.             count = count + 1
  33.         end
  34.         return count
  35.     end
  36. end
  37.  
  38. function loadList()
  39.     if fs.exists("spawnList") then
  40.         local spawns = 0
  41.         local file = fs.open("spawnList", "r")
  42.         spawnTurtleID = tonumber(file.readLine())
  43.         print("Turtle ID: "..spawnTurtleID)
  44.         while true do
  45.             local line = file.readLine()
  46.             spawns = spawns + 1
  47.             if line == nil then break end
  48.             if spawns < 9 then
  49.                 if spawns == 8 then
  50.                     table.insert(pageOneSpawns, line)
  51.                     table.insert(pageOneSpawns, "Next page")
  52.                 else
  53.                     table.insert(pageOneSpawns, line)
  54.                 end
  55.             else
  56.                     table.insert(pageTwoSpawns, line)
  57.             end
  58.         end
  59.         if spawns > 9 then
  60.             table.insert(pageTwoSpawns, line)
  61.             table.insert(pageTwoSpawns, "Previous page")
  62.         end
  63.         file:close()
  64.     else
  65.         error("Spawn list not found.")
  66.     end
  67. end
  68.  
  69. function autoButton(bText, bX, bY, pg)
  70.     xMax = bX + string.len(bText) + 1
  71.     yMax = bY
  72.    
  73.     if pg == 1 then
  74.         if string.lower(bText) == "next page" then
  75.             pageOne:add(bText, func, bX, bY, xMax, yMax, colors.purple, colors.lime)
  76.         else
  77.             pageOne:add(bText, func, bX, bY, xMax, yMax, colors.gray, colors.lightBlue)
  78.         end
  79.     end
  80.    
  81.     if pg == 2 then
  82.         if string.lower(bText) == "previous page" then
  83.             pageTwo:add(bText, func, bX, bY, xMax, yMax, colors.purple, colors.lime)
  84.         else
  85.             pageTwo:add(bText, func, bX, bY, xMax, yMax, colors.gray, colors.lightBlue)
  86.         end
  87.     end
  88. end
  89.  
  90. function sendCommand(cmd)
  91.     print("Sending command - ".. cmd)
  92.     notSent = true
  93.     while notSent do
  94.         rednet.send(spawnTurtleID, cmd)
  95.         os.sleep(.1)
  96.         message = nil
  97.         senderID, message, distance = rednet.receive(3)
  98.         if message == "gotIt" then
  99.             notSent = false
  100.         end
  101.         print("Re-sending command...")
  102.     end
  103.     print("Command Received.")
  104. end
  105.  
  106. function drawPage()
  107.     if page == 1 then
  108.         pageOne:draw()
  109.     end
  110.     if page == 2 then
  111.         pageTwo:draw()
  112.     end
  113. end
  114.  
  115. loadList()
  116.  
  117. for i = 1, tableLength(pageOneSpawns) do
  118.     autoButton(pageOneSpawns[i], math.ceil((width - string.len(pageOneSpawns[i]))/ 2), i * space, 1)   
  119. end
  120.  
  121. if tableLength(pageTwoSpawns) > 0 then
  122.     for n = 1, tableLength(pageTwoSpawns) do
  123.         autoButton(pageTwoSpawns[n], math.ceil((width - string.len(pageTwoSpawns[n]))/ 2), n * space, 2)
  124.     end
  125. end
  126.  
  127. drawPage()
  128. print("Waiting for input.")
  129. while true do
  130.     event = nil
  131.     p1 = nil
  132.     if page == 1 then
  133.         event, p1 = pageOne:handleEvents(os.pullEvent())
  134.     end
  135.    
  136.     if page == 2 then
  137.         event, p1 = pageTwo:handleEvents(os.pullEvent())
  138.     end
  139.    
  140.     if page == 1 then
  141.         for k = 1, tableLength(pageOneSpawns) do
  142.            
  143.             if p1 == pageOneSpawns[k] then
  144.                 if k == 9 then
  145.                     pageOne:flash(pageOneSpawns[k])
  146.                     page = 2
  147.                     drawPage()
  148.                 else
  149.                     pageOne:toggleButton(pageOneSpawns[k])
  150.                     sendCommand(k)
  151.                 end
  152.             end
  153.         end
  154.     end
  155.  
  156.     if page == 2 then
  157.         for k = 1, tableLength(pageTwoSpawns) do
  158.            
  159.             if p1 == pageTwoSpawns[k] then
  160.                 if  string.lower(p1) == "previous page" then
  161.                     pageTwo:flash(pageTwoSpawns[k])
  162.                     page = 1
  163.                     drawPage()
  164.                 else
  165.                     pageTwo:toggleButton(pageTwoSpawns[k])
  166.                     sendCommand(k + 8)
  167.                 end
  168.             end
  169.         end
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment