Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local state = "unknown"
- --[[
- States:
- home --> Can go to chests, mining, setup
- chests --> Can go to mining, returning, home
- mining --> Can go to returning
- returning --> Can go to chests,home
- setup --> Can go to home
- unknown --> Can go to returning
- ]]
- local lastKnownCoords = {0,0,0}
- local homeCoords = {0,0,0}
- local data = {}
- local saveLocation = "save.dat"
- local function copy(a,b)
- assert(type(a) == "table","copy: first input is not a table.")
- for k,v in pairs(a) do
- if type(v) == "table" then
- b[k] = {}
- copy(a[k],b[k])
- else
- b[k] = v
- end
- end
- end
- local function saveData()
- data.State = state
- data.Coords = {}
- data.homeCoords = {}
- copy(homeCoords,data.homeCoords)
- copy(lastKnownCoords,data.Coords)
- local h = fs.open("savedata","w")
- if h then
- h.write(textutils.serialize(data))
- h.close()
- end
- end
- local function loadData()
- if not fs.exists("savedata") then
- state = "setup"
- saveData()
- return false,0
- else
- local h = fs.open(saveLocation,"r")
- if h then
- local dat = textutils.unserialize(h.readAll())
- h.close()
- if dat then
- else
- state = "setup"
- saveData()
- return false,1
- end
- else
- state = "setup"
- saveData()
- return false,2
- end
- end
- end
- --Determine the state by checking the immediate surroundings
- --If unable to determine, return "unknown"
- --How unknown is to be handled: ping reciever, get location, attempt to return home, if fail: stop, ping reciever, sleep
- local function determineState()
- for i = 1,4 do
- local a,block = turtle.inspect()
- if a then
- if block.name:find("chest") then
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- return "home"
- end
- if block.name == "minecraft:cobblestone" then
- return "returning"
- end
- end
- turtle.turnLeft()
- end
- local a,block = turtle.inspectUp()
- if a then
- if block.name == "minecraft:cobblestone" then
- return "returning"
- end
- end
- return "unknown"
- end
- if data == {} then loadData() end
- local function main()
- if
- while true do
- --TODO: Handle states
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement