Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serializer = require('serialization');
- local internet = require('internet');
- local sleepIntervals = 20;
- local maxPairs = 13 --due to size of Ender Chest
- local adress = 'http://localhost:5001/Breeder';
- local apiaryIdentifier = "niejuntmgfjmekswrf";
- local debugging = true;
- function allTransposers()
- return component.list('transposer');
- end
- local bees = { apiaryIdentifier = apiaryIdentifier, list = {}};
- local transposers = allTransposers();
- local transposersFlushes = {};
- local transposersBreedInvs = {};
- function isApiary(transposer, side)
- return transposer.getInventorySize(side) == 12 and transposer.getInventoryName(side) == 'forestry:apiary';
- end
- function isEnderChest(transposer, side)
- return transposer.getInventorySize(side) == 27 and transposer.getInventoryName(side) == 'enderstorage:ender_storage';
- end
- function isFlush(transposer, side)
- return transposer.getInventoryName(side) == 'extrautils2:trashcan';
- end
- function isBreedInv(transposer, side)
- return isEnderChest(transposer, side);
- end
- function isEnderChest(transposer, side)
- return transposer.getInventorySize(side) == 27 and transposer.getInventoryName(side) == 'enderstorage:ender_storage';
- end
- function isApiaryChest(transposer, side)
- return transposer.getInventorySize(side) == 125 and transposer.getInventoryName(side) == 'forestry:bee_chest';
- end
- function serialize(obj)
- return serializer.serialize(obj);
- end
- function unserialize(obj)
- return serializer.unserialize(obj);
- end
- function updateFlushes()
- for adress in transposers
- do
- local transposer = component.proxy(adress);
- for side=0,5
- do
- if (isFlush(transposer, side)) then
- transposersFlushes[adress] = side;
- break;
- end
- end
- end
- end
- function updateBreedInvs()
- for adress in transposers do
- local transposer = component.proxy(adress);
- for side=0,5
- do
- if (isBreedInv(transposer, side)) then
- transposersBreedInvs[adress] = side;
- break;
- end
- end
- end
- end
- function isAnalyzedBee(item)
- return item.individual ~= nil and item.individual.type == 'bee' and item.individual.isAnalyzed;
- end
- function debugPrint(debugString)
- if (debugging) then
- print(debugString);
- end
- end
- function debugWrite(debugString)
- if (debugging) then
- io.write(debugString);
- end
- end
- function apiaryIsEmpty(apiary)
- return apiary ~= nil and apiary[1].label =='Air' and apiary[2].label == 'Air'
- end
- function reqestServerForInstructions(request)
- local res = internet.request(adress, request, {['Content-Type'] = 'application/json'}, 'POST');
- local response = '';
- for a in res do
- response = response .. a;
- end
- return unserialize(response);
- end
- function fillBees()
- bees.list = {};
- for adress in transposers
- do
- local transposer = component.proxy(adress);
- for side=0,5
- do
- if (isApiaryChest(transposer, side)) then
- local sideStacks = transposer.getAllStacks(side);
- if(sideStacks ~= nil ) then
- for i = 1, 125 do
- if (isAnalyzedBee(sideStacks[i])) then
- bees.list[adress..'-'..side..'-'..i] = { beePosition = {trans = adress, side = side, slot = i }, bee = sideStacks[i]};
- end
- end
- end
- end
- end
- end
- function placeBreedingBees(instructions)
- debugPrint('Placing bee breeds from instructions...')
- local i = 1;
- if (instructions == nil) then return end
- for n, pair in pairs(instructions)
- do
- debugPrint(n);
- local transposer = component.proxy(pair.princess.position.trans);
- local invSide = transposersBreedInvs[pair.princess.position.trans];
- if(transposer == nil or invSide == nil) then goto continue end;
- debugWrite(pair.princess.position.trans .. ' ' .. invSide .. ' | ');
- local breedInvStacks = transposer.getAllStacks(invSide);
- repeat
- if (breedInvStacks[i*2-1].label =='Air' and breedInvStacks[i*2].label == 'Air')
- then
- break;
- end
- i = i + 1;
- until(i > maxPairs)
- if(i > maxPairs) then return end
- transposer.transferItem(pair.princess.position.side, invSide, 1, pair.princess.position.slot, i*2-1);
- debugPrint(pair.princess.position.side .. ' ' .. pair.princess.position.slot .. ' ' .. 1 .. ' ' .. invSide .. ' ' .. i*2-1);
- transposer = component.proxy(pair.drone.position.trans);
- invSide = transposersBreedInvs[pair.drone.position.trans];
- if(transposer == nil or invSide == nil) then goto continue end;
- debugWrite(pair.princess.position.trans .. ' ' .. invSide .. ' | ');
- transposer.transferItem(pair.drone.position.side, invSide, 1, pair.drone.position.slot, i*2);
- debugPrint(pair.drone.position.side .. ' ' .. pair.drone.position.slot .. ' ' .. 1 .. ' ' .. invSide .. ' ' .. i*2);
- i = i + 1;
- ::continue::
- end
- end
- end
- function dispatchBreedingBees()
- debugPrint('Dispatching bees to the avaliable apiaries...');
- local i = 1;
- for adress in transposers
- do
- local transposer = component.proxy(adress);
- for side=0,5
- do
- if (isApiary(transposer, side)) then
- debugWrite(' Found an apiary, checking... ');
- local sideStacks = transposer.getAllStacks(side);
- if(apiaryIsEmpty(sideStacks)) then
- debugPrint('Empty.')
- debugPrint(' Checking for bees...');
- local invSide = transposersBreedInvs[adress];
- local breedInvStacks = transposer.getAllStacks(invSide);
- repeat
- if (isAnalyzedBee(breedInvStacks[i*2-1]) and isAnalyzedBee(breedInvStacks[i*2])) then
- debugPrint(' Found bees at ' .. i*2-1 .. ' and ' .. i*2 .. ', attempting to transfer...');
- transposer.transferItem(invSide, side, 1, i*2-1, 1);
- debugPrint(' Transfering princess: '..invSide .. ' ' .. side .. ' ' .. 1 .. ' ' .. i*2-1 .. ' ' .. 1);
- transposer.transferItem(invSide, side, 1, i*2, 2);
- debugPrint(' Transfering drone: '..invSide .. ' ' .. side .. ' ' .. 1 .. ' ' .. i*2 .. ' ' .. 2);
- break;
- else
- debugPrint(' No avaliable pair found at ' .. i*2-1 .. ' and ' .. i*2);
- end
- i = i + 1;
- until (i > maxPairs)
- if(i > maxPairs) then return end
- else
- debugPrint('Busy.')
- end
- end
- end
- end
- end
- function flushBees(instructions)
- debugPrint('Flushing bees breeds from instructions...');
- if (instructions == nil) then return end
- for n, bee in pairs(instructions)
- do
- debugWrite(n .. '. ');
- local transposer = component.proxy(bee.position.trans);
- local invSide = transposersFlushes[bee.position.trans];
- debugPrint(bee.position.trans .. ' ' .. invSide);
- if(transposer == nil or invSide == nil) then goto continue end;
- local res = transposer.transferItem(bee.position.side, invSide, 64, bee.position.slot, 1);
- debugWrite('(' .. res .. ') ');
- debugPrint(' Flushing drone: '.. bee.position.side .. ' ' .. invSide .. ' ' .. 1 .. ' ' .. bee.position.slot .. ' ' .. 1);
- ::continue::
- end
- end
- repeat
- debugPrint('===========================');
- transposers = allTransposers();
- fillBees();
- updateFlushes();
- updateBreedInvs();
- local resultString = serialize(bees);
- debugPrint('Sending ' .. #bees.list .. ' bees to the server...');
- local serverInstructions = reqestServerForInstructions(resultString);
- debugPrint(serialize(serverInstructions));
- flushBees(serverInstructions.flush);
- placeBreedingBees(serverInstructions.breed);
- dispatchBreedingBees();
- debugPrint('===========================');
- debugPrint('');
- os.sleep(sleepIntervals);
- until(false);
Add Comment
Please, Sign In to add comment