Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require("robot")
- local os = require("os")
- local io = require("io")
- local term = require("term")
- local computer = require("computer")
- --[[defines misc variables]]
- programrunthrough = 0 --defines whether the program has finished filling the chunk
- --[[Defines variables for inventory state]]
- hasinventory = 1 --defines whether the inventory is empty or not
- invsize = robot.inventorySize() -- defines the inventory size of the robot
- maxe = computer.maxEnergy() - 400 --defines the max energey storage of the robot minus 400 because the robots don't always recharge 100%
- thirde = maxe / 3 --defines one third of the robots energy for the purpose of delineating when the robot is low on energy
- --[[Defines variables for mutliple robots and only doing part of a chunk]]
- robotstartpos = 1
- fillfullchunk = 1
- robotstartposdefined = 0
- fillfullchunkdefined = 0
- --[[Defines variables for tracking movement]]
- y = 0 --Longitudinal
- z = 0 --Vertical
- a = 0 --Lateral
- facing = 1 -- The longitudinal direction the robot is facing
- evena = true --defines whether 'a' variable is even or not
- --[[Redefines variables for tracking movement based on robotstartpos]]
- function startpostracking()
- print ("startpostracking start")
- if robotstartpos == 1 then
- y = 0
- a = 1
- evena = false
- elseif robotstartpos == 2 then
- y = -1
- a = 2
- evena = true
- end
- if robotstartpos == 1 and fillfullchunk == 1 then
- maxa = 16
- elseif robotstartpos == 1 and fillfullchunk == 0 then
- maxa = 8
- elseif robotstartpos == 2 and fillfullchunk == 1 then
- mina = 1
- elseif robotstartpos == 2 and fillfullchunk == 0 then
- mina = 9
- end
- end
- --[[Asks for user input to define variables at the start of the program]]
- function userinput()
- while robotstartposdefined == 0 do
- robotstartposfunc()
- end
- while fillfullchunkdefined == 0 do
- fillfullchunkfunc()
- end
- end
- function robotstartposfunc()
- term.write("Is this robot in position 1 or 2? - ")
- robotstartposinput = io.read()
- if robotstartposinput == "1" then
- robotstartpos = 1
- robotstartposdefined = 1
- elseif robotstartposinput == "2" then
- robotstartpos = 2
- robotstartposdefined = 1
- else
- robotstartposdefined = 0
- print("Invalid input")
- end
- print("position "..robotstartpos)
- end
- function fillfullchunkfunc()
- term.write("How many robots are you using 1 or 2? - ")
- fillfullchunkinput = io.read()
- if fillfullchunkinput == "1" then
- fillfullchunk = 1
- fillfullchunkdefined = 1
- elseif fillfullchunkinput == "2" then
- fillfullchunk = 0
- fillfullchunkdefined = 1
- else
- fillfullchunkdefined = 0
- print("Invalid input")
- end
- print("fillfullchunk is "..fillfullchunk)
- end
- --[[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]]
- function slotselect()
- print ("slotselect start")
- print ("inventory size is "..invsize)
- slot = 1
- while slot < invsize do
- if robot.count(slot) > 0 then
- robot.select(slot)
- print ("selecting slot "..slot)
- break
- else
- slot = slot + 1
- slotc = slot - 1
- print (slotc.." empty checking slot "..slot)
- end
- end
- if slot == invsize and robot.count(slot) > 0 then
- robot.select(slot)
- print ("selecting slot "..slot)
- elseif slot == invsize and robot.count(slot) == 0 then
- print ("Inventory Empty")
- hasinventory = 0
- print ("slotselect to returntostart")
- returntostart()
- end
- if computer.energy() <= thirde then
- print ("Low on energy")
- hasinventory = 0
- print ("slotselect to returntostart")
- returntostart()
- end
- end
- --[[Checks current inventory slot for items then either places one or starts the selectslot function]]
- function placed()
- print ("placed start")
- if robot.count() > 0 and computer.energy() > thirde then
- robot.placeDown()
- else
- print ("placed to slotselect")
- slotselect()
- end
- end
- --[[Moves the robot forward]]
- function forwardo()
- print ("forwardo start")
- if robot.detect() == false then
- robot.forward()
- y = y + 1
- else
- print ("I'm Blocked!")
- end
- end
- function righto()
- print ("righto start")
- if robot.detect() == false then
- robot.forward()
- a = a + 1
- evena = not evena
- print ("a is "..a)
- print ("evena is "..tostring (evena))
- else
- print ("I'm Blocked!")
- end
- end
- --[[Moves the robot backward]]
- function backwardo()
- print ("backwardo start")
- if robot.detect() == false then
- robot.forward()
- y = y - 1
- else
- print ("I'm Blocked!")
- end
- end
- --[[Fills in all empty blocks below robot (robot nerd pole)]]
- function nerdpole()
- print ("nerdpole start")
- while true do
- if robot.detectDown() == false and hasinventory == 1 then
- robot.down()
- z = z + 1
- elseif robot.detectDown() == true and hasinventory == 1 then
- robot.up()
- z = z - 1
- print ("nerdpole to placed")
- placed()
- else
- print ("nerdpole break")
- break
- end
- if z <= 0 then
- print ("nerdpole z axis break")
- break
- end
- end
- end
- --[[Fills in a line of 16 blocks up to the floor height of the robot]]
- function fillforward()
- print ("fillforward start")
- while true do
- if robot.detectDown() == true and y < 16 and hasinventory == 1 then
- print ("fillforward to forwardo")
- forwardo()
- print ("Forward 1")
- print (y)
- elseif y == 16 and robot.detectDown() == true then
- break
- elseif y == 16 and robot.detectDown() == false then
- print ("fillforward to nerdpole")
- nerdpole()
- break
- elseif hasinventory == 0 then
- print ("fillfoward break")
- break
- else
- print ("Nerdpole!")
- nerdpole()
- end
- end
- end
- --[[Fills in a line of 16 blocks up to the floor height of the robot but goes backwards instead of forwards]]
- function fillbackward()
- print ("fillbackward start")
- while true do
- if robot.detectDown() == true and y > 1 and hasinventory == 1 then
- print ("fillbackward to backwardo")
- backwardo()
- print ("backward 1")
- print (y)
- elseif y == 1 and robot.detectDown() == true then
- break
- elseif y == 1 and robot.detectDown() == false then
- print ("fillbackward to nerdpole")
- nerdpole()
- break
- elseif hasinventory == 0 then
- print ("fillbackward break")
- break
- else
- print ("Nerdpole!")
- nerdpole()
- end
- end
- end
- --[[Moves the robot to its right by one line facing -y]]
- function latright()
- print ("latright start")
- if hasinventory == 1 then
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- evena = not evena
- print ("evena = "..tostring (evena))
- if robotstartpos == 1 then
- facing = 0
- a = a + 1
- print ("Robot 1 at A "..a)
- elseif robotstartpos == 2 then
- facing = 1
- a = a - 1
- print ("Robot 2 at A "..a)
- end
- else
- end
- end
- --[[Moves the robot to its left by one line facing +y]]
- function latleft()
- print ("latleft start")
- if hasinventory == 1 then
- robot.turnLeft()
- robot.forward()
- robot.turnLeft()
- evena = not evena
- print ("evena = "..tostring (evena))
- if robotstartpos == 1 then
- facing = 1
- a = a + 1
- print ("Robot 1 at A "..a)
- elseif robotstartpos == 2 then
- facing = 0
- a = a - 1
- print ("Robot 2 at A "..a)
- end
- else
- end
- end
- --[[Turns robot towards start on longitudinal axis]]
- function turntostart()
- print ("turntostart start")
- if facing == 1 then
- robot.turnAround()
- facing = 0
- else
- end
- end
- --[[Robot returns to start]]
- function returntostart()
- print ("returntostart start")
- print ("returntostart to turntostart")
- turntostart()
- if robotstartpos == 1 then
- yb = y
- ab = a - 1
- elseif robotstartpos == 2 then
- yb = y + 1
- ab = a - 2
- end
- for zback = 1, z do
- robot.up()
- z = z - 1
- end
- for yback = 1, yb do
- robot.forward()
- y = y - 1
- end
- robot.turnRight()
- for aback = 1, ab do
- robot.forward()
- a = a - 1
- end
- if programrunthrough == 0 then
- refill()
- print ("returntostart to refill")
- elseif programrunthrough == 1 then
- print ("robot has finished")
- end
- end
- --[[Moves the robot from start position to the chunk]]
- function movetochunk()
- print ("movetochunk start")
- if robotstartpos == 1 then
- robot.turnRight()
- print("movetochunk to forwardo")
- forwardo()
- elseif robotstartpos == 2 then
- robot.turnAround()
- while a < 16 do
- righto()
- evena = not evena
- end
- robot.turnLeft()
- while y < 1 do
- print("movetochunk to forwardo")
- forwardo()
- end
- end
- print("movetochunk to fillchunk")
- fillchunk()
- end
- --[[refills the robots inventory and pauses to charge]]
- function refill()
- print ("refill start")
- for fill = 1, invsize do
- robot.suckUp()
- end
- while computer.energy() < maxe do
- os.sleep(5)
- end
- if robot.count(1) > 0 then
- hasinventory = 1
- robot.select(1)
- print ("refill to movetochunk")
- movetochunk()
- else
- print ("Refill station empty")
- end
- end
- --[[Tells the robot which lines to fill]]
- function fillchunk()
- print("fillchunk start")
- if robotstartpos == 1 and y == 1 and a == 1 and z == 0 then
- print("fillchunk to fillpos1")
- fillpos1()
- elseif robotstartpos == 2 and y == 1 and a == 16 and z == 0 then
- print("fillchunk to fillpos2")
- fillpos2()
- else
- print ("out of position to fillchunk")
- end
- end
- --[[Fills out the defined area from in chunk position1]]
- function fillpos1()
- print ("fillpost1 start")
- while true do
- if hasinventory == 1 and a < maxa and evena == false then
- print ("from fillpos1 to fillforward")
- fillforward()
- print ("from fillpos1 to latright")
- latright()
- elseif hasinventory == 1 and a < maxa and evena == true then
- print ("from fillpos1 to fillbackward")
- fillbackward()
- print ("from fillpos1 to latleft")
- latleft()
- elseif hasinventory == 1 and a == maxa and y > 1 then
- print ("from fillpos1 to fillbackward y < 1")
- fillbackward()
- elseif hasinventory == 1 and a == maxa and y == 1 then
- if robot.detectDown() == false then
- print ("from fillpos1 to nerdpole")
- nerdpole()
- else
- programrunthrough = 1
- print ("robot has finished returning to start")
- returntostart()
- break
- end
- elseif hasinventory == 0 then
- print ("fillpos1 inv break")
- break
- end
- end
- end
- --[[Fills out the defined area from in chunk position2]]
- function fillpos2()
- print ("fillpost2 start")
- while true do
- if hasinventory == 1 and a > mina and evena == true then
- print ("from fillpos2 to fillforward")
- fillforward()
- print ("from fillpos2 to latright")
- latleft()
- print ("from fillpos2 to latleft")
- elseif hasinventory == 1 and a > mina and evena == false then
- fillbackward()
- print ("from fillpos2 to fillbackward")
- latright()
- elseif hasinventory == 1 and a == mina and y > 1 then
- print ("from fillpos2 to fillbackward y > 1")
- fillbackward()
- elseif hasinventory == 1 and a == mina and y == 1 then
- if robot.detectDown() == false then
- print ("from fillpos2 to nerdpole")
- nerdpole()
- else
- programrunthrough = 1
- print ("robot has finished returning to start")
- returntostart()
- break
- end
- elseif hasinventory == 0 then
- print ("fillpos2 inv break")
- break
- end
- end
- end
- print ("main to userinput")
- userinput()
- print ("main to startpostracking")
- startpostracking()
- print ("main to refill")
- refill()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement