Advertisement
guitarplayer616

GPS Api reborn

Dec 21st, 2016
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.80 KB | None | 0 0
  1. local tArgs = {...}
  2. if not fs.exists("refill") then
  3.     shell.run("pastebin get biP6fFEi refill")
  4. end
  5.  
  6. shell.run("refill")
  7.  
  8. function recordStep(n)
  9.     local h = fs.open("nObjective",'w')
  10.     h.writeLine("nObjective = "..tostring(n)..";")
  11.     h.close()
  12. end
  13.  
  14. function recordPos(heightPos,widthPos,lengthPos,face)
  15.     local h = fs.open("position","w")
  16.     h.writeLine("heightPos = "..tostring(heightPos)..";")
  17.     h.writeLine("widthPos = "..tostring(widthPos)..";")
  18.     h.writeLine("lengthPos = "..tostring(lengthPos)..";")
  19.     h.writeLine("face = ".."\""..tostring(face).."\""..";")
  20.     h.close()
  21. end
  22.  
  23. function updateVar(file,variable,newValue)
  24.     local exists
  25.     local withinTable = false
  26.     if not fs.exists(file) then
  27.         error("file does not exist")
  28.     end
  29.    
  30.     if variable:find("%.") then
  31.         variable = variable:gsub("(.*%.)","")
  32.         withinTable = true
  33.         print(variable)
  34.     end
  35.     --if variable
  36.    
  37.     variable = tostring(variable)
  38.     newValue = textutils.serialize(newValue)
  39.     --print(variable)
  40.    
  41.     local string = fs.open(file,"r").readAll()
  42.     local pattern,replace
  43.     if not withinTable then
  44.         pattern = variable.." =(.-;)"
  45.         replace = variable.." = "..newValue..";"
  46.     else
  47.         pattern = variable.." =(.-,)"
  48.         replace = variable.." = "..newValue..","
  49.         print("pattern: ",pattern)
  50.         print("replace: ",replace)
  51.     end
  52.     --print(string:find(pattern))
  53.  
  54.     exists = string:find(pattern)
  55.  
  56.     if exists then
  57.         local h = fs.open(file,"w")
  58.         string = string:gsub(pattern,replace)
  59.         h.write(string)
  60.         h.close()
  61.     --else
  62.         --local h = fs.open(file,"a")
  63.         --h.writeLine(replace)
  64.         --h.close()
  65.     end
  66. end
  67.  
  68.  
  69. function reinitialize()
  70.     heightPos = 0
  71.     widthPos = 0
  72.     lengthPos = 0
  73.     face = "south"
  74.     recordPos(heightPos,widthPos,lengthPos,face)
  75. end
  76.  
  77. if tonumber(tArgs[1]) then
  78.     heightPos = tonumber(tArgs[1])
  79.     widthPos = tonumber(tArgs[2])
  80.     lengthPos = tonumber(tArgs[3])
  81.     face = tArgs[4]
  82.     recordPos(heightPos,widthPos,lengthPos,face)
  83. end
  84.  
  85. if tArgs[1] == "reset" then
  86.     reinitialize()
  87. end
  88.  
  89. if fs.exists("position") then
  90.     shell.run("position")
  91. else
  92.     reinitialize()
  93. end
  94.  
  95. --Navigation function--
  96.  
  97.  
  98. local function matchHeight(heightGoal)
  99.     if heightGoal > heightPos then
  100.         while heightGoal > heightPos do
  101.             up()
  102.         end
  103.     elseif heightGoal < heightPos then
  104.         while heightGoal < heightPos do
  105.             down()
  106.         end
  107.     end
  108. end
  109.  
  110. local function matchWidth(widthGoal)
  111.     if widthGoal < widthPos then
  112.         turn("west")
  113.         while widthGoal < widthPos do
  114.             forward()
  115.         end
  116.     elseif widthGoal > widthPos then
  117.         turn("east")
  118.         while widthGoal > widthPos do
  119.             forward()
  120.         end
  121.     end
  122. end
  123.  
  124. local function matchLength(lengthGoal)
  125.     if lengthGoal > lengthPos then
  126.         turn("south")
  127.         while lengthGoal > lengthPos do
  128.             forward()
  129.         end
  130.     elseif lengthGoal < lengthPos then
  131.         turn("north")
  132.         while lengthGoal < lengthPos do
  133.             forward()
  134.         end
  135.     end
  136. end
  137.  
  138. function goto2(heightGoal,widthGoal,lengthGoal)
  139.     --shell.run("position")
  140.     if turtle.getFuelLevel() < 200 then
  141.         assert(refill)
  142.         refill()
  143.     end
  144.    
  145.     assert(face)
  146.     matchHeight(heightGoal)
  147.     if face == "south" or face == "north" then 
  148.         if face == "south" and lengthGoal > lengthPos then
  149.             matchLength(lengthGoal)
  150.             matchWidth(widthGoal)
  151.         elseif face == "north" and lengthGoal < lengthPos then
  152.             matchLength(lengthGoal)
  153.             matchWidth(widthGoal)
  154.         else
  155.             matchWidth(widthGoal)
  156.             matchLength(lengthGoal)
  157.         end
  158.     else
  159.         if face == "west" and widthGoal < widthPos then
  160.             matchWidth(widthGoal)
  161.             matchLength(lengthGoal)
  162.         elseif face == "east" and widthGoal > widthPos then
  163.             matchWidth(widthGoal)
  164.             matchLength(lengthGoal)
  165.         else
  166.             matchLength(lengthGoal)
  167.             matchWidth(widthGoal)
  168.         end
  169.     end
  170. end
  171.  
  172. function goto(heightGoal,widthGoal,lengthGoal)
  173.     --shell.run("position")
  174.     assert(face)
  175.     matchHeight(heightGoal)
  176.     if face == "south" then
  177.         if lengthGoal > lengthPos then
  178.             while lengthGoal > lengthPos do
  179.                 forward()
  180.             end
  181.         elseif lengthGoal < lengthPos then
  182.             while lengthGoal < lengthPos do
  183.                 backward()
  184.             end
  185.         end
  186.         matchWidth(widthGoal)
  187.     elseif face == "north" then
  188.         if lengthGoal < lengthPos then
  189.             while lengthGoal < lengthPos do
  190.                 forward()
  191.             end
  192.         elseif lengthGoal > lengthPos then
  193.             while lengthGoal > lengthPos do
  194.                 backward()
  195.             end
  196.         end
  197.         matchWidth(widthGoal)
  198.     elseif face == "west" then
  199.         if widthGoal < widthPos then
  200.             while widthGoal < widthPos do
  201.                 forward()
  202.             end
  203.         elseif widthGoal > widthPos then
  204.             while widthGoal > widthPos do
  205.                 backward()
  206.             end
  207.         end
  208.         matchLength(lengthGoal)
  209.     elseif face == "east" then
  210.         if widthGoal > widthPos then
  211.             while widthGoal > widthPos do
  212.                 forward()
  213.             end
  214.         elseif widthGoal < widthPos then
  215.             while widthGoal < widthPos do
  216.                 backward()
  217.             end
  218.         end
  219.         matchLength(lengthGoal)
  220.     end
  221. end
  222.  
  223.  
  224. function update(dir)
  225.         if dir == "forward" then
  226.             if face == "north" then
  227.                 lengthPos = lengthPos - 1
  228.             elseif face == "south" then
  229.                 lengthPos = lengthPos + 1
  230.             elseif face == "east" then
  231.                 widthPos = widthPos + 1
  232.             elseif face == "west" then
  233.                 widthPos = widthPos - 1
  234.             end
  235.         elseif dir == "backward" then
  236.                 if face == "north" then
  237.                         lengthPos = lengthPos + 1
  238.                 elseif face == "south" then
  239.                         lengthPos = lengthPos - 1
  240.                 elseif face == "east" then
  241.                         widthPos = widthPos - 1
  242.                 elseif face == "west" then
  243.                         widthPos = widthPos + 1
  244.                 end
  245.         elseif dir == "up" then
  246.                 heightPos = heightPos + 1
  247.         elseif dir == "down" then
  248.                 heightPos = heightPos - 1
  249.         elseif dir == "right" then
  250.                 if face == "north" then
  251.                         face = "east"
  252.                 elseif face == "east" then
  253.                         face = "south"
  254.                 elseif face == "south" then
  255.                         face = "west"
  256.                 elseif face == "west" then
  257.                         face = "north"
  258.                 end
  259.         elseif dir == "left" then
  260.                 if face == "north" then
  261.                         face = "west"
  262.                 elseif face == "west" then
  263.                         face = "south"
  264.                 elseif face == "south" then
  265.                         face = "east"
  266.                 elseif face == "east" then
  267.                         face = "north"
  268.                 end
  269.         end
  270.         recordPos(heightPos,widthPos,lengthPos,face)
  271. end
  272.  
  273. --Movement functions--
  274.  
  275. function courtesyOLD()
  276.     --if not turtle then
  277.  
  278.     if turtle.up() then
  279.         sleep(3)
  280.         while not turtle.down() do
  281.             sleep(1)
  282.         end
  283.         return
  284.     elseif turtle.down() then
  285.         sleep(3)
  286.         while not turtle.up() do
  287.             sleep(1)
  288.         end
  289.         return
  290.     elseif turtle.forward() then
  291.         sleep(3)
  292.         while not turtle.back() do
  293.             sleep(1)
  294.         end
  295.         return
  296.     elseif turtle.back() then
  297.         sleep(3)
  298.         while not turtle.forward() do
  299.             sleep(1)
  300.         end
  301.         return
  302.     end
  303. end
  304.  
  305. function courtesy()
  306.     local choice = math.random(1,6)
  307.     if choice == 1 then
  308.         if turtle.up() then
  309.             update("up")
  310.             sleep(3)
  311.             while not turtle.down() do
  312.                 sleep(1)
  313.             end
  314.             update("down")
  315.         end
  316.         return
  317.     elseif choice == 2 then
  318.         if turtle.down() then
  319.             update("down")
  320.             sleep(3)
  321.             while not turtle.up() do
  322.                 sleep(1)
  323.             end
  324.             update("up")
  325.         end
  326.         return
  327.     elseif choice == 3 then
  328.         if turtle.forward() then
  329.             update("forward")
  330.             sleep(3)
  331.             while not turtle.back() do
  332.                 sleep(1)
  333.             end
  334.             update("backward")
  335.         end
  336.         return
  337.     elseif choice == 4 then
  338.         if turtle.back() then
  339.             update("backward")
  340.             sleep(3)
  341.             while not turtle.forward() do
  342.                 sleep(1)
  343.             end
  344.             update("forward")
  345.         end
  346.         return
  347.     elseif choice == 5 then
  348.         right()
  349.         if turtle.forward() then
  350.             update("forward")
  351.             sleep(3)
  352.             while not turtle.back() do
  353.                 sleep(1)
  354.             end
  355.             update("backward")
  356.         end
  357.         left()
  358.         return
  359.     elseif choice == 6 then
  360.         left()
  361.         if turtle.forward() then
  362.             update("forward")
  363.             sleep(3)
  364.             while not turtle.back() do
  365.                 sleep(1)
  366.             end
  367.             update("backward")
  368.         end
  369.         right()
  370.         return
  371.     end
  372. end
  373.  
  374. function forward()
  375.   update("forward")
  376.   while not turtle.forward() do
  377.     if turtle.getFuelLevel() == 0 then
  378.         error("out of fuel")
  379.     end
  380.     local a,check = turtle.inspect()
  381.     if a then
  382.         if not check.name:lower():find("turtle") then
  383.             turtle.dig()
  384.         else
  385.             courtesy()
  386.         end
  387.     end
  388.   end
  389. end
  390.  
  391. function backward()
  392.   update("backward")
  393.   while not turtle.back() do
  394.     if turtle.getFuelLevel() == 0 then
  395.         error("out of fuel")
  396.     end
  397.     right()
  398.     right()
  399.     local a,check = turtle.inspect()
  400.     if a then
  401.         if not check.name:lower():find("turtle") then
  402.             turtle.dig()
  403.         else
  404.             courtesy()
  405.         end
  406.     end
  407.     right()
  408.     right()
  409.   end
  410. end
  411.  
  412. function up()
  413.   update("up")
  414.   while not turtle.up() do
  415.     if turtle.getFuelLevel() == 0 then
  416.         error("out of fuel")
  417.     end
  418.     local a,check = turtle.inspectUp()
  419.     if a then
  420.         if not check.name:lower():find("turtle") then
  421.             turtle.digUp()
  422.         else
  423.             courtesy()
  424.         end
  425.     end
  426.   end
  427. end
  428.  
  429. function down()
  430.   update("down")
  431.   while not turtle.down() do
  432.     if turtle.getFuelLevel() == 0 then
  433.         error("out of fuel")
  434.     end
  435.     local a,check = turtle.inspectDown()
  436.     if a then
  437.         if not check.name:lower():find("turtle") then
  438.             turtle.digDown()
  439.         else
  440.             courtesy()
  441.         end
  442.     end
  443.   end
  444. end
  445.  
  446. function right()
  447.   update("right")
  448.   turtle.turnRight()
  449. end
  450.    
  451. function left()
  452.   update("left")
  453.   turtle.turnLeft()
  454. end
  455.  
  456. function turn(dir)
  457.     if face == "north" then
  458.         if dir == "east" then
  459.             while face ~= dir do
  460.                 right()
  461.             end
  462.         else
  463.             while face ~= dir do
  464.                 left()
  465.             end
  466.         end
  467.    elseif face == "east" then
  468.         if dir == "south" then
  469.             while face ~= dir do
  470.                 right()
  471.             end
  472.         else
  473.             while face ~= dir do
  474.                 left()
  475.             end
  476.         end
  477.     elseif face == "south" then
  478.         if dir == "west" then
  479.             while face ~= dir do
  480.                 right()
  481.             end
  482.         else
  483.             while face ~= dir do
  484.                 left()
  485.             end
  486.         end
  487.     elseif face == "west" then
  488.         if dir == "north" then
  489.             while face ~= dir do
  490.                 right()
  491.             end
  492.         else
  493.             while face ~= dir do
  494.                 left()
  495.             end
  496.         end
  497.     end
  498. end
  499.  
  500. function place()
  501.   while not turtle.placeDown() do
  502.     turtle.digDown()
  503.   end
  504.   return true
  505. end
  506.  
  507. function saveInventory()
  508.  
  509. end
  510.  
  511. function updateInventory()
  512.     --updates inventory table with whats in the turtle inventory
  513.     local inventory = {}
  514.     for i = 1,16 do
  515.         inventory[i] = turtle.getItemDetail(i)
  516.     end
  517. end
  518.  
  519. function find(id,data,slots)
  520.     --input numid or strname, numdata, and tabslots
  521.     --output slotNumber
  522.     --slots[id][data] = {name,damage,orientation}
  523.     local exceptions = {"stairs","slabs","door","ladder","torch","lever","button","rail","chest","furnace"}
  524.     local name, damage, orientation
  525.    
  526.     if type(id) == "number" then
  527.         name = slots[id][data][1]
  528.         damage = slots[id][data][2]
  529.         orientation = data
  530.         --convert to string ie minecraft:stone
  531.     elseif type(id) == "string" then
  532.         name = id
  533.         damage = data
  534.         orientation = data
  535.     end
  536.     --assert(name)
  537.     if not name then
  538.         --skips if not assigned in slots
  539.         return "skip"
  540.     end    
  541.  
  542.     for i=1,16 do
  543.         local item = turtle.getItemDetail(i)
  544.         if item and item.name == name then      
  545.            if item.damage == damage then
  546.                 return i
  547.             end
  548.         end
  549.     end
  550.     if term.isColor() then
  551.         term.setTextColor(colors.red)
  552.     end
  553.     print("not enough "..name..", "..damage)
  554.     term.setTextColor(colors.white)
  555.  
  556.     return nil
  557. end
  558.  
  559. function findWrench()
  560.   for i =1,16 do
  561.     local deets = turtle.getItemDetail(i)
  562.     if deets and (deets.name:lower():find("wrench") or deets.name:lower():find("hammer")) then
  563.       return i
  564.     end
  565.   end
  566.   error("can't find the wrench/hammer")
  567. end
  568.  
  569. function includeOffset(block,data,relativeDirection)
  570.  
  571.     local function direction2offset(relativeDirection)
  572.         if relativeDirection == "south" then
  573.             return 0
  574.         elseif relativeDirection == "west" then
  575.             return 1
  576.         elseif relativeDirection == "north" then
  577.             return 2
  578.         elseif relativeDirection == "east" then
  579.             return 3
  580.         end
  581.     end
  582.  
  583.     local function implementData(data,relativeDirection)
  584.         turn_offset = direction2offset(relativeDirection)
  585.         local chestDirs = {[0]=2,5,3,4}
  586.         for i,v in pairs(chestDirs) do
  587.             if data == chestDirs[i] then
  588.                 data = chestDirs[(i+turn_offset)%4]
  589.                 return data
  590.             end
  591.         end
  592.     end
  593.  
  594.     local function implementDataStairs(data,relativeDirection)
  595.         turn_offset = direction2offset(relativeDirection)
  596.         local chestDirs = {[0]=3,0,2,1}
  597.         local reversed = false
  598.         if data > 3 then
  599.             data = data - 4
  600.             reversed = true
  601.         end
  602.         for i,v in pairs(chestDirs) do
  603.             if data == chestDirs[i] then
  604.                 data = chestDirs[(i+turn_offset)%4]
  605.                 if reversed then
  606.                     data = data + 4
  607.                 end
  608.                 return data
  609.             end
  610.         end
  611.     end
  612.  
  613.     local function implementDataStairsOld(data,relativeDirection)
  614.         turn_offset = direction2offset(relativeDirection)
  615.         for i = 0,3 do
  616.             if data == i then
  617.                 data = (data + turn_offset)%4
  618.                 return data
  619.             end
  620.         end
  621.         for i = 4,7 do
  622.             if data == i then
  623.                 data = ((data-4) + turn_offset)%4 + 4
  624.                 return data
  625.             end
  626.         end
  627.     end
  628.  
  629.    if block.name:lower():find("stairs") then
  630.             data = implementDataStairs(data,relativeDirection)
  631.     elseif block.name:lower():find("chest") then
  632.             data = implementData(data,relativeDirection)
  633.     end
  634.     return data
  635. end
  636.  
  637. function findAndPlace(id,data,slots,wrench)
  638. --wrench turning included
  639. --check for wrench 14
  640. --check if slabs or stairs
  641.     local nSlot = find(id,data,slots)
  642.     if nSlot == "skip" then
  643.         return
  644.     end
  645.     if not nSlot then
  646.         assert(nObjective)
  647.         assert(instructions)
  648.         assert(slots)
  649.         checklist = checklistMaker(nObjective,instructions,slots)
  650.         checklist, rubbishList = subtractInventory(checklist)
  651.         check_chests(checklist,rubbishList)
  652.         nSlot = find(id,data,slots)
  653.         if not nSlot then
  654.             error("please refill materials in refill chest")
  655.         end
  656.         local x,y,z = unpack(instructions[nObjective],1,3)
  657.         if reference.multiturtle then
  658.             goto(reference.finalx+1,y,z)
  659.         end
  660.         goto(x+1,y,z)
  661.     end
  662.     turtle.select(nSlot)
  663.     local block = turtle.getItemDetail()
  664.     if block then
  665.         --lookup stairs or slabs
  666.         if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest") or block.name:lower():find("furnace")) and wrench and id~=324 and id~=96 then
  667.             --place until metadata matches
  668.             place()
  669.             data = includeOffset(block,data,reference.relativeDirection)
  670.             local tries = 0
  671.             while true do
  672.                 local stair,orient = turtle.inspectDown()
  673.                 if stair then
  674.                     if orient.metadata == data then
  675.                         break
  676.                     end
  677.                 end
  678.                 turtle.select(findWrench())
  679.                 turtle.placeDown()
  680.                 tries = tries + 1
  681.                 if tries > 9 then
  682.                     break
  683.                 end
  684.             end
  685.         else
  686.             place()
  687.         end
  688.     else
  689.         return false
  690.     end
  691. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement