Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local database = {}
- local Coords = {}
- local isPosSaved = true
- --returns xCoord as int
- function database.GetXCoord()
- if(Coords.xCoord == nil) then
- printError("x co-ordinate is nil, did you run PopulateDB first?")
- end
- return Coords.xCoord
- end
- function database.SetXCoord(xCoord)
- if(Coords.xCoord == nil) then
- printError("x co-ordinate is nil, did you run PopulateDB first?")
- return
- end
- Coords.xCoord = xCoord
- database.StorePosition()
- end
- --returns yCoord as int
- function database.GetYCoord()
- if(Coords.yCoord == nil) then
- printError("y co-ordinate is nil, did you run PopulateDB first?")
- end
- return Coords.yCoord
- end
- function database.SetYCoord(yCoord)
- if(Coords.yCoord == nil) then
- printError("y co-ordinate is nil, did you run PopulateDB first?")
- return
- end
- Coords.yCoord = yCoord
- database.StorePosition()
- end
- --returns zCoord as int
- function database.GetZCoord()
- if(Coords.zCoord == nil) then
- printError("z co-ordinate is nil, did you run PopulateDB first?")
- end
- return Coords.zCoord
- end
- function database.SetZCoord(zCoord)
- if(Coords.zCoord == nil) then
- printError("z co-ordinate is nil, did you run PopulateDB first?")
- return
- end
- Coords.zCoord = zCoord
- database.StorePosition()
- end
- --function returns table of x,y,z coordinates indexed by xCoord, yCoord, and zCoord
- function database.GetCoords()
- if(Coords.xCoord == nil or Coords.y == nil or Coords.z == nil ) then
- printError("one or more co-ordinates in the table are nil, did you run PopulateDB first?")
- end
- return Coords
- end
- --reads from the position.txt file and saves the coordinates, creates the position file at /home/smart_quarry/database/position.txt
- --returns true if succeeds and false if fails, prints error messages to console
- function database.PopulateDB()
- local pos = fs.open("smart_quarry/database/position.txt","r")
- local dbPopulatedCorrectly = true
- if(pos == nil) then
- printError("position.txt file does not exist, creating one now at home/smart_quarry/database/position.txt, please add turtle start position to file with co-ordinate on a seperate line. It goes in order x, y, z. only the co-ordinates should be present no letters")
- isPosSaved = false
- pos = fs.open("smart_quarry/database/position.txt","w")
- dbPopulatedCorrectly = false
- return dbPopulatedCorrectly
- end
- local x = pos.readLine()
- local y = pos.readLine()
- local z = pos.readLine()
- if(x == nil) then
- printError("x value nil, check position.txt file for syntax errors")
- dbPopulatedCorrectly = false
- end
- if(y == nil) then
- printError("y value nil, check position.txt file for syntax errors")
- dbPopulatedCorrectly = false
- end
- if(z == nil) then
- printError("z value nil, check position.txt file for syntax errors")
- dbPopulatedCorrectly = false
- end
- if(dbPopulatedCorrectly) then
- Coords.xCoord = tonumber(x)
- Coords.yCoord = tonumber(y)
- Coords.zCoord = tonumber(z)
- end
- pos.close()
- return dbPopulatedCorrectly
- end
- --Writes the positions to the position.txt file in /home/smart_quarry/database/position.txt
- --This is necessary to properly save the database values
- function database.StorePosition()
- local pos = fs.open("/smart_quarry/database/position.txt","w")
- pos.writeLine(Coords.xCoord)
- pos.writeLine(Coords.yCoord)
- pos.writeLine(Coords.zCoord)
- pos.close()
- end
- --Updates positions in database for x y and z Coords, takes 3 seperate integer values for x,y,z coordinates respectively
- function database.UpdatePosition(xCoord, yCoord, zCoord)
- Coords.xCoord = xCoord
- Coords.yCoord = yCoord
- Coords.zCoord = zCoord
- end
- return database
Advertisement
Add Comment
Please, Sign In to add comment