Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
DBevillJr

Robot Wont Cut Tree

Question

Hello,

 

I am using a script that is not mine, as I have no idea how to write one yet, and it does everything that it is suppose to do except for cutting the full grown trees down. I am trying to figure out why but am very unsuccessfully doing so.

here is the code i am using, any help would be appreciated. 

 

local W, H = 7, 11 
local INTERVAL = 30 
local DROPTOOL = false 
 
local com = require("component") 
local robot = require("robot") 
local term = require("term") 
local comp = require("computer") 
 
local magnet = com.tractor_beam 
local inv = com.inventory_controller 
local robocom = com.robot 
 
local INV = robot.inventorySize() 
 
local wrappers = { 
  rpt = function(f, to) 
    to = to or .25 
    return function(...) 
      while not f(...) == true do 
        os.sleep(to) 
      end 
      os.sleep(0) 
    end 
  end, 
  detect = function(f, to) 
    to = to or .25 
    return function(...) 
      while true do 
        local s, rsn = f(...) 
        if s then return true end 
        if rsn == "entity" then 
          robot.swing() 
        elseif rsn == "air" then 
          return true 
        elseif rsn == "nothing selected" then 
          return false 
        else 
          os.sleep(to) 
        end 
      end 
    end 
  end 

 
local r = { 
  fwd = wrappers.detect(robot.forward), 
  back = wrappers.rpt(robot.back), 
  left = wrappers.rpt(robot.turnLeft), 
  right = wrappers.rpt(robot.turnRight), 
  around = wrappers.rpt(robot.turnAround), 
  up = wrappers.rpt(robot.up), 
  down = wrappers.rpt(robot.down), 
  suck = function() 
    robot.select(INV) 
    while magnet.suck() do 
      os.sleep(0.05) 
    end 
    if robot.count() ~= 64 then 
      for i = 1, INV, 1 do 
        if robot.count(i) > 0 then 
          if robocom.compareTo(i) then 
            robot.select(i) 
            robocom.transferTo(INV) 
            robot.select(INV) 
          end 
        end 
      end 
    end 
  end, 
  swing = wrappers.detect(function() 
    if robot.durability() > .1 then 
      return robot.swing() 
    end 
    return true 
  end), 
  place = wrappers.detect(robot.place) 

local function row() 
  r.right() 
  for i = 1, H, 1 do 
    r.fwd() 
    r.left() 
    robot.select(INV - 1) 
    if robocom.compare(3) and robot.durability() and robot.durability() > .1 then 
      robot.select(INV) 
      r.swing() 
    end 
    if not robot.detect() and robot.count(INV) > 1 then 
      robot.select(INV) 
      r.place() 
    end 
    robot.select(INV - 1) 
    r.around() 
    if robocom.compare(3) and robot.durability() and robot.durability() > .1 then 
      robot.select(INV) 
      r.swing() 
    end 
    r.suck() 
    if not robot.detect() and robot.count(INV) > 1 then 
      robot.select(INV) 
      r.place() 
    end 
    robot.select(INV - 1) 
    r.left() 
    if i ~= H then 
      r.fwd() 
    end 
  end 
  for i = 1, H, 1 do 
    r.back() 
    if i % 2 == 0 then 
      -- collect the saplings when going back 
      -- useful when fastleafdecay is installed 
      r.suck() 
    end 
    if i ~= H then 
      r.back() 
    end 
  end 
end 
 
local function dropAll() 
  for i = 1, INV - 2, 1 do 
    if robot.count(i) > 0 then 
      robot.select(i) 
      robot.drop() 
    end 
  end 
end 
 
local function field() 
  if DROPTOOL then 
    robot.select(1) 
    robot.suckDown() 
    inv.equip() 
  end 
  robot.select(INV - 1) 
  for i = 1, W, 1 do 
    r.fwd() 
    row() 
    if i ~= W then 
      r.left() 
      for fw = 1, 3, 1 do 
        r.fwd() 
      end 
    end 
  end 
  r.right() 
  for i = 1, W - 1, 1 do 
    for fw = 1, 4 do 
      r.fwd() 
    end 
  end 
  r.fwd() 
  dropAll() 
  r.around() 
  if DROPTOOL then 
    robot.select(1) 
    inv.equip() 
    robot.dropDown() 
  end 
end 
 
while true do 
  local x, y = term.getCursor() 
  term.clearLine() 
  term.setCursor(x, y) 
  io.write("Running") 
  field() 
  if type(INTERVAL) == "number" then 
    for i = 0, INTERVAL, 1 do 
      term.setCursor(x, y) 
      term.clearLine() 
      io.write("Sleep: " .. i .. " out of " .. INTERVAL) 
      os.sleep(1) 
    end 
  else 
   local time = 0 
    repeat 
      term.setCursor(x, y) 
      term.clearLine() 
      io.write("Sleep: " .. time .. " (" .. comp.energy() .. "/" .. comp.maxEnergy() .. ")") 
      os.sleep(1) 
      time = time + 1 
    until comp.energy() / comp.maxEnergy() > .98 
  end 
  os.sleep(0) 
  term.setCursor(x, y) 
end 
 

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
1 hour ago, Fingercomp said:

Insert saplings in the bottom-right slot of robot inventory, and a log in the previous one. The program compares blocks in front of robot with contents of these slots to distinguish saplings from trees.

This worked. Thank you very much.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.