Advertisement
charonme

monstercleanup

Jan 9th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- monstercleanup
  2. -- sweeps a (MAX_X+1) x (MAX_Y+1) area, kills monsters and plants torches (chunks must be loaded!)
  3. -- plant turtle ONE BLOCK ABOVE ground/floor so that torches could be placed under the turtle !!!
  4. -- plant turtle to the left bottom corner of the desired cleaning area (this will be the 0,0 position)
  5. -- X coordinate increases to the right, Y coordinate increases to the front
  6. -- turtle will need to be refueled to minimum of approx. (MAX_X+2)*(MAX_Y+2) (1 coal=80)
  7. -- load slots with torches (or whatever you want to be placed under the turtle as it sweeps the area)
  8. -- you will need about 2 torches per 100m^2 (+borders)
  9.  
  10. local MAX_X = 58
  11. local MAX_Y = 58
  12. local posX = 0
  13. local posY = 0
  14. local orient = 0 -- orientation relative to original placement: 0 = front, 1 = right, 2 = back, 3 = left
  15. local slotIndex = 1
  16.  
  17. -- ------------------------------------------------------------------------------------------------------------
  18. function left()
  19.     turtle.turnLeft()
  20.     orient = orient-1
  21.     while orient<0 do
  22.         orient = orient+4
  23.     end
  24. end
  25.  
  26. -- ------------------------------------------------------------------------------------------------------------
  27. function right()
  28.     turtle.turnRight()
  29.     orient = orient+1
  30.     while orient>3 do
  31.         orient = orient-4
  32.     end
  33. end
  34.  
  35. -- ------------------------------------------------------------------------------------------------------------
  36. function killMonsters()
  37.    
  38.     turtle.attack()
  39.     turtle.attack()
  40.     turtle.attackUp()
  41.     turtle.attackUp()
  42.     turtle.attackDown()
  43.     turtle.attackDown()
  44.     turtle.dig()
  45.     turtle.digDown()
  46.     os.sleep(0.7)
  47. end
  48.  
  49. -- ------------------------------------------------------------------------------------------------------------
  50. function forward()
  51.     while not turtle.forward() do
  52.         killMonsters()
  53.     end
  54.     if orient==0 then
  55.         posY = posY+1
  56.     end
  57.     if orient==1 then
  58.         posX = posX+1
  59.     end
  60.     if orient==2 then
  61.         posY = posY-1
  62.     end
  63.     if orient==3 then
  64.         posX = posX-1
  65.     end
  66. end
  67.  
  68. -- ------------------------------------------------------------------------------------------------------------
  69. function cleanupPlaceTorch()
  70.     while turtle.getItemCount(slotIndex)==0 and slotIndex<16 do
  71.         slotIndex = slotIndex + 1
  72.     end
  73.     if turtle.getItemCount(slotIndex) > 0 then
  74.         while not turtle.placeDown() do
  75.             killMonsters()
  76.         end
  77.     end
  78. end
  79.  
  80. -- ------------------------------------------------------------------------------------------------------------
  81. function cleanupStepAction()
  82.     -- kill monsters all around
  83.     for i=1,4 do
  84.         killMonsters()
  85.         turtle.turnRight()
  86.     end
  87.  
  88.     if (posX % 10 == 0 and posY % 10 == 0) or ((posX+5) % 10 == 0 and (posY+5) % 10 == 0) then
  89.         cleanupPlaceTorch()
  90.     end
  91.  
  92. end
  93.  
  94. -- ------------------------------------------------------------------------------------------------------------
  95. function cleanupStepMove()
  96.     local result = true
  97.  
  98.     if orient==0 then
  99.         if posY<MAX_Y then
  100.             forward()
  101.         elseif posX<MAX_X then
  102.             right()
  103.             forward()
  104.             right()
  105.         else
  106.             result = false
  107.         end
  108.     elseif orient==2 then
  109.         if posY>0 then
  110.             forward()
  111.         elseif posX<MAX_X then
  112.             left()
  113.             forward()
  114.             left()
  115.         else
  116.             result = false
  117.         end
  118.     end
  119.  
  120.     return result
  121. end
  122.  
  123. -- ------------------------------------------------------------------------------------------------------------
  124. function cleanupFinish()
  125.     -- move back to starting position
  126.     while orient~=3 do
  127.         right()
  128.     end
  129.     while posX>0 do
  130.         forward()
  131.     end
  132.     while orient~=2 do
  133.         left()
  134.     end
  135.     while posY>0 do
  136.         forward()
  137.     end
  138. end
  139.  
  140. -- ------------------------------------------------------------------------------------------------------------
  141. function main()
  142.     local cycle = true
  143.     while cycle do
  144.         cleanupStepAction()
  145.         cycle = cleanupStepMove() -- returns false if finished
  146.     end
  147.     cleanupFinish() -- move back to starting position
  148.  
  149. end
  150.  
  151. -- ============================================================================================================
  152.  
  153. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement