Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --MasterPC--never break it or you're fucked...
- --variables--
- local worker = {}
- local wtblin = 0 -- "workerTableIndex" - for RednetListener()
- local rednetTimeout = 2
- local jobNumber = 0 -- for GetJobForTurtle()
- --functions--
- function JobConstructor() --
- job = {}
- local i = 0
- local length --this is how many subjobs per job are given
- local ilje = 1 --indexLastJobEnd
- function DoNumberShit(_k)
- --print(_k)
- --print(i)
- if _k == length then ilje = ilje + length end
- i = i + 1
- end
- --JobTurtlePlacer--
- job[i] = "TurtlePlacer"
- i = i + 1
- ilje = 1
- -- --JobDigFirstTunnelRight
- -- length = 7-1
- -- for k = 0,length do
- -- job[i] = "Diggerx1 2 0 " .. 3*(k)+1 .. " x 48"
- -- DoNumberShit(k)
- -- end
- -- --JobDigSecondTunnelRight at y = -1 --
- -- length = 7-1
- -- for k = 0, length do
- -- job[i] = "Digger 4 -1 " .. 3*(k)+1 .. " x 46"
- -- DoNumberShit(k)
- -- end
- -- --[[ --JobDigFirstTunnel Straight at x = 3 --
- -- length = 6
- -- for k = 0, length do
- -- job[i] = "Digger 4 -1 " .. 3*(k)+1 .. " y 49"
- -- DoNumberShit(k)
- -- end ]]
- --JobDigTerrain from (-2, 0, 0) to (50, 50, ~~20)
- -- local xLength = 50
- -- for k = -2, math.floor(xLength/2)*2-1, 2 do
- -- for h = 0, 6 do
- -- job[i] = "Digger " .. k .. " 0 " .. 3*(h)+1 .. " y 50"
- -- DoNumberShit(k)
- -- end
- -- end
- -- if xLength/2 == math.floor(xLength/2) then
- -- for h = 0, 6 do
- -- job[i] = "Diggerx1 " .. xLength .. " 0 " .. 3*(h)+1 .. " y 50"
- -- i = i + 1
- -- end
- -- end
- --JobExcavate from (0, 1, 0) to (50, 50, ~~ground)
- local sx = 0
- local sy = 1
- local sz = 0
- local xLength = 50
- local yLength = 50
- local zLength = computerHeight - 1 --positive if excavating downwards
- local zJobCount = math.floor(zLength/3)
- for x = sx, sx+math.floor((xLength)/2)*2-1, 2 do
- for z = sz, sz+zJobCount-1 do
- job[i] = "Miner " .. sx+x .. " " .. sy .. " " .. sz-(3*z+2) .. " y " .. yLength
- --print(job[i]) sleep(0.1)
- i = i + 1
- end
- end
- if (xLength)/2 == math.floor((xLength-sx)/2) then
- for z = sz, sz+zJobCount-1 do
- job[i] = "Minerx1 " .. xLength .. " 0 " .. -(3*z+2) .. " y " .. yLength
- --print(job[i]) sleep(0.1)
- i = i + 1
- end
- end
- --JobBuildFloor at height z with width x and depth y
- local sx, sy, sz, sd = 0, 0, -1, "y"
- local xLength = 50
- local yLength = 50
- local z = -1
- local jobCount = math.floor(xLength/2)
- for x = 0, math.floor(xLength/2)*2-1, 2 do
- job[i] = "Floorx2 " .. x+sx .. " " .. sy .. " " .. sz .. " " .. sd .. " " .. yLength
- --print(job[i]) sleep(0.5)
- i = i + 1
- end
- if math.floor(xLength/2) == xLength/2 then
- job[i] = "Floorx1 " .. xLength+sx .. " " .. sy .. " " .. sz .. " " .. sd .. " " .. yLength
- --print(job[i]) sleep(0.5)
- i = i + 1
- end
- print("Total Jobs to do: " .. i)
- end
- function Split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function SendAndGetResponse(_recID, _message)
- SendMessage(_recID, _message)
- local startedTime = os.clock()
- while (os.clock() - startedTime) <= rednetTimeout do
- local id, msg, dis = rednet.receive(rednetTimeout)
- if id == _recID or _recID == 0 then
- local splitMessage = Split(msg, " ")
- if (splitMessage[1] == "pa") then
- return msg
- end
- end
- end
- return false
- end
- function SendAndGetResponseNoTimeout(_recID, _message)
- sendMessage(_recID ,_message)
- while true do
- local id, msg, dis = rednet.receive(0)
- if id == _recID or _recID == 0 then
- local splitMessage = split(msg, " ")
- if (splitMessage[1] == "pa") then
- return msg
- end
- end
- end
- end
- function SendMessage(_recID, _message)
- rednet.send(_recID, _message)
- end
- function ConsoleInput()
- print("Ready for console input!")
- while true do
- local cmd = Split(read(), " ")
- if cmd[1] == "job" then
- if cmd[2] == "do" then
- local function Arguments()
- local i = 4
- local arguments = ""
- while cmd[i] do
- arguments = arguments .. " " .. cmd[i]
- end
- i = i + 1
- return arguments
- end
- if cmd[3] == "TurtlePlacer" or cmd[3] == "Digger" or cmd[3] == "Miner" then
- table.insert(job, jobNumber, cmd[3] .. Arguments())
- end
- end
- end
- end
- end
- function RednetListener()
- while true do
- --print("Ready for RednetInput")
- local id, message, distance = rednet.receive()
- local splitMessage = Split(message, " ")
- if splitMessage[1] == "pa" then
- if splitMessage[2] == "setup" then
- if splitMessage[3] == "locate" then
- rednet.send(id, "pa setup masterHere")
- worker[wtblin] = id
- wtblin = wtblin + 1
- print("Worker" .. wtblin .. " listed")
- print("Has id " .. tonumber(worker[wtblin-1]))
- end
- elseif splitMessage[2] == "job" then
- if splitMessage[3] == "request" then
- local job = GetJobForTurtle()
- print("Turtle " .. id .. " now has job " .. job)
- if SendAndGetResponse(id, "pa job do " .. job) ~= "pa job confirm" then
- jobNumber = jobNumber - 1
- print("JobRequestReceived - but not confirmed!!!")
- end
- end
- end
- end
- end
- end
- function GetJobForTurtle()
- jobNumber = jobNumber + 1
- return job[jobNumber - 1]
- end
- function GetHeightFromPlayer()
- print("Please type the computers actual height (look at minimap)")
- computerHeight = tonumber(read())
- while computerHeight < 1 or computerHeight > 255 do
- print("must be between 1 and 255!")
- computerHeight = tonumber(read())
- end
- print("computerHeight set to " .. computerHeight)
- end
- --welcome message--
- rednet.open("top")
- print("Welcome to tBase!")
- GetHeightFromPlayer()
- JobConstructor()
- parallel.waitForAny(RednetListener, ConsoleInput) --enables rednetListener while be able to access command line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement