Advertisement
melzneni

quarry_defFunctions

Mar 19th, 2020 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 36.24 KB | None | 0 0
  1. local TYPE_FUNCTION = 1
  2. local TYPE_VALUE = 2
  3. local TYPE_VAR = 3
  4. local TYPE_USER_FUNCTION = 4
  5. local TYPE_DEFAULT_FUNCTION = 5
  6. local TYPE_ARR = 6
  7. local MODE_TEST = 1
  8. local MODE_MC = 2
  9.  
  10. local test = false
  11.  
  12. os.loadAPI("DFE")
  13.  
  14. local turtleAdapter = DFE.turtleAdapter
  15.  
  16. local storageData = { direction = 0 }
  17. DFE.setStorageData(storageData)
  18.  
  19. function printTable(tbl)
  20.     printTableIndex(tbl, "", {})
  21. end
  22.  
  23. function tableContainsValue(table, value)
  24.     for i, v in pairs(table) do
  25.         if v == value then
  26.             return true
  27.         end
  28.     end
  29.     return false
  30. end
  31.  
  32. function printTableIndex(tbl, before, alreadyUsed)
  33.     for i, v in pairs(tbl) do
  34.         if type(v) == "table" then
  35.             if tableContainsValue(alreadyUsed, v) then
  36.                 print(before .. i .. ":", "alreadyUsed");
  37.             else
  38.                 print(before .. i .. ": [");
  39.                 table.insert(alreadyUsed, v)
  40.                 printTableIndex(v, before .. "\t", alreadyUsed)
  41.                 print(before .. "]")
  42.             end
  43.         else
  44.             print(before .. i .. ":", v);
  45.         end
  46.     end
  47. end
  48.  
  49. local gpsAdapter = {
  50.     locate = function()
  51.         local x, y, z = gps.locate()
  52.         if x == nil then
  53.             if not test then
  54.                 shell.run("gps", "locate")
  55.             end
  56.             error("unable to execute gps.locate()")
  57.         end
  58.         return { x, y, z }
  59.     end
  60. }
  61.  
  62. local AREA_EMPTY = -1
  63. local AREA_SET = -2
  64.  
  65. function swapInArray(arr1, arr2, index)
  66.     local v = arr1[index]
  67.     arr1[index] = arr2[index]
  68.     arr2[index] = v;
  69. end
  70.  
  71. local fcts = {
  72.     navi_setSpace = function(args)
  73.         local space = {}
  74.         local pos = args[1]
  75.         local pos2 = args[2]
  76.         if pos[1] > pos2[1] then
  77.             swapInArray(pos, pos2, 1);
  78.         end
  79.         if pos[2] > pos2[2] then
  80.             swapInArray(pos, pos2, 2);
  81.         end
  82.         if pos[3] > pos2[3] then
  83.             swapInArray(pos, pos2, 3);
  84.         end
  85.         storageData.spaceOffset = args[1]
  86.         storageData.spaceEnd = args[2]
  87.         for x = 1, pos2[1] - pos[1] + 1 do
  88.             space[x] = {}
  89.             for y = 1, pos2[2] - pos[2] + 1 do
  90.                 space[x][y] = {}
  91.                 for z = 1, pos2[3] - pos[3] + 1 do
  92.                     space[x][y][z] = AREA_EMPTY
  93.                 end
  94.             end
  95.         end
  96.         storageData.space = space
  97.     end,
  98.     navi_setNullPos = function(args)
  99.         storageData.nullpos = gpsAdapter.locate()
  100.         local o
  101.         --for i = 0, 3 do
  102.         o = checkOrientation(storageData.nullpos)
  103.         if o < 0 then
  104.             o = o + 4
  105.         end
  106.         o = o % 4
  107.         --end
  108.         if o == nil then
  109.             error("fail")
  110.         end
  111.         storageData.nullOr = o
  112.         storageData.direction = 0
  113.         print("finished init: ", storageData.nullOr)
  114.     end,
  115.     navi_getPos = function(args)
  116.         local pos = gpsAdapter.locate()
  117.         return pos --{ pos[1] - nullPos[1] + 1, pos[2] - nullPos[2] + 1, pos[3] - nullPos[3] + 1 }
  118.     end
  119. }
  120.  
  121. function checkOrientation(currentPos)
  122.     for i = 1, 4 do
  123.         local o
  124.         if turtleAdapter.forward() then
  125.             local newPos = gpsAdapter.locate()
  126.             if newPos[1] > currentPos[1] then
  127.                 o = 0
  128.             elseif newPos[3] > currentPos[3] then
  129.                 o = 1
  130.             elseif newPos[1] < currentPos[1] then
  131.                 o = 2
  132.             elseif newPos[3] < currentPos[3] then
  133.                 o = 3
  134.             end
  135.             turtleAdapter.turnLeft()
  136.             turtleAdapter.turnLeft()
  137.             turtleAdapter.forward()
  138.             turtleAdapter.turnLeft()
  139.             turtleAdapter.turnLeft()
  140.         end
  141.         if o ~= nil then
  142.             for j = 1, i - 1 do
  143.                 turtleAdapter.turnLeft()
  144.             end
  145.             o = o - i + 1
  146.             if o < 0 then
  147.                 o = o + 4
  148.             end
  149.             return o
  150.         end
  151.         turtleAdapter.turnRight()
  152.     end
  153.     error("unable to init orientation, turtle is obstructed")
  154. end
  155.  
  156. function initToContinue()
  157.     if not test then
  158.         local o = checkOrientation(gpsAdapter.locate())
  159.         local o1 = (storageData.direction + storageData.nullOr) % 4
  160.         if o ~= o1 then
  161.             if o > o1 then
  162.                 o1 = o1 + 4
  163.             end
  164.             for i = 1, o1 - o do
  165.                 turtle.turnRight()
  166.             end
  167.             local o = checkOrientation(gpsAdapter.locate())
  168.             local o1 = (storageData.direction + storageData.nullOr) % 4
  169.             if o ~= o1 then
  170.                 error("fail: " .. o .. ", " .. o1)
  171.             end
  172.         end
  173.     end
  174. end
  175.  
  176. function printSpace(args)
  177.     for z = 1, #storageData.space[1][1] do
  178.         local txt = ""
  179.         for x = 1, #storageData.space do
  180.             txt = txt .. storageData.space[x][1][z] .. ", "
  181.         end
  182.         print(txt)
  183.     end
  184. end
  185.  
  186. function moveTo(pos, direction)
  187.     local done = false
  188.     local pos = pos
  189.     while not done do
  190.         local path = getPathTo(pos)
  191.         if path == nil then
  192.             error("unable to get to pos (" .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ")")
  193.         end
  194.         done = followPath(path)
  195.     end
  196.  
  197.     setOrientation(direction)
  198. end
  199.  
  200. local defaultFunctions = {
  201.     {
  202.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "activateSave",
  203.         fct = function(args)
  204.             storageData.save = args[1];
  205.         end
  206.     }, {
  207.         type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "navi_setSpace",
  208.         fct = fcts.navi_setSpace
  209.     }, {
  210.         type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "moveTo",
  211.         fct = function(args)
  212.             moveTo(args[1], args[2])
  213.         end
  214.     }, {
  215.         type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "navi_setNullPos",
  216.         fct = fcts.navi_setNullPos
  217.     }, {
  218.         type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "navi_getPos",
  219.         fct = fcts.navi_getPos
  220.     }, {
  221.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "print",
  222.         fct = function(args)
  223.             if type(args[1]) == "table" then
  224.                 printTable(args[1])
  225.             else
  226.                 print(args[1])
  227.             end
  228.         end
  229.     }, {
  230.         type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvFwd",
  231.         fct = function(args)
  232.             local par = tonumber(args[1])
  233.             if par == nil then
  234.                 par = 1
  235.             end
  236.             for i = 1, par do
  237.                 turtleAdapter.forward()
  238.             end
  239.         end
  240.     }, {
  241.         type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvBack",
  242.         fct = function(args)
  243.             local par = tonumber(args[1])
  244.             if par == nil then
  245.                 par = 1
  246.             end
  247.             turtleAdapter.turnLeft()
  248.             turtleAdapter.turnLeft()
  249.             for i = 1, par do
  250.                 turtleAdapter.forward()
  251.             end
  252.             turtleAdapter.turnLeft()
  253.             turtleAdapter.turnLeft()
  254.         end
  255.     }, { type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvUp",
  256.          fct = function(args)
  257.              local par = tonumber(args[1])
  258.              if par == nil then
  259.                  par = 1
  260.              end
  261.              for i = 1, par do
  262.                  turtleAdapter.up()
  263.              end
  264.          end
  265.     }, {
  266.         type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvDown",
  267.         fct = function(args)
  268.             local par = tonumber(args[1])
  269.             if par == nil then
  270.                 par = 1
  271.             end
  272.             for i = 1, par do
  273.                 turtleAdapter.down()
  274.             end
  275.         end
  276.     }, {
  277.         type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "left",
  278.         fct = function(args)
  279.             local par = tonumber(args[1])
  280.             if par == nil then
  281.                 par = 1
  282.             end
  283.             for i = 1, par do
  284.                 turtleAdapter.turnLeft()
  285.             end
  286.         end
  287.     }, {
  288.         type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "right",
  289.         fct = function(args)
  290.             local par = tonumber(args[1])
  291.             if par == nil then
  292.                 par = 1
  293.             end
  294.             for i = 1, par do
  295.                 turtleAdapter.turnRight()
  296.             end
  297.         end
  298.     }, {
  299.         type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "printSpace",
  300.         fct = printSpace
  301.     }, {
  302.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "getItemName",
  303.         fct = function(args)
  304.             return turtleAdapter.getItemName(args[1])
  305.         end
  306.     }, {
  307.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "getItemCount",
  308.         fct = function(args)
  309.             return turtleAdapter.getItemCount(args[1])
  310.         end
  311.     }, {
  312.         type = TYPE_DEFAULT_FUNCTION, pars = { 2, 3 }, name = "digAt",
  313.         fct = function(args)
  314.             local pos = args[2]
  315.             local dir = faceTowardsBlock(pos, true)
  316.             turtleAdapter.select(args[1])
  317.             if not test then
  318.                 while true do
  319.                     local name = turtleAdapter.getItemName(args[1])
  320.                     if name == nil or args[3] == nil or name == args[3] then
  321.                         break
  322.                     end
  323.                     turtleAdapter.dropUp()
  324.                 end
  325.             end
  326.             if dir < 4 then
  327.                 dig(args[1])
  328.             elseif dir == 4 then
  329.                 digUp(args[1])
  330.             elseif dir == 5 then
  331.                 digDown(args[1])
  332.             end
  333.         end
  334.     }, {
  335.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dig",
  336.         fct = function(args)
  337.             if not test then
  338.                 while true do
  339.                     local name = turtleAdapter.getItemName(args[1])
  340.                     if name == nil or args[2] == nil or name == args[2] then
  341.                         break
  342.                     end
  343.                     turtleAdapter.dropUp()
  344.                 end
  345.             end
  346.             turtleAdapter.dig()
  347.         end
  348.     }, {
  349.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "digUp",
  350.         fct = function(args)
  351.             turtleAdapter.select(args[1])
  352.             if not test then
  353.                 while true do
  354.                     local name = turtleAdapter.getItemName(args[1])
  355.                     if name == nil or args[2] == nil or name == args[2] then
  356.                         break
  357.                     end
  358.                     turtleAdapter.dropUp()
  359.                 end
  360.             end
  361.             turtleAdapter.digUp()
  362.         end
  363.     }, {
  364.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "digDown",
  365.         fct = function(args)
  366.             turtleAdapter.select(args[1])
  367.             if not test then
  368.                 while true do
  369.                     local name = turtleAdapter.getItemName(args[1])
  370.                     if name == nil or args[2] == nil or name == args[2] then
  371.                         break
  372.                     end
  373.                     turtleAdapter.dropUp()
  374.                 end
  375.             end
  376.             turtleAdapter.digDown()
  377.         end
  378.     }, {
  379.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "place",
  380.         fct = function(args)
  381.             turtleAdapter.select(args[1])
  382.             turtleAdapter.place()
  383.         end
  384.     }, {
  385.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "placeUp",
  386.         fct = function(args)
  387.             turtleAdapter.select(args[1])
  388.             turtleAdapter.placeUp()
  389.         end
  390.     }, {
  391.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "placeDown",
  392.         fct = function(args)
  393.             turtleAdapter.select(args[1])
  394.             turtleAdapter.placeDown()
  395.         end
  396.     }, {
  397.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 3 }, name = "placeAt",
  398.         fct = function(args)
  399.             if args[3] ~= nil then
  400.                 local pos = args[2]
  401.                 local direction = args[3]
  402.                 if direction < 4 then
  403.                     if direction == 0 then
  404.                         moveTo({ pos[1] - 1, pos[2], pos[3] }, direction)
  405.                     elseif direction == 1 then
  406.                         moveTo({ pos[1], pos[2], pos[3] - 1 }, direction)
  407.                     elseif direction == 2 then
  408.                         moveTo({ pos[1] + 1, pos[2], pos[3] }, direction)
  409.                     elseif direction == 3 then
  410.                         moveTo({ pos[1], pos[2], pos[3] + 1 }, direction)
  411.                     else
  412.                         error("fail")
  413.                     end
  414.                     place(args[1])
  415.                 elseif direction == 4 then
  416.                     moveTo({ pos[1], pos[2] - 1, pos[3] }, 0)
  417.                     placeUp(args[1])
  418.                 elseif direction == 5 then
  419.                     moveTo({ pos[1], pos[2] + 1, pos[3] }, 0)
  420.                     placeDown(args[1])
  421.                 end
  422.             elseif args[2] ~= nil then
  423.                 local pos = args[2]
  424.                 local dir = faceTowardsBlock(pos, true)
  425.                 if dir < 4 then
  426.                     place(args[1])
  427.                 elseif dir == 4 then
  428.                     placeUp(args[1])
  429.                 elseif dir == 5 then
  430.                     placeDown(args[1])
  431.                 end
  432.             else
  433.                 place(args[1])
  434.             end
  435.  
  436.         end
  437.     }, {
  438.         type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "peripheral",
  439.         fct = function(args)
  440.             peripheral.call(args[1], args[2])
  441.         end
  442.     }, {
  443.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "setTestOutput",
  444.         fct = function(args)
  445.             if type(args[1]) ~= "boolean" then
  446.                 error("arg of function 'setstorageData.testOutput' needs to be of type 'boolean'")
  447.             end
  448.             storageData.testOutput = args[1]
  449.         end
  450.     }, {
  451.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "drop",
  452.         fct = function(args)
  453.             turtleAdapter.select(args[1])
  454.             if args[2] ~= nil then
  455.                 turtleAdapter.drop(args[2])
  456.             else
  457.                 turtleAdapter.drop(1)
  458.             end
  459.         end
  460.     }, {
  461.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dropUp",
  462.         fct = function(args)
  463.             turtleAdapter.select(args[1])
  464.             if args[2] ~= nil then
  465.                 turtleAdapter.dropUp(args[2])
  466.             else
  467.                 turtleAdapter.dropUp(1)
  468.             end
  469.         end
  470.     }, {
  471.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dropDown",
  472.         fct = function(args)
  473.             turtleAdapter.select(args[1])
  474.             if args[2] ~= nil then
  475.                 turtleAdapter.dropDown(args[2])
  476.             else
  477.                 turtleAdapter.dropDown(1)
  478.             end
  479.         end
  480.     }, {
  481.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suck",
  482.         fct = function(args)
  483.             turtleAdapter.select(args[1])
  484.             if args[2] ~= nil then
  485.                 turtleAdapter.suck(args[2])
  486.             else
  487.                 turtleAdapter.suck(1)
  488.             end
  489.         end
  490.     }, {
  491.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suckUp",
  492.         fct = function(args)
  493.             turtleAdapter.select(args[1])
  494.             if args[2] ~= nil then
  495.                 turtleAdapter.suckUp(args[2])
  496.             else
  497.                 turtleAdapter.suckUp(1)
  498.             end
  499.         end
  500.     }, {
  501.         type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suckDown",
  502.         fct = function(args)
  503.             turtleAdapter.select(args[1])
  504.             if args[2] ~= nil then
  505.                 turtleAdapter.suckDown(args[2])
  506.             else
  507.                 turtleAdapter.suckDown(1)
  508.             end
  509.         end
  510.     }, {
  511.         type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "redstone",
  512.         fct = function(args)
  513.             redstone.setOutput(args[1], args[2])
  514.         end
  515.     }, {
  516.         type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "refuel",
  517.         fct = function(args)
  518.             for i = 1, 16 do
  519.                 turtleAdapter.select(i)
  520.                 turtleAdapter.refuel()
  521.             end
  522.         end
  523.     }, {
  524.         type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "getFuelLevel",
  525.         fct = function(args)
  526.             return turtleAdapter.getFuelLevel()
  527.         end
  528.     }, {
  529.         type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "sleep",
  530.         fct = function(args)
  531.             if args[1] > 2 then
  532.                 if storageData.toSleep ~= nil then
  533.                     args[1] = storageData.toSleep - (storageData.currTime - storageData.sleepStart)
  534.                 end
  535.                 storageData.sleepStart = os.clock()
  536.                 storageData.currTime = os.clock()
  537.                 storageData.toSleep = args[1]
  538.                 local printLast = 0
  539.                 local count = 0
  540.                 while storageData.currTime - storageData.sleepStart < args[1] do
  541.                     sleep(0.1)
  542.                     storageData.currTime = os.clock()
  543.                     if storageData.currTime - storageData.sleepStart > printLast + 1 then
  544.                         printLast = printLast + 1
  545.                         if count ~= 0 then
  546.                             local x, y = term.getCursorPos()
  547.                             term.setCursorPos(1, y - 1)
  548.                         else
  549.                             count = 1
  550.                         end
  551.                         print("slept: ", printLast, "/", storageData.toSleep, "s")
  552.                         save()
  553.                     end
  554.                 end
  555.                 storageData.toSleep = nil
  556.             else
  557.                 sleep(args[1])
  558.             end
  559.         end
  560.     }
  561. }
  562.  
  563. function faceTowardsBlock(pos, upOrDownAllowed)
  564.     local done = false
  565.  
  566.     local minInd
  567.     while not done do
  568.         local path0 = { getPathTo({ pos[1] - 1, pos[2], pos[3] }), 0 }
  569.         local path1 = { getPathTo({ pos[1], pos[2], pos[3] - 1 }), 1 }
  570.         local path2 = { getPathTo({ pos[1] + 1, pos[2], pos[3] }), 2 }
  571.         local path3 = { getPathTo({ pos[1], pos[2], pos[3] + 1 }), 3 }
  572.         local path4 = {}
  573.         local path5 = {}
  574.         if upOrDownAllowed then
  575.             path4[1] = getPathTo({ pos[1], pos[2] + 1, pos[3] })
  576.             path4[2] = 5
  577.             path5[1] = getPathTo({ pos[1], pos[2] - 1, pos[3] })
  578.             path5[2] = 4
  579.         end
  580.  
  581.         local min
  582.         local minPath
  583.         for i, storageData in ipairs({ path0, path1, path2, path3, path4, path5 }) do
  584.             if storageData[1] ~= nil and (min == nil or #storageData[1] < min) then
  585.                 min = #storageData[1]
  586.                 minPath = storageData[1]
  587.                 minInd = storageData[2]
  588.             end
  589.         end
  590.         if min == nil then
  591.             error("unable to get to pos (" .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ")")
  592.         end
  593.  
  594.         done = followPath(minPath)
  595.         setOrientation(minInd % 4)
  596.     end
  597.     return minInd
  598. end
  599.  
  600. function digUp(id)
  601.     local pos = translateToSpacePos(fcts.navi_getPos())
  602.     storageData.space[pos[1]][pos[2] + 1][pos[3]] = AREA_EMPTY
  603.     turtleAdapter.select(id)
  604.     turtleAdapter.digUp()
  605. end
  606.  
  607. function digDown(id)
  608.     local pos = translateToSpacePos(fcts.navi_getPos())
  609.     storageData.space[pos[1]][pos[2] - 1][pos[3]] = AREA_EMPTY
  610.     turtleAdapter.select(id)
  611.     turtleAdapter.digDown()
  612. end
  613.  
  614. function dig(id)
  615.     local pos = translateToSpacePos(fcts.navi_getPos())
  616.     if storageData.direction == 0 then
  617.         storageData.space[pos[1] + 1][pos[2]][pos[3]] = AREA_EMPTY
  618.     elseif storageData.direction == 1 then
  619.         storageData.space[pos[1]][pos[2]][pos[3] + 1] = AREA_EMPTY
  620.     elseif storageData.direction == 2 then
  621.         storageData.space[pos[1] - 1][pos[2]][pos[3]] = AREA_EMPTY
  622.     elseif storageData.direction == 3 then
  623.         storageData.space[pos[1]][pos[2]][pos[3] - 1] = AREA_EMPTY
  624.     end
  625.     turtleAdapter.select(id)
  626.     turtleAdapter.dig()
  627. end
  628.  
  629. function placeUp(id)
  630.     local pos = translateToSpacePos(fcts.navi_getPos())
  631.     storageData.space[pos[1]][pos[2] + 1][pos[3]] = AREA_SET
  632.     turtleAdapter.select(id)
  633.     while not turtleAdapter.placeUp() do
  634.         if turtleAdapter.detectUp() then
  635.             turtleAdapter.digUp()
  636.             turtleAdapter.attackUp()
  637.         end
  638.     end
  639. end
  640.  
  641. function placeDown(id)
  642.     local pos = translateToSpacePos(fcts.navi_getPos())
  643.     storageData.space[pos[1]][pos[2] - 1][pos[3]] = AREA_SET
  644.     turtleAdapter.select(id)
  645.     while not turtleAdapter.placeDown() do
  646.         if turtleAdapter.detectDown() then
  647.             turtleAdapter.digDown()
  648.             turtleAdapter.attackDown()
  649.         end
  650.     end
  651. end
  652.  
  653. function place(id)
  654.     local pos = translateToSpacePos(fcts.navi_getPos())
  655.     if storageData.direction == 0 then
  656.         storageData.space[pos[1] + 1][pos[2]][pos[3]] = AREA_SET
  657.     elseif storageData.direction == 1 then
  658.         storageData.space[pos[1]][pos[2]][pos[3] + 1] = AREA_SET
  659.     elseif storageData.direction == 2 then
  660.         storageData.space[pos[1] - 1][pos[2]][pos[3]] = AREA_SET
  661.     elseif storageData.direction == 3 then
  662.         storageData.space[pos[1]][pos[2]][pos[3] - 1] = AREA_SET
  663.     end
  664.     turtleAdapter.select(id)
  665.     while not turtleAdapter.place() do
  666.         if turtleAdapter.detect() then
  667.             turtleAdapter.dig()
  668.             turtleAdapter.attack()
  669.         end
  670.     end
  671. end
  672.  
  673. function followPath(path)
  674.     local currPos = translateToSpacePos(fcts.navi_getPos())
  675.     for i, pos in ipairs(path) do
  676.         if pos[1] > currPos[1] then
  677.             setOrientation(0)
  678.             while not turtleAdapter.forward() do
  679.                 if turtleAdapter.detect() then
  680.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  681.                     return false
  682.                 else
  683.                     turtleAdapter.attack()
  684.                 end
  685.             end
  686.         elseif pos[1] < currPos[1] then
  687.             setOrientation(2)
  688.             while not turtleAdapter.forward() do
  689.                 if turtleAdapter.detect() then
  690.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  691.                     return false
  692.                 else
  693.                     turtleAdapter.attack()
  694.                 end
  695.             end
  696.         elseif pos[2] > currPos[2] then
  697.             while not turtleAdapter.up() do
  698.                 if turtleAdapter.detectUp() then
  699.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  700.                     return false
  701.                 else
  702.                     turtleAdapter.attackUp()
  703.                 end
  704.             end
  705.         elseif pos[2] < currPos[2] then
  706.             while not turtleAdapter.down() do
  707.                 if turtleAdapter.detectDown() then
  708.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  709.                     return false
  710.                 else
  711.                     turtleAdapter.attackDown()
  712.                 end
  713.             end
  714.         elseif pos[3] > currPos[3] then
  715.             setOrientation(1)
  716.             while not turtleAdapter.forward() do
  717.                 if turtleAdapter.detect() then
  718.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  719.                     return false
  720.                 else
  721.                     turtleAdapter.attack()
  722.                 end
  723.             end
  724.         elseif pos[3] < currPos[3] then
  725.             setOrientation(3)
  726.             while not turtleAdapter.forward() do
  727.                 if turtleAdapter.detect() then
  728.                     storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
  729.                     return false
  730.                 else
  731.                     turtleAdapter.attack()
  732.                 end
  733.             end
  734.         end
  735.         currPos = pos
  736.     end
  737.  
  738.     return true
  739.     --[[printTable(translateToSpacePos(fcts.navi_getPos()))
  740.     printTable(currPos)]]
  741. end
  742.  
  743. function setOrientation(orientation)
  744.     if orientation ~= storageData.direction then
  745.         if orientation > storageData.direction then
  746.             if orientation - storageData.direction == 3 then
  747.                 turtleAdapter.turnLeft()
  748.             else
  749.                 for i = storageData.direction + 1, orientation do
  750.                     turtleAdapter.turnRight()
  751.                 end
  752.             end
  753.         else
  754.             if orientation - storageData.direction == -3 then
  755.                 turtleAdapter.turnRight()
  756.             else
  757.                 for i = orientation + 1, storageData.direction do
  758.                     turtleAdapter.turnLeft()
  759.                 end
  760.             end
  761.         end
  762.     end
  763. end
  764.  
  765. function translateToSpacePos(pos, spaceCoordinates)
  766.     local nullPos = storageData.nullpos
  767.     if spaceCoordinates == nil then
  768.         pos = { pos[1] - nullPos[1], pos[2] - nullPos[2], pos[3] - nullPos[3] }
  769.         local x = pos[1]
  770.         local z = pos[3]
  771.         if storageData.nullOr == 1 then
  772.             pos[1] = z
  773.             pos[3] = -x
  774.         elseif storageData.nullOr == 2 then
  775.             pos[1] = -x
  776.             pos[3] = -z
  777.         elseif storageData.nullOr == 3 then
  778.             pos[1] = -z
  779.             pos[3] = x
  780.         end
  781.         pos = { pos[1] + 1, pos[2] + 1, pos[3] + 1 }
  782.     end
  783.     pos = {
  784.         pos[1] - storageData.spaceOffset[1] + 1,
  785.         pos[2] - storageData.spaceOffset[2] + 1,
  786.         pos[3] - storageData.spaceOffset[3] + 1
  787.     }
  788.     return pos
  789. end
  790.  
  791. function getPathTo(posSource)
  792.     local currPosSource = fcts.navi_getPos()
  793.     local pos = translateToSpacePos(posSource, true)
  794.     local currPos = translateToSpacePos(currPosSource)
  795.     local space = {}
  796.     for x = 1, #storageData.space do
  797.         space[x] = {}
  798.         for y = 1, #storageData.space[1] do
  799.             space[x][y] = {}
  800.             for z = 1, #storageData.space[1][1] do
  801.                 space[x][y][z] = storageData.space[x][y][z]
  802.             end
  803.         end
  804.     end
  805.     if pos[1] > #space or pos[2] > #space[1] or pos[3] > #space[1][1]
  806.             or pos[1] < 1 or pos[2] < 1 or pos[3] < 1 then
  807.         return nil
  808.     end
  809.     if space[pos[1]][pos[2]][pos[3]] == AREA_SET then
  810.         return nil
  811.     end
  812.     space[pos[1]][pos[2]][pos[3]] = 0
  813.     local done = false
  814.     while not done do
  815.         done = true
  816.         for x = 1, #space do
  817.             for y = 1, #space[1] do
  818.                 for z = 1, #space[1][1] do
  819.                     local vSelf = space[x][y][z]
  820.                     if vSelf ~= AREA_SET then
  821.                         --x
  822.                         if x > 1 and space[x - 1][y][z] >= 0 and (vSelf == AREA_EMPTY or space[x - 1][y][z] + 1 < vSelf) then
  823.                             space[x][y][z] = space[x - 1][y][z] + 1
  824.                             vSelf = space[x][y][z]
  825.                             done = false
  826.                         end
  827.                         if x < #space and space[x + 1][y][z] >= 0 and (vSelf == AREA_EMPTY or space[x + 1][y][z] + 1 < vSelf) then
  828.                             space[x][y][z] = space[x + 1][y][z] + 1
  829.                             vSelf = space[x][y][z]
  830.                             done = false
  831.                         end
  832.                         --z
  833.                         if z > 1 and space[x][y][z - 1] >= 0 and (vSelf == AREA_EMPTY or space[x][y][z - 1] + 1 < vSelf) then
  834.                             space[x][y][z] = space[x][y][z - 1] + 1
  835.                             vSelf = space[x][y][z]
  836.                             done = false
  837.                         end
  838.                         if z < #space[1][1] and space[x][y][z + 1] >= 0 and (vSelf == AREA_EMPTY or space[x][y][z + 1] + 1 < vSelf) then
  839.                             space[x][y][z] = space[x][y][z + 1] + 1
  840.                             vSelf = space[x][y][z]
  841.                             done = false
  842.                         end
  843.                         --y
  844.                         if y > 1 and space[x][y - 1][z] >= 0 and (vSelf == AREA_EMPTY or space[x][y - 1][z] + 1 < vSelf) then
  845.                             space[x][y][z] = space[x][y - 1][z] + 1
  846.                             vSelf = space[x][y][z]
  847.                             done = false
  848.                         end
  849.                         if y < #space[1] and space[x][y + 1][z] >= 0 and (vSelf == AREA_EMPTY or space[x][y + 1][z] + 1 < vSelf) then
  850.                             space[x][y][z] = space[x][y + 1][z] + 1
  851.                             vSelf = space[x][y][z]
  852.                             done = false
  853.                         end
  854.                     end
  855.                 end
  856.             end
  857.         end
  858.     end
  859.  
  860.     local path = {}
  861.     local x = currPos[1]
  862.     local y = currPos[2]
  863.     local z = currPos[3]
  864.     local num = space[x][y][z]
  865.     if num == nil then
  866.         error(x .. ", " .. y .. ", " .. z)
  867.     end
  868.     if num < 0 then
  869.         return nil
  870.     end
  871.     while num > 0 do
  872.         if x > 1 and space[x - 1][y][z] == num - 1 then
  873.             x = x - 1
  874.             num = num - 1
  875.         elseif x < #space and space[x + 1][y][z] == num - 1 then
  876.             x = x + 1
  877.             num = num - 1
  878.         elseif z > 1 and space[x][y][z - 1] == num - 1 then
  879.             z = z - 1
  880.             num = num - 1
  881.         elseif z < #space[1][1] and space[x][y][z + 1] == num - 1 then
  882.             z = z + 1
  883.             num = num - 1
  884.         elseif y > 1 and space[x][y - 1][z] == num - 1 then
  885.             y = y - 1
  886.             num = num - 1
  887.         elseif y < #space[1] and space[x][y + 1][z] == num - 1 then
  888.             y = y + 1
  889.             num = num - 1
  890.         else
  891.             error("fail " .. x .. "," .. y .. "," .. z .. ": " .. num)
  892.         end
  893.         table.insert(path, { x, y, z })
  894.     end
  895.     return path
  896. end
  897.  
  898. function getDefaultFunctions()
  899.     return defaultFunctions
  900. end
  901.  
  902. function setTest(value, tbl)
  903.     for i, v in pairs(tbl) do
  904.         _G[i] = v
  905.     end
  906.     test = value
  907.     if test then
  908.         _G.sleep = function(n)
  909.             -- seconds
  910.             local t0 = os.clock()
  911.             while os.clock() - t0 <= n do
  912.             end
  913.         end
  914.         _G.turtle = {
  915.             forward = function()
  916.                 if storageData.testOutput then
  917.                     print("fct: turtle.forward()")
  918.                 end
  919.                 return true
  920.             end,
  921.             up = function()
  922.                 if storageData.testOutput then
  923.                     print("fct: turtle.up()")
  924.                 end
  925.                 return true
  926.             end,
  927.             down = function()
  928.                 if storageData.testOutput then
  929.                     print("fct: turtle.down()")
  930.                 end
  931.                 return true
  932.             end,
  933.             turnLeft = function()
  934.                 if storageData.testOutput then
  935.                     print("fct: turtle.turnLeft()")
  936.                 end
  937.                 return true
  938.             end,
  939.             turnRight = function()
  940.                 if storageData.testOutput then
  941.                     print("fct: turtle.turnRight()")
  942.                 end
  943.                 return true
  944.             end,
  945.             select = function(id)
  946.                 if storageData.testOutput then
  947.                     print("fct: turtle.select(" .. id .. ")")
  948.                 end
  949.                 return true
  950.             end,
  951.             place = function()
  952.                 if storageData.testOutput then
  953.                     print("fct: turtle.place()")
  954.                 end
  955.                 return true
  956.             end,
  957.             placeUp = function()
  958.                 if storageData.testOutput then
  959.                     print("fct: turtle.placeUp()")
  960.                 end
  961.                 return true
  962.             end,
  963.             placeDown = function()
  964.                 if storageData.testOutput then
  965.                     print("fct: turtle.placeDown()")
  966.                 end
  967.                 return true
  968.             end,
  969.             dig = function()
  970.                 if storageData.testOutput then
  971.                     print("fct: turtle.dig()")
  972.                 end
  973.                 return true
  974.             end,
  975.             digUp = function()
  976.                 if storageData.testOutput then
  977.                     print("fct: turtle.digUp()")
  978.                 end
  979.                 return true
  980.             end,
  981.             digDown = function()
  982.                 if storageData.testOutput then
  983.                     print("fct: turtle.digDown()")
  984.                 end
  985.                 return true
  986.             end,
  987.             attack = function()
  988.                 if storageData.testOutput then
  989.                     print("fct: turtle.attack()")
  990.                 end
  991.                 return true
  992.             end,
  993.             attackUp = function()
  994.                 if storageData.testOutput then
  995.                     print("fct: turtle.attackUp()")
  996.                 end
  997.                 return true
  998.             end,
  999.             attackDown = function()
  1000.                 if storageData.testOutput then
  1001.                     print("fct: turtle.attackDown()")
  1002.                 end
  1003.                 return true
  1004.             end,
  1005.             detect = function()
  1006.                 if storageData.space ~= nil then
  1007.                     local pos = storageData.pos
  1008.                     local o = storageData.direction
  1009.                     if o == 0 then
  1010.                         if #storageData.space > pos[1] then
  1011.                             return #storageData.space[pos[1] + 1][pos[2]][pos[3]] == AREA_SET
  1012.                         end
  1013.                         return false
  1014.                     elseif o == 1 then
  1015.                         if #storageData.space[1][1] > pos[3] then
  1016.                             return #storageData.space[pos[1]][pos[2]][pos[3] + 1] == AREA_SET
  1017.                         end
  1018.                         return false
  1019.                     elseif o == 2 then
  1020.                         if #storageData.space < pos[1] then
  1021.                             return #storageData.space[pos[1] - 1][pos[2]][pos[3]] == AREA_SET
  1022.                         end
  1023.                         return false
  1024.                     elseif o == 3 then
  1025.                         if #storageData.space[1][1] < pos[3] then
  1026.                             return #storageData.space[pos[1]][pos[2]][pos[3] - 1] == AREA_SET
  1027.                         end
  1028.                         return false
  1029.                     end
  1030.                     error("fail:" .. o)
  1031.                 else
  1032.                     return false;
  1033.                 end
  1034.             end,
  1035.             detectUp = function()
  1036.                 if storageData.space ~= nil then
  1037.                     local pos = storageData.pos
  1038.                     if #storageData.space[1] > pos[2] then
  1039.                         return #storageData.space[pos[1]][pos[2] + 1][pos[3]] == AREA_SET
  1040.                     end
  1041.                     return false
  1042.                 else
  1043.                     return false;
  1044.                 end
  1045.             end,
  1046.             detectDown = function()
  1047.                 if storageData.space ~= nil then
  1048.                     local pos = storageData.pos
  1049.                     if #storageData.space[1] < pos[2] then
  1050.                         return #storageData.space[pos[1]][pos[2] - 1][pos[3]] == AREA_SET
  1051.                     end
  1052.                     return false
  1053.                 else
  1054.                     return false;
  1055.                 end
  1056.             end,
  1057.             getItemCount = function(id)
  1058.                 return 0
  1059.             end
  1060.         }
  1061.         _G.gps = {
  1062.             locate = function()
  1063.                 return storageData.pos[1], storageData.pos[2], storageData.pos[3]
  1064.             end
  1065.         }
  1066.         _G.peripheral = {
  1067.             call = function(arg1, arg2)
  1068.                 if storageData.testOutput then
  1069.                     print("fct: peripheral.call(" .. arg1 .. ", " .. arg2 .. ")")
  1070.                 end
  1071.             end
  1072.         }
  1073.     end
  1074. end
  1075.  
  1076. function setHu(arg)
  1077. end
  1078.  
  1079. function setStorageData(data, saveFunction)
  1080.     _G.save = saveFunction
  1081.     storageData = data
  1082.     DFE.setStorageData(storageData)
  1083.     if test and storageData.pos == nil then
  1084.         storageData.pos = { 123, 60, 546 }
  1085.     end
  1086.     if storageData.direction == nil then
  1087.         storageData.direction = 0
  1088.     end
  1089.     if storageData.nullOr == nil then
  1090.         storageData.nullOr = 0
  1091.     end
  1092.     if storageData.testOutput == nil then
  1093.         storageData.testOutput = true
  1094.     end
  1095. end
  1096.  
  1097. return { setTest, setStorageData, defaultFunctions, initToContinue }
  1098.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement