Advertisement
fatboychummy

OmegaQuarry

Oct 17th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local state = "unknown"
  2. --[[
  3. States:
  4.   home      --> Can go to chests, mining, setup
  5.   chests    --> Can go to mining, returning, home
  6.   mining    --> Can go to returning
  7.   returning --> Can go to chests,home
  8.   setup     --> Can go to home
  9.   unknown   --> Can go to returning
  10. ]]
  11. local lastKnownCoords = {0,0,0}
  12. local homeCoords = {0,0,0}
  13. local data = {}
  14. local saveLocation = "save.dat"
  15.  
  16. local function copy(a,b)
  17.   assert(type(a) == "table","copy: first input is not a table.")
  18.   for k,v in pairs(a) do
  19.     if type(v) == "table" then
  20.       b[k] = {}
  21.       copy(a[k],b[k])
  22.     else
  23.       b[k] = v
  24.     end
  25.   end
  26. end
  27.  
  28. local function saveData()
  29.   data.State = state
  30.   data.Coords = {}
  31.   data.homeCoords = {}
  32.   copy(homeCoords,data.homeCoords)
  33.   copy(lastKnownCoords,data.Coords)
  34.   local h = fs.open("savedata","w")
  35.   if h then
  36.     h.write(textutils.serialize(data))
  37.     h.close()
  38.   end
  39. end
  40.  
  41. local function loadData()
  42.   if not fs.exists("savedata") then
  43.     state = "setup"
  44.     saveData()
  45.     return false,0
  46.   else
  47.     local h = fs.open(saveLocation,"r")
  48.     if h then
  49.       local dat = textutils.unserialize(h.readAll())
  50.       h.close()
  51.       if dat then
  52.        
  53.       else
  54.         state = "setup"
  55.         saveData()
  56.         return false,1
  57.       end
  58.     else
  59.       state = "setup"
  60.       saveData()
  61.       return false,2
  62.     end
  63.   end
  64. end
  65.  
  66.  
  67.  
  68. --Determine the state by checking the immediate surroundings
  69. --If unable to determine, return "unknown"
  70. --How unknown is to be handled: ping reciever, get location, attempt to return home, if fail: stop, ping reciever, sleep
  71. local function determineState()
  72.   for i = 1,4 do
  73.     local a,block = turtle.inspect()
  74.     if a then
  75.       if block.name:find("chest") then
  76.         turtle.turnLeft()
  77.         turtle.forward()
  78.         turtle.turnRight()
  79.         return "home"
  80.       end
  81.       if block.name == "minecraft:cobblestone" then
  82.         return "returning"
  83.       end
  84.     end
  85.     turtle.turnLeft()
  86.   end
  87.   local a,block = turtle.inspectUp()
  88.   if a then
  89.     if block.name == "minecraft:cobblestone" then
  90.       return "returning"
  91.     end
  92.   end
  93.   return "unknown"
  94. end
  95.  
  96.  
  97.  
  98.  
  99.  
  100. if data == {} then loadData() end
  101.  
  102.  
  103.  
  104.  
  105. local function main()
  106.   if
  107.   while true do
  108.     --TODO: Handle states
  109.  
  110.   end
  111. end
  112.  
  113.  
  114. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement