Advertisement
lucifersamfr

mineController

Mar 15th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. local nbMiners = 8
  2. local nbLoaders = 1
  3.  
  4. -- MODEM CONFIG
  5. local idChannel = 10
  6. local idRespChannel = 11
  7. local cmdChannel = 12
  8. local cmdRespChannel = 13
  9.  
  10. -- MONITOR CONFIG
  11. os.loadAPI("button")
  12.  
  13. local modem = peripheral.wrap("right")
  14. local monitor = peripheral.wrap("top")
  15.  
  16. -- MONITOR MAIN TABLE
  17. function fillMainTable()
  18.   button.setTable("getIds", bgetIds, 10,20,3,5)
  19.   button.setTable("mine", bmine, 22,32,3,5)
  20.   button.screen()
  21. end
  22. -- MONITOR TOUCH EVENT
  23. function getClick()
  24.   event, side, x, y = os.pullEvent("monitor_touch")
  25.   button.checkxy(x,y)
  26. end
  27. -- BUTTON GETIDS
  28. function bgetIds()
  29.   button.flash("getIds")
  30.   getWorkersId()
  31. end
  32. -- BUTTON MINE
  33. function bmine()
  34.   button.toggleButton("mine")
  35.   mine()
  36.   button.toggleButton("mine")
  37. end
  38.  
  39. -- MINING PROCESS
  40. function mine()
  41.   modem.open(cmdRespChannel)
  42.  
  43.   local op={name="mine", mode=1}
  44.   modem.transmit(cmdChannel,cmdRespChannel,textutils.serialize(op))
  45.  
  46.   local repMinerCount = 0
  47.   local repLoaderCount = 0
  48.   local waiting = true
  49.  
  50.   while ( waiting ) do
  51.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  52.     if ( modemSide == "right") then
  53.       local rep=textutils.unserialize(message)
  54.       if (rep.name == "waiting" ) then
  55.         print("ID:"..(rep.ID).."label:"..(rep.label).."type:"..(rep.wType))
  56.         if ( rep.wType == "miner" ) then
  57.           repMinerCount = repMinerCount + 1
  58.         elseif ( rep.wType == "chunkLoader" ) then
  59.           repLoaderCount = repLoaderCount +1
  60.         end
  61.       end
  62.       if ( repMinerCount == nbMiners and repLoaderCount == nbLoaders ) then
  63.         waiting = false
  64.         modem.close(cmdRespChannel)
  65.         print(repMinerCount.." miners waiting.")
  66.         print(repLoaderCount.." chunk loaders identified.")
  67.       end    
  68.     end
  69.   end
  70.   modem.close(cmdRespChannel)
  71. end
  72.  
  73. -- GATHER WORKERS DATA
  74. function getWorkersId()
  75.   print("open idRespChannel")
  76.   modem.open(idRespChannel)
  77.  
  78.   print("Transmit request to idChannel("..idChannel..")")
  79.   local op={name="ID"}
  80.   print("op.name= "..op.name)
  81.   modem.transmit(idChannel,idRespChannel,textutils.serialize(op))
  82.   print("transmit OK")
  83.    
  84.   local repMinerCount = 0
  85.   local repLoaderCount = 0
  86.   local waiting = true
  87.  
  88.   while ( waiting ) do
  89.     print("Waiting for answers")
  90.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  91.     local rep=textutils.unserialize(message)
  92.     if (rep.name == "ID" ) then
  93.       print("ID:"..(rep.ID).."label:"..(rep.label).."type:"..(rep.wType))
  94.       if ( rep.wType == "miner" ) then
  95.         repMinerCount = repMinerCount + 1
  96.       elseif ( rep.wType == "chunkLoader" ) then
  97.         repLoaderCount = repLoaderCount +1
  98.       end
  99.     end
  100.     if ( repMinerCount == nbMiners and repLoaderCount == nbLoaders ) then
  101.       waiting = false
  102.       modem.close(idRespChannel)
  103.       print(repMinerCount.." miners identified.")
  104.       print(repLoaderCount.." chunk loaders identified.")
  105.     end
  106.   end
  107.   modem.close(idRespChannel)
  108. end
  109.  
  110.  
  111.  
  112. monitor.clear()
  113. fillMainTable()
  114. button.heading("Mining System")
  115.  
  116. while true do
  117.   getClick()
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement