zefie icon

Minecraft OpenOS z_funcs (robot helper functions)

zefie | PRO | 08/07/17 11:46:53 PM UTC | 0 ⭐ | 766 👁️ | Never ⏰ | []
Lua |

3.04 KB

|

None

|

0 👍

/

0 👎

local c = require("component")
local r = require("robot")
local s = require("sides")
local z_funcs = {};
local keepSlot = {};
local selectedSlot = nil;
 
z_funcs.keepItems = {
  "ic2:electric_treetap",
  "ic2:treetap"
};
 
z_funcs.appname = "undefined";
 
function z_funcs.writeStr(str)
  io.write("["..z_funcs.appname.."] "..str.."\n");
  io.flush();
end
 
function z_funcs.moveVertical(side,count)
  local side = side or s.down;
  local count = count or 1;
  for i=1,count do
    if (side == s.up) then
      while r.detectUp() do   
        os.sleep(1);
      end
      r.up();
    end
    if (side == s.down) then
      while r.detectDown() do   
        os.sleep(1);
      end
      r.down();
    end
  end
end
 
function z_funcs.cachedSelect(slot)
  if slot ~= selectedSlot then
    r.select(slot)
    selectedSlot = slot
  end
end
 
function z_funcs.moveForward(count)
  local count = count or 1;
  for i=1,count do
    while r.detect() do   
      os.sleep(1);
    end
    r.forward();
  end
  return true;
end
 
function z_funcs.dropItems()
  if c.isAvailable("inventory_controller") then
    while not c.inventory_controller.getInventorySize(s.down) do
      z_funcs.writeStr("Cannot find chest?");
      os.sleep(5);
    end
  end
 
  -- Reset keepSlot
  keepSlot = {};
  if z_funcs.keepItems then
    for k,v in pairs(z_funcs.keepItems) do
      local keep = z_funcs.findInLocalInv(v);
      if keep then
        keepSlot[keep] = true;
      end
    end
  end
 
  z_funcs.writeStr("Putting items into chest...")
  for slot = 1, r.inventorySize() do
    while not keepSlot[slot] and r.count(slot) > 0 do
      z_funcs.cachedSelect(slot);
      r.dropDown();
    end
  end
  z_funcs.cachedSelect(1);
end
 
function z_funcs.getFromChest(itemName,localSlot,direction,count)
  local direction = direction or s.down;
  local localSlot = localSlot or 1;
  local count = count or 1;
 
  z_funcs.writeStr("Trying to get "..count.." "..itemName.." from chest...")
 
  if c.isAvailable("inventory_controller") then
    while not c.inventory_controller.getInventorySize(direction) do
      z_funcs.writeStr("Cannot find chest?");
      os.sleep(5);
    end
  end
 
  z_funcs.cachedSelect(localSlot);
  if r.count(localSlot) > 0 then
    for i=1,r.inventorySize() do
      if not i == localSlot then
        if r.count(i) == 0 then
          r.transferTo(i);
          break;   
        end
      end
    end
  end
 
  for i=1,c.inventory_controller.getInventorySize(direction) do
    local itemInfo = c.inventory_controller.getStackInSlot(direction,i);
    if itemInfo then
      if itemName == itemInfo.name then
        return c.inventory_controller.suckFromSlot(direction, i, count);
      end
    end
  end
  return false;
end
 
function z_funcs.findInLocalInv(itemName)
  for i=1,r.inventorySize() do
    local itemInfo = c.inventory_controller.getStackInInternalSlot(i);
    if itemInfo then
      if itemName == itemInfo.name then
        return i;
      end
    end
  end
end
 
return z_funcs;

Comments