Advertisement
Neon1432

moving and returning

Nov 26th, 2020 (edited)
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.09 KB | None | 0 0
  1. dofile("bs/apis/basics")
  2. dofile("bs/apis/ui")
  3.  
  4.  
  5.  
  6. -- general
  7.  
  8.  
  9.  
  10. local posX = 0
  11. local posZ = 1
  12. local negX = 2
  13. local negZ = 3
  14. direction = { posX = posX, posZ = posZ, negX = negX, negZ = negZ }
  15.  
  16.  
  17.  
  18. --pathList
  19.  
  20.  
  21.  
  22. local Apath = {}
  23. pathList = {}
  24.  
  25. local function addToPathList(path)
  26.     local x = 1
  27.     while pathList[x] ~= nil do
  28.         x = x + 1
  29.     end
  30.     pathList[x] = path
  31. end
  32.  
  33.  
  34.  
  35. local function getIndex(path)
  36.     local x = 1
  37.     while pathList[x] ~= nil do
  38.         if pathList[x] == path then
  39.             return x
  40.         end
  41.         x = x + 1
  42.     end
  43.     return nil
  44. end
  45.  
  46.  
  47.  
  48. local function deleteFromPathList(path)
  49.     local x = 1
  50.     local trigger = false
  51.     while pathList[x] ~= nil do
  52.         if pathList[x] == path then
  53.             trigger = true
  54.         end
  55.         if trigger == true then
  56.             pathList[x] = pathList[x + 1]
  57.         end
  58.         x = x + 1
  59.     end
  60.     if trigger == true then
  61.         return true
  62.     else
  63.         return false
  64.     end
  65. end
  66.  
  67.  
  68.  
  69. local function addToPath(action)
  70.     local inner = {}
  71.     inner[1] = 1
  72.     inner[2] = action
  73.     local x = 1
  74.     while pathList[x] ~= nil do
  75.         local y = 1
  76.         while pathList[x][y] ~= nil do
  77.             y = y + 1
  78.         end
  79.         pathList[x][y] = inner
  80.         x = x + 1
  81.     end
  82. end
  83.  
  84.  
  85.  
  86. -- move
  87.  
  88.  
  89.  
  90. local function left()
  91.     turtle.turnLeft()
  92.  
  93.     addToPath("left")
  94. end
  95.  
  96.  
  97.  
  98. local function right()
  99.     turtle.turnRight()
  100.  
  101.     addToPath("right")
  102. end
  103.  
  104.  
  105.  
  106. local function Fforward()
  107.     if not turtle.forward() then
  108.         print("something is obstructing the way")
  109.         sleep(1)
  110.         ui.clear()
  111.         Fforward()
  112.     end
  113. end
  114.  
  115. local function forward()
  116.     if turtle.getFuelLevel() == 0 then
  117.         print("Out of fuel! Give me coal")
  118.         ui.pressenter()
  119.         basics.refuelanywhere()
  120.     end
  121.     if not turtle.forward() then
  122.         ui.clear()
  123.         sleep(1)
  124.         Fforward()
  125.     end
  126.  
  127.     addToPath("forward")
  128. end
  129.  
  130.  
  131.  
  132. local function Fback()
  133.     if not turtle.back() then
  134.         print("something is obstucting the way")
  135.         sleep(1)
  136.         ui.clear()
  137.         Fback()
  138.     end
  139. end
  140.  
  141. local function back()
  142.     if turtle.getFuelLevel() == 0 then
  143.         print("Out of fuel! Give me coal")
  144.         ui.pressenter()
  145.         basics.refuelanywhere()
  146.     end
  147.     if not turtle.back() then
  148.         ui.clear()
  149.         sleep(1)
  150.         Fback()
  151.     end
  152.  
  153.     addToPath("back")
  154. end
  155.  
  156.  
  157.  
  158. local function Fup()
  159.     if not turtle.up() then
  160.         print("something is obstucting the way")
  161.         sleep(1)
  162.         ui.clear()
  163.         Fup()  
  164.     end
  165. end
  166.  
  167. local function up()
  168.     if turtle.getFuelLevel() == 0 then
  169.         print("Out of fuel! Give me coal")
  170.         ui.pressenter()
  171.         basics.refuelanywhere()
  172.     end
  173.     if not turtle.up() then
  174.         ui.clear()
  175.         sleep(1)
  176.         Fup()  
  177.     end
  178.  
  179.     py = py + 1
  180.     addToPath("up")
  181. end
  182.  
  183.  
  184.  
  185. local function Fdown()
  186.     if not turtle.down() then
  187.         print("something is obstucting the way")
  188.         sleep(1)
  189.         ui.clear()
  190.         Fdown()
  191.     end
  192. end
  193.  
  194. local function down()
  195.     if turtle.getFuelLevel() == 0 then
  196.         print("Out of fuel! Give me coal")
  197.         ui.pressenter()
  198.         basics.refuelanywhere()
  199.     end
  200.     if not turtle.down() then
  201.         ui.clear()
  202.         sleep(1)
  203.         Fdown()
  204.     end
  205.  
  206.     py = py - 1
  207.     addToPath("down")
  208. end
  209.  
  210.  
  211.  
  212. local function relativeTo(x,y,z)
  213.     if x == nil or y == nil or z == nil then
  214.         ui.printing("Enter x, y, z as parameters", colors.red)
  215.         ui.pressenter()
  216.         return false
  217.     end
  218.     if tonumber(x) == nil or tonumber(y) == nil or tonumber(z) == nil then
  219.         ui.printing("One of the parameters you entered isnt a number", colors.red)
  220.         ui.pressenter()
  221.         return false
  222.     end
  223.  
  224.     local FxNegative = false
  225.     if x < 0 then
  226.         x = math.abs(x)
  227.         FxNegative = true
  228.         left()
  229.         left()
  230.     end
  231.     for I = 1, x do
  232.         forward()
  233.     end
  234.  
  235.     if y < 0 then
  236.         y = math.abs(y)
  237.         for I = 1, y do
  238.             down()
  239.         end
  240.     else
  241.         for I = 1, y do
  242.             up()
  243.         end
  244.     end
  245.  
  246.     if z == 0 then return true end
  247.     if z < 0 then
  248.         if FxNegative == true then
  249.             z = math.abs(z)
  250.             right()
  251.         else
  252.             z = math.abs(z)
  253.             left()
  254.         end
  255.     else
  256.         if FxNegative == true then
  257.             left()
  258.         else
  259.             right()
  260.         end
  261.     end
  262.     for I = 1, z do
  263.         forward()
  264.     end
  265. end
  266.  
  267.  
  268.  
  269. -- path
  270.  
  271.  
  272.  
  273. local function getPos()
  274.     local x, y, z = gps.locate()
  275.     local Adirection = 0
  276.     local turncounter = 0
  277.     while not turtle.forward() do
  278.         turtle.turnRight()
  279.         turncounter = turncounter + 1
  280.         if turncounter > 7 then
  281.             print("unable to get direction")
  282.             return nil
  283.         end
  284.     end
  285.  
  286.     local x2, y2, z2 = gps.locate()    
  287.     if x2 ~= x then
  288.         if x2 > x then
  289.             Adirection = direction.posX
  290.         elseif x2 < x then
  291.             Adirection = negX
  292.         end
  293.     else
  294.         if z2 > z then
  295.             Adirection = posZ
  296.         elseif z2 < z then
  297.             Adirection = negZ
  298.         end
  299.     end
  300.     turtle.back()
  301.  
  302.     for I = 1, turncounter do
  303.         if turncounter == 4 then
  304.             break
  305.         end
  306.         turtle.turnLeft()
  307.         if Adirection == 0 then
  308.             Adirection = 3
  309.             else
  310.             Adirection = Adirection - 1
  311.         end
  312.     end
  313.  
  314.     if x < 0 then x = x + 1 end
  315.     if z < 0 then z = z + 1 end
  316.     local coordinates = {x, y, z, Adirection}
  317.     return coordinates
  318. end
  319.  
  320.  
  321. -- bs.path.follow({{2, "back"}, {2, "left"}, {1, "forward"}, {1, "right"}, {2, "back"}})
  322. -- bs.path.follow(bs.path.inverse({{2, "back"}, {2, "left"}, {1, "forward"}, {1, "right"}, {2, "back"}}))
  323.  
  324.  
  325. local function followStatement(statement)
  326.     if statement == "forward" then
  327.         forward()
  328.     elseif statement == "back" then
  329.         back()
  330.     elseif statement == "up" then
  331.         up()
  332.     elseif statement == "down" then
  333.         down()
  334.     elseif statement == "left" then
  335.         left()
  336.     elseif statement == "right" then
  337.         right()
  338.     else
  339.         print("Error in -followStatement- invalid statement: '"..statement.."'")
  340.         ui.pressenter()
  341.     end
  342. end
  343.  
  344.  
  345.  
  346. local function follow(path)
  347.     local x = 1
  348.     while path[x] ~= nil do
  349.         for I= 1, path[x][1] do
  350.             followStatement(path[x][2])
  351.         end
  352.         x = x + 1
  353.     end
  354. end
  355.  
  356.  
  357.  
  358. local function inverseStatement(statement)
  359.     if statement == "forward" then
  360.         return "back"
  361.     elseif statement == "back" then
  362.         return "forward"
  363.     elseif statement == "up" then
  364.         return "down"
  365.     elseif statement == "down" then
  366.         return "up"
  367.     elseif statement == "left" then
  368.         return "right"
  369.     elseif statement == "right" then
  370.         return "left"
  371.     else
  372.         print("Error in -inverseStatement- invalid statement: '"..statement.."'")
  373.         ui.pressenter()
  374.     end
  375. end
  376.  
  377.  
  378.  
  379. local function inverse(path)
  380.     local x = 1
  381.     local MaxIndex = 0
  382.     while path[x] ~= nil do
  383.         MaxIndex = x
  384.         x = x + 1
  385.     end
  386.     x = 1
  387.     local returnPath = {}
  388.     for I= MaxIndex, 1, -1 do
  389.         local inner = {}
  390.         inner[1] = path[I][1]
  391.         inner[2] = inverseStatement(path[I][2])
  392.         returnPath[x] = inner
  393.         x = x + 1
  394.     end
  395.     return returnPath
  396. end
  397.  
  398.  
  399. -- tt = bs.path.make(bs.path.getPos(), {-11, 59, -9, 2})
  400.  
  401. local function make(coordinates1, coordinates2)
  402.     local tx = coordinates2[1] - coordinates1[1]
  403.     local ty = coordinates2[2] - coordinates1[2]
  404.     local tz = coordinates2[3] - coordinates1[3]
  405.     local curDirection = coordinates1[4]
  406.     local returnpath = {}
  407.     if tx > 0 then
  408.         if curDirection == 3 then
  409.             returnpath[1] = {1, "right"}
  410.         else
  411.             returnpath[1] = {curDirection, "left"}
  412.         end
  413.         curDirection = 0
  414.     elseif tx == 0 then
  415.         returnpath[1] = {0, "right"}
  416.     else
  417.         if curDirection == 3 then
  418.             returnpath[1] = {1, "left"}
  419.         else
  420.             returnpath[1] = {2 - curDirection, "right"}
  421.         end
  422.         tx = tx * -1
  423.         curDirection = 2
  424.     end
  425.     returnpath[2] = {tx, "forward"}
  426.  
  427.  
  428.     if ty > 0 then
  429.         returnpath[3] = {ty, "up"}
  430.     elseif ty == 0 then
  431.         returnpath[3] = {0, "up"}
  432.     else
  433.         returnpath[3] = {ty * -1, "down"}
  434.     end
  435.  
  436.  
  437.     if tz > 0 then
  438.         if curDirection == 0 then
  439.             returnpath[4] = {1, "right"}
  440.         else
  441.             returnpath[4] = {curDirection - 1, "left"}
  442.         end
  443.         curDirection = 1
  444.     elseif tz == 0 then
  445.         returnpath[4] = {0, "forward"}
  446.     else
  447.         if curDirection == 0 then
  448.             returnpath[4] = {1, "left"}
  449.         else
  450.             returnpath[4] = {3 - curDirection, "right"}
  451.         end
  452.         tz = tz * -1
  453.         curDirection = 3
  454.     end
  455.     returnpath[5] = {tz, "forward"}
  456.  
  457.     if coordinates2[4] ~= curDirection then
  458.         if coordinates2[4] > curDirection then
  459.             returnpath[6] = {coordinates2[4] - curDirection, "right"}  
  460.         else
  461.             returnpath[6] = {curDirection - coordinates2[4], "left"}
  462.         end
  463.     end
  464.  
  465.     return returnpath
  466. end
  467.  
  468.  
  469.  
  470. --returning
  471.  
  472. local startPos = {}
  473.  
  474. local function setStart()
  475.     startPos = getPos()
  476.     Apath = {}
  477.     if pathList[getIndex(Apath)] == nil then
  478.         addToPathList(Apath)
  479.     end
  480. end
  481.  
  482. local function endRecording()
  483.     if getIndex(Apath) ~= nil then
  484.         deleteFromPathList(Apath)
  485.     end
  486. end
  487.  
  488.  
  489.  
  490.  local function normalReturn()
  491.     local backWay = make(getPos(), startPos)
  492.     follow(backWay)
  493.     setStart()
  494.  end
  495.  
  496.  
  497.  
  498. local function sameWayReturn()
  499.     follow(inverse(Apath))
  500.     setStart()
  501. end
  502.  
  503.  
  504.  
  505. move = { up = up, down = down, left = left, right = right, forward = forward, back = back, relativeTo = relativeTo }
  506. path = { getPos = getPos, follow = follow, inverse = inverse, make = make }
  507. pathsList = { add = addToPathList, index = getIndex, delete = deleteFromPathList }
  508. returning = {sameWay = sameWayReturn, setStart = setStart, endRecording = endRecording, normalWay = normalReturn, Z = returnZ, Y = returnY, X = returnX }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement