Last active
January 9, 2019 11:53
-
-
Save MEXAHOTABOP/16ba6444d127f11d9f40fc8926c67e16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
robot = component.proxy(component.list("robot")()) | |
inv_c = component.proxy(component.list("inventory_controller")()) | |
crafting = component.proxy(component.list("crafting")()) | |
bottom = 0 -- направления | |
top = 1 | |
back = 2 | |
front = 3 | |
right = 4 | |
left = 5 | |
around = 6 | |
wood_chest = bottom | |
coal_chest = front -- робот не может подбирать сзади и по сторонам( | |
charger = front | |
start_step = 2 | |
step = 4 -- расстояние между деревьями | |
sizeX = 3 -- размер поля в деревьях | |
sizeZ = 5 | |
inv_size = robot.inventorySize() - 3 --используемые слоты | |
craft_slot = inv_size + 1 | |
tool_slot = inv_size + 2 | |
sapling_slot= inv_size + 3 | |
localX = 0 -- координаты робота относительно старта | |
localY = 0 | |
localZ = 0 | |
dir = 0--[[ | |
0 = +z | |
1 = -x | |
2 = -z | |
3 = +x | |
]] | |
function tool_check() --проверка инструмента не нужна с пилой оставил для копипасты | |
end | |
function swing (side) | |
--tool_check() | |
if robot.detect(side) then | |
robot.swing(side) | |
robot.suck(side) | |
return true | |
end | |
return false | |
end | |
function turn(side) | |
local clockwise = side == right and true or false | |
robot.turn(clockwise) | |
dir = (dir + (clockwise and 1 or -1)) | |
if dir == 4 then dir = 0 end | |
if dir == -1 then dir = 3 end | |
if side == around then turn(left) end | |
end | |
function move(side) | |
if side == right or side == left or side == around then --с поворотом | |
turn(side) | |
side = front | |
end | |
if robot.move(side) then | |
if side == front or side == back then | |
local tmp_dir = dir | |
if side == back then tmp_dir = (tmp_dir + 2) % 4 end--назад инвертируем направление | |
if tmp_dir % 2 == 0 then | |
localZ = localZ + (tmp_dir == 0 and 1 or -1) | |
else | |
localX = localX + (tmp_dir == 3 and 1 or -1) | |
end | |
elseif side == bottom or side == top then | |
localY = localY + (side == top and 1 or -1) | |
end | |
return true | |
end | |
return false | |
end | |
function move_for(side, steps) | |
if side == left or side == right or side == around then | |
turn(side) | |
side = front | |
end | |
for i = 0, steps - 1 do | |
if not move(side) then return steps - i end | |
end | |
return 0 | |
end | |
function move_and_destroy(side,steps) | |
local steps_left = move_for(side,steps) | |
while steps_left > 0 do | |
swing(front) | |
steps_left = move_for(front, steps_left) | |
end | |
end | |
function drop_somewhere(side) | |
local chest_size = inv_c.getInventorySize(side) | |
local item = inv_c.getStackInInternalSlot() | |
if item == nil then return false end | |
local i_size = item.size | |
for j = 1, chest_size do | |
local item_ex = inv_c.getStackInSlot(side, j) | |
if item_ex == nil then | |
inv_c.dropIntoSlot(side, j) | |
break | |
elseif item.label == item_ex.label then | |
local to_drop = item_ex.maxSize - item_ex.size | |
if to_drop >= i_size then | |
inv_c.dropIntoSlot(side, j, i_size) | |
break | |
else | |
i_size = i_size - to_drop | |
inv_c.dropIntoSlot(side, j, to_drop) | |
end | |
end | |
end | |
return true | |
end | |
function chop_tree() --высота 1 | |
swing(bottom) | |
robot.select(sapling_slot) | |
robot.place(bottom) | |
while swing(top) do | |
move(top) | |
end | |
move_for(bottom,localY-1) | |
end | |
function chop_chop() | |
for x = 1, sizeX do | |
for z = 1, sizeZ do | |
if x == 1 and z == 1 then | |
move_and_destroy(front,start_step+1) | |
elseif z == 1 then | |
move_and_destroy(front,1) | |
else | |
move_and_destroy(front,step+1) | |
end | |
if robot.detect(top) then | |
chop_tree() | |
end | |
end | |
move(front) | |
turn_side = x % 2 == 0 and right or left | |
move_and_destroy(turn_side,step+1) | |
turn(turn_side) | |
end | |
end | |
function start() | |
robot.select(tool_slot) | |
turn(around) | |
inv_c.suckFromSlot(charger,1) -- подбираем пилу | |
turn(around) | |
inv_c.equip() | |
robot.select(sapling_slot) -- пополняем саженцы | |
local item = inv_c.getStackInInternalSlot() | |
if item == nil or item.name:find("sapling") == nil then | |
computer.shutdown() | |
end | |
if item.size < 32 then -- меньше пол стака | |
local chest_size = inv_c.getInventorySize(wood_chest) | |
local need = item.maxSize - item.size | |
for i = 1, chest_size do | |
local item_ex = inv_c.getStackInSlot(wood_chest, i) | |
if item_ex == nil then goto continue2 end | |
if item.label == item_ex.label then | |
if item_ex.size >= need then | |
inv_c.suckFromSlot(wood_chest, i, need) | |
break | |
else | |
need = need - item_ex.size | |
inv_c.suckFromSlot(wood_chest, i, item_ex.size) | |
end | |
end | |
::continue2:: | |
end | |
end | |
move(top) | |
end | |
function back_to_base() -- возврат на базу | |
turn(left) | |
move(front) | |
while dir ~= 2 do | |
turn(left) | |
end | |
move_and_destroy(front,localZ) | |
move_and_destroy(left,localX) | |
move(bottom) | |
turn(left) | |
if localZ ~=0 or localX ~= 0 or localY ~=0 or dir ~= 0 then -- защита от того что робот уйдёт с работы | |
computer.shutdown() | |
end | |
robot.select(tool_slot) -- зарядка пилы | |
inv_c.equip() | |
turn(around) | |
inv_c.dropIntoSlot(charger,1) | |
turn(around) | |
-- разгрузка дерева | |
for i = 1, inv_size do | |
robot.select(i) | |
drop_somewhere(wood_chest) | |
end | |
end | |
function sleep(sec) | |
local deadline = computer.uptime() + sec | |
while deadline > computer.uptime() do | |
computer.pullSignal(deadline - computer.uptime()) | |
end | |
end | |
function main() | |
start() | |
chop_chop() | |
back_to_base() | |
sleep(1200) | |
end | |
while true do | |
main() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment