Leon3226

beeBreeder

Jul 22nd, 2021 (edited)
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local serializer = require('serialization');
  2.   local internet = require('internet');
  3.  
  4.  
  5.   local sleepIntervals = 20;
  6.   local maxPairs = 13 --due to size of Ender Chest
  7.   local adress = 'http://localhost:5001/Breeder';
  8.   local apiaryIdentifier = "niejuntmgfjmekswrf";
  9.   local debugging = true;
  10.  
  11.  
  12.   function allTransposers()
  13.     return component.list('transposer');
  14.   end
  15.  
  16.  
  17.   local bees = { apiaryIdentifier = apiaryIdentifier, list = {}};
  18.   local transposers = allTransposers();
  19.   local transposersFlushes = {};
  20.   local transposersBreedInvs = {};
  21.  
  22.   function isApiary(transposer, side)
  23.     return transposer.getInventorySize(side) == 12 and transposer.getInventoryName(side) == 'forestry:apiary';
  24.   end
  25.  
  26.   function isEnderChest(transposer, side)
  27.     return transposer.getInventorySize(side) == 27 and transposer.getInventoryName(side) == 'enderstorage:ender_storage';
  28.   end
  29.  
  30.   function isFlush(transposer, side)
  31.     return transposer.getInventoryName(side) == 'extrautils2:trashcan';
  32.   end
  33.  
  34.   function isBreedInv(transposer, side)
  35.     return isEnderChest(transposer, side);
  36.   end
  37.  
  38.   function isEnderChest(transposer, side)
  39.     return transposer.getInventorySize(side) == 27 and transposer.getInventoryName(side) == 'enderstorage:ender_storage';
  40.   end
  41.  
  42.   function isApiaryChest(transposer, side)
  43.     return transposer.getInventorySize(side) == 125 and transposer.getInventoryName(side) == 'forestry:bee_chest';
  44.   end
  45.  
  46.   function serialize(obj)
  47.     return serializer.serialize(obj);
  48.   end
  49.  
  50.   function unserialize(obj)
  51.     return serializer.unserialize(obj);
  52.   end
  53.  
  54.   function updateFlushes()
  55.     for adress in transposers
  56.     do
  57.       local transposer = component.proxy(adress);
  58.       for side=0,5
  59.       do
  60.        if (isFlush(transposer, side)) then        
  61.         transposersFlushes[adress] = side;
  62.         break;
  63.       end
  64.     end
  65.   end
  66.   end
  67.  
  68.   function updateBreedInvs()
  69.     for adress in transposers do
  70.       local transposer = component.proxy(adress);
  71.       for side=0,5
  72.       do
  73.        if (isBreedInv(transposer, side)) then        
  74.         transposersBreedInvs[adress] = side;
  75.         break;
  76.       end
  77.     end
  78.   end
  79.   end
  80.  
  81.   function isAnalyzedBee(item)
  82.     return item.individual ~= nil and item.individual.type == 'bee' and item.individual.isAnalyzed;
  83.   end
  84.  
  85.   function debugPrint(debugString)
  86.     if (debugging) then
  87.       print(debugString);
  88.     end
  89.   end
  90.  
  91.   function debugWrite(debugString)
  92.     if (debugging) then
  93.       io.write(debugString);
  94.     end
  95.   end
  96.  
  97.   function apiaryIsEmpty(apiary)
  98.     return apiary ~= nil and apiary[1].label =='Air' and apiary[2].label == 'Air'
  99.   end
  100.  
  101.   function reqestServerForInstructions(request)
  102.     local res = internet.request(adress, request, {['Content-Type'] = 'application/json'}, 'POST');
  103.  
  104.     local response = '';
  105.     for a in res do
  106.       response = response .. a;
  107.     end
  108.     return unserialize(response);
  109.   end
  110.  
  111.   function fillBees()
  112.     bees.list = {};
  113.     for adress in transposers
  114.     do
  115.       local transposer = component.proxy(adress);
  116.       for side=0,5
  117.       do
  118.        if (isApiaryChest(transposer, side)) then        
  119.         local sideStacks = transposer.getAllStacks(side);
  120.         if(sideStacks ~= nil ) then
  121.           for i = 1, 125 do
  122.             if  (isAnalyzedBee(sideStacks[i])) then
  123.               bees.list[adress..'-'..side..'-'..i] = { beePosition = {trans = adress, side = side, slot = i }, bee = sideStacks[i]};
  124.             end
  125.           end  
  126.         end    
  127.       end
  128.     end
  129.   end
  130.  
  131.   function placeBreedingBees(instructions)
  132.     debugPrint('Placing bee breeds from instructions...')
  133.     local i = 1;
  134.     if (instructions == nil) then return end
  135.     for n, pair in pairs(instructions)
  136.     do
  137.       debugPrint(n);
  138.       local transposer = component.proxy(pair.princess.position.trans);
  139.       local invSide = transposersBreedInvs[pair.princess.position.trans];
  140.       if(transposer == nil or invSide == nil) then  goto continue end;
  141.       debugWrite(pair.princess.position.trans .. ' ' .. invSide .. ' | ');
  142.       local breedInvStacks = transposer.getAllStacks(invSide);
  143.       repeat
  144.         if (breedInvStacks[i*2-1].label =='Air' and breedInvStacks[i*2].label == 'Air')
  145.           then
  146.             break;
  147.           end
  148.           i = i + 1;
  149.         until(i > maxPairs)
  150.         if(i > maxPairs) then return end
  151.  
  152.         transposer.transferItem(pair.princess.position.side, invSide, 1,  pair.princess.position.slot, i*2-1);
  153.         debugPrint(pair.princess.position.side .. ' ' .. pair.princess.position.slot .. ' ' .. 1 .. ' ' .. invSide .. ' ' .. i*2-1);
  154.  
  155.         transposer = component.proxy(pair.drone.position.trans);
  156.         invSide = transposersBreedInvs[pair.drone.position.trans];
  157.         if(transposer == nil or invSide == nil) then  goto continue end;
  158.         debugWrite(pair.princess.position.trans .. ' ' .. invSide .. ' | ');
  159.  
  160.         transposer.transferItem(pair.drone.position.side, invSide, 1, pair.drone.position.slot, i*2);
  161.         debugPrint(pair.drone.position.side .. ' ' .. pair.drone.position.slot .. ' ' .. 1 .. ' ' .. invSide .. ' ' .. i*2);
  162.         i = i + 1;
  163.         ::continue::
  164.       end
  165.     end
  166.   end
  167.  
  168.   function dispatchBreedingBees()
  169.     debugPrint('Dispatching bees to the avaliable apiaries...');
  170.     local i = 1;
  171.     for adress in transposers
  172.     do
  173.       local transposer = component.proxy(adress);
  174.       for side=0,5
  175.       do
  176.        if (isApiary(transposer, side)) then        
  177.         debugWrite(' Found an apiary, checking... ');
  178.         local sideStacks = transposer.getAllStacks(side);
  179.         if(apiaryIsEmpty(sideStacks)) then
  180.           debugPrint('Empty.')
  181.           debugPrint('  Checking for bees...');
  182.           local invSide = transposersBreedInvs[adress];
  183.           local breedInvStacks = transposer.getAllStacks(invSide);
  184.           repeat
  185.             if (isAnalyzedBee(breedInvStacks[i*2-1]) and isAnalyzedBee(breedInvStacks[i*2])) then
  186.               debugPrint('  Found bees at ' .. i*2-1 .. ' and ' .. i*2 .. ', attempting to transfer...');
  187.               transposer.transferItem(invSide, side, 1,  i*2-1, 1);
  188.               debugPrint('  Transfering princess: '..invSide .. ' ' .. side .. ' ' .. 1 .. ' ' .. i*2-1 .. ' ' .. 1);
  189.  
  190.               transposer.transferItem(invSide, side, 1, i*2, 2);
  191.               debugPrint('  Transfering drone: '..invSide .. ' ' .. side .. ' ' .. 1 .. ' ' .. i*2 .. ' ' .. 2);
  192.               break;
  193.             else
  194.               debugPrint('  No avaliable pair found at ' .. i*2-1 .. ' and ' .. i*2);
  195.             end
  196.             i = i + 1;
  197.           until (i > maxPairs)
  198.           if(i > maxPairs) then return end
  199.         else
  200.           debugPrint('Busy.')
  201.         end
  202.       end    
  203.     end
  204.   end
  205.   end
  206.  
  207.  
  208.   function flushBees(instructions)
  209.     debugPrint('Flushing bees breeds from instructions...');
  210.   if (instructions == nil) then return end
  211.     for n, bee in pairs(instructions)
  212.     do
  213.       debugWrite(n .. '. ');
  214.       local transposer = component.proxy(bee.position.trans);
  215.       local invSide = transposersFlushes[bee.position.trans];
  216.       debugPrint(bee.position.trans .. ' ' .. invSide);
  217.       if(transposer == nil or invSide == nil) then  goto continue end;
  218.         local res = transposer.transferItem(bee.position.side, invSide, 64,  bee.position.slot, 1);
  219.         debugWrite('(' .. res .. ') ');
  220.         debugPrint(' Flushing drone: '.. bee.position.side .. ' ' .. invSide .. ' ' .. 1 .. ' ' .. bee.position.slot .. ' ' .. 1);
  221.         ::continue::
  222.       end
  223.   end
  224.  
  225.   repeat
  226.     debugPrint('===========================');
  227.     transposers = allTransposers();
  228.     fillBees();
  229.     updateFlushes();
  230.     updateBreedInvs();
  231.     local resultString = serialize(bees);
  232.     debugPrint('Sending ' .. #bees.list .. ' bees to the server...');
  233.     local serverInstructions = reqestServerForInstructions(resultString);
  234.     debugPrint(serialize(serverInstructions));
  235.     flushBees(serverInstructions.flush);
  236.     placeBreedingBees(serverInstructions.breed);
  237.     dispatchBreedingBees();
  238.  
  239.     debugPrint('===========================');
  240.     debugPrint('');
  241.  
  242.     os.sleep(sleepIntervals);
  243.   until(false);
  244.  
  245.  
RAW Paste Data