Advertisement
lucifersamfr

MSminer

Apr 1st, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. --delai entre chaque vidage de coffre
  2. local stepDelay = 3
  3.  
  4. --channel de reception d'ordre de minage et channel de reponse
  5. local minChannel = 10
  6. local minRespChannel = 11
  7.  
  8. --ouverture du modem / reception des ordres
  9. local modem = peripheral.wrap("right")
  10. modem.open(minChannel)
  11.  
  12. function reportCommander(valid)
  13.   if (valid) then
  14.     print("report OK to Commander")
  15.     modem.transmit(minRespChannel,minChannel,"mining OK")
  16.   else
  17.     print("report KO to Commander")
  18.     modem.transmit(minRespChannel,minChannel,"mining KO")
  19.   end
  20. end
  21.  
  22. function cleanInv()
  23.   print("cleaning inventory...")
  24.   local nbItems = 0
  25.   for i=1,16 do
  26.     turtle.select(i)
  27.     nbItems = nbItems + turtle.getItemCount(i)
  28.     turtle.dropUp()
  29.   end
  30.   return nbItems
  31. end
  32.  
  33. function storeMiner()
  34.   print("storing Miner")
  35.   turtle.select(1)
  36.   turtle.dig()
  37. end
  38.  
  39. function placeMiner()
  40.   print("place Miner")
  41.   turtle.select(1)
  42.   turtle.place()
  43.   sleep(stepDelay)
  44. end
  45.  
  46. function initMining()
  47.   print("[initMining]")
  48.   placeMiner()
  49.  
  50.   local hasFinishedMining = false
  51.   local hasMined = false
  52.   while not hasFinishedMining do
  53.     if ( cleanInv() > 0 ) then
  54.       --continue mining if there was stuff mined in the last stepDelay
  55.       hasMined = true
  56.       sleep(stepDelay)
  57.     else
  58.       if ( hasMined ) then
  59.         --if no stuff was mined in the last stepDelay and stuff already has been mined before,
  60.         --  then signal end of mining
  61.         hasFinishedMining = true
  62.       end
  63.     end
  64.   end
  65.  
  66.   storeMiner()
  67.   reportCommander(true)
  68. end
  69.  
  70. function getMessage()
  71.   print("waiting for message...")
  72.   local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  73.   if ( message == "initMining!" ) then
  74.     initMining()
  75.   end
  76. end
  77.  
  78. while true do
  79.   getMessage()
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement