Advertisement
nameless12242312

CC:Tweaked MoveLib

Jun 5th, 2024 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | Gaming | 0 0
  1. lib = {}
  2.  
  3. lib.vectorFacings = {
  4.     [0] = vector.new(0, 0, -1), -- north (-Z)
  5.     [1] = vector.new(1, 0, 0),  -- east (+X)
  6.     [2] = vector.new(0, 0, 1),  -- south (+Z)
  7.     [3] = vector.new(-1, 0, 0), -- west (-X)
  8. }
  9.  
  10. function lib.forward()
  11.     moved = turtle.forward()
  12.     if moved then
  13.         position = position + lib.vectorFacings[facing]
  14.     else
  15.         turtle.dig()
  16.         lib.forward()
  17.     end
  18. end
  19.  
  20. function lib.backward()
  21.     moved = turtle.backward()
  22.     if moved then
  23.         position = position + vector.new(0, 1, 0)
  24.     else
  25.         lib.face(math.abs(facing-2)%4)
  26.         turtle.dig()
  27.         lib.face(math.abs(facing-2)%4)
  28.         lib.backward()
  29.     end
  30. end
  31.  
  32. function lib.upward()
  33.     moved = turtle.up()
  34.     if moved then
  35.         position = position + vector.new(0, 1, 0)
  36.     else
  37.         turtle.digUp()
  38.         lib.upward()
  39.     end
  40. end
  41.  
  42. function lib.downward()
  43.     moved = turtle.down()
  44.     if moved then
  45.         position = position + vector.new(0, -1, 0)
  46.     else
  47.         turtle.digDown()
  48.         lib.downward()
  49.     end
  50. end
  51.  
  52. function lib.turn_right()
  53.     turtle.turnRight()
  54.     facing = (facing + 1) % 4
  55. end
  56.  
  57. function lib.turn_left()
  58.     turtle.turnLeft()
  59.     facing = (facing - 1) % 4
  60.     if facing < 0 then
  61.         facing = 3
  62.     end
  63. end
  64.  
  65. function lib.face(direction)
  66.     if facing == direction then return end
  67.  
  68.     local turn
  69.  
  70.     if (facing + 1) % 4 == direction then
  71.        turn = lib.turn_right
  72.     else
  73.        turn = lib.turn_left
  74.     end
  75.  
  76.     repeat
  77.         turn()
  78.     until facing == direction
  79. end
  80.  
  81. function lib.goto(newPosition)
  82.     -- Align on the y-axis
  83.     while position.y < newPosition.y do
  84.         lib.upward()
  85.     end
  86.     while position.y > newPosition.y do
  87.         lib.downward()
  88.     end
  89.  
  90.     -- Align on the z-axis
  91.     if position.z > newPosition.z then
  92.         lib.face(0) -- towards the negative direction
  93.     elseif position.z < newPosition.z then
  94.         lib.face(2)
  95.     end
  96.    
  97.     while position.z ~= newPosition.z do
  98.         lib.forward()
  99.     end
  100.  
  101.     -- Align on the x-axis
  102.     if position.x > newPosition.x then
  103.         lib.face(3) -- towards the negative direction
  104.     elseif position.x < newPosition.x then
  105.         lib.face(1)
  106.     end
  107.  
  108.     while position.x ~= newPosition.x do
  109.         lib.forward()
  110.     end
  111. end
  112.  
  113. function lib.tunnelBore(length, width, height)
  114.     startPose = position
  115.     startFacing = facing
  116.  
  117.     lib.forward()
  118.     for i = 1, ((length) * width) do
  119.         gotoHeight = (i % 2 == 1) and position.y + height - 1 or startPose.y
  120.         lib.goto(vector.new(position.x, gotoHeight, position.z))  
  121.  
  122.         if i % width == 0 or i == 1 then
  123.             dir = (((i / width) % 2 == 1)) and lib.vectorFacings[(startFacing - 1) % 4]  or lib.vectorFacings[(startFacing + 1) % 4]
  124.             if i == 1 then
  125.                 lib.goto(position:add(dir))
  126.             elseif i~= length * width then
  127.                 lib.face(startFacing)
  128.                 lib.forward()
  129.             end
  130.         else
  131.             lib.goto(position:add(dir))
  132.         end
  133.     end
  134.    
  135.     lib.goto(startPose)
  136.     lib.face(startFacing)
  137. end
  138.  
  139. function lib.quarryBore(length, width, height)
  140.     startPose = position
  141.     startFacing = facing
  142.  
  143.     for i = 1, ((length) * height) do
  144.         gotoWidth = (i % 2 == 1) and position:add(lib.vectorFacings[startFacing]:mul(width - 1)) or position:sub(lib.vectorFacings[startFacing]:mul(width - 1))
  145.         lib.goto(gotoWidth)  
  146.  
  147.         if i % width == 0 or i == 1 then
  148.             dir = (((i / width) % 2 == 1)) and lib.vectorFacings[(startFacing - 1) % 4]  or lib.vectorFacings[(startFacing + 1) % 4]
  149.             if i == 1 then
  150.                 lib.goto(position:add(dir))
  151.             elseif i~= length * height then
  152.                 lib.face(startFacing)
  153.                 lib.downward()
  154.             end
  155.         else
  156.             lib.goto(position:add(dir))
  157.         end
  158.     end
  159.  
  160.     lib.goto(startPose)
  161.     lib.face(startFacing)
  162. end
  163.  
  164. return lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement