Advertisement
Eladin

Untitled

Mar 8th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.25 KB | None | 0 0
  1. local robot = require("robot")
  2. local os = require("os")
  3. local io = require("io")
  4. local term = require("term")
  5. local computer = require("computer")
  6.  
  7.  
  8. --[[defines misc variables]]
  9. programrunthrough = 0 --defines whether the program has finished filling the chunk
  10.  
  11.  
  12. --[[Defines variables for inventory state]]
  13. hasinventory = 1 --defines whether the inventory is empty or not
  14. invsize = robot.inventorySize() -- defines the inventory size of the robot
  15. maxe = computer.maxEnergy() - 400 --defines the max energey storage of the robot minus 400 because the robots don't always recharge 100%
  16. thirde = maxe / 3 --defines one third of the robots energy for the purpose of delineating when the robot is low on energy
  17.  
  18. --[[Defines variables for mutliple robots and only doing part of a chunk]]
  19. robotstartpos = 1
  20. fillfullchunk = 1
  21. robotstartposdefined = 0
  22. fillfullchunkdefined = 0
  23.  
  24.  
  25. --[[Defines variables for tracking movement]]
  26. y = 0   --Longitudinal
  27. z = 0   --Vertical
  28. a = 0   --Lateral
  29. facing = 1 -- The longitudinal direction the robot is facing
  30. evena = true --defines whether 'a' variable is even or not
  31.  
  32.  
  33. --[[Redefines variables for tracking movement based on robotstartpos]]
  34. function startpostracking()
  35. print ("startpostracking start")
  36.  if robotstartpos == 1 then
  37.   y = 0
  38.   a = 1
  39.   evena = false
  40.  elseif robotstartpos == 2 then
  41.   y = -1
  42.   a = 2
  43.   evena = true
  44.  end
  45.  if robotstartpos == 1 and fillfullchunk == 1 then
  46.   maxa = 16
  47.  elseif robotstartpos == 1 and fillfullchunk == 0 then
  48.   maxa = 8
  49.  elseif robotstartpos == 2 and fillfullchunk == 1 then
  50.   mina = 1
  51.  elseif robotstartpos == 2 and fillfullchunk == 0 then
  52.   mina = 9
  53.  end
  54. end
  55.  
  56.  
  57. --[[Asks for user input to define variables at the start of the program]]
  58. function userinput()
  59.  while robotstartposdefined == 0 do
  60.   robotstartposfunc()
  61.  end
  62.  while fillfullchunkdefined == 0 do
  63.   fillfullchunkfunc()
  64.  end
  65. end
  66.  
  67.  
  68. function robotstartposfunc()
  69.  term.write("Is this robot in position 1 or 2? - ")
  70.  robotstartposinput = io.read()
  71.  if robotstartposinput == "1" then
  72.   robotstartpos = 1
  73.   robotstartposdefined = 1
  74.  elseif robotstartposinput == "2" then
  75.   robotstartpos = 2
  76.   robotstartposdefined = 1
  77.  else
  78.   robotstartposdefined = 0
  79.   print("Invalid input")
  80.  end
  81.  print("position "..robotstartpos)
  82. end
  83.  
  84.  
  85.  function fillfullchunkfunc()
  86.  term.write("How many robots are you using 1 or 2? - ")
  87.  fillfullchunkinput = io.read()
  88.  if fillfullchunkinput == "1" then
  89.   fillfullchunk = 1
  90.   fillfullchunkdefined = 1
  91.  elseif fillfullchunkinput == "2" then
  92.   fillfullchunk = 0
  93.   fillfullchunkdefined = 1
  94.  else
  95.   fillfullchunkdefined = 0
  96.   print("Invalid input")
  97.  end
  98.  print("fillfullchunk is "..fillfullchunk)
  99. end
  100.  
  101.  
  102. --[[Checks each slot until it finds one that isn't empty and then selects it. if all slots are empty it changes 'hasinventory' state to 0]]
  103.  
  104. function slotselect()
  105. print ("slotselect start")
  106.  print ("inventory size is "..invsize)
  107.  slot = 1
  108.  while slot < invsize do
  109.   if robot.count(slot) > 0 then
  110.    robot.select(slot)
  111.    print ("selecting slot "..slot)
  112.    break
  113.    else
  114.    slot = slot + 1
  115.    slotc = slot - 1
  116.    print (slotc.." empty checking slot "..slot)
  117.   end
  118.  end
  119.  if slot == invsize and robot.count(slot) > 0 then
  120.   robot.select(slot)
  121.   print ("selecting slot "..slot)
  122.  elseif slot == invsize and robot.count(slot) == 0 then
  123.   print ("Inventory Empty")
  124.   hasinventory = 0
  125. print ("slotselect to returntostart")
  126.   returntostart()
  127.  end
  128.  if computer.energy() <= thirde then
  129.    print ("Low on energy")
  130.   hasinventory = 0
  131. print ("slotselect to returntostart")
  132.   returntostart()
  133.  end
  134. end
  135.  
  136.  
  137. --[[Checks current inventory slot for items then either places one or starts the selectslot function]]
  138. function placed()
  139. print ("placed start")
  140.  if robot.count() > 0 and computer.energy() > thirde then
  141.   robot.placeDown()
  142.  else
  143.   print ("placed to slotselect")
  144.   slotselect()
  145.  end
  146. end
  147.  
  148.  
  149. --[[Moves the robot forward]]
  150. function forwardo()
  151. print ("forwardo start")
  152.  if robot.detect() == false then
  153.   robot.forward()
  154.   y = y + 1
  155.  else
  156.   print ("I'm Blocked!")
  157.  end
  158. end
  159.  
  160.  
  161. function righto()
  162. print ("righto start")
  163.  if robot.detect() == false then
  164.   robot.forward()
  165.   a = a + 1
  166.   evena = not evena
  167.   print ("a is "..a)
  168.   print ("evena is "..tostring (evena))
  169.  else
  170.   print ("I'm Blocked!")
  171.  end
  172. end
  173.  
  174.  
  175. --[[Moves the robot backward]]
  176. function backwardo()
  177. print ("backwardo start")
  178.  if robot.detect() == false then
  179.   robot.forward()
  180.   y = y - 1
  181.  else
  182.   print ("I'm Blocked!")
  183.  end
  184. end
  185.  
  186.  
  187. --[[Fills in all empty blocks below robot (robot nerd pole)]]
  188. function nerdpole()
  189. print ("nerdpole start")
  190.  while true do
  191.   if robot.detectDown() == false and hasinventory == 1 then
  192.    robot.down()
  193.    z = z + 1
  194.   elseif robot.detectDown() == true and hasinventory == 1 then
  195.    robot.up()
  196.    z = z - 1
  197.    print ("nerdpole to placed")
  198.    placed()
  199.   else
  200.    print ("nerdpole break")
  201.   break
  202.   end
  203.   if z <= 0 then
  204.    print ("nerdpole z axis break")
  205.   break
  206.   end
  207.  end
  208. end
  209.  
  210.  
  211. --[[Fills in a line of 16 blocks up to the floor height of the robot]]
  212. function fillforward()
  213. print ("fillforward start")
  214.  while true do
  215.   if robot.detectDown() == true and y < 16 and hasinventory == 1 then
  216.    print ("fillforward to forwardo")
  217.    forwardo()
  218.    print ("Forward 1")
  219.    print (y)
  220.   elseif y == 16 and robot.detectDown() == true then
  221.   break
  222.   elseif y == 16 and robot.detectDown() == false then
  223.    print ("fillforward to nerdpole")
  224.    nerdpole()
  225.   break
  226.   elseif hasinventory == 0 then
  227.    print ("fillfoward break")
  228.   break
  229.   else
  230.    print ("Nerdpole!")
  231.    nerdpole()
  232.   end
  233.  end
  234. end
  235.  
  236.  
  237. --[[Fills in a line of 16 blocks up to the floor height of the robot but goes backwards instead of forwards]]
  238. function fillbackward()
  239. print ("fillbackward start")
  240.  while true do
  241.    if robot.detectDown() == true and y > 1 and hasinventory == 1 then
  242.     print ("fillbackward to backwardo")
  243.     backwardo()
  244.     print ("backward 1")
  245.     print (y)
  246.    elseif y == 1 and robot.detectDown() == true then
  247.    break
  248.    elseif y == 1 and robot.detectDown() == false then
  249.    print ("fillbackward to nerdpole")
  250.     nerdpole()
  251.    break
  252.    elseif hasinventory == 0 then
  253.     print ("fillbackward break")
  254.    break
  255.    else
  256.     print ("Nerdpole!")
  257.     nerdpole()
  258.    end
  259.  end
  260. end
  261.  
  262.  
  263. --[[Moves the robot to its right by one line facing -y]]
  264. function latright()
  265. print ("latright start")
  266.  if hasinventory == 1 then
  267.   robot.turnRight()
  268.   robot.forward()
  269.   robot.turnRight()
  270.   evena = not evena
  271.   print ("evena = "..tostring (evena))
  272.   if robotstartpos == 1 then
  273.    facing = 0
  274.    a = a + 1
  275.    print ("Robot 1 at A "..a)
  276.   elseif robotstartpos == 2 then
  277.    facing = 1
  278.    a = a - 1
  279.    print ("Robot 2 at A "..a)
  280.   end
  281.  else
  282.  end
  283. end
  284.  
  285.  
  286. --[[Moves the robot to its left by one line facing +y]]
  287. function latleft()
  288. print ("latleft start")
  289.  if hasinventory == 1 then
  290.   robot.turnLeft()
  291.   robot.forward()
  292.   robot.turnLeft()
  293.   evena = not evena
  294.   print ("evena = "..tostring (evena))
  295.   if robotstartpos == 1 then
  296.    facing = 1
  297.    a = a + 1
  298.    print ("Robot 1 at A "..a)
  299.   elseif robotstartpos == 2 then
  300.    facing = 0
  301.    a = a - 1
  302.    print ("Robot 2 at A "..a)
  303.   end
  304.  else
  305.  end
  306. end
  307.  
  308.  
  309. --[[Turns robot towards start on longitudinal axis]]
  310. function turntostart()
  311. print ("turntostart start")
  312.  if facing == 1 then
  313.   robot.turnAround()
  314.   facing = 0
  315.  else
  316.  end
  317. end
  318.  
  319.  
  320. --[[Robot returns to start]]
  321. function returntostart()
  322. print ("returntostart start")
  323.  print ("returntostart to turntostart")
  324.  turntostart()
  325.   if robotstartpos == 1 then
  326.    yb = y
  327.    ab = a - 1
  328.   elseif robotstartpos == 2 then
  329.    yb = y + 1
  330.    ab = a - 2
  331.   end
  332.   for zback = 1, z do
  333.    robot.up()
  334.    z = z - 1
  335.   end
  336.    for yback = 1, yb do
  337.     robot.forward()
  338.     y = y - 1
  339.    end
  340.  robot.turnRight()
  341.   for aback = 1, ab do
  342.    robot.forward()
  343.    a = a - 1
  344.   end
  345.   if programrunthrough == 0 then
  346.    refill()
  347.    print ("returntostart to refill")
  348.   elseif programrunthrough == 1 then
  349.   print ("robot has finished")
  350.   end
  351. end
  352.  
  353.  
  354.  --[[Moves the robot from start position to the chunk]]
  355. function movetochunk()
  356. print ("movetochunk start")
  357.  if robotstartpos == 1 then
  358.   robot.turnRight()
  359.  print("movetochunk to forwardo")
  360.   forwardo()
  361.  elseif robotstartpos == 2 then
  362.   robot.turnAround()
  363.   while a < 16 do
  364.    righto()
  365.    evena = not evena
  366.   end
  367.   robot.turnLeft()
  368.   while y < 1 do
  369.    print("movetochunk to forwardo")
  370.    forwardo()
  371.   end
  372.  end
  373.  print("movetochunk to fillchunk")
  374.  fillchunk()
  375. end
  376.  
  377.  
  378. --[[refills the robots inventory and pauses to charge]]
  379. function refill()
  380. print ("refill start")
  381.  for fill = 1, invsize do
  382.  robot.suckUp()
  383.  end
  384.  while computer.energy() < maxe do
  385.   os.sleep(5)
  386.   end
  387.  if robot.count(1) > 0 then
  388.   hasinventory = 1
  389.   robot.select(1)
  390.   print ("refill to movetochunk")
  391.   movetochunk()
  392.  else
  393.   print ("Refill station empty")
  394.  end
  395. end
  396.  
  397. --[[Tells the robot which lines to fill]]
  398. function fillchunk()
  399. print("fillchunk start")
  400.  if robotstartpos == 1 and y == 1 and a == 1 and z == 0 then
  401.   print("fillchunk to fillpos1")
  402.   fillpos1()
  403.  elseif robotstartpos == 2 and y == 1 and a == 16 and z == 0 then
  404.   print("fillchunk to fillpos2")
  405.   fillpos2()
  406.  else
  407.  print ("out of position to fillchunk")
  408.  end
  409. end
  410.  
  411.  
  412. --[[Fills out the defined area from in chunk position1]]
  413. function fillpos1()
  414. print ("fillpost1 start")
  415.  while true do
  416.   if hasinventory == 1 and a < maxa and evena == false then
  417.    print ("from fillpos1 to fillforward")
  418.    fillforward()
  419.    print ("from fillpos1 to latright")
  420.    latright()
  421.   elseif hasinventory == 1 and a < maxa and evena == true then
  422.    print ("from fillpos1 to fillbackward")
  423.    fillbackward()
  424.    print ("from fillpos1 to latleft")
  425.    latleft()
  426.   elseif hasinventory == 1 and a == maxa and y > 1 then
  427.   print ("from fillpos1 to fillbackward y < 1")
  428.    fillbackward()
  429.   elseif hasinventory == 1 and a == maxa and y == 1 then
  430.    if robot.detectDown() == false then
  431.    print ("from fillpos1 to nerdpole")
  432.    nerdpole()
  433.    else
  434.    programrunthrough = 1
  435.    print ("robot has finished returning to start")
  436.    returntostart()
  437.    break
  438.    end
  439.   elseif hasinventory == 0 then
  440.    print ("fillpos1 inv break")
  441.    break
  442.   end
  443.  end
  444. end
  445.  
  446.  
  447. --[[Fills out the defined area from in chunk position2]]
  448. function fillpos2()
  449. print ("fillpost2 start")
  450.  while true do
  451.   if hasinventory == 1 and a > mina and evena == true then
  452.    print ("from fillpos2 to fillforward")
  453.    fillforward()
  454.    print ("from fillpos2 to latright")
  455.    latleft()
  456.    print ("from fillpos2 to latleft")
  457.   elseif hasinventory == 1 and a > mina and evena == false then
  458.    fillbackward()
  459.    print ("from fillpos2 to fillbackward")
  460.    latright()
  461.   elseif hasinventory == 1 and a == mina and y > 1 then
  462.   print ("from fillpos2 to fillbackward y > 1")
  463.    fillbackward()
  464.   elseif hasinventory == 1 and a == mina and y == 1 then
  465.    if robot.detectDown() == false then
  466.    print ("from fillpos2 to nerdpole")
  467.    nerdpole()
  468.    else
  469.    programrunthrough = 1
  470.    print ("robot has finished returning to start")
  471.    returntostart()
  472.    break
  473.    end
  474.   elseif hasinventory == 0 then
  475.    print ("fillpos2 inv break")
  476.    break
  477.   end
  478.  end
  479. end
  480.  
  481.  
  482. print ("main to userinput")
  483. userinput()
  484. print ("main to startpostracking")
  485. startpostracking()
  486. print ("main to refill")
  487. refill()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement