Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This dynamically built off of the save file so in theory
- --It doesn't need to be fully declared but this helps make
- --it a bit more clear.
- local state = {
- startingCoords = {
- x = nil,
- y = nil,
- z = nil
- },
- currentCoords = {
- x = nil,
- y = nil,
- z = nil
- },
- facing = nil,
- desiredDiameter = nil,
- useGPS = true
- }
- local saveFileName = "gpsState"
- local helpers = {
- split = function(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end,
- getInput = function(message)
- local input
- print(message)
- input = read()
- return input
- end,
- whereAmI = function()
- print("The turtle is at is x="..state.currentCoords.x..", y="..state.currentCoords.y..", z="..state.currentCoords.z)
- end,
- printObject = function(self, key, value)
- if type(value) == "table" then
- for subKey,subValue in pairs(value) do
- print(key.."={")
- self.printObject(self, subKey, subValue)
- print("}")
- end
- else
- print(key.."="..tostring(value))
- end
- end,
- clearScreen = function()
- term.clear()
- term.setCursorPos(1,1)
- end,
- getInputAndClearScreen = function(message)
- local input
- print(message)
- input = read()
- term.clear()
- term.setCursorPos(1,1)
- return input
- end,
- getCoordinate = function(message, self)
- local input
- if self == nil then
- error("getCoordinate is missing the argument helpers.")
- end
- print(message)
- input = read()
- if tonumber(input) == nil then
- print("That is not a number.")
- --Reflection to get a valid number
- return self.getCoordinate(message, self)
- else
- return input
- end
- end,
- saveObjectToFileAsJson = function(fileName, object)
- local file
- if fs.exists("json") ~= true then
- error("you do not have the json function. please enter 'pastebin get 4nRg9CHU json'")
- end
- if json == nil then
- os.loadAPI("json")
- end
- file = fs.open(fileName, "w")
- file.writeLine(json.encode(object))
- file.close()
- end,
- manualStartingPointEnter = function(self)
- local input
- if self == nil then
- error("manualStartingPointEnter is missing the argument helpers.")
- end
- self.clearScreen()
- state.startingCoords.x = self.getCoordinate("Please enter the starting x coordinate.", self)
- state.currentCoords.x = state.startingCoords.x
- self.clearScreen()
- state.startingCoords.y = self.getCoordinate("Please enter the starting y coordinate.", self)
- state.currentCoords.y = state.startingCoords.y
- self.clearScreen()
- state.startingCoords.z = self.getCoordinate("Please enter the starting z coordinate.", self)
- state.currentCoords.z = state.startingCoords.z
- self.clearScreen()
- self.whereAmI()
- input = self.getInput("Is the correct(yes/no)?")
- if input ~= "yes" then
- if input ~= "y" then
- self.manualStartingPointEnter()
- end
- end
- end,
- loadJsonObjectFromFile = function(fileName, object)
- local file, result, line
- if fs.exists("json") ~= true then
- error("you do not have the json function. please enter 'pastebin get 4nRg9CHU json'")
- end
- if json == nil then
- os.loadAPI("json")
- end
- result = json.decodeFromFile(fileName)
- for key,value in pairs(result) do
- object[key] = value
- end
- end,
- formatState = function()
- if (state.useGPS ~= nil) and (state.useGPS == 1) then
- state.useGPS = true
- elseif (state.useGPS ~= nil) then
- state.useGPS = false
- end
- if state.desiredDiameter ~= nil then
- state.desiredDiameter = tonumber(state.desiredDiameter)
- end
- end,
- loadCurrentLocation = function()
- local line
- local file = fs.open("CurrentLocation", "r")
- line = file.readLine(readLine)
- print("parseString in loadCurrentLocation")
- self.parseString(line, self, state)
- file.close()
- end,
- openRednetConnection = function()
- local sides = {"left","right","front","back","bottom","top"}
- for i=1, #sides do
- if pcall(rednet.open, sides[i]) then
- return
- end
- end
- error("could not find a wireless modem")
- end,
- closeRednetConnection = function()
- local sides = {"left","right","front","back","bottom","top"}
- for i=1, #sides do
- if rednet.isOpen(sides[i]) then
- rednet.close(sides[i])
- return
- end
- end
- error("could not find a open connection")
- end,
- objectContains = function(array, valueToCheck)
- for key,value in pairs(array) do
- if valueToCheck == value then
- return true
- end
- end
- return false
- end,
- tableLength = function(tableObject)
- local count = 0
- for key,value in pairs(tableObject) do
- count = count + 1
- end
- return count
- end
- }
- local smartTurtleClass = {
- run = function(self)
- error("Not implemented")
- end
- }
- local worldEaterTurtleClass = {
- run = function(self)
- local command, commanderID, distence, loop
- loop = true
- helpers.openRednetConnection()
- while loop do
- rednet.broadcast("ready and waiting")
- print("waiting for command")
- commanderID, command, distence = self.reciveCommand()
- if command == "dig" then
- print("begin digging down")
- elseif command == "refuel" then
- print("Check fuel level and refill if needed")
- elseif command == "done" then
- rednet.send(commanderID, "Shuttingdown")
- loop = false
- end
- end
- helpers.closeRednetConnection()
- end,
- reciveCommand = function()
- local commanderID, command, distence, loop
- loop = true
- while loop do
- commanderID, command, distence = rednet.receive()
- if string.find(command, "Command:") == 1 then
- loop = false
- return commanderID, string.sub(command, 9), distence
- end
- end
- end,
- goDown = function(self)
- local running = true
- while running do
- turtle.dig()
- turtle.digDown()
- if turtle.down() == false then
- turtle.attackDown()
- end
- end
- state.currentCoords.y = state.currentCoords.y - 1
- helpers.saveState()
- if state.currentCoords.y < 5 then
- running = false
- end
- end,
- goUp = function(self)
- local running = true
- while running do
- turtle.dig()
- turtle.digUp()
- if turtle.up() == false then
- turtle.attackUp()
- end
- end
- state.currentCoords.y = state.currentCoords.y + 1
- helpers.saveState()
- if state.currentCoords.y == state.startingCoords.y then
- running = false
- end
- end
- }
- local masterWorldeaterComputerClass = {
- --ToDo:
- -- Master listens to chat for:
- -- ServerShutdown 30 min or less
- -- Finish round and stop
- -- ServerShutDown 1 min or less
- -- Emergency stop
- -- Means the turtles will have to be listening to the master while running.
- -- Ranger1230 logged out
- -- Finish round and stop.
- -- Worldeater stop!
- -- Respond with "Finishing round sorry if I was lagging the server"
- -- If Sirloin says it finish round and pick up all chunkloaders
- -- If Sirloin says "Worldeater stop now!
- -- Emergency stop and pick up all chunkloaders
- -- Master waits for all slaves to finish around before:
- -- Tells slaves to refuel if needed.
- -- Tells slaves to begin next round.
- runningLoop = true,
- connections = {},
- run = function(self)
- helpers.openRednetConnection()
- print("Gathering connections")
- parallel.waitForAny(self.waitForInput, function() self.gatherConnections(self) end)
- print("Run master computer")
- --Use a parallel to listen to game chat for command and run the mining program
- self.eatTheWorld(self)
- helpers.closeRednetConnection()
- end,
- eatTheWorld = function(self)
- self.sendCommand(self, "done")
- end,
- sendCommand = function(self, command)
- for turtleID,value in pairs(self.connections) do
- rednet.send(turtleID, "Command:"..command, true)
- end
- end,
- waitForInput = function()
- local input
- input = read()
- helpers.clearScreen()
- print("input received")
- end,
- gatherConnections = function(self)
- local senderId, message, distance
- while true do
- senderId, message, distance = rednet.receive(10)
- if (message == "ready and waiting") and (distance < 1000) then
- self.connections[senderId] = true
- helpers.clearScreen()
- print("There are "..(helpers.tableLength(self.connections)).." turtles connected. Hit enter to run.")
- end
- end
- end
- }
- function getDiameter()
- local input = helpers.getInput("What diameter would you like to harvest?")
- diameter = tonumber(input)
- if diameter == nil then
- if input == "worldeater" then
- print("Entering Worldeater mode.")
- state.facing = nil
- state.diameter = nil
- state.currentCoords.x = state.startingCoords.x
- state.currentCoords.y = state.startingCoords.y
- state.currentCoords.z = state.startingCoords.z
- else
- print("Please enter a number.")
- getDiameter()
- end
- end
- end
- function runTurtle()
- if fs.exists(saveFileName) then
- helpers.loadJsonObjectFromFile(saveFileName, state)
- else
- x, y, z = gps.locate(5)
- if (x == nil) or (y == nil) or (z == nil) then
- print("No gps computer was detected. please enter current coordinates manually.")
- helpers.manualStartingPointEnter(helpers)
- state.useGPS = false
- end
- state.facing = 0
- getDiameter()
- helpers.saveObjectToFileAsJson(saveFileName, state)
- end
- if state.useGPS == true then
- worldEaterTurtleClass.run(worldEaterTurtleClass)
- else
- smartTurtleClass.run(smartTurtleClass)
- end
- end
- function runGpsComputer()
- if fs.exists(saveFileName) then
- helpers.loadJsonObjectFromFile(saveFileName, state)
- else
- helpers.manualStartingPointEnter(helpers)
- state.diameter = 0
- state.facing = 0
- helpers.saveObjectToFileAsJson(saveFileName, state)
- end
- shell.run("gps","host",state.startingCoords.x,state.startingCoords.y,state.startingCoords.z)
- end
- if turtle == nil then
- local loop, input
- loop = true
- while loop do
- input = helpers.getInput("Is this a GPS provider computer? (yes/no)")
- if (input == "yes") or (input == "y") then
- loop = false
- runGpsComputer()
- elseif (input == "no") or (input == "n") then
- loop = false
- masterWorldeaterComputerClass.run(masterWorldeaterComputerClass)
- end
- end
- else
- runTurtle()
- end
Advertisement
Add Comment
Please, Sign In to add comment