exa-byte

tBasePC v1.01

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