Advertisement
zefie

Minecraft OpenOS z_funcs (robot helper functions)

Aug 7th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. local c = require("component")
  2. local r = require("robot")
  3. local s = require("sides")
  4. local z_funcs = {};
  5. local keepSlot = {};
  6. local selectedSlot = nil;
  7.  
  8. z_funcs.keepItems = {
  9.   "ic2:electric_treetap",
  10.   "ic2:treetap"
  11. };
  12.  
  13. z_funcs.appname = "undefined";
  14.  
  15. function z_funcs.writeStr(str)
  16.   io.write("["..z_funcs.appname.."] "..str.."\n");
  17.   io.flush();
  18. end
  19.  
  20. function z_funcs.moveVertical(side,count)
  21.   local side = side or s.down;
  22.   local count = count or 1;
  23.   for i=1,count do
  24.     if (side == s.up) then
  25.       while r.detectUp() do  
  26.         os.sleep(1);
  27.       end
  28.       r.up();
  29.     end
  30.     if (side == s.down) then
  31.       while r.detectDown() do  
  32.         os.sleep(1);
  33.       end
  34.       r.down();
  35.     end
  36.   end
  37. end
  38.  
  39. function z_funcs.cachedSelect(slot)
  40.   if slot ~= selectedSlot then
  41.     r.select(slot)
  42.     selectedSlot = slot
  43.   end
  44. end
  45.  
  46. function z_funcs.moveForward(count)
  47.   local count = count or 1;
  48.   for i=1,count do
  49.     while r.detect() do  
  50.       os.sleep(1);
  51.     end
  52.     r.forward();
  53.   end
  54.   return true;
  55. end
  56.  
  57. function z_funcs.dropItems()
  58.   if c.isAvailable("inventory_controller") then
  59.     while not c.inventory_controller.getInventorySize(s.down) do
  60.       z_funcs.writeStr("Cannot find chest?");
  61.       os.sleep(5);
  62.     end
  63.   end
  64.  
  65.   -- Reset keepSlot
  66.   keepSlot = {};
  67.   if z_funcs.keepItems then
  68.     for k,v in pairs(z_funcs.keepItems) do
  69.       local keep = z_funcs.findInLocalInv(v);
  70.       if keep then
  71.         keepSlot[keep] = true;
  72.       end
  73.     end
  74.   end
  75.  
  76.   z_funcs.writeStr("Putting items into chest...")
  77.   for slot = 1, r.inventorySize() do
  78.     while not keepSlot[slot] and r.count(slot) > 0 do
  79.       z_funcs.cachedSelect(slot);
  80.       r.dropDown();
  81.     end
  82.   end
  83.   z_funcs.cachedSelect(1);
  84. end
  85.  
  86. function z_funcs.getFromChest(itemName,localSlot,direction,count)
  87.   local direction = direction or s.down;
  88.   local localSlot = localSlot or 1;
  89.   local count = count or 1;
  90.  
  91.   z_funcs.writeStr("Trying to get "..count.." "..itemName.." from chest...")
  92.  
  93.   if c.isAvailable("inventory_controller") then
  94.     while not c.inventory_controller.getInventorySize(direction) do
  95.       z_funcs.writeStr("Cannot find chest?");
  96.       os.sleep(5);
  97.     end
  98.   end
  99.  
  100.   z_funcs.cachedSelect(localSlot);
  101.   if r.count(localSlot) > 0 then
  102.     for i=1,r.inventorySize() do
  103.       if not i == localSlot then
  104.         if r.count(i) == 0 then
  105.           r.transferTo(i);
  106.           break;  
  107.         end
  108.       end
  109.     end
  110.   end
  111.  
  112.   for i=1,c.inventory_controller.getInventorySize(direction) do
  113.     local itemInfo = c.inventory_controller.getStackInSlot(direction,i);
  114.     if itemInfo then
  115.       if itemName == itemInfo.name then
  116.         return c.inventory_controller.suckFromSlot(direction, i, count);
  117.       end
  118.     end
  119.   end
  120.   return false;
  121. end
  122.  
  123. function z_funcs.findInLocalInv(itemName)
  124.   for i=1,r.inventorySize() do
  125.     local itemInfo = c.inventory_controller.getStackInInternalSlot(i);
  126.     if itemInfo then
  127.       if itemName == itemInfo.name then
  128.         return i;
  129.       end
  130.     end
  131.   end
  132. end
  133.  
  134. return z_funcs;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement