Advertisement
Nokiyen

transport

Jun 14th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. --[[
  2. ***********
  3.  * transport
  4.  *
  5. **********
  6. ]]
  7.  
  8.  
  9. ----------
  10. -- Variables.
  11. ----------
  12. -- define vars.
  13. local refuelLevel = 5000
  14. local waitInterval = 3
  15. local waitDif = 1
  16.  
  17. local chest1st  = {-214, 71, 687, 3}
  18. local chest1stID = 600
  19. local fuel1st = {-210, 71, 687, 1}
  20. local fuel1stID = 597
  21. local chest2nd  = {-214, 81, 687, 3}
  22. local chest2ndID = 608
  23. local fuel2nd = {-210, 81, 687, 1}
  24. local fuel2ndID = 609
  25.  
  26. local birchSpSt = 15
  27. local birchSpCh = {-214, 70, 676, 1}
  28.  
  29. local birchWdSt = 16
  30. local birchWdCh = {-214, 70, 684, 1}
  31.  
  32. local coalCh = {-214, 70, 686, 1}
  33. local trashPos = {-214, 70, 673, 2}
  34.  
  35. local storeID = 606
  36.  
  37. -- create move instance.
  38. dofile("move")
  39. local moveObj, error_msg = Move.new()
  40. if moveObj == false then
  41.     print("Something wrong. Can't create Move obj.")
  42.     print(error_msg)
  43.     return
  44. end
  45.  
  46.  
  47. ----------
  48. -- Functions.
  49. ----------
  50. function collect(collectID, storeID, storeTarget, pos)
  51.     rednet.send(collectID, "check")
  52.     local id, msg = rednet.receive()
  53.     if tonumber(msg) == 0 then
  54.         return false
  55.     end
  56.    
  57.     rednet.send(storeID, storeTarget)
  58.     id, msg = rednet.receive()
  59.     if tonumber(msg) >= 13 then
  60.         return false
  61.     end
  62.    
  63.     goToPoint(pos[1], pos[2], pos[3], pos[4])
  64.     turtle.select(1)
  65.     while turtle.suck() do
  66.     end
  67.     return true
  68. end
  69.  
  70. function store(slot, chest)
  71.         returnToPoint(chest[1], chest[2], chest[3], chest[4])
  72.         for i=1, 8, 1 do
  73.             turtle.select(i)
  74.             if turtle.compareTo(slot) then
  75.                 turtle.drop()
  76.             end
  77.         end
  78.         turtle.select(1)
  79. end
  80.  
  81. function deliver(id, pos)
  82.     rednet.send(id, "check")
  83.     local id, msg = rednet.receive()
  84.     if tonumber(msg) >= 5 then
  85.         return false
  86.     end
  87.    
  88.     moveObj:turnLeft()
  89.     local res = 0
  90.     for i=1, 8, 1 do
  91.         res = res + boolToNum(turtle.suck())
  92.     end
  93.     moveObj:turnRight()
  94.     if res == 0 then
  95.         return false
  96.     end
  97.    
  98.     goToPoint(pos[1], pos[2], pos[3], pos[4])
  99.     for i=1, 8, 1 do
  100.         turtle.select(i)
  101.         turtle.drop()
  102.     end
  103.     returnToIniPoint()
  104.     return true
  105. end
  106.  
  107. function goToPoint(posX, posY, posZ, dir)
  108.     local distX, distY, distZ = moveObj:getDistances(posX, posY, posZ)
  109.    
  110.     moveObj:goStraightX(distX)
  111.     moveObj:goStraightZ(distZ)
  112.     moveObj:goStraightY(distY)
  113.     moveObj:rotate(dir)
  114. end
  115.  
  116. function returnToPoint(posX, posY, posZ, dir)
  117.     local distX, distY, distZ = moveObj:getDistances(posX, posY, posZ)
  118.    
  119.     moveObj:goStraightY(distY)
  120.     moveObj:goStraightZ(distZ)
  121.     moveObj:goStraightX(distX)
  122.     moveObj:rotate(dir)
  123. end
  124.  
  125. function returnToIniPoint()
  126.     returnToPoint(moveObj.initialPos[1], moveObj.initialPos[2], moveObj.initialPos[3], moveObj.initialPos[4])
  127. end
  128.  
  129. function refuel()
  130.     if turtle.getFuelLevel() < refuelLevel then
  131.         turtle.select(1)
  132.         moveObj:turnLeft()
  133.         for i=1, 8, 1 do
  134.             turtle.suck()
  135.         end
  136.         moveObj:turnRight()
  137.         for i=1, 8, 1 do
  138.             turtle.select(1)
  139.             turtle.refuel()
  140.         end
  141.         turtle.select(1)
  142.     end
  143.    
  144.     if turtle.getFuelLevel() < refuelLevel then
  145.         return false
  146.     else
  147.         return true
  148.     end
  149. end
  150.  
  151. function getTime()
  152.     local hour, min = math.modf(os.time())
  153.     min = math.floor( min * 60 )
  154.    
  155.     local time = os.day()..":"..hour..":"..min
  156.     return time
  157. end
  158.  
  159. function boolToNum(bool)
  160.     if bool == true then
  161.         return 1
  162.     else
  163.         return 0
  164.     end
  165. end
  166.  
  167. function makeTableLine1(startTime)
  168.     local timeStr = getTime()
  169.     local str = "|"..startTime.." -> "..timeStr
  170.     for i=1, 35 - string.len(timeStr), 1 do
  171.         str = str.." "
  172.     end
  173.     str = str.."|"
  174.  
  175.     return str
  176. end
  177.  
  178. function makeTableLine2(type, res1, res2, res3, res4, res5)
  179.     local str1 = getResultStr(res1)
  180.     local str2 = getResultStr(res2)
  181.     local str3 = getResultStr(res3)
  182.     local str4 = getResultStr(res4)
  183.     local str5 = getResultStr(res5)
  184.  
  185.     return "|"..type.."| "..str1.." | "..str2.." | "..str3.." | "..str4.." | "..str5.." |"
  186. end
  187.  
  188. function getResultStr(bool)
  189.     if bool == true then
  190.         return "ooo"
  191.     else
  192.         return "xxx"
  193.     end
  194. end
  195.  
  196. function getWaitTime(interval, givenDif)
  197.     local dif = givenDif or 0
  198.  
  199.     local hour, min = math.modf(os.time())
  200.     local totalTime = os.day()*24 + hour + 12*dif
  201.     local residue = (12*interval) - totalTime % (12*interval)
  202.  
  203.     return residue * 50
  204. end
  205.  
  206.  
  207. ----------
  208. -- Main Thread.
  209. ----------
  210. term.clear()
  211. term.setCursorPos(1,1)
  212. term.write("Wood Cutting System")
  213. term.setCursorPos(1,3)
  214. term.write("|     |grand| 1st | 2nd | 3rd | 4th |")
  215.  
  216. returnToIniPoint()
  217. rednet.open("left")
  218. sleep(getWaitTime(waitInterval, waitDif))
  219. while true do
  220.     local startTime = getTime()
  221.     local flag02 = false
  222.     local flag11 = false
  223.     local flag12 = false
  224.     local flag21 = false
  225.     local flag22 = false
  226.     local flag31 = false
  227.     local flag32 = false
  228.     local flag41 = false
  229.     local flag42 = false
  230.    
  231.     flag02 = refuel()
  232.     if flag02 == true then
  233.         flag11 = collect(chest1stID, storeID, "coal", chest1st)
  234.         flag21 = collect(chest2ndID, storeID, "birchWd", chest2nd)
  235.         if flag11 == true or flag21 == true then
  236.             store(birchWdSt, coalCh)
  237.             store(birchWdSt, birchWdCh)
  238.             store(birchSpSt, birchSpCh)
  239.             store(birchSpSt, trashPos)
  240.             returnToIniPoint()
  241.         end
  242.         flag12 = deliver(fuel1stID, fuel1st)
  243.         flag22 = deliver(fuel2ndID, fuel2nd)
  244.     end
  245.  
  246.     term.scroll(3)
  247.     term.setCursorPos(1,1)
  248.     term.clearLine()
  249.     term.write("Wood Cutting System")
  250.     term.setCursorPos(1,2)
  251.     term.clearLine()
  252.     term.setCursorPos(1,3)
  253.     term.clearLine()
  254.     term.write("|     |grand| 1st | 2nd | 3rd | 4th |")
  255.  
  256.     term.setCursorPos(1,10)
  257.     term.clearLine()
  258.     term.write(makeTableLine1(startTime))
  259.     term.setCursorPos(1,11)
  260.     term.clearLine()
  261.     term.write(makeTableLine2("chest", false, flag11, flag21, flag31, flag41))
  262.     term.setCursorPos(1,12)
  263.     term.clearLine()
  264.     term.write(makeTableLine2("fuel ", flag02, flag12, flag22, flag32, flag42))
  265.  
  266.     sleep(getWaitTime(waitInterval, waitDif))
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement