krom

Minecraft - Waterboy

Feb 19th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. local bRedNet = false
  2. local nBatterySlot = 1
  3. -- ----------------------------------------------------------
  4. local function say(message)
  5.         if bRedNet == true then
  6.                 rednet.send(nMasterComputerID, message)
  7.         end
  8.         print(message)
  9.         -- sleep(0.2)
  10. end
  11. -- ----------------------------------------------------------
  12. local function turnLeft()
  13.         turtle.turnLeft()
  14. end
  15. -- ----------------------------------------------------------
  16. local function turnRight()
  17.         turtle.turnRight()
  18. end
  19. -- ----------------------------------------------------------
  20. local function getFreeSlots()
  21.     local nSpace = 0
  22.     for invloop=1,16 do
  23.         local items = turtle.getItemCount(invloop)
  24.         if items == 0 then
  25.             nSpace = nSpace + 1
  26.         end
  27.     end
  28.     return nSpace
  29. end
  30. -- ----------------------------------------------------------
  31. local function tryForwards()
  32.     local nMaxTries = 10
  33.     local nTries = 0
  34.     while not turtle.forward() do
  35.         sleep(0.8)
  36.         nTries = nTries + 1
  37.         if nTries >= nMaxTries then
  38.                 return false
  39.         end
  40.     end
  41.     return true
  42. end
  43. -- ----------------------------------------------------------
  44.  
  45. local function recharge()
  46.         say("trying to recharge")
  47.         turtle.select(nBatterySlot)
  48.         local e = turtle.getFuelLevel()
  49.         turtle.refuel()
  50.         nEnergy = turtle.getFuelLevel()
  51.         say("fuel: "..e.." -> "..nEnergy)
  52.         if nEnergy > e then
  53.                 return true
  54.         end
  55.         return false
  56. end
  57.  
  58. -- ----------------------------------------------------------
  59. -- ----------------------------------------------------------
  60. local function waitForEnergy()  
  61.         local count = 0
  62.         local hasEnergy = false
  63.         say("Waiting for energy in Slot "..nBatterySlot)
  64.  
  65.         while not hasEnergy do
  66.                 if recharge() == true then
  67.                         hasEnergy = true
  68.                         return true
  69.                 else
  70.                         sleep(5)
  71.                         count = count + 1
  72.                         if count == 5 then
  73.                                 say("Waiting for energy in Slot "..nBatterySlot)
  74.                                 count = 0
  75.                         end
  76.                 end
  77.         end
  78.         return true
  79. end
  80. -- ----------------------------------------------------------
  81. local function isOperational()
  82.         -- get energy
  83.         local nEnergyLevel = turtle.getFuelLevel()
  84.         local required = 20
  85.         if nEnergyLevel <= required then
  86.         waitForEnergy()  
  87.         end
  88.         -- all cool
  89.         return true
  90. end
  91. -- ----------------------------------------------------------
  92. local function loopi()
  93. isOperational()
  94.  
  95. while not turtle.suck() do
  96.     sleep(2)
  97. end
  98.  
  99. turnRight()
  100. turnRight()
  101. tryForwards()
  102. tryForwards()
  103. tryForwards()
  104. turnLeft()
  105. tryForwards()
  106. turnRight()
  107. tryForwards()
  108. tryForwards()
  109. tryForwards()
  110. tryForwards()
  111. turnLeft()
  112. tryForwards()
  113. turtle.select(2)
  114. turtle.drop()
  115. turnLeft()
  116. turnLeft()
  117. tryForwards()
  118. turnRight()
  119. tryForwards()
  120. tryForwards()
  121. tryForwards()
  122. tryForwards()
  123. turnLeft()
  124. tryForwards()
  125. turnRight()
  126. tryForwards()
  127. tryForwards()
  128. tryForwards()
  129. turtle.select(2)
  130. turtle.drop()
  131.  
  132. return true
  133. end
  134.  
  135. local done = false
  136. while not done do
  137.    loopi()
  138. end
Advertisement
Add Comment
Please, Sign In to add comment