Advertisement
guitarplayer616

GPS API with ServerSave (better implementation required)

Jan 10th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = {...}
  2.  
  3. function delay()
  4.     sleep(.4)
  5.     return true
  6. end
  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.     if not fs.exists(file) then
  26.         error("file does not exist")
  27.     end
  28.    
  29.     variable = tostring(variable)
  30.     newValue = textutils.serialize(newValue)
  31.     --print(variable)
  32.    
  33.     local string = fs.open(file,"r").readAll()
  34.     local pattern = variable.."=(.*;)"
  35.     local replace = variable.."="..newValue..";"
  36.     --print(string:find(pattern))
  37.  
  38.     exists = string:find(pattern)
  39.  
  40.     if exists then
  41.         local h = fs.open(file,"w")
  42.         string = string:gsub(pattern,replace)
  43.         h.write(string)
  44.         h.close()
  45.     else
  46.         local h = fs.open(file,"a")
  47.         h.writeLine(replace)
  48.         h.close()
  49.     end
  50. end
  51.  
  52.  
  53. function reinitialize()
  54.     heightPos = 0
  55.     widthPos = 0
  56.     lengthPos = 0
  57.     face = "south"
  58.     recordPos(heightPos,widthPos,lengthPos,face)
  59. end
  60.  
  61. if tArgs[1] == "reset" then
  62.     reinitialize()
  63. end
  64.  
  65. if fs.exists("position") then
  66.     shell.run("position")
  67. else
  68.     reinitialize()
  69. end
  70.  
  71. --[[Navigation function]]--
  72.  
  73. function goto(heightGoal,widthGoal,lengthGoal)
  74.     shell.run("position")
  75.     if turtle.getFuelLevel() < 200 then
  76.         refill()
  77.     end
  78.     if heightGoal > heightPos then
  79.         while heightGoal > heightPos do
  80.             up()
  81.         end
  82.     elseif heightGoal < heightPos then
  83.         while heightGoal < heightPos do
  84.             down()
  85.         end
  86.     end
  87.     if widthGoal > widthPos then
  88.         turn("east")
  89.         while widthGoal > widthPos do
  90.             forward()
  91.         end
  92.     elseif widthGoal < widthPos then
  93.         turn("west")
  94.         while widthGoal < widthPos do
  95.             forward()
  96.         end
  97.     end
  98.     if lengthGoal > lengthPos then
  99.         turn("south")
  100.         while lengthGoal > lengthPos do
  101.             forward()
  102.         end
  103.     elseif lengthGoal < lengthPos then
  104.         turn("north")
  105.         while lengthGoal < lengthPos do
  106.             forward()
  107.         end
  108.     end
  109. end
  110.  
  111. function update(dir)
  112.         if dir == "forward" then
  113.             if face == "north" then
  114.                 lengthPos = lengthPos - 1
  115.             elseif face == "south" then
  116.                 lengthPos = lengthPos + 1
  117.             elseif face == "west" then
  118.                 widthPos = widthPos - 1
  119.             elseif face == "east" then
  120.                 widthPos = widthPos + 1
  121.             end
  122.         elseif dir == "backward" then
  123.                 if face == "north" then
  124.                         lengthPos = lengthPos + 1
  125.                 elseif face == "south" then
  126.                         lengthPos = lengthPos - 1
  127.                 elseif face == "west" then
  128.                         widthPos = widthPos + 1
  129.                 elseif face == "east" then
  130.                         widthPos = widthPos - 1
  131.                 end
  132.         elseif dir == "up" then
  133.                 heightPos = heightPos + 1
  134.         elseif dir == "down" then
  135.                 heightPos = heightPos - 1
  136.         elseif dir == "right" then
  137.                 if face == "north" then
  138.                         face = "east"
  139.                 elseif face == "east" then
  140.                         face = "south"
  141.                 elseif face == "south" then
  142.                         face = "west"
  143.                 elseif face == "west" then
  144.                         face = "north"
  145.                 end
  146.         elseif dir == "left" then
  147.                 if face == "north" then
  148.                         face = "west"
  149.                 elseif face == "west" then
  150.                         face = "south"
  151.                 elseif face == "south" then
  152.                         face = "east"
  153.                 elseif face == "east" then
  154.                         face = "north"
  155.                 end
  156.         end
  157.         recordPos(heightPos,widthPos,lengthPos,face)
  158. end
  159.  
  160. --[[Movement functions]]--
  161.  
  162. function forward()
  163.   parallel.waitForAll(delay,function() while not turtle.forward() do turtle.dig() end return true end,function() update("forward") end)
  164. end
  165.  
  166. function up()
  167.   parallel.waitForAll(delay,function() while not turtle.up() do turtle.digUp() end return true end,function() update("up") end)
  168. end
  169.  
  170. function down()
  171.   parallel.waitForAll(delay,function() while not turtle.down() do turtle.digDown() end return true end,function() update("down") end)
  172. end
  173.  
  174. function right()
  175.   parallel.waitForAll(delay,turtle.turnRight,function() update("right") end)
  176. end
  177.    
  178. function left()
  179.   parallel.waitForAll(delay,turtle.turnLeft,function() update("left") end)
  180. end
  181.  
  182. function turn(dir)
  183.     if face == "north" then
  184.         if dir == "east" then
  185.             while face ~= dir do
  186.                 right()
  187.             end
  188.         else
  189.             while face ~= dir do
  190.                 left()
  191.             end
  192.         end
  193.    elseif face == "east" then
  194.         if dir == "south" then
  195.             while face ~= dir do
  196.                 right()
  197.             end
  198.         else
  199.             while face ~= dir do
  200.                 left()
  201.             end
  202.         end
  203.     elseif face == "south" then
  204.         if dir == "west" then
  205.             while face ~= dir do
  206.                 right()
  207.             end
  208.         else
  209.             while face ~= dir do
  210.                 left()
  211.             end
  212.         end
  213.     elseif face == "west" then
  214.         if dir == "north" then
  215.             while face ~= dir do
  216.                 right()
  217.             end
  218.         else
  219.             while face ~= dir do
  220.                 left()
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226. function place()
  227.   while not turtle.placeDown() do
  228.     turtle.digDown()
  229.   end
  230.   return true
  231. end
  232.  
  233. function saveInventory()
  234.  
  235. end
  236.  
  237. function updateInventory()
  238.     --updates inventory table with whats in the turtle inventory
  239.     local inventory = {}
  240.     for i = 1,16 do
  241.         inventory[i] = turtle.getItemDetail(i)
  242.     end
  243. end
  244.  
  245.  
  246. function find(id,data,slots)
  247.     --input numid or strname, numdata, and tabslots
  248.     --output slotNumber
  249.     --slots[id][data] = {name,damage,orientation}
  250.     local exceptions = {"stairs","slabs","door","ladder","torch","lever","button","rail","chest","furnace"}
  251.     local name, damage, orientation
  252.    
  253.     if type(id) == "number" then
  254.         name = slots[id][data][1]
  255.         damage = slots[id][data][2]
  256.         orientation = data
  257.         --convert to string ie minecraft:stone
  258.     elseif type(id) == "string" then
  259.         name = id
  260.         damage = data
  261.         orientation = data
  262.     end
  263.     --assert(name)
  264.     if not name then
  265.         --skips if not assigned in slots
  266.         return nil
  267.     end    
  268.  
  269.     for i=1,16 do
  270.         local item = turtle.getItemDetail(i)
  271.         if item and item.name == name then
  272.             --[[for s = 1,#exceptions do
  273.                 if name:find(exceptions[s]) then
  274.                     return i
  275.                 end
  276.             end]]
  277.            
  278.             if item.damage == damage then
  279.                 return i
  280.             end
  281.         end
  282.     end
  283.    
  284.    
  285.     error("not enough "..name..", "..damage)
  286. end
  287.  
  288. function findBackup(id,data,slots)
  289.     --input numid or strname, numdata, and tabslots
  290.     --output slotNumber
  291.     --slots[id][data] = {name,damage,orientation}
  292.     local exceptions = {"stairs","slabs","door","ladder","torch","lever","button","rail","chest","furnace"}
  293.     local name, damage, orientation
  294.    
  295.     if type(id) == "number" then
  296.         name = slots[id][data][1]
  297.         damage = slots[id][data][2]
  298.         orientation = data
  299.         --convert to string ie minecraft:stone
  300.     elseif type(id) == "string" then
  301.         name = id
  302.         damage = data
  303.         orientation = data
  304.     end
  305.     --assert(name)
  306.     if not name then
  307.         --skips if not assigned in slots
  308.         return nil
  309.     end    
  310.  
  311.     for i=1,16 do
  312.         turtle.select(i)
  313.         local item = turtle.getItemDetail()
  314.         if item and item.name == name then
  315.             --[[for s = 1,#exceptions do
  316.                 if name:find(exceptions[s]) then
  317.                     return i
  318.                 end
  319.             end]]
  320.            
  321.             if item.damage == damage then
  322.                 return i
  323.             end
  324.         end
  325.     end
  326.    
  327.    
  328.     error("not enough "..name..", "..damage)
  329. end
  330.  
  331. function smartPlace(wrench,orientation)
  332. --wrench turning included
  333. --check for wrench 14
  334. --check if slabs or stairs
  335.     local block = turtle.getItemDetail()
  336.     if block then
  337.         --lookup stairs or slabs
  338.         if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest")) and wrench then
  339.             --place until metadata matches
  340.             place()
  341.             while true do
  342.                 local stair,orient = turtle.inspectDown()
  343.                 if stair then
  344.                     if orient.metadata == orientation then
  345.                         break
  346.                     end
  347.                 end
  348.                 local mem = turtle.getSelectedSlot()
  349.                 turtle.select(wrench)
  350.                 turtle.placeDown()
  351.                 turtle.select(mem)
  352.             end
  353.         else
  354.             place()
  355.         end
  356.     else
  357.         return false
  358.     end
  359. end
  360.  
  361. function findAndPlace(id,data,slots,wrench)
  362. --wrench turning included
  363. --check for wrench 14
  364. --check if slabs or stairs
  365.     local nSlot = find(id,data,slots)
  366.     if not nSlot then
  367.         return
  368.     end
  369.     turtle.select(nSlot)
  370.     local block = turtle.getItemDetail()
  371.     if block then
  372.         --lookup stairs or slabs
  373.          if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest")) and wrench and id~=324 and id~=96 then
  374.             --place until metadata matches
  375.             place()
  376.             while true do
  377.                 local stair,orient = turtle.inspectDown()
  378.                 if stair then
  379.                     if orient.metadata == data then
  380.                         break
  381.                     end
  382.                 end
  383.                 local mem = turtle.getSelectedSlot()
  384.                 turtle.select(wrench)
  385.                 turtle.placeDown()
  386.                 turtle.select(mem)
  387.             end
  388.         else
  389.             place()
  390.         end
  391.     else
  392.         return false
  393.     end
  394. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement