Advertisement
exa-byte

tBasePC v1.03

Dec 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. --MasterPC--never break it or you're fucked...
  2.  
  3. --variables--
  4.  
  5. local worker = {}
  6. local wtblin = 0                            -- "workerTableIndex" - for RednetListener()
  7. local rednetTimeout = 2
  8. local jobNumber = 0                         -- for GetJobForTurtle()
  9.  
  10. --functions--
  11.  
  12. function JobConstructor()                   --
  13.     job = {}
  14.     local i = 0
  15.     local length  --this is how many subjobs per job are given
  16.     local ilje = 1 --indexLastJobEnd
  17.     function DoNumberShit(_k)
  18.         --print(_k)
  19.         --print(i)
  20.         if _k == length then ilje = ilje + length end
  21.         i = i + 1
  22.     end
  23.  
  24.     --JobTurtlePlacer--
  25.     job[i] = "TurtlePlacer"
  26.     i = i + 1
  27.     ilje = 1
  28.    
  29.     -- --JobDigFirstTunnelRight
  30.     -- length = 7-1
  31.     -- for k = 0,length do
  32.         -- job[i] = "Diggerx1 2 0 " .. 3*(k)+1 .. " x 48"
  33.         -- DoNumberShit(k)
  34.     -- end
  35.    
  36.     -- --JobDigSecondTunnelRight at y = -1 --
  37.     -- length = 7-1
  38.     -- for k = 0, length do
  39.         -- job[i] = "Digger 4 -1 " .. 3*(k)+1 .. " x 46"
  40.         -- DoNumberShit(k)
  41.     -- end
  42.    
  43.     -- --[[ --JobDigFirstTunnel Straight at x = 3 --
  44.     -- length = 6
  45.     -- for k = 0, length do
  46.         -- job[i] = "Digger 4 -1 " .. 3*(k)+1 .. " y 49"
  47.         -- DoNumberShit(k)
  48.     -- end ]]
  49.    
  50.     --JobDigTerrain from (-2, 0, 0) to (50, 50, ~~20)
  51.     -- local xLength = 50
  52.     -- for k = -2, math.floor(xLength/2)*2-1, 2 do
  53.         -- for h = 0, 6 do
  54.             -- job[i] = "Digger " .. k .. " 0 " .. 3*(h)+1 .. " y 50"
  55.             -- DoNumberShit(k)
  56.         -- end
  57.     -- end
  58.     -- if xLength/2 == math.floor(xLength/2) then
  59.         -- for h = 0, 6 do
  60.             -- job[i] = "Diggerx1 " .. xLength .. " 0 " .. 3*(h)+1 .. " y 50"
  61.             -- i = i + 1
  62.         -- end
  63.     -- end
  64.    
  65.     --JobExcavate from (0, 1, 0) to (50, 50, ~~ground)
  66.     local sx = 0
  67.     local sy = 1
  68.     local sz = 0
  69.     local xLength = 50
  70.     local yLength = 50
  71.     local zLength = computerHeight - 1   --positive if excavating downwards
  72.     local zJobCount = math.floor(zLength/3)
  73.     for x = sx, sx+math.floor((xLength)/2)*2-1, 2 do
  74.         for z = sz, sz+zJobCount-1 do
  75.             job[i] = "Miner " .. sx+x .. " " .. sy .. " " .. sz-(3*z+2) .. " y " .. yLength
  76.             --print(job[i]) sleep(0.1)
  77.             i = i + 1
  78.         end
  79.     end
  80.     if (xLength)/2 == math.floor((xLength-sx)/2) then
  81.         for z = sz, sz+zJobCount-1 do
  82.             job[i] = "Minerx1 " .. xLength .. " 0 " .. -(3*z+2) .. " y " .. yLength
  83.             --print(job[i]) sleep(0.1)
  84.             i = i + 1
  85.         end
  86.     end
  87.    
  88.     --JobBuildFloor at height z with width x and depth y
  89.     local sx, sy, sz, sd = 0, 0, -1, "y"
  90.     local xLength = 50
  91.     local yLength = 50
  92.     local z = -1
  93.     local jobCount = math.floor(xLength/2)
  94.     for x = 0, math.floor(xLength/2)*2-1, 2 do
  95.         job[i] = "Floorx2 " .. x+sx .. " " .. sy .. " " .. sz .. " " .. sd .. " " .. yLength
  96.         --print(job[i]) sleep(0.5)
  97.         i = i + 1
  98.     end
  99.     if math.floor(xLength/2) == xLength/2 then
  100.         job[i] = "Floorx1 " .. xLength+sx .. " " .. sy .. " " .. sz .. " " .. sd .. " " .. yLength
  101.         --print(job[i]) sleep(0.5)
  102.         i = i + 1
  103.     end
  104.    
  105.     print("Total Jobs to do: " .. i)
  106. end
  107. function Split(str, pat)
  108.    local t = {}
  109.    local fpat = "(.-)" .. pat
  110.    local last_end = 1
  111.    local s, e, cap = str:find(fpat, 1)
  112.    while s do
  113.       if s ~= 1 or cap ~= "" then
  114.          table.insert(t,cap)
  115.       end
  116.       last_end = e+1
  117.       s, e, cap = str:find(fpat, last_end)
  118.    end
  119.    if last_end <= #str then
  120.       cap = str:sub(last_end)
  121.       table.insert(t, cap)
  122.    end
  123.    return t
  124. end
  125. function SendAndGetResponse(_recID, _message)
  126.     SendMessage(_recID, _message)
  127.     local startedTime = os.clock()
  128.     while (os.clock() - startedTime) <= rednetTimeout do
  129.         local id, msg, dis = rednet.receive(rednetTimeout)
  130.         if id == _recID or _recID == 0 then
  131.             local splitMessage = Split(msg, " ")
  132.             if (splitMessage[1] == "pa") then
  133.                 return msg
  134.             end
  135.         end
  136.     end
  137.     return false
  138. end
  139. function SendAndGetResponseNoTimeout(_recID, _message)
  140.     sendMessage(_recID ,_message)
  141.     while true do
  142.         local id, msg, dis = rednet.receive(0)
  143.         if id == _recID or _recID == 0 then
  144.             local splitMessage = split(msg, " ")
  145.             if (splitMessage[1] == "pa") then
  146.                 return msg
  147.             end
  148.         end
  149.     end
  150. end
  151. function SendMessage(_recID, _message)
  152.     rednet.send(_recID, _message)
  153. end
  154. function ConsoleInput()
  155.     print("Ready for console input!")
  156.     while true do
  157.         local cmd = Split(read(), " ")
  158.         if cmd[1] == "job" then
  159.             if cmd[2] == "do" then
  160.                 local function Arguments()
  161.                     local i = 4
  162.                     local arguments = ""
  163.                     while cmd[i] do
  164.                         arguments = arguments .. " " .. cmd[i]
  165.                     end
  166.                     i = i + 1
  167.                     return arguments
  168.                 end            
  169.                 if cmd[3] == "TurtlePlacer" or cmd[3] == "Digger" or cmd[3] == "Miner" then
  170.                     table.insert(job, jobNumber, cmd[3] .. Arguments())
  171.                 end
  172.             end
  173.         end
  174.     end
  175. end
  176. function RednetListener()
  177.     while true do
  178.         --print("Ready for RednetInput")
  179.         local id, message, distance = rednet.receive()
  180.         local splitMessage = Split(message, " ")
  181.         if splitMessage[1] == "pa" then
  182.             if splitMessage[2] == "setup" then
  183.                 if splitMessage[3] == "locate" then
  184.                     rednet.send(id, "pa setup masterHere")
  185.                     worker[wtblin] = id
  186.                     wtblin = wtblin + 1
  187.                     print("Worker" .. wtblin .. " listed")
  188.                     print("Has id " .. tonumber(worker[wtblin-1]))
  189.                 end
  190.             elseif splitMessage[2] == "job" then
  191.                 if splitMessage[3] == "request" then
  192.                     local job = GetJobForTurtle()
  193.                     print("Turtle " .. id .. " now has job " .. job)
  194.                     if SendAndGetResponse(id, "pa job do " .. job) ~= "pa job confirm" then
  195.                         jobNumber = jobNumber - 1
  196.                         print("JobRequestReceived - but not confirmed!!!")
  197.                     end
  198.                 end
  199.             end
  200.         end
  201.     end
  202. end
  203. function GetJobForTurtle()
  204.     jobNumber = jobNumber + 1
  205.     return job[jobNumber - 1]
  206. end
  207. function GetHeightFromPlayer()
  208.     print("Please type the computers actual height (look at minimap)")
  209.     computerHeight = tonumber(read())
  210.     while computerHeight < 1 or computerHeight > 255 do
  211.         print("must be between 1 and 255!")
  212.         computerHeight = tonumber(read())
  213.     end
  214.     print("computerHeight set to " .. computerHeight)
  215. end
  216.  
  217. --welcome message--
  218. rednet.open("top")
  219. print("Welcome to tBase!")
  220. GetHeightFromPlayer()
  221. JobConstructor()
  222. parallel.waitForAny(RednetListener, ConsoleInput) --enables rednetListener while be able to access command line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement