Guest User

miners

a guest
Jul 23rd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.95 KB | None | 0 0
  1. --new mining script for turtle drone
  2.  
  3.  
  4.  
  5. --declare variables for the entire code
  6. local locationFile --handle for the location file read and writes
  7. local deltaXNow,deltaYNow,deltaZNow,orientation --variables to hold change in coordinates from "home" location.
  8. local yHome --the "home" y level one block below the mining boss
  9. local modem --modem handle
  10. local listenPort
  11. local coroutineAction --what action should be taken during the coroutine call
  12. local turtleID --id of the turtle
  13. local miningStatus = 0--if the turtle is mining hold a variable for requests this is used in miningCoroutine to resume state
  14. local itemDetails --table to pass results of turtle.inspect() between coroutines
  15.  
  16. local junkItems = {} --table to hold all junk items
  17.  
  18. --create the table of junk items to not mine
  19. local junkFile = io.open("junk","r") --open the file junkFile
  20. repeat
  21.     junkItems[junkFile.read("*line") or "endOfLine"] = true --save the line from file as the key set value true until all lines are done then set key endOfLine true
  22. until junkItems["endOfLine"] --file is all imported
  23. io.close(junkFile) --close the file
  24.  
  25. ------
  26. --coroutines for movement without losing rednet messages.
  27. local actionCoroutine = coroutine.create(function(coroutineAction)
  28.     while true do
  29.         print("actionCoroutine called with "..coroutineAction)--move the turtle in the requested direction
  30.         --turtle orientation is n=1,e=2,s=3,w=4
  31.         if coroutineAction == "forward" then
  32.             while not turtle.forward() do --if the turtle cannot move forwards
  33.                 if turtle.detect() then --if there is a block in front
  34.                     if turtle.dig() then --dig it
  35.                         digTimerID = os.startTimer(0.35) --start a timer until the dig action completes
  36.                         coroutine.yield("digging") --yield the coroutine until the dig timer expires
  37.                     else
  38.                         --something is very wrong-----------------------------------------------------------------------------------------
  39.                     end
  40.                 elseif turtle.attack() then --if it hits a mob
  41.                     attackTimerID = os.startTimer(0.4) -- to prevent code hangs this loop may only execute once every 0.4 seconds
  42.                     coroutine.yield("attacking") --yield the coroutine until the attack timer fires
  43.                 end
  44.             end
  45.             updateLocation(1)
  46.             actionTimerID = os.startTimer(0.35) --turtle is now moving forwards successfully, 0.35s until move completion
  47.         elseif coroutineAction == "down" then
  48.             while not turtle.down() do --if the turtle cannot move down
  49.                 if turtle.detectDown() then --if there is a block below
  50.                     if turtle.digDown() then --then dig it
  51.                         digTimerID = os.startTimer(0.35)
  52.                         coroutine.yield("digging")
  53.                     else
  54.                         --assume bedrock
  55.                         --transmit to mining boss bedrock
  56.                         coroutine.resume(miningCoroutine,true)
  57.                         break --exit the while not turtle.down loop
  58.                     end
  59.                 elseif turtle.attackDown() then --if it hits a mob
  60.                     attackTimerID = os.startTimer(0.4) -- to prevent code hangs this loop may only execute once every 0.4 seconds
  61.                     coroutine.yield("attacking") --yield the coroutine until the attack timer fires
  62.                 end
  63.             end
  64.             deltaYNow = deltaYNow - 1
  65.             writeFileBackup()
  66.             actionTimerID = os.startTimer(0.35) --turtle is now moving down successfully, 0.35s until move completion
  67.         elseif coroutineAction == "up" then
  68.             while not turtle.up() do --if the turtle cannot move down
  69.                 if turtle.detectUp() then --if there is a block below
  70.                     if turtle.digUp() then --then dig it
  71.                         digTimerID = os.startTimer(0.35)
  72.                         coroutine.yield("digging")
  73.                     else
  74.                         --something is very very wrong
  75.                     end
  76.                 elseif turtle.attackUp() then --if it hits a mob
  77.                     attackTimerID = os.startTimer(0.4) -- to prevent code hangs this loop may only execute once every 0.4 seconds
  78.                     coroutine.yield("attacking") --yield the coroutine until the attack timer fires
  79.                 end
  80.             end
  81.             deltaYNow = deltaYNow + 1 --alter current displacement
  82.             writeFileBackup() --backup the change immediately
  83.             actionTimerID = os.startTimer(0.35) --turtle is now moving up successfully, 0.35s until move completion
  84.         elseif coroutineAction == "left" then
  85.             turtle.turnLeft() --turn left
  86.             orientation = (orientation - 1) % 4 --turtle is now facing to the left of previous
  87.             actionTimerID = os.startTimer(0.35) --turtle is now turning, 0.35s until move completion
  88.         elseif coroutineAction == "right" then     
  89.             turtle.turnRight() --turn right
  90.             orientation = (orientation + 1) % 4 --turtle is now facing to the right of previous        
  91.             actionTimerID = os.startTimer(0.35) --turtle is now turning, 0.35s until move completion
  92.         elseif coroutineAction == "inspect" then --turtle inspects the block in front          
  93. print("Inspect reached")           
  94. itemDetails = {turtle.inspect()} --see what's in front of the turtle
  95.         actionTimerID = os.startTimer(0.5)
  96. print("Timer set ID:"..actionTimerID)
  97. elseif coroutineAction == "dig" then --dig the block in front of the turtle
  98.             turtle.dig()
  99.             actionTimerID = os.startTimer(0.35) --0.35 till block is dug
  100.        
  101.        
  102.         elseif coroutineAction == "unloadInventory" then --the inventory is full empty it into an enderchest
  103.        
  104.         turtle.select(1)
  105.             if not turtle.placeUp() then --for some reason the turtle can't place the chest above it
  106.                 if turtle.detectUp() then --if there is a block above
  107.                     if turtle.digUp() then --then dig it
  108.                         digTimerID = os.startTimer(0.35)
  109.                         coroutine.yield("digging")
  110.                     else
  111.                         --something is very very wrong
  112.                     end
  113.                 elseif turtle.attackUp() then --if it hits a mob
  114.                     attackTimerID = os.startTimer(0.4) -- to prevent code hangs this loop may only execute once every 0.4 seconds
  115.                     coroutine.yield("attacking") --yield the coroutine until the attack timer fires
  116.                 end
  117.             end
  118.             placeTimerID = os.startTimer(0.35)
  119.             coroutine.yield()
  120.            
  121.             for i=2,16 do
  122.                 turtle.select(i)
  123.                 turtle.dropUp()
  124.                 placeTimerID = os.startTimer(0.5)
  125.                 coroutine.yield()
  126.             end
  127.             turtle.select(1) --move the selected slot back to 1
  128.             turtle.digUp() --collect the ender chest
  129.            
  130.             actionTimerID = os.startTimer(0.35) --item drop off is finished
  131.         end
  132.         coroutineAction = false
  133. --coroutine.yield()
  134.     end
  135. end)
  136.  
  137. local stage = false --is set to true when hitting bedrock in actionCoroutine
  138.  
  139. local miningCoroutine = coroutine.create(function(stage)
  140. print("miningCoroutine started")
  141.     local function checkAndMine() --this is only used inside the mining coroutine
  142.         print("check and mine function called")
  143. coroutine.resume(actionCoroutine,"inspect") --inspect the block in front
  144. print("inspect passed")    
  145. coroutine.yield()      
  146.         if not junkItems[itemDetails[2].name] then --run the actionCoroutine to see what the block in front is
  147.             coroutine.resume(actionCoroutine,"dig") --dig the non junk block
  148.             coroutine.yield()
  149.         end
  150.     end
  151.     print("starting first repeat") 
  152.     repeat
  153. print("just after first repeat miningStatus = "..miningStatus)
  154.         if  not (miningStatus > 1) then --if the miner has progressed past stage 1(or needs to skip turning for the first action)
  155. print("inside first repeat mine block 1")          
  156. checkAndMine()
  157.             coroutine.resume(actionCoroutine,"left")
  158.             coroutine.yield("waiting")
  159.         elseif miningStatus == 2 then --if it was only skip once then downgrade the status to 1
  160.             miningStatus = 1
  161.             writeFileBackup()
  162.         end
  163.         if not (miningStatus > 1) then --if the turtle has progressed past all of this section skip,       
  164.         checkAndMine()
  165.         coroutine.resume(actionCoroutine,"down")
  166.         coroutine.yield("waiting")
  167.             if stage then
  168.                 miningStatus = 3
  169.                 writeFileBackup()
  170.                 stage = false
  171.             end
  172.  
  173.         end
  174.        
  175.     until miningStatus >= 3 --this stage is done if the status is equal or higher than 3
  176.    
  177.     if not (miningStatus > 4) then
  178.         coroutine.resume(actionCoroutine,"right")
  179.         miningStatus = 4
  180.         writeFileBackup()
  181.         coroutine.yield("waiting")
  182.     elseif not (miningStatus > 5) then
  183.         coroutine.resume(actionCoroutine,"right")
  184.         miningStatus = 5
  185.         writeFileBackup()
  186.         coroutine.yield("waiting")
  187.     end
  188.    
  189.     repeat
  190.    
  191.         if miningStatus == 6 then
  192.             miningStatus = 5
  193.             writeFileBackup()
  194.         else
  195.             checkAndMine()
  196.             coroutine.resume(actionCoroutine,"right")
  197.             coroutine.yield("waiting")
  198.         end
  199.        
  200.         checkAndMine()
  201.         coroutine.resume(actionCoroutine,"up")
  202.         coroutine.yield("waiting")
  203.        
  204.     until yHome == deltaYNow
  205.     modem.transmit(2999,listenPort,"miningFinihed")
  206.     miningStatus = 0
  207.    
  208. end)
  209.  
  210.  
  211. --code functions
  212.  
  213. local function writeFileBackup() --write all the variables to file in case of unexpected shutdown
  214.  
  215.     locationFile = io.open("location","w") --open a new empty file, overwrite the previous file
  216.     locationFile.write(deltaXNow,"/n",deltaYNow,"/n",deltaZNow,"/n",orientation,"/n",turtleID,"/n",yHome,"/n",miningStatus) --write all the variables
  217.     io.close(LocationFile)
  218.    
  219. end
  220.  
  221. local direction -- passes parameter from function updateLocation
  222. local function updateLocation(direction) --pass +1 to move forwards and -1 to go backwards
  223. --based on orientation adjust the current location when moving "forwards"
  224.     if orientation == 0 then
  225.         deltaXNow = deltaZNow - direction
  226.     elseif orientation == 1 then
  227.         deltaXNow = deltaXNow - direction
  228.     elseif orientation == 2 then
  229.         deltaZNow = deltaZNow + direction
  230.     elseif orientation == 3 then
  231.         deltaXNow = deltaXNow + direction
  232.     end
  233.     writeFileBackup() --backup the change to file
  234. end
  235.  
  236.  
  237.  
  238. --------
  239. --run on startup
  240. --wrap the modem and begin listening
  241. local modem = peripheral.wrap("right") --wrap the peripheral on the right, (modem)
  242.  
  243.  
  244.  
  245. --load the saved delta from the store file locationFile
  246. locationFile=io.open("location","r")
  247. if locationFile == nil then --if the file does not exist then exit script, else save the handle
  248.     --for a turtle freshly placed with no assigned ID assume has been placed underneath the mining boss in the same orientation, this is important.
  249.     modem.open(3000) --listen on  channel 3000
  250.     modem.transmit(2999,3000,"new_miner")
  251.     local recievedTable = {} -- a table to hold all the received location and orientation data
  252.     for i = 1 , 5 do
  253.            
  254.         local eventQueue = {os.pullEventRaw("modem_message")} --pull only modem message events from the event Queue
  255.        
  256.         recievedTable[i] = tonumber(eventQueue[5])
  257.        
  258.     end
  259.    
  260.     deltaXNow = recievedTable[1]--pull stored locations from the received rednet
  261.     deltaYNow = recievedTable[2]
  262.     deltaZNow = recievedTable[3]
  263.     orientation = recievedTable[4]
  264.     turtleID = recievedTable[5]
  265.     yHome = deltaYNow
  266.     modem.close(3000) --this port is ONLY for initial config
  267.    
  268.    
  269. else
  270.        
  271.     deltaXNow = tonumber(locationFile.read("*line")) --pull stored locations from file
  272.     deltaYNow = tonumber(locationFile.read("*line")) --"
  273.     deltaZNow = tonumber(locationFile.read("*line")) --"
  274.     orientation = tonumber(locationFile.read("*line")) --"
  275.     turtleID = tonumber(locationFile.read("*line")) --"
  276.     yHome = tonumber(locationFile.read("*line"))
  277.     miningStatus = tonumber(locationFile.read("*line"))
  278.     io.close(locationFile) --close the file
  279.     print(miningStatus)
  280.     if miningStatus > 0 then --if the turtle shutdown mid mining then
  281.         print("miningstatus is > 0")--calculate exactly what point of the mining action it was doing
  282.         if ((((deltaYNow - 1) % 4) == orientation) and (miningStatus == 1)) then --if the turtle has already turned at this y level
  283.             miningStatus = 2 --skip the turn action and go down
  284.         elseif (not (((deltaYNow - 2) % 4) == orientation) and (miningstatus == 5)) then --if it has already turned comming up
  285.             miningStatus = 6 --skip the turn and move up
  286. end    
  287. coroutine.resume(miningCoroutine) --trigger the coroutine movement and all associated timers that should set the entire thing going
  288.    
  289.     end
  290. end
  291.  
  292. listenPort = turtleID --the listen port is the same as the turtle ID
  293. modem.open(listenPort) --open the listening port
  294.  
  295.  
  296.  
  297.  
  298. -- main code body here
  299.  
  300.     --declare local variables
  301.     local actionTimerID --numerical ID for action cooldown
  302.     local attackTimerID --numerical timer for attack action cooldown
  303.     local digTimerID --numerical timer for attack action cooldown
  304.     local placeTimerID
  305.     while true do
  306. print(actionTimerID)   
  307. local eventQueue = {os.pullEventRaw()}  --pull ALL events from the event Queue
  308.  --sleep(3)
  309.    print(eventQueue[1]," ",eventQueue[2]," ",eventQueue[3]," ",eventQueue[4])
  310.    
  311. if eventQueue[1] == "terminate" then -- if it is a user generated terminate event
  312.         modem.transmit(2999,listenPort,"terminate") --inform the boss that this turtle has terminated
  313.         print("Terminate Event") --print to log that this is a user triggered terminate
  314.         exit() --end
  315.     elseif eventQueue[1] == "timer" then --a timer has triggered
  316.         print("timer event switch")
  317. exit(0)
  318.         if eventQueue[2] == actionTimerID then --the turtle has finished the action and may act again
  319. print("action timer fired and found")          
  320. if miningStatus > 0 then --if the turtle is still mining down or up then
  321. print("turtle is mid mining")              
  322. coroutine.resume(miningCoroutine) --continue this action
  323.             else --report in all done
  324.                 --transmit to boss
  325.             end
  326.         elseif eventQueue[2] == attackTimerID then --the turtle has finished attacking and may attack again
  327.             coroutine.resume(actionCoroutine) --resume the coroutine on line 30
  328.            
  329.         elseif eventQueue[2] == digTimerID then --the turtle has finished digging so resume the coroutine
  330.             coroutine.resume(actionCoroutine) --resume the coroutine on line 24
  331.            
  332.         elseif eventQueue[2] == placeTimerID then --the turtle has finished digging so resume the coroutine
  333.             coroutine.resume(actionCoroutine) --resume the coroutine on line 115
  334.            
  335.         end
  336.        
  337.     elseif eventQueue[1] == "turtle_response" then --a java handled action has completed, the coroutine can now function again, pass the result to the coroutine to unlock it and begin a timer for the action to complete if it succeeded
  338. print("passback to coroutine")
  339.         coroutine.resume(actionCoroutine, unpack(eventQueue)) --results are handled in the coroutine
  340.        
  341.     elseif eventQueue[1] == "modem_message" then --orders received from the mining boss
  342.    
  343.         if eventQueue[5] == "mine" then --when the mining boss sends mine down
  344.             miningStatus = 1
  345.             writeFileBackup()
  346.             coroutine.resume(miningCoroutine) --start mining the column
  347.         end
  348.     end
  349.    
  350.  
  351.    
  352.  
  353. end
Advertisement
Add Comment
Please, Sign In to add comment