Advertisement
Pinkishu

an

Dec 20th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.03 KB | None | 0 0
  1. local nav = nil
  2. local navDir = 1
  3. local verbose = true
  4. local slotSel = 1
  5. local baseData = {}
  6. local roamingData = {selSlot=1}
  7. turtle.select(1)
  8.  
  9. local gpsDataFile = "gpsData"
  10. local slotFile = "slotFile"
  11. local baseDataFile = "baseData"
  12. local roamingDataFile = "roamingData"
  13.  
  14. local vecVars = {"x","y","z"}
  15.  
  16. local vecMods = {}
  17. vecMods[1] = vector.new(0,0,1)
  18. vecMods[2] = vector.new(-1,0,0)
  19. vecMods[3] = vector.new(0,0,-1)
  20. vecMods[4] = vector.new(1,0,0)
  21. vecMods[5] = vector.new(0,1,0)
  22. vecMods[6] = vector.new(0,-1,0)
  23.  
  24. function vectorFromTable(tab)
  25.     return vector.new(tab.x,tab.y,tab.z)
  26. end
  27.  
  28. function tableToFile(fileName,saveTable)
  29.     local f = fs.open(fileName,"w")
  30.     f.writeLine(textutils.serialize(saveTable))
  31.     f.close()
  32. end
  33.  
  34. function tableFromFile(fileName)
  35.     local f = fs.open(fileName,"r")
  36.     if not f then return nil end
  37.     local t = textutils.unserialize(f.readLine())
  38.     f.close()
  39.     return t
  40. end
  41.  
  42. function saveBaseData()
  43.     tableToFile(baseDataFile,baseData)
  44. end
  45.  
  46. function loadBaseData()
  47.     return tableFromFile(baseDataFile)
  48. end
  49.  
  50. function getRoamingData(vName)
  51.     return roamingData[vName]
  52. end
  53.  
  54. function getBaseData(vName)
  55.     return baseData[vName]
  56. end
  57.  
  58. function saveRoamingData()
  59.     tableToFile(roamingDataFile,roamingData)
  60. end
  61. if not fs.exists(roamingDataFile) then saveRoamingData() end
  62.  
  63. function loadRoamingData()
  64.     return tableFromFile(roamingDataFile)
  65. end
  66.  
  67. function setBaseData(bName,bVal)
  68.     print(tostring(bName).."/"..tostring(bVal))
  69.     baseData[bName] = bVal
  70.     saveBaseData()
  71. end
  72.  
  73.  
  74.  
  75. function setRoamingData(bName,bVal,forceRAM)
  76.     forceRAM = forceRAM or false
  77.     roamingData[bName] = bVal
  78.    
  79.     if not forceRAM then
  80.        
  81.         saveRoamingData()
  82.     end
  83. end
  84.  
  85. function saveSlot()
  86.     setRoamingData("selSlot",selSlot)
  87. end
  88.  
  89. function loadSlot()
  90.     return roamingData.selSlot
  91. end
  92.  
  93. function select(nSlot)
  94.     slotSel = nSlot
  95.     saveSlot()
  96.     turtle.select(nSlot)
  97. end
  98.  
  99. function vPrint(str)
  100.     if verbose then print(str) end
  101. end
  102.  
  103. function refuel()
  104.     turtle.select(baseData.fuelSlot)
  105.     turtle.refuel(1)
  106.     turtle.select(slotSel)
  107. end
  108.  
  109. function checkFuel()
  110.     if turtle.getFuelLevel() < 1 then
  111.         refuel()
  112.     end
  113. end
  114.  
  115. function tryLoadGPSData()
  116.     if not fs.exists(gpsDataFile) then return false end
  117.   local navD = tableFromFile(gpsDataFile)
  118.   if navD == nil then return false end
  119.   nav = vectorFromTable(navD[1])
  120.   navDir = navD[2]
  121.   return true
  122. end
  123.  
  124. function copyTable(it)
  125.     local ot = {}
  126.     for k,v in pairs(it) do
  127.         ot[k] = v
  128.     end
  129.     setmetatable(ot,getmetatable(it))
  130.     return ot
  131. end
  132.  
  133. function saveGPSData()
  134.     tableToFile(gpsDataFile,{nav,navDir})
  135. end
  136.  
  137. function saveFuel()
  138.     setRoamingData("fuel",turtle.getFuelLevel())
  139. end
  140.  
  141. function loadFuel()
  142.     return roamingData.fuel
  143. end
  144.  
  145. function saveCmdDir(dir)
  146.     setRoamingData("cmdDir",dir)
  147. end
  148.  
  149. function loadCmdDir()
  150.     return roamingData.cmdDir
  151. end
  152.  
  153. function getDir()
  154.     return navDir
  155. end
  156.  
  157. function getReverseDir()
  158.     return (navDir+2)%4
  159. end
  160.  
  161. function getLoc()
  162.     return copyTable(nav)
  163. end
  164.  
  165. function setDir(nDir)
  166.     if dir ~= nDir then
  167.         navDir=nDir
  168.         saveGPSData()
  169.     end
  170. end
  171.  
  172. function translate(vec)
  173.   nav = nav + vec
  174.     saveGPSData()
  175. end
  176.  
  177. function getVecMod(dir)
  178.   dir = dir or getDir()
  179.   return vecMods[dir]
  180. end
  181.  
  182. function setSandboxBoundaries(lx,ly,lz,hx,hy,hz)
  183.  
  184.     an.setBaseData("sandbox_boundaries",{worldToLocal(vector.new(lx,ly,lz)),worldToLocal(vector.new(hx,hy,hz))})
  185. end
  186.  
  187. function setSandboxAction(action)
  188.     an.setBaseData("sandbox_action",action)
  189. end
  190.  
  191. function initGPS(useRednet)
  192.     useRednet = useRednet or false
  193.     vPrint("Init GPS...")
  194.     baseData = loadBaseData()
  195.     roamingData = loadRoamingData()
  196.     local sl = loadSlot()
  197.     if sl ~= nil then turtle.select(sl) slotSel = sl end
  198.     if baseData == nil then baseData = {} end
  199.     setBaseData("fuelSlot",15)
  200.  
  201.   if baseData.navOrigin == nil then setBaseData("navOrigin",vector.new(0,0,0)) end
  202.  
  203.     if not useRednet then
  204.         vPrint("Using local GPS.")
  205.         if not tryLoadGPSData() then nav = vector.new(0,0,0) navDir=1 vPrint("No file found, resetting.") end
  206.     end
  207.  
  208.     local fuel = roamingData.fuel
  209.   vPrint("Fuel: "..tostring(fuel))
  210.     if fuel ~= nil then
  211.  
  212.         vPrint("Loaded fuel:" .. fuel)
  213.         vPrint("TFuel: "..turtle.getFuelLevel())
  214.         if turtle.getFuelLevel() ~= fuel then
  215.             vPrint("Mislocated. Adding one translation.")
  216.             local cmdDir = roamingData.cmdDir
  217.             translate(getVecMod(cmdDir))
  218.             saveFuel()
  219.         end
  220.     end
  221. end
  222.  
  223. function resetGPS()
  224.     nav = vector.new(0,0,0)
  225.     navDir = 1
  226.     saveFuel()
  227.     saveGPSData()
  228. end
  229.  
  230. function checkSandbox()
  231.     local sb = an.getBaseData("sandbox_boundaries")
  232.     local sba = an.getBaseData("sandbox_action")
  233.  
  234.     if sb == nil then return true end
  235. --print("NAV:"..textutils.serialize(nav))
  236. --int("SB:"..textutils.serialize(sb[1]))
  237.     if ( nav.x < sb[1].x or nav.y < sb[1].y or nav.z < sb[1].z ) or ( nav.x > sb[2].x or nav.y > sb[2].y or nav.z > sb[2].z ) then
  238.         if sba == "kill" then error("Terminated due to leaving sandbox constraints.") end
  239.         return false
  240.     end
  241.     return true
  242. end
  243.  
  244. function forward()
  245.     checkFuel()
  246.     setRoamingData("fuel",turtle.getFuelLevel(),true)
  247.     setRoamingData("cmdDir",getDir())
  248.     local allow = checkSandbox()
  249.     if allow and turtle.forward() then
  250.         setRoamingData("fuel",turtle.getFuelLevel())
  251.         translate(getVecMod())
  252.         return true
  253.     end
  254.     return false
  255. end
  256.  
  257. function back()
  258.     checkFuel()
  259.     setRoamingData("fuel",turtle.getFuelLevel(),true)
  260.     setRoamingData("cmdDir",getReverseDir())
  261.     local allow = checkSandbox()
  262.     if allow and turtle.back() then
  263.         setRoamingData("fuel",turtle.getFuelLevel())
  264.         translate(getVecMod(getReverseDir()))
  265.         return true
  266.     end
  267.     return false
  268. end
  269.  
  270. function turnLeft()
  271.     local dir = getDir() - 1
  272.     if dir == 0 then dir = 4 end
  273.     setDir(dir)
  274.     turtle.turnLeft()
  275. end
  276.  
  277. function turnRight()
  278.     local dir = getDir()
  279.     dir = ( dir + 1 )
  280.     if dir == 5 then dir = 1 end
  281.     setDir(dir)
  282.     turtle.turnRight()
  283. end
  284.  
  285. function turnDir(nDir)
  286.     if nDir == 5 or nDir == 6 then return end
  287.     cDir = nDir - 1
  288.     local dir = getDir()
  289.     if ( dir ) % 4 == cDir then turnRight()
  290.     else while getDir() ~= nDir do turnLeft() end end
  291. end
  292.  
  293. function turnAround()
  294.     turnDir(getReverseDir())
  295. end
  296.  
  297. function up()
  298.     checkFuel()
  299.     setRoamingData("fuel",turtle.getFuelLevel(),true)
  300.     setRoamingData("cmdDir",5)
  301.     local allow = checkSandbox()
  302.     if allow and turtle.up() then
  303.         setRoamingData("fuel",turtle.getFuelLevel())
  304.         translate(getVecMod(5))
  305.         return true
  306.     end
  307.     return false
  308. end
  309.  
  310. function down()
  311.     checkFuel()
  312.     setRoamingData("fuel",turtle.getFuelLevel(),true)
  313.     setRoamingData("cmdDir",6)
  314.     local allow = checkSandbox()
  315.     if allow and turtle.down() then
  316.         setRoamingData("fuel",turtle.getFuelLevel())
  317.         translate(getVecMod(6))
  318.         return true
  319.     end
  320.     return false
  321. end
  322.  
  323. function localToWorld(vLocal)
  324.     local vWorld = vector.new(0,0,0)
  325.     vWorld = baseData.navOrigin + vLocal
  326.  
  327.     return vWorld
  328. end
  329.  
  330. function worldToLocal(vWorld)
  331.     local vLocal = vector.new(0,0,0)  
  332.     vLocal = vWorld - baseData.navOrigin
  333.     return vLocal
  334. end
  335.  
  336. function vecSum(vecA)
  337.     return vecA.x+vecA.y+vecA.z
  338. end
  339.  
  340. function mulVec(vecA,vecB)
  341.     return vector.new(vecA.x*vecB.x,vecA.y*vecB.y,vecA.z*vecB.z)
  342. end
  343.  
  344. function getDirFromVec(vec)
  345.     for i=1,#vecMods,1 do
  346.        
  347.         if vecSum(vecMods[i]) == vecSum(vec:normalize()) and vec:normalize():sub(vecMods[i]):length() == 0 then return i end
  348.     end
  349. end
  350.  
  351. function localDelta(vLocal)
  352.     return vLocal - nav
  353. end
  354.  
  355. function goToWorld(dWorld,mOrder)
  356.     return goTo(an.localDelta(an.worldToLocal(dWorld)),mOrder)
  357. end
  358.  
  359. function goToLocal(dLocal,mOrder)
  360.     return goTo(an.localDelta(dLocal),mOrder)
  361. end
  362.  
  363. function goTo(dLocal,mOrder)
  364.     print("moving delta full: "..tostring(dLocal))
  365.     local pIndex = 1
  366.     if dLocal:length() == 0 then return false end
  367.     while #mOrder > 0 do
  368.         local dLocalSub = vector.new(0,0,0)
  369.         local v = mOrder[pIndex]
  370.         dLocalSub[v] = dLocal[v]
  371.         if dLocalSub:length() ~= 0 then
  372.             local tDir = getDirFromVec(dLocalSub)
  373.             if tDir ~= 5 and tDir ~= 6 then
  374.                 turnDir(tDir)
  375.             end
  376.             local steps = math.abs(dLocalSub[v])
  377.  
  378.             local mFunc = forward
  379.             if tDir == 5 then mFunc = up
  380.             elseif tDir == 6 then mFunc = down end
  381.             --print("steps to move: "..steps)
  382.             while steps > 0 and mFunc() do steps = steps-1 end
  383.             local diff = math.abs(dLocalSub[v]) - steps
  384.             --print("modding dLocal by: "..(getVecMod(tDir) * diff)[v])
  385.             dLocal[v] = dLocal[v] - (getVecMod(tDir) * diff)[v]
  386.             --print("moving on "..v)
  387.             --print("moved diff: " .. diff)
  388.             --print("new dLocal: "..dLocal[v])
  389.             --print("new dlocal sub: "..dLocalSub[v])
  390.             --read()
  391.             if diff == math.abs(dLocalSub[v]) then
  392.                 --print("move on this axis done")
  393.                 --read()
  394.                 table.remove(mOrder,pIndex)
  395.             else
  396.                 --print("moving to next axis to come bakc later")
  397.                 --read()
  398.                 pIndex = pIndex + 1
  399.             end
  400.  
  401.             if pIndex > #mOrder then pIndex = 1 end
  402.         else
  403.             table.remove(mOrder,pIndex)
  404.             --print("removed "..v.." From move order (delta==0)") read()
  405.             if pIndex > #mOrder then pIndex = 1 end
  406.         end
  407.     end
  408. end
  409.  
  410. function test(ip)
  411.     for i=1,ip,1 do
  412.         print(getLoc())
  413.         goTo(vector.new(2,0,1),{"z","x"})
  414.         goTo(vector.new(0,0,-1),{"z"})
  415.         print(getLoc())
  416.         goTo(vector.new(-2,0,-1),{"z","x"})
  417.         goTo(vector.new(0,0,1),{"z"})
  418.     end
  419. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement